--- BasiliskII/src/Unix/video_x.cpp 2000/07/22 16:07:21 1.17 +++ BasiliskII/src/Unix/video_x.cpp 2000/11/30 16:09:03 1.31 @@ -52,6 +52,13 @@ # include #endif +#ifdef ENABLE_VOSF +# include +# include +# include +# include +#endif + #include "cpu_emulation.h" #include "main.h" #include "adb.h" @@ -115,13 +122,19 @@ static Colormap cmap[2]; // Two co static XColor black, white; static unsigned long black_pixel, white_pixel; static int eventmask; -static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask; -static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask; +static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask; +static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask; +static Atom WM_DELETE_WINDOW = (Atom)0; static XColor palette[256]; // Color palette for 8-bit mode static bool palette_changed = false; // Flag: Palette changed, redraw thread must set new colors #ifdef HAVE_PTHREADS static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect palette +#define LOCK_PALETTE pthread_mutex_lock(&palette_lock) +#define UNLOCK_PALETTE pthread_mutex_unlock(&palette_lock) +#else +#define LOCK_PALETTE +#define UNLOCK_PALETTE #endif // Variables for window mode @@ -142,6 +155,11 @@ static Window suspend_win; // "Sus static void *fb_save = NULL; // Saved frame buffer for suspend #ifdef HAVE_PTHREADS static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer +#define LOCK_FRAME_BUFFER pthread_mutex_lock(&frame_buffer_lock); +#define UNLOCK_FRAME_BUFFER pthread_mutex_unlock(&frame_buffer_lock); +#else +#define LOCK_FRAME_BUFFER +#define UNLOCK_FRAME_BUFFER #endif // Variables for fbdev DGA mode @@ -154,10 +172,86 @@ static XF86VidModeModeInfo **x_video_mod static int num_x_video_modes; #endif +#ifdef ENABLE_VOSF +static bool use_vosf = true; // Flag: VOSF enabled +#else +static const bool use_vosf = false; // Flag: VOSF enabled +#endif + +#ifdef ENABLE_VOSF +// Variables for Video on SEGV support (taken from the Win32 port) +static uint8 *the_host_buffer; // Host frame buffer in VOSF mode +static uint32 the_buffer_size; // Size of allocated the_buffer + +struct ScreenPageInfo { + int top, bottom; // Mapping between this virtual page and Mac scanlines +}; + +struct ScreenInfo { + uint32 memBase; // Real start address + uint32 memStart; // Start address aligned to page boundary + uint32 memEnd; // Address of one-past-the-end of the screen + uint32 memLength; // Length of the memory addressed by the screen pages + + uint32 pageSize; // Size of a page + int pageBits; // Shift count to get the page number + uint32 pageCount; // Number of pages allocated to the screen + + uint8 * dirtyPages; // Table of flags set if page was altered + ScreenPageInfo * pageInfo; // Table of mappings page -> Mac scanlines +}; + +static ScreenInfo mainBuffer; + +#define PFLAG_SET(page) mainBuffer.dirtyPages[page] = 1 +#define PFLAG_CLEAR(page) mainBuffer.dirtyPages[page] = 0 +#define PFLAG_ISSET(page) mainBuffer.dirtyPages[page] +#define PFLAG_ISCLEAR(page) (mainBuffer.dirtyPages[page] == 0) +#ifdef UNALIGNED_PROFITABLE +# define PFLAG_ISCLEAR_4(page) (*((uint32 *)(mainBuffer.dirtyPages + page)) == 0) +#else +# define PFLAG_ISCLEAR_4(page) \ + (mainBuffer.dirtyPages[page ] == 0) \ + && (mainBuffer.dirtyPages[page+1] == 0) \ + && (mainBuffer.dirtyPages[page+2] == 0) \ + && (mainBuffer.dirtyPages[page+3] == 0) +#endif +#define PFLAG_CLEAR_ALL memset(mainBuffer.dirtyPages, 0, mainBuffer.pageCount) +#define PFLAG_SET_ALL memset(mainBuffer.dirtyPages, 1, mainBuffer.pageCount) + +static int zero_fd = -1; +static bool Screen_fault_handler_init(); +static struct sigaction vosf_sa; + +#ifdef HAVE_PTHREADS +static pthread_mutex_t vosf_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer (dirtyPages in fact) +#define LOCK_VOSF pthread_mutex_lock(&vosf_lock); +#define UNLOCK_VOSF pthread_mutex_unlock(&vosf_lock); +#else +#define LOCK_VOSF +#define UNLOCK_VOSF +#endif + +static int log_base_2(uint32 x) +{ + uint32 mask = 0x80000000; + int l = 31; + while (l >= 0 && (x & mask) == 0) { + mask >>= 1; + l--; + } + return l; +} + +#endif + +// VideoRefresh function +void VideoRefreshInit(void); +static void (*video_refresh)(void); // Prototypes static void *redraw_func(void *arg); -static int event2keycode(XKeyEvent *ev); +static int event2keycode(XKeyEvent &ev); // From main_unix.cpp @@ -167,6 +261,10 @@ extern Display *x_display; // From sys_unix.cpp extern void SysMountFirstFloppy(void); +#ifdef ENABLE_VOSF +# include "video_vosf.h" +#endif + /* * Initialization @@ -175,7 +273,7 @@ extern void SysMountFirstFloppy(void); // Set VideoMonitor according to video mode void set_video_monitor(int width, int height, int bytes_per_row, bool native_byte_order) { -#if !REAL_ADDRESSING +#if !REAL_ADDRESSING && !DIRECT_ADDRESSING int layout = FLAYOUT_DIRECT; switch (depth) { case 1: @@ -223,6 +321,60 @@ void set_video_monitor(int width, int he VideoMonitor.bytes_per_row = bytes_per_row; } +// Set window name and class +static void set_window_name(Window w, int name) +{ + const char *str = GetString(name); + XStoreName(x_display, w, str); + XSetIconName(x_display, w, str); + + XClassHint *hints; + hints = XAllocClassHint(); + if (hints) { + hints->res_name = "BasiliskII"; + hints->res_class = "BasiliskII"; + XSetClassHint(x_display, w, hints); + XFree((char *)hints); + } +} + +// Set window input focus flag +static void set_window_focus(Window w) +{ + XWMHints *hints = XAllocWMHints(); + if (hints) { + hints->input = True; + hints->initial_state = NormalState; + hints->flags = InputHint | StateHint; + XSetWMHints(x_display, w, hints); + XFree((char *)hints); + } +} + +// Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget) +static void set_window_delete_protocol(Window w) +{ + WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false); + XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1); +} + +// Wait until window is mapped/unmapped +void wait_mapped(Window w) +{ + XEvent e; + do { + XMaskEvent(x_display, StructureNotifyMask, &e); + } while ((e.type != MapNotify) || (e.xmap.event != w)); +} + +void wait_unmapped(Window w) +{ + XEvent e; + do { + XMaskEvent(x_display, StructureNotifyMask, &e); + } while ((e.type != UnmapNotify) || (e.xmap.event != w)); +} + // Trap SHM errors static bool shm_error = false; static int (*old_error_handler)(Display *, XErrorEvent *); @@ -252,38 +404,38 @@ static bool init_window(int width, int h XSetWindowAttributes wattr; wattr.event_mask = eventmask = win_eventmask; wattr.background_pixel = black_pixel; - wattr.border_pixel = black_pixel; - wattr.backing_store = NotUseful; - wattr.save_under = false; - wattr.backing_planes = xdepth; + wattr.colormap = cmap[0]; - XSync(x_display, false); the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, - InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | - CWBackingStore | CWBackingPlanes, &wattr); - XSync(x_display, false); - XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE)); - XMapRaised(x_display, the_win); - XSync(x_display, false); + InputOutput, vis, CWEventMask | CWBackPixel | (depth == 8 ? CWColormap : 0), &wattr); - // Set colormap - if (depth == 8) { - XSetWindowColormap(x_display, the_win, cmap[0]); - XSetWMColormapWindows(x_display, the_win, &the_win, 1); - } + // Set window name/class + set_window_name(the_win, STR_WINDOW_TITLE); + + // Indicate that we want keyboard input + set_window_focus(the_win); + + // Set delete protocol property + set_window_delete_protocol(the_win); // Make window unresizable - XSizeHints *hints; - if ((hints = XAllocSizeHints()) != NULL) { - hints->min_width = width; - hints->max_width = width; - hints->min_height = height; - hints->max_height = height; - hints->flags = PMinSize | PMaxSize; - XSetWMNormalHints(x_display, the_win, hints); - XFree((char *)hints); + { + XSizeHints *hints = XAllocSizeHints(); + if (hints) { + hints->min_width = width; + hints->max_width = width; + hints->min_height = height; + hints->max_height = height; + hints->flags = PMinSize | PMaxSize; + XSetWMNormalHints(x_display, the_win, hints); + XFree((char *)hints); + } } + // Show window + XMapWindow(x_display, the_win); + wait_mapped(the_win); + // Try to create and attach SHM image have_shm = false; if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) { @@ -337,8 +489,20 @@ static bool init_window(int width, int h img->bitmap_bit_order = MSBFirst; } +#ifdef ENABLE_VOSF + // Allocate a page-aligned chunk of memory for frame buffer + the_buffer_size = align_on_page_boundary((aligned_height + 2) * img->bytes_per_line); + the_host_buffer = the_buffer_copy; + + the_buffer_copy = (uint8 *)allocate_framebuffer(the_buffer_size); + memset(the_buffer_copy, 0, the_buffer_size); + + the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size); + memset(the_buffer, 0, the_buffer_size); +#else // Allocate memory for frame buffer the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line); +#endif // Create GC the_gc = XCreateGC(x_display, the_win, 0, 0); @@ -352,14 +516,19 @@ static bool init_window(int width, int h XDefineCursor(x_display, the_win, mac_cursor); // Set VideoMonitor + bool native_byte_order; #ifdef WORDS_BIGENDIAN - set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == MSBFirst); + native_byte_order = (XImageByteOrder(x_display) == MSBFirst); #else - set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == LSBFirst); + native_byte_order = (XImageByteOrder(x_display) == LSBFirst); #endif +#ifdef ENABLE_VOSF + do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW); +#endif + set_video_monitor(width, height, img->bytes_per_line, native_byte_order); -#if REAL_ADDRESSING - VideoMonitor.mac_frame_base = (uint32)the_buffer; +#if REAL_ADDRESSING || DIRECT_ADDRESSING + VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer); #else VideoMonitor.mac_frame_base = MacFrameBaseMac; #endif @@ -438,21 +607,26 @@ static bool init_fbdev_dga(char *in_fb_n // Create window XSetWindowAttributes wattr; - wattr.override_redirect = True; - wattr.backing_store = NotUseful; - wattr.background_pixel = white_pixel; - wattr.border_pixel = black_pixel; - wattr.event_mask = eventmask = dga_eventmask; + wattr.event_mask = eventmask = dga_eventmask; + wattr.background_pixel = white_pixel; + wattr.override_redirect = True; + wattr.colormap = cmap[0]; - XSync(x_display, false); the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, InputOutput, vis, - CWEventMask|CWBackPixel|CWBorderPixel|CWOverrideRedirect|CWBackingStore, + CWEventMask | CWBackPixel | CWOverrideRedirect | (depth == 8 ? CWColormap : 0), &wattr); - XSync(x_display, false); + + // Set window name/class + set_window_name(the_win, STR_WINDOW_TITLE); + + // Indicate that we want keyboard input + set_window_focus(the_win); + + // Show window XMapRaised(x_display, the_win); - XSync(x_display, false); + wait_mapped(the_win); // Grab mouse and keyboard XGrabKeyboard(x_display, the_win, True, @@ -461,12 +635,6 @@ static bool init_fbdev_dga(char *in_fb_n PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime); - // Set colormap - if (depth == 8) { - XSetWindowColormap(x_display, the_win, cmap[0]); - XSetWMColormapWindows(x_display, the_win, &the_win, 1); - } - // Set VideoMonitor int bytes_per_row = width; switch (depth) { @@ -492,9 +660,31 @@ static bool init_fbdev_dga(char *in_fb_n } } +#if ENABLE_VOSF +#if REAL_ADDRESSING || DIRECT_ADDRESSING + // If the blit function is null, i.e. just a copy of the buffer, + // we first try to avoid the allocation of a temporary frame buffer + use_vosf = true; + do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA); + if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw)) + use_vosf = false; + + if (use_vosf) { + the_host_buffer = the_buffer; + the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row); + the_buffer_copy = (uint8 *)malloc(the_buffer_size); + memset(the_buffer_copy, 0, the_buffer_size); + the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size); + memset(the_buffer, 0, the_buffer_size); + } +#else + use_vosf = false; +#endif +#endif + set_video_monitor(width, height, bytes_per_row, true); -#if REAL_ADDRESSING - VideoMonitor.mac_frame_base = (uint32)the_buffer; +#if REAL_ADDRESSING || DIRECT_ADDRESSING + VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer); #else VideoMonitor.mac_frame_base = MacFrameBaseMac; #endif @@ -524,22 +714,27 @@ static bool init_xf86_dga(int width, int } XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]); XF86VidModeSetViewPort(x_display, screen, 0, 0); + XSync(x_display, false); } #endif // Create window XSetWindowAttributes wattr; wattr.event_mask = eventmask = dga_eventmask; - wattr.border_pixel = black_pixel; wattr.override_redirect = True; - XSync(x_display, false); the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, - InputOutput, vis, CWEventMask | CWBorderPixel | CWOverrideRedirect, &wattr); - XSync(x_display, false); - XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE)); + InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr); + + // Set window name/class + set_window_name(the_win, STR_WINDOW_TITLE); + + // Indicate that we want keyboard input + set_window_focus(the_win); + + // Show window XMapRaised(x_display, the_win); - XSync(x_display, false); + wait_mapped(the_win); // Establish direct screen connection XMoveResizeWindow(x_display, the_win, 0, 0, width, height); @@ -556,9 +751,9 @@ static bool init_xf86_dga(int width, int // Set colormap if (depth == 8) { XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]); - XSetWMColormapWindows(x_display, the_win, &the_win, 1); XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); } + XSync(x_display, false); // Set VideoMonitor int bytes_per_row = (v_width + 7) & ~7; @@ -575,10 +770,32 @@ static bool init_xf86_dga(int width, int bytes_per_row *= 4; break; } + +#if REAL_ADDRESSING || DIRECT_ADDRESSING + // If the blit function is null, i.e. just a copy of the buffer, + // we first try to avoid the allocation of a temporary frame buffer + use_vosf = true; + do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA); + if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw)) + use_vosf = false; + + if (use_vosf) { + the_host_buffer = the_buffer; + the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row); + the_buffer_copy = (uint8 *)malloc(the_buffer_size); + memset(the_buffer_copy, 0, the_buffer_size); + the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size); + memset(the_buffer, 0, the_buffer_size); + } +#elif defined(ENABLE_VOSF) + // The UAE memory handlers will already handle color conversion, if needed. + use_vosf = false; +#endif + set_video_monitor(width, height, bytes_per_row, true); -#if REAL_ADDRESSING - VideoMonitor.mac_frame_base = (uint32)the_buffer; - MacFrameLayout = FLAYOUT_DIRECT; +#if REAL_ADDRESSING || DIRECT_ADDRESSING + VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer); +// MacFrameLayout = FLAYOUT_DIRECT; #else VideoMonitor.mac_frame_base = MacFrameBaseMac; #endif @@ -654,8 +871,81 @@ static void keycode_init(void) } } +bool VideoInitBuffer() +{ +#ifdef ENABLE_VOSF + if (use_vosf) { + const uint32 page_size = getpagesize(); + const uint32 page_mask = page_size - 1; + + mainBuffer.memBase = (uint32) the_buffer; + // Align the frame buffer on page boundary + mainBuffer.memStart = (uint32)((((unsigned long) the_buffer) + page_mask) & ~page_mask); + mainBuffer.memLength = the_buffer_size; + mainBuffer.memEnd = mainBuffer.memStart + mainBuffer.memLength; + + mainBuffer.pageSize = page_size; + mainBuffer.pageCount = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize; + mainBuffer.pageBits = log_base_2(mainBuffer.pageSize); + + if (mainBuffer.dirtyPages != 0) + free(mainBuffer.dirtyPages); + + mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount); + + if (mainBuffer.pageInfo != 0) + free(mainBuffer.pageInfo); + + mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo)); + + if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0)) + return false; + + PFLAG_CLEAR_ALL; + + uint32 a = 0; + for (int i = 0; i < mainBuffer.pageCount; i++) { + int y1 = a / VideoMonitor.bytes_per_row; + if (y1 >= VideoMonitor.y) + y1 = VideoMonitor.y - 1; + + int y2 = (a + mainBuffer.pageSize) / VideoMonitor.bytes_per_row; + if (y2 >= VideoMonitor.y) + y2 = VideoMonitor.y - 1; + + mainBuffer.pageInfo[i].top = y1; + mainBuffer.pageInfo[i].bottom = y2; + + a += mainBuffer.pageSize; + if (a > mainBuffer.memLength) + a = mainBuffer.memLength; + } + + // We can now write-protect the frame buffer + if (mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ) != 0) + return false; + } +#endif + return true; +} + bool VideoInit(bool classic) { +#ifdef ENABLE_VOSF + // Open /dev/zero + zero_fd = open("/dev/zero", O_RDWR); + if (zero_fd < 0) { + char str[256]; + sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno)); + ErrorAlert(str); + return false; + } + + // Zero the mainBuffer structure + mainBuffer.dirtyPages = 0; + mainBuffer.pageInfo = 0; +#endif + // Check if X server runs on local machine local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0) || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0); @@ -802,12 +1092,10 @@ bool VideoInit(bool classic) break; } -#ifdef HAVE_PTHREADS // Lock down frame buffer - pthread_mutex_lock(&frame_buffer_lock); -#endif + LOCK_FRAME_BUFFER; -#if !REAL_ADDRESSING +#if !REAL_ADDRESSING && !DIRECT_ADDRESSING // Set variables for UAE memory mapping MacFrameBaseHost = the_buffer; MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y; @@ -817,6 +1105,27 @@ bool VideoInit(bool classic) MacFrameLayout = FLAYOUT_NONE; #endif +#ifdef ENABLE_VOSF + if (use_vosf) { + // Initialize the mainBuffer structure + if (!VideoInitBuffer()) { + // TODO: STR_VOSF_INIT_ERR ? + ErrorAlert("Could not initialize Video on SEGV signals"); + return false; + } + + // Initialize the handler for SIGSEGV + if (!Screen_fault_handler_init()) { + // TODO: STR_VOSF_INIT_ERR ? + ErrorAlert("Could not initialize Video on SEGV signals"); + return false; + } + } +#endif + + // Initialize VideoRefresh function + VideoRefreshInit(); + XSync(x_display, false); #ifdef HAVE_PTHREADS @@ -849,10 +1158,8 @@ void VideoExit(void) } #endif -#ifdef HAVE_PTHREADS // Unlock frame buffer - pthread_mutex_unlock(&frame_buffer_lock); -#endif + UNLOCK_FRAME_BUFFER; // Close window and server connection if (x_display != NULL) { @@ -885,17 +1192,51 @@ void VideoExit(void) XFreeColormap(x_display, cmap[0]); XFreeColormap(x_display, cmap[1]); } + + if (!use_vosf) { + if (the_buffer) { + free(the_buffer); + the_buffer = NULL; + } - if (the_buffer) { - free(the_buffer); - the_buffer = NULL; + if (!have_shm && the_buffer_copy) { + free(the_buffer_copy); + the_buffer_copy = NULL; + } + } +#ifdef ENABLE_VOSF + else { + if (the_buffer != (uint8 *)MAP_FAILED) { + munmap((caddr_t)the_buffer, the_buffer_size); + the_buffer = 0; + } + + if (the_buffer_copy != (uint8 *)MAP_FAILED) { + munmap((caddr_t)the_buffer_copy, the_buffer_size); + the_buffer_copy = 0; + } + } +#endif + } + +#ifdef ENABLE_VOSF + if (use_vosf) { + // Clear mainBuffer data + if (mainBuffer.pageInfo) { + free(mainBuffer.pageInfo); + mainBuffer.pageInfo = 0; } - if (!have_shm && the_buffer_copy) { - free(the_buffer_copy); - the_buffer_copy = NULL; + if (mainBuffer.dirtyPages) { + free(mainBuffer.dirtyPages); + mainBuffer.dirtyPages = 0; } } + + // Close /dev/zero + if (zero_fd > 0) + close(zero_fd); +#endif } @@ -921,12 +1262,10 @@ void VideoInterrupt(void) if (emerg_quit) QuitEmulator(); -#ifdef HAVE_PTHREADS // Temporarily give up frame buffer lock (this is the point where // we are suspended when the user presses Ctrl-Tab) - pthread_mutex_unlock(&frame_buffer_lock); - pthread_mutex_lock(&frame_buffer_lock); -#endif + UNLOCK_FRAME_BUFFER; + LOCK_FRAME_BUFFER; } @@ -936,9 +1275,7 @@ void VideoInterrupt(void) void video_set_palette(uint8 *pal) { -#ifdef HAVE_PTHREDS - pthread_mutex_lock(&palette_lock); -#endif + LOCK_PALETTE; // Convert colors to XColor array for (int i=0; i<256; i++) { @@ -952,9 +1289,7 @@ void video_set_palette(uint8 *pal) // Tell redraw thread to change palette palette_changed = true; -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&palette_lock); -#endif + UNLOCK_PALETTE; } @@ -970,10 +1305,8 @@ static void suspend_emul(void) ADBKeyUp(0x36); ctrl_down = false; -#ifdef HAVE_PTHREADS // Lock frame buffer (this will stop the MacOS thread) - pthread_mutex_lock(&frame_buffer_lock); -#endif + LOCK_FRAME_BUFFER; // Save frame buffer fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row); @@ -987,25 +1320,18 @@ static void suspend_emul(void) XUngrabPointer(x_display, CurrentTime); XUngrabKeyboard(x_display, CurrentTime); XUnmapWindow(x_display, the_win); - XSync(x_display, false); + wait_unmapped(the_win); // Open "suspend" window XSetWindowAttributes wattr; wattr.event_mask = KeyPressMask; wattr.background_pixel = black_pixel; - wattr.border_pixel = black_pixel; - wattr.backing_store = Always; - wattr.backing_planes = xdepth; - wattr.colormap = DefaultColormap(x_display, screen); - XSync(x_display, false); suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth, - InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | - CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr); - XSync(x_display, false); - XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE)); - XMapRaised(x_display, suspend_win); - XSync(x_display, false); + InputOutput, vis, CWEventMask | CWBackPixel, &wattr); + set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE); + set_window_focus(suspend_win); + XMapWindow(x_display, suspend_win); emul_suspended = true; } } @@ -1018,8 +1344,8 @@ static void resume_emul(void) // Reopen full screen display XMapRaised(x_display, the_win); + wait_mapped(the_win); XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0); - XSync(x_display, false); XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime); XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); #ifdef ENABLE_XF86_DGA @@ -1027,9 +1353,25 @@ static void resume_emul(void) XF86DGASetViewPort(x_display, screen, 0, 0); #endif XSync(x_display, false); - + + // the_buffer already contains the data to restore. i.e. since a temporary + // frame buffer is used when VOSF is actually used, fb_save is therefore + // not necessary. +#ifdef ENABLE_VOSF + if (use_vosf) { + LOCK_VOSF; + PFLAG_SET_ALL; + UNLOCK_VOSF; + memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y); + } +#endif + // Restore frame buffer if (fb_save) { +#ifdef ENABLE_VOSF + // Don't copy fb_save to the temporary frame buffer in VOSF mode + if (!use_vosf) +#endif memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row); free(fb_save); fb_save = NULL; @@ -1040,11 +1382,9 @@ static void resume_emul(void) XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); #endif -#ifdef HAVE_PTHREADS // Unlock frame buffer (and continue MacOS thread) - pthread_mutex_unlock(&frame_buffer_lock); + UNLOCK_FRAME_BUFFER; emul_suspended = false; -#endif } #endif @@ -1198,14 +1538,14 @@ static int kc_decode(KeySym ks) return -1; } -static int event2keycode(XKeyEvent *ev) +static int event2keycode(XKeyEvent &ev) { KeySym ks; int as; int i = 0; do { - ks = XLookupKeysym(ev, i++); + ks = XLookupKeysym(&ev, i++); as = kc_decode(ks); if (as != -1) return as; @@ -1221,15 +1561,14 @@ static int event2keycode(XKeyEvent *ev) static void handle_events(void) { - XEvent event; - for (;;) { - if (!XCheckMaskEvent(x_display, eventmask, &event)) - break; + while (XPending(x_display)) { + XEvent event; + XNextEvent(x_display, &event); switch (event.type) { // Mouse button case ButtonPress: { - unsigned int button = ((XButtonEvent *)&event)->button; + unsigned int button = event.xbutton.button; if (button < 4) ADBMouseDown(button - 1); else if (button < 6) { // Wheel mouse @@ -1248,7 +1587,7 @@ static void handle_events(void) break; } case ButtonRelease: { - unsigned int button = ((XButtonEvent *)&event)->button; + unsigned int button = event.xbutton.button; if (button < 4) ADBMouseUp(button - 1); break; @@ -1256,20 +1595,18 @@ static void handle_events(void) // Mouse moved case EnterNotify: - ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y); - break; case MotionNotify: - ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y); + ADBMouseMoved(event.xmotion.x, event.xmotion.y); break; // Keyboard case KeyPress: { int code; if (use_keycodes) { - event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys - code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff]; + event2keycode(event.xkey); // This is called to process the hotkeys + code = keycode_table[event.xkey.keycode & 0xff]; } else - code = event2keycode((XKeyEvent *)&event); + code = event2keycode(event.xkey); if (code != -1) { if (!emul_suspended) { if (code == 0x39) { // Caps Lock pressed @@ -1296,10 +1633,10 @@ static void handle_events(void) case KeyRelease: { int code; if (use_keycodes) { - event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys - code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff]; + event2keycode(event.xkey); // This is called to process the hotkeys + code = keycode_table[event.xkey.keycode & 0xff]; } else - code = event2keycode((XKeyEvent *)&event); + code = event2keycode(event.xkey); if (code != -1 && code != 0x39) { // Don't propagate Caps Lock releases ADBKeyUp(code); if (code == 0x36) @@ -1311,16 +1648,33 @@ static void handle_events(void) // Hidden parts exposed, force complete refresh of window case Expose: if (display_type == DISPLAY_WINDOW) { +#ifdef ENABLE_VOSF + if (use_vosf) { // VOSF refresh + LOCK_VOSF; + PFLAG_SET_ALL; + UNLOCK_VOSF; + memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y); + } + else +#endif if (frame_skip == 0) { // Dynamic refresh int x1, y1; for (y1=0; y1<16; y1++) for (x1=0; x1<16; x1++) updt_box[x1][y1] = true; nr_boxes = 16 * 16; - } else + } else // Static refresh memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y); } break; + + // Window "close" widget clicked + case ClientMessage: + if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) { + ADBKeyDown(0x7f); // Power key + ADBKeyUp(0x7f); + } + break; } } } @@ -1449,7 +1803,7 @@ static void update_display_static(void) // Check for first column from left and first column from right that have changed if (high) { if (depth == 1) { - x1 = VideoMonitor.x; + x1 = VideoMonitor.x - 1; for (j=y1; j<=y2; j++) { p = &the_buffer[j * bytes_per_row]; p2 = &the_buffer_copy[j * bytes_per_row]; @@ -1458,8 +1812,7 @@ static void update_display_static(void) x1 = i << 3; break; } - p++; - p2++; + p++; p2++; } } x2 = x1; @@ -1469,15 +1822,14 @@ static void update_display_static(void) p += bytes_per_row; p2 += bytes_per_row; for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) { - p--; - p2--; + p--; p2--; if (*p != *p2) { - x2 = i << 3; + x2 = (i << 3) + 7; break; } } } - wide = x2 - x1; + wide = x2 - x1 + 1; // Update copy of the_buffer if (high && wide) { @@ -1492,13 +1844,12 @@ static void update_display_static(void) for (j=y1; j<=y2; j++) { p = &the_buffer[j * bytes_per_row]; p2 = &the_buffer_copy[j * bytes_per_row]; - for (i=0; ix2; i--) { - p -= bytes_per_pixel; - p2 -= bytes_per_pixel; - if (memcmp(p, p2, bytes_per_pixel)) { - x2 = i; + for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) { + p--; + p2--; + if (*p != *p2) { + x2 = i / bytes_per_pixel; break; } } @@ -1539,39 +1890,55 @@ static void update_display_static(void) /* - * Thread for screen refresh, input handling etc. + * Screen refresh functions */ -void VideoRefresh(void) +// The specialisations hereunder are meant to enable VOSF with DGA in direct +// addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could +// not get mapped correctly with respect to the predetermined host frame +// buffer base address. +// +// Hmm, in other words, when in direct addressing mode and DGA is requested, +// we first try to "triple allocate" the address spaces according to the real +// host frame buffer address. Then, if it fails, we will use a temporary +// frame buffer thus making the real host frame buffer updated when pages +// of the temp frame buffer are altered. +// +// As a side effect, a little speed gain in screen updates could be noticed +// for other modes than DGA. +// +// The following two functions below are inline so that a clever compiler +// could specialise the code according to the current screen depth and +// display type. A more clever compiler would the job by itself though... +// (update_display_vosf is inlined as well) + +static inline void possibly_quit_dga_mode() { #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) // Quit DGA mode if requested if (quit_full_screen) { quit_full_screen = false; - if (display_type == DISPLAY_DGA) { #ifdef ENABLE_XF86_DGA - XF86DGADirectVideo(x_display, screen, 0); + XF86DGADirectVideo(x_display, screen, 0); #endif - XUngrabPointer(x_display, CurrentTime); - XUngrabKeyboard(x_display, CurrentTime); - XUnmapWindow(x_display, the_win); - XSync(x_display, false); - } + XUngrabPointer(x_display, CurrentTime); + XUngrabKeyboard(x_display, CurrentTime); + XUnmapWindow(x_display, the_win); + XSync(x_display, false); } #endif +} - // Handle X events - handle_events(); +static inline void handle_palette_changes(int depth, int display_type) +{ + LOCK_PALETTE; - // Handle palette changes -#ifdef HAVE_PTHREADS - pthread_mutex_lock(&palette_lock); -#endif if (palette_changed) { palette_changed = false; if (depth == 8) { XStoreColors(x_display, cmap[0], palette, 256); XStoreColors(x_display, cmap[1], palette, 256); + XSync(x_display, false); #ifdef ENABLE_XF86_DGA if (display_type == DISPLAY_DGA) { @@ -1581,35 +1948,151 @@ void VideoRefresh(void) #endif } } -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&palette_lock); + + UNLOCK_PALETTE; +} + +static void video_refresh_dga(void) +{ + // Quit DGA mode if requested + possibly_quit_dga_mode(); + + // Handle X events + handle_events(); + + // Handle palette changes + handle_palette_changes(depth, DISPLAY_DGA); +} + +#ifdef ENABLE_VOSF +#if REAL_ADDRESSING || DIRECT_ADDRESSING +static void video_refresh_dga_vosf(void) +{ + // Quit DGA mode if requested + possibly_quit_dga_mode(); + + // Handle X events + handle_events(); + + // Handle palette changes + handle_palette_changes(depth, DISPLAY_DGA); + + // Update display (VOSF variant) + static int tick_counter = 0; + if (++tick_counter >= frame_skip) { + tick_counter = 0; + LOCK_VOSF; + update_display_dga_vosf(); + UNLOCK_VOSF; + } +} #endif - // In window mode, update display +static void video_refresh_window_vosf(void) +{ + // Quit DGA mode if requested + possibly_quit_dga_mode(); + + // Handle X events + handle_events(); + + // Handle palette changes + handle_palette_changes(depth, DISPLAY_WINDOW); + + // Update display (VOSF variant) + static int tick_counter = 0; + if (++tick_counter >= frame_skip) { + tick_counter = 0; + LOCK_VOSF; + update_display_window_vosf(); + UNLOCK_VOSF; + } +} +#endif // def ENABLE_VOSF + +static void video_refresh_window_static(void) +{ + // Handle X events + handle_events(); + + // Handle_palette changes + handle_palette_changes(depth, DISPLAY_WINDOW); + + // Update display (static variant) + static int tick_counter = 0; + if (++tick_counter >= frame_skip) { + tick_counter = 0; + update_display_static(); + } +} + +static void video_refresh_window_dynamic(void) +{ + // Handle X events + handle_events(); + + // Handle_palette changes + handle_palette_changes(depth, DISPLAY_WINDOW); + + // Update display (dynamic variant) static int tick_counter = 0; - if (display_type == DISPLAY_WINDOW) { - tick_counter++; + tick_counter++; + update_display_dynamic(tick_counter); +} + + +/* + * Thread for screen refresh, input handling etc. + */ + +void VideoRefreshInit(void) +{ + // TODO: set up specialised 8bpp VideoRefresh handlers ? + if (display_type == DISPLAY_DGA) { +#if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING) + if (use_vosf) + video_refresh = video_refresh_dga_vosf; + else +#endif + video_refresh = video_refresh_dga; + } + else { +#ifdef ENABLE_VOSF + if (use_vosf) + video_refresh = video_refresh_window_vosf; + else +#endif if (frame_skip == 0) - update_display_dynamic(tick_counter); - else if (tick_counter >= frame_skip) { - tick_counter = 0; - update_display_static(); - } + video_refresh = video_refresh_window_dynamic; + else + video_refresh = video_refresh_window_static; } } +void VideoRefresh(void) +{ + // TODO: make main_unix/VideoRefresh call directly video_refresh() ? + video_refresh(); +} + #ifdef HAVE_PTHREADS static void *redraw_func(void *arg) { + uint64 start = GetTicks_usec(); + int64 ticks = 0; + uint64 next = GetTicks_usec(); while (!redraw_thread_cancel) { -#ifdef HAVE_NANOSLEEP - struct timespec req = {0, 16666667}; - nanosleep(&req, NULL); -#else - usleep(16667); -#endif - VideoRefresh(); + video_refresh(); + next += 16667; + int64 delay = next - GetTicks_usec(); + if (delay > 0) + Delay_usec(delay); + else if (delay < -16667) + next = GetTicks_usec(); + ticks++; } + uint64 end = GetTicks_usec(); + printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks); return NULL; } #endif