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.15 by cebix, 2000-07-13T13:47:12Z vs.
Revision 1.25 by cebix, 2000-10-13T16:47:52Z

# 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 60 | Line 67
67   #include "user_strings.h"
68   #include "video.h"
69  
70 < #define DEBUG 1
70 > #define DEBUG 0
71   #include "debug.h"
72  
73  
# Line 81 | Line 88 | static int16 mouse_wheel_mode = 1;
88   static int16 mouse_wheel_lines = 3;
89  
90   static int display_type = DISPLAY_WINDOW;                       // See enum above
91 + static bool local_X11;                                                          // Flag: X server running on local machine?
92   static uint8 *the_buffer;                                                       // Mac frame buffer
93  
94   #ifdef HAVE_PTHREADS
# Line 127 | Line 135 | static pthread_mutex_t palette_lock = PT
135   static GC the_gc;
136   static XImage *img = NULL;
137   static XShmSegmentInfo shminfo;
130 static XImage *cursor_image, *cursor_mask_image;
131 static Pixmap cursor_map, cursor_mask_map;
138   static Cursor mac_cursor;
133 static GC cursor_gc, cursor_mask_gc;
139   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer
135 static uint8 the_cursor[64];                                            // Cursor image data
140   static bool have_shm = false;                                           // Flag: SHM extensions available
141   static bool updt_box[17][17];                                           // Flag for Update
142   static int nr_boxes;
# Line 157 | 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 164 | Line 241 | static int event2keycode(XKeyEvent *ev);
241  
242  
243   // From main_unix.cpp
244 + extern char *x_display_name;
245   extern Display *x_display;
246  
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 177 | 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 249 | Line 331 | static bool init_window(int width, int h
331  
332          // Read frame skip prefs
333          frame_skip = PrefsFindInt32("frameskip");
252        if (frame_skip == 0)
253                frame_skip = 1;
334  
335          // Create window
336          XSetWindowAttributes wattr;
# Line 290 | Line 370 | static bool init_window(int width, int h
370          
371          // Try to create and attach SHM image
372          have_shm = false;
373 <        if (depth != 1 && XShmQueryExtension(x_display)) {
373 >        if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) {
374  
375                  // Create SHM image ("height + 2" for safety)
376                  img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
# Line 341 | Line 421 | static bool init_window(int width, int h
421                  img->bitmap_bit_order = MSBFirst;
422          }
423  
424 <        // Allocate memory for frame buffer copy
424 > #ifdef ENABLE_VOSF
425 >        // Allocate a page-aligned chunk of memory for frame buffer
426 >        the_buffer_size = align_on_page_boundary((aligned_height + 2) * img->bytes_per_line);
427 >        the_host_buffer = the_buffer_copy;
428 >        
429 >        the_buffer_copy = (uint8 *)allocate_framebuffer(the_buffer_size);
430 >        memset(the_buffer_copy, 0, the_buffer_size);
431 >        
432 >        the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
433 >        memset(the_buffer, 0, the_buffer_size);
434 > #else
435 >        // Allocate memory for frame buffer
436          the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
437 + #endif
438  
439          // Create GC
440          the_gc = XCreateGC(x_display, the_win, 0, 0);
441          XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
442  
443          // Create no_cursor
444 <        mac_cursor = XCreatePixmapCursor (x_display,
445 <                                           XCreatePixmap (x_display, the_win, 1, 1, 1),
446 <                                           XCreatePixmap (x_display, the_win, 1, 1, 1),
447 <                                           &black, &white, 0, 0);
448 <        XDefineCursor (x_display, the_win, mac_cursor);
444 >        mac_cursor = XCreatePixmapCursor(x_display,
445 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
446 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
447 >           &black, &white, 0, 0);
448 >        XDefineCursor(x_display, the_win, mac_cursor);
449  
450          // Set VideoMonitor
451 +        bool native_byte_order;
452   #ifdef WORDS_BIGENDIAN
453 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == MSBFirst);
453 >        native_byte_order = (img->bitmap_bit_order == MSBFirst);
454   #else
455 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == LSBFirst);
455 >        native_byte_order = (img->bitmap_bit_order == LSBFirst);
456   #endif
457 + #ifdef ENABLE_VOSF
458 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW);
459 + #endif
460 +        set_video_monitor(width, height, img->bytes_per_line, native_byte_order);
461          
462 < #if REAL_ADDRESSING
463 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
462 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
463 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
464   #else
465          VideoMonitor.mac_frame_base = MacFrameBaseMac;
466   #endif
# Line 496 | Line 593 | static bool init_fbdev_dga(char *in_fb_n
593                  }
594          }
595          
596 + #if ENABLE_VOSF
597 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
598 +        // If the blit function is null, i.e. just a copy of the buffer,
599 +        // we first try to avoid the allocation of a temporary frame buffer
600 +        use_vosf = true;
601 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
602 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
603 +                use_vosf = false;
604 +        
605 +        if (use_vosf) {
606 +                the_host_buffer = the_buffer;
607 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
608 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
609 +                memset(the_buffer_copy, 0, the_buffer_size);
610 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
611 +                memset(the_buffer, 0, the_buffer_size);
612 +        }
613 + #else
614 +        use_vosf = false;
615 + #endif
616 + #endif
617 +        
618          set_video_monitor(width, height, bytes_per_row, true);
619 < #if REAL_ADDRESSING
620 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
619 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
620 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
621   #else
622          VideoMonitor.mac_frame_base = MacFrameBaseMac;
623   #endif
# Line 579 | Line 698 | static bool init_xf86_dga(int width, int
698                          bytes_per_row *= 4;
699                          break;
700          }
701 +        
702 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
703 +        // If the blit function is null, i.e. just a copy of the buffer,
704 +        // we first try to avoid the allocation of a temporary frame buffer
705 +        use_vosf = true;
706 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
707 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
708 +                use_vosf = false;
709 +        
710 +        if (use_vosf) {
711 +                the_host_buffer = the_buffer;
712 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
713 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
714 +                memset(the_buffer_copy, 0, the_buffer_size);
715 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
716 +                memset(the_buffer, 0, the_buffer_size);
717 +        }
718 + #elif defined(ENABLE_VOSF)
719 +        // The UAE memory handlers will already handle color conversion, if needed.
720 +        use_vosf = false;
721 + #endif
722 +        
723          set_video_monitor(width, height, bytes_per_row, true);
724 < #if REAL_ADDRESSING
725 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
726 <        MacFrameLayout = FLAYOUT_DIRECT;
724 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
725 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
726 > //      MacFrameLayout = FLAYOUT_DIRECT;
727   #else
728          VideoMonitor.mac_frame_base = MacFrameBaseMac;
729   #endif
# Line 658 | Line 799 | static void keycode_init(void)
799          }
800   }
801  
802 + bool VideoInitBuffer()
803 + {
804 + #ifdef ENABLE_VOSF
805 +        if (use_vosf) {
806 +                const uint32 page_size  = getpagesize();
807 +                const uint32 page_mask  = page_size - 1;
808 +                
809 +                mainBuffer.memBase      = (uint32) the_buffer;
810 +                // Align the frame buffer on page boundary
811 +                mainBuffer.memStart             = (uint32)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
812 +                mainBuffer.memLength    = the_buffer_size;
813 +                mainBuffer.memEnd       = mainBuffer.memStart + mainBuffer.memLength;
814 +
815 +                mainBuffer.pageSize     = page_size;
816 +                mainBuffer.pageCount    = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
817 +                mainBuffer.pageBits     = log_base_2(mainBuffer.pageSize);
818 +
819 +                if (mainBuffer.dirtyPages != 0)
820 +                        free(mainBuffer.dirtyPages);
821 +
822 +                mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount);
823 +
824 +                if (mainBuffer.pageInfo != 0)
825 +                        free(mainBuffer.pageInfo);
826 +
827 +                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
828 +
829 +                if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0))
830 +                        return false;
831 +
832 +                PFLAG_CLEAR_ALL;
833 +
834 +                uint32 a = 0;
835 +                for (int i = 0; i < mainBuffer.pageCount; i++) {
836 +                        int y1 = a / VideoMonitor.bytes_per_row;
837 +                        if (y1 >= VideoMonitor.y)
838 +                                y1 = VideoMonitor.y - 1;
839 +
840 +                        int y2 = (a + mainBuffer.pageSize) / VideoMonitor.bytes_per_row;
841 +                        if (y2 >= VideoMonitor.y)
842 +                                y2 = VideoMonitor.y - 1;
843 +
844 +                        mainBuffer.pageInfo[i].top = y1;
845 +                        mainBuffer.pageInfo[i].bottom = y2;
846 +
847 +                        a += mainBuffer.pageSize;
848 +                        if (a > mainBuffer.memLength)
849 +                                a = mainBuffer.memLength;
850 +                }
851 +                
852 +                // We can now write-protect the frame buffer
853 +                if (mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ) != 0)
854 +                        return false;
855 +        }
856 + #endif
857 +        return true;
858 + }
859 +
860   bool VideoInit(bool classic)
861   {
862 + #ifdef ENABLE_VOSF
863 +        // Open /dev/zero
864 +        zero_fd = open("/dev/zero", O_RDWR);
865 +        if (zero_fd < 0) {
866 +        char str[256];
867 +                sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
868 +                ErrorAlert(str);
869 +        return false;
870 +        }
871 +        
872 +        // Zero the mainBuffer structure
873 +        mainBuffer.dirtyPages = 0;
874 +        mainBuffer.pageInfo = 0;
875 + #endif
876 +        
877 +        // Check if X server runs on local machine
878 +        local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
879 +                 || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
880 +    
881          // Init keycode translation
882          keycode_init();
883  
# Line 688 | Line 906 | bool VideoInit(bool classic)
906   #ifdef ENABLE_XF86_DGA
907          // DGA available?
908          int dga_event_base, dga_error_base;
909 <        if (XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) {
909 >        if (local_X11 && XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) {
910                  int dga_flags = 0;
911                  XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
912                  has_dga = dga_flags & XF86DGADirectPresent;
# Line 807 | Line 1025 | bool VideoInit(bool classic)
1025          pthread_mutex_lock(&frame_buffer_lock);
1026   #endif
1027  
1028 < #if !REAL_ADDRESSING
1028 > #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1029          // Set variables for UAE memory mapping
1030          MacFrameBaseHost = the_buffer;
1031          MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
# Line 817 | Line 1035 | bool VideoInit(bool classic)
1035                  MacFrameLayout = FLAYOUT_NONE;
1036   #endif
1037  
1038 + #ifdef ENABLE_VOSF
1039 +        if (use_vosf) {
1040 +                // Initialize the mainBuffer structure
1041 +                if (!VideoInitBuffer()) {
1042 +                        // TODO: STR_VOSF_INIT_ERR ?
1043 +                        ErrorAlert("Could not initialize Video on SEGV signals");
1044 +                return false;
1045 +                }
1046 +
1047 +                // Initialize the handler for SIGSEGV
1048 +                if (!Screen_fault_handler_init()) {
1049 +                        // TODO: STR_VOSF_INIT_ERR ?
1050 +                        ErrorAlert("Could not initialize Video on SEGV signals");
1051 +                        return false;
1052 +                }
1053 +        }
1054 + #endif
1055 +        
1056 +        // Initialize VideoRefresh function
1057 +        VideoRefreshInit();
1058 +        
1059          XSync(x_display, false);
1060  
1061   #ifdef HAVE_PTHREADS
# Line 885 | Line 1124 | void VideoExit(void)
1124                          XFreeColormap(x_display, cmap[0]);
1125                          XFreeColormap(x_display, cmap[1]);
1126                  }
1127 +                
1128 +                if (!use_vosf) {
1129 +                        if (the_buffer) {
1130 +                                free(the_buffer);
1131 +                                the_buffer = NULL;
1132 +                        }
1133  
1134 <                if (the_buffer) {
1135 <                        free(the_buffer);
1136 <                        the_buffer = NULL;
1134 >                        if (!have_shm && the_buffer_copy) {
1135 >                                free(the_buffer_copy);
1136 >                                the_buffer_copy = NULL;
1137 >                        }
1138 >                }
1139 > #ifdef ENABLE_VOSF
1140 >                else {
1141 >                        if (the_buffer != (uint8 *)MAP_FAILED) {
1142 >                                munmap((caddr_t)the_buffer, the_buffer_size);
1143 >                                the_buffer = 0;
1144 >                        }
1145 >                        
1146 >                        if (the_buffer_copy != (uint8 *)MAP_FAILED) {
1147 >                                munmap((caddr_t)the_buffer_copy, the_buffer_size);
1148 >                                the_buffer_copy = 0;
1149 >                        }
1150 >                }
1151 > #endif
1152 >        }
1153 >        
1154 > #ifdef ENABLE_VOSF
1155 >        if (use_vosf) {
1156 >                // Clear mainBuffer data
1157 >                if (mainBuffer.pageInfo) {
1158 >                        free(mainBuffer.pageInfo);
1159 >                        mainBuffer.pageInfo = 0;
1160                  }
1161  
1162 <                if (!have_shm && the_buffer_copy) {
1163 <                        free(the_buffer_copy);
1164 <                        the_buffer_copy = NULL;
1162 >                if (mainBuffer.dirtyPages) {
1163 >                        free(mainBuffer.dirtyPages);
1164 >                        mainBuffer.dirtyPages = 0;
1165                  }
1166          }
1167 +
1168 +        // Close /dev/zero
1169 +        if (zero_fd > 0)
1170 +                close(zero_fd);
1171 + #endif
1172   }
1173  
1174  
# Line 1027 | Line 1300 | static void resume_emul(void)
1300          XF86DGASetViewPort(x_display, screen, 0, 0);
1301   #endif
1302          XSync(x_display, false);
1303 <
1303 >        
1304 >        // the_buffer already contains the data to restore. i.e. since a temporary
1305 >        // frame buffer is used when VOSF is actually used, fb_save is therefore
1306 >        // not necessary.
1307 > #ifdef ENABLE_VOSF
1308 >        if (use_vosf) {
1309 > #ifdef HAVE_PTHREADS
1310 >                pthread_mutex_lock(&Screen_draw_lock);
1311 > #endif
1312 >                PFLAG_SET_ALL;
1313 > #ifdef HAVE_PTHREADS
1314 >                pthread_mutex_unlock(&Screen_draw_lock);
1315 > #endif
1316 >                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1317 >        }
1318 > #endif
1319 >        
1320          // Restore frame buffer
1321          if (fb_save) {
1322 + #ifdef ENABLE_VOSF
1323 +                // Don't copy fb_save to the temporary frame buffer in VOSF mode
1324 +                if (!use_vosf)
1325 + #endif
1326                  memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row);
1327                  free(fb_save);
1328                  fb_save = NULL;
# Line 1311 | Line 1604 | static void handle_events(void)
1604                          // Hidden parts exposed, force complete refresh of window
1605                          case Expose:
1606                                  if (display_type == DISPLAY_WINDOW) {
1607 <                                        int x1, y1;
1608 <                                        for (y1=0; y1<16; y1++)
1609 <                                        for (x1=0; x1<16; x1++)
1610 <                                                updt_box[x1][y1] = true;
1611 <                                        nr_boxes = 16 * 16;
1607 > #ifdef ENABLE_VOSF
1608 >                                        if (use_vosf) {                 // VOSF refresh
1609 > #ifdef HAVE_PTHREADS
1610 >                                                pthread_mutex_lock(&Screen_draw_lock);
1611 > #endif
1612 >                                                PFLAG_SET_ALL;
1613 > #ifdef HAVE_PTHREADS
1614 >                                                pthread_mutex_unlock(&Screen_draw_lock);
1615 > #endif
1616 >                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1617 >                                        }
1618 >                                        else
1619 > #endif
1620 >                                        if (frame_skip == 0) {  // Dynamic refresh
1621 >                                                int x1, y1;
1622 >                                                for (y1=0; y1<16; y1++)
1623 >                                                for (x1=0; x1<16; x1++)
1624 >                                                        updt_box[x1][y1] = true;
1625 >                                                nr_boxes = 16 * 16;
1626 >                                        } else                                  // Static refresh
1627 >                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1628                                  }
1629                                  break;
1630                  }
# Line 1327 | Line 1636 | static void handle_events(void)
1636   *  Window display update
1637   */
1638  
1639 < static void update_display(int ticker)
1639 > // Dynamic display update (variable frame rate for each box)
1640 > static void update_display_dynamic(int ticker)
1641   {
1642 <        int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xic, xicl, xi;
1642 >        int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
1643          int xil = 0;
1644          int rxm = 0, rxmo = 0;
1645          int bytes_per_row = VideoMonitor.bytes_per_row;
# Line 1416 | Line 1726 | static void update_display(int ticker)
1726          }
1727   }
1728  
1729 + // Static display update (fixed frame rate, but incremental)
1730 + static void update_display_static(void)
1731 + {
1732 +        // Incremental update code
1733 +        int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1734 +        int bytes_per_row = VideoMonitor.bytes_per_row;
1735 +        int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1736 +        uint8 *p, *p2;
1737 +
1738 +        // Check for first line from top and first line from bottom that have changed
1739 +        y1 = 0;
1740 +        for (j=0; j<VideoMonitor.y; j++) {
1741 +                if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1742 +                        y1 = j;
1743 +                        break;
1744 +                }
1745 +        }
1746 +        y2 = y1 - 1;
1747 +        for (j=VideoMonitor.y-1; j>=y1; j--) {
1748 +                if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1749 +                        y2 = j;
1750 +                        break;
1751 +                }
1752 +        }
1753 +        high = y2 - y1 + 1;
1754 +
1755 +        // Check for first column from left and first column from right that have changed
1756 +        if (high) {
1757 +                if (depth == 1) {
1758 +                        x1 = VideoMonitor.x - 1;
1759 +                        for (j=y1; j<=y2; j++) {
1760 +                                p = &the_buffer[j * bytes_per_row];
1761 +                                p2 = &the_buffer_copy[j * bytes_per_row];
1762 +                                for (i=0; i<(x1>>3); i++) {
1763 +                                        if (*p != *p2) {
1764 +                                                x1 = i << 3;
1765 +                                                break;
1766 +                                        }
1767 +                                        p++; p2++;
1768 +                                }
1769 +                        }
1770 +                        x2 = x1;
1771 +                        for (j=y1; j<=y2; j++) {
1772 +                                p = &the_buffer[j * bytes_per_row];
1773 +                                p2 = &the_buffer_copy[j * bytes_per_row];
1774 +                                p += bytes_per_row;
1775 +                                p2 += bytes_per_row;
1776 +                                for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1777 +                                        p--; p2--;
1778 +                                        if (*p != *p2) {
1779 +                                                x2 = (i << 3) + 7;
1780 +                                                break;
1781 +                                        }
1782 +                                }
1783 +                        }
1784 +                        wide = x2 - x1 + 1;
1785 +
1786 +                        // Update copy of the_buffer
1787 +                        if (high && wide) {
1788 +                                for (j=y1; j<=y2; j++) {
1789 +                                        i = j * bytes_per_row + (x1 >> 3);
1790 +                                        memcpy(the_buffer_copy + i, the_buffer + i, wide >> 3);
1791 +                                }
1792 +                        }
1793 +
1794 +                } else {
1795 +                        x1 = VideoMonitor.x;
1796 +                        for (j=y1; j<=y2; j++) {
1797 +                                p = &the_buffer[j * bytes_per_row];
1798 +                                p2 = &the_buffer_copy[j * bytes_per_row];
1799 +                                for (i=0; i<x1*bytes_per_pixel; i++) {
1800 +                                        if (*p != *p2) {
1801 +                                                x1 = i / bytes_per_pixel;
1802 +                                                break;
1803 +                                        }
1804 +                                        p++; p2++;
1805 +                                }
1806 +                        }
1807 +                        x2 = x1;
1808 +                        for (j=y1; j<=y2; j++) {
1809 +                                p = &the_buffer[j * bytes_per_row];
1810 +                                p2 = &the_buffer_copy[j * bytes_per_row];
1811 +                                p += bytes_per_row;
1812 +                                p2 += bytes_per_row;
1813 +                                for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1814 +                                        p--;
1815 +                                        p2--;
1816 +                                        if (*p != *p2) {
1817 +                                                x2 = i / bytes_per_pixel;
1818 +                                                break;
1819 +                                        }
1820 +                                }
1821 +                        }
1822 +                        wide = x2 - x1;
1823 +
1824 +                        // Update copy of the_buffer
1825 +                        if (high && wide) {
1826 +                                for (j=y1; j<=y2; j++) {
1827 +                                        i = j * bytes_per_row + x1 * bytes_per_pixel;
1828 +                                        memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
1829 +                                }
1830 +                        }
1831 +                }
1832 +        }
1833 +
1834 +        // Refresh display
1835 +        if (high && wide) {
1836 +                if (have_shm)
1837 +                        XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
1838 +                else
1839 +                        XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1840 +        }
1841 + }
1842 +
1843 +
1844 + /*
1845 + *      Screen refresh functions
1846 + */
1847 +
1848 + // The specialisations hereunder are meant to enable VOSF with DGA in direct
1849 + // addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could
1850 + // not get mapped correctly with respect to the predetermined host frame
1851 + // buffer base address.
1852 + //
1853 + // Hmm, in other words, when in direct addressing mode and DGA is requested,
1854 + // we first try to "triple allocate" the address spaces according to the real
1855 + // host frame buffer address. Then, if it fails, we will use a temporary
1856 + // frame buffer thus making the real host frame buffer updated when pages
1857 + // of the temp frame buffer are altered.
1858 + //
1859 + // As a side effect, a little speed gain in screen updates could be noticed
1860 + // for other modes than DGA.
1861 + //
1862 + // The following two functions below are inline so that a clever compiler
1863 + // could specialise the code according to the current screen depth and
1864 + // display type. A more clever compiler would the job by itself though...
1865 + // (update_display_vosf is inlined as well)
1866 +
1867 + static inline void possibly_quit_dga_mode()
1868 + {
1869 + #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1870 +        // Quit DGA mode if requested
1871 +        if (quit_full_screen) {
1872 +                quit_full_screen = false;
1873 + #ifdef ENABLE_XF86_DGA
1874 +                XF86DGADirectVideo(x_display, screen, 0);
1875 + #endif
1876 +                XUngrabPointer(x_display, CurrentTime);
1877 +                XUngrabKeyboard(x_display, CurrentTime);
1878 +                XUnmapWindow(x_display, the_win);
1879 +                XSync(x_display, false);
1880 +        }
1881 + #endif
1882 + }
1883 +
1884 + static inline void handle_palette_changes(int depth, int display_type)
1885 + {
1886 + #ifdef HAVE_PTHREADS
1887 +        pthread_mutex_lock(&palette_lock);
1888 + #endif
1889 +        if (palette_changed) {
1890 +                palette_changed = false;
1891 +                if (depth == 8) {
1892 +                        XStoreColors(x_display, cmap[0], palette, 256);
1893 +                        XStoreColors(x_display, cmap[1], palette, 256);
1894 +                                
1895 + #ifdef ENABLE_XF86_DGA
1896 +                        if (display_type == DISPLAY_DGA) {
1897 +                                current_dga_cmap ^= 1;
1898 +                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1899 +                        }
1900 + #endif
1901 +                }
1902 +        }
1903 + #ifdef HAVE_PTHREADS
1904 +        pthread_mutex_unlock(&palette_lock);
1905 + #endif
1906 + }
1907 +
1908 + static void video_refresh_dga(void)
1909 + {
1910 +        // Quit DGA mode if requested
1911 +        possibly_quit_dga_mode();
1912 +        
1913 +        // Handle X events
1914 +        handle_events();
1915 +        
1916 +        // Handle palette changes
1917 +        handle_palette_changes(depth, DISPLAY_DGA);
1918 + }
1919 +
1920 + #ifdef ENABLE_VOSF
1921 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
1922 + static void video_refresh_dga_vosf(void)
1923 + {
1924 +        // Quit DGA mode if requested
1925 +        possibly_quit_dga_mode();
1926 +        
1927 +        // Handle X events
1928 +        handle_events();
1929 +        
1930 +        // Handle palette changes
1931 +        handle_palette_changes(depth, DISPLAY_DGA);
1932 +        
1933 +        // Update display (VOSF variant)
1934 +        static int tick_counter = 0;
1935 +        if (++tick_counter >= frame_skip) {
1936 +                tick_counter = 0;
1937 + #ifdef HAVE_PTHREADS
1938 +                pthread_mutex_lock(&Screen_draw_lock);
1939 + #endif
1940 +                update_display_dga_vosf();
1941 + #ifdef HAVE_PTHREADS
1942 +                pthread_mutex_unlock(&Screen_draw_lock);
1943 + #endif
1944 +        }
1945 + }
1946 + #endif
1947 +
1948 + static void video_refresh_window_vosf(void)
1949 + {
1950 +        // Quit DGA mode if requested
1951 +        possibly_quit_dga_mode();
1952 +        
1953 +        // Handle X events
1954 +        handle_events();
1955 +        
1956 +        // Handle palette changes
1957 +        handle_palette_changes(depth, DISPLAY_WINDOW);
1958 +        
1959 +        // Update display (VOSF variant)
1960 +        static int tick_counter = 0;
1961 +        if (++tick_counter >= frame_skip) {
1962 +                tick_counter = 0;
1963 + #ifdef HAVE_PTHREADS
1964 +                pthread_mutex_lock(&Screen_draw_lock);
1965 + #endif
1966 +                update_display_window_vosf();
1967 + #ifdef HAVE_PTHREADS
1968 +                pthread_mutex_unlock(&Screen_draw_lock);
1969 + #endif
1970 +        }
1971 + }
1972 + #endif // def ENABLE_VOSF
1973 +
1974 + static void video_refresh_window_static(void)
1975 + {
1976 +        // Handle X events
1977 +        handle_events();
1978 +        
1979 +        // Handle_palette changes
1980 +        handle_palette_changes(depth, DISPLAY_WINDOW);
1981 +        
1982 +        // Update display (static variant)
1983 +        static int tick_counter = 0;
1984 +        if (++tick_counter >= frame_skip) {
1985 +                tick_counter = 0;
1986 +                update_display_static();
1987 +        }
1988 + }
1989 +
1990 + static void video_refresh_window_dynamic(void)
1991 + {
1992 +        // Handle X events
1993 +        handle_events();
1994 +        
1995 +        // Handle_palette changes
1996 +        handle_palette_changes(depth, DISPLAY_WINDOW);
1997 +        
1998 +        // Update display (dynamic variant)
1999 +        static int tick_counter = 0;
2000 +        tick_counter++;
2001 +        update_display_dynamic(tick_counter);
2002 + }
2003 +
2004  
2005   /*
2006   *  Thread for screen refresh, input handling etc.
2007   */
2008  
2009 + void VideoRefreshInit(void)
2010 + {
2011 +        // TODO: set up specialised 8bpp VideoRefresh handlers ?
2012 +        if (display_type == DISPLAY_DGA) {
2013 + #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
2014 +                if (use_vosf)
2015 +                        video_refresh = video_refresh_dga_vosf;
2016 +                else
2017 + #endif
2018 +                        video_refresh = video_refresh_dga;
2019 +        }
2020 +        else {
2021 + #ifdef ENABLE_VOSF
2022 +                if (use_vosf)
2023 +                        video_refresh = video_refresh_window_vosf;
2024 +                else
2025 + #endif
2026 +                if (frame_skip == 0)
2027 +                        video_refresh = video_refresh_window_dynamic;
2028 +                else
2029 +                        video_refresh = video_refresh_window_static;
2030 +        }
2031 + }
2032 +
2033 + void VideoRefresh(void)
2034 + {
2035 +        // TODO: make main_unix/VideoRefresh call directly video_refresh() ?
2036 +        video_refresh();
2037 + }
2038 +
2039 + #if 0
2040   void VideoRefresh(void)
2041   {
2042   #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
# Line 1468 | Line 2084 | void VideoRefresh(void)
2084          static int tick_counter = 0;
2085          if (display_type == DISPLAY_WINDOW) {
2086                  tick_counter++;
2087 <                update_display(tick_counter);
2087 >                if (frame_skip == 0)
2088 >                        update_display_dynamic(tick_counter);
2089 >                else if (tick_counter >= frame_skip) {
2090 >                        tick_counter = 0;
2091 >                        update_display_static();
2092 >                }
2093          }
2094   }
2095 + #endif
2096  
2097   #ifdef HAVE_PTHREADS
2098   static void *redraw_func(void *arg)
2099   {
2100 +        uint64 start = GetTicks_usec();
2101 +        int64 ticks = 0;
2102 +        uint64 next = GetTicks_usec();
2103          while (!redraw_thread_cancel) {
2104 < #ifdef HAVE_NANOSLEEP
2105 <                struct timespec req = {0, 16666667};
2106 <                nanosleep(&req, NULL);
2107 < #else
2108 <                usleep(16667);
2109 < #endif
2110 <                VideoRefresh();
2104 > //              VideoRefresh();
2105 >                video_refresh();
2106 >                next += 16667;
2107 >                int64 delay = next - GetTicks_usec();
2108 >                if (delay > 0)
2109 >                        Delay_usec(delay);
2110 >                else if (delay < -16667)
2111 >                        next = GetTicks_usec();
2112 >                ticks++;
2113          }
2114 +        uint64 end = GetTicks_usec();
2115 +        printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks);
2116          return NULL;
2117   }
2118   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines