1 /* 2 * Copyright 2016, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "video.h" 8 9 #include <stdlib.h> 10 11 #include <boot/kernel_args.h> 12 #include <boot/menu.h> 13 #include <boot/platform.h> 14 #include <boot/platform/generic/video.h> 15 #include <boot/stage2.h> 16 #include <boot/stdio.h> 17 #include <drivers/driver_settings.h> 18 #include <util/list.h> 19 20 #include "efi_platform.h" 21 #include <efi/protocol/graphics-output.h> 22 23 24 //#define TRACE_VIDEO 25 #ifdef TRACE_VIDEO 26 # define TRACE(x) dprintf x 27 #else 28 # define TRACE(x) ; 29 #endif 30 31 32 struct video_mode { 33 list_link link; 34 size_t mode; 35 size_t width, height, bits_per_pixel, bytes_per_row; 36 }; 37 38 39 static efi_guid sGraphicsOutputGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 40 static efi_graphics_output_protocol *sGraphicsOutput; 41 static size_t sGraphicsMode; 42 static struct list sModeList; 43 static uint32 sModeCount; 44 static bool sModeChosen; 45 static bool sSettingsLoaded; 46 47 48 static int 49 compare_video_modes(video_mode *a, video_mode *b) 50 { 51 int compare = a->width - b->width; 52 if (compare != 0) 53 return compare; 54 55 compare = a->height - b->height; 56 if (compare != 0) 57 return compare; 58 59 return a->bits_per_pixel - b->bits_per_pixel; 60 } 61 62 63 static void 64 add_video_mode(video_mode *videoMode) 65 { 66 video_mode *mode = NULL; 67 while ((mode = (video_mode*)list_get_next_item(&sModeList, mode)) 68 != NULL) { 69 int compare = compare_video_modes(videoMode, mode); 70 if (compare == 0) { 71 // mode already exists 72 return; 73 } 74 75 if (compare > 0) 76 break; 77 } 78 79 list_insert_item_before(&sModeList, mode, videoMode); 80 sModeCount++; 81 } 82 83 84 static video_mode* 85 closest_video_mode(uint32 width, uint32 height, uint32 depth) 86 { 87 video_mode *bestMode = NULL; 88 int64 bestDiff = 0; 89 90 video_mode *mode = NULL; 91 while ((mode = (video_mode*)list_get_next_item(&sModeList, mode)) != NULL) { 92 if (mode->width > width) { 93 // Only choose modes with a width less or equal than the searched 94 // one; or else it might well be that the monitor cannot keep up. 95 continue; 96 } 97 98 int64 diff = 2 * abs((int64)mode->width - width) 99 + abs((int64)mode->height - height) 100 + abs((int64)mode->bits_per_pixel - depth); 101 102 if (bestMode == NULL || bestDiff > diff) { 103 bestMode = mode; 104 bestDiff = diff; 105 } 106 } 107 108 return bestMode; 109 } 110 111 112 static void 113 get_mode_from_settings(void) 114 { 115 if (sSettingsLoaded) 116 return; 117 118 void *handle = load_driver_settings("vesa"); 119 if (handle == NULL) 120 return; 121 122 const driver_settings *settings = get_driver_settings(handle); 123 if (settings == NULL) 124 goto out; 125 126 sSettingsLoaded = true; 127 128 for (int32 i = 0; i < settings->parameter_count; i++) { 129 driver_parameter ¶meter = settings->parameters[i]; 130 131 if (parameter.value_count < 3 || strcmp(parameter.name, "mode") != 0) continue; 132 uint32 width = strtoul(parameter.values[0], NULL, 0); 133 uint32 height = strtoul(parameter.values[1], NULL, 0); 134 uint32 depth = strtoul(parameter.values[2], NULL, 0); 135 136 // search mode that fits 137 video_mode *mode = closest_video_mode(width, height, depth); 138 if (mode != NULL) { 139 sGraphicsMode = mode->mode; 140 break; 141 } 142 } 143 144 out: 145 unload_driver_settings(handle); 146 } 147 148 149 extern "C" status_t 150 platform_init_video(void) 151 { 152 list_init(&sModeList); 153 154 // we don't support VESA modes or EDID 155 gKernelArgs.vesa_modes = NULL; 156 gKernelArgs.vesa_modes_size = 0; 157 gKernelArgs.edid_info = NULL; 158 159 // make a guess at the best video mode to use, and save the mode ID 160 // for switching to graphics mode 161 efi_status status = kBootServices->LocateProtocol(&sGraphicsOutputGuid, 162 NULL, (void **)&sGraphicsOutput); 163 if (sGraphicsOutput == NULL || status != EFI_SUCCESS) { 164 dprintf("GOP protocol not found\n"); 165 gKernelArgs.frame_buffer.enabled = false; 166 sGraphicsOutput = NULL; 167 return B_ERROR; 168 } 169 170 size_t bestArea = 0; 171 size_t bestDepth = 0; 172 173 TRACE(("looking for best graphics mode...\n")); 174 175 for (size_t mode = 0; mode < sGraphicsOutput->Mode->MaxMode; ++mode) { 176 efi_graphics_output_mode_information *info; 177 size_t size, depth; 178 sGraphicsOutput->QueryMode(sGraphicsOutput, mode, &size, &info); 179 size_t area = info->HorizontalResolution * info->VerticalResolution; 180 TRACE((" mode: %lu\n", mode)); 181 TRACE((" width: %u\n", info->HorizontalResolution)); 182 TRACE((" height: %u\n", info->VerticalResolution)); 183 TRACE((" area: %lu\n", area)); 184 if (info->PixelFormat == PixelRedGreenBlueReserved8BitPerColor) { 185 depth = 32; 186 } else if (info->PixelFormat == PixelBlueGreenRedReserved8BitPerColor) { 187 // seen this in the wild, but acts like RGB, go figure... 188 depth = 32; 189 } else if (info->PixelFormat == PixelBitMask 190 && info->PixelInformation.RedMask == 0xFF0000 191 && info->PixelInformation.GreenMask == 0x00FF00 192 && info->PixelInformation.BlueMask == 0x0000FF 193 && info->PixelInformation.ReservedMask == 0) { 194 depth = 24; 195 } else { 196 TRACE((" pixel format: %x unsupported\n", 197 info->PixelFormat)); 198 continue; 199 } 200 TRACE((" depth: %lu\n", depth)); 201 202 video_mode *videoMode = (video_mode*)malloc(sizeof(struct video_mode)); 203 if (videoMode != NULL) { 204 videoMode->mode = mode; 205 videoMode->width = info->HorizontalResolution; 206 videoMode->height = info->VerticalResolution; 207 videoMode->bits_per_pixel = info->PixelFormat == PixelBitMask ? 24 : 32; 208 videoMode->bytes_per_row = info->PixelsPerScanLine * depth / 8; 209 add_video_mode(videoMode); 210 } 211 212 area *= depth; 213 TRACE((" area (w/depth): %lu\n", area)); 214 if (area >= bestArea) { 215 TRACE(("selected new best mode: %lu\n", mode)); 216 bestArea = area; 217 bestDepth = depth; 218 sGraphicsMode = mode; 219 } 220 } 221 222 if (bestArea == 0 || bestDepth == 0) { 223 sGraphicsOutput = NULL; 224 gKernelArgs.frame_buffer.enabled = false; 225 return B_ERROR; 226 } 227 228 gKernelArgs.frame_buffer.enabled = true; 229 sModeChosen = false; 230 sSettingsLoaded = false; 231 return B_OK; 232 } 233 234 235 extern "C" void 236 platform_switch_to_logo(void) 237 { 238 if (sGraphicsOutput == NULL || !gKernelArgs.frame_buffer.enabled) 239 return; 240 241 if (!sModeChosen) 242 get_mode_from_settings(); 243 244 sGraphicsOutput->SetMode(sGraphicsOutput, sGraphicsMode); 245 gKernelArgs.frame_buffer.physical_buffer.start = 246 sGraphicsOutput->Mode->FrameBufferBase; 247 gKernelArgs.frame_buffer.physical_buffer.size = 248 sGraphicsOutput->Mode->FrameBufferSize; 249 gKernelArgs.frame_buffer.width = 250 sGraphicsOutput->Mode->Info->HorizontalResolution; 251 gKernelArgs.frame_buffer.height = 252 sGraphicsOutput->Mode->Info->VerticalResolution; 253 gKernelArgs.frame_buffer.depth = 254 sGraphicsOutput->Mode->Info->PixelFormat == PixelBitMask ? 24 : 32; 255 gKernelArgs.frame_buffer.bytes_per_row = 256 sGraphicsOutput->Mode->Info->PixelsPerScanLine 257 * gKernelArgs.frame_buffer.depth / 8; 258 259 video_display_splash(gKernelArgs.frame_buffer.physical_buffer.start); 260 } 261 262 263 bool 264 video_mode_hook(Menu *menu, MenuItem *item) 265 { 266 Menu* submenu = item->Submenu(); 267 MenuItem* subitem = submenu->FindMarked(); 268 if (subitem != NULL) { 269 sGraphicsMode = (size_t)subitem->Data(); 270 sModeChosen = true; 271 } 272 273 return true; 274 } 275 276 277 Menu* 278 video_mode_menu() 279 { 280 Menu *menu = new(std::nothrow)Menu(CHOICE_MENU, "Select Video Mode"); 281 MenuItem *item; 282 283 video_mode *mode = NULL; 284 while ((mode = (video_mode*)list_get_next_item(&sModeList, mode)) != NULL) { 285 char label[64]; 286 snprintf(label, sizeof(label), "%lux%lu %lu bit", mode->width, 287 mode->height, mode->bits_per_pixel); 288 289 menu->AddItem(item = new (std::nothrow)MenuItem(label)); 290 item->SetData((const void*)mode->mode); 291 if (mode->mode == sGraphicsMode) { 292 item->SetMarked(true); 293 item->Select(true); 294 } 295 } 296 297 menu->AddSeparatorItem(); 298 menu->AddItem(item = new(std::nothrow)MenuItem("Return to main menu")); 299 item->SetType(MENU_ITEM_NO_CHOICE); 300 301 return menu; 302 } 303 304 305 extern "C" void 306 platform_blit4(addr_t frameBuffer, const uint8 *data, 307 uint16 width, uint16 height, uint16 imageWidth, 308 uint16 left, uint16 top) 309 { 310 panic("platform_blit4 unsupported"); 311 return; 312 } 313 314 315 extern "C" void 316 platform_set_palette(const uint8 *palette) 317 { 318 panic("platform_set_palette unsupported"); 319 return; 320 } 321