ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
(Generate patch)

Comparing BasiliskII/src/Unix/video_x.cpp (file contents):
Revision 1.17 by cebix, 2000-07-22T16:07:21Z vs.
Revision 1.28 by cebix, 2000-11-03T12:23:49Z

# Line 52 | Line 52
52   # include <sys/mman.h>
53   #endif
54  
55 + #ifdef ENABLE_VOSF
56 + # include <unistd.h>
57 + # include <signal.h>
58 + # include <fcntl.h>
59 + # include <sys/mman.h>
60 + #endif
61 +
62   #include "cpu_emulation.h"
63   #include "main.h"
64   #include "adb.h"
# Line 115 | Line 122 | static Colormap cmap[2];                                                       // Two co
122   static XColor black, white;
123   static unsigned long black_pixel, white_pixel;
124   static int eventmask;
125 < static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask;
125 > static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | FocusChangeMask | ExposureMask;
126   static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
127  
128   static XColor palette[256];                                                     // Color palette for 8-bit mode
# Line 154 | Line 161 | static XF86VidModeModeInfo **x_video_mod
161   static int num_x_video_modes;
162   #endif
163  
164 + #ifdef ENABLE_VOSF
165 + static bool use_vosf = true;                                            // Flag: VOSF enabled
166 + #else
167 + static const bool use_vosf = false;                                     // Flag: VOSF enabled
168 + #endif
169 +
170 + #ifdef ENABLE_VOSF
171 + static uint8 * the_host_buffer;                                         // Host frame buffer in VOSF mode
172 + static uint32 the_buffer_size;                                          // Size of allocated the_buffer
173 + #endif
174 +
175 + #ifdef ENABLE_VOSF
176 + // Variables for Video on SEGV support (taken from the Win32 port)
177 + struct ScreenPageInfo {
178 +    int top, bottom;                    // Mapping between this virtual page and Mac scanlines
179 + };
180 +
181 + struct ScreenInfo {
182 +    uint32 memBase;                             // Real start address
183 +    uint32 memStart;                    // Start address aligned to page boundary
184 +    uint32 memEnd;                              // Address of one-past-the-end of the screen
185 +    uint32 memLength;                   // Length of the memory addressed by the screen pages
186 +    
187 +    uint32 pageSize;                    // Size of a page
188 +    int pageBits;                               // Shift count to get the page number
189 +    uint32 pageCount;                   // Number of pages allocated to the screen
190 +    
191 +    uint8 * dirtyPages;                 // Table of flags set if page was altered
192 +    ScreenPageInfo * pageInfo;  // Table of mappings page -> Mac scanlines
193 + };
194 +
195 + static ScreenInfo mainBuffer;
196 +
197 + #define PFLAG_SET(page)                 mainBuffer.dirtyPages[page] = 1
198 + #define PFLAG_CLEAR(page)               mainBuffer.dirtyPages[page] = 0
199 + #define PFLAG_ISSET(page)               mainBuffer.dirtyPages[page]
200 + #define PFLAG_ISCLEAR(page)             (mainBuffer.dirtyPages[page] == 0)
201 + #ifdef UNALIGNED_PROFITABLE
202 + # define PFLAG_ISCLEAR_4(page)  (*((uint32 *)(mainBuffer.dirtyPages + page)) == 0)
203 + #else
204 + # define PFLAG_ISCLEAR_4(page)  \
205 +                (mainBuffer.dirtyPages[page  ] == 0) \
206 +        &&      (mainBuffer.dirtyPages[page+1] == 0) \
207 +        &&      (mainBuffer.dirtyPages[page+2] == 0) \
208 +        &&      (mainBuffer.dirtyPages[page+3] == 0)
209 + #endif
210 + #define PFLAG_CLEAR_ALL                 memset(mainBuffer.dirtyPages, 0, mainBuffer.pageCount)
211 + #define PFLAG_SET_ALL                   memset(mainBuffer.dirtyPages, 1, mainBuffer.pageCount)
212 +
213 + static int zero_fd = -1;
214 + static bool Screen_fault_handler_init();
215 + static struct sigaction vosf_sa;
216 +
217 + #ifdef HAVE_PTHREADS
218 + static pthread_mutex_t Screen_draw_lock = PTHREAD_MUTEX_INITIALIZER;    // Mutex to protect frame buffer (dirtyPages in fact)
219 + #endif
220 +
221 + static int log_base_2(uint32 x)
222 + {
223 +        uint32 mask = 0x80000000;
224 +        int l = 31;
225 +        while (l >= 0 && (x & mask) == 0) {
226 +                mask >>= 1;
227 +                l--;
228 +        }
229 +        return l;
230 + }
231 +
232 + #endif
233 +
234 + // VideoRefresh function
235 + void VideoRefreshInit(void);
236 + static void (*video_refresh)(void);
237  
238   // Prototypes
239   static void *redraw_func(void *arg);
# Line 167 | Line 247 | extern Display *x_display;
247   // From sys_unix.cpp
248   extern void SysMountFirstFloppy(void);
249  
250 + #ifdef ENABLE_VOSF
251 + # include "video_vosf.h"
252 + #endif
253 +
254  
255   /*
256   *  Initialization
# Line 175 | Line 259 | extern void SysMountFirstFloppy(void);
259   // Set VideoMonitor according to video mode
260   void set_video_monitor(int width, int height, int bytes_per_row, bool native_byte_order)
261   {
262 < #if !REAL_ADDRESSING
262 > #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
263          int layout = FLAYOUT_DIRECT;
264          switch (depth) {
265                  case 1:
# Line 252 | Line 336 | static bool init_window(int width, int h
336          XSetWindowAttributes wattr;
337          wattr.event_mask = eventmask = win_eventmask;
338          wattr.background_pixel = black_pixel;
255        wattr.border_pixel = black_pixel;
256        wattr.backing_store = NotUseful;
257        wattr.save_under = false;
258        wattr.backing_planes = xdepth;
339  
340          XSync(x_display, false);
341          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
342 <                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
263 <                CWBackingStore | CWBackingPlanes, &wattr);
264 <        XSync(x_display, false);
265 <        XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
266 <        XMapRaised(x_display, the_win);
267 <        XSync(x_display, false);
342 >                InputOutput, vis, CWEventMask | CWBackPixel, &wattr);
343  
344 <        // Set colormap
345 <        if (depth == 8) {
346 <                XSetWindowColormap(x_display, the_win, cmap[0]);
347 <                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
344 >        // Indicate that we want keyboard input
345 >        {
346 >                XWMHints *hints = XAllocWMHints();
347 >                if (hints) {
348 >                        hints->input = True;
349 >                        hints->flags = InputHint;
350 >                        XSetWMHints(x_display, the_win, hints);
351 >                        XFree((char *)hints);
352 >                }
353          }
354  
355          // Make window unresizable
356 <        XSizeHints *hints;
357 <        if ((hints = XAllocSizeHints()) != NULL) {
358 <                hints->min_width = width;
359 <                hints->max_width = width;
360 <                hints->min_height = height;
361 <                hints->max_height = height;
362 <                hints->flags = PMinSize | PMaxSize;
363 <                XSetWMNormalHints(x_display, the_win, hints);
364 <                XFree((char *)hints);
356 >        {
357 >                XSizeHints *hints = XAllocSizeHints();
358 >                if (hints) {
359 >                        hints->min_width = width;
360 >                        hints->max_width = width;
361 >                        hints->min_height = height;
362 >                        hints->max_height = height;
363 >                        hints->flags = PMinSize | PMaxSize;
364 >                        XSetWMNormalHints(x_display, the_win, hints);
365 >                        XFree((char *)hints);
366 >                }
367          }
368          
369 +        // Set window title
370 +        XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
371 +
372 +        // Set window class
373 +        {
374 +                XClassHint *hints;
375 +                hints = XAllocClassHint();
376 +                if (hints) {
377 +                        hints->res_name = "BasiliskII";
378 +                        hints->res_class = "BasiliskII";
379 +                        XSetClassHint(x_display, the_win, hints);
380 +                        XFree((char *)hints);
381 +                }
382 +        }
383 +
384 +        // Show window
385 +        XSync(x_display, false);
386 +        XMapRaised(x_display, the_win);
387 +        XFlush(x_display);
388 +
389 +        // Set colormap
390 +        if (depth == 8)
391 +                XSetWindowColormap(x_display, the_win, cmap[0]);
392 +
393          // Try to create and attach SHM image
394          have_shm = false;
395          if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) {
# Line 337 | Line 443 | static bool init_window(int width, int h
443                  img->bitmap_bit_order = MSBFirst;
444          }
445  
446 + #ifdef ENABLE_VOSF
447 +        // Allocate a page-aligned chunk of memory for frame buffer
448 +        the_buffer_size = align_on_page_boundary((aligned_height + 2) * img->bytes_per_line);
449 +        the_host_buffer = the_buffer_copy;
450 +        
451 +        the_buffer_copy = (uint8 *)allocate_framebuffer(the_buffer_size);
452 +        memset(the_buffer_copy, 0, the_buffer_size);
453 +        
454 +        the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
455 +        memset(the_buffer, 0, the_buffer_size);
456 + #else
457          // Allocate memory for frame buffer
458          the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
459 + #endif
460  
461          // Create GC
462          the_gc = XCreateGC(x_display, the_win, 0, 0);
# Line 352 | Line 470 | static bool init_window(int width, int h
470          XDefineCursor(x_display, the_win, mac_cursor);
471  
472          // Set VideoMonitor
473 +        bool native_byte_order;
474   #ifdef WORDS_BIGENDIAN
475 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == MSBFirst);
475 >        native_byte_order = (img->bitmap_bit_order == MSBFirst);
476   #else
477 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == LSBFirst);
477 >        native_byte_order = (img->bitmap_bit_order == LSBFirst);
478 > #endif
479 > #ifdef ENABLE_VOSF
480 >        do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW);
481   #endif
482 +        set_video_monitor(width, height, img->bytes_per_line, native_byte_order);
483          
484 < #if REAL_ADDRESSING
485 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
484 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
485 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
486   #else
487          VideoMonitor.mac_frame_base = MacFrameBaseMac;
488   #endif
# Line 438 | Line 561 | static bool init_fbdev_dga(char *in_fb_n
561          
562          // Create window
563          XSetWindowAttributes wattr;
441        wattr.override_redirect = True;
442        wattr.backing_store             = NotUseful;
443        wattr.background_pixel  = white_pixel;
444        wattr.border_pixel              = black_pixel;
564          wattr.event_mask                = eventmask = dga_eventmask;
565 +        wattr.background_pixel  = white_pixel;
566 +        wattr.override_redirect = True;
567          
568          XSync(x_display, false);
569          the_win = XCreateWindow(x_display, rootwin,
570                  0, 0, width, height,
571                  0, xdepth, InputOutput, vis,
572 <                CWEventMask|CWBackPixel|CWBorderPixel|CWOverrideRedirect|CWBackingStore,
572 >                CWEventMask | CWBackPixel | CWOverrideRedirect,
573                  &wattr);
574          XSync(x_display, false);
575          XMapRaised(x_display, the_win);
# Line 462 | Line 583 | static bool init_fbdev_dga(char *in_fb_n
583                  GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
584          
585          // Set colormap
586 <        if (depth == 8) {
586 >        if (depth == 8)
587                  XSetWindowColormap(x_display, the_win, cmap[0]);
467                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
468        }
588          
589          // Set VideoMonitor
590          int bytes_per_row = width;
# Line 492 | Line 611 | static bool init_fbdev_dga(char *in_fb_n
611                  }
612          }
613          
614 + #if ENABLE_VOSF
615 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
616 +        // If the blit function is null, i.e. just a copy of the buffer,
617 +        // we first try to avoid the allocation of a temporary frame buffer
618 +        use_vosf = true;
619 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
620 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
621 +                use_vosf = false;
622 +        
623 +        if (use_vosf) {
624 +                the_host_buffer = the_buffer;
625 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
626 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
627 +                memset(the_buffer_copy, 0, the_buffer_size);
628 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
629 +                memset(the_buffer, 0, the_buffer_size);
630 +        }
631 + #else
632 +        use_vosf = false;
633 + #endif
634 + #endif
635 +        
636          set_video_monitor(width, height, bytes_per_row, true);
637 < #if REAL_ADDRESSING
638 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
637 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
638 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
639   #else
640          VideoMonitor.mac_frame_base = MacFrameBaseMac;
641   #endif
# Line 530 | Line 671 | static bool init_xf86_dga(int width, int
671          // Create window
672          XSetWindowAttributes wattr;
673          wattr.event_mask = eventmask = dga_eventmask;
533        wattr.border_pixel = black_pixel;
674          wattr.override_redirect = True;
675  
676          XSync(x_display, false);
677          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
678 <                InputOutput, vis, CWEventMask | CWBorderPixel | CWOverrideRedirect, &wattr);
678 >                InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
679          XSync(x_display, false);
680          XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
681          XMapRaised(x_display, the_win);
# Line 556 | Line 696 | static bool init_xf86_dga(int width, int
696          // Set colormap
697          if (depth == 8) {
698                  XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
559                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
699                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
700          }
701  
# Line 575 | Line 714 | static bool init_xf86_dga(int width, int
714                          bytes_per_row *= 4;
715                          break;
716          }
717 +        
718 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
719 +        // If the blit function is null, i.e. just a copy of the buffer,
720 +        // we first try to avoid the allocation of a temporary frame buffer
721 +        use_vosf = true;
722 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
723 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
724 +                use_vosf = false;
725 +        
726 +        if (use_vosf) {
727 +                the_host_buffer = the_buffer;
728 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
729 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
730 +                memset(the_buffer_copy, 0, the_buffer_size);
731 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
732 +                memset(the_buffer, 0, the_buffer_size);
733 +        }
734 + #elif defined(ENABLE_VOSF)
735 +        // The UAE memory handlers will already handle color conversion, if needed.
736 +        use_vosf = false;
737 + #endif
738 +        
739          set_video_monitor(width, height, bytes_per_row, true);
740 < #if REAL_ADDRESSING
741 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
742 <        MacFrameLayout = FLAYOUT_DIRECT;
740 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
741 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
742 > //      MacFrameLayout = FLAYOUT_DIRECT;
743   #else
744          VideoMonitor.mac_frame_base = MacFrameBaseMac;
745   #endif
# Line 654 | Line 815 | static void keycode_init(void)
815          }
816   }
817  
818 + bool VideoInitBuffer()
819 + {
820 + #ifdef ENABLE_VOSF
821 +        if (use_vosf) {
822 +                const uint32 page_size  = getpagesize();
823 +                const uint32 page_mask  = page_size - 1;
824 +                
825 +                mainBuffer.memBase      = (uint32) the_buffer;
826 +                // Align the frame buffer on page boundary
827 +                mainBuffer.memStart             = (uint32)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
828 +                mainBuffer.memLength    = the_buffer_size;
829 +                mainBuffer.memEnd       = mainBuffer.memStart + mainBuffer.memLength;
830 +
831 +                mainBuffer.pageSize     = page_size;
832 +                mainBuffer.pageCount    = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
833 +                mainBuffer.pageBits     = log_base_2(mainBuffer.pageSize);
834 +
835 +                if (mainBuffer.dirtyPages != 0)
836 +                        free(mainBuffer.dirtyPages);
837 +
838 +                mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount);
839 +
840 +                if (mainBuffer.pageInfo != 0)
841 +                        free(mainBuffer.pageInfo);
842 +
843 +                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
844 +
845 +                if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0))
846 +                        return false;
847 +
848 +                PFLAG_CLEAR_ALL;
849 +
850 +                uint32 a = 0;
851 +                for (int i = 0; i < mainBuffer.pageCount; i++) {
852 +                        int y1 = a / VideoMonitor.bytes_per_row;
853 +                        if (y1 >= VideoMonitor.y)
854 +                                y1 = VideoMonitor.y - 1;
855 +
856 +                        int y2 = (a + mainBuffer.pageSize) / VideoMonitor.bytes_per_row;
857 +                        if (y2 >= VideoMonitor.y)
858 +                                y2 = VideoMonitor.y - 1;
859 +
860 +                        mainBuffer.pageInfo[i].top = y1;
861 +                        mainBuffer.pageInfo[i].bottom = y2;
862 +
863 +                        a += mainBuffer.pageSize;
864 +                        if (a > mainBuffer.memLength)
865 +                                a = mainBuffer.memLength;
866 +                }
867 +                
868 +                // We can now write-protect the frame buffer
869 +                if (mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ) != 0)
870 +                        return false;
871 +        }
872 + #endif
873 +        return true;
874 + }
875 +
876   bool VideoInit(bool classic)
877   {
878 + #ifdef ENABLE_VOSF
879 +        // Open /dev/zero
880 +        zero_fd = open("/dev/zero", O_RDWR);
881 +        if (zero_fd < 0) {
882 +        char str[256];
883 +                sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
884 +                ErrorAlert(str);
885 +        return false;
886 +        }
887 +        
888 +        // Zero the mainBuffer structure
889 +        mainBuffer.dirtyPages = 0;
890 +        mainBuffer.pageInfo = 0;
891 + #endif
892 +        
893          // Check if X server runs on local machine
894          local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
895                   || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
# Line 807 | Line 1041 | bool VideoInit(bool classic)
1041          pthread_mutex_lock(&frame_buffer_lock);
1042   #endif
1043  
1044 < #if !REAL_ADDRESSING
1044 > #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1045          // Set variables for UAE memory mapping
1046          MacFrameBaseHost = the_buffer;
1047          MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
# Line 817 | Line 1051 | bool VideoInit(bool classic)
1051                  MacFrameLayout = FLAYOUT_NONE;
1052   #endif
1053  
1054 + #ifdef ENABLE_VOSF
1055 +        if (use_vosf) {
1056 +                // Initialize the mainBuffer structure
1057 +                if (!VideoInitBuffer()) {
1058 +                        // TODO: STR_VOSF_INIT_ERR ?
1059 +                        ErrorAlert("Could not initialize Video on SEGV signals");
1060 +                return false;
1061 +                }
1062 +
1063 +                // Initialize the handler for SIGSEGV
1064 +                if (!Screen_fault_handler_init()) {
1065 +                        // TODO: STR_VOSF_INIT_ERR ?
1066 +                        ErrorAlert("Could not initialize Video on SEGV signals");
1067 +                        return false;
1068 +                }
1069 +        }
1070 + #endif
1071 +        
1072 +        // Initialize VideoRefresh function
1073 +        VideoRefreshInit();
1074 +        
1075          XSync(x_display, false);
1076  
1077   #ifdef HAVE_PTHREADS
# Line 885 | Line 1140 | void VideoExit(void)
1140                          XFreeColormap(x_display, cmap[0]);
1141                          XFreeColormap(x_display, cmap[1]);
1142                  }
1143 +                
1144 +                if (!use_vosf) {
1145 +                        if (the_buffer) {
1146 +                                free(the_buffer);
1147 +                                the_buffer = NULL;
1148 +                        }
1149  
1150 <                if (the_buffer) {
1151 <                        free(the_buffer);
1152 <                        the_buffer = NULL;
1150 >                        if (!have_shm && the_buffer_copy) {
1151 >                                free(the_buffer_copy);
1152 >                                the_buffer_copy = NULL;
1153 >                        }
1154 >                }
1155 > #ifdef ENABLE_VOSF
1156 >                else {
1157 >                        if (the_buffer != (uint8 *)MAP_FAILED) {
1158 >                                munmap((caddr_t)the_buffer, the_buffer_size);
1159 >                                the_buffer = 0;
1160 >                        }
1161 >                        
1162 >                        if (the_buffer_copy != (uint8 *)MAP_FAILED) {
1163 >                                munmap((caddr_t)the_buffer_copy, the_buffer_size);
1164 >                                the_buffer_copy = 0;
1165 >                        }
1166 >                }
1167 > #endif
1168 >        }
1169 >        
1170 > #ifdef ENABLE_VOSF
1171 >        if (use_vosf) {
1172 >                // Clear mainBuffer data
1173 >                if (mainBuffer.pageInfo) {
1174 >                        free(mainBuffer.pageInfo);
1175 >                        mainBuffer.pageInfo = 0;
1176                  }
1177  
1178 <                if (!have_shm && the_buffer_copy) {
1179 <                        free(the_buffer_copy);
1180 <                        the_buffer_copy = NULL;
1178 >                if (mainBuffer.dirtyPages) {
1179 >                        free(mainBuffer.dirtyPages);
1180 >                        mainBuffer.dirtyPages = 0;
1181                  }
1182          }
1183 +
1184 +        // Close /dev/zero
1185 +        if (zero_fd > 0)
1186 +                close(zero_fd);
1187 + #endif
1188   }
1189  
1190  
# Line 936 | Line 1225 | void VideoInterrupt(void)
1225  
1226   void video_set_palette(uint8 *pal)
1227   {
1228 < #ifdef HAVE_PTHREDS
1228 > #ifdef HAVE_PTHREADS
1229          pthread_mutex_lock(&palette_lock);
1230   #endif
1231  
# Line 993 | Line 1282 | static void suspend_emul(void)
1282                  XSetWindowAttributes wattr;
1283                  wattr.event_mask = KeyPressMask;
1284                  wattr.background_pixel = black_pixel;
1285 <                wattr.border_pixel = black_pixel;
997 <                wattr.backing_store = Always;
1285 >                wattr.backing_store = WhenMapped;
1286                  wattr.backing_planes = xdepth;
1287                  wattr.colormap = DefaultColormap(x_display, screen);
1288                  
1289                  XSync(x_display, false);
1290                  suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1291 <                        InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1292 <                        CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1291 >                        InputOutput, vis, CWEventMask | CWBackPixel | CWBackingStore |
1292 >                        CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1293                  XSync(x_display, false);
1294                  XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1295                  XMapRaised(x_display, suspend_win);
# Line 1027 | Line 1315 | static void resume_emul(void)
1315          XF86DGASetViewPort(x_display, screen, 0, 0);
1316   #endif
1317          XSync(x_display, false);
1318 <
1318 >        
1319 >        // the_buffer already contains the data to restore. i.e. since a temporary
1320 >        // frame buffer is used when VOSF is actually used, fb_save is therefore
1321 >        // not necessary.
1322 > #ifdef ENABLE_VOSF
1323 >        if (use_vosf) {
1324 > #ifdef HAVE_PTHREADS
1325 >                pthread_mutex_lock(&Screen_draw_lock);
1326 > #endif
1327 >                PFLAG_SET_ALL;
1328 > #ifdef HAVE_PTHREADS
1329 >                pthread_mutex_unlock(&Screen_draw_lock);
1330 > #endif
1331 >                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1332 >        }
1333 > #endif
1334 >        
1335          // Restore frame buffer
1336          if (fb_save) {
1337 + #ifdef ENABLE_VOSF
1338 +                // Don't copy fb_save to the temporary frame buffer in VOSF mode
1339 +                if (!use_vosf)
1340 + #endif
1341                  memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row);
1342                  free(fb_save);
1343                  fb_save = NULL;
# Line 1311 | Line 1619 | static void handle_events(void)
1619                          // Hidden parts exposed, force complete refresh of window
1620                          case Expose:
1621                                  if (display_type == DISPLAY_WINDOW) {
1622 + #ifdef ENABLE_VOSF
1623 +                                        if (use_vosf) {                 // VOSF refresh
1624 + #ifdef HAVE_PTHREADS
1625 +                                                pthread_mutex_lock(&Screen_draw_lock);
1626 + #endif
1627 +                                                PFLAG_SET_ALL;
1628 + #ifdef HAVE_PTHREADS
1629 +                                                pthread_mutex_unlock(&Screen_draw_lock);
1630 + #endif
1631 +                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1632 +                                        }
1633 +                                        else
1634 + #endif
1635                                          if (frame_skip == 0) {  // Dynamic refresh
1636                                                  int x1, y1;
1637                                                  for (y1=0; y1<16; y1++)
1638                                                  for (x1=0; x1<16; x1++)
1639                                                          updt_box[x1][y1] = true;
1640                                                  nr_boxes = 16 * 16;
1641 <                                        } else
1641 >                                        } else                                  // Static refresh
1642                                                  memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1643                                  }
1644                                  break;
1645 +
1646 +                        case FocusIn:
1647 +                        case FocusOut:
1648 +                                break;
1649                  }
1650          }
1651   }
# Line 1449 | Line 1774 | static void update_display_static(void)
1774          // Check for first column from left and first column from right that have changed
1775          if (high) {
1776                  if (depth == 1) {
1777 <                        x1 = VideoMonitor.x;
1777 >                        x1 = VideoMonitor.x - 1;
1778                          for (j=y1; j<=y2; j++) {
1779                                  p = &the_buffer[j * bytes_per_row];
1780                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1458 | Line 1783 | static void update_display_static(void)
1783                                                  x1 = i << 3;
1784                                                  break;
1785                                          }
1786 <                                        p++;
1462 <                                        p2++;
1786 >                                        p++; p2++;
1787                                  }
1788                          }
1789                          x2 = x1;
# Line 1469 | Line 1793 | static void update_display_static(void)
1793                                  p += bytes_per_row;
1794                                  p2 += bytes_per_row;
1795                                  for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1796 <                                        p--;
1473 <                                        p2--;
1796 >                                        p--; p2--;
1797                                          if (*p != *p2) {
1798 <                                                x2 = i << 3;
1798 >                                                x2 = (i << 3) + 7;
1799                                                  break;
1800                                          }
1801                                  }
1802                          }
1803 <                        wide = x2 - x1;
1803 >                        wide = x2 - x1 + 1;
1804  
1805                          // Update copy of the_buffer
1806                          if (high && wide) {
# Line 1492 | Line 1815 | static void update_display_static(void)
1815                          for (j=y1; j<=y2; j++) {
1816                                  p = &the_buffer[j * bytes_per_row];
1817                                  p2 = &the_buffer_copy[j * bytes_per_row];
1818 <                                for (i=0; i<x1; i++) {
1819 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1820 <                                                x1 = i;
1818 >                                for (i=0; i<x1*bytes_per_pixel; i++) {
1819 >                                        if (*p != *p2) {
1820 >                                                x1 = i / bytes_per_pixel;
1821                                                  break;
1822                                          }
1823 <                                        p += bytes_per_pixel;
1501 <                                        p2 += bytes_per_pixel;
1823 >                                        p++; p2++;
1824                                  }
1825                          }
1826                          x2 = x1;
# Line 1507 | Line 1829 | static void update_display_static(void)
1829                                  p2 = &the_buffer_copy[j * bytes_per_row];
1830                                  p += bytes_per_row;
1831                                  p2 += bytes_per_row;
1832 <                                for (i=VideoMonitor.x; i>x2; i--) {
1833 <                                        p -= bytes_per_pixel;
1834 <                                        p2 -= bytes_per_pixel;
1835 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1836 <                                                x2 = i;
1832 >                                for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1833 >                                        p--;
1834 >                                        p2--;
1835 >                                        if (*p != *p2) {
1836 >                                                x2 = i / bytes_per_pixel;
1837                                                  break;
1838                                          }
1839                                  }
# Line 1539 | Line 1861 | static void update_display_static(void)
1861  
1862  
1863   /*
1864 + *      Screen refresh functions
1865 + */
1866 +
1867 + // The specialisations hereunder are meant to enable VOSF with DGA in direct
1868 + // addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could
1869 + // not get mapped correctly with respect to the predetermined host frame
1870 + // buffer base address.
1871 + //
1872 + // Hmm, in other words, when in direct addressing mode and DGA is requested,
1873 + // we first try to "triple allocate" the address spaces according to the real
1874 + // host frame buffer address. Then, if it fails, we will use a temporary
1875 + // frame buffer thus making the real host frame buffer updated when pages
1876 + // of the temp frame buffer are altered.
1877 + //
1878 + // As a side effect, a little speed gain in screen updates could be noticed
1879 + // for other modes than DGA.
1880 + //
1881 + // The following two functions below are inline so that a clever compiler
1882 + // could specialise the code according to the current screen depth and
1883 + // display type. A more clever compiler would the job by itself though...
1884 + // (update_display_vosf is inlined as well)
1885 +
1886 + static inline void possibly_quit_dga_mode()
1887 + {
1888 + #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1889 +        // Quit DGA mode if requested
1890 +        if (quit_full_screen) {
1891 +                quit_full_screen = false;
1892 + #ifdef ENABLE_XF86_DGA
1893 +                XF86DGADirectVideo(x_display, screen, 0);
1894 + #endif
1895 +                XUngrabPointer(x_display, CurrentTime);
1896 +                XUngrabKeyboard(x_display, CurrentTime);
1897 +                XUnmapWindow(x_display, the_win);
1898 +                XSync(x_display, false);
1899 +        }
1900 + #endif
1901 + }
1902 +
1903 + static inline void handle_palette_changes(int depth, int display_type)
1904 + {
1905 + #ifdef HAVE_PTHREADS
1906 +        pthread_mutex_lock(&palette_lock);
1907 + #endif
1908 +        if (palette_changed) {
1909 +                palette_changed = false;
1910 +                if (depth == 8) {
1911 +                        XStoreColors(x_display, cmap[0], palette, 256);
1912 +                        XStoreColors(x_display, cmap[1], palette, 256);
1913 +                                
1914 + #ifdef ENABLE_XF86_DGA
1915 +                        if (display_type == DISPLAY_DGA) {
1916 +                                current_dga_cmap ^= 1;
1917 +                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1918 +                        }
1919 + #endif
1920 +                }
1921 +        }
1922 + #ifdef HAVE_PTHREADS
1923 +        pthread_mutex_unlock(&palette_lock);
1924 + #endif
1925 + }
1926 +
1927 + static void video_refresh_dga(void)
1928 + {
1929 +        // Quit DGA mode if requested
1930 +        possibly_quit_dga_mode();
1931 +        
1932 +        // Handle X events
1933 +        handle_events();
1934 +        
1935 +        // Handle palette changes
1936 +        handle_palette_changes(depth, DISPLAY_DGA);
1937 + }
1938 +
1939 + #ifdef ENABLE_VOSF
1940 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
1941 + static void video_refresh_dga_vosf(void)
1942 + {
1943 +        // Quit DGA mode if requested
1944 +        possibly_quit_dga_mode();
1945 +        
1946 +        // Handle X events
1947 +        handle_events();
1948 +        
1949 +        // Handle palette changes
1950 +        handle_palette_changes(depth, DISPLAY_DGA);
1951 +        
1952 +        // Update display (VOSF variant)
1953 +        static int tick_counter = 0;
1954 +        if (++tick_counter >= frame_skip) {
1955 +                tick_counter = 0;
1956 + #ifdef HAVE_PTHREADS
1957 +                pthread_mutex_lock(&Screen_draw_lock);
1958 + #endif
1959 +                update_display_dga_vosf();
1960 + #ifdef HAVE_PTHREADS
1961 +                pthread_mutex_unlock(&Screen_draw_lock);
1962 + #endif
1963 +        }
1964 + }
1965 + #endif
1966 +
1967 + static void video_refresh_window_vosf(void)
1968 + {
1969 +        // Quit DGA mode if requested
1970 +        possibly_quit_dga_mode();
1971 +        
1972 +        // Handle X events
1973 +        handle_events();
1974 +        
1975 +        // Handle palette changes
1976 +        handle_palette_changes(depth, DISPLAY_WINDOW);
1977 +        
1978 +        // Update display (VOSF variant)
1979 +        static int tick_counter = 0;
1980 +        if (++tick_counter >= frame_skip) {
1981 +                tick_counter = 0;
1982 + #ifdef HAVE_PTHREADS
1983 +                pthread_mutex_lock(&Screen_draw_lock);
1984 + #endif
1985 +                update_display_window_vosf();
1986 + #ifdef HAVE_PTHREADS
1987 +                pthread_mutex_unlock(&Screen_draw_lock);
1988 + #endif
1989 +        }
1990 + }
1991 + #endif // def ENABLE_VOSF
1992 +
1993 + static void video_refresh_window_static(void)
1994 + {
1995 +        // Handle X events
1996 +        handle_events();
1997 +        
1998 +        // Handle_palette changes
1999 +        handle_palette_changes(depth, DISPLAY_WINDOW);
2000 +        
2001 +        // Update display (static variant)
2002 +        static int tick_counter = 0;
2003 +        if (++tick_counter >= frame_skip) {
2004 +                tick_counter = 0;
2005 +                update_display_static();
2006 +        }
2007 + }
2008 +
2009 + static void video_refresh_window_dynamic(void)
2010 + {
2011 +        // Handle X events
2012 +        handle_events();
2013 +        
2014 +        // Handle_palette changes
2015 +        handle_palette_changes(depth, DISPLAY_WINDOW);
2016 +        
2017 +        // Update display (dynamic variant)
2018 +        static int tick_counter = 0;
2019 +        tick_counter++;
2020 +        update_display_dynamic(tick_counter);
2021 + }
2022 +
2023 +
2024 + /*
2025   *  Thread for screen refresh, input handling etc.
2026   */
2027  
2028 + void VideoRefreshInit(void)
2029 + {
2030 +        // TODO: set up specialised 8bpp VideoRefresh handlers ?
2031 +        if (display_type == DISPLAY_DGA) {
2032 + #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
2033 +                if (use_vosf)
2034 +                        video_refresh = video_refresh_dga_vosf;
2035 +                else
2036 + #endif
2037 +                        video_refresh = video_refresh_dga;
2038 +        }
2039 +        else {
2040 + #ifdef ENABLE_VOSF
2041 +                if (use_vosf)
2042 +                        video_refresh = video_refresh_window_vosf;
2043 +                else
2044 + #endif
2045 +                if (frame_skip == 0)
2046 +                        video_refresh = video_refresh_window_dynamic;
2047 +                else
2048 +                        video_refresh = video_refresh_window_static;
2049 +        }
2050 + }
2051 +
2052 + void VideoRefresh(void)
2053 + {
2054 +        // TODO: make main_unix/VideoRefresh call directly video_refresh() ?
2055 +        video_refresh();
2056 + }
2057 +
2058 + #if 0
2059   void VideoRefresh(void)
2060   {
2061   #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
# Line 1597 | Line 2111 | void VideoRefresh(void)
2111                  }
2112          }
2113   }
2114 + #endif
2115  
2116   #ifdef HAVE_PTHREADS
2117   static void *redraw_func(void *arg)
2118   {
2119 +        uint64 start = GetTicks_usec();
2120 +        int64 ticks = 0;
2121 +        uint64 next = GetTicks_usec();
2122          while (!redraw_thread_cancel) {
2123 < #ifdef HAVE_NANOSLEEP
2124 <                struct timespec req = {0, 16666667};
2125 <                nanosleep(&req, NULL);
2126 < #else
2127 <                usleep(16667);
2128 < #endif
2129 <                VideoRefresh();
2123 > //              VideoRefresh();
2124 >                video_refresh();
2125 >                next += 16667;
2126 >                int64 delay = next - GetTicks_usec();
2127 >                if (delay > 0)
2128 >                        Delay_usec(delay);
2129 >                else if (delay < -16667)
2130 >                        next = GetTicks_usec();
2131 >                ticks++;
2132          }
2133 +        uint64 end = GetTicks_usec();
2134 +        printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks);
2135          return NULL;
2136   }
2137   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines