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 22 23 //#define TRACE_VIDEO 24 #ifdef TRACE_VIDEO 25 # define TRACE(x) dprintf x 26 #else 27 # define TRACE(x) ; 28 #endif 29 30 31 struct video_mode { 32 list_link link; 33 UINTN mode; 34 UINTN width, height, bits_per_pixel, bytes_per_row; 35 }; 36 37 38 static EFI_GUID sGraphicsOutputGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 39 static EFI_GRAPHICS_OUTPUT_PROTOCOL *sGraphicsOutput; 40 static UINTN sGraphicsMode; 41 static struct list sModeList; 42 static uint32 sModeCount; 43 static bool sModeChosen; 44 static bool sSettingsLoaded; 45 46 47 static int 48 compare_video_modes(video_mode *a, video_mode *b) 49 { 50 int compare = a->width - b->width; 51 if (compare != 0) 52 return compare; 53 54 compare = a->height - b->height; 55 if (compare != 0) 56 return compare; 57 58 return a->bits_per_pixel - b->bits_per_pixel; 59 } 60 61 62 static void 63 add_video_mode(video_mode *videoMode) 64 { 65 video_mode *mode = NULL; 66 while ((mode = (video_mode*)list_get_next_item(&sModeList, mode)) 67 != NULL) { 68 int compare = compare_video_modes(videoMode, mode); 69 if (compare == 0) { 70 // mode already exists 71 return; 72 } 73 74 if (compare > 0) 75 break; 76 } 77 78 list_insert_item_before(&sModeList, mode, videoMode); 79 sModeCount++; 80 } 81 82 83 static video_mode* 84 closest_video_mode(uint32 width, uint32 height, uint32 depth) 85 { 86 video_mode *bestMode = NULL; 87 uint32 bestDiff = 0; 88 89 video_mode *mode = NULL; 90 while ((mode = (video_mode*)list_get_next_item(&sModeList, mode)) != NULL) { 91 if (mode->width > width) { 92 // Only choose modes with a width less or equal than the searched 93 // one; or else it might well be that the monitor cannot keep up. 94 continue; 95 } 96 97 uint32 diff = 2 * abs(mode->width - width) + abs(mode->height - height) 98 + abs(mode->bits_per_pixel - depth); 99 100 if (bestMode == NULL || bestDiff > diff) { 101 bestMode = mode; 102 bestDiff = diff; 103 } 104 } 105 106 return bestMode; 107 } 108 109 110 static void 111 get_mode_from_settings(void) 112 { 113 if (sSettingsLoaded) 114 return; 115 116 void *handle = load_driver_settings("vesa"); 117 if (handle == NULL) 118 return; 119 120 bool found = false; 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 (strcmp(parameter.name, "mode") == 0 && parameter.value_count > 2) { 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 found = true; 140 sGraphicsMode = mode->mode; 141 } 142 } 143 } 144 145 out: 146 unload_driver_settings(handle); 147 } 148 149 150 extern "C" status_t 151 platform_init_video(void) 152 { 153 list_init(&sModeList); 154 155 // we don't support VESA modes or EDID 156 gKernelArgs.vesa_modes = NULL; 157 gKernelArgs.vesa_modes_size = 0; 158 gKernelArgs.edid_info = NULL; 159 160 // make a guess at the best video mode to use, and save the mode ID 161 // for switching to graphics mode 162 EFI_STATUS status = kBootServices->LocateProtocol(&sGraphicsOutputGuid, 163 NULL, (void **)&sGraphicsOutput); 164 if (sGraphicsOutput == NULL || status != EFI_SUCCESS) { 165 gKernelArgs.frame_buffer.enabled = false; 166 sGraphicsOutput = NULL; 167 return B_ERROR; 168 } 169 170 UINTN bestArea = 0; 171 UINTN bestDepth = 0; 172 173 TRACE(("looking for best graphics mode...\n")); 174 175 for (UINTN mode = 0; mode < sGraphicsOutput->Mode->MaxMode; ++mode) { 176 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info; 177 UINTN size, depth; 178 sGraphicsOutput->QueryMode(sGraphicsOutput, mode, &size, &info); 179 UINTN 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 = item->Submenu(); 267 item = menu->FindMarked(); 268 if (item != NULL) { 269 sGraphicsMode = (UINTN)item->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