1 /* 2 * Copyright 2007-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <create_display_modes.h> 8 9 #include <math.h> 10 #include <stdlib.h> 11 #include <string.h> 12 13 #include <video_overlay.h> 14 15 16 #define POSITIVE_SYNC \ 17 (B_POSITIVE_HSYNC | B_POSITIVE_VSYNC) 18 #define MODE_FLAGS \ 19 (B_8_BIT_DAC | B_HARDWARE_CURSOR | B_PARALLEL_ACCESS | B_DPMS | B_SUPPORTS_OVERLAYS) 20 21 // TODO: move this list into the app_server 22 static const display_mode kBaseModeList[] = { 23 {{25175, 640, 656, 752, 800, 350, 387, 389, 449, B_POSITIVE_HSYNC}, B_CMAP8, 640, 350, 0, 0, MODE_FLAGS}, /* 640x350 - www.epanorama.net/documents/pc/vga_timing.html) */ 24 25 {{25175, 640, 656, 752, 800, 400, 412, 414, 449, B_POSITIVE_VSYNC}, B_CMAP8, 640, 400, 0, 0, MODE_FLAGS}, /* 640x400 - www.epanorama.net/documents/pc/vga_timing.html) */ 26 27 {{25175, 640, 656, 752, 800, 480, 490, 492, 525, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(640X480X8.Z1) */ 28 {{27500, 640, 672, 768, 864, 480, 488, 494, 530, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* 640X480X60Hz */ 29 {{30500, 640, 672, 768, 864, 480, 517, 523, 588, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* SVGA_640X480X60HzNI */ 30 {{31500, 640, 664, 704, 832, 480, 489, 492, 520, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@70-72Hz_(640X480X8.Z1) */ 31 {{31500, 640, 656, 720, 840, 480, 481, 484, 500, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(640X480X8.Z1) */ 32 {{36000, 640, 696, 752, 832, 480, 481, 484, 509, 0}, B_CMAP8, 640, 480, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@85Hz_(640X480X8.Z1) */ 33 34 {{29580, 800, 816, 896, 992, 480, 481, 484, 497, B_POSITIVE_VSYNC}, B_CMAP8, 800, 480, 0, 0, MODE_FLAGS}, /* 800x480x60Hz */ 35 36 {{38100, 800, 832, 960, 1088, 600, 602, 606, 620, 0}, B_CMAP8, 800, 600, 0, 0, MODE_FLAGS}, /* SVGA_800X600X56HzNI */ 37 {{40000, 800, 840, 968, 1056, 600, 601, 605, 628, POSITIVE_SYNC}, B_CMAP8, 800, 600, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(800X600X8.Z1) */ 38 {{49500, 800, 816, 896, 1056, 600, 601, 604, 625, POSITIVE_SYNC}, B_CMAP8, 800, 600, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(800X600X8.Z1) */ 39 {{50000, 800, 856, 976, 1040, 600, 637, 643, 666, POSITIVE_SYNC}, B_CMAP8, 800, 600, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@70-72Hz_(800X600X8.Z1) */ 40 {{56250, 800, 832, 896, 1048, 600, 601, 604, 631, POSITIVE_SYNC}, B_CMAP8, 800, 600, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@85Hz_(800X600X8.Z1) */ 41 42 {{65000, 1024, 1048, 1184, 1344, 768, 771, 777, 806, 0}, B_CMAP8, 1024, 768, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1024X768X8.Z1) */ 43 {{75000, 1024, 1048, 1184, 1328, 768, 771, 777, 806, 0}, B_CMAP8, 1024, 768, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@70-72Hz_(1024X768X8.Z1) */ 44 {{78750, 1024, 1040, 1136, 1312, 768, 769, 772, 800, POSITIVE_SYNC}, B_CMAP8, 1024, 768, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(1024X768X8.Z1) */ 45 {{94500, 1024, 1072, 1168, 1376, 768, 769, 772, 808, POSITIVE_SYNC}, B_CMAP8, 1024, 768, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@85Hz_(1024X768X8.Z1) */ 46 47 {{81640, 1152, 1216, 1336, 1520, 864, 865, 868, 895, POSITIVE_SYNC}, B_CMAP8, 1152, 864, 0, 0, MODE_FLAGS}, /* 1152x864x60Hz */ 48 {{94200, 1152, 1184, 1280, 1472, 864, 865, 868, 914, POSITIVE_SYNC}, B_CMAP8, 1152, 864, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@70Hz_(1152X864X8.Z1) */ 49 {{108000, 1152, 1216, 1344, 1600, 864, 865, 868, 900, POSITIVE_SYNC}, B_CMAP8, 1152, 864, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(1152X864X8.Z1) */ 50 {{121500, 1152, 1216, 1344, 1568, 864, 865, 868, 911, POSITIVE_SYNC}, B_CMAP8, 1152, 864, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@85Hz_(1152X864X8.Z1) */ 51 52 {{74520, 1280, 1368, 1424, 1656, 720, 724, 730, 750, POSITIVE_SYNC}, B_CMAP8, 1280, 720, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1280X720) */ 53 54 {{83460, 1280, 1344, 1480, 1680, 800, 801, 804, 828, B_POSITIVE_VSYNC}, B_CMAP8, 1280, 800, 0, 0, MODE_FLAGS}, /* WXGA (1280x800x60) */ 55 56 {{108000, 1280, 1328, 1440, 1688, 1024, 1025, 1028, 1066, POSITIVE_SYNC}, B_CMAP8, 1280, 1024, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1280X1024X8.Z1) */ 57 {{135000, 1280, 1296, 1440, 1688, 1024, 1025, 1028, 1066, POSITIVE_SYNC}, B_CMAP8, 1280, 1024, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(1280X1024X8.Z1) */ 58 {{157500, 1280, 1344, 1504, 1728, 1024, 1025, 1028, 1072, POSITIVE_SYNC}, B_CMAP8, 1280, 1024, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@85Hz_(1280X1024X8.Z1) */ 59 60 {{122600, 1400, 1488, 1640, 1880, 1050, 1051, 1054, 1087, POSITIVE_SYNC}, B_CMAP8, 1400, 1050, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1400X1050) */ 61 {{155800, 1400, 1464, 1784, 1912, 1050, 1052, 1064, 1090, POSITIVE_SYNC}, B_CMAP8, 1400, 1050, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(1400X1050) */ 62 63 {{106500, 1440, 1520, 1672, 1904, 900, 901, 904, 932, POSITIVE_SYNC}, B_CMAP8, 1440, 900, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1440X900) */ 64 65 {{162000, 1600, 1664, 1856, 2160, 1200, 1201, 1204, 1250, POSITIVE_SYNC}, B_CMAP8, 1600, 1200, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1600X1200X8.Z1) */ 66 {{175500, 1600, 1664, 1856, 2160, 1200, 1201, 1204, 1250, POSITIVE_SYNC}, B_CMAP8, 1600, 1200, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@65Hz_(1600X1200X8.Z1) */ 67 {{189000, 1600, 1664, 1856, 2160, 1200, 1201, 1204, 1250, POSITIVE_SYNC}, B_CMAP8, 1600, 1200, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@70Hz_(1600X1200X8.Z1) */ 68 {{202500, 1600, 1664, 1856, 2160, 1200, 1201, 1204, 1250, POSITIVE_SYNC}, B_CMAP8, 1600, 1200, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@75Hz_(1600X1200X8.Z1) */ 69 {{216000, 1600, 1664, 1856, 2160, 1200, 1201, 1204, 1250, POSITIVE_SYNC}, B_CMAP8, 1600, 1200, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@80Hz_(1600X1200X8.Z1) */ 70 {{229500, 1600, 1664, 1856, 2160, 1200, 1201, 1204, 1250, POSITIVE_SYNC}, B_CMAP8, 1600, 1200, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@85Hz_(1600X1200X8.Z1) */ 71 72 {{147100, 1680, 1784, 1968, 2256, 1050, 1051, 1054, 1087, POSITIVE_SYNC}, B_CMAP8, 1680, 1050, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1680X1050) */ 73 74 {{160000, 1920, 2010, 2060, 2110, 1200, 1202, 1208, 1235, POSITIVE_SYNC}, B_CMAP8, 1920, 1200, 0, 0, MODE_FLAGS}, /* Vesa_Monitor_@60Hz_(1920X1200) */ 75 }; 76 static const uint32 kNumBaseModes = sizeof(kBaseModeList) / sizeof(display_mode); 77 78 79 namespace BPrivate { 80 81 class ModeList { 82 public: 83 ModeList(); 84 ~ModeList(); 85 86 bool AddModes(edid1_info* info); 87 bool AddModes(const display_mode* modes, uint32 count); 88 89 bool CreateColorSpaces(const color_space* spaces, uint32 count); 90 void Filter(check_display_mode_hook hook); 91 void Clean(); 92 93 const display_mode* Modes() const { return fModes; } 94 uint32 Count() const { return fCount; } 95 96 private: 97 bool _MakeSpace(uint32 count); 98 bool _AddMode(const display_mode* mode); 99 void _RemoveModeAt(uint32 index); 100 void _AddBaseMode(uint16 width, uint16 height, uint32 refresh); 101 102 display_mode* fModes; 103 uint32 fCount; 104 uint32 fCapacity; 105 }; 106 107 } // namespace BPrivate 108 109 using namespace BPrivate; 110 111 112 static float 113 get_refresh_rate(const display_mode& mode) 114 { 115 return float(mode.timing.pixel_clock * 1000) / 116 float(mode.timing.h_total * mode.timing.v_total); 117 } 118 119 120 static int 121 compare_mode(const void* _mode1, const void* _mode2) 122 { 123 display_mode *mode1 = (display_mode *)_mode1; 124 display_mode *mode2 = (display_mode *)_mode2; 125 uint16 width1, width2, height1, height2; 126 127 width1 = mode1->virtual_width; 128 height1 = mode1->virtual_height; 129 width2 = mode2->virtual_width; 130 height2 = mode2->virtual_height; 131 132 if (width1 != width2) 133 return width1 - width2; 134 135 if (height1 != height2) 136 return height1 - height2; 137 138 if (mode1->space != mode2->space) 139 return mode1->space - mode2->space; 140 141 return (int)(100 * (get_refresh_rate(*mode1) - get_refresh_rate(*mode2))); 142 } 143 144 145 // #pragma mark - 146 147 148 ModeList::ModeList() 149 : 150 fModes(NULL), 151 fCount(0), 152 fCapacity(0) 153 { 154 } 155 156 157 ModeList::~ModeList() 158 { 159 free(fModes); 160 } 161 162 163 bool 164 ModeList::AddModes(edid1_info* info) 165 { 166 if (info->established_timing.res_720x400x70) 167 _AddBaseMode(720, 400, 70); 168 if (info->established_timing.res_720x400x88) 169 _AddBaseMode(720, 400, 88); 170 171 if (info->established_timing.res_640x480x60) 172 _AddBaseMode(640, 480, 60); 173 if (info->established_timing.res_640x480x67) 174 _AddBaseMode(640, 480, 67); 175 if (info->established_timing.res_640x480x72) 176 _AddBaseMode(640, 480, 72); 177 if (info->established_timing.res_640x480x75) 178 _AddBaseMode(640, 480, 75); 179 180 if (info->established_timing.res_800x600x56) 181 _AddBaseMode(800, 600, 56); 182 if (info->established_timing.res_800x600x60) 183 _AddBaseMode(800, 600, 60); 184 if (info->established_timing.res_800x600x72) 185 _AddBaseMode(800, 600, 72); 186 if (info->established_timing.res_800x600x75) 187 _AddBaseMode(800, 600, 75); 188 189 #if 0 190 if (info->established_timing.res_832x624x75) 191 _AddBaseMode(832, 624, 75); 192 193 if (info->established_timing.res_1024x768x87i) 194 _AddBaseMode(1024, 768, 87); 195 #endif 196 if (info->established_timing.res_1024x768x60) 197 _AddBaseMode(1024, 768, 60); 198 if (info->established_timing.res_1024x768x70) 199 _AddBaseMode(1024, 768, 70); 200 if (info->established_timing.res_1024x768x75) 201 _AddBaseMode(1024, 768, 75); 202 203 if (info->established_timing.res_1152x870x75) 204 _AddBaseMode(1152, 870, 75); 205 if (info->established_timing.res_1280x1024x75) 206 _AddBaseMode(1280, 1024, 75); 207 208 for (uint32 i = 0; i < EDID1_NUM_STD_TIMING; ++i) { 209 if (info->std_timing[i].h_size <= 256) 210 continue; 211 212 _AddBaseMode(info->std_timing[i].h_size, info->std_timing[i].v_size, 213 info->std_timing[i].refresh); 214 } 215 216 for (uint32 i = 0; i < EDID1_NUM_DETAILED_MONITOR_DESC; ++i) { 217 if (info->detailed_monitor[i].monitor_desc_type != EDID1_IS_DETAILED_TIMING) 218 continue; 219 220 // TODO: handle flags correctly! 221 const edid1_detailed_timing& timing = info->detailed_monitor[i].data.detailed_timing; 222 display_mode mode; 223 224 if (timing.pixel_clock <= 0 || timing.sync != 3) 225 continue; 226 227 mode.timing.pixel_clock = timing.pixel_clock * 10; 228 mode.timing.h_display = timing.h_active; 229 mode.timing.h_sync_start = timing.h_active + timing.h_sync_off; 230 mode.timing.h_sync_end = mode.timing.h_sync_start + timing.h_sync_width; 231 mode.timing.h_total = timing.h_active + timing.h_blank; 232 mode.timing.v_display = timing.v_active; 233 mode.timing.v_sync_start = timing.v_active + timing.v_sync_off; 234 mode.timing.v_sync_end = mode.timing.v_sync_start + timing.v_sync_width; 235 mode.timing.v_total = timing.v_active + timing.v_blank; 236 mode.timing.flags = 0; 237 if (timing.sync == 3) { 238 if (timing.misc & 1) 239 mode.timing.flags |= B_POSITIVE_HSYNC; 240 if (timing.misc & 2) 241 mode.timing.flags |= B_POSITIVE_VSYNC; 242 } 243 if (timing.interlaced) 244 mode.timing.flags |= B_TIMING_INTERLACED; 245 mode.space = B_RGB32; 246 mode.virtual_width = timing.h_active; 247 mode.virtual_height = timing.v_active; 248 mode.h_display_start = 0; 249 mode.v_display_start = 0; 250 mode.flags = MODE_FLAGS; 251 252 _AddMode(&mode); 253 } 254 255 // TODO: add other modes from the base list that satisfy the display's 256 // requirements! 257 258 return true; 259 } 260 261 262 bool 263 ModeList::AddModes(const display_mode* modes, uint32 count) 264 { 265 if (!_MakeSpace(count)) 266 return false; 267 268 for (uint32 i = 0; i < count; i++) { 269 fModes[fCount++] = modes[i]; 270 } 271 272 return true; 273 } 274 275 276 bool 277 ModeList::CreateColorSpaces(const color_space* spaces, uint32 count) 278 { 279 uint32 modeCount = fCount; 280 281 for (uint32 i = 0; i < count; i++) { 282 if (i > 0 && !AddModes(fModes, modeCount)) 283 return false; 284 285 for (uint32 j = 0; j < modeCount; j++) { 286 fModes[j + fCount - modeCount].space = spaces[i]; 287 } 288 } 289 290 return true; 291 } 292 293 294 void 295 ModeList::Filter(check_display_mode_hook hook) 296 { 297 if (hook == NULL) 298 return; 299 300 for (uint32 i = fCount; i-- > 0;) { 301 if (!hook(&fModes[i])) 302 _RemoveModeAt(i); 303 } 304 } 305 306 307 void 308 ModeList::Clean() 309 { 310 // sort mode list 311 qsort(fModes, fCount, sizeof(display_mode), compare_mode); 312 313 // remove duplicates 314 for (uint32 i = fCount; i-- > 1;) { 315 if (compare_mode(&fModes[i], &fModes[i - 1]) == 0) 316 _RemoveModeAt(i); 317 } 318 } 319 320 321 void 322 ModeList::_AddBaseMode(uint16 width, uint16 height, uint32 refresh) 323 { 324 for (uint32 i = 0; i < kNumBaseModes; i++) { 325 const display_mode& mode = kBaseModeList[i]; 326 327 // Add mode if width and height match, and the computed refresh rate of 328 // the mode is within 1.2 percent of the refresh rate specified by the 329 // caller. Note that refresh rates computed from mode parameters is 330 // not exact; thus, the tolerance of 1.2% was obtained by testing the 331 // various established modes that can be selected by the EDID info. 332 333 if (mode.timing.h_display == width && mode.timing.v_display == height 334 && fabs(get_refresh_rate(mode) - refresh) < refresh * 0.012) { 335 _AddMode(&mode); 336 } 337 } 338 } 339 340 341 bool 342 ModeList::_MakeSpace(uint32 count) 343 { 344 if (fCount + count <= fCapacity) 345 return true; 346 347 uint32 capacity = (fCapacity + count + 0xf) & ~0xf; 348 display_mode* modes = (display_mode*)realloc(fModes, 349 capacity * sizeof(display_mode)); 350 if (modes == NULL) 351 return false; 352 353 fModes = modes; 354 fCapacity = capacity; 355 return true; 356 } 357 358 359 bool 360 ModeList::_AddMode(const display_mode* mode) 361 { 362 if (!_MakeSpace(1)) 363 return false; 364 365 fModes[fCount++] = *mode; 366 return true; 367 } 368 369 370 void 371 ModeList::_RemoveModeAt(uint32 index) 372 { 373 if (index < fCount - 1) { 374 memmove(&fModes[index], &fModes[index + 1], 375 (fCount - 1 - index) * sizeof(display_mode)); 376 } 377 378 fCount--; 379 } 380 381 382 // #pragma mark - 383 384 385 extern "C" area_id 386 create_display_modes(const char* name, edid1_info* edid, 387 const display_mode* initialModes, uint32 initialModeCount, 388 const color_space *spaces, uint32 spacesCount, 389 check_display_mode_hook hook, display_mode** _modes, uint32* _count) 390 { 391 if (_modes == NULL || _count == NULL) 392 return B_BAD_VALUE; 393 394 // compile initial mode list from the different sources 395 396 ModeList modes; 397 modes.AddModes(initialModes, initialModeCount); 398 399 if (edid != NULL) 400 modes.AddModes(edid); 401 else 402 modes.AddModes(kBaseModeList, kNumBaseModes); 403 404 // filter out modes the caller doesn't like, and multiply modes for 405 // every color space 406 407 if (spaces == NULL) { 408 const color_space kDefaultSpaces[] = {B_RGB32_LITTLE, B_RGB16_LITTLE, 409 B_RGB15_LITTLE, B_CMAP8}; 410 modes.CreateColorSpaces(kDefaultSpaces, 411 sizeof(kDefaultSpaces) / sizeof(kDefaultSpaces[0])); 412 } else 413 modes.CreateColorSpaces(spaces, spacesCount); 414 415 modes.Filter(hook); 416 modes.Clean(); 417 418 // create area for output modes 419 420 size_t size = (sizeof(display_mode) * modes.Count() + B_PAGE_SIZE - 1) 421 & ~(B_PAGE_SIZE - 1); 422 display_mode *list; 423 area_id area = create_area(name, (void **)&list, B_ANY_ADDRESS, 424 size, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA); 425 if (area < B_OK) 426 return area; 427 428 memcpy(list, modes.Modes(), sizeof(display_mode) * modes.Count()); 429 *_modes = list; 430 *_count = modes.Count(); 431 432 return area; 433 } 434 435