--- SheepShaver/src/Unix/video_x.cpp 2004/04/11 10:46:32 1.14 +++ SheepShaver/src/Unix/video_x.cpp 2004/04/18 23:17:54 1.17 @@ -32,7 +32,7 @@ #include #ifdef ENABLE_XF86_DGA -#include +# include #endif #ifdef ENABLE_XF86_VIDMODE @@ -63,6 +63,7 @@ static int32 frame_skip; static int16 mouse_wheel_mode; static int16 mouse_wheel_lines; static bool redraw_thread_active = false; // Flag: Redraw thread installed +static pthread_attr_t redraw_thread_attr; // Redraw thread attributes static pthread_t redraw_thread; // Redraw thread static bool local_X11; // Flag: X server running on local machine? @@ -125,8 +126,6 @@ static uint8 *the_buffer_copy = NULL; / static uint32 the_buffer_size; // Size of allocated the_buffer // Variables for DGA mode -static char *dga_screen_base; -static int dga_fb_width; static int current_dga_cmap; #ifdef ENABLE_XF86_VIDMODE @@ -196,10 +195,30 @@ static int palette_size(int mode) } } +// Return bits per pixel for requested depth +static inline int bytes_per_pixel(int depth) +{ + int bpp; + switch (depth) { + case 8: + bpp = 1; + break; + case 15: case 16: + bpp = 2; + break; + case 24: case 32: + bpp = 4; + break; + default: + abort(); + } + return bpp; +} + // Map video_mode depth ID to numerical depth value static inline int depth_of_video_mode(int mode) { - int depth = -1; + int depth; switch (mode) { case APPLE_1_BIT: depth = 1; @@ -349,7 +368,7 @@ static void set_window_delete_protocol(W } // Wait until window is mapped/unmapped -void wait_mapped(Window w) +static void wait_mapped(Window w) { XEvent e; do { @@ -357,7 +376,7 @@ void wait_mapped(Window w) } while ((e.type != MapNotify) || (e.xmap.event != w)); } -void wait_unmapped(Window w) +static void wait_unmapped(Window w) { XEvent e; do { @@ -522,6 +541,19 @@ static bool open_dga(int width, int heig // Set relative mouse mode ADBSetRelMouseMode(true); + // Create window + XSetWindowAttributes wattr; + wattr.event_mask = eventmask = dga_eventmask; + wattr.override_redirect = True; + wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]); + the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, + InputOutput, vis, CWEventMask | CWOverrideRedirect | + (color_class == DirectColor ? CWColormap : 0), &wattr); + + // Show window + XMapRaised(x_display, the_win); + wait_mapped(the_win); + #ifdef ENABLE_XF86_VIDMODE // Switch to best mode if (has_vidmode) { @@ -538,19 +570,26 @@ static bool open_dga(int width, int heig #endif // Establish direct screen connection + XMoveResizeWindow(x_display, the_win, 0, 0, width, height); + XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0); XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime); XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); + + int v_width, v_bank, v_size; + XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size); XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse); XF86DGASetViewPort(x_display, screen, 0, 0); XF86DGASetVidPage(x_display, screen, 0); // Set colormap - if (depth == 8) + if (!IsDirectMode(get_current_mode())) { + XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]); XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); + } + XSync(x_display, false); - // Set bytes per row - int bytes_per_row = TrivialBytesPerRow((dga_fb_width + 7) & ~7, DepthModeForPixelDepth(depth)); - + // Init blitting routines + int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, DepthModeForPixelDepth(depth)); #if ENABLE_VOSF bool native_byte_order; #ifdef WORDS_BIGENDIAN @@ -569,16 +608,17 @@ static bool open_dga(int width, int heig the_buffer_size = page_extend((height + 2) * bytes_per_row); the_buffer_copy = (uint8 *)malloc(the_buffer_size); the_buffer = (uint8 *)vm_acquire(the_buffer_size); + D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer)); } #else use_vosf = false; - the_buffer = dga_screen_base; #endif #endif - screen_base = (uint32)the_buffer; + // Set frame buffer base + D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf)); + screen_base = (uint32)the_buffer; VModes[cur_mode].viRowBytes = bytes_per_row; - XSync(x_display, false); return true; #else ErrorAlert("SheepShaver has been compiled with DGA support disabled."); @@ -711,13 +751,6 @@ static void close_window(void) if (the_gc) XFreeGC(x_display, the_gc); - // Close window - if (the_win) { - XUnmapWindow(x_display, the_win); - wait_unmapped(the_win); - XDestroyWindow(x_display, the_win); - } - XFlush(x_display); XSync(x_display, false); } @@ -755,6 +788,13 @@ static void close_display(void) else if (display_type == DIS_WINDOW) close_window(); + // Close window + if (the_win) { + XUnmapWindow(x_display, the_win); + wait_unmapped(the_win); + XDestroyWindow(x_display, the_win); + } + // Free colormaps if (cmap[0]) { XFreeColormap(x_display, cmap[0]); @@ -871,6 +911,39 @@ static void keycode_init(void) } } +// Find Apple mode matching best specified dimensions +static int find_apple_resolution(int xsize, int ysize) +{ + int apple_id; + if (xsize < 800) + apple_id = APPLE_640x480; + else if (xsize < 1024) + apple_id = APPLE_800x600; + else if (xsize < 1152) + apple_id = APPLE_1024x768; + else if (xsize < 1280) { + if (ysize < 900) + apple_id = APPLE_1152x768; + else + apple_id = APPLE_1152x900; + } + else if (xsize < 1600) + apple_id = APPLE_1280x1024; + else + apple_id = APPLE_1600x1200; + return apple_id; +} + +// Find mode in list of supported modes +static int find_mode(int apple_mode, int apple_id, int type) +{ + for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) { + if (p->viType == type && p->viAppleID == apple_id && p->viAppleMode == apple_mode) + return p - VModes; + } + return -1; +} + // Add mode to list of supported modes static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type) { @@ -891,6 +964,10 @@ static void add_mode(VideoInfo *&p, uint p->viXsize = 1024; p->viYsize = 768; break; + case APPLE_1152x768: + p->viXsize = 1152; + p->viYsize = 768; + break; case APPLE_1152x900: p->viXsize = 1152; p->viYsize = 900; @@ -1037,6 +1114,8 @@ bool VideoInit(void) add_mode(p, screen_modes, 2, default_mode, APPLE_800x600, DIS_SCREEN); if (has_mode(1024, 768)) add_mode(p, screen_modes, 4, default_mode, APPLE_1024x768, DIS_SCREEN); + if (has_mode(1152, 768)) + add_mode(p, screen_modes, 64, default_mode, APPLE_1152x768, DIS_SCREEN); if (has_mode(1152, 900)) add_mode(p, screen_modes, 8, default_mode, APPLE_1152x900, DIS_SCREEN); if (has_mode(1280, 1024)) @@ -1046,19 +1125,7 @@ bool VideoInit(void) } else if (screen_modes) { int xsize = DisplayWidth(x_display, screen); int ysize = DisplayHeight(x_display, screen); - int apple_id; - if (xsize < 800) - apple_id = APPLE_640x480; - else if (xsize < 1024) - apple_id = APPLE_800x600; - else if (xsize < 1152) - apple_id = APPLE_1024x768; - else if (xsize < 1280) - apple_id = APPLE_1152x900; - else if (xsize < 1600) - apple_id = APPLE_1280x1024; - else - apple_id = APPLE_1600x1200; + int apple_id = find_apple_resolution(xsize, ysize); p->viType = DIS_SCREEN; p->viRowBytes = 0; p->viXsize = xsize; @@ -1075,14 +1142,15 @@ bool VideoInit(void) // Find default mode (window 640x480) cur_mode = -1; - for (p = VModes; p->viType != DIS_INVALID; p++) { - if (p->viType == DIS_WINDOW - && p->viAppleID == APPLE_W_640x480 - && p->viAppleMode == default_mode) { - cur_mode = p - VModes; - break; - } + if (has_dga && screen_modes) { + int screen_width = DisplayWidth(x_display, screen); + int screen_height = DisplayHeight(x_display, screen); + int apple_id = find_apple_resolution(screen_width, screen_height); + if (apple_id != -1) + cur_mode = find_mode(default_mode, apple_id, DIS_SCREEN); } + if (cur_mode == -1) + cur_mode = find_mode(default_mode, APPLE_W_640x480, DIS_WINDOW); assert(cur_mode != -1); #if DEBUG @@ -1093,14 +1161,6 @@ bool VideoInit(void) } #endif -#ifdef ENABLE_XF86_DGA - if (has_dga && screen_modes) { - int v_bank, v_size; - XF86DGAGetVideo(x_display, screen, &dga_screen_base, &dga_fb_width, &v_bank, &v_size); - D(bug("DGA screen_base %p, v_width %d\n", dga_screen_base, dga_fb_width)); - } -#endif - // Open window/screen if (!open_display()) return false; @@ -1112,7 +1172,8 @@ bool VideoInit(void) // Start periodic thread XSync(x_display, false); - redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0); + Set_pthread_attr(&redraw_thread_attr, 0); + redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0); D(bug("Redraw thread installed (%ld)\n", redraw_thread)); return true; } @@ -1475,12 +1536,15 @@ static void handle_events(void) break; } - // Mouse moved + // Mouse entered window case EnterNotify: - ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y); + if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab) + ADBMouseMoved(event.xmotion.x, event.xmotion.y); break; + + // Mouse moved case MotionNotify: - ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y); + ADBMouseMoved(event.xmotion.x, event.xmotion.y); break; // Keyboard @@ -1548,44 +1612,6 @@ void VideoVBL(void) */ #if 0 -// Rectangle blitting -static void accl_bitblt(accl_params *p) -{ - D(bug("accl_bitblt\n")); - - // Get blitting parameters - int16 src_X = p->src_rect[1] - p->src_bounds[1]; - int16 src_Y = p->src_rect[0] - p->src_bounds[0]; - int16 dest_X = p->dest_rect[1] - p->dest_bounds[1]; - int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0]; - int16 width = p->dest_rect[3] - p->dest_rect[1] - 1; - int16 height = p->dest_rect[2] - p->dest_rect[0] - 1; - D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y)); - D(bug(" width %d, height %d\n", width, height)); - - // And perform the blit - bitblt_hook(src_X, src_Y, dest_X, dest_Y, width, height); -} - -static bool accl_bitblt_hook(accl_params *p) -{ - D(bug("accl_draw_hook %p\n", p)); - - // Check if we can accelerate this bitblt - if (p->src_base_addr == screen_base && p->dest_base_addr == screen_base && - display_type == DIS_SCREEN && bitblt_hook != NULL && - ((uint32 *)p)[0x18 >> 2] + ((uint32 *)p)[0x128 >> 2] == 0 && - ((uint32 *)p)[0x130 >> 2] == 0 && - p->transfer_mode == 0 && - p->src_row_bytes > 0 && ((uint32 *)p)[0x15c >> 2] > 0) { - - // Yes, set function pointer - p->draw_proc = accl_bitblt; - return true; - } - return false; -} - // Rectangle filling/inversion static void accl_fillrect8(accl_params *p) { @@ -1663,25 +1689,97 @@ static bool accl_fillrect_hook(accl_para return false; } +static struct accl_hook_info fillrect_hook_info = {accl_fillrect_hook, accl_sync_hook, ACCL_FILLRECT}; +#endif + +// Rectangle blitting +// TODO: optimize for VOSF and target pixmap == screen +void NQD_bitblt(uint32 arg) +{ + D(bug("accl_bitblt %08x\n", arg)); + accl_params *p = (accl_params *)arg; + + // Get blitting parameters + int16 src_X = p->src_rect[1] - p->src_bounds[1]; + int16 src_Y = p->src_rect[0] - p->src_bounds[0]; + int16 dest_X = p->dest_rect[1] - p->dest_bounds[1]; + int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0]; + int16 width = p->dest_rect[3] - p->dest_rect[1]; + int16 height = p->dest_rect[2] - p->dest_rect[0]; + D(bug(" src addr %08x, dest addr %08x\n", p->src_base_addr, p->dest_base_addr)); + D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y)); + D(bug(" width %d, height %d\n", width, height)); + + // And perform the blit + const int bpp = bytes_per_pixel(p->src_pixel_size); + width *= bpp; + if (p->src_row_bytes > 0) { + const int src_row_bytes = p->src_row_bytes; + const int dst_row_bytes = p->dest_row_bytes; + uint8 *src = (uint8 *)p->src_base_addr + (src_Y * src_row_bytes) + (src_X * bpp); + uint8 *dst = (uint8 *)p->dest_base_addr + (dest_Y * dst_row_bytes) + (dest_X * bpp); + for (int i = 0; i < height; i++) { + memcpy(dst, src, width); + src += src_row_bytes; + dst += dst_row_bytes; + } + } + else { + const int src_row_bytes = -p->src_row_bytes; + const int dst_row_bytes = -p->dest_row_bytes; + uint8 *src = (uint8 *)p->src_base_addr + ((src_Y + height - 1) * src_row_bytes) + (src_X * bpp); + uint8 *dst = (uint8 *)p->dest_base_addr + ((dest_Y + height - 1) * dst_row_bytes) + (dest_X * bpp); + for (int i = height - 1; i >= 0; i--) { + memcpy(dst, src, width); + src -= src_row_bytes; + dst -= dst_row_bytes; + } + } +} + +bool NQD_bitblt_hook(uint32 arg) +{ + D(bug("accl_draw_hook %08x\n", arg)); + accl_params *p = (accl_params *)arg; + + // Check if we can accelerate this bitblt + if (((uint32 *)p)[0x18 >> 2] + ((uint32 *)p)[0x128 >> 2] == 0 && + ((uint32 *)p)[0x130 >> 2] == 0 && + p->src_pixel_size >= 8 && p->src_pixel_size == p->dest_pixel_size && + ((p->src_row_bytes ^ p->dest_row_bytes) >> 31) == 0 && + p->transfer_mode == 0 && + ((uint32 *)p)[0x15c >> 2] > 0) { + + // Yes, set function pointer + p->draw_proc = NativeTVECT(NATIVE_BITBLT); + return true; + } + return false; +} + // Wait for graphics operation to finish -static bool accl_sync_hook(void *arg) +bool NQD_sync_hook(uint32 arg) { - D(bug("accl_sync_hook %p\n", arg)); - if (sync_hook != NULL) - sync_hook(); + D(bug("accl_sync_hook %08x\n", arg)); return true; } -static struct accl_hook_info bitblt_hook_info = {accl_bitblt_hook, accl_sync_hook, ACCL_BITBLT}; -static struct accl_hook_info fillrect_hook_info = {accl_fillrect_hook, accl_sync_hook, ACCL_FILLRECT}; -#endif - void VideoInstallAccel(void) { // Install acceleration hooks if (PrefsFindBool("gfxaccel")) { D(bug("Video: Installing acceleration hooks\n")); -//!! NQDMisc(6, &bitblt_hook_info); + uint32 base; + + SheepVar bitblt_hook_info(sizeof(accl_hook_info)); + base = bitblt_hook_info.addr(); + WriteMacInt32(base + 0, NativeTVECT(NATIVE_BITBLT_HOOK)); + WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK)); + WriteMacInt32(base + 8, ACCL_BITBLT); +#if defined(__powerpc__) // Temporary hack until it's fixed for e.g. little-endian & 64-bit platforms + NQDMisc(6, bitblt_hook_info.ptr()); +#endif + // NQDMisc(6, &fillrect_hook_info); } } @@ -1999,6 +2097,9 @@ static void *redraw_func(void *arg) XF86DGADirectVideo(x_display, screen, 0); XUngrabPointer(x_display, CurrentTime); XUngrabKeyboard(x_display, CurrentTime); + XUnmapWindow(x_display, the_win); + wait_unmapped(the_win); + XDestroyWindow(x_display, the_win); #endif XSync(x_display, false); XDisplayUnlock();