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

Comparing BasiliskII/src/Unix/video_vosf.h (file contents):
Revision 1.25 by cebix, 2001-07-03T15:59:47Z vs.
Revision 1.50 by gbeauche, 2005-04-02T09:50:17Z

# Line 1 | Line 1
1   /*
2   *  video_vosf.h - Video/graphics emulation, video on SEGV signals support
3   *
4 < *  Basilisk II (C) 1997-2001 Christian Bauer
4 > *  Basilisk II (C) 1997-2005 Christian Bauer
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 21 | Line 21
21   #ifndef VIDEO_VOSF_H
22   #define VIDEO_VOSF_H
23  
24 < // Note: this file is #include'd in video_x.cpp
24 > // Note: this file must be #include'd only in video_x.cpp
25   #ifdef ENABLE_VOSF
26  
27 #include <fcntl.h>
28 #include <sys/mman.h>
27   #include "sigsegv.h"
28   #include "vm_alloc.h"
29 + #ifdef _WIN32
30 + #include "util_windows.h"
31 + #endif
32  
33 < #ifdef ENABLE_MON
34 < # include "mon.h"
33 > // Glue for SDL and X11 support
34 > #ifdef USE_SDL_VIDEO
35 > #define MONITOR_INIT                    SDL_monitor_desc &monitor
36 > #define VIDEO_DRV_INIT                  driver_window *drv
37 > #define VIDEO_DRV_ROW_BYTES             drv->s->pitch
38 > #define VIDEO_DRV_LOCK_PIXELS   if (SDL_MUSTLOCK(drv->s)) SDL_LockSurface(drv->s)
39 > #define VIDEO_DRV_UNLOCK_PIXELS if (SDL_MUSTLOCK(drv->s)) SDL_UnlockSurface(drv->s)
40 > #else
41 > #ifdef SHEEPSHAVER
42 > #define MONITOR_INIT                    /* nothing */
43 > #define VIDEO_DRV_INIT                  /* nothing */
44 > #define VIDEO_DRV_WINDOW                the_win
45 > #define VIDEO_DRV_GC                    the_gc
46 > #define VIDEO_DRV_IMAGE                 img
47 > #define VIDEO_DRV_HAVE_SHM              have_shm
48 > #else
49 > #define MONITOR_INIT                    X11_monitor_desc &monitor
50 > #define VIDEO_DRV_INIT                  driver_window *drv
51 > #define VIDEO_DRV_WINDOW                drv->w
52 > #define VIDEO_DRV_GC                    drv->gc
53 > #define VIDEO_DRV_IMAGE                 drv->img
54 > #define VIDEO_DRV_HAVE_SHM              drv->have_shm
55 > #endif
56 > #define VIDEO_DRV_LOCK_PIXELS   /* nothing */
57 > #define VIDEO_DRV_UNLOCK_PIXELS /* nothing */
58 > #define VIDEO_DRV_ROW_BYTES             VIDEO_DRV_IMAGE->bytes_per_line
59   #endif
60  
61   // Variables for Video on SEGV support
62   static uint8 *the_host_buffer;  // Host frame buffer in VOSF mode
63 < static uint32 the_buffer_size;  // Size of allocated the_buffer
63 > static uint32 the_host_buffer_row_bytes; // Host frame buffer number of bytes per row
64  
65   struct ScreenPageInfo {
66      int top, bottom;                    // Mapping between this virtual page and Mac scanlines
67   };
68  
69   struct ScreenInfo {
45    uintptr memBase;                    // Real start address
70      uintptr memStart;                   // Start address aligned to page boundary
47    uintptr memEnd;                             // Address of one-past-the-end of the screen
71      uint32 memLength;                   // Length of the memory addressed by the screen pages
72      
73 <    uint32 pageSize;                    // Size of a page
73 >    uintptr pageSize;                   // Size of a page
74      int pageBits;                               // Shift count to get the page number
75      uint32 pageCount;                   // Number of pages allocated to the screen
76      
77          bool dirty;                                     // Flag: set if the frame buffer was touched
78 +        bool very_dirty;                        // Flag: set if the frame buffer was completely modified (e.g. colormap changes)
79      char * dirtyPages;                  // Table of flags set if page was altered
80      ScreenPageInfo * pageInfo;  // Table of mappings page -> Mac scanlines
81   };
# Line 97 | Line 121 | static ScreenInfo mainBuffer;
121   #define PFLAG_CLEAR_ALL do { \
122          PFLAG_CLEAR_RANGE(0, mainBuffer.pageCount); \
123          mainBuffer.dirty = false; \
124 +        mainBuffer.very_dirty = false; \
125 + } while (0)
126 +
127 + #define PFLAG_SET_VERY_DIRTY do { \
128 +        mainBuffer.very_dirty = true; \
129   } while (0)
130  
131   // Set the following macro definition to 1 if your system
# Line 131 | Line 160 | static inline int find_next_page_clear(i
160   #endif
161   }
162  
163 < #ifdef HAVE_PTHREADS
163 > #ifdef HAVE_SPINLOCKS
164 > static spinlock_t vosf_lock = SPIN_LOCK_UNLOCKED;                               // Mutex to protect frame buffer (dirtyPages in fact)
165 > #define LOCK_VOSF spin_lock(&vosf_lock)
166 > #define UNLOCK_VOSF spin_unlock(&vosf_lock)
167 > #elif defined(_WIN32)
168 > static mutex_t vosf_lock;                                                                               // Mutex to protect frame buffer (dirtyPages in fact)
169 > #define LOCK_VOSF vosf_lock.lock();
170 > #define UNLOCK_VOSF vosf_lock.unlock();
171 > #elif defined(HAVE_PTHREADS)
172   static pthread_mutex_t vosf_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer (dirtyPages in fact)
173   #define LOCK_VOSF pthread_mutex_lock(&vosf_lock);
174   #define UNLOCK_VOSF pthread_mutex_unlock(&vosf_lock);
# Line 154 | Line 191 | static int log_base_2(uint32 x)
191   // Extend size to page boundary
192   static uint32 page_extend(uint32 size)
193   {
194 <        const uint32 page_size = getpagesize();
194 >        const uint32 page_size = vm_get_page_size();
195          const uint32 page_mask = page_size - 1;
196          return (size + page_mask) & ~page_mask;
197   }
198  
199  
200   /*
201 < *  Initialize mainBuffer structure
201 > *  Check if VOSF acceleration is profitable on this platform
202   */
203  
204 < static bool video_init_buffer(void)
204 > const int VOSF_PROFITABLE_TRIES = 3;                    // Make 3 attempts for full screen update
205 > const int VOSF_PROFITABLE_THRESHOLD = 16667;    // 60 Hz
206 >
207 > static bool video_vosf_profitable(void)
208   {
209 <        if (use_vosf) {
210 <                const uint32 page_size  = getpagesize();
211 <                const uint32 page_mask  = page_size - 1;
212 <                
213 <                mainBuffer.memBase      = (uintptr) the_buffer;
214 <                // Round up frame buffer base to page boundary
215 <                mainBuffer.memStart             = (uintptr)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
216 <                mainBuffer.memLength    = the_buffer_size;
177 <                mainBuffer.memEnd       = mainBuffer.memStart + mainBuffer.memLength;
178 <
179 <                mainBuffer.pageSize     = page_size;
180 <                mainBuffer.pageCount    = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
181 <                mainBuffer.pageBits     = log_base_2(mainBuffer.pageSize);
182 <
183 <                if (mainBuffer.dirtyPages) {
184 <                        free(mainBuffer.dirtyPages);
185 <                        mainBuffer.dirtyPages = NULL;
209 >        int64 durations[VOSF_PROFITABLE_TRIES];
210 >        int mean_duration = 0;
211 >
212 >        for (int i = 0; i < VOSF_PROFITABLE_TRIES; i++) {
213 >                uint64 start = GetTicks_usec();
214 >                for (int p = 0; p < mainBuffer.pageCount; p++) {
215 >                        uint8 *addr = (uint8 *)(mainBuffer.memStart + (p * mainBuffer.pageSize));
216 >                        addr[0] = 0; // Trigger Screen_fault_handler()
217                  }
218 +                int64 duration = GetTicks_usec() - start;
219 +                mean_duration += duration;
220 +                durations[i] = duration;
221  
222 <                mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2);
222 >                PFLAG_CLEAR_ALL;
223 >                mainBuffer.dirty = false;
224 >                if (vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ) != 0)
225 >                        return false;
226 >        }
227  
228 <                if (mainBuffer.pageInfo) {
229 <                        free(mainBuffer.pageInfo);
230 <                        mainBuffer.pageInfo = NULL;
231 <                }
228 >        mean_duration /= VOSF_PROFITABLE_TRIES;
229 >        D(bug("Triggered %d screen faults in %ld usec on average\n", mainBuffer.pageCount, mean_duration));
230 >        return (mean_duration < (VOSF_PROFITABLE_THRESHOLD * (frame_skip ? frame_skip : 1)));
231 > }
232  
195                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
233  
234 <                if ((mainBuffer.dirtyPages == NULL) || (mainBuffer.pageInfo == NULL))
235 <                        return false;
236 <                
200 <                mainBuffer.dirty = false;
234 > /*
235 > *  Initialize the VOSF system (mainBuffer structure, SIGSEGV handler)
236 > */
237  
238 <                PFLAG_CLEAR_ALL;
239 <                // Safety net to insure the loops in the update routines will terminate
240 <                // See "How can we deal with array overrun conditions ?" hereunder for further details
241 <                PFLAG_CLEAR(mainBuffer.pageCount);
242 <                PFLAG_SET(mainBuffer.pageCount+1);
243 <
244 <                uint32 a = 0;
245 <                for (int i = 0; i < mainBuffer.pageCount; i++) {
246 <                        int y1 = a / VideoMonitor.mode.bytes_per_row;
247 <                        if (y1 >= VideoMonitor.mode.y)
248 <                                y1 = VideoMonitor.mode.y - 1;
249 <
250 <                        int y2 = (a + mainBuffer.pageSize) / VideoMonitor.mode.bytes_per_row;
251 <                        if (y2 >= VideoMonitor.mode.y)
252 <                                y2 = VideoMonitor.mode.y - 1;
253 <
254 <                        mainBuffer.pageInfo[i].top = y1;
255 <                        mainBuffer.pageInfo[i].bottom = y2;
256 <
257 <                        a += mainBuffer.pageSize;
258 <                        if (a > mainBuffer.memLength)
259 <                                a = mainBuffer.memLength;
260 <                }
238 > static bool video_vosf_init(MONITOR_INIT)
239 > {
240 >        VIDEO_MODE_INIT_MONITOR;
241 >
242 >        const uintptr page_size = vm_get_page_size();
243 >        const uintptr page_mask = page_size - 1;
244 >        
245 >        // Round up frame buffer base to page boundary
246 >        mainBuffer.memStart = (((uintptr) the_buffer) + page_mask) & ~page_mask;
247 >        
248 >        // The frame buffer size shall already be aligned to page boundary (use page_extend)
249 >        mainBuffer.memLength = the_buffer_size;
250 >        
251 >        mainBuffer.pageSize = page_size;
252 >        mainBuffer.pageBits = log_base_2(mainBuffer.pageSize);
253 >        mainBuffer.pageCount =  (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
254 >        
255 >        // The "2" more bytes requested are a safety net to insure the
256 >        // loops in the update routines will terminate.
257 >        // See "How can we deal with array overrun conditions ?" hereunder for further details.
258 >        mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2);
259 >        if (mainBuffer.dirtyPages == NULL)
260 >                return false;
261                  
262 <                // We can now write-protect the frame buffer
263 <                if (vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ) != 0)
264 <                        return false;
262 >        PFLAG_CLEAR_ALL;
263 >        PFLAG_CLEAR(mainBuffer.pageCount);
264 >        PFLAG_SET(mainBuffer.pageCount+1);
265 >        
266 >        // Allocate and fill in pageInfo with start and end (inclusive) row in number of bytes
267 >        mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
268 >        if (mainBuffer.pageInfo == NULL)
269 >                return false;
270 >        
271 >        uint32 a = 0;
272 >        for (unsigned i = 0; i < mainBuffer.pageCount; i++) {
273 >                unsigned y1 = a / VIDEO_MODE_ROW_BYTES;
274 >                if (y1 >= VIDEO_MODE_Y)
275 >                        y1 = VIDEO_MODE_Y - 1;
276 >
277 >                unsigned y2 = (a + mainBuffer.pageSize) / VIDEO_MODE_ROW_BYTES;
278 >                if (y2 >= VIDEO_MODE_Y)
279 >                        y2 = VIDEO_MODE_Y - 1;
280 >
281 >                mainBuffer.pageInfo[i].top = y1;
282 >                mainBuffer.pageInfo[i].bottom = y2;
283 >
284 >                a += mainBuffer.pageSize;
285 >                if (a > mainBuffer.memLength)
286 >                        a = mainBuffer.memLength;
287          }
288 +        
289 +        // We can now write-protect the frame buffer
290 +        if (vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ) != 0)
291 +                return false;
292 +        
293 +        // The frame buffer is sane, i.e. there is no write to it yet
294 +        mainBuffer.dirty = false;
295          return true;
296   }
297  
298  
299   /*
300 + * Deinitialize VOSF system
301 + */
302 +
303 + static void video_vosf_exit(void)
304 + {
305 +        if (mainBuffer.pageInfo) {
306 +                free(mainBuffer.pageInfo);
307 +                mainBuffer.pageInfo = NULL;
308 +        }
309 +        if (mainBuffer.dirtyPages) {
310 +                free(mainBuffer.dirtyPages);
311 +                mainBuffer.dirtyPages = NULL;
312 +        }
313 + }
314 +
315 +
316 + /*
317   * Screen fault handler
318   */
319  
320 < static bool screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
320 > bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
321   {
240        D(bug("screen_fault_handler: ADDR=0x%08X from IP=0x%08X\n", fault_address, fault_instruction));
322          const uintptr addr = (uintptr)fault_address;
323          
324          /* Someone attempted to write to the frame buffer. Make it writeable
325           * now so that the data could actually be written to. It will be made
326           * read-only back in one of the screen update_*() functions.
327           */
328 <        if ((addr >= mainBuffer.memStart) && (addr < mainBuffer.memEnd)) {
329 <                const int page  = (addr - mainBuffer.memStart) >> mainBuffer.pageBits;
249 <                caddr_t page_ad = (caddr_t)(addr & -mainBuffer.pageSize);
328 >        if (((uintptr)addr - mainBuffer.memStart) < mainBuffer.memLength) {
329 >                const int page  = ((uintptr)addr - mainBuffer.memStart) >> mainBuffer.pageBits;
330                  LOCK_VOSF;
331                  PFLAG_SET(page);
332 <                vm_protect((char *)page_ad, mainBuffer.pageSize, VM_PAGE_READ | VM_PAGE_WRITE);
332 >                vm_protect((char *)(addr & -mainBuffer.pageSize), mainBuffer.pageSize, VM_PAGE_READ | VM_PAGE_WRITE);
333                  mainBuffer.dirty = true;
334                  UNLOCK_VOSF;
335                  return true;
336          }
337          
338          /* Otherwise, we don't know how to handle the fault, let it crash */
259        fprintf(stderr, "do_handle_screen_fault: unhandled address 0x%08X", addr);
260        if (fault_instruction != SIGSEGV_INVALID_PC)
261                fprintf(stderr, " [IP=0x%08X]", fault_instruction);
262        fprintf(stderr, "\n");
263 #if EMULATED_68K
264        uaecptr nextpc;
265        extern void m68k_dumpstate(uaecptr *nextpc);
266        m68k_dumpstate(&nextpc);
267 #endif
268        VideoQuitFullScreen();
269 #ifdef ENABLE_MON
270        char *arg[4] = {"mon", "-m", "-r", NULL};
271        mon(3, arg);
272        QuitEmulator();
273 #endif
339          return false;
340   }
341  
# Line 279 | Line 344 | static bool screen_fault_handler(sigsegv
344   *      Update display for Windowed mode and VOSF
345   */
346  
282 // From video_blit.cpp
283 extern void (*Screen_blit)(uint8 * dest, const uint8 * source, uint32 length);
284 extern bool Screen_blitter_init(XVisualInfo * visual_info, bool native_byte_order, video_depth mac_depth);
285 extern uint32 ExpandMap[256];
286
347   /*      How can we deal with array overrun conditions ?
348          
349          The state of the framebuffer pages that have been touched are maintained
# Line 317 | Line 377 | There are two cases to check:
377          than pageCount.
378   */
379  
380 < static inline void update_display_window_vosf(driver_window *drv)
380 > static inline void update_display_window_vosf(VIDEO_DRV_INIT)
381   {
382 +        VIDEO_MODE_INIT;
383 +
384          int page = 0;
385          for (;;) {
386 <                const int first_page = find_next_page_set(page);
386 >                const unsigned first_page = find_next_page_set(page);
387                  if (first_page >= mainBuffer.pageCount)
388                          break;
389  
# Line 337 | Line 399 | static inline void update_display_window
399                  const int y1 = mainBuffer.pageInfo[first_page].top;
400                  const int y2 = mainBuffer.pageInfo[page - 1].bottom;
401                  const int height = y2 - y1 + 1;
340                
341                if (VideoMonitor.mode.depth < VDEPTH_8BIT) {
402  
403 <                        // Update the_host_buffer and copy of the_buffer
404 <                        const int src_bytes_per_row = VideoMonitor.mode.bytes_per_row;
405 <                        const int dst_bytes_per_row = drv->img->bytes_per_line;
406 <                        const int pixels_per_byte = VideoMonitor.mode.x / src_bytes_per_row;
407 <                        int i1 = y1 * src_bytes_per_row, i2 = y1 * dst_bytes_per_row, j;
408 <                        for (j = y1; j <= y2; j++) {
409 <                                Screen_blit(the_host_buffer + i2, the_buffer + i1, VideoMonitor.mode.x / pixels_per_byte);
410 <                                i1 += src_bytes_per_row;
411 <                                i2 += dst_bytes_per_row;
352 <                        }
353 <
354 <                } else {
355 <
356 <                        // Update the_host_buffer and copy of the_buffer
357 <                        const int src_bytes_per_row = VideoMonitor.mode.bytes_per_row;
358 <                        const int dst_bytes_per_row = drv->img->bytes_per_line;
359 <                        const int bytes_per_pixel = src_bytes_per_row / VideoMonitor.mode.x;
360 <                        int i1 = y1 * src_bytes_per_row, i2 = y1 * dst_bytes_per_row, j;
361 <                        for (j = y1; j <= y2; j++) {
362 <                                Screen_blit(the_host_buffer + i2, the_buffer + i1, bytes_per_pixel * VideoMonitor.mode.x);
363 <                                i1 += src_bytes_per_row;
364 <                                i2 += dst_bytes_per_row;
365 <                        }
403 >                // Update the_host_buffer
404 >                VIDEO_DRV_LOCK_PIXELS;
405 >                const int src_bytes_per_row = VIDEO_MODE_ROW_BYTES;
406 >                const int dst_bytes_per_row = VIDEO_DRV_ROW_BYTES;
407 >                int i1 = y1 * src_bytes_per_row, i2 = y1 * dst_bytes_per_row, j;
408 >                for (j = y1; j <= y2; j++) {
409 >                        Screen_blit(the_host_buffer + i2, the_buffer + i1, src_bytes_per_row);
410 >                        i1 += src_bytes_per_row;
411 >                        i2 += dst_bytes_per_row;
412                  }
413 +                VIDEO_DRV_UNLOCK_PIXELS;
414  
415 <                if (drv->have_shm)
416 <                        XShmPutImage(x_display, drv->w, drv->gc, drv->img, 0, y1, 0, y1, VideoMonitor.mode.x, height, 0);
415 > #ifdef USE_SDL_VIDEO
416 >                SDL_UpdateRect(drv->s, 0, y1, VIDEO_MODE_X, height);
417 > #else
418 >                if (VIDEO_DRV_HAVE_SHM)
419 >                        XShmPutImage(x_display, VIDEO_DRV_WINDOW, VIDEO_DRV_GC, VIDEO_DRV_IMAGE, 0, y1, 0, y1, VIDEO_MODE_X, height, 0);
420                  else
421 <                        XPutImage(x_display, drv->w, drv->gc, drv->img, 0, y1, 0, y1, VideoMonitor.mode.x, height);
421 >                        XPutImage(x_display, VIDEO_DRV_WINDOW, VIDEO_DRV_GC, VIDEO_DRV_IMAGE, 0, y1, 0, y1, VIDEO_MODE_X, height);
422 > #endif
423          }
424          mainBuffer.dirty = false;
425   }
# Line 382 | Line 433 | static inline void update_display_window
433   #if REAL_ADDRESSING || DIRECT_ADDRESSING
434   static inline void update_display_dga_vosf(void)
435   {
436 +        VIDEO_MODE_INIT;
437 +
438 +        if (mainBuffer.very_dirty) {
439 +                PFLAG_CLEAR_ALL;
440 +                vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ);
441 +                VIDEO_DRV_LOCK_PIXELS;
442 +                memcpy(the_buffer_copy, the_buffer, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
443 +                Screen_blit(the_host_buffer, the_buffer, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
444 +                VIDEO_DRV_UNLOCK_PIXELS;
445 +                return;
446 +        }
447 +
448          int page = 0;
449          for (;;) {
450 <                const int first_page = find_next_page_set(page);
450 >                const unsigned first_page = find_next_page_set(page);
451                  if (first_page >= mainBuffer.pageCount)
452                          break;
453  
# Line 396 | Line 459 | static inline void update_display_dga_vo
459                  const uint32 length = (page - first_page) << mainBuffer.pageBits;
460                  vm_protect((char *)mainBuffer.memStart + offset, length, VM_PAGE_READ);
461                  
462 <                // I am sure that y2 >= y1 and depth != 1
462 >                // There is at least one line to update
463                  const int y1 = mainBuffer.pageInfo[first_page].top;
464                  const int y2 = mainBuffer.pageInfo[page - 1].bottom;
465 <                
466 <                const int bytes_per_row = VideoMonitor.mode.bytes_per_row;
467 <                const int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
465 >
466 > #ifndef USE_SDL_VIDEO
467 >                // Update the_host_buffer and copy of the_buffer (use 64 bytes chunks)
468 >                const int src_bytes_per_row = VIDEO_MODE_ROW_BYTES;
469 >                const int dst_bytes_per_row = the_host_buffer_row_bytes;
470 >                const int n_pixels = 64;
471 >                const int n_chunks = VIDEO_MODE_X / n_pixels;
472 >                const int src_chunk_size = src_bytes_per_row / n_chunks;
473 >                const int dst_chunk_size = dst_bytes_per_row / n_chunks;
474 >                const int src_chunk_size_left = src_bytes_per_row - (n_chunks * src_chunk_size);
475 >                const int dst_chunk_size_left = dst_bytes_per_row - (n_chunks * dst_chunk_size);
476 >                int i1 = y1 * src_bytes_per_row;
477 >                int i2 = y1 * dst_bytes_per_row;
478 >                VIDEO_DRV_LOCK_PIXELS;
479 >                for (int j = y1; j <= y2; j++) {
480 >                        for (int i = 0; i < n_chunks; i++) {
481 >                                if (memcmp(the_buffer_copy + i1, the_buffer + i1, src_chunk_size) != 0) {
482 >                                        memcpy(the_buffer_copy + i1, the_buffer + i1, src_chunk_size);
483 >                                        Screen_blit(the_host_buffer + i2, the_buffer + i1, src_chunk_size);
484 >                                }
485 >                                i1 += src_chunk_size;
486 >                                i2 += dst_chunk_size;
487 >                        }
488 >                        if (src_chunk_size_left && dst_chunk_size_left) {
489 >                                if (memcmp(the_buffer_copy + i1, the_buffer + i1, src_chunk_size_left) != 0) {
490 >                                        memcpy(the_buffer_copy + i1, the_buffer + i1, src_chunk_size_left);
491 >                                        Screen_blit(the_host_buffer + i2, the_buffer + i1, src_chunk_size_left);
492 >                                }
493 >                                i1 += src_chunk_size_left;
494 >                                i2 += dst_chunk_size_left;
495 >                        }
496 >                }
497 >                VIDEO_DRV_UNLOCK_PIXELS;
498 > #else
499 >                // Check for first chunk from left and first chunk from right that have changed
500 >                typedef uint64 chunk_t;
501 >                const int chunk_size = sizeof(chunk_t);
502 >                const int bytes_per_row = VIDEO_MODE_ROW_BYTES;
503 >
504                  int i, j;
505 <                
506 <                // Check for first column from left and first column
408 <                // from right that have changed
409 <                int x1 = VideoMonitor.mode.x * bytes_per_pixel - 1;
505 >                int b1 = bytes_per_row / chunk_size;
506 >                int b2 = 0;
507                  for (j = y1; j <= y2; j++) {
508 <                        uint8 * const p1 = &the_buffer[j * bytes_per_row];
509 <                        uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
510 <                        for (i = 0; i < x1; i++) {
508 >                        chunk_t * const p1 = (chunk_t *)(the_buffer + (j * bytes_per_row));
509 >                        chunk_t * const p2 = (chunk_t *)(the_buffer_copy + (j * bytes_per_row));
510 >                        for (i = 0; i < b1; i++) {
511                                  if (p1[i] != p2[i]) {
512 <                                        x1 = i;
512 >                                        b1 = i;
513                                          break;
514                                  }
515                          }
516 <                }
517 <                x1 /= bytes_per_pixel;
518 <                
422 <                int x2 = x1 * bytes_per_pixel;
423 <                for (j = y2; j >= y1; j--) {
424 <                        uint8 * const p1 = &the_buffer[j * bytes_per_row];
425 <                        uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
426 <                        for (i = VideoMonitor.mode.x * bytes_per_pixel - 1; i > x2; i--) {
516 >                        if (b1 > b2)
517 >                                b2 = b1;
518 >                        for (i = (bytes_per_row / chunk_size) - 1; i > b2; i--) {
519                                  if (p1[i] != p2[i]) {
520 <                                        x2 = i;
520 >                                        b2 = i;
521                                          break;
522                                  }
523                          }
524                  }
525 <                x2 /= bytes_per_pixel;
526 <                
525 >                b2++;
526 >
527 >                // Convert to pixel information
528 >                int x1, x2;
529 >                switch (VIDEO_MODE_DEPTH) {
530 >                case VIDEO_DEPTH_1BIT:  x1 = (b1 * chunk_size) << 3; x2 = (b2 * chunk_size) << 3;       break;
531 >                case VIDEO_DEPTH_2BIT:  x1 = (b1 * chunk_size) << 2; x2 = (b2 * chunk_size) << 2;       break;
532 >                case VIDEO_DEPTH_4BIT:  x1 = (b1 * chunk_size) << 1; x2 = (b2 * chunk_size) << 1;       break;
533 >                case VIDEO_DEPTH_8BIT:  x1 = b1 * chunk_size; x2 = b2 * chunk_size;                                     break;
534 >                case VIDEO_DEPTH_16BIT: x1 = (b1 * chunk_size) >> 1; x2 = (b2 * chunk_size) >> 1;       break;
535 >                case VIDEO_DEPTH_32BIT: x1 = (b1 * chunk_size) >> 2; x2 = (b2 * chunk_size) >> 2;       break;
536 >                }
537 >                const int width = x2 - x1;
538 >
539 >                // Normalize bounds for for the next blit
540 >                const int src_bytes_per_row = VIDEO_MODE_ROW_BYTES;
541 >                const int dst_bytes_per_row = the_host_buffer_row_bytes;
542 >                const int dst_bytes_per_pixel = dst_bytes_per_row / VIDEO_MODE_X;
543 >                int i2 = y1 * dst_bytes_per_row + x1 * dst_bytes_per_pixel;
544 >                int i1, n_bytes;
545 >                if ((int)VIDEO_MODE_DEPTH < VIDEO_DEPTH_8BIT) {
546 >                        const int src_pixels_per_byte = VIDEO_MODE_X / src_bytes_per_row;
547 >                        i1 = y1 * src_bytes_per_row + x1 / src_pixels_per_byte;
548 >                        n_bytes = width / src_pixels_per_byte;
549 >                } else {
550 >                        const int src_bytes_per_pixel = src_bytes_per_row / VIDEO_MODE_X;
551 >                        i1 = y1 * src_bytes_per_row + x1 * src_bytes_per_pixel;
552 >                        n_bytes = width * src_bytes_per_pixel;
553 >                }
554 >
555                  // Update the_host_buffer and copy of the_buffer
556 <                // There should be at least one pixel to copy
437 <                const int width = x2 - x1 + 1;
438 <                i = y1 * bytes_per_row + x1 * bytes_per_pixel;
556 >                VIDEO_DRV_LOCK_PIXELS;
557                  for (j = y1; j <= y2; j++) {
558 <                        Screen_blit(the_host_buffer + i, the_buffer + i, bytes_per_pixel * width);
559 <                        memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * width);
560 <                        i += bytes_per_row;
558 >                        Screen_blit(the_host_buffer + i2, the_buffer + i1, n_bytes);
559 >                        memcpy(the_buffer_copy + i1, the_buffer + i1, n_bytes);
560 >                        i1 += src_bytes_per_row;
561 >                        i2 += dst_bytes_per_row;
562                  }
563 +                VIDEO_DRV_UNLOCK_PIXELS;
564 + #endif
565          }
566          mainBuffer.dirty = false;
567   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines