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.27 by cebix, 2000-11-02T14:45:16Z

# 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 261 | Line 345 | static bool init_window(int width, int h
345          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
346                  InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
347                  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);
348  
349 <        // Set colormap
350 <        if (depth == 8) {
351 <                XSetWindowColormap(x_display, the_win, cmap[0]);
352 <                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
349 >        // Indicate that we want keyboard input
350 >        {
351 >                XWMHints *hints = XAllocWMHints();
352 >                if (hints) {
353 >                        hints->input = True;
354 >                        hints->flags = InputHint;
355 >                        XSetWMHints(x_display, the_win, hints);
356 >                        XFree((char *)hints);
357 >                }
358          }
359  
360          // Make window unresizable
361 <        XSizeHints *hints;
362 <        if ((hints = XAllocSizeHints()) != NULL) {
363 <                hints->min_width = width;
364 <                hints->max_width = width;
365 <                hints->min_height = height;
366 <                hints->max_height = height;
367 <                hints->flags = PMinSize | PMaxSize;
368 <                XSetWMNormalHints(x_display, the_win, hints);
369 <                XFree((char *)hints);
361 >        {
362 >                XSizeHints *hints = XAllocSizeHints();
363 >                if (hints) {
364 >                        hints->min_width = width;
365 >                        hints->max_width = width;
366 >                        hints->min_height = height;
367 >                        hints->max_height = height;
368 >                        hints->flags = PMinSize | PMaxSize;
369 >                        XSetWMNormalHints(x_display, the_win, hints);
370 >                        XFree((char *)hints);
371 >                }
372          }
373          
374 +        // Set window title
375 +        {
376 +                XTextProperty title_prop;
377 +                const char *title = GetString(STR_WINDOW_TITLE);
378 +                XStringListToTextProperty((char **)&title, 1, &title_prop);
379 +                XSetWMName(x_display, the_win, &title_prop);
380 +                XFree(title_prop.value);
381 +        }
382 +
383 +        // Set window class
384 +        {
385 +                XClassHint *hints;
386 +                hints = XAllocClassHint();
387 +                if (hints) {
388 +                        hints->res_name = "BasiliskII";
389 +                        hints->res_class = "BasiliskII";
390 +                        XSetClassHint(x_display, the_win, hints);
391 +                        XFree((char *)hints);
392 +                }
393 +        }
394 +
395 +        // Show window
396 +        XSync(x_display, false);
397 +        XMapRaised(x_display, the_win);
398 +        XFlush(x_display);
399 +
400 +        // Set colormap
401 +        if (depth == 8)
402 +                XSetWindowColormap(x_display, the_win, cmap[0]);
403 +
404          // Try to create and attach SHM image
405          have_shm = false;
406          if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) {
# Line 337 | Line 454 | static bool init_window(int width, int h
454                  img->bitmap_bit_order = MSBFirst;
455          }
456  
457 + #ifdef ENABLE_VOSF
458 +        // Allocate a page-aligned chunk of memory for frame buffer
459 +        the_buffer_size = align_on_page_boundary((aligned_height + 2) * img->bytes_per_line);
460 +        the_host_buffer = the_buffer_copy;
461 +        
462 +        the_buffer_copy = (uint8 *)allocate_framebuffer(the_buffer_size);
463 +        memset(the_buffer_copy, 0, the_buffer_size);
464 +        
465 +        the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
466 +        memset(the_buffer, 0, the_buffer_size);
467 + #else
468          // Allocate memory for frame buffer
469          the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
470 + #endif
471  
472          // Create GC
473          the_gc = XCreateGC(x_display, the_win, 0, 0);
# Line 352 | Line 481 | static bool init_window(int width, int h
481          XDefineCursor(x_display, the_win, mac_cursor);
482  
483          // Set VideoMonitor
484 +        bool native_byte_order;
485   #ifdef WORDS_BIGENDIAN
486 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == MSBFirst);
486 >        native_byte_order = (img->bitmap_bit_order == MSBFirst);
487   #else
488 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == LSBFirst);
488 >        native_byte_order = (img->bitmap_bit_order == LSBFirst);
489   #endif
490 + #ifdef ENABLE_VOSF
491 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW);
492 + #endif
493 +        set_video_monitor(width, height, img->bytes_per_line, native_byte_order);
494          
495 < #if REAL_ADDRESSING
496 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
495 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
496 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
497   #else
498          VideoMonitor.mac_frame_base = MacFrameBaseMac;
499   #endif
# Line 462 | Line 596 | static bool init_fbdev_dga(char *in_fb_n
596                  GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
597          
598          // Set colormap
599 <        if (depth == 8) {
599 >        if (depth == 8)
600                  XSetWindowColormap(x_display, the_win, cmap[0]);
467                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
468        }
601          
602          // Set VideoMonitor
603          int bytes_per_row = width;
# Line 492 | Line 624 | static bool init_fbdev_dga(char *in_fb_n
624                  }
625          }
626          
627 + #if ENABLE_VOSF
628 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
629 +        // If the blit function is null, i.e. just a copy of the buffer,
630 +        // we first try to avoid the allocation of a temporary frame buffer
631 +        use_vosf = true;
632 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
633 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
634 +                use_vosf = false;
635 +        
636 +        if (use_vosf) {
637 +                the_host_buffer = the_buffer;
638 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
639 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
640 +                memset(the_buffer_copy, 0, the_buffer_size);
641 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
642 +                memset(the_buffer, 0, the_buffer_size);
643 +        }
644 + #else
645 +        use_vosf = false;
646 + #endif
647 + #endif
648 +        
649          set_video_monitor(width, height, bytes_per_row, true);
650 < #if REAL_ADDRESSING
651 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
650 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
651 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
652   #else
653          VideoMonitor.mac_frame_base = MacFrameBaseMac;
654   #endif
# Line 556 | Line 710 | static bool init_xf86_dga(int width, int
710          // Set colormap
711          if (depth == 8) {
712                  XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
559                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
713                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
714          }
715  
# Line 575 | Line 728 | static bool init_xf86_dga(int width, int
728                          bytes_per_row *= 4;
729                          break;
730          }
731 +        
732 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
733 +        // If the blit function is null, i.e. just a copy of the buffer,
734 +        // we first try to avoid the allocation of a temporary frame buffer
735 +        use_vosf = true;
736 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
737 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
738 +                use_vosf = false;
739 +        
740 +        if (use_vosf) {
741 +                the_host_buffer = the_buffer;
742 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
743 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
744 +                memset(the_buffer_copy, 0, the_buffer_size);
745 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
746 +                memset(the_buffer, 0, the_buffer_size);
747 +        }
748 + #elif defined(ENABLE_VOSF)
749 +        // The UAE memory handlers will already handle color conversion, if needed.
750 +        use_vosf = false;
751 + #endif
752 +        
753          set_video_monitor(width, height, bytes_per_row, true);
754 < #if REAL_ADDRESSING
755 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
756 <        MacFrameLayout = FLAYOUT_DIRECT;
754 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
755 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
756 > //      MacFrameLayout = FLAYOUT_DIRECT;
757   #else
758          VideoMonitor.mac_frame_base = MacFrameBaseMac;
759   #endif
# Line 654 | Line 829 | static void keycode_init(void)
829          }
830   }
831  
832 + bool VideoInitBuffer()
833 + {
834 + #ifdef ENABLE_VOSF
835 +        if (use_vosf) {
836 +                const uint32 page_size  = getpagesize();
837 +                const uint32 page_mask  = page_size - 1;
838 +                
839 +                mainBuffer.memBase      = (uint32) the_buffer;
840 +                // Align the frame buffer on page boundary
841 +                mainBuffer.memStart             = (uint32)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
842 +                mainBuffer.memLength    = the_buffer_size;
843 +                mainBuffer.memEnd       = mainBuffer.memStart + mainBuffer.memLength;
844 +
845 +                mainBuffer.pageSize     = page_size;
846 +                mainBuffer.pageCount    = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
847 +                mainBuffer.pageBits     = log_base_2(mainBuffer.pageSize);
848 +
849 +                if (mainBuffer.dirtyPages != 0)
850 +                        free(mainBuffer.dirtyPages);
851 +
852 +                mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount);
853 +
854 +                if (mainBuffer.pageInfo != 0)
855 +                        free(mainBuffer.pageInfo);
856 +
857 +                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
858 +
859 +                if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0))
860 +                        return false;
861 +
862 +                PFLAG_CLEAR_ALL;
863 +
864 +                uint32 a = 0;
865 +                for (int i = 0; i < mainBuffer.pageCount; i++) {
866 +                        int y1 = a / VideoMonitor.bytes_per_row;
867 +                        if (y1 >= VideoMonitor.y)
868 +                                y1 = VideoMonitor.y - 1;
869 +
870 +                        int y2 = (a + mainBuffer.pageSize) / VideoMonitor.bytes_per_row;
871 +                        if (y2 >= VideoMonitor.y)
872 +                                y2 = VideoMonitor.y - 1;
873 +
874 +                        mainBuffer.pageInfo[i].top = y1;
875 +                        mainBuffer.pageInfo[i].bottom = y2;
876 +
877 +                        a += mainBuffer.pageSize;
878 +                        if (a > mainBuffer.memLength)
879 +                                a = mainBuffer.memLength;
880 +                }
881 +                
882 +                // We can now write-protect the frame buffer
883 +                if (mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ) != 0)
884 +                        return false;
885 +        }
886 + #endif
887 +        return true;
888 + }
889 +
890   bool VideoInit(bool classic)
891   {
892 + #ifdef ENABLE_VOSF
893 +        // Open /dev/zero
894 +        zero_fd = open("/dev/zero", O_RDWR);
895 +        if (zero_fd < 0) {
896 +        char str[256];
897 +                sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
898 +                ErrorAlert(str);
899 +        return false;
900 +        }
901 +        
902 +        // Zero the mainBuffer structure
903 +        mainBuffer.dirtyPages = 0;
904 +        mainBuffer.pageInfo = 0;
905 + #endif
906 +        
907          // Check if X server runs on local machine
908          local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
909                   || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
# Line 807 | Line 1055 | bool VideoInit(bool classic)
1055          pthread_mutex_lock(&frame_buffer_lock);
1056   #endif
1057  
1058 < #if !REAL_ADDRESSING
1058 > #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1059          // Set variables for UAE memory mapping
1060          MacFrameBaseHost = the_buffer;
1061          MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
# Line 817 | Line 1065 | bool VideoInit(bool classic)
1065                  MacFrameLayout = FLAYOUT_NONE;
1066   #endif
1067  
1068 + #ifdef ENABLE_VOSF
1069 +        if (use_vosf) {
1070 +                // Initialize the mainBuffer structure
1071 +                if (!VideoInitBuffer()) {
1072 +                        // TODO: STR_VOSF_INIT_ERR ?
1073 +                        ErrorAlert("Could not initialize Video on SEGV signals");
1074 +                return false;
1075 +                }
1076 +
1077 +                // Initialize the handler for SIGSEGV
1078 +                if (!Screen_fault_handler_init()) {
1079 +                        // TODO: STR_VOSF_INIT_ERR ?
1080 +                        ErrorAlert("Could not initialize Video on SEGV signals");
1081 +                        return false;
1082 +                }
1083 +        }
1084 + #endif
1085 +        
1086 +        // Initialize VideoRefresh function
1087 +        VideoRefreshInit();
1088 +        
1089          XSync(x_display, false);
1090  
1091   #ifdef HAVE_PTHREADS
# Line 885 | Line 1154 | void VideoExit(void)
1154                          XFreeColormap(x_display, cmap[0]);
1155                          XFreeColormap(x_display, cmap[1]);
1156                  }
1157 +                
1158 +                if (!use_vosf) {
1159 +                        if (the_buffer) {
1160 +                                free(the_buffer);
1161 +                                the_buffer = NULL;
1162 +                        }
1163  
1164 <                if (the_buffer) {
1165 <                        free(the_buffer);
1166 <                        the_buffer = NULL;
1164 >                        if (!have_shm && the_buffer_copy) {
1165 >                                free(the_buffer_copy);
1166 >                                the_buffer_copy = NULL;
1167 >                        }
1168 >                }
1169 > #ifdef ENABLE_VOSF
1170 >                else {
1171 >                        if (the_buffer != (uint8 *)MAP_FAILED) {
1172 >                                munmap((caddr_t)the_buffer, the_buffer_size);
1173 >                                the_buffer = 0;
1174 >                        }
1175 >                        
1176 >                        if (the_buffer_copy != (uint8 *)MAP_FAILED) {
1177 >                                munmap((caddr_t)the_buffer_copy, the_buffer_size);
1178 >                                the_buffer_copy = 0;
1179 >                        }
1180 >                }
1181 > #endif
1182 >        }
1183 >        
1184 > #ifdef ENABLE_VOSF
1185 >        if (use_vosf) {
1186 >                // Clear mainBuffer data
1187 >                if (mainBuffer.pageInfo) {
1188 >                        free(mainBuffer.pageInfo);
1189 >                        mainBuffer.pageInfo = 0;
1190                  }
1191  
1192 <                if (!have_shm && the_buffer_copy) {
1193 <                        free(the_buffer_copy);
1194 <                        the_buffer_copy = NULL;
1192 >                if (mainBuffer.dirtyPages) {
1193 >                        free(mainBuffer.dirtyPages);
1194 >                        mainBuffer.dirtyPages = 0;
1195                  }
1196          }
1197 +
1198 +        // Close /dev/zero
1199 +        if (zero_fd > 0)
1200 +                close(zero_fd);
1201 + #endif
1202   }
1203  
1204  
# Line 936 | Line 1239 | void VideoInterrupt(void)
1239  
1240   void video_set_palette(uint8 *pal)
1241   {
1242 < #ifdef HAVE_PTHREDS
1242 > #ifdef HAVE_PTHREADS
1243          pthread_mutex_lock(&palette_lock);
1244   #endif
1245  
# Line 1027 | Line 1330 | static void resume_emul(void)
1330          XF86DGASetViewPort(x_display, screen, 0, 0);
1331   #endif
1332          XSync(x_display, false);
1333 <
1333 >        
1334 >        // the_buffer already contains the data to restore. i.e. since a temporary
1335 >        // frame buffer is used when VOSF is actually used, fb_save is therefore
1336 >        // not necessary.
1337 > #ifdef ENABLE_VOSF
1338 >        if (use_vosf) {
1339 > #ifdef HAVE_PTHREADS
1340 >                pthread_mutex_lock(&Screen_draw_lock);
1341 > #endif
1342 >                PFLAG_SET_ALL;
1343 > #ifdef HAVE_PTHREADS
1344 >                pthread_mutex_unlock(&Screen_draw_lock);
1345 > #endif
1346 >                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1347 >        }
1348 > #endif
1349 >        
1350          // Restore frame buffer
1351          if (fb_save) {
1352 + #ifdef ENABLE_VOSF
1353 +                // Don't copy fb_save to the temporary frame buffer in VOSF mode
1354 +                if (!use_vosf)
1355 + #endif
1356                  memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row);
1357                  free(fb_save);
1358                  fb_save = NULL;
# Line 1311 | Line 1634 | static void handle_events(void)
1634                          // Hidden parts exposed, force complete refresh of window
1635                          case Expose:
1636                                  if (display_type == DISPLAY_WINDOW) {
1637 + #ifdef ENABLE_VOSF
1638 +                                        if (use_vosf) {                 // VOSF refresh
1639 + #ifdef HAVE_PTHREADS
1640 +                                                pthread_mutex_lock(&Screen_draw_lock);
1641 + #endif
1642 +                                                PFLAG_SET_ALL;
1643 + #ifdef HAVE_PTHREADS
1644 +                                                pthread_mutex_unlock(&Screen_draw_lock);
1645 + #endif
1646 +                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1647 +                                        }
1648 +                                        else
1649 + #endif
1650                                          if (frame_skip == 0) {  // Dynamic refresh
1651                                                  int x1, y1;
1652                                                  for (y1=0; y1<16; y1++)
1653                                                  for (x1=0; x1<16; x1++)
1654                                                          updt_box[x1][y1] = true;
1655                                                  nr_boxes = 16 * 16;
1656 <                                        } else
1656 >                                        } else                                  // Static refresh
1657                                                  memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1658                                  }
1659                                  break;
1660 +
1661 +                        case FocusIn:
1662 +                        case FocusOut:
1663 +                                break;
1664                  }
1665          }
1666   }
# Line 1449 | Line 1789 | static void update_display_static(void)
1789          // Check for first column from left and first column from right that have changed
1790          if (high) {
1791                  if (depth == 1) {
1792 <                        x1 = VideoMonitor.x;
1792 >                        x1 = VideoMonitor.x - 1;
1793                          for (j=y1; j<=y2; j++) {
1794                                  p = &the_buffer[j * bytes_per_row];
1795                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1458 | Line 1798 | static void update_display_static(void)
1798                                                  x1 = i << 3;
1799                                                  break;
1800                                          }
1801 <                                        p++;
1462 <                                        p2++;
1801 >                                        p++; p2++;
1802                                  }
1803                          }
1804                          x2 = x1;
# Line 1469 | Line 1808 | static void update_display_static(void)
1808                                  p += bytes_per_row;
1809                                  p2 += bytes_per_row;
1810                                  for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1811 <                                        p--;
1473 <                                        p2--;
1811 >                                        p--; p2--;
1812                                          if (*p != *p2) {
1813 <                                                x2 = i << 3;
1813 >                                                x2 = (i << 3) + 7;
1814                                                  break;
1815                                          }
1816                                  }
1817                          }
1818 <                        wide = x2 - x1;
1818 >                        wide = x2 - x1 + 1;
1819  
1820                          // Update copy of the_buffer
1821                          if (high && wide) {
# Line 1492 | Line 1830 | static void update_display_static(void)
1830                          for (j=y1; j<=y2; j++) {
1831                                  p = &the_buffer[j * bytes_per_row];
1832                                  p2 = &the_buffer_copy[j * bytes_per_row];
1833 <                                for (i=0; i<x1; i++) {
1834 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1835 <                                                x1 = i;
1833 >                                for (i=0; i<x1*bytes_per_pixel; i++) {
1834 >                                        if (*p != *p2) {
1835 >                                                x1 = i / bytes_per_pixel;
1836                                                  break;
1837                                          }
1838 <                                        p += bytes_per_pixel;
1501 <                                        p2 += bytes_per_pixel;
1838 >                                        p++; p2++;
1839                                  }
1840                          }
1841                          x2 = x1;
# Line 1507 | Line 1844 | static void update_display_static(void)
1844                                  p2 = &the_buffer_copy[j * bytes_per_row];
1845                                  p += bytes_per_row;
1846                                  p2 += bytes_per_row;
1847 <                                for (i=VideoMonitor.x; i>x2; i--) {
1848 <                                        p -= bytes_per_pixel;
1849 <                                        p2 -= bytes_per_pixel;
1850 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1851 <                                                x2 = i;
1847 >                                for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1848 >                                        p--;
1849 >                                        p2--;
1850 >                                        if (*p != *p2) {
1851 >                                                x2 = i / bytes_per_pixel;
1852                                                  break;
1853                                          }
1854                                  }
# Line 1539 | Line 1876 | static void update_display_static(void)
1876  
1877  
1878   /*
1879 + *      Screen refresh functions
1880 + */
1881 +
1882 + // The specialisations hereunder are meant to enable VOSF with DGA in direct
1883 + // addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could
1884 + // not get mapped correctly with respect to the predetermined host frame
1885 + // buffer base address.
1886 + //
1887 + // Hmm, in other words, when in direct addressing mode and DGA is requested,
1888 + // we first try to "triple allocate" the address spaces according to the real
1889 + // host frame buffer address. Then, if it fails, we will use a temporary
1890 + // frame buffer thus making the real host frame buffer updated when pages
1891 + // of the temp frame buffer are altered.
1892 + //
1893 + // As a side effect, a little speed gain in screen updates could be noticed
1894 + // for other modes than DGA.
1895 + //
1896 + // The following two functions below are inline so that a clever compiler
1897 + // could specialise the code according to the current screen depth and
1898 + // display type. A more clever compiler would the job by itself though...
1899 + // (update_display_vosf is inlined as well)
1900 +
1901 + static inline void possibly_quit_dga_mode()
1902 + {
1903 + #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1904 +        // Quit DGA mode if requested
1905 +        if (quit_full_screen) {
1906 +                quit_full_screen = false;
1907 + #ifdef ENABLE_XF86_DGA
1908 +                XF86DGADirectVideo(x_display, screen, 0);
1909 + #endif
1910 +                XUngrabPointer(x_display, CurrentTime);
1911 +                XUngrabKeyboard(x_display, CurrentTime);
1912 +                XUnmapWindow(x_display, the_win);
1913 +                XSync(x_display, false);
1914 +        }
1915 + #endif
1916 + }
1917 +
1918 + static inline void handle_palette_changes(int depth, int display_type)
1919 + {
1920 + #ifdef HAVE_PTHREADS
1921 +        pthread_mutex_lock(&palette_lock);
1922 + #endif
1923 +        if (palette_changed) {
1924 +                palette_changed = false;
1925 +                if (depth == 8) {
1926 +                        XStoreColors(x_display, cmap[0], palette, 256);
1927 +                        XStoreColors(x_display, cmap[1], palette, 256);
1928 +                                
1929 + #ifdef ENABLE_XF86_DGA
1930 +                        if (display_type == DISPLAY_DGA) {
1931 +                                current_dga_cmap ^= 1;
1932 +                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1933 +                        }
1934 + #endif
1935 +                }
1936 +        }
1937 + #ifdef HAVE_PTHREADS
1938 +        pthread_mutex_unlock(&palette_lock);
1939 + #endif
1940 + }
1941 +
1942 + static void video_refresh_dga(void)
1943 + {
1944 +        // Quit DGA mode if requested
1945 +        possibly_quit_dga_mode();
1946 +        
1947 +        // Handle X events
1948 +        handle_events();
1949 +        
1950 +        // Handle palette changes
1951 +        handle_palette_changes(depth, DISPLAY_DGA);
1952 + }
1953 +
1954 + #ifdef ENABLE_VOSF
1955 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
1956 + static void video_refresh_dga_vosf(void)
1957 + {
1958 +        // Quit DGA mode if requested
1959 +        possibly_quit_dga_mode();
1960 +        
1961 +        // Handle X events
1962 +        handle_events();
1963 +        
1964 +        // Handle palette changes
1965 +        handle_palette_changes(depth, DISPLAY_DGA);
1966 +        
1967 +        // Update display (VOSF variant)
1968 +        static int tick_counter = 0;
1969 +        if (++tick_counter >= frame_skip) {
1970 +                tick_counter = 0;
1971 + #ifdef HAVE_PTHREADS
1972 +                pthread_mutex_lock(&Screen_draw_lock);
1973 + #endif
1974 +                update_display_dga_vosf();
1975 + #ifdef HAVE_PTHREADS
1976 +                pthread_mutex_unlock(&Screen_draw_lock);
1977 + #endif
1978 +        }
1979 + }
1980 + #endif
1981 +
1982 + static void video_refresh_window_vosf(void)
1983 + {
1984 +        // Quit DGA mode if requested
1985 +        possibly_quit_dga_mode();
1986 +        
1987 +        // Handle X events
1988 +        handle_events();
1989 +        
1990 +        // Handle palette changes
1991 +        handle_palette_changes(depth, DISPLAY_WINDOW);
1992 +        
1993 +        // Update display (VOSF variant)
1994 +        static int tick_counter = 0;
1995 +        if (++tick_counter >= frame_skip) {
1996 +                tick_counter = 0;
1997 + #ifdef HAVE_PTHREADS
1998 +                pthread_mutex_lock(&Screen_draw_lock);
1999 + #endif
2000 +                update_display_window_vosf();
2001 + #ifdef HAVE_PTHREADS
2002 +                pthread_mutex_unlock(&Screen_draw_lock);
2003 + #endif
2004 +        }
2005 + }
2006 + #endif // def ENABLE_VOSF
2007 +
2008 + static void video_refresh_window_static(void)
2009 + {
2010 +        // Handle X events
2011 +        handle_events();
2012 +        
2013 +        // Handle_palette changes
2014 +        handle_palette_changes(depth, DISPLAY_WINDOW);
2015 +        
2016 +        // Update display (static variant)
2017 +        static int tick_counter = 0;
2018 +        if (++tick_counter >= frame_skip) {
2019 +                tick_counter = 0;
2020 +                update_display_static();
2021 +        }
2022 + }
2023 +
2024 + static void video_refresh_window_dynamic(void)
2025 + {
2026 +        // Handle X events
2027 +        handle_events();
2028 +        
2029 +        // Handle_palette changes
2030 +        handle_palette_changes(depth, DISPLAY_WINDOW);
2031 +        
2032 +        // Update display (dynamic variant)
2033 +        static int tick_counter = 0;
2034 +        tick_counter++;
2035 +        update_display_dynamic(tick_counter);
2036 + }
2037 +
2038 +
2039 + /*
2040   *  Thread for screen refresh, input handling etc.
2041   */
2042  
2043 + void VideoRefreshInit(void)
2044 + {
2045 +        // TODO: set up specialised 8bpp VideoRefresh handlers ?
2046 +        if (display_type == DISPLAY_DGA) {
2047 + #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
2048 +                if (use_vosf)
2049 +                        video_refresh = video_refresh_dga_vosf;
2050 +                else
2051 + #endif
2052 +                        video_refresh = video_refresh_dga;
2053 +        }
2054 +        else {
2055 + #ifdef ENABLE_VOSF
2056 +                if (use_vosf)
2057 +                        video_refresh = video_refresh_window_vosf;
2058 +                else
2059 + #endif
2060 +                if (frame_skip == 0)
2061 +                        video_refresh = video_refresh_window_dynamic;
2062 +                else
2063 +                        video_refresh = video_refresh_window_static;
2064 +        }
2065 + }
2066 +
2067 + void VideoRefresh(void)
2068 + {
2069 +        // TODO: make main_unix/VideoRefresh call directly video_refresh() ?
2070 +        video_refresh();
2071 + }
2072 +
2073 + #if 0
2074   void VideoRefresh(void)
2075   {
2076   #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
# Line 1597 | Line 2126 | void VideoRefresh(void)
2126                  }
2127          }
2128   }
2129 + #endif
2130  
2131   #ifdef HAVE_PTHREADS
2132   static void *redraw_func(void *arg)
2133   {
2134 +        uint64 start = GetTicks_usec();
2135 +        int64 ticks = 0;
2136 +        uint64 next = GetTicks_usec();
2137          while (!redraw_thread_cancel) {
2138 < #ifdef HAVE_NANOSLEEP
2139 <                struct timespec req = {0, 16666667};
2140 <                nanosleep(&req, NULL);
2141 < #else
2142 <                usleep(16667);
2143 < #endif
2144 <                VideoRefresh();
2138 > //              VideoRefresh();
2139 >                video_refresh();
2140 >                next += 16667;
2141 >                int64 delay = next - GetTicks_usec();
2142 >                if (delay > 0)
2143 >                        Delay_usec(delay);
2144 >                else if (delay < -16667)
2145 >                        next = GetTicks_usec();
2146 >                ticks++;
2147          }
2148 +        uint64 end = GetTicks_usec();
2149 +        printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks);
2150          return NULL;
2151   }
2152   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines