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.26 by cebix, 2001-07-06T20:49:48Z vs.
Revision 1.39 by gbeauche, 2004-06-24T15:19:56Z

# 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-2004 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>
# Line 29 | Line 29
29   #include "sigsegv.h"
30   #include "vm_alloc.h"
31  
32 < #ifdef ENABLE_MON
33 < # include "mon.h"
32 > // Glue for SDL and X11 support
33 > #ifdef USE_SDL_VIDEO
34 > #define MONITOR_INIT                    SDL_monitor_desc &monitor
35 > #define VIDEO_DRV_INIT                  driver_window *drv
36 > #define VIDEO_DRV_ROW_BYTES             drv->s->pitch
37 > #define VIDEO_DRV_LOCK_PIXELS   if (SDL_MUSTLOCK(drv->s)) SDL_LockSurface(drv->s)
38 > #define VIDEO_DRV_UNLOCK_PIXELS if (SDL_MUSTLOCK(drv->s)) SDL_UnlockSurface(drv->s)
39 > #else
40 > #ifdef SHEEPSHAVER
41 > #define MONITOR_INIT                    /* nothing */
42 > #define VIDEO_DRV_INIT                  /* nothing */
43 > #define VIDEO_DRV_WINDOW                the_win
44 > #define VIDEO_DRV_GC                    the_gc
45 > #define VIDEO_DRV_IMAGE                 img
46 > #define VIDEO_DRV_HAVE_SHM              have_shm
47 > #else
48 > #define MONITOR_INIT                    X11_monitor_desc &monitor
49 > #define VIDEO_DRV_INIT                  driver_window *drv
50 > #define VIDEO_DRV_WINDOW                drv->w
51 > #define VIDEO_DRV_GC                    drv->gc
52 > #define VIDEO_DRV_IMAGE                 drv->img
53 > #define VIDEO_DRV_HAVE_SHM              drv->have_shm
54 > #endif
55 > #define VIDEO_DRV_LOCK_PIXELS   /* nothing */
56 > #define VIDEO_DRV_UNLOCK_PIXELS /* nothing */
57 > #define VIDEO_DRV_ROW_BYTES             VIDEO_DRV_IMAGE->bytes_per_line
58   #endif
59  
60   // Variables for Video on SEGV support
61   static uint8 *the_host_buffer;  // Host frame buffer in VOSF mode
38 static uint32 the_buffer_size;  // Size of allocated the_buffer
62  
63   struct ScreenPageInfo {
64      int top, bottom;                    // Mapping between this virtual page and Mac scanlines
65   };
66  
67   struct ScreenInfo {
45    uintptr memBase;                    // Real start address
68      uintptr memStart;                   // Start address aligned to page boundary
47    uintptr memEnd;                             // Address of one-past-the-end of the screen
69      uint32 memLength;                   // Length of the memory addressed by the screen pages
70      
71 <    uint32 pageSize;                    // Size of a page
71 >    uintptr pageSize;                   // Size of a page
72      int pageBits;                               // Shift count to get the page number
73      uint32 pageCount;                   // Number of pages allocated to the screen
74      
# Line 131 | Line 152 | static inline int find_next_page_clear(i
152   #endif
153   }
154  
155 < #ifdef HAVE_PTHREADS
155 > #ifdef HAVE_SPINLOCKS
156 > static spinlock_t vosf_lock = SPIN_LOCK_UNLOCKED;                               // Mutex to protect frame buffer (dirtyPages in fact)
157 > #define LOCK_VOSF spin_lock(&vosf_lock)
158 > #define UNLOCK_VOSF spin_unlock(&vosf_lock)
159 > #elif defined(HAVE_PTHREADS)
160   static pthread_mutex_t vosf_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer (dirtyPages in fact)
161   #define LOCK_VOSF pthread_mutex_lock(&vosf_lock);
162   #define UNLOCK_VOSF pthread_mutex_unlock(&vosf_lock);
# Line 161 | Line 186 | static uint32 page_extend(uint32 size)
186  
187  
188   /*
189 < *  Initialize mainBuffer structure
189 > *  Initialize the VOSF system (mainBuffer structure, SIGSEGV handler)
190   */
191  
192 < static bool video_init_buffer(void)
192 > static bool video_vosf_init(MONITOR_INIT)
193   {
194 <        if (use_vosf) {
170 <                const uint32 page_size  = getpagesize();
171 <                const uint32 page_mask  = page_size - 1;
172 <                
173 <                mainBuffer.memBase      = (uintptr) the_buffer;
174 <                // Round up frame buffer base to page boundary
175 <                mainBuffer.memStart             = (uintptr)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
176 <                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;
186 <                }
194 >        VIDEO_MODE_INIT;
195  
196 <                mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2);
197 <
198 <                if (mainBuffer.pageInfo) {
199 <                        free(mainBuffer.pageInfo);
200 <                        mainBuffer.pageInfo = NULL;
201 <                }
196 >        const uintptr page_size = getpagesize();
197 >        const uintptr page_mask = page_size - 1;
198 >        
199 >        // Round up frame buffer base to page boundary
200 >        mainBuffer.memStart = (((uintptr) the_buffer) + page_mask) & ~page_mask;
201 >        
202 >        // The frame buffer size shall already be aligned to page boundary (use page_extend)
203 >        mainBuffer.memLength = the_buffer_size;
204 >        
205 >        mainBuffer.pageSize = page_size;
206 >        mainBuffer.pageBits = log_base_2(mainBuffer.pageSize);
207 >        mainBuffer.pageCount =  (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
208 >        
209 >        // The "2" more bytes requested are a safety net to insure the
210 >        // loops in the update routines will terminate.
211 >        // See "How can we deal with array overrun conditions ?" hereunder for further details.
212 >        mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2);
213 >        if (mainBuffer.dirtyPages == NULL)
214 >                return false;
215 >                
216 >        PFLAG_CLEAR_ALL;
217 >        PFLAG_CLEAR(mainBuffer.pageCount);
218 >        PFLAG_SET(mainBuffer.pageCount+1);
219 >        
220 >        // Allocate and fill in pageInfo with start and end (inclusive) row in number of bytes
221 >        mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
222 >        if (mainBuffer.pageInfo == NULL)
223 >                return false;
224 >        
225 >        uint32 a = 0;
226 >        for (unsigned i = 0; i < mainBuffer.pageCount; i++) {
227 >                unsigned y1 = a / VIDEO_MODE_ROW_BYTES;
228 >                if (y1 >= VIDEO_MODE_Y)
229 >                        y1 = VIDEO_MODE_Y - 1;
230 >
231 >                unsigned y2 = (a + mainBuffer.pageSize) / VIDEO_MODE_ROW_BYTES;
232 >                if (y2 >= VIDEO_MODE_Y)
233 >                        y2 = VIDEO_MODE_Y - 1;
234 >
235 >                mainBuffer.pageInfo[i].top = y1;
236 >                mainBuffer.pageInfo[i].bottom = y2;
237 >
238 >                a += mainBuffer.pageSize;
239 >                if (a > mainBuffer.memLength)
240 >                        a = mainBuffer.memLength;
241 >        }
242 >        
243 >        // We can now write-protect the frame buffer
244 >        if (vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ) != 0)
245 >                return false;
246 >        
247 >        // The frame buffer is sane, i.e. there is no write to it yet
248 >        mainBuffer.dirty = false;
249 >        return true;
250 > }
251  
195                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
252  
253 <                if ((mainBuffer.dirtyPages == NULL) || (mainBuffer.pageInfo == NULL))
254 <                        return false;
255 <                
200 <                mainBuffer.dirty = false;
253 > /*
254 > * Deinitialize VOSF system
255 > */
256  
257 <                PFLAG_CLEAR_ALL;
258 <                // Safety net to insure the loops in the update routines will terminate
259 <                // See "How can we deal with array overrun conditions ?" hereunder for further details
260 <                PFLAG_CLEAR(mainBuffer.pageCount);
261 <                PFLAG_SET(mainBuffer.pageCount+1);
262 <
263 <                uint32 a = 0;
264 <                for (int i = 0; i < mainBuffer.pageCount; i++) {
265 <                        int y1 = a / VideoMonitor.mode.bytes_per_row;
211 <                        if (y1 >= VideoMonitor.mode.y)
212 <                                y1 = VideoMonitor.mode.y - 1;
213 <
214 <                        int y2 = (a + mainBuffer.pageSize) / VideoMonitor.mode.bytes_per_row;
215 <                        if (y2 >= VideoMonitor.mode.y)
216 <                                y2 = VideoMonitor.mode.y - 1;
217 <
218 <                        mainBuffer.pageInfo[i].top = y1;
219 <                        mainBuffer.pageInfo[i].bottom = y2;
220 <
221 <                        a += mainBuffer.pageSize;
222 <                        if (a > mainBuffer.memLength)
223 <                                a = mainBuffer.memLength;
224 <                }
225 <                
226 <                // We can now write-protect the frame buffer
227 <                if (vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ) != 0)
228 <                        return false;
257 > static void video_vosf_exit(void)
258 > {
259 >        if (mainBuffer.pageInfo) {
260 >                free(mainBuffer.pageInfo);
261 >                mainBuffer.pageInfo = NULL;
262 >        }
263 >        if (mainBuffer.dirtyPages) {
264 >                free(mainBuffer.dirtyPages);
265 >                mainBuffer.dirtyPages = NULL;
266          }
230        return true;
267   }
268  
269  
# Line 235 | Line 271 | static bool video_init_buffer(void)
271   * Screen fault handler
272   */
273  
274 < static bool screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
274 > bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
275   {
240 //      D(bug("screen_fault_handler: ADDR=0x%08X from IP=0x%08X\n", fault_address, fault_instruction));
276          const uintptr addr = (uintptr)fault_address;
277          
278          /* Someone attempted to write to the frame buffer. Make it writeable
279           * now so that the data could actually be written to. It will be made
280           * read-only back in one of the screen update_*() functions.
281           */
282 <        if ((addr >= mainBuffer.memStart) && (addr < mainBuffer.memEnd)) {
283 <                const int page  = (addr - mainBuffer.memStart) >> mainBuffer.pageBits;
249 <                caddr_t page_ad = (caddr_t)(addr & -mainBuffer.pageSize);
282 >        if (((uintptr)addr - mainBuffer.memStart) < mainBuffer.memLength) {
283 >                const int page  = ((uintptr)addr - mainBuffer.memStart) >> mainBuffer.pageBits;
284                  LOCK_VOSF;
285                  PFLAG_SET(page);
286 <                vm_protect((char *)page_ad, mainBuffer.pageSize, VM_PAGE_READ | VM_PAGE_WRITE);
286 >                vm_protect((char *)(addr & -mainBuffer.pageSize), mainBuffer.pageSize, VM_PAGE_READ | VM_PAGE_WRITE);
287                  mainBuffer.dirty = true;
288                  UNLOCK_VOSF;
289                  return true;
290          }
291          
292          /* 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
293          return false;
294   }
295  
# Line 279 | Line 298 | static bool screen_fault_handler(sigsegv
298   *      Update display for Windowed mode and VOSF
299   */
300  
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
301   /*      How can we deal with array overrun conditions ?
302          
303          The state of the framebuffer pages that have been touched are maintained
# Line 317 | Line 331 | There are two cases to check:
331          than pageCount.
332   */
333  
334 < static inline void update_display_window_vosf(driver_window *drv)
334 > static inline void update_display_window_vosf(VIDEO_DRV_INIT)
335   {
336 +        VIDEO_MODE_INIT;
337 +
338          int page = 0;
339          for (;;) {
340 <                const int first_page = find_next_page_set(page);
340 >                const unsigned first_page = find_next_page_set(page);
341                  if (first_page >= mainBuffer.pageCount)
342                          break;
343  
# Line 337 | Line 353 | static inline void update_display_window
353                  const int y1 = mainBuffer.pageInfo[first_page].top;
354                  const int y2 = mainBuffer.pageInfo[page - 1].bottom;
355                  const int height = y2 - y1 + 1;
356 <                
357 <                if (VideoMonitor.mode.depth < VDEPTH_8BIT) {
356 >
357 >                VIDEO_DRV_LOCK_PIXELS;
358 >
359 >                if (VIDEO_MODE_DEPTH < VIDEO_DEPTH_8BIT) {
360  
361                          // Update the_host_buffer and copy of the_buffer
362 <                        const int src_bytes_per_row = VideoMonitor.mode.bytes_per_row;
363 <                        const int dst_bytes_per_row = drv->img->bytes_per_line;
364 <                        const int pixels_per_byte = VideoMonitor.mode.x / src_bytes_per_row;
362 >                        const int src_bytes_per_row = VIDEO_MODE_ROW_BYTES;
363 >                        const int dst_bytes_per_row = VIDEO_DRV_ROW_BYTES;
364 >                        const int pixels_per_byte = VIDEO_MODE_X / src_bytes_per_row;
365                          int i1 = y1 * src_bytes_per_row, i2 = y1 * dst_bytes_per_row, j;
366                          for (j = y1; j <= y2; j++) {
367 <                                Screen_blit(the_host_buffer + i2, the_buffer + i1, VideoMonitor.mode.x / pixels_per_byte);
367 >                                Screen_blit(the_host_buffer + i2, the_buffer + i1, VIDEO_MODE_X / pixels_per_byte);
368                                  i1 += src_bytes_per_row;
369                                  i2 += dst_bytes_per_row;
370                          }
# Line 354 | Line 372 | static inline void update_display_window
372                  } else {
373  
374                          // Update the_host_buffer and copy of the_buffer
375 <                        const int src_bytes_per_row = VideoMonitor.mode.bytes_per_row;
376 <                        const int dst_bytes_per_row = drv->img->bytes_per_line;
377 <                        const int bytes_per_pixel = src_bytes_per_row / VideoMonitor.mode.x;
375 >                        const int src_bytes_per_row = VIDEO_MODE_ROW_BYTES;
376 >                        const int dst_bytes_per_row = VIDEO_DRV_ROW_BYTES;
377 >                        const int bytes_per_pixel = src_bytes_per_row / VIDEO_MODE_X;
378                          int i1 = y1 * src_bytes_per_row, i2 = y1 * dst_bytes_per_row, j;
379                          for (j = y1; j <= y2; j++) {
380 <                                Screen_blit(the_host_buffer + i2, the_buffer + i1, bytes_per_pixel * VideoMonitor.mode.x);
380 >                                Screen_blit(the_host_buffer + i2, the_buffer + i1, bytes_per_pixel * VIDEO_MODE_X);
381                                  i1 += src_bytes_per_row;
382                                  i2 += dst_bytes_per_row;
383                          }
384                  }
385  
386 <                if (drv->have_shm)
387 <                        XShmPutImage(x_display, drv->w, drv->gc, drv->img, 0, y1, 0, y1, VideoMonitor.mode.x, height, 0);
386 >                VIDEO_DRV_UNLOCK_PIXELS;
387 >
388 > #ifdef USE_SDL_VIDEO
389 >                SDL_UpdateRect(drv->s, 0, y1, VIDEO_MODE_X, height);
390 > #else
391 >                if (VIDEO_DRV_HAVE_SHM)
392 >                        XShmPutImage(x_display, VIDEO_DRV_WINDOW, VIDEO_DRV_GC, VIDEO_DRV_IMAGE, 0, y1, 0, y1, VIDEO_MODE_X, height, 0);
393                  else
394 <                        XPutImage(x_display, drv->w, drv->gc, drv->img, 0, y1, 0, y1, VideoMonitor.mode.x, height);
394 >                        XPutImage(x_display, VIDEO_DRV_WINDOW, VIDEO_DRV_GC, VIDEO_DRV_IMAGE, 0, y1, 0, y1, VIDEO_MODE_X, height);
395 > #endif
396          }
397          mainBuffer.dirty = false;
398   }
# Line 382 | Line 406 | static inline void update_display_window
406   #if REAL_ADDRESSING || DIRECT_ADDRESSING
407   static inline void update_display_dga_vosf(void)
408   {
409 +        VIDEO_MODE_INIT;
410 +
411          int page = 0;
412          for (;;) {
413 <                const int first_page = find_next_page_set(page);
413 >                const unsigned first_page = find_next_page_set(page);
414                  if (first_page >= mainBuffer.pageCount)
415                          break;
416  
# Line 400 | Line 426 | static inline void update_display_dga_vo
426                  const int y1 = mainBuffer.pageInfo[first_page].top;
427                  const int y2 = mainBuffer.pageInfo[page - 1].bottom;
428                  
429 <                const int bytes_per_row = VideoMonitor.mode.bytes_per_row;
430 <                const int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
429 >                const int bytes_per_row = VIDEO_MODE_ROW_BYTES;
430 >                const int bytes_per_pixel = VIDEO_MODE_ROW_BYTES / VIDEO_MODE_X;
431                  int i, j;
432                  
433                  // Check for first column from left and first column
434                  // from right that have changed
435 <                int x1 = VideoMonitor.mode.x * bytes_per_pixel - 1;
435 >                int x1 = VIDEO_MODE_X * bytes_per_pixel - 1;
436                  for (j = y1; j <= y2; j++) {
437                          uint8 * const p1 = &the_buffer[j * bytes_per_row];
438                          uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
# Line 423 | Line 449 | static inline void update_display_dga_vo
449                  for (j = y2; j >= y1; j--) {
450                          uint8 * const p1 = &the_buffer[j * bytes_per_row];
451                          uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
452 <                        for (i = VideoMonitor.mode.x * bytes_per_pixel - 1; i > x2; i--) {
452 >                        for (i = VIDEO_MODE_X * bytes_per_pixel - 1; i > x2; i--) {
453                                  if (p1[i] != p2[i]) {
454                                          x2 = i;
455                                          break;
# Line 434 | Line 460 | static inline void update_display_dga_vo
460                  
461                  // Update the_host_buffer and copy of the_buffer
462                  // There should be at least one pixel to copy
463 +                VIDEO_DRV_LOCK_PIXELS;
464                  const int width = x2 - x1 + 1;
465                  i = y1 * bytes_per_row + x1 * bytes_per_pixel;
466                  for (j = y1; j <= y2; j++) {
# Line 441 | Line 468 | static inline void update_display_dga_vo
468                          memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * width);
469                          i += bytes_per_row;
470                  }
471 +                VIDEO_DRV_UNLOCK_PIXELS;
472          }
473          mainBuffer.dirty = false;
474   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines