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.15 by cebix, 2001-03-06T18:41:12Z vs.
Revision 1.59 by gbeauche, 2006-05-14T08:32:05Z

# 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 < /*
28 < *  Page-aligned memory allocation
29 < */
27 > #include "sigsegv.h"
28 > #include "vm_alloc.h"
29 > #ifdef _WIN32
30 > #include "util_windows.h"
31 > #endif
32 >
33 > // Glue for SDL and X11 support
34 > #ifdef TEST_VOSF_PERFORMANCE
35 > #define MONITOR_INIT                    /* nothing */
36 > #else
37 > #ifdef USE_SDL_VIDEO
38 > #define MONITOR_INIT                    SDL_monitor_desc &monitor
39 > #define VIDEO_DRV_WIN_INIT              driver_window *drv
40 > #define VIDEO_DRV_DGA_INIT              driver_fullscreen *drv
41 > #define VIDEO_DRV_LOCK_PIXELS   SDL_VIDEO_LOCK_SURFACE(drv->s)
42 > #define VIDEO_DRV_UNLOCK_PIXELS SDL_VIDEO_UNLOCK_SURFACE(drv->s)
43 > #define VIDEO_DRV_DEPTH                 drv->s->format->BitsPerPixel
44 > #define VIDEO_DRV_WIDTH                 drv->s->w
45 > #define VIDEO_DRV_HEIGHT                drv->s->h
46 > #define VIDEO_DRV_ROW_BYTES             drv->s->pitch
47 > #else
48 > #ifdef SHEEPSHAVER
49 > #define MONITOR_INIT                    /* nothing */
50 > #define VIDEO_DRV_WIN_INIT              /* nothing */
51 > #define VIDEO_DRV_DGA_INIT              /* nothing */
52 > #define VIDEO_DRV_WINDOW                the_win
53 > #define VIDEO_DRV_GC                    the_gc
54 > #define VIDEO_DRV_IMAGE                 img
55 > #define VIDEO_DRV_HAVE_SHM              have_shm
56 > #else
57 > #define MONITOR_INIT                    X11_monitor_desc &monitor
58 > #define VIDEO_DRV_WIN_INIT              driver_window *drv
59 > #define VIDEO_DRV_DGA_INIT              driver_dga *drv
60 > #define VIDEO_DRV_WINDOW                drv->w
61 > #define VIDEO_DRV_GC                    drv->gc
62 > #define VIDEO_DRV_IMAGE                 drv->img
63 > #define VIDEO_DRV_HAVE_SHM              drv->have_shm
64 > #endif
65 > #define VIDEO_DRV_LOCK_PIXELS   /* nothing */
66 > #define VIDEO_DRV_UNLOCK_PIXELS /* nothing */
67 > #define VIDEO_DRV_DEPTH                 VIDEO_DRV_IMAGE->depth
68 > #define VIDEO_DRV_WIDTH                 VIDEO_DRV_IMAGE->width
69 > #define VIDEO_DRV_HEIGHT                VIDEO_DRV_IMAGE->height
70 > #define VIDEO_DRV_ROW_BYTES             VIDEO_DRV_IMAGE->bytes_per_line
71 > #endif
72 > #endif
73  
74 < // Align on page boundaries
75 < static uintptr align_on_page_boundary(uintptr size)
74 > // Prototypes
75 > static void vosf_do_set_dirty_area(uintptr first, uintptr last);
76 > static void vosf_set_dirty_area(int x, int y, int w, int h, int screen_width, int screen_height, int bytes_per_row);
77 >
78 > // Variables for Video on SEGV support
79 > static uint8 *the_host_buffer;  // Host frame buffer in VOSF mode
80 >
81 > struct ScreenPageInfo {
82 >    int top, bottom;                    // Mapping between this virtual page and Mac scanlines
83 > };
84 >
85 > struct ScreenInfo {
86 >    uintptr memStart;                   // Start address aligned to page boundary
87 >    uint32 memLength;                   // Length of the memory addressed by the screen pages
88 >    
89 >    uintptr pageSize;                   // Size of a page
90 >    int pageBits;                               // Shift count to get the page number
91 >    uint32 pageCount;                   // Number of pages allocated to the screen
92 >    
93 >        bool dirty;                                     // Flag: set if the frame buffer was touched
94 >        bool very_dirty;                        // Flag: set if the frame buffer was completely modified (e.g. colormap changes)
95 >    char * dirtyPages;                  // Table of flags set if page was altered
96 >    ScreenPageInfo * pageInfo;  // Table of mappings page -> Mac scanlines
97 > };
98 >
99 > static ScreenInfo mainBuffer;
100 >
101 > #define PFLAG_SET_VALUE                 0x00
102 > #define PFLAG_CLEAR_VALUE               0x01
103 > #define PFLAG_SET_VALUE_4               0x00000000
104 > #define PFLAG_CLEAR_VALUE_4             0x01010101
105 > #define PFLAG_SET(page)                 mainBuffer.dirtyPages[page] = PFLAG_SET_VALUE
106 > #define PFLAG_CLEAR(page)               mainBuffer.dirtyPages[page] = PFLAG_CLEAR_VALUE
107 > #define PFLAG_ISSET(page)               (mainBuffer.dirtyPages[page] == PFLAG_SET_VALUE)
108 > #define PFLAG_ISCLEAR(page)             (mainBuffer.dirtyPages[page] != PFLAG_SET_VALUE)
109 >
110 > #ifdef UNALIGNED_PROFITABLE
111 > # define PFLAG_ISSET_4(page)    (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_SET_VALUE_4)
112 > # define PFLAG_ISCLEAR_4(page)  (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_CLEAR_VALUE_4)
113 > #else
114 > # define PFLAG_ISSET_4(page) \
115 >                PFLAG_ISSET(page  ) && PFLAG_ISSET(page+1) \
116 >        &&      PFLAG_ISSET(page+2) && PFLAG_ISSET(page+3)
117 > # define PFLAG_ISCLEAR_4(page) \
118 >                PFLAG_ISCLEAR(page  ) && PFLAG_ISCLEAR(page+1) \
119 >        &&      PFLAG_ISCLEAR(page+2) && PFLAG_ISCLEAR(page+3)
120 > #endif
121 >
122 > // Set the selected page range [ first_page, last_page [ into the SET state
123 > #define PFLAG_SET_RANGE(first_page, last_page) \
124 >        memset(mainBuffer.dirtyPages + (first_page), PFLAG_SET_VALUE, \
125 >                (last_page) - (first_page))
126 >
127 > // Set the selected page range [ first_page, last_page [ into the CLEAR state
128 > #define PFLAG_CLEAR_RANGE(first_page, last_page) \
129 >        memset(mainBuffer.dirtyPages + (first_page), PFLAG_CLEAR_VALUE, \
130 >                (last_page) - (first_page))
131 >
132 > #define PFLAG_SET_ALL do { \
133 >        PFLAG_SET_RANGE(0, mainBuffer.pageCount); \
134 >        mainBuffer.dirty = true; \
135 > } while (0)
136 >
137 > #define PFLAG_CLEAR_ALL do { \
138 >        PFLAG_CLEAR_RANGE(0, mainBuffer.pageCount); \
139 >        mainBuffer.dirty = false; \
140 >        mainBuffer.very_dirty = false; \
141 > } while (0)
142 >
143 > #define PFLAG_SET_VERY_DIRTY do { \
144 >        mainBuffer.very_dirty = true; \
145 > } while (0)
146 >
147 > // Set the following macro definition to 1 if your system
148 > // provides a really fast strchr() implementation
149 > //#define HAVE_FAST_STRCHR 0
150 >
151 > static inline int find_next_page_set(int page)
152 > {
153 > #if HAVE_FAST_STRCHR
154 >        char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_SET_VALUE);
155 >        return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
156 > #else
157 >        while (PFLAG_ISCLEAR_4(page))
158 >                page += 4;
159 >        while (PFLAG_ISCLEAR(page))
160 >                page++;
161 >        return page;
162 > #endif
163 > }
164 >
165 > static inline int find_next_page_clear(int page)
166   {
167 <        const uint32 page_size = getpagesize();
168 <        const uint32 page_mask = page_size - 1;
169 <        return (size + page_mask) & ~page_mask;
167 > #if HAVE_FAST_STRCHR
168 >        char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_CLEAR_VALUE);
169 >        return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
170 > #else
171 >        while (PFLAG_ISSET_4(page))
172 >                page += 4;
173 >        while (PFLAG_ISSET(page))
174 >                page++;
175 >        return page;
176 > #endif
177 > }
178 >
179 > #if defined(HAVE_PTHREADS)
180 > static pthread_mutex_t vosf_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer (dirtyPages in fact)
181 > #define LOCK_VOSF pthread_mutex_lock(&vosf_lock);
182 > #define UNLOCK_VOSF pthread_mutex_unlock(&vosf_lock);
183 > #elif defined(_WIN32)
184 > static mutex_t vosf_lock;                                                                               // Mutex to protect frame buffer (dirtyPages in fact)
185 > #define LOCK_VOSF vosf_lock.lock();
186 > #define UNLOCK_VOSF vosf_lock.unlock();
187 > #elif defined(HAVE_SPINLOCKS)
188 > static spinlock_t vosf_lock = SPIN_LOCK_UNLOCKED;                               // Mutex to protect frame buffer (dirtyPages in fact)
189 > #define LOCK_VOSF spin_lock(&vosf_lock)
190 > #define UNLOCK_VOSF spin_unlock(&vosf_lock)
191 > #else
192 > #define LOCK_VOSF
193 > #define UNLOCK_VOSF
194 > #endif
195 >
196 > static int log_base_2(uint32 x)
197 > {
198 >        uint32 mask = 0x80000000;
199 >        int l = 31;
200 >        while (l >= 0 && (x & mask) == 0) {
201 >                mask >>= 1;
202 >                l--;
203 >        }
204 >        return l;
205   }
206  
207 < // Allocate memory on page boundary
208 < static void * allocate_framebuffer(uint32 size, uint8 * hint = 0)
207 > // Extend size to page boundary
208 > static uint32 page_extend(uint32 size)
209   {
210 <        // Remind that the system can allocate at 0x00000000...
211 <        return mmap((caddr_t)hint, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0);
210 >        const uint32 page_size = vm_get_page_size();
211 >        const uint32 page_mask = page_size - 1;
212 >        return (size + page_mask) & ~page_mask;
213   }
214  
215  
216   /*
217 < *      Screen fault handler
217 > *  Check if VOSF acceleration is profitable on this platform
218   */
219  
220 < const uintptr INVALID_PC = (uintptr)-1;
220 > const int VOSF_PROFITABLE_TRIES = 3;                    // Make 3 attempts for full screen update
221 > const int VOSF_PROFITABLE_THRESHOLD = 16667;    // 60 Hz
222  
223 < static inline void do_handle_screen_fault(uintptr addr, uintptr pc = INVALID_PC)
223 > static bool video_vosf_profitable(void)
224   {
225 <        /* Someone attempted to write to the frame buffer. Make it writeable
226 <         * now so that the data could actually be written. It will be made
227 <         * read-only back in one of the screen update_*() functions.
228 <         */
229 <        if ((addr >= mainBuffer.memStart) && (addr < mainBuffer.memEnd)) {
230 <                const int page  = (addr - mainBuffer.memStart) >> mainBuffer.pageBits;
231 <                caddr_t page_ad = (caddr_t)(addr & ~(mainBuffer.pageSize - 1));
232 <                LOCK_VOSF;
233 <                PFLAG_SET(page);
234 <                mprotect(page_ad, mainBuffer.pageSize, PROT_READ | PROT_WRITE);
235 <                mainBuffer.dirty = true;
236 <                UNLOCK_VOSF;
237 <                return;
225 >        int64 durations[VOSF_PROFITABLE_TRIES];
226 >        int mean_duration = 0;
227 >
228 > #ifdef SHEEPSHAVER
229 >        const bool accel = PrefsFindBool("gfxaccel");
230 > #else
231 >        const bool accel = false;
232 > #endif
233 >
234 >        for (int i = 0; i < VOSF_PROFITABLE_TRIES; i++) {
235 >                uint64 start = GetTicks_usec();
236 >                for (int p = 0; p < mainBuffer.pageCount; p++) {
237 >                        uint8 *addr = (uint8 *)(mainBuffer.memStart + (p * mainBuffer.pageSize));
238 >                        if (accel)
239 >                                vosf_do_set_dirty_area((uintptr)addr, (uintptr)addr + mainBuffer.pageSize - 1);
240 >                        else
241 >                                addr[0] = 0; // Trigger Screen_fault_handler()
242 >                }
243 >                int64 duration = GetTicks_usec() - start;
244 >                mean_duration += duration;
245 >                durations[i] = duration;
246 >
247 >                PFLAG_CLEAR_ALL;
248 >                mainBuffer.dirty = false;
249 >                if (vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ) != 0)
250 >                        return false;
251          }
252 <        
253 <        /* Otherwise, we don't know how to handle the fault, let it crash */
254 <        fprintf(stderr, "do_handle_screen_fault: unhandled address 0x%08X", addr);
255 <        if (pc != INVALID_PC)
73 <                fprintf(stderr, " [IP=0x%08X]", pc);
74 <        fprintf(stderr, "\n");
75 <        
76 <        signal(SIGSEGV, SIG_DFL);
252 >
253 >        mean_duration /= VOSF_PROFITABLE_TRIES;
254 >        D(bug("Triggered %d screen faults in %ld usec on average\n", mainBuffer.pageCount, mean_duration));
255 >        return (mean_duration < (VOSF_PROFITABLE_THRESHOLD * (frame_skip ? frame_skip : 1)));
256   }
257  
79 #if defined(HAVE_SIGINFO_T)
258  
259 < static void Screen_fault_handler(int, siginfo_t * sip, void *)
259 > /*
260 > *  Initialize the VOSF system (mainBuffer structure, SIGSEGV handler)
261 > */
262 >
263 > static bool video_vosf_init(MONITOR_INIT)
264   {
265 <        D(bug("Screen_fault_handler: ADDR=0x%08X\n", sip->si_addr));
266 <        do_handle_screen_fault((uintptr)sip->si_addr);
265 >        VIDEO_MODE_INIT_MONITOR;
266 >
267 >        const uintptr page_size = vm_get_page_size();
268 >        const uintptr page_mask = page_size - 1;
269 >        
270 >        // Round up frame buffer base to page boundary
271 >        mainBuffer.memStart = (((uintptr) the_buffer) + page_mask) & ~page_mask;
272 >        
273 >        // The frame buffer size shall already be aligned to page boundary (use page_extend)
274 >        mainBuffer.memLength = the_buffer_size;
275 >        
276 >        mainBuffer.pageSize = page_size;
277 >        mainBuffer.pageBits = log_base_2(mainBuffer.pageSize);
278 >        mainBuffer.pageCount =  (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
279 >        
280 >        // The "2" more bytes requested are a safety net to insure the
281 >        // loops in the update routines will terminate.
282 >        // See "How can we deal with array overrun conditions ?" hereunder for further details.
283 >        mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2);
284 >        if (mainBuffer.dirtyPages == NULL)
285 >                return false;
286 >                
287 >        PFLAG_CLEAR_ALL;
288 >        PFLAG_CLEAR(mainBuffer.pageCount);
289 >        PFLAG_SET(mainBuffer.pageCount+1);
290 >        
291 >        // Allocate and fill in pageInfo with start and end (inclusive) row in number of bytes
292 >        mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
293 >        if (mainBuffer.pageInfo == NULL)
294 >                return false;
295 >        
296 >        uint32 a = 0;
297 >        for (unsigned i = 0; i < mainBuffer.pageCount; i++) {
298 >                unsigned y1 = a / VIDEO_MODE_ROW_BYTES;
299 >                if (y1 >= VIDEO_MODE_Y)
300 >                        y1 = VIDEO_MODE_Y - 1;
301 >
302 >                unsigned y2 = (a + mainBuffer.pageSize) / VIDEO_MODE_ROW_BYTES;
303 >                if (y2 >= VIDEO_MODE_Y)
304 >                        y2 = VIDEO_MODE_Y - 1;
305 >
306 >                mainBuffer.pageInfo[i].top = y1;
307 >                mainBuffer.pageInfo[i].bottom = y2;
308 >
309 >                a += mainBuffer.pageSize;
310 >                if (a > mainBuffer.memLength)
311 >                        a = mainBuffer.memLength;
312 >        }
313 >        
314 >        // We can now write-protect the frame buffer
315 >        if (vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ) != 0)
316 >                return false;
317 >        
318 >        // The frame buffer is sane, i.e. there is no write to it yet
319 >        mainBuffer.dirty = false;
320 >        return true;
321   }
322  
87 #elif defined(HAVE_SIGCONTEXT_SUBTERFUGE)
323  
324 < # if defined(__i386__) && defined(__linux__)
325 < static void Screen_fault_handler(int, struct sigcontext scs)
324 > /*
325 > * Deinitialize VOSF system
326 > */
327 >
328 > static void video_vosf_exit(void)
329   {
330 <        D(bug("Screen_fault_handler: ADDR=0x%08X from IP=0x%08X\n", scs.cr2, scs.eip));
331 <        do_handle_screen_fault((uintptr)scs.cr2, (uintptr)scs.eip);
330 >        if (mainBuffer.pageInfo) {
331 >                free(mainBuffer.pageInfo);
332 >                mainBuffer.pageInfo = NULL;
333 >        }
334 >        if (mainBuffer.dirtyPages) {
335 >                free(mainBuffer.dirtyPages);
336 >                mainBuffer.dirtyPages = NULL;
337 >        }
338   }
339  
96 # elif defined(__m68k__) && defined(__NetBSD__)
340  
341 < # include <m68k/frame.h>
342 < static void Screen_fault_handler(int, int code, struct sigcontext *scp)
341 > /*
342 > * Update VOSF state with specified dirty area
343 > */
344 >
345 > static void vosf_do_set_dirty_area(uintptr first, uintptr last)
346   {
347 <        D(bug("Screen_fault_handler: ADDR=0x%08X\n", code));
348 <        struct sigstate {
349 <                int ss_flags;
350 <                struct frame ss_frame;
351 <        };
352 <        struct sigstate *state = (struct sigstate *)scp->sc_ap;
353 <        uintptr fault_addr;
354 <        switch (state->ss_frame.f_format) {
355 <                case 7:         // 68040 access error
110 <                        // "code" is sometimes unreliable (i.e. contains NULL or a bogus address), reason unknown
111 <                        fault_addr = state->ss_frame.f_fmt7.f_fa;
112 <                        break;
113 <                default:
114 <                        fault_addr = (uintptr)code;
115 <                        break;
347 >        const int first_page = (first - mainBuffer.memStart) >> mainBuffer.pageBits;
348 >        const int last_page = (last - mainBuffer.memStart) >> mainBuffer.pageBits;
349 >        uint8 *addr = (uint8 *)(first & -mainBuffer.pageSize);
350 >        for (int i = first_page; i <= last_page; i++) {
351 >                if (PFLAG_ISCLEAR(i)) {
352 >                        PFLAG_SET(i);
353 >                        vm_protect(addr, mainBuffer.pageSize, VM_PAGE_READ | VM_PAGE_WRITE);
354 >                }
355 >                addr += mainBuffer.pageSize;
356          }
117        do_handle_screen_fault(fault_addr);
357   }
358  
359 < # elif defined(__powerpc__) && defined(__linux__)
121 <
122 < static void Screen_fault_handler(int, struct sigcontext_struct *scs)
359 > static void vosf_set_dirty_area(int x, int y, int w, int h, int screen_width, int screen_height, int bytes_per_row)
360   {
361 <        D(bug("Screen_fault_handler: ADDR=0x%08X from IP=0x%08X\n", scs->regs->dar, scs->regs->nip));
362 <        do_handle_screen_fault((uintptr)scs->regs->dar, (uintptr)scs->regs->nip);
361 >        if (x < 0) {
362 >                w -= -x;
363 >                x = 0;
364 >        }
365 >        if (y < 0) {
366 >                h -= -y;
367 >                y = 0;
368 >        }
369 >        if (w <= 0 || h <= 0)
370 >                return;
371 >        if (x + w > screen_width)
372 >                w -= (x + w) - screen_width;
373 >        if (y + h > screen_height)
374 >                h -= (y + h) - screen_height;
375 >        LOCK_VOSF;
376 >        if (bytes_per_row >= screen_width) {
377 >                const int bytes_per_pixel = bytes_per_row / screen_width;
378 >                if (bytes_per_row <= mainBuffer.pageSize) {
379 >                        const uintptr a0 = mainBuffer.memStart + y * bytes_per_row + x * bytes_per_pixel;
380 >                        const uintptr a1 = mainBuffer.memStart + (y + h - 1) * bytes_per_row + (x + w - 1) * bytes_per_pixel;
381 >                        vosf_do_set_dirty_area(a0, a1);
382 >                } else {
383 >                        for (int j = y; j < y + h; j++) {
384 >                                const uintptr a0 = mainBuffer.memStart + j * bytes_per_row + x * bytes_per_pixel;
385 >                                const uintptr a1 = a0 + (w - 1) * bytes_per_pixel;
386 >                                vosf_do_set_dirty_area(a0, a1);
387 >                        }
388 >                }
389 >        } else {
390 >                const int pixels_per_byte = screen_width / bytes_per_row;
391 >                if (bytes_per_row <= mainBuffer.pageSize) {
392 >                        const uintptr a0 = mainBuffer.memStart + y * bytes_per_row + x / pixels_per_byte;
393 >                        const uintptr a1 = mainBuffer.memStart + (y + h - 1) * bytes_per_row + (x + w - 1) / pixels_per_byte;
394 >                        vosf_do_set_dirty_area(a0, a1);
395 >                } else {
396 >                        for (int j = y; j < y + h; j++) {
397 >                                const uintptr a0 = mainBuffer.memStart + j * bytes_per_row + x / pixels_per_byte;
398 >                                const uintptr a1 = mainBuffer.memStart + j * bytes_per_row + (x + w - 1) / pixels_per_byte;
399 >                                vosf_do_set_dirty_area(a0, a1);
400 >                        }
401 >                }
402 >        }
403 >        mainBuffer.dirty = true;
404 >        UNLOCK_VOSF;
405   }
406  
128 # else
129 #  error "No suitable subterfuge for Video on SEGV signals"
130 # endif
131 #else
132 # error "Can't do Video on SEGV signals"
133 #endif
134
407  
408   /*
409 < *      Screen fault handler initialization
409 > * Screen fault handler
410   */
411  
412 < #if defined(HAVE_SIGINFO_T)
141 < static bool Screen_fault_handler_init()
412 > bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
413   {
414 <        // Setup SIGSEGV handler to process writes to frame buffer
415 <        sigemptyset(&vosf_sa.sa_mask);
416 <        vosf_sa.sa_sigaction = Screen_fault_handler;
417 <        vosf_sa.sa_flags = SA_SIGINFO;
418 <        return (sigaction(SIGSEGV, &vosf_sa, NULL) == 0);
419 < }
420 < #elif defined(HAVE_SIGCONTEXT_SUBTERFUGE)
421 < static bool Screen_fault_handler_init()
422 < {
423 <        // Setup SIGSEGV handler to process writes to frame buffer
424 <        sigemptyset(&vosf_sa.sa_mask);
425 <        vosf_sa.sa_handler = (void (*)(int)) Screen_fault_handler;
426 < #if !EMULATED_68K && defined(__NetBSD__)
427 <        sigaddset(&vosf_sa.sa_mask, SIGALRM);
428 <        vosf_sa.sa_flags = SA_ONSTACK;
429 < #else
430 <        vosf_sa.sa_flags = 0;
431 < #endif
432 <        return (sigaction(SIGSEGV, &vosf_sa, NULL) == 0);
414 >        const uintptr addr = (uintptr)fault_address;
415 >        
416 >        /* Someone attempted to write to the frame buffer. Make it writeable
417 >         * now so that the data could actually be written to. It will be made
418 >         * read-only back in one of the screen update_*() functions.
419 >         */
420 >        if (((uintptr)addr - mainBuffer.memStart) < mainBuffer.memLength) {
421 >                const int page  = ((uintptr)addr - mainBuffer.memStart) >> mainBuffer.pageBits;
422 >                LOCK_VOSF;
423 >                if (PFLAG_ISCLEAR(page)) {
424 >                        PFLAG_SET(page);
425 >                        vm_protect((char *)(addr & -mainBuffer.pageSize), mainBuffer.pageSize, VM_PAGE_READ | VM_PAGE_WRITE);
426 >                }
427 >                mainBuffer.dirty = true;
428 >                UNLOCK_VOSF;
429 >                return true;
430 >        }
431 >        
432 >        /* Otherwise, we don't know how to handle the fault, let it crash */
433 >        return false;
434   }
163 #endif
435  
436  
437   /*
438   *      Update display for Windowed mode and VOSF
439   */
440  
170 // From video_blit.cpp
171 extern void (*Screen_blit)(uint8 * dest, const uint8 * source, uint32 length);
172 extern bool Screen_blitter_init(XVisualInfo * visual_info, bool native_byte_order);
173
441   /*      How can we deal with array overrun conditions ?
442          
443          The state of the framebuffer pages that have been touched are maintained
# Line 204 | Line 471 | There are two cases to check:
471          than pageCount.
472   */
473  
474 < static inline void update_display_window_vosf(void)
474 > #ifndef TEST_VOSF_PERFORMANCE
475 > static void update_display_window_vosf(VIDEO_DRV_WIN_INIT)
476   {
477 +        VIDEO_MODE_INIT;
478 +
479          int page = 0;
480          for (;;) {
481 <                const int first_page = find_next_page_set(page);
481 >                const unsigned first_page = find_next_page_set(page);
482                  if (first_page >= mainBuffer.pageCount)
483                          break;
484  
# Line 218 | Line 488 | static inline void update_display_window
488                  // Make the dirty pages read-only again
489                  const int32 offset  = first_page << mainBuffer.pageBits;
490                  const uint32 length = (page - first_page) << mainBuffer.pageBits;
491 <                mprotect((caddr_t)(mainBuffer.memStart + offset), length, PROT_READ);
491 >                vm_protect((char *)mainBuffer.memStart + offset, length, VM_PAGE_READ);
492                  
493                  // There is at least one line to update
494                  const int y1 = mainBuffer.pageInfo[first_page].top;
495                  const int y2 = mainBuffer.pageInfo[page - 1].bottom;
496                  const int height = y2 - y1 + 1;
227                
228                const int bytes_per_row = VideoMonitor.bytes_per_row;
229                const int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
230                int i = y1 * bytes_per_row, j;
231                
232                if (depth == 1) {
233
234                        // Update the_host_buffer and copy of the_buffer
235                        for (j = y1; j <= y2; j++) {
236                                Screen_blit(the_host_buffer + i, the_buffer + i, VideoMonitor.x >> 3);
237                                i += bytes_per_row;
238                        }
497  
498 <                } else {
499 <
500 <                        // Update the_host_buffer and copy of the_buffer
501 <                        for (j = y1; j <= y2; j++) {
502 <                                Screen_blit(the_host_buffer + i, the_buffer + i, bytes_per_pixel * VideoMonitor.x);
503 <                                i += bytes_per_row;
504 <                        }
498 >                // Update the_host_buffer
499 >                VIDEO_DRV_LOCK_PIXELS;
500 >                const int src_bytes_per_row = VIDEO_MODE_ROW_BYTES;
501 >                const int dst_bytes_per_row = VIDEO_DRV_ROW_BYTES;
502 >                int i1 = y1 * src_bytes_per_row, i2 = y1 * dst_bytes_per_row, j;
503 >                for (j = y1; j <= y2; j++) {
504 >                        Screen_blit(the_host_buffer + i2, the_buffer + i1, src_bytes_per_row);
505 >                        i1 += src_bytes_per_row;
506 >                        i2 += dst_bytes_per_row;
507                  }
508 +                VIDEO_DRV_UNLOCK_PIXELS;
509  
510 <                if (have_shm)
511 <                        XShmPutImage(x_display, the_win, the_gc, img, 0, y1, 0, y1, VideoMonitor.x, height, 0);
510 > #ifdef USE_SDL_VIDEO
511 >                SDL_UpdateRect(drv->s, 0, y1, VIDEO_MODE_X, height);
512 > #else
513 >                if (VIDEO_DRV_HAVE_SHM)
514 >                        XShmPutImage(x_display, VIDEO_DRV_WINDOW, VIDEO_DRV_GC, VIDEO_DRV_IMAGE, 0, y1, 0, y1, VIDEO_MODE_X, height, 0);
515                  else
516 <                        XPutImage(x_display, the_win, the_gc, img, 0, y1, 0, y1, VideoMonitor.x, height);
516 >                        XPutImage(x_display, VIDEO_DRV_WINDOW, VIDEO_DRV_GC, VIDEO_DRV_IMAGE, 0, y1, 0, y1, VIDEO_MODE_X, height);
517 > #endif
518          }
254
519          mainBuffer.dirty = false;
520   }
521 + #endif
522  
523  
524   /*
525   *      Update display for DGA mode and VOSF
526 < *      (only in Direct Addressing mode)
526 > *      (only in Real or Direct Addressing mode)
527   */
528  
529 + #ifndef TEST_VOSF_PERFORMANCE
530   #if REAL_ADDRESSING || DIRECT_ADDRESSING
531 < static inline void update_display_dga_vosf(void)
531 > static void update_display_dga_vosf(VIDEO_DRV_DGA_INIT)
532   {
533 <        int page = 0;
533 >        VIDEO_MODE_INIT;
534 >
535 >        // Compute number of bytes per row, take care to virtual screens
536 >        const int src_bytes_per_row = VIDEO_MODE_ROW_BYTES;
537 >        const int dst_bytes_per_row = TrivialBytesPerRow(VIDEO_MODE_X, DepthModeForPixelDepth(VIDEO_DRV_DEPTH));
538 >        const int scr_bytes_per_row = VIDEO_DRV_ROW_BYTES;
539 >        assert(dst_bytes_per_row <= scr_bytes_per_row);
540 >        const int scr_bytes_left = scr_bytes_per_row - dst_bytes_per_row;
541 >
542 >        // Full screen update requested?
543 >        if (mainBuffer.very_dirty) {
544 >                PFLAG_CLEAR_ALL;
545 >                vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ);
546 >                memcpy(the_buffer_copy, the_buffer, VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y);
547 >                VIDEO_DRV_LOCK_PIXELS;
548 >                int i1 = 0, i2 = 0;
549 >                for (int j = 0;  j < VIDEO_MODE_Y; j++) {
550 >                        Screen_blit(the_host_buffer + i2, the_buffer + i1, src_bytes_per_row);
551 >                        i1 += src_bytes_per_row;
552 >                        i2 += scr_bytes_per_row;
553 >                }
554 > #ifdef USE_SDL_VIDEO
555 >                SDL_UpdateRect(drv->s, 0, 0, VIDEO_MODE_X, VIDEO_MODE_Y);
556 > #endif
557 >                VIDEO_DRV_UNLOCK_PIXELS;
558 >                return;
559 >        }
560 >
561 >        // Setup partial blitter (use 64-pixel wide chunks)
562 >        const int n_pixels = 64;
563 >        const int n_chunks = VIDEO_MODE_X / n_pixels;
564 >        const int n_pixels_left = VIDEO_MODE_X - (n_chunks * n_pixels);
565 >        const int src_chunk_size = src_bytes_per_row / n_chunks;
566 >        const int dst_chunk_size = dst_bytes_per_row / n_chunks;
567 >        const int src_chunk_size_left = src_bytes_per_row - (n_chunks * src_chunk_size);
568 >        const int dst_chunk_size_left = dst_bytes_per_row - (n_chunks * dst_chunk_size);
569 >
570 >        int page = 0, last_scanline = -1;
571          for (;;) {
572 <                const int first_page = find_next_page_set(page);
572 >                const unsigned first_page = find_next_page_set(page);
573                  if (first_page >= mainBuffer.pageCount)
574                          break;
575  
# Line 276 | Line 579 | static inline void update_display_dga_vo
579                  // Make the dirty pages read-only again
580                  const int32 offset  = first_page << mainBuffer.pageBits;
581                  const uint32 length = (page - first_page) << mainBuffer.pageBits;
582 <                mprotect((caddr_t)(mainBuffer.memStart + offset), length, PROT_READ);
583 <                
584 <                // I am sure that y2 >= y1 and depth != 1
585 <                const int y1 = mainBuffer.pageInfo[first_page].top;
586 <                const int y2 = mainBuffer.pageInfo[page - 1].bottom;
587 <                
588 <                const int bytes_per_row = VideoMonitor.bytes_per_row;
589 <                const int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
590 <                int i, j;
591 <                
592 <                // Check for first column from left and first column
593 <                // from right that have changed
594 <                int x1 = VideoMonitor.x * bytes_per_pixel - 1;
595 <                for (j = y1; j <= y2; j++) {
596 <                        uint8 * const p1 = &the_buffer[j * bytes_per_row];
597 <                        uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
598 <                        for (i = 0; i < x1; i++) {
599 <                                if (p1[i] != p2[i]) {
600 <                                        x1 = i;
601 <                                        break;
582 >                vm_protect((char *)mainBuffer.memStart + offset, length, VM_PAGE_READ);
583 >
584 >                // Optimized for scanlines, don't process overlapping lines again
585 >                int y1 = mainBuffer.pageInfo[first_page].top;
586 >                int y2 = mainBuffer.pageInfo[page - 1].bottom;
587 >                if (y1 <= last_scanline && ++y1 >= VIDEO_MODE_Y)
588 >                        continue;
589 >                if (y2 <= last_scanline && ++y2 >= VIDEO_MODE_Y)
590 >                        continue;
591 >                last_scanline = y2;
592 >
593 >                // Update the_host_buffer and copy of the_buffer, one line at a time
594 >                int i1 = y1 * src_bytes_per_row;
595 >                int i2 = y1 * scr_bytes_per_row;
596 > #ifdef USE_SDL_VIDEO
597 >                int bbi = 0;
598 >                SDL_Rect bb[3] = {
599 >                        { VIDEO_MODE_X, y1, 0, 0 },
600 >                        { VIDEO_MODE_X, -1, 0, 0 },
601 >                        { VIDEO_MODE_X, -1, 0, 0 }
602 >                };
603 > #endif
604 >                VIDEO_DRV_LOCK_PIXELS;
605 >                for (int j = y1; j <= y2; j++) {
606 >                        for (int i = 0; i < n_chunks; i++) {
607 >                                if (memcmp(the_buffer_copy + i1, the_buffer + i1, src_chunk_size) != 0) {
608 >                                        memcpy(the_buffer_copy + i1, the_buffer + i1, src_chunk_size);
609 >                                        Screen_blit(the_host_buffer + i2, the_buffer + i1, src_chunk_size);
610 > #ifdef USE_SDL_VIDEO
611 >                                        const int x = i * n_pixels;
612 >                                        if (x < bb[bbi].x) {
613 >                                                if (bb[bbi].w)
614 >                                                        bb[bbi].w += bb[bbi].x - x;
615 >                                                else
616 >                                                        bb[bbi].w = n_pixels;
617 >                                                bb[bbi].x = x;
618 >                                        }
619 >                                        else if (x >= bb[bbi].x + bb[bbi].w)
620 >                                                bb[bbi].w = x + n_pixels - bb[bbi].x;
621 > #endif
622                                  }
623 +                                i1 += src_chunk_size;
624 +                                i2 += dst_chunk_size;
625                          }
626 <                }
627 <                x1 /= bytes_per_pixel;
628 <                
629 <                int x2 = x1 * bytes_per_pixel;
630 <                for (j = y2; j >= y1; j--) {
631 <                        uint8 * const p1 = &the_buffer[j * bytes_per_row];
632 <                        uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
633 <                        for (i = VideoMonitor.x * bytes_per_pixel - 1; i > x2; i--) {
634 <                                if (p1[i] != p2[i]) {
635 <                                        x2 = i;
636 <                                        break;
626 >                        if (src_chunk_size_left && dst_chunk_size_left) {
627 >                                if (memcmp(the_buffer_copy + i1, the_buffer + i1, src_chunk_size_left) != 0) {
628 >                                        memcpy(the_buffer_copy + i1, the_buffer + i1, src_chunk_size_left);
629 >                                        Screen_blit(the_host_buffer + i2, the_buffer + i1, src_chunk_size_left);
630 >                                }
631 >                                i1 += src_chunk_size_left;
632 >                                i2 += dst_chunk_size_left;
633 > #ifdef USE_SDL_VIDEO
634 >                                const int x = n_chunks * n_pixels;
635 >                                if (x < bb[bbi].x) {
636 >                                        if (bb[bbi].w)
637 >                                                bb[bbi].w += bb[bbi].x - x;
638 >                                        else
639 >                                                bb[bbi].w = n_pixels_left;
640 >                                        bb[bbi].x = x;
641                                  }
642 +                                else if (x >= bb[bbi].x + bb[bbi].w)
643 +                                        bb[bbi].w  = x + n_pixels_left - bb[bbi].x;
644 + #endif
645                          }
646 +                        i2 += scr_bytes_left;
647 + #ifdef USE_SDL_VIDEO
648 +                        bb[bbi].h++;
649 +                        if (bb[bbi].w && (j == y1 || j == y2 - 1 || j == y2)) {
650 +                                bbi++;
651 +                                assert(bbi <= 3);
652 +                                if (j != y2)
653 +                                        bb[bbi].y = j + 1;
654 +                        }
655 + #endif
656                  }
657 <                x2 /= bytes_per_pixel;
658 <                
659 <                // Update the_host_buffer and copy of the_buffer
660 <                // There should be at least one pixel to copy
319 <                const int width = x2 - x1 + 1;
320 <                i = y1 * bytes_per_row + x1 * bytes_per_pixel;
321 <                for (j = y1; j <= y2; j++) {
322 <                        Screen_blit(the_host_buffer + i, the_buffer + i, bytes_per_pixel * width);
323 <                        memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * width);
324 <                        i += bytes_per_row;
325 <                }
657 > #ifdef USE_SDL_VIDEO
658 >                SDL_UpdateRects(drv->s, bbi, bb);
659 > #endif
660 >                VIDEO_DRV_UNLOCK_PIXELS;
661          }
662          mainBuffer.dirty = false;
663   }
664   #endif
665 + #endif
666  
667   #endif /* ENABLE_VOSF */
668  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines