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.6 by cebix, 2000-10-11T17:55:06Z 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-2000 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 < */
30 <
31 < // Align on page boundaries
32 < static uintptr align_on_page_boundary(uintptr size)
33 < {
34 <        const uint32 page_size = getpagesize();
35 <        const uint32 page_mask = page_size - 1;
36 <        return (size + page_mask) & ~page_mask;
37 < }
38 <
39 < // Allocate memory on page boundary
40 < static void * allocate_framebuffer(uint32 size, uint8 * hint = 0)
41 < {
42 <        // Remind that the system can allocate at 0x00000000...
43 <        return mmap((caddr_t)hint, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0);
44 < }
45 <
27 > #include "sigsegv.h"
28 > #include "vm_alloc.h"
29 > #ifdef _WIN32
30 > #include "util_windows.h"
31 > #endif
32  
33 < /*
34 < *      Screen depth identification
35 < */
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_host_buffer_row_bytes; // Host frame buffer number of bytes per row
64  
65 < enum {
66 <        ID_DEPTH_UNKNOWN = -1,
53 <        ID_DEPTH_1,
54 <        ID_DEPTH_8,
55 <        ID_DEPTH_15,
56 <        ID_DEPTH_16,
57 <        ID_DEPTH_24,
58 <        ID_DEPTH_32 = ID_DEPTH_24,
59 <        ID_DEPTH_COUNT
65 > struct ScreenPageInfo {
66 >    int top, bottom;                    // Mapping between this virtual page and Mac scanlines
67   };
68  
69 < static int depth_id(int depth)
70 < {
71 <        int id;
72 <        switch (depth) {
73 <                case 1  : id = ID_DEPTH_1;      break;
74 <                case 8  : id = ID_DEPTH_8;      break;
75 <                case 15 : id = ID_DEPTH_15;     break;
76 <                case 16 : id = ID_DEPTH_16;     break;
77 <                case 24 : id = ID_DEPTH_24;     break;
78 <                case 32 : id = ID_DEPTH_32;     break;
79 <                default : id = ID_DEPTH_UNKNOWN;
80 <        }
81 <        return id;
75 < }
76 <
77 <
78 < /*
79 < *      Frame buffer copy function templates
80 < */
69 > struct ScreenInfo {
70 >    uintptr memStart;                   // Start address aligned to page boundary
71 >    uint32 memLength;                   // Length of the memory addressed by the screen pages
72 >    
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 > };
82  
83 < // No conversion required
83 > static ScreenInfo mainBuffer;
84  
85 < #define MEMCPY_PROFITABLE
86 < #ifdef MEMCPY_PROFITABLE
87 < static void do_fbcopy_raw(uint8 * dest, const uint8 * source, uint32 length)
88 < {
89 <        memcpy(dest, source, length);
90 < }
85 > #define PFLAG_SET_VALUE                 0x00
86 > #define PFLAG_CLEAR_VALUE               0x01
87 > #define PFLAG_SET_VALUE_4               0x00000000
88 > #define PFLAG_CLEAR_VALUE_4             0x01010101
89 > #define PFLAG_SET(page)                 mainBuffer.dirtyPages[page] = PFLAG_SET_VALUE
90 > #define PFLAG_CLEAR(page)               mainBuffer.dirtyPages[page] = PFLAG_CLEAR_VALUE
91 > #define PFLAG_ISSET(page)               (mainBuffer.dirtyPages[page] == PFLAG_SET_VALUE)
92 > #define PFLAG_ISCLEAR(page)             (mainBuffer.dirtyPages[page] != PFLAG_SET_VALUE)
93 >
94 > #ifdef UNALIGNED_PROFITABLE
95 > # define PFLAG_ISSET_4(page)    (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_SET_VALUE_4)
96 > # define PFLAG_ISCLEAR_4(page)  (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_CLEAR_VALUE_4)
97   #else
98 < #define FB_BLIT_1(dst, src)     (dst = (src))
99 < #define FB_BLIT_2(dst, src)     (dst = (src))
100 < #define FB_DEPTH                        0
101 < #define FB_FUNC_NAME            do_fbcopy_raw
102 < #include "video_blit.h"
103 < #endif
104 <
105 <
106 < // RGB 555
107 <
108 < #ifdef WORDS_BIGENDIAN
109 < # define FB_FUNC_NAME do_fbcopy_15_obo
98 > # define PFLAG_ISSET_4(page) \
99 >                PFLAG_ISSET(page  ) && PFLAG_ISSET(page+1) \
100 >        &&      PFLAG_ISSET(page+2) && PFLAG_ISSET(page+3)
101 > # define PFLAG_ISCLEAR_4(page) \
102 >                PFLAG_ISCLEAR(page  ) && PFLAG_ISCLEAR(page+1) \
103 >        &&      PFLAG_ISCLEAR(page+2) && PFLAG_ISCLEAR(page+3)
104 > #endif
105 >
106 > // Set the selected page range [ first_page, last_page [ into the SET state
107 > #define PFLAG_SET_RANGE(first_page, last_page) \
108 >        memset(mainBuffer.dirtyPages + (first_page), PFLAG_SET_VALUE, \
109 >                (last_page) - (first_page))
110 >
111 > // Set the selected page range [ first_page, last_page [ into the CLEAR state
112 > #define PFLAG_CLEAR_RANGE(first_page, last_page) \
113 >        memset(mainBuffer.dirtyPages + (first_page), PFLAG_CLEAR_VALUE, \
114 >                (last_page) - (first_page))
115 >
116 > #define PFLAG_SET_ALL do { \
117 >        PFLAG_SET_RANGE(0, mainBuffer.pageCount); \
118 >        mainBuffer.dirty = true; \
119 > } while (0)
120 >
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
132 > // provides a really fast strchr() implementation
133 > //#define HAVE_FAST_STRCHR 0
134 >
135 > static inline int find_next_page_set(int page)
136 > {
137 > #if HAVE_FAST_STRCHR
138 >        char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_SET_VALUE);
139 >        return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
140   #else
141 < # define FB_FUNC_NAME do_fbcopy_15_nbo
141 >        while (PFLAG_ISCLEAR_4(page))
142 >                page += 4;
143 >        while (PFLAG_ISCLEAR(page))
144 >                page++;
145 >        return page;
146   #endif
147 + }
148  
149 < #define FB_BLIT_1(dst, src) \
150 <        (dst = (((src) >> 8) & 0xff) | (((src) & 0xff) << 8))
151 <        
152 < #define FB_BLIT_2(dst, src) \
153 <        (dst = (((src) >> 8) & 0x00ff00ff) | (((src) & 0x00ff00ff) << 8))
112 <
113 < #define FB_DEPTH 15
114 < #include "video_blit.h"
115 <
116 <
117 < // RGB 565
118 <
119 < #ifdef WORDS_BIGENDIAN
120 <
121 < // native byte order
122 <
123 < #define FB_BLIT_1(dst, src) \
124 <        (dst = (((src) & 0x1f) | (((src) << 1) & 0xffc0)))
125 <
126 < #define FB_BLIT_2(dst, src) \
127 <        (dst = (((src) & 0x001f001f) | (((src) << 1) & 0xffc0ffc0)))
128 <
129 < #define FB_DEPTH 16
130 < #define FB_FUNC_NAME do_fbcopy_16_nbo
131 < #include "video_blit.h"
132 <
133 < // opposite byte order (untested)
134 <
135 < #define FB_BLIT_1(dst, src) \
136 <        (dst = ((((src) >> 6) & 0xff) | (((src) & 0x60) << 9)))
137 <
138 < #define FB_BLIT_2(dst, src) \
139 <        (dst = ((((src) >> 6) & 0x00ff00ff) | (((src) & 0x00600060) << 9)))
140 <
141 < #define FB_DEPTH 16
142 < #define FB_FUNC_NAME do_fbcopy_16_obo
143 < #include "video_blit.h"
144 <
149 > static inline int find_next_page_clear(int page)
150 > {
151 > #if HAVE_FAST_STRCHR
152 >        char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_CLEAR_VALUE);
153 >        return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
154   #else
155 <
156 < // native byte order
157 <
158 < #define FB_BLIT_1(dst, src) \
159 <        (dst = (((src) >> 8) & 0x001f) | (((src) << 9) & 0xfe00) | (((src) >> 7) & 0x01c0))
151 <        
152 < #define FB_BLIT_2(dst, src) \
153 <        (dst = (((src) >> 8) & 0x001f001f) | (((src) << 9) & 0xfe00fe00) | (((src) >> 7) & 0x01c001c0))
154 <
155 < #define FB_DEPTH 16
156 < #define FB_FUNC_NAME do_fbcopy_16_nbo
157 < #include "video_blit.h"
158 <
159 < // opposite byte order (untested)
160 <
161 < #define FB_BLIT_1(dst, src) \
162 <        (dst = (((src) & 0x1f00) | (((src) << 1) & 0xe0fe) | (((src) >> 15) & 1)))
163 <
164 < #define FB_BLIT_2(dst, src) \
165 <        (dst = (((src) & 0x1f001f00) | (((src) << 1) & 0xe0fee0fe) | (((src) >> 15) & 0x10001)))
166 <
167 < #define FB_DEPTH 16
168 < #define FB_FUNC_NAME do_fbcopy_16_obo
169 < #include "video_blit.h"
170 <
155 >        while (PFLAG_ISSET_4(page))
156 >                page += 4;
157 >        while (PFLAG_ISSET(page))
158 >                page++;
159 >        return page;
160   #endif
161 + }
162  
163 < // RGB 888
164 <
165 < #ifdef WORDS_BIGENDIAN
166 < # define FB_FUNC_NAME do_fbcopy_24_obo
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);
175   #else
176 < # define FB_FUNC_NAME do_fbcopy_24_nbo
176 > #define LOCK_VOSF
177 > #define UNLOCK_VOSF
178   #endif
179  
180 < #define FB_BLIT_1(dst, src) \
181 <        (dst = (src))
182 <
183 < #define FB_BLIT_2(dst, src) \
184 <        (dst = (((src) >> 24) & 0xff) | (((src) >> 8) & 0xff00) | (((src) & 0xff00) << 8) | (((src) & 0xff) << 24))
180 > static int log_base_2(uint32 x)
181 > {
182 >        uint32 mask = 0x80000000;
183 >        int l = 31;
184 >        while (l >= 0 && (x & mask) == 0) {
185 >                mask >>= 1;
186 >                l--;
187 >        }
188 >        return l;
189 > }
190  
191 < #define FB_DEPTH 24
192 < #include "video_blit.h"
191 > // Extend size to page boundary
192 > static uint32 page_extend(uint32 size)
193 > {
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 < *      Frame buffer copy functions map table
201 > *  Check if VOSF acceleration is profitable on this platform
202   */
203  
204 < typedef void (*fbcopy_func)(uint8 *, const uint8 *, uint32);
205 < static fbcopy_func do_update_framebuffer;
197 <
198 < #define FBCOPY_FUNC(aHandler) do_ ## aHandler
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 < #if REAL_ADDRESSING || DIRECT_ADDRESSING
208 < #define WD(X) { FBCOPY_FUNC(X), FBCOPY_FUNC(X) }
209 < #else
210 < #define WD(X) { FBCOPY_FUNC(fbcopy_raw), FBCOPY_FUNC(fbcopy_raw) }
204 < #endif
205 <
206 < // fb_copy_funcs[depth_id][native_byte_order][dga_mode]
207 < // NT  : not tested
208 < // OK  : has been successfully tested
209 < // NBO : native byte order
210 < // OBO : opposite byte order
211 < static fbcopy_func fbcopy_funcs[ID_DEPTH_COUNT][2][2] = {
212 < #ifdef WORDS_BIGENDIAN
213 <                                /*      opposite byte order             native byte order       */
214 < /*  1 bpp */    {       WD(fbcopy_raw)          ,       WD(fbcopy_raw)          },      // NT
215 < /*  8 bpp */    {       WD(fbcopy_raw)          ,       WD(fbcopy_raw)          },      // OK (NBO)
216 < /* 15 bpp */    {       WD(fbcopy_15_obo)       ,       WD(fbcopy_raw)          },      // NT
217 < /* 16 bpp */    {       WD(fbcopy_16_obo)       ,       WD(fbcopy_16_nbo)       },      // NT
218 < /* 24 bpp */    {       WD(fbcopy_24_obo)       ,       WD(fbcopy_raw)          }       // NT
219 < #else
220 <                                /*      opposite byte order             native byte order       */
221 < /*  1 bpp */    {       WD(fbcopy_raw)          ,       WD(fbcopy_raw)          },      // NT
222 < /*  8 bpp */    {       WD(fbcopy_raw)          ,       WD(fbcopy_raw)          },      // OK (NBO)
223 < /* 15 bpp */    {       WD(fbcopy_raw)          ,       WD(fbcopy_15_nbo)       },      // OK (NBO)
224 < /* 16 bpp */    {       WD(fbcopy_16_obo)       ,       WD(fbcopy_16_nbo)       },      // OK (NBO)
225 < /* 24 bpp */    {       WD(fbcopy_raw)          ,       WD(fbcopy_24_nbo)       }       // NT
226 < #endif
227 < };
228 <
229 < #undef WD
207 > static bool video_vosf_profitable(void)
208 > {
209 >        int64 durations[VOSF_PROFITABLE_TRIES];
210 >        int mean_duration = 0;
211  
212 < #define FBCOPY_FUNC_ERROR \
213 <        ErrorAlert("Invalid screen depth")
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 >                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 < #define GET_FBCOPY_FUNC(aDepth, aNativeByteOrder, aDisplay) \
229 <        ((depth_id(aDepth) == ID_DEPTH_UNKNOWN) ? ( FBCOPY_FUNC_ERROR, (fbcopy_func)0 ) : \
230 <        fbcopy_funcs[depth_id(aDepth)][(aNativeByteOrder)][(aDisplay) == DISPLAY_DGA ? 1 : 0])
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  
233  
234   /*
235 < *      Screen fault handler
235 > *  Initialize the VOSF system (mainBuffer structure, SIGSEGV handler)
236   */
237  
238 < static inline void do_handle_screen_fault(uintptr addr)
238 > static bool video_vosf_init(MONITOR_INIT)
239   {
240 <        if ((addr < mainBuffer.memStart) || (addr >= mainBuffer.memEnd)) {
241 <                fprintf(stderr, "Segmentation fault at 0x%08X\n", addr);
242 <                abort();
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 >        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 <        const int page  = (addr - mainBuffer.memStart) >> mainBuffer.pageBits;
290 <        caddr_t page_ad = (caddr_t)(addr & ~(mainBuffer.pageSize - 1));
291 < #ifdef HAVE_PTHREADS
292 <        pthread_mutex_lock(&Screen_draw_lock);
293 < #endif
294 <        PFLAG_SET(page);
295 <        mprotect(page_ad, mainBuffer.pageSize, PROT_READ | PROT_WRITE);
257 < #ifdef HAVE_PTHREADS
258 <        pthread_mutex_unlock(&Screen_draw_lock);
259 < #endif
260 < }
261 <
262 < #if defined(HAVE_SIGINFO_T)
263 <
264 < static void Screen_fault_handler(int, siginfo_t * sip, void *)
265 < {
266 <        D(bug("Screen_fault_handler: ADDR=0x%08X\n", sip->si_addr));
267 <        do_handle_screen_fault((uintptr)sip->si_addr);
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  
270 #elif defined(HAVE_SIGCONTEXT_SUBTERFUGE)
271
272 # if defined(__i386__) && defined(__linux__)
273 static void Screen_fault_handler(int, struct sigcontext scs)
274 {
275        D(bug("Screen_fault_handler: ADDR=0x%08X from IP=0x%08X\n", scs.cr2, scs.eip));
276        do_handle_screen_fault((uintptr)scs.cr2);
277 }
298  
299 < # elif defined(__m68k__) && defined(__NetBSD__)
299 > /*
300 > * Deinitialize VOSF system
301 > */
302  
303 < # include <m68k/frame.h>
282 < static void Screen_fault_handler(int, int code, struct sigcontext *scp)
303 > static void video_vosf_exit(void)
304   {
305 <        D(bug("Screen_fault_handler: ADDR=0x%08X\n", code));
306 <        struct sigstate {
307 <                int ss_flags;
308 <                struct frame ss_frame;
309 <        };
310 <        struct sigstate *state = (struct sigstate *)scp->sc_ap;
311 <        uintptr fault_addr;
291 <        switch (state->ss_frame.f_format) {
292 <                case 7:         // 68040 access error
293 <                        // "code" is sometimes unreliable (i.e. contains NULL or a bogus address), reason unknown
294 <                        fault_addr = state->ss_frame.f_fmt7.f_fa;
295 <                        break;
296 <                default:
297 <                        fault_addr = (uintptr)code;
298 <                        break;
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          }
300        do_handle_screen_fault(fault_addr);
313   }
314  
303 # else
304 #  error "No suitable subterfuge for Video on SEGV signals"
305 # endif
306 #else
307 # error "Can't do Video on SEGV signals"
308 #endif
309
315  
316   /*
317 < *      Screen fault handler initialization
317 > * Screen fault handler
318   */
319  
320 < #if defined(HAVE_SIGINFO_T)
316 < static bool Screen_fault_handler_init()
317 < {
318 <        // Setup SIGSEGV handler to process writes to frame buffer
319 <        sigemptyset(&vosf_sa.sa_mask);
320 <        vosf_sa.sa_sigaction = Screen_fault_handler;
321 <        vosf_sa.sa_flags = SA_SIGINFO;
322 <        return (sigaction(SIGSEGV, &vosf_sa, NULL) == 0);
323 < }
324 < #elif defined(HAVE_SIGCONTEXT_SUBTERFUGE)
325 < static bool Screen_fault_handler_init()
320 > bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
321   {
322 <        // Setup SIGSEGV handler to process writes to frame buffer
323 <        sigemptyset(&vosf_sa.sa_mask);
324 <        vosf_sa.sa_handler = (void (*)(int)) Screen_fault_handler;
325 <        vosf_sa.sa_flags = 0;
326 <        return (sigaction(SIGSEGV, &vosf_sa, NULL) == 0);
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 (((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 *)(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 */
339 >        return false;
340   }
333 #endif
341  
342  
343   /*
344   *      Update display for Windowed mode and VOSF
345   */
346  
347 < static inline void update_display_window_vosf(void)
347 > /*      How can we deal with array overrun conditions ?
348 >        
349 >        The state of the framebuffer pages that have been touched are maintained
350 >        in the dirtyPages[] table. That table is (pageCount + 2) bytes long.
351 >
352 > Terminology
353 >        
354 >        "Last Page" denotes the pageCount-nth page, i.e. dirtyPages[pageCount - 1].
355 >        "CLEAR Page Guard" refers to the page following the Last Page but is always
356 >        in the CLEAR state. "SET Page Guard" refers to the page following the CLEAR
357 >        Page Guard but is always in the SET state.
358 >
359 > Rough process
360 >        
361 >        The update routines must determine which pages have to be blitted to the
362 >        screen. This job consists in finding the first_page that was touched.
363 >        i.e. find the next page that is SET. Then, finding how many pages were
364 >        touched starting from first_page. i.e. find the next page that is CLEAR.
365 >
366 > There are two cases to check:
367 >
368 >        - Last Page is CLEAR: find_next_page_set() will reach the SET Page Guard
369 >        but it is beyond the valid pageCount value. Therefore, we exit from the
370 >        update routine.
371 >        
372 >        - Last Page is SET: first_page equals (pageCount - 1) and
373 >        find_next_page_clear() will reach the CLEAR Page Guard. We blit the last
374 >        page to the screen. On the next iteration, page equals pageCount and
375 >        find_next_page_set() will reach the SET Page Guard. We still safely exit
376 >        from the update routine because the SET Page Guard position is greater
377 >        than pageCount.
378 > */
379 >
380 > static inline void update_display_window_vosf(VIDEO_DRV_INIT)
381   {
382 +        VIDEO_MODE_INIT;
383 +
384          int page = 0;
385          for (;;) {
386 <                while (PFLAG_ISCLEAR_4(page))
387 <                        page += 4;
346 <                
347 <                while (PFLAG_ISCLEAR(page))
348 <                        page++;
349 <                
350 <                if (page >= mainBuffer.pageCount)
386 >                const unsigned first_page = find_next_page_set(page);
387 >                if (first_page >= mainBuffer.pageCount)
388                          break;
389 <                
390 <                const int first_page = page;
391 <                while ((page < mainBuffer.pageCount) && PFLAG_ISSET(page)) {
392 <                        PFLAG_CLEAR(page);
356 <                        ++page;
357 <                }
358 <                
389 >
390 >                page = find_next_page_clear(first_page);
391 >                PFLAG_CLEAR_RANGE(first_page, page);
392 >
393                  // Make the dirty pages read-only again
394                  const int32 offset  = first_page << mainBuffer.pageBits;
395                  const uint32 length = (page - first_page) << mainBuffer.pageBits;
396 <                mprotect((caddr_t)(mainBuffer.memStart + offset), length, PROT_READ);
396 >                vm_protect((char *)mainBuffer.memStart + offset, length, VM_PAGE_READ);
397                  
398                  // There is at least one line to update
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;
368                
369                const int bytes_per_row = VideoMonitor.bytes_per_row;
370                const int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
371                int i, j;
372                
373                // Check for first column from left and first column
374                // from right that have changed
375                int x1, x2, width;
376                if (depth == 1) {
377
378                        x1 = VideoMonitor.x - 1;
379                        for (j = y1; j <= y2; j++) {
380                                uint8 * const p1 = &the_buffer[j * bytes_per_row];
381                                uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
382                                for (i = 0; i < (x1>>3); i++) {
383                                        if (p1[i] != p2[i]) {
384                                                x1 = i << 3;
385                                                break;
386                                        }
387                                }
388                        }
402  
403 <                        x2 = x1;
404 <                        for (j = y2; j >= y1; j--) {
405 <                                uint8 * const p1 = &the_buffer[j * bytes_per_row];
406 <                                uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
407 <                                for (i = (VideoMonitor.x>>3) - 1; i > (x2>>3); i--) {
408 <                                        if (p1[i] != p2[i]) {
409 <                                                x2 = i << 3;
410 <                                                break;
411 <                                        }
399 <                                }
400 <                        }
401 <                        width = x2 - x1 + 1;
402 <
403 <                        // Update the_host_buffer and copy of the_buffer
404 <                        i = y1 * bytes_per_row + (x1 >> 3);
405 <                        for (j = y1; j <= y2; j++) {
406 <                                do_update_framebuffer(the_host_buffer + i, the_buffer + i, width >> 3);
407 <                                memcpy(the_buffer_copy + i, the_buffer + i, width >> 3);
408 <                                i += bytes_per_row;
409 <                        }
410 <
411 <                } else {
412 <
413 <                        x1 = VideoMonitor.x * bytes_per_pixel - 1;
414 <                        for (j = y1; j <= y2; j++) {
415 <                                uint8 * const p1 = &the_buffer[j * bytes_per_row];
416 <                                uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
417 <                                for (i = 0; i < x1; i++) {
418 <                                        if (p1[i] != p2[i]) {
419 <                                                x1 = i;
420 <                                                break;
421 <                                        }
422 <                                }
423 <                        }
424 <                        x1 /= bytes_per_pixel;
425 <                
426 <                        x2 = x1 * bytes_per_pixel;
427 <                        for (j = y2; j >= y1; j--) {
428 <                                uint8 * const p1 = &the_buffer[j * bytes_per_row];
429 <                                uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
430 <                                for (i = VideoMonitor.x * bytes_per_pixel - 1; i > x2; i--) {
431 <                                        if (p1[i] != p2[i]) {
432 <                                                x2 = i;
433 <                                                break;
434 <                                        }
435 <                                }
436 <                        }
437 <                        x2 /= bytes_per_pixel;
438 <                        width = x2 - x1 + 1;
439 <
440 <                        // Update the_host_buffer and copy of the_buffer
441 <                        i = y1 * bytes_per_row + x1 * bytes_per_pixel;
442 <                        for (j = y1; j <= y2; j++) {
443 <                                do_update_framebuffer(the_host_buffer + i, the_buffer + i, bytes_per_pixel * width);
444 <                                memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * width);
445 <                                i += bytes_per_row;
446 <                        }
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 <                
414 <                if (have_shm)
415 <                        XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, width, height, 0);
413 >                VIDEO_DRV_UNLOCK_PIXELS;
414 >
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, the_win, the_gc, img, x1, y1, x1, y1, width, 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   }
426  
427  
428   /*
429   *      Update display for DGA mode and VOSF
430 < *      (only in Direct Addressing mode)
430 > *      (only in Real or Direct Addressing mode)
431   */
432  
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 <                while (PFLAG_ISCLEAR_4(page))
451 <                        page += 4;
469 <                
470 <                while (PFLAG_ISCLEAR(page))
471 <                        page++;
472 <                
473 <                if (page >= mainBuffer.pageCount)
450 >                const unsigned first_page = find_next_page_set(page);
451 >                if (first_page >= mainBuffer.pageCount)
452                          break;
453 <                
454 <                const int first_page = page;
455 <                while ((page < mainBuffer.pageCount) && PFLAG_ISSET(page)) {
456 <                        PFLAG_CLEAR(page);
479 <                        ++page;
480 <                }
481 <                
453 >
454 >                page = find_next_page_clear(first_page);
455 >                PFLAG_CLEAR_RANGE(first_page, page);
456 >
457                  // Make the dirty pages read-only again
458                  const int32 offset  = first_page << mainBuffer.pageBits;
459                  const uint32 length = (page - first_page) << mainBuffer.pageBits;
460 <                mprotect((caddr_t)(mainBuffer.memStart + offset), length, PROT_READ);
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.bytes_per_row;
467 <                const int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.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
496 <                // from right that have changed
497 <                int x1 = VideoMonitor.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 <                
510 <                int x2 = x1 * bytes_per_pixel;
511 <                for (j = y2; j >= y1; j--) {
512 <                        uint8 * const p1 = &the_buffer[j * bytes_per_row];
513 <                        uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
514 <                        for (i = VideoMonitor.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
525 <                const int width = x2 - x1 + 1;
526 <                i = y1 * bytes_per_row + x1 * bytes_per_pixel;
556 >                VIDEO_DRV_LOCK_PIXELS;
557                  for (j = y1; j <= y2; j++) {
558 <                        do_update_framebuffer(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   }
568   #endif
569  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines