124 |
|
static unsigned long black_pixel, white_pixel; |
125 |
|
static int eventmask; |
126 |
|
|
127 |
< |
static XColor palette[256]; // Color palette for indexed modes |
127 |
> |
static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes |
128 |
> |
|
129 |
> |
static XColor palette[256]; // Color palette to be used as CLUT and gamma table |
130 |
|
static bool palette_changed = false; // Flag: Palette changed, redraw thread must set new colors |
131 |
|
|
132 |
|
#ifdef ENABLE_FBDEV_DGA |
185 |
|
* Utility functions |
186 |
|
*/ |
187 |
|
|
188 |
+ |
// Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals) |
189 |
+ |
static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue) |
190 |
+ |
{ |
191 |
+ |
return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift); |
192 |
+ |
} |
193 |
+ |
|
194 |
|
// Add mode to list of supported modes |
195 |
|
static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth) |
196 |
|
{ |
203 |
|
VideoModes.push_back(mode); |
204 |
|
} |
205 |
|
|
206 |
+ |
// Add standard list of windowed modes for given color depth |
207 |
+ |
static void add_window_modes(video_depth depth) |
208 |
+ |
{ |
209 |
+ |
add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), depth); |
210 |
+ |
add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), depth); |
211 |
+ |
add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), depth); |
212 |
+ |
add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth), depth); |
213 |
+ |
add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, depth), depth); |
214 |
+ |
add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, depth), depth); |
215 |
+ |
add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, depth), depth); |
216 |
+ |
} |
217 |
+ |
|
218 |
|
// Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac) |
219 |
|
static void set_mac_frame_buffer(video_depth depth, bool native_byte_order) |
220 |
|
{ |
223 |
|
if (depth == VDEPTH_16BIT) |
224 |
|
layout = (xdepth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565; |
225 |
|
else if (depth == VDEPTH_32BIT) |
226 |
< |
layour = (xdepth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT; |
226 |
> |
layout = (xdepth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT; |
227 |
|
if (native_byte_order) |
228 |
|
MacFrameLayout = layout; |
229 |
|
else |
404 |
|
{ |
405 |
|
if (cmap[0] && cmap[1]) { |
406 |
|
int num = 256; |
407 |
< |
if (vis->c_class == DirectColor && VideoMonitor.mode.depth == VDEPTH_16BIT) |
408 |
< |
num = vis->map_entries; |
407 |
> |
if (IsDirectMode(VideoMonitor.mode)) |
408 |
> |
num = vis->map_entries; // Palette is gamma table |
409 |
> |
else if (vis->c_class == DirectColor) |
410 |
> |
return; // Indexed mode on true color screen, don't set CLUT |
411 |
|
XStoreColors(x_display, cmap[0], palette, num); |
412 |
|
XStoreColors(x_display, cmap[1], palette, num); |
413 |
|
} |
434 |
|
XSetWindowAttributes wattr; |
435 |
|
wattr.event_mask = eventmask = win_eventmask; |
436 |
|
wattr.background_pixel = black_pixel; |
437 |
< |
wattr.colormap = cmap[0]; |
437 |
> |
wattr.colormap = (mode.depth == VDEPTH_1BIT && vis->c_class == PseudoColor ? DefaultColormap(x_display, screen) : cmap[0]); |
438 |
|
w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, |
439 |
< |
InputOutput, vis, CWEventMask | CWBackPixel | ((mode.depth == VDEPTH_1BIT || cmap[0] == 0) ? 0 : CWColormap), &wattr); |
439 |
> |
InputOutput, vis, CWEventMask | CWBackPixel | (vis->c_class == PseudoColor || vis->c_class == DirectColor ? CWColormap : 0), &wattr); |
440 |
|
|
441 |
|
// Set window name/class |
442 |
|
set_window_name(w, STR_WINDOW_TITLE); |
465 |
|
XMapWindow(x_display, w); |
466 |
|
wait_mapped(w); |
467 |
|
|
468 |
+ |
// 1-bit mode is big-endian; if the X server is little-endian, we can't |
469 |
+ |
// use SHM because that doesn't allow changing the image byte order |
470 |
+ |
bool need_msb_image = (mode.depth == VDEPTH_1BIT && XImageByteOrder(x_display) == LSBFirst); |
471 |
+ |
|
472 |
|
// Try to create and attach SHM image |
473 |
< |
if (local_X11 && XShmQueryExtension(x_display)) { |
473 |
> |
if (local_X11 && !need_msb_image && XShmQueryExtension(x_display)) { |
474 |
|
|
475 |
|
// Create SHM image ("height + 2" for safety) |
476 |
|
img = XShmCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, &shminfo, width, height); |
497 |
|
|
498 |
|
// Create normal X image if SHM doesn't work ("height + 2" for safety) |
499 |
|
if (!have_shm) { |
500 |
< |
int bytes_per_row = TrivialBytesPerRow(aligned_width, mode.depth); |
500 |
> |
int bytes_per_row = (mode.depth == VDEPTH_1BIT ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth))); |
501 |
|
the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row); |
502 |
|
img = XCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row); |
503 |
|
} |
504 |
|
|
505 |
< |
// 1-Bit mode is big-endian |
480 |
< |
if (mode.depth == VDEPTH_1BIT) { |
505 |
> |
if (need_msb_image) { |
506 |
|
img->byte_order = MSBFirst; |
507 |
|
img->bitmap_bit_order = MSBFirst; |
508 |
|
} |
537 |
|
native_byte_order = (XImageByteOrder(x_display) == LSBFirst); |
538 |
|
#endif |
539 |
|
#ifdef ENABLE_VOSF |
540 |
< |
Screen_blitter_init(&visualInfo, native_byte_order); |
540 |
> |
Screen_blitter_init(&visualInfo, native_byte_order, mode.depth); |
541 |
|
#endif |
542 |
|
|
543 |
|
// Set VideoMonitor |
805 |
|
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
806 |
|
// Screen_blitter_init() returns TRUE if VOSF is mandatory |
807 |
|
// i.e. the framebuffer update function is not Blit_Copy_Raw |
808 |
< |
use_vosf = Screen_blitter_init(&visualInfo, true); |
808 |
> |
use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth); |
809 |
|
|
810 |
|
if (use_vosf) { |
811 |
|
// Allocate memory for frame buffer (SIZE is extended to page-boundary) |
921 |
|
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
922 |
|
// Screen_blitter_init() returns TRUE if VOSF is mandatory |
923 |
|
// i.e. the framebuffer update function is not Blit_Copy_Raw |
924 |
< |
use_vosf = Screen_blitter_init(&visualInfo, true); |
924 |
> |
use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth); |
925 |
|
|
926 |
|
if (use_vosf) { |
927 |
|
// Allocate memory for frame buffer (SIZE is extended to page-boundary) |
1045 |
|
// Open display for specified mode |
1046 |
|
static bool video_open(const video_mode &mode) |
1047 |
|
{ |
1048 |
+ |
// Load gray ramp to color map |
1049 |
+ |
int num = (vis->c_class == DirectColor ? vis->map_entries : 256); |
1050 |
+ |
for (int i=0; i<num; i++) { |
1051 |
+ |
int c = (i * 256) / num; |
1052 |
+ |
palette[i].red = c * 0x0101; |
1053 |
+ |
palette[i].green = c * 0x0101; |
1054 |
+ |
palette[i].blue = c * 0x0101; |
1055 |
+ |
if (vis->c_class == PseudoColor) |
1056 |
+ |
palette[i].pixel = i; |
1057 |
+ |
palette[i].flags = DoRed | DoGreen | DoBlue; |
1058 |
+ |
} |
1059 |
+ |
if (cmap[0] && cmap[1]) { |
1060 |
+ |
XStoreColors(x_display, cmap[0], palette, num); |
1061 |
+ |
XStoreColors(x_display, cmap[1], palette, num); |
1062 |
+ |
} |
1063 |
+ |
|
1064 |
+ |
#ifdef ENABLE_VOSF |
1065 |
+ |
// Load gray ramp to 8->16/32 expand map |
1066 |
+ |
if (!IsDirectMode(mode) && (vis->c_class == TrueColor || vis->c_class == DirectColor)) |
1067 |
+ |
for (int i=0; i<256; i++) |
1068 |
+ |
ExpandMap[i] = map_rgb(i, i, i); |
1069 |
+ |
#endif |
1070 |
+ |
|
1071 |
|
// Create display driver object of requested type |
1072 |
|
switch (display_type) { |
1073 |
|
case DISPLAY_WINDOW: |
1232 |
|
if (color_class == PseudoColor || color_class == DirectColor) { |
1233 |
|
cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll); |
1234 |
|
cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll); |
1235 |
+ |
} |
1236 |
|
|
1237 |
< |
// Preset pixel members for gamma table |
1238 |
< |
if (color_class == DirectColor) { |
1239 |
< |
int num = vis->map_entries; |
1240 |
< |
uint32 rmask = vis->red_mask, gmask = vis->green_mask, bmask = vis->blue_mask; |
1241 |
< |
uint32 mask; |
1242 |
< |
int rloss = 8, rshift = 0; |
1243 |
< |
for (mask=rmask; !(mask&1); mask>>=1) |
1244 |
< |
++rshift; |
1245 |
< |
for (; mask&1; mask>>=1) |
1246 |
< |
--rloss; |
1247 |
< |
int gloss = 8, gshift = 0; |
1248 |
< |
for (mask=gmask; !(mask&1); mask>>=1) |
1249 |
< |
++gshift; |
1250 |
< |
for (; mask&1; mask>>=1) |
1251 |
< |
--gloss; |
1252 |
< |
int bloss = 8, bshift = 0; |
1253 |
< |
for (mask=bmask; !(mask&1); mask>>=1) |
1254 |
< |
++bshift; |
1255 |
< |
for (; mask&1; mask>>=1) |
1256 |
< |
--bloss; |
1257 |
< |
for (int i=0; i<num; i++) { |
1258 |
< |
int c = (i * 256) / num; |
1259 |
< |
palette[i].pixel = ((c >> rloss) << rshift) | ((c >> gloss) << gshift) | ((c >> bloss) << bshift); |
1260 |
< |
} |
1237 |
> |
// Find pixel format of direct modes |
1238 |
> |
if (color_class == DirectColor || color_class == TrueColor) { |
1239 |
> |
rshift = gshift = bshift = 0; |
1240 |
> |
rloss = gloss = bloss = 8; |
1241 |
> |
uint32 mask; |
1242 |
> |
for (mask=vis->red_mask; !(mask&1); mask>>=1) |
1243 |
> |
++rshift; |
1244 |
> |
for (; mask&1; mask>>=1) |
1245 |
> |
--rloss; |
1246 |
> |
for (mask=vis->green_mask; !(mask&1); mask>>=1) |
1247 |
> |
++gshift; |
1248 |
> |
for (; mask&1; mask>>=1) |
1249 |
> |
--gloss; |
1250 |
> |
for (mask=vis->blue_mask; !(mask&1); mask>>=1) |
1251 |
> |
++bshift; |
1252 |
> |
for (; mask&1; mask>>=1) |
1253 |
> |
--bloss; |
1254 |
> |
} |
1255 |
> |
|
1256 |
> |
// Preset palette pixel values for gamma table |
1257 |
> |
if (color_class == DirectColor) { |
1258 |
> |
int num = vis->map_entries; |
1259 |
> |
for (int i=0; i<num; i++) { |
1260 |
> |
int c = (i * 256) / num; |
1261 |
> |
palette[i].pixel = map_rgb(c, c, c); |
1262 |
|
} |
1263 |
|
} |
1264 |
|
|
1295 |
|
else if (default_height > DisplayHeight(x_display, screen)) |
1296 |
|
default_height = DisplayHeight(x_display, screen); |
1297 |
|
|
1298 |
< |
// Mac screen depth is always 1 bit in Classic mode, but follows X depth otherwise |
1299 |
< |
int depth = (classic_mode ? 1 : xdepth); |
1250 |
< |
video_depth depth_mode = DepthModeForPixelDepth(depth); |
1298 |
> |
// Mac screen depth follows X depth |
1299 |
> |
video_depth default_depth = DepthModeForPixelDepth(xdepth); |
1300 |
|
|
1301 |
|
// Construct list of supported modes |
1302 |
|
if (display_type == DISPLAY_WINDOW) { |
1303 |
|
if (classic) |
1304 |
< |
add_mode(512, 342, 0x80, 64, depth_mode); |
1304 |
> |
add_mode(512, 342, 0x80, 64, VDEPTH_1BIT); |
1305 |
|
else { |
1306 |
< |
add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth_mode), depth_mode); |
1307 |
< |
add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth_mode), depth_mode); |
1308 |
< |
add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth_mode), depth_mode); |
1309 |
< |
add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth_mode), depth_mode); |
1310 |
< |
add_mode(1280, 1024, 0x84, TrivialBytesPerRow(1280, depth_mode), depth_mode); |
1306 |
> |
if (default_depth != VDEPTH_1BIT) |
1307 |
> |
add_window_modes(VDEPTH_1BIT); // 1-bit modes are always available |
1308 |
> |
#ifdef ENABLE_VOSF |
1309 |
> |
if (default_depth > VDEPTH_8BIT) { |
1310 |
> |
add_window_modes(VDEPTH_2BIT); // 2, 4 and 8-bit modes are also possible on 16/32-bit screens with VOSF blitters |
1311 |
> |
add_window_modes(VDEPTH_4BIT); |
1312 |
> |
add_window_modes(VDEPTH_8BIT); |
1313 |
> |
} |
1314 |
> |
#endif |
1315 |
> |
add_window_modes(default_depth); |
1316 |
|
} |
1317 |
|
} else |
1318 |
< |
add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, depth_mode), depth_mode); |
1318 |
> |
add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth); |
1319 |
|
|
1320 |
|
// Find requested default mode and open display |
1321 |
|
if (VideoModes.size() == 1) |
1322 |
|
return video_open(VideoModes[0]); |
1323 |
|
else { |
1324 |
|
// Find mode with specified dimensions |
1325 |
< |
std::vector<video_mode>::const_iterator i = VideoModes.begin(), end = VideoModes.end(); |
1326 |
< |
while (i != end) { |
1327 |
< |
if (i->x == default_width && i->y == default_height) |
1325 |
> |
std::vector<video_mode>::const_iterator i, end = VideoModes.end(); |
1326 |
> |
for (i = VideoModes.begin(); i != end; ++i) { |
1327 |
> |
if (i->x == default_width && i->y == default_height && i->depth == default_depth) |
1328 |
|
return video_open(*i); |
1275 |
– |
++i; |
1329 |
|
} |
1330 |
|
return video_open(VideoModes[0]); |
1331 |
|
} |
1439 |
|
* Set palette |
1440 |
|
*/ |
1441 |
|
|
1442 |
< |
void video_set_palette(uint8 *pal) |
1442 |
> |
void video_set_palette(uint8 *pal, int num_in) |
1443 |
|
{ |
1444 |
|
LOCK_PALETTE; |
1445 |
|
|
1446 |
|
// Convert colors to XColor array |
1447 |
< |
int num_in = 256, num_out = 256; |
1448 |
< |
if (VideoMonitor.mode.depth == VDEPTH_16BIT) { |
1449 |
< |
num_in = 32; |
1450 |
< |
// If X is in 565 mode we have to stretch the palette from 32 to 64 entries |
1398 |
< |
if (vis->c_class == DirectColor) |
1399 |
< |
num_out = vis->map_entries; |
1447 |
> |
int num_out = 256; |
1448 |
> |
if (IsDirectMode(VideoMonitor.mode)) { |
1449 |
> |
// If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries |
1450 |
> |
num_out = vis->map_entries; |
1451 |
|
} |
1452 |
|
XColor *p = palette; |
1453 |
|
for (int i=0; i<num_out; i++) { |
1455 |
|
p->red = pal[c*3 + 0] * 0x0101; |
1456 |
|
p->green = pal[c*3 + 1] * 0x0101; |
1457 |
|
p->blue = pal[c*3 + 2] * 0x0101; |
1458 |
< |
if (!IsDirectMode(VideoMonitor.mode)) |
1458 |
> |
if (vis->c_class == PseudoColor) |
1459 |
|
p->pixel = i; |
1460 |
|
p->flags = DoRed | DoGreen | DoBlue; |
1461 |
|
p++; |
1462 |
|
} |
1463 |
|
|
1464 |
+ |
#ifdef ENABLE_VOSF |
1465 |
+ |
// Recalculate pixel color expansion map |
1466 |
+ |
if (!IsDirectMode(VideoMonitor.mode) && (vis->c_class == TrueColor || vis->c_class == DirectColor)) { |
1467 |
+ |
for (int i=0; i<256; i++) { |
1468 |
+ |
int c = i % num_in; // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier) |
1469 |
+ |
ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]); |
1470 |
+ |
} |
1471 |
+ |
|
1472 |
+ |
// We have to redraw everything because the interpretation of pixel values changed |
1473 |
+ |
LOCK_VOSF; |
1474 |
+ |
PFLAG_SET_ALL; |
1475 |
+ |
UNLOCK_VOSF; |
1476 |
+ |
memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y); |
1477 |
+ |
} |
1478 |
+ |
#endif |
1479 |
+ |
|
1480 |
|
// Tell redraw thread to change palette |
1481 |
|
palette_changed = true; |
1482 |
|
|