Browse Source

Draw a black outline around the mouse cursor

The cursor is now a white arrow with a one-pixel black outline instead of
solid white, so it stays visible over white pages and document content.

cursor_shape.h holds the shared 16x16 fill and outline bitmaps; the
software cursor, the Cirrus two-plane hardware cursor and the Radeon ARGB
cursor all build their images from them.  The QMP cursor regression test
follows the shifted fill bitmap.
Johannes Zellner 2 weeks ago
parent
commit
5fc22ccfef
6 changed files with 110 additions and 63 deletions
  1. 3 3
      Makefile
  2. 15 14
      cirrus.c
  3. 50 0
      cursor_shape.h
  4. 21 29
      mouse.c
  5. 17 13
      radeon.c
  6. 4 4
      tools/qmp_cursor_test.py

+ 3 - 3
Makefile

@@ -124,7 +124,7 @@ gpu.o: gpu.c gpu.h pci.h ati.h cirrus.h nvidia.h
 ati.o: ati.c ati.h pci.h radeon.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-cirrus.o: cirrus.c cirrus.h pci.h vga.h
+cirrus.o: cirrus.c cirrus.h cursor_shape.h pci.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 nvidia.o: nvidia.c nvidia.h nv50.h pci.h process.h serial.h
@@ -133,7 +133,7 @@ nvidia.o: nvidia.c nvidia.h nv50.h pci.h process.h serial.h
 nv50.o: nv50.c nv50.h nvidia.h serial.h
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
-radeon.o: radeon.c radeon.h pci.h paging.h serial.h vga.h
+radeon.o: radeon.c radeon.h cursor_shape.h pci.h paging.h serial.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 mtrr.o: mtrr.c mtrr.h serial.h
@@ -217,7 +217,7 @@ fatformat.o: fatformat.c fatformat.h blkdev.h io.h mm.h sched.h
 format.o: format.c blkdev.h disk.h fatformat.h io.h process.h sched.h vga.h vfs.h
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
-mouse.o: mouse.c mouse.h bootinfo.h cirrus.h radeon.h timer.h usbhid.h vga.h
+mouse.o: mouse.c mouse.h bootinfo.h cirrus.h cursor_shape.h radeon.h timer.h usbhid.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 idle.o: idle.c idle.h

+ 15 - 14
cirrus.c

@@ -2,6 +2,7 @@
    Reference: QEMU hw/display/cirrus_vga.c (register offsets, ROP codes). */
 
 #include "cirrus.h"
+#include "cursor_shape.h"
 #include "pci.h"
 #include "vga.h"
 
@@ -194,14 +195,6 @@ void cirrus_fill(unsigned int x, unsigned int y,
 		;
 }
 
-/* Pointer-arrow shape (16x16), same as the software cursor: tip at top-left,
-   widening triangle then a narrowing tail. Placed in the top-left of the
-   32x32 hardware-cursor box. MSB (bit 15) = column 0. */
-static const unsigned short cirrus_cursor_arrow[16] = {
-	0x8000, 0xC000, 0xE000, 0xF000, 0xF800, 0xFC00, 0xFE00, 0xFF00,
-	0xFC00, 0xE000, 0x8000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
-};
-
 void cirrus_cursor_setup(void)
 {
 	unsigned char bitmap[256];
@@ -210,14 +203,22 @@ void cirrus_cursor_setup(void)
 	unsigned char *dst;
 
 	/* 32x32 two-plane cursor: AND plane (offset 0..127), XOR plane (128..255),
-	   4 bytes per row. The arrow is drawn in the XOR plane (so pixels are
-	   color0 = white) with the AND plane all-zero (no invert). */
+	   4 bytes per row.  The shared arrow is placed in the top-left of the
+	   box (the cursor position is the top-left of the 32x32 box):
+	     AND=0 XOR=0 -> transparent
+	     AND=0 XOR=1 -> color0 (white fill)
+	     AND=1 XOR=1 -> color1 (black outline)
+	   The hidden DAC entries 0 and 15 are programmed white and black. */
 	for (i = 0; i < 256; i++)
 		bitmap[i] = 0;
-	for (r = 0; r < 16; r++) {
-		unsigned short bits = cirrus_cursor_arrow[r];
-		bitmap[128 + r * 4 + 0] = (unsigned char)((bits >> 8) & 0xFF); /* x=0..7  */
-		bitmap[128 + r * 4 + 1] = (unsigned char)(bits & 0xFF);        /* x=8..15 */
+	for (r = 0; r < CURSOR_SHAPE_HEIGHT; r++) {
+		unsigned short fill = cursor_shape_fill[r];
+		unsigned short outl = cursor_shape_outline[r];
+		unsigned short xor = fill | outl;
+		bitmap[r * 4 + 0] = (unsigned char)((outl >> 8) & 0xFF);        /* AND x=0..7  */
+		bitmap[r * 4 + 1] = (unsigned char)(outl & 0xFF);               /* AND x=8..15 */
+		bitmap[128 + r * 4 + 0] = (unsigned char)((xor >> 8) & 0xFF);   /* XOR x=0..7  */
+		bitmap[128 + r * 4 + 1] = (unsigned char)(xor & 0xFF);          /* XOR x=8..15 */
 	}
 
 	/* Upload to the top of VRAM (SR[0x13] = 0 selects the first 256-byte block). */

+ 50 - 0
cursor_shape.h

@@ -0,0 +1,50 @@
+#ifndef CURSOR_SHAPE_INCLUDED
+#define CURSOR_SHAPE_INCLUDED
+
+/* Shared 16x16 mouse-pointer image: a white arrow with a one-pixel black
+   outline, so the cursor stays visible over white pages and windows too.
+   Bit 15 of each row is column 0; the hotspot is the top-left pixel of the
+   16x16 box.  The software cursor, the Cirrus two-plane cursor and the
+   Radeon ARGB cursor all build their bitmaps from these tables. */
+
+#define CURSOR_SHAPE_WIDTH 16
+#define CURSOR_SHAPE_HEIGHT 16
+
+/* White fill (the arrow body). */
+static const unsigned short cursor_shape_fill[CURSOR_SHAPE_HEIGHT] = {
+	0x0000,
+	0x4000,  /* 1....... ........ */
+	0x6000,  /* 11...... ........ */
+	0x7000,  /* 111..... ........ */
+	0x7800,  /* 1111.... ........ */
+	0x7C00,  /* 11111... ........ */
+	0x7E00,  /* 111111.. ........ */
+	0x7F00,  /* 1111111. ........ */
+	0x7F80,  /* 11111111 ........ */
+	0x7E00,  /* 111111.. ........ */
+	0x7000,  /* 111..... ........ */
+	0x4000,  /* 1....... ........ */
+	0x0000,
+	0x0000,
+	0x0000,
+	0x0000
+};
+
+/* Black outline (everything adjacent to the fill). */
+static const unsigned short cursor_shape_outline[CURSOR_SHAPE_HEIGHT] = {
+	0xC000,  /* 11...... ........  tip */
+	0xA000,  /* 1.1..... ........ */
+	0x9000,  /* 1..1.... ........ */
+	0x8800,  /* 1...1... ........ */
+	0x8400,  /* 1....1.. ........ */
+	0x8200,  /* 1.....1. ........ */
+	0x8100,  /* 1......1 ........ */
+	0x8080,  /* 1....... 1....... */
+	0x8040,  /* 1....... .1...... */
+	0x8180,  /* 1......1 1....... */
+	0x8E00,  /* 1...111. ........ */
+	0xB000,  /* 1.11.... ........ */
+	0x4000   /* .1...... ........ */
+};
+
+#endif

+ 21 - 29
mouse.c

@@ -1,6 +1,7 @@
 #include "mouse.h"
 #include "bootinfo.h"
 #include "cirrus.h"
+#include "cursor_shape.h"
 #include "radeon.h"
 #include "timer.h"
 #include "usbhid.h"
@@ -17,30 +18,8 @@
 #define MOUSE_ACK     0xFA
 #define MOUSE_RESEND  0xFE
 
-#define MOUSE_CUR_W 16
-#define MOUSE_CUR_H 16
-
-/* Simple triangle cursor bitmap, 16×16.
-   Hotspot is the top-left pixel of the 16×16 box (x0,y0).
-   Each row is encoded as 16 bits; MSB (bit 15) maps to c=0. */
-static const unsigned short mouse_pointer_bits[MOUSE_CUR_H] = {
-	0x8000,  /* 1....... ........  row 0: tip */
-	0xC000,  /* 11...... ........  row 1 */
-	0xE000,  /* 111..... ........  row 2 */
-	0xF000,  /* 1111.... ........  row 3 */
-	0xF800,  /* 11111... ........  row 4 */
-	0xFC00,  /* 111111.. ........  row 5 */
-	0xFE00,  /* 1111111. ........  row 6 */
-	0xFF00,  /* 11111111 ........  row 7 */
-	0xFC00,  /* 111111.. ........  row 8 */
-	0xE000,  /* 111..... ........  row 9 */
-	0x8000,  /* 1....... ........  row 10 */
-	0x0000,  /* ........ ........  row 11 */
-	0x0000,  /* ........ ........  row 12 */
-	0x0000,  /* ........ ........  row 13 */
-	0x0000,  /* ........ ........  row 14 */
-	0x0000   /* ........ ........  row 15 */
-};
+#define MOUSE_CUR_W CURSOR_SHAPE_WIDTH
+#define MOUSE_CUR_H CURSOR_SHAPE_HEIGHT
 
 static int mouse_present_flag;
 static int mouse_streaming_flag;
@@ -129,11 +108,21 @@ static int mouse_send_cmd_ack(unsigned char cmd)
 	return r;
 }
 
-static int mouse_bit_at(int r, int c)
+enum {
+	MOUSE_PIX_TRANSPARENT = 0,
+	MOUSE_PIX_OUTLINE,
+	MOUSE_PIX_FILL
+};
+
+static int mouse_pixel_at(int r, int c)
 {
 	if (r < 0 || c < 0 || r >= (int)MOUSE_CUR_H || c >= (int)MOUSE_CUR_W)
-		return 0;
-	return (mouse_pointer_bits[(unsigned int)r] & (0x8000u >> c)) != 0;
+		return MOUSE_PIX_TRANSPARENT;
+	if (cursor_shape_fill[(unsigned int)r] & (0x8000u >> c))
+		return MOUSE_PIX_FILL;
+	if (cursor_shape_outline[(unsigned int)r] & (0x8000u >> c))
+		return MOUSE_PIX_OUTLINE;
+	return MOUSE_PIX_TRANSPARENT;
 }
 
 static void cursor_save_under(void)
@@ -156,11 +145,14 @@ static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
 {
 	unsigned int r, c;
 
-	/* Draw only the cursor fill bitmap (no outline). */
+	/* White arrow body with a black outline around it. */
 	for (r = 0; r < MOUSE_CUR_H; r++) {
 		for (c = 0; c < MOUSE_CUR_W; c++) {
-			if (mouse_bit_at((int)r, (int)c))
+			int px = mouse_pixel_at((int)r, (int)c);
+			if (px == MOUSE_PIX_FILL)
 				vga_put_pixel_rgb(x0 + c, y0 + r, 255, 255, 255);
+			else if (px == MOUSE_PIX_OUTLINE)
+				vga_put_pixel_rgb(x0 + c, y0 + r, 0, 0, 0);
 		}
 	}
 }

+ 17 - 13
radeon.c

@@ -22,6 +22,7 @@
  */
 
 #include "radeon.h"
+#include "cursor_shape.h"
 #include "pci.h"
 #include "paging.h"
 #include "serial.h"
@@ -76,12 +77,6 @@ static inline void mmio_mm_write(unsigned int reg, unsigned int v)
 	mmio_write(RADEON_MM_DATA, v);
 }
 
-/* 16x16 arrow (MSB = column 0), matching the software cursor shape. */
-static const unsigned short radeon_arrow[16] = {
-	0x8000, 0xC000, 0xE000, 0xF000, 0xF800, 0xFC00, 0xFE00, 0xFF00,
-	0xFC00, 0xE000, 0x8000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
-};
-
 void radeon_init(unsigned int bus, unsigned int dev, unsigned int device_id)
 {
 	unsigned int bar2, bar2_base, bar2_size;
@@ -142,19 +137,28 @@ void radeon_cursor_setup(void)
 
 	dst = (unsigned char *)(vga_fb_lfb_addr() + RADEON_CUR_VRAM_OFS);
 
-	/* Transparent background (alpha 0), then the opaque white arrow. */
+	/* Transparent background (alpha 0), then the opaque white fill and
+	   black outline of the shared arrow. */
 	for (i = 0; i < RADEON_CUR_BYTES; i++)
 		dst[i] = 0;
-	for (r = 0; r < 16; r++) {
-		unsigned short bits = radeon_arrow[r];
-		for (c = 0; c < 16; c++) {
-			if (bits & (0x8000u >> c)) {
-				unsigned int off = (r * RADEON_CUR_SIZE + c) * 4;
+	for (r = 0; r < CURSOR_SHAPE_HEIGHT; r++) {
+		unsigned short fill = cursor_shape_fill[r];
+		unsigned short outl = cursor_shape_outline[r];
+		for (c = 0; c < CURSOR_SHAPE_WIDTH; c++) {
+			unsigned short bit = (unsigned short)(0x8000u >> c);
+			unsigned int off = (r * RADEON_CUR_SIZE + c) * 4;
+			if (fill & bit) {
 				dst[off + 0] = 0xFF;  /* B */
 				dst[off + 1] = 0xFF;  /* G */
 				dst[off + 2] = 0xFF;  /* R */
-				dst[off + 3] = 0xFF;  /* A */
+			} else if (outl & bit) {
+				dst[off + 0] = 0x00;  /* B */
+				dst[off + 1] = 0x00;  /* G */
+				dst[off + 2] = 0x00;  /* R */
+			} else {
+				continue;
 			}
+			dst[off + 3] = 0xFF;          /* A */
 		}
 	}
 

+ 4 - 4
tools/qmp_cursor_test.py

@@ -25,9 +25,9 @@ import time
 
 ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
-# 16x16 cursor bitmap rows (same table as mouse.c); bit 15 = column 0.
-CUR = [0x8000, 0xC000, 0xE000, 0xF000, 0xF800, 0xFC00, 0xFE00, 0xFF00,
-       0xFC00, 0xE000, 0x8000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000]
+# 16x16 cursor fill rows (same table as cursor_shape.h); bit 15 = column 0.
+CUR = [0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7C00, 0x7E00, 0x7F00,
+       0x7F80, 0x7E00, 0x7000, 0x4000, 0x0000, 0x0000, 0x0000, 0x0000]
 
 
 def qmp_connect(sock):
@@ -78,7 +78,7 @@ def pixel(w, pix, x, y):
 
 
 def cursor_matches(w, pix, x0, y0):
-    for r in range(8):
+    for r in range(16):
         for c in range(16):
             want = (CUR[r] & (0x8000 >> c)) != 0
             got = pixel(w, pix, x0 + c, y0 + r) == (255, 255, 255)