ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/video_x.cpp
(Generate patch)

Comparing SheepShaver/src/Unix/video_x.cpp (file contents):
Revision 1.14 by gbeauche, 2004-04-11T10:46:32Z vs.
Revision 1.29 by gbeauche, 2004-06-22T20:01:18Z

# Line 32 | Line 32
32   #include <algorithm>
33  
34   #ifdef ENABLE_XF86_DGA
35 < #include <X11/extensions/xf86dga.h>
35 > # include <X11/extensions/xf86dga.h>
36   #endif
37  
38   #ifdef ENABLE_XF86_VIDMODE
# Line 57 | Line 57 | using std::sort;
57  
58   // Constants
59   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
60 + static const bool hw_mac_cursor_accl = true;    // Flag: Enable MacOS to X11 copy of cursor?
61  
62   // Global variables
63   static int32 frame_skip;
64   static int16 mouse_wheel_mode;
65   static int16 mouse_wheel_lines;
66   static bool redraw_thread_active = false;       // Flag: Redraw thread installed
67 + static pthread_attr_t redraw_thread_attr;       // Redraw thread attributes
68 + static volatile bool redraw_thread_cancel;      // Flag: Cancel Redraw thread
69   static pthread_t redraw_thread;                         // Redraw thread
70  
71   static bool local_X11;                                          // Flag: X server running on local machine?
# Line 80 | Line 83 | static const bool use_vosf = false;                    //
83  
84   static bool palette_changed = false;            // Flag: Palette changed, redraw thread must update palette
85   static bool ctrl_down = false;                          // Flag: Ctrl key pressed
86 + static bool caps_on = false;                            // Flag: Caps Lock on
87   static bool quit_full_screen = false;           // Flag: DGA close requested from redraw thread
88   static volatile bool quit_full_screen_ack = false;      // Acknowledge for quit_full_screen
89   static bool emerg_quit = false;                         // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
# Line 125 | Line 129 | static uint8 *the_buffer_copy = NULL;          /
129   static uint32 the_buffer_size;                          // Size of allocated the_buffer
130  
131   // Variables for DGA mode
128 static char *dga_screen_base;
129 static int dga_fb_width;
132   static int current_dga_cmap;
133  
134   #ifdef ENABLE_XF86_VIDMODE
# Line 196 | Line 198 | static int palette_size(int mode)
198          }
199   }
200  
201 + // Return bits per pixel for requested depth
202 + static inline int bytes_per_pixel(int depth)
203 + {
204 +        int bpp;
205 +        switch (depth) {
206 +        case 8:
207 +                bpp = 1;
208 +                break;
209 +        case 15: case 16:
210 +                bpp = 2;
211 +                break;
212 +        case 24: case 32:
213 +                bpp = 4;
214 +                break;
215 +        default:
216 +                abort();
217 +        }
218 +        return bpp;
219 + }
220 +
221   // Map video_mode depth ID to numerical depth value
222   static inline int depth_of_video_mode(int mode)
223   {
224 <        int depth = -1;
224 >        int depth;
225          switch (mode) {
226          case APPLE_1_BIT:
227                  depth = 1;
# Line 349 | Line 371 | static void set_window_delete_protocol(W
371   }
372  
373   // Wait until window is mapped/unmapped
374 < void wait_mapped(Window w)
374 > static void wait_mapped(Window w)
375   {
376          XEvent e;
377          do {
# Line 357 | Line 379 | void wait_mapped(Window w)
379          } while ((e.type != MapNotify) || (e.xmap.event != w));
380   }
381  
382 < void wait_unmapped(Window w)
382 > static void wait_unmapped(Window w)
383   {
384          XEvent e;
385          do {
# Line 486 | Line 508 | static bool open_window(int width, int h
508          XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
509  
510          // Create cursor
511 <        cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2);
512 <        cursor_image->byte_order = MSBFirst;
513 <        cursor_image->bitmap_bit_order = MSBFirst;
514 <        cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 36, 16, 16, 16, 2);
515 <        cursor_mask_image->byte_order = MSBFirst;
516 <        cursor_mask_image->bitmap_bit_order = MSBFirst;
517 <        cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
518 <        cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
519 <        cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
520 <        cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
521 <        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
522 <        cursor_changed = false;
511 >        if (hw_mac_cursor_accl) {
512 >                cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2);
513 >                cursor_image->byte_order = MSBFirst;
514 >                cursor_image->bitmap_bit_order = MSBFirst;
515 >                cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 36, 16, 16, 16, 2);
516 >                cursor_mask_image->byte_order = MSBFirst;
517 >                cursor_mask_image->bitmap_bit_order = MSBFirst;
518 >                cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
519 >                cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
520 >                cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
521 >                cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
522 >                mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
523 >                cursor_changed = false;
524 >        }
525 >
526 >        // Create no_cursor
527 >        else {
528 >                mac_cursor = XCreatePixmapCursor(x_display,
529 >                        XCreatePixmap(x_display, the_win, 1, 1, 1),
530 >                        XCreatePixmap(x_display, the_win, 1, 1, 1),
531 >                        &black, &white, 0, 0);
532 >                XDefineCursor(x_display, the_win, mac_cursor);
533 >        }
534  
535          // Init blitting routines
536          bool native_byte_order;
# Line 522 | Line 555 | static bool open_dga(int width, int heig
555          // Set relative mouse mode
556          ADBSetRelMouseMode(true);
557  
558 +        // Create window
559 +        XSetWindowAttributes wattr;
560 +        wattr.event_mask = eventmask = dga_eventmask;
561 +        wattr.override_redirect = True;
562 +        wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
563 +        the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
564 +                InputOutput, vis, CWEventMask | CWOverrideRedirect |
565 +                (color_class == DirectColor ? CWColormap : 0), &wattr);
566 +
567 +        // Show window
568 +        XMapRaised(x_display, the_win);
569 +        wait_mapped(the_win);
570 +
571   #ifdef ENABLE_XF86_VIDMODE
572          // Switch to best mode
573          if (has_vidmode) {
# Line 538 | Line 584 | static bool open_dga(int width, int heig
584   #endif
585  
586          // Establish direct screen connection
587 +        XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
588 +        XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
589          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
590          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
591 +
592 +        int v_width, v_bank, v_size;
593 +        XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
594          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
595          XF86DGASetViewPort(x_display, screen, 0, 0);
596          XF86DGASetVidPage(x_display, screen, 0);
597  
598          // Set colormap
599 <        if (depth == 8)
599 >        if (!IsDirectMode(get_current_mode())) {
600 >                XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
601                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
602 +        }
603 +        XSync(x_display, false);
604  
605 <        // Set bytes per row
606 <        int bytes_per_row = TrivialBytesPerRow((dga_fb_width + 7) & ~7, DepthModeForPixelDepth(depth));
553 <
605 >        // Init blitting routines
606 >        int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, DepthModeForPixelDepth(depth));
607   #if ENABLE_VOSF
608          bool native_byte_order;
609   #ifdef WORDS_BIGENDIAN
# Line 569 | Line 622 | static bool open_dga(int width, int heig
622            the_buffer_size = page_extend((height + 2) * bytes_per_row);
623            the_buffer_copy = (uint8 *)malloc(the_buffer_size);
624            the_buffer = (uint8 *)vm_acquire(the_buffer_size);
625 +          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
626          }
627   #else
628          use_vosf = false;
575        the_buffer = dga_screen_base;
629   #endif
630   #endif
578        screen_base = (uint32)the_buffer;
631  
632 +        // Set frame buffer base
633 +        D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
634 +        screen_base = (uint32)the_buffer;
635          VModes[cur_mode].viRowBytes = bytes_per_row;
581        XSync(x_display, false);
636          return true;
637   #else
638          ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
# Line 711 | Line 765 | static void close_window(void)
765          if (the_gc)
766                  XFreeGC(x_display, the_gc);
767  
714        // Close window
715        if (the_win) {
716                XUnmapWindow(x_display, the_win);
717                wait_unmapped(the_win);
718                XDestroyWindow(x_display, the_win);
719        }
720
768          XFlush(x_display);
769          XSync(x_display, false);
770   }
# Line 755 | Line 802 | static void close_display(void)
802          else if (display_type == DIS_WINDOW)
803                  close_window();
804  
805 +        // Close window
806 +        if (the_win) {
807 +                XUnmapWindow(x_display, the_win);
808 +                wait_unmapped(the_win);
809 +                XDestroyWindow(x_display, the_win);
810 +        }
811 +
812          // Free colormaps
813          if (cmap[0]) {
814                  XFreeColormap(x_display, cmap[0]);
# Line 830 | Line 884 | static void keycode_init(void)
884  
885                  // Search for server vendor string, then read keycodes
886                  const char *vendor = ServerVendor(x_display);
887 + #if (defined(__APPLE__) && defined(__MACH__))
888 +                // Force use of MacX mappings on MacOS X with Apple's X server
889 +                int dummy;
890 +                if (XQueryExtension(x_display, "Apple-DRI", &dummy, &dummy, &dummy))
891 +                        vendor = "MacX";
892 + #endif
893                  bool vendor_found = false;
894                  char line[256];
895                  while (fgets(line, 255, f)) {
# Line 871 | Line 931 | static void keycode_init(void)
931          }
932   }
933  
934 + // Find Apple mode matching best specified dimensions
935 + static int find_apple_resolution(int xsize, int ysize)
936 + {
937 +        int apple_id;
938 +        if (xsize < 800)
939 +                apple_id = APPLE_640x480;
940 +        else if (xsize < 1024)
941 +                apple_id = APPLE_800x600;
942 +        else if (xsize < 1152)
943 +                apple_id = APPLE_1024x768;
944 +        else if (xsize < 1280) {
945 +                if (ysize < 900)
946 +                        apple_id = APPLE_1152x768;
947 +                else
948 +                        apple_id = APPLE_1152x900;
949 +        }
950 +        else if (xsize < 1600)
951 +                apple_id = APPLE_1280x1024;
952 +        else
953 +                apple_id = APPLE_1600x1200;
954 +        return apple_id;
955 + }
956 +
957 + // Find mode in list of supported modes
958 + static int find_mode(int apple_mode, int apple_id, int type)
959 + {
960 +        for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
961 +                if (p->viType == type && p->viAppleID == apple_id && p->viAppleMode == apple_mode)
962 +                        return p - VModes;
963 +        }
964 +        return -1;
965 + }
966 +
967   // Add mode to list of supported modes
968   static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type)
969   {
# Line 891 | Line 984 | static void add_mode(VideoInfo *&p, uint
984                                  p->viXsize = 1024;
985                                  p->viYsize = 768;
986                                  break;
987 +                        case APPLE_1152x768:
988 +                                p->viXsize = 1152;
989 +                                p->viYsize = 768;
990 +                                break;
991                          case APPLE_1152x900:
992                                  p->viXsize = 1152;
993                                  p->viYsize = 900;
# Line 1037 | Line 1134 | bool VideoInit(void)
1134                          add_mode(p, screen_modes, 2, default_mode, APPLE_800x600, DIS_SCREEN);
1135                  if (has_mode(1024, 768))
1136                          add_mode(p, screen_modes, 4, default_mode, APPLE_1024x768, DIS_SCREEN);
1137 +                if (has_mode(1152, 768))
1138 +                        add_mode(p, screen_modes, 64, default_mode, APPLE_1152x768, DIS_SCREEN);
1139                  if (has_mode(1152, 900))
1140                          add_mode(p, screen_modes, 8, default_mode, APPLE_1152x900, DIS_SCREEN);
1141                  if (has_mode(1280, 1024))
# Line 1046 | Line 1145 | bool VideoInit(void)
1145          } else if (screen_modes) {
1146                  int xsize = DisplayWidth(x_display, screen);
1147                  int ysize = DisplayHeight(x_display, screen);
1148 <                int apple_id;
1050 <                if (xsize < 800)
1051 <                        apple_id = APPLE_640x480;
1052 <                else if (xsize < 1024)
1053 <                        apple_id = APPLE_800x600;
1054 <                else if (xsize < 1152)
1055 <                        apple_id = APPLE_1024x768;
1056 <                else if (xsize < 1280)
1057 <                        apple_id = APPLE_1152x900;
1058 <                else if (xsize < 1600)
1059 <                        apple_id = APPLE_1280x1024;
1060 <                else
1061 <                        apple_id = APPLE_1600x1200;
1148 >                int apple_id = find_apple_resolution(xsize, ysize);
1149                  p->viType = DIS_SCREEN;
1150                  p->viRowBytes = 0;
1151                  p->viXsize = xsize;
# Line 1075 | Line 1162 | bool VideoInit(void)
1162  
1163          // Find default mode (window 640x480)
1164          cur_mode = -1;
1165 <        for (p = VModes; p->viType != DIS_INVALID; p++) {
1166 <                if (p->viType == DIS_WINDOW
1167 <                        && p->viAppleID == APPLE_W_640x480
1168 <                        && p->viAppleMode == default_mode) {
1169 <                        cur_mode = p - VModes;
1170 <                        break;
1165 >        if (has_dga && screen_modes) {
1166 >                int screen_width = DisplayWidth(x_display, screen);
1167 >                int screen_height = DisplayHeight(x_display, screen);
1168 >                int apple_id = find_apple_resolution(screen_width, screen_height);
1169 >                if (apple_id != -1)
1170 >                        cur_mode = find_mode(default_mode, apple_id, DIS_SCREEN);
1171 >        }
1172 >        if (cur_mode == -1) {
1173 >                // pick up first windowed mode available
1174 >                for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
1175 >                        if (p->viType == DIS_WINDOW && p->viAppleMode == default_mode) {
1176 >                                cur_mode = p - VModes;
1177 >                                break;
1178 >                        }
1179                  }
1180          }
1181          assert(cur_mode != -1);
# Line 1093 | Line 1188 | bool VideoInit(void)
1188          }
1189   #endif
1190  
1096 #ifdef ENABLE_XF86_DGA
1097        if (has_dga && screen_modes) {
1098                int v_bank, v_size;
1099                XF86DGAGetVideo(x_display, screen, &dga_screen_base, &dga_fb_width, &v_bank, &v_size);
1100                D(bug("DGA screen_base %p, v_width %d\n", dga_screen_base, dga_fb_width));
1101        }
1102 #endif
1103
1191          // Open window/screen
1192          if (!open_display())
1193                  return false;
# Line 1112 | Line 1199 | bool VideoInit(void)
1199  
1200          // Start periodic thread
1201          XSync(x_display, false);
1202 <        redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1202 >        Set_pthread_attr(&redraw_thread_attr, 0);
1203 >        redraw_thread_cancel = false;
1204 >        redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1205          D(bug("Redraw thread installed (%ld)\n", redraw_thread));
1206          return true;
1207   }
# Line 1126 | Line 1215 | void VideoExit(void)
1215   {
1216          // Stop redraw thread
1217          if (redraw_thread_active) {
1218 +                redraw_thread_cancel = true;
1219                  pthread_cancel(redraw_thread);
1220                  pthread_join(redraw_thread, NULL);
1221                  redraw_thread_active = false;
# Line 1404 | Line 1494 | static int kc_decode(KeySym ks)
1494          return -1;
1495   }
1496  
1497 < static int event2keycode(XKeyEvent &ev)
1497 > static int event2keycode(XKeyEvent &ev, bool key_down)
1498   {
1499          KeySym ks;
1410        int as;
1500          int i = 0;
1501  
1502          do {
1503                  ks = XLookupKeysym(&ev, i++);
1504 <                as = kc_decode(ks);
1505 <                if (as != -1)
1504 >                int as = kc_decode(ks);
1505 >                if (as >= 0)
1506 >                        return as;
1507 >                if (as == -2)
1508                          return as;
1509          } while (ks != NoSymbol);
1510  
# Line 1475 | Line 1566 | static void handle_events(void)
1566                                  break;
1567                          }
1568  
1569 <                        // Mouse moved
1569 >                        // Mouse entered window
1570                          case EnterNotify:
1571 <                                ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1571 >                                if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
1572 >                                        ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1573                                  break;
1574 +
1575 +                        // Mouse moved
1576                          case MotionNotify:
1577 <                                ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1577 >                                ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1578                                  break;
1579  
1580                          // Keyboard
1581                          case KeyPress: {
1582 <                                int code = event2keycode(event.xkey);
1583 <                                if (use_keycodes && code != -1)
1584 <                                        code = keycode_table[event.xkey.keycode & 0xff];
1585 <                                if (code != -1) {
1582 >                                int code = -1;
1583 >                                if (use_keycodes) {
1584 >                                        if (event2keycode(event.xkey, true) != -2)      // This is called to process the hotkeys
1585 >                                                code = keycode_table[event.xkey.keycode & 0xff];
1586 >                                } else
1587 >                                        code = event2keycode(event.xkey, true);
1588 >                                if (code >= 0) {
1589                                          if (!emul_suspended) {
1590 <                                                ADBKeyDown(code);
1590 >                                                if (code == 0x39) {     // Caps Lock pressed
1591 >                                                        if (caps_on) {
1592 >                                                                ADBKeyUp(code);
1593 >                                                                caps_on = false;
1594 >                                                        } else {
1595 >                                                                ADBKeyDown(code);
1596 >                                                                caps_on = true;
1597 >                                                        }
1598 >                                                } else
1599 >                                                        ADBKeyDown(code);
1600                                                  if (code == 0x36)
1601                                                          ctrl_down = true;
1602                                          } else {
# Line 1501 | Line 1607 | static void handle_events(void)
1607                                  break;
1608                          }
1609                          case KeyRelease: {
1610 <                                int code = event2keycode(event.xkey);
1611 <                                if (use_keycodes && code != 1)
1612 <                                        code = keycode_table[event.xkey.keycode & 0xff];
1613 <                                if (code != -1) {
1610 >                                int code = -1;
1611 >                                if (use_keycodes) {
1612 >                                        if (event2keycode(event.xkey, false) != -2)     // This is called to process the hotkeys
1613 >                                                code = keycode_table[event.xkey.keycode & 0xff];
1614 >                                } else
1615 >                                        code = event2keycode(event.xkey, false);
1616 >                                if (code >= 0 && code != 0x39) {        // Don't propagate Caps Lock releases
1617                                          ADBKeyUp(code);
1618                                          if (code == 0x36)
1619                                                  ctrl_down = false;
# Line 1547 | Line 1656 | void VideoVBL(void)
1656   *  Install graphics acceleration
1657   */
1658  
1659 < #if 0
1660 < // Rectangle blitting
1661 < static void accl_bitblt(accl_params *p)
1659 > // Rectangle inversion
1660 > template< int bpp >
1661 > static inline void do_invrect(uint8 *dest, uint32 length)
1662   {
1663 <        D(bug("accl_bitblt\n"));
1663 > #define INVERT_1(PTR, OFS) ((uint8  *)(PTR))[OFS] = ~((uint8  *)(PTR))[OFS]
1664 > #define INVERT_2(PTR, OFS) ((uint16 *)(PTR))[OFS] = ~((uint16 *)(PTR))[OFS]
1665 > #define INVERT_4(PTR, OFS) ((uint32 *)(PTR))[OFS] = ~((uint32 *)(PTR))[OFS]
1666 > #define INVERT_8(PTR, OFS) ((uint64 *)(PTR))[OFS] = ~((uint64 *)(PTR))[OFS]
1667  
1668 <        // Get blitting parameters
1669 <        int16 src_X = p->src_rect[1] - p->src_bounds[1];
1670 <        int16 src_Y = p->src_rect[0] - p->src_bounds[0];
1671 <        int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];
1672 <        int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];
1673 <        int16 width = p->dest_rect[3] - p->dest_rect[1] - 1;
1562 <        int16 height = p->dest_rect[2] - p->dest_rect[0] - 1;
1563 <        D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1564 <        D(bug(" width %d, height %d\n", width, height));
1668 > #ifndef UNALIGNED_PROFITABLE
1669 >        // Align on 16-bit boundaries
1670 >        if (bpp < 16 && (((uintptr)dest) & 1)) {
1671 >                INVERT_1(dest, 0);
1672 >                dest += 1; length -= 1;
1673 >        }
1674  
1675 <        // And perform the blit
1676 <        bitblt_hook(src_X, src_Y, dest_X, dest_Y, width, height);
1677 < }
1675 >        // Align on 32-bit boundaries
1676 >        if (bpp < 32 && (((uintptr)dest) & 2)) {
1677 >                INVERT_2(dest, 0);
1678 >                dest += 2; length -= 2;
1679 >        }
1680 > #endif
1681  
1682 < static bool accl_bitblt_hook(accl_params *p)
1683 < {
1684 <        D(bug("accl_draw_hook %p\n", p));
1682 >        // Invert 8-byte words
1683 >        if (length >= 8) {
1684 >                const int r = (length / 8) % 8;
1685 >                dest += r * 8;
1686  
1687 <        // Check if we can accelerate this bitblt
1688 <        if (p->src_base_addr == screen_base && p->dest_base_addr == screen_base &&
1689 <                display_type == DIS_SCREEN && bitblt_hook != NULL &&
1690 <                ((uint32 *)p)[0x18 >> 2] + ((uint32 *)p)[0x128 >> 2] == 0 &&
1691 <                ((uint32 *)p)[0x130 >> 2] == 0 &&
1692 <                p->transfer_mode == 0 &&
1693 <                p->src_row_bytes > 0 && ((uint32 *)p)[0x15c >> 2] > 0) {
1687 >                int n = ((length / 8) + 7) / 8;
1688 >                switch (r) {
1689 >                case 0: do {
1690 >                                dest += 64;
1691 >                                INVERT_8(dest, -8);
1692 >                case 7: INVERT_8(dest, -7);
1693 >                case 6: INVERT_8(dest, -6);
1694 >                case 5: INVERT_8(dest, -5);
1695 >                case 4: INVERT_8(dest, -4);
1696 >                case 3: INVERT_8(dest, -3);
1697 >                case 2: INVERT_8(dest, -2);
1698 >                case 1: INVERT_8(dest, -1);
1699 >                                } while (--n > 0);
1700 >                }
1701 >        }
1702  
1703 <                // Yes, set function pointer
1704 <                p->draw_proc = accl_bitblt;
1705 <                return true;
1703 >        // 32-bit cell to invert?
1704 >        if (length & 4) {
1705 >                INVERT_4(dest, 0);
1706 >                if (bpp <= 16)
1707 >                        dest += 4;
1708          }
1709 <        return false;
1709 >
1710 >        // 16-bit cell to invert?
1711 >        if (bpp <= 16 && (length & 2)) {
1712 >                INVERT_2(dest, 0);
1713 >                if (bpp <= 8)
1714 >                        dest += 2;
1715 >        }
1716 >
1717 >        // 8-bit cell to invert?
1718 >        if (bpp <= 8 && (length & 1))
1719 >                INVERT_1(dest, 0);
1720 >
1721 > #undef INVERT_1
1722 > #undef INVERT_2
1723 > #undef INVERT_4
1724 > #undef INVERT_8
1725   }
1726  
1727 < // Rectangle filling/inversion
1590 < static void accl_fillrect8(accl_params *p)
1727 > void NQD_invrect(uint32 p)
1728   {
1729 <        D(bug("accl_fillrect8\n"));
1729 >        D(bug("accl_invrect %08x\n", p));
1730  
1731 <        // Get filling parameters
1732 <        int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];
1733 <        int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];
1734 <        int16 dest_X_max = p->dest_rect[3] - p->dest_bounds[1] - 1;
1735 <        int16 dest_Y_max = p->dest_rect[2] - p->dest_bounds[0] - 1;
1599 <        uint8 color = p->pen_mode == 8 ? p->fore_pen : p->back_pen;
1731 >        // Get inversion parameters
1732 >        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1733 >        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1734 >        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1735 >        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1736          D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1737 <        D(bug(" dest X max %d, dest Y max %d\n", dest_X_max, dest_Y_max));
1737 >        D(bug(" width %d, height %d, bytes_per_row %d\n", width, height, (int32)ReadMacInt32(p + acclDestRowBytes)));
1738  
1739 <        // And perform the fill
1740 <        fillrect8_hook(dest_X, dest_Y, dest_X_max, dest_Y_max, color);
1739 >        //!!?? pen_mode == 14
1740 >
1741 >        // And perform the inversion
1742 >        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1743 >        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1744 >        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1745 >        width *= bpp;
1746 >        switch (bpp) {
1747 >        case 1:
1748 >                for (int i = 0; i < height; i++) {
1749 >                        do_invrect<8>(dest, width);
1750 >                        dest += dest_row_bytes;
1751 >                }
1752 >                break;
1753 >        case 2:
1754 >                for (int i = 0; i < height; i++) {
1755 >                        do_invrect<16>(dest, width);
1756 >                        dest += dest_row_bytes;
1757 >                }
1758 >                break;
1759 >        case 4:
1760 >                for (int i = 0; i < height; i++) {
1761 >                        do_invrect<32>(dest, width);
1762 >                        dest += dest_row_bytes;
1763 >                }
1764 >                break;
1765 >        }
1766   }
1767  
1768 < static void accl_fillrect32(accl_params *p)
1768 > // Rectangle filling
1769 > template< int bpp >
1770 > static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
1771   {
1772 <        D(bug("accl_fillrect32\n"));
1772 > #define FILL_1(PTR, OFS, VAL) ((uint8  *)(PTR))[OFS] = (VAL)
1773 > #define FILL_2(PTR, OFS, VAL) ((uint16 *)(PTR))[OFS] = (VAL)
1774 > #define FILL_4(PTR, OFS, VAL) ((uint32 *)(PTR))[OFS] = (VAL)
1775 > #define FILL_8(PTR, OFS, VAL) ((uint64 *)(PTR))[OFS] = (VAL)
1776  
1777 <        // Get filling parameters
1778 <        int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];
1779 <        int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];
1780 <        int16 dest_X_max = p->dest_rect[3] - p->dest_bounds[1] - 1;
1781 <        int16 dest_Y_max = p->dest_rect[2] - p->dest_bounds[0] - 1;
1782 <        uint32 color = p->pen_mode == 8 ? p->fore_pen : p->back_pen;
1617 <        D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1618 <        D(bug(" dest X max %d, dest Y max %d\n", dest_X_max, dest_Y_max));
1777 > #ifndef UNALIGNED_PROFITABLE
1778 >        // Align on 16-bit boundaries
1779 >        if (bpp < 16 && (((uintptr)dest) & 1)) {
1780 >                FILL_1(dest, 0, color);
1781 >                dest += 1; length -= 1;
1782 >        }
1783  
1784 <        // And perform the fill
1785 <        fillrect32_hook(dest_X, dest_Y, dest_X_max, dest_Y_max, color);
1784 >        // Align on 32-bit boundaries
1785 >        if (bpp < 32 && (((uintptr)dest) & 2)) {
1786 >                FILL_2(dest, 0, color);
1787 >                dest += 2; length -= 2;
1788 >        }
1789 > #endif
1790 >
1791 >        // Fill 8-byte words
1792 >        if (length >= 8) {
1793 >                const uint64 c = (((uint64)color) << 32) | color;
1794 >                const int r = (length / 8) % 8;
1795 >                dest += r * 8;
1796 >
1797 >                int n = ((length / 8) + 7) / 8;
1798 >                switch (r) {
1799 >                case 0: do {
1800 >                                dest += 64;
1801 >                                FILL_8(dest, -8, c);
1802 >                case 7: FILL_8(dest, -7, c);
1803 >                case 6: FILL_8(dest, -6, c);
1804 >                case 5: FILL_8(dest, -5, c);
1805 >                case 4: FILL_8(dest, -4, c);
1806 >                case 3: FILL_8(dest, -3, c);
1807 >                case 2: FILL_8(dest, -2, c);
1808 >                case 1: FILL_8(dest, -1, c);
1809 >                                } while (--n > 0);
1810 >                }
1811 >        }
1812 >
1813 >        // 32-bit cell to fill?
1814 >        if (length & 4) {
1815 >                FILL_4(dest, 0, color);
1816 >                if (bpp <= 16)
1817 >                        dest += 4;
1818 >        }
1819 >
1820 >        // 16-bit cell to fill?
1821 >        if (bpp <= 16 && (length & 2)) {
1822 >                FILL_2(dest, 0, color);
1823 >                if (bpp <= 8)
1824 >                        dest += 2;
1825 >        }
1826 >
1827 >        // 8-bit cell to fill?
1828 >        if (bpp <= 8 && (length & 1))
1829 >                FILL_1(dest, 0, color);
1830 >
1831 > #undef FILL_1
1832 > #undef FILL_2
1833 > #undef FILL_4
1834 > #undef FILL_8
1835   }
1836  
1837 < static void accl_invrect(accl_params *p)
1837 > void NQD_fillrect(uint32 p)
1838   {
1839 <        D(bug("accl_invrect\n"));
1839 >        D(bug("accl_fillrect %08x\n", p));
1840  
1841 <        // Get inversion parameters
1842 <        int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];
1843 <        int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];
1844 <        int16 dest_X_max = p->dest_rect[3] - p->dest_bounds[1] - 1;
1845 <        int16 dest_Y_max = p->dest_rect[2] - p->dest_bounds[0] - 1;
1841 >        // Get filling parameters
1842 >        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1843 >        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1844 >        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1845 >        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1846 >        uint32 color = htonl(ReadMacInt32(p + acclPenMode) == 8 ? ReadMacInt32(p + acclForePen) : ReadMacInt32(p + acclBackPen));
1847          D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1848 <        D(bug(" dest X max %d, dest Y max %d\n", dest_X_max, dest_Y_max));
1849 <
1636 <        //!!?? pen_mode == 14
1848 >        D(bug(" width %d, height %d\n", width, height));
1849 >        D(bug(" bytes_per_row %d color %08x\n", (int32)ReadMacInt32(p + acclDestRowBytes), color));
1850  
1851 <        // And perform the inversion
1852 <        invrect_hook(dest_X, dest_Y, dest_X_max, dest_Y_max);
1851 >        // And perform the fill
1852 >        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1853 >        const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1854 >        uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1855 >        width *= bpp;
1856 >        switch (bpp) {
1857 >        case 1:
1858 >                for (int i = 0; i < height; i++) {
1859 >                        memset(dest, color, width);
1860 >                        dest += dest_row_bytes;
1861 >                }
1862 >                break;
1863 >        case 2:
1864 >                for (int i = 0; i < height; i++) {
1865 >                        do_fillrect<16>(dest, color, width);
1866 >                        dest += dest_row_bytes;
1867 >                }
1868 >                break;
1869 >        case 4:
1870 >                for (int i = 0; i < height; i++) {
1871 >                        do_fillrect<32>(dest, color, width);
1872 >                        dest += dest_row_bytes;
1873 >                }
1874 >                break;
1875 >        }
1876   }
1877  
1878 < static bool accl_fillrect_hook(accl_params *p)
1878 > bool NQD_fillrect_hook(uint32 p)
1879   {
1880 <        D(bug("accl_fillrect_hook %p\n", p));
1880 >        D(bug("accl_fillrect_hook %08x\n", p));
1881  
1882          // Check if we can accelerate this fillrect
1883 <        if (p->dest_base_addr == screen_base && ((uint32 *)p)[0x284 >> 2] != 0 && display_type == DIS_SCREEN) {
1884 <                if (p->transfer_mode == 8) {
1883 >        if (ReadMacInt32(p + 0x284) != 0 && ReadMacInt32(p + acclDestPixelSize) >= 8) {
1884 >                const int transfer_mode = ReadMacInt32(p + acclTransferMode);
1885 >                if (transfer_mode == 8) {
1886                          // Fill
1887 <                        if (p->dest_pixel_size == 8 && fillrect8_hook != NULL) {
1888 <                                p->draw_proc = accl_fillrect8;
1889 <                                return true;
1890 <                        } else if (p->dest_pixel_size == 32 && fillrect32_hook != NULL) {
1654 <                                p->draw_proc = accl_fillrect32;
1655 <                                return true;
1656 <                        }
1657 <                } else if (p->transfer_mode == 10 && invrect_hook != NULL) {
1887 >                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_FILLRECT));
1888 >                        return true;
1889 >                }
1890 >                else if (transfer_mode == 10) {
1891                          // Invert
1892 <                        p->draw_proc = accl_invrect;
1892 >                        WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_INVRECT));
1893                          return true;
1894                  }
1895          }
1896          return false;
1897   }
1898  
1899 + // Rectangle blitting
1900 + // TODO: optimize for VOSF and target pixmap == screen
1901 + void NQD_bitblt(uint32 p)
1902 + {
1903 +        D(bug("accl_bitblt %08x\n", p));
1904 +
1905 +        // Get blitting parameters
1906 +        int16 src_X  = (int16)ReadMacInt16(p + acclSrcRect + 2) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 2);
1907 +        int16 src_Y  = (int16)ReadMacInt16(p + acclSrcRect + 0) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 0);
1908 +        int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1909 +        int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1910 +        int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1911 +        int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1912 +        D(bug(" src addr %08x, dest addr %08x\n", ReadMacInt32(p + acclSrcBaseAddr), ReadMacInt32(p + acclDestBaseAddr)));
1913 +        D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1914 +        D(bug(" width %d, height %d\n", width, height));
1915 +
1916 +        // And perform the blit
1917 +        const int bpp = bytes_per_pixel(ReadMacInt32(p + acclSrcPixelSize));
1918 +        width *= bpp;
1919 +        if ((int32)ReadMacInt32(p + acclSrcRowBytes) > 0) {
1920 +                const int src_row_bytes = (int32)ReadMacInt32(p + acclSrcRowBytes);
1921 +                const int dst_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1922 +                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + (src_Y * src_row_bytes) + (src_X * bpp));
1923 +                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dst_row_bytes) + (dest_X * bpp));
1924 +                for (int i = 0; i < height; i++) {
1925 +                        memmove(dst, src, width);
1926 +                        src += src_row_bytes;
1927 +                        dst += dst_row_bytes;
1928 +                }
1929 +        }
1930 +        else {
1931 +                const int src_row_bytes = -(int32)ReadMacInt32(p + acclSrcRowBytes);
1932 +                const int dst_row_bytes = -(int32)ReadMacInt32(p + acclDestRowBytes);
1933 +                uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + ((src_Y + height - 1) * src_row_bytes) + (src_X * bpp));
1934 +                uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + ((dest_Y + height - 1) * dst_row_bytes) + (dest_X * bpp));
1935 +                for (int i = height - 1; i >= 0; i--) {
1936 +                        memmove(dst, src, width);
1937 +                        src -= src_row_bytes;
1938 +                        dst -= dst_row_bytes;
1939 +                }
1940 +        }
1941 + }
1942 +
1943 + /*
1944 +  BitBlt transfer modes:
1945 +  0 : srcCopy
1946 +  1 : srcOr
1947 +  2 : srcXor
1948 +  3 : srcBic
1949 +  4 : notSrcCopy
1950 +  5 : notSrcOr
1951 +  6 : notSrcXor
1952 +  7 : notSrcBic
1953 +  32 : blend
1954 +  33 : addPin
1955 +  34 : addOver
1956 +  35 : subPin
1957 +  36 : transparent
1958 +  37 : adMax
1959 +  38 : subOver
1960 +  39 : adMin
1961 +  50 : hilite
1962 + */
1963 +
1964 + bool NQD_bitblt_hook(uint32 p)
1965 + {
1966 +        D(bug("accl_draw_hook %08x\n", p));
1967 +
1968 +        // Check if we can accelerate this bitblt
1969 +        if (ReadMacInt32(p + 0x018) + ReadMacInt32(p + 0x128) == 0 &&
1970 +                ReadMacInt32(p + 0x130) == 0 &&
1971 +                ReadMacInt32(p + acclSrcPixelSize) >= 8 &&
1972 +                ReadMacInt32(p + acclSrcPixelSize) == ReadMacInt32(p + acclDestPixelSize) &&
1973 +                (ReadMacInt32(p + acclSrcRowBytes) ^ ReadMacInt32(p + acclDestRowBytes)) >= 0 && // same sign?
1974 +                ReadMacInt32(p + acclTransferMode) == 0 &&                                                                               // srcCopy?
1975 +                ReadMacInt32(p + 0x15c) > 0) {
1976 +
1977 +                // Yes, set function pointer
1978 +                WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_BITBLT));
1979 +                return true;
1980 +        }
1981 +        return false;
1982 + }
1983 +
1984   // Wait for graphics operation to finish
1985 < static bool accl_sync_hook(void *arg)
1985 > bool NQD_sync_hook(uint32 arg)
1986   {
1987 <        D(bug("accl_sync_hook %p\n", arg));
1670 <        if (sync_hook != NULL)
1671 <                sync_hook();
1987 >        D(bug("accl_sync_hook %08x\n", arg));
1988          return true;
1989   }
1990  
1675 static struct accl_hook_info bitblt_hook_info = {accl_bitblt_hook, accl_sync_hook, ACCL_BITBLT};
1676 static struct accl_hook_info fillrect_hook_info = {accl_fillrect_hook, accl_sync_hook, ACCL_FILLRECT};
1677 #endif
1678
1991   void VideoInstallAccel(void)
1992   {
1993          // Install acceleration hooks
1994          if (PrefsFindBool("gfxaccel")) {
1995                  D(bug("Video: Installing acceleration hooks\n"));
1996 < //!!    NQDMisc(6, &bitblt_hook_info);
1997 < //              NQDMisc(6, &fillrect_hook_info);
1996 >                uint32 base;
1997 >
1998 >                SheepVar bitblt_hook_info(sizeof(accl_hook_info));
1999 >                base = bitblt_hook_info.addr();
2000 >                WriteMacInt32(base + 0, NativeTVECT(NATIVE_BITBLT_HOOK));
2001 >                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
2002 >                WriteMacInt32(base + 8, ACCL_BITBLT);
2003 >                NQDMisc(6, bitblt_hook_info.ptr());
2004 >
2005 >                SheepVar fillrect_hook_info(sizeof(accl_hook_info));
2006 >                base = fillrect_hook_info.addr();
2007 >                WriteMacInt32(base + 0, NativeTVECT(NATIVE_FILLRECT_HOOK));
2008 >                WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
2009 >                WriteMacInt32(base + 8, ACCL_FILLRECT);
2010 >                NQDMisc(6, fillrect_hook_info.ptr());
2011          }
2012   }
2013  
# Line 1790 | Line 2115 | void video_set_palette(void)
2115  
2116  
2117   /*
2118 + *  Can we set the MacOS cursor image into the window?
2119 + */
2120 +
2121 + bool video_can_change_cursor(void)
2122 + {
2123 +        return hw_mac_cursor_accl && (display_type != DIS_SCREEN);
2124 + }
2125 +
2126 +
2127 + /*
2128   *  Set cursor image for window
2129   */
2130  
# Line 1969 | Line 2304 | static void *redraw_func(void *arg)
2304          int64 ticks = 0;
2305          uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2306  
2307 <        for (;;) {
2307 >        while (!redraw_thread_cancel) {
2308  
2309                  // Pause if requested (during video mode switches)
2310                  while (thread_stop_req)
# Line 1999 | Line 2334 | static void *redraw_func(void *arg)
2334                                          XF86DGADirectVideo(x_display, screen, 0);
2335                                          XUngrabPointer(x_display, CurrentTime);
2336                                          XUngrabKeyboard(x_display, CurrentTime);
2337 +                                        XUnmapWindow(x_display, the_win);
2338 +                                        wait_unmapped(the_win);
2339 +                                        XDestroyWindow(x_display, the_win);
2340   #endif
2341                                          XSync(x_display, false);
2342                                          XDisplayUnlock();
# Line 2031 | Line 2369 | static void *redraw_func(void *arg)
2369                                                  update_display();
2370  
2371                                          // Set new cursor image if it was changed
2372 <                                        if (cursor_changed) {
2372 >                                        if (hw_mac_cursor_accl && cursor_changed) {
2373                                                  cursor_changed = false;
2374                                                  memcpy(cursor_image->data, MacCursor + 4, 32);
2375                                                  memcpy(cursor_mask_image->data, MacCursor + 36, 32);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines