Răsfoiți Sursa

Draw through a render target and a blit backend

Groundwork for the accelerated and alpha steps: the drawing code no longer
knows about the shadow buffer.

- vga.c: struct surface { px, w, h, pitch, bpp, alpha } and a per-process
  render target.  vga_target() is the screen's shadow buffer unless the
  running process set an override (stored in the PCB, so a draw that is
  preempted mid-layer can never land in another process's buffer).  The
  pixel/geometry accessors describe the screen, not the target.
  vga_target_damage() marks damage only while the target is the screen --
  drawing into a layer must not touch the screen's damage list.
  vga_present_shadow() is the old budgeted flush.
- blit.c/h (new): struct blit_ops { fill, copy, blend, canvas, present,
  flip } with the software implementation (the old fill, the drag's
  overlap-safe copy moved out of wm.c, and an ARGB8888 blend ready for the
  layer compositor).  copy()/present() work on the screen; the rest draw
  into the target.
- fbtext.c/gfx.c: draw into the target; cell/phrase damage goes through
  vga_target_damage().  The 4th byte of 32 bpp pixels is now an alpha:
  255 for the screen (it used to be 0 padding), the surface's alpha for a
  layer's background fill, always opaque for glyph foregrounds.
- wm.c: the drag copies through blit->copy and the canvas blit through
  blit->canvas.  wm_drag_fast() is gone: its Cirrus VRAM BitBLT was
  overwritten by the subsequent shadow flush (the shadow needs the moved
  pixels for later partial repaints), so it was duplicate work.
  cirrus_blit() stays for a future VRAM/flip backend.
- Boot log and `display` print the active backend (software today).

Behaviour is unchanged; PCB_SIZE grows 116 -> 120 for the target field.
Johannes Zellner 2 săptămâni în urmă
părinte
comite
3841f3662d
13 a modificat fișierele cu 500 adăugiri și 220 ștergeri
  1. 13 4
      AGENTS.md
  2. 6 3
      Makefile
  3. 225 0
      blit.c
  4. 51 0
      blit.h
  5. 4 0
      display.c
  6. 25 15
      fbtext.c
  7. 22 21
      gfx.c
  8. 17 1
      process.c
  9. 8 0
      process.h
  10. 1 1
      switch.S
  11. 88 94
      vga.c
  12. 35 1
      vga.h
  13. 5 80
      wm.c

+ 13 - 4
AGENTS.md

@@ -100,8 +100,16 @@ below the run area), so adding code no longer moves any address.
   still reaches the terminal), closing a window kills its session, and
   still reaches the terminal), closing a window kills its session, and
   `sys_gfx_reap`/`sys_gfx_collect` free the canvas after exit/crash.  `clock`
   `sys_gfx_reap`/`sys_gfx_collect` free the canvas after exit/crash.  `clock`
   and `colors` are its users.
   and `colors` are its users.
-- **Graphics**: `vga.c/h`, `vga_copy.S`, `gpu.c`, `ati.c`, `nvidia.c`, `nv50.c`,
-  `pci.h` — VESA LFB + shadow buffer.  Drawing marks damage rectangles (a small
+- **Graphics**: `vga.c/h`, `vga_copy.S`, `blit.c/h`, `gpu.c`, `ati.c`, `nvidia.c`,
+  `nv50.c`, `pci.h` — VESA LFB + shadow buffer.  Drawing goes into a
+  `struct surface` render target: `vga_target()` is the screen's shadow buffer
+  unless the running process set an override (`vga_set_target()`, stored in the
+  PCB so a preempted draw can never leak), and the WM's pixel operations go
+  through the `blit` vtable (`blit.c/h`: fill/copy/blend/canvas/present/flip,
+  software only today) so an accelerator can replace single ops without
+  touching the WM.  `vga_target_damage()` is a no-op while the target is not
+  the screen.  The boot log prints `blit backend: <name>`.  Drawing marks
+  damage rectangles (a small
   list that merges rects when they overlap or touch, so a keystroke's cells
   list that merges rects when they overlap or touch, so a keystroke's cells
   collapse while unrelated damage stays apart); the main loop copies them to
   collapse while unrelated damage stays apart); the main loop copies them to
   VRAM on every iteration under a byte budget (`VGA_FLUSH_BUDGET_BYTES`), no
   VRAM on every iteration under a byte budget (`VGA_FLUSH_BUDGET_BYTES`), no
@@ -642,8 +650,9 @@ Caveats:
   `procs[i].cr3` made the boot-time MMIO mapping (`paging_map_mmio_cr3`) fault
   `procs[i].cr3` made the boot-time MMIO mapping (`paging_map_mmio_cr3`) fault
   on the board while QEMU was fine.  Anything above `MM_MAX_PHYS` (128 MB) is
   on the board while QEMU was fine.  Anything above `MM_MAX_PHYS` (128 MB) is
   also *unmapped*, so a wild pointer there is an instant #PF.
   also *unmapped*, so a wild pointer there is an instant #PF.
-- **All drawing goes into the shadow buffer** (`vga_put_pixel_rgb` etc.); the
-  main loop flushes the pending damage rects to VRAM on every iteration (byte
+- **All drawing goes into the current render target** (the shadow buffer by
+  default; `vga_target()`, see the graphics bullet); the main loop flushes the
+  pending damage rects to VRAM on every iteration (byte
   budget, see `VGA_FLUSH_BUDGET_BYTES`) so small updates (cursor, a keystroke)
   budget, see `VGA_FLUSH_BUDGET_BYTES`) so small updates (cursor, a keystroke)
   reach the screen within a loop instead of waiting for a fixed tick. A window
   reach the screen within a loop instead of waiting for a fixed tick. A window
   drag flushes its rectangles immediately (`wm_drag_shadow`) so each step is
   drag flushes its rectangles immediately (`wm_drag_shadow`) so each step is

+ 6 - 3
Makefile

@@ -77,7 +77,10 @@ bin_recv.o: bin_recv.c bin_recv.h serial.h io.h timer.h
 serial_recv.o: serial_recv.c serial.h bin_recv.h io.h
 serial_recv.o: serial_recv.c serial.h bin_recv.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
-vga.o: vga.c vga.h pci.h gpu.h ati.h
+vga.o: vga.c vga.h blit.h pci.h gpu.h ati.h process.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+blit.o: blit.c blit.h vga.h gfx.h
 	$(CC) $(CFLAGS) -o $@ $<
 	$(CC) $(CFLAGS) -o $@ $<
 
 
 vga_copy.o: vga_copy.S
 vga_copy.o: vga_copy.S
@@ -248,7 +251,7 @@ sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h
 syscall_stub.o: syscall_stub.S irqret.inc
 syscall_stub.o: syscall_stub.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 	$(AS) $(ASFLAGS) -o $@ $<
 
 
-kernel.elf: vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o prog_embed.o space_embed.o cmds_pack.o ramfs.o payload.o
+kernel.elf: vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o prog_embed.o space_embed.o cmds_pack.o ramfs.o payload.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -583,4 +586,4 @@ net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 
 clean:
 clean:
-	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o cmds.pack cmds_pack.o ramfs.o payload.o mkpack $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) $(TLSLIB_OBJS) tls_test bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor
+	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o cmds.pack cmds_pack.o ramfs.o payload.o mkpack $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) $(TLSLIB_OBJS) tls_test bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor

+ 225 - 0
blit.c

@@ -0,0 +1,225 @@
+/* Blit backend: software implementation (the fallback for every machine).
+ *
+ * The ops draw into the current render target, so a later layer compositor can
+ * reuse them unchanged; copy() and present() work on the screen.  The software
+ * paths are plain cached-memory loops (the target is the shadow buffer in RAM
+ * for now; a flip/VRAM backend would replace them with GPU operations). */
+
+#include "blit.h"
+#include "gfx.h"
+#include "vga.h"
+
+static unsigned int pixel_bytes(unsigned int bpp)
+{
+	if (bpp == 16)
+		return 2;
+	if (bpp == 24)
+		return 3;
+	return 4;
+}
+
+/* Solid fill of the target. */
+static void software_fill(unsigned int x, unsigned int y, unsigned int w,
+			  unsigned int h, unsigned int rgb)
+{
+	struct surface *t = vga_target();
+	unsigned int r = (rgb >> 16) & 0xFF;
+	unsigned int g = (rgb >> 8) & 0xFF;
+	unsigned int b = rgb & 0xFF;
+	unsigned int row, x1, y1;
+
+	if (!t || !t->px || w == 0 || h == 0)
+		return;
+	if (x >= t->w || y >= t->h)
+		return;
+	x1 = x + w;
+	y1 = y + h;
+	if (x1 > t->w)
+		x1 = t->w;
+	if (y1 > t->h)
+		y1 = t->h;
+
+	if (t->bpp == 16) {
+		unsigned short val = (unsigned short)(((r >> 3) << 11) |
+					((g >> 2) << 5) | (b >> 3));
+		for (row = y; row < y1; row++) {
+			unsigned short *p = (unsigned short *)(t->px +
+					(unsigned long)row * t->pitch + x * 2);
+			unsigned int xx;
+			for (xx = x; xx < x1; xx++)
+				*p++ = val;
+		}
+	} else if (t->bpp == 24) {
+		for (row = y; row < y1; row++) {
+			unsigned char *p = t->px + (unsigned long)row * t->pitch + x * 3;
+			unsigned int xx;
+			for (xx = x; xx < x1; xx++) {
+				p[0] = (unsigned char)b;
+				p[1] = (unsigned char)g;
+				p[2] = (unsigned char)r;
+				p += 3;
+			}
+		}
+	} else {
+		unsigned int val = ((unsigned int)t->alpha << 24) |
+				   (r << 16) | (g << 8) | b;
+		for (row = y; row < y1; row++) {
+			unsigned int *p = (unsigned int *)(t->px +
+					(unsigned long)row * t->pitch + x * 4);
+			unsigned int xx;
+			for (xx = x; xx < x1; xx++)
+				*p++ = val;
+		}
+	}
+	vga_target_damage(x, y, x1 - x, y1 - y);
+}
+
+/* Overlap-safe row copy (dword when aligned, bytes otherwise). */
+static void row_move(unsigned char *dst, const unsigned char *src, unsigned int n)
+{
+	if (dst == src || n == 0)
+		return;
+
+	if ((((unsigned long)dst | (unsigned long)src) & 3) == 0) {
+		unsigned int words = n >> 2;
+		unsigned int i;
+		unsigned int *dw = (unsigned int *)dst;
+		const unsigned int *sw = (const unsigned int *)src;
+
+		if (dst < src) {
+			for (i = 0; i < words; i++)
+				dw[i] = sw[i];
+			for (i = words << 2; i < n; i++)
+				dst[i] = src[i];
+		} else {
+			/* Overlapping move down: back to front. */
+			for (i = n; i > (words << 2); i--)
+				dst[i - 1] = src[i - 1];
+			for (i = words; i-- > 0; )
+				dw[i] = sw[i];
+		}
+		return;
+	}
+
+	if (dst < src) {
+		unsigned int i;
+		for (i = 0; i < n; i++)
+			dst[i] = src[i];
+	} else {
+		while (n--)
+			dst[n] = src[n];
+	}
+}
+
+/* Rectangle copy inside the target (a window move). */
+static void software_copy(unsigned int dx, unsigned int dy, unsigned int sx,
+			  unsigned int sy, unsigned int w, unsigned int h)
+{
+	struct surface *t = vga_target();
+	unsigned int pb, row_bytes, r;
+
+	if (!t || !t->px || w == 0 || h == 0)
+		return;
+	pb = pixel_bytes(t->bpp);
+	if (dx >= t->w || dy >= t->h || sx >= t->w || sy >= t->h)
+		return;
+	if (w > t->w - dx) w = t->w - dx;
+	if (w > t->w - sx) w = t->w - sx;
+	if (h > t->h - dy) h = t->h - dy;
+	if (h > t->h - sy) h = t->h - sy;
+	if (w == 0 || h == 0)
+		return;
+	row_bytes = w * pb;
+
+	if (dy > sy || (dy == sy && dx > sx)) {
+		for (r = h; r-- > 0; )
+			row_move(t->px + (unsigned long)(dy + r) * t->pitch + dx * pb,
+				 t->px + (unsigned long)(sy + r) * t->pitch + sx * pb,
+				 row_bytes);
+	} else {
+		for (r = 0; r < h; r++)
+			row_move(t->px + (unsigned long)(dy + r) * t->pitch + dx * pb,
+				 t->px + (unsigned long)(sy + r) * t->pitch + sx * pb,
+				 row_bytes);
+	}
+	vga_target_damage(dx, dy, w, h);
+}
+
+/* Blend an ARGB8888 source onto the target (uniform alpha, 32 bpp only).
+ * The source alpha byte is the per-pixel coverage; `alpha` scales it. */
+static void software_blend(unsigned int x, unsigned int y, unsigned int w,
+			   unsigned int h, const struct surface *src,
+			   unsigned int sx, unsigned int sy, unsigned int alpha)
+{
+	struct surface *t = vga_target();
+	unsigned int row, col;
+
+	if (!t || !t->px || !src || !src->px)
+		return;
+	if (t->bpp != 32 || src->bpp != 32 || w == 0 || h == 0)
+		return;
+	if (x >= t->w || y >= t->h || sx >= src->w || sy >= src->h)
+		return;
+	if (w > t->w - x) w = t->w - x;
+	if (w > src->w - sx) w = src->w - sx;
+	if (h > t->h - y) h = t->h - y;
+	if (h > src->h - sy) h = src->h - sy;
+	if (w == 0 || h == 0)
+		return;
+
+	for (row = 0; row < h; row++) {
+		unsigned int *d = (unsigned int *)(t->px +
+				(unsigned long)(y + row) * t->pitch + x * 4);
+		const unsigned int *s = (const unsigned int *)(src->px +
+				(unsigned long)(sy + row) * src->pitch + sx * 4);
+		for (col = 0; col < w; col++) {
+			unsigned int sv = s[col];
+			unsigned int a = (sv >> 24) & 0xFF;
+			unsigned int dv = d[col];
+			unsigned int ia;
+
+			if (alpha != 255)
+				a = (a * alpha) / 255;
+			if (a == 0)
+				continue;
+			if (a == 255) {
+				d[col] = sv & 0x00FFFFFFu;
+				continue;
+			}
+			ia = 255 - a;
+			d[col] = ((((((sv >> 16) & 0xFF) * a + ((dv >> 16) & 0xFF) * ia) / 255) & 0xFF) << 16) |
+				 ((((((sv >> 8) & 0xFF) * a + ((dv >> 8) & 0xFF) * ia) / 255) & 0xFF) << 8) |
+				  ((((sv & 0xFF) * a + (dv & 0xFF) * ia) / 255) & 0xFF);
+		}
+	}
+	vga_target_damage(x, y, w, h);
+}
+
+static void software_canvas(const struct gfx_canvas *c, unsigned int x,
+			    unsigned int y, unsigned int cx, unsigned int cy,
+			    unsigned int cw, unsigned int ch)
+{
+	gfx_blit_clip(c, x, y, cx, cy, cw, ch);
+}
+
+static void software_present(unsigned int max_bytes)
+{
+	vga_present_shadow(max_bytes);
+}
+
+static void software_flip(void)
+{
+	/* Single-buffered: present() already pushed the damage to VRAM. */
+}
+
+const struct blit_ops blit_software = {
+	"software",
+	software_fill,
+	software_copy,
+	software_blend,
+	software_canvas,
+	software_present,
+	software_flip,
+};
+
+const struct blit_ops *blit = &blit_software;

+ 51 - 0
blit.h

@@ -0,0 +1,51 @@
+/* Blit backend: the drawing operations the window manager uses, behind a
+ * vtable so a GPU/driver implementation can replace the software one without
+ * touching the WM.  Ops paint into the current render target (vga_target());
+ * copy() and present() work on the screen.
+ *
+ * The software table is the only backend today; it is also the fallback for
+ * every machine without (or before) a usable accelerator. */
+#ifndef BLIT_H
+#define BLIT_H
+
+struct surface;
+struct gfx_canvas;
+
+struct blit_ops {
+	const char *name;
+
+	/* Solid fill with a packed 0xRRGGBB colour. */
+	void (*fill)(unsigned int x, unsigned int y, unsigned int w,
+		     unsigned int h, unsigned int rgb);
+
+	/* Overlap-safe rectangle copy inside the target. */
+	void (*copy)(unsigned int dx, unsigned int dy, unsigned int sx,
+		     unsigned int sy, unsigned int w, unsigned int h);
+
+	/* Blend an ARGB8888 source surface onto the target: uniform alpha for
+	 * 255, per-pixel source alpha otherwise.  Used by the layer
+	 * compositor. */
+	void (*blend)(unsigned int x, unsigned int y, unsigned int w,
+		      unsigned int h, const struct surface *src,
+		      unsigned int sx, unsigned int sy, unsigned int alpha);
+
+	/* Canvas (RGB565) blit clipped to (cx, cy, cw, ch), scaled to the
+	 * target's pixel format. */
+	void (*canvas)(const struct gfx_canvas *c, unsigned int x,
+		       unsigned int y, unsigned int cx, unsigned int cy,
+		       unsigned int cw, unsigned int ch);
+
+	/* Make drawn content visible: copy the pending damage rects (up to
+	 * max_bytes, 0 = everything), or flip when the backend has two
+	 * buffers. */
+	void (*present)(unsigned int max_bytes);
+
+	/* Wait for the next frame and swap (no-op for the single-buffered
+	 * software path). */
+	void (*flip)(void);
+};
+
+extern const struct blit_ops *blit;      /* the active backend */
+extern const struct blit_ops blit_software;
+
+#endif

+ 4 - 0
display.c

@@ -1,6 +1,7 @@
 /* display command: print resolution, color depth, and other graphics info. */
 /* display command: print resolution, color depth, and other graphics info. */
 
 
 #include "io.h"
 #include "io.h"
+#include "blit.h"
 #include "vga.h"
 #include "vga.h"
 #include "gpu.h"
 #include "gpu.h"
 #include "ati.h"
 #include "ati.h"
@@ -57,6 +58,9 @@ void program_display_main(void)
 	io_putstr("color depth ");
 	io_putstr("color depth ");
 	put_uint(vga_fb_bpp());
 	put_uint(vga_fb_bpp());
 	io_putstr(" bpp\n");
 	io_putstr(" bpp\n");
+	io_putstr("blit backend ");
+	io_putstr(blit ? blit->name : "none");
+	io_putstr("\n");
 	io_putstr("pitch ");
 	io_putstr("pitch ");
 	put_uint(vga_fb_pitch());
 	put_uint(vga_fb_pitch());
 	io_putstr(" bytes/line\n");
 	io_putstr(" bytes/line\n");

+ 25 - 15
fbtext.c

@@ -34,13 +34,21 @@ static void draw_glyph_at_clip(unsigned int x0, unsigned int y0, unsigned char c
 {
 {
 	unsigned int x, y;
 	unsigned int x, y;
 	const unsigned char *glyph = &font_8x8[(unsigned int)c * FONT_8X8_ROWS];
 	const unsigned char *glyph = &font_8x8[(unsigned int)c * FONT_8X8_ROWS];
-	unsigned char *fb = (unsigned char *)vga_fb_ptr();
-	unsigned int pitch = vga_fb_pitch();
-	unsigned int bpp = vga_fb_bpp();
-	unsigned int fg32 = (r_fg << 16) | (g_fg << 8) | b_fg;
-	unsigned int bg32 = (r_bg << 16) | (g_bg << 8) | b_bg;
-	unsigned short fg16 = (unsigned short)(((r_fg >> 3) << 11) | ((g_fg >> 2) << 5) | (b_fg >> 3));
-	unsigned short bg16 = (unsigned short)(((r_bg >> 3) << 11) | ((g_bg >> 2) << 5) | (b_bg >> 3));
+	struct surface *t = vga_target();
+	unsigned char *fb = t->px;
+	unsigned int pitch = t->pitch;
+	unsigned int bpp = t->bpp;
+	unsigned int fg32, bg32;
+	unsigned short fg16, bg16;
+
+	if (!fb)
+		return;
+	/* Foreground pixels are opaque; the background takes the target's
+	 * alpha so a window layer can composite with a translucent fill. */
+	fg32 = ((r_fg << 16) | (g_fg << 8) | b_fg) | 0xFF000000u;
+	bg32 = ((r_bg << 16) | (g_bg << 8) | b_bg) | ((unsigned int)t->alpha << 24);
+	fg16 = (unsigned short)(((r_fg >> 3) << 11) | ((g_fg >> 2) << 5) | (b_fg >> 3));
+	bg16 = (unsigned short)(((r_bg >> 3) << 11) | ((g_bg >> 2) << 5) | (b_bg >> 3));
 
 
 	for (y = 0; y < height; y++) {
 	for (y = 0; y < height; y++) {
 		unsigned char row_bits = (y < FONT_8X8_ROWS) ? glyph[y] : 0;
 		unsigned char row_bits = (y < FONT_8X8_ROWS) ? glyph[y] : 0;
@@ -191,7 +199,7 @@ void fbtext_draw_cells(const unsigned short *cells, unsigned int cols, unsigned
 				rf, gf, bf, rb, gb, bb, ROW_HEIGHT, 0);
 				rf, gf, bf, rb, gb, bb, ROW_HEIGHT, 0);
 		}
 		}
 	}
 	}
-	vga_fb_mark_dirty_rect_xy(y0, rows * ROW_HEIGHT, x0, x0 + cols * 8);
+	vga_target_damage(x0, y0, cols * 8, rows * ROW_HEIGHT);
 }
 }
 
 
 void fbtext_draw_cells_clip(const unsigned short *cells, unsigned int cols, unsigned int rows,
 void fbtext_draw_cells_clip(const unsigned short *cells, unsigned int cols, unsigned int rows,
@@ -224,16 +232,18 @@ void fbtext_draw_cells_clip(const unsigned short *cells, unsigned int cols, unsi
 				cx, cy, cx + cw, cy + ch);
 				cx, cy, cx + cw, cy + ch);
 		}
 		}
 	}
 	}
-	vga_fb_mark_dirty_rect_xy(y0 + (unsigned int)row0 * ROW_HEIGHT,
-		(unsigned int)(row1 - row0) * ROW_HEIGHT,
-		x0 + (unsigned int)col0 * 8, x0 + (unsigned int)col1 * 8);
+	vga_target_damage(x0 + (unsigned int)col0 * 8,
+		y0 + (unsigned int)row0 * ROW_HEIGHT,
+		(unsigned int)(col1 - col0) * 8,
+		(unsigned int)(row1 - row0) * ROW_HEIGHT);
 }
 }
 
 
 void fbtext_scroll_up_one_line(void)
 void fbtext_scroll_up_one_line(void)
 {
 {
 	unsigned int y, i;
 	unsigned int y, i;
-	unsigned int pitch = vga_fb_pitch();
-	unsigned char *fb = (unsigned char *)vga_fb_ptr();
+	struct surface *scr = vga_screen_surface();
+	unsigned int pitch = scr->pitch;
+	unsigned char *fb = scr->px;
 	unsigned int height = vga_fb_height();
 	unsigned int height = vga_fb_height();
 
 
 	if (!fb)
 	if (!fb)
@@ -347,7 +357,7 @@ void fbtext_draw_string_xy(unsigned int x0, unsigned int y0, const char *str,
 		draw_glyph_at(x0 + i * 8, y0, (unsigned char)str[i], r_fg, g_fg, b_fg, 0, 0, 0, FONT_8X8_ROWS, transparent_bg);
 		draw_glyph_at(x0 + i * 8, y0, (unsigned char)str[i], r_fg, g_fg, b_fg, 0, 0, 0, FONT_8X8_ROWS, transparent_bg);
 		i++;
 		i++;
 	}
 	}
-	vga_fb_mark_dirty_rect_xy(y0, FONT_8X8_ROWS, x0, x0 + i * 8);
+	vga_target_damage(x0, y0, i * 8, FONT_8X8_ROWS);
 }
 }
 
 
 /* Like fbtext_draw_string_xy, but per-pixel clipped to (cx, cy, cw, ch): only
 /* Like fbtext_draw_string_xy, but per-pixel clipped to (cx, cy, cw, ch): only
@@ -381,7 +391,7 @@ void fbtext_draw_string_xy_clip(unsigned int x0, unsigned int y0, const char *st
 					vga_put_pixel_rgb(x, py, r_fg, g_fg, b_fg);
 					vga_put_pixel_rgb(x, py, r_fg, g_fg, b_fg);
 		}
 		}
 	}
 	}
-	vga_fb_mark_dirty_rect_xy(cy, ch, cx, cx + cw);
+	vga_target_damage(cx, cy, cw, ch);
 }
 }
 
 
 #define RAINBOW_LINE_HEIGHT  2   /* 2-pixel tall line for visibility */
 #define RAINBOW_LINE_HEIGHT  2   /* 2-pixel tall line for visibility */

+ 22 - 21
gfx.c

@@ -47,9 +47,10 @@ void gfx_free(struct gfx_canvas *c)
 	c->seq = c->drawn = 0;
 	c->seq = c->drawn = 0;
 }
 }
 
 
-/* One row of RGB565 -> the shadow buffer's pixel format.  Byte order mirrors
+/* One row of RGB565 -> the target's pixel format.  Byte order mirrors
  * fbtext.c's draw_glyph_at(); the 8-bit values replicate the high bits of the
  * fbtext.c's draw_glyph_at(); the 8-bit values replicate the high bits of the
- * 5/6-bit fields so black/white and the EGA palette stay exact. */
+ * 5/6-bit fields so black/white and the EGA palette stay exact.  Canvas
+ * pixels are opaque, so the 32 bpp high byte is 0xFF. */
 static void blit_row(unsigned char *dst, const unsigned short *src,
 static void blit_row(unsigned char *dst, const unsigned short *src,
 		     unsigned int n, unsigned int bpp)
 		     unsigned int n, unsigned int bpp)
 {
 {
@@ -79,7 +80,8 @@ static void blit_row(unsigned char *dst, const unsigned short *src,
 			unsigned int g6 = (p >> 5) & 0x3F;
 			unsigned int g6 = (p >> 5) & 0x3F;
 			unsigned int b5 = p & 0x1F;
 			unsigned int b5 = p & 0x1F;
 
 
-			d[x] = ((((r5 << 3) | (r5 >> 2))) << 16) |
+			d[x] = 0xFF000000u |
+			       ((((r5 << 3) | (r5 >> 2))) << 16) |
 			       ((((g6 << 2) | (g6 >> 4))) << 8) |
 			       ((((g6 << 2) | (g6 >> 4))) << 8) |
 			       ((b5 << 3) | (b5 >> 2));
 			       ((b5 << 3) | (b5 >> 2));
 		}
 		}
@@ -88,32 +90,30 @@ static void blit_row(unsigned char *dst, const unsigned short *src,
 
 
 void gfx_blit(const struct gfx_canvas *c, unsigned int x0, unsigned int y0)
 void gfx_blit(const struct gfx_canvas *c, unsigned int x0, unsigned int y0)
 {
 {
-	unsigned char *fb = (unsigned char *)vga_fb_ptr();
-	unsigned int pitch = vga_fb_pitch();
-	unsigned int bpp = vga_fb_bpp();
-	unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
+	struct surface *t = vga_target();
+	unsigned int pb = (t->bpp == 16) ? 2 : (t->bpp == 24) ? 3 : 4;
 	unsigned int y;
 	unsigned int y;
 
 
-	if (!c->px || !fb)
+	if (!c->px || !t->px)
+		return;
+	if (y0 + c->h > t->h || x0 + c->w > t->w)
 		return;
 		return;
 	for (y = 0; y < c->h; y++)
 	for (y = 0; y < c->h; y++)
-		blit_row(fb + (unsigned long)(y0 + y) * pitch + (unsigned long)x0 * pb,
-			 c->px + (unsigned long)y * c->w, c->w, bpp);
-	vga_fb_mark_dirty_rect_xy(y0, c->h, x0, x0 + c->w);
+		blit_row(t->px + (unsigned long)(y0 + y) * t->pitch + (unsigned long)x0 * pb,
+			 c->px + (unsigned long)y * c->w, c->w, t->bpp);
+	vga_target_damage(x0, y0, c->w, c->h);
 }
 }
 
 
 void gfx_blit_clip(const struct gfx_canvas *c, unsigned int x0, unsigned int y0,
 void gfx_blit_clip(const struct gfx_canvas *c, unsigned int x0, unsigned int y0,
 		   unsigned int cx, unsigned int cy, unsigned int cw, unsigned int ch)
 		   unsigned int cx, unsigned int cy, unsigned int cw, unsigned int ch)
 {
 {
-	unsigned char *fb = (unsigned char *)vga_fb_ptr();
-	unsigned int pitch = vga_fb_pitch();
-	unsigned int bpp = vga_fb_bpp();
-	unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
+	struct surface *t = vga_target();
+	unsigned int pb = (t->bpp == 16) ? 2 : (t->bpp == 24) ? 3 : 4;
 	int dx0 = (int)x0, dy0 = (int)y0;
 	int dx0 = (int)x0, dy0 = (int)y0;
 	int sx0 = 0, sy0 = 0, sx1 = (int)c->w, sy1 = (int)c->h;
 	int sx0 = 0, sy0 = 0, sx1 = (int)c->w, sy1 = (int)c->h;
 	int y;
 	int y;
 
 
-	if (!c->px || !fb)
+	if (!c->px || !t->px)
 		return;
 		return;
 
 
 	/* Canvas coords of the clip rect (signed: the rect may miss entirely). */
 	/* Canvas coords of the clip rect (signed: the rect may miss entirely). */
@@ -129,11 +129,12 @@ void gfx_blit_clip(const struct gfx_canvas *c, unsigned int x0, unsigned int y0,
 		return;
 		return;
 
 
 	for (y = sy0; y < sy1; y++)
 	for (y = sy0; y < sy1; y++)
-		blit_row(fb + (unsigned long)(y0 + (unsigned int)y) * pitch +
+		blit_row(t->px + (unsigned long)(y0 + (unsigned int)y) * t->pitch +
 				(unsigned long)(x0 + (unsigned int)sx0) * pb,
 				(unsigned long)(x0 + (unsigned int)sx0) * pb,
 			 c->px + (unsigned long)y * c->w + sx0,
 			 c->px + (unsigned long)y * c->w + sx0,
-			 (unsigned int)(sx1 - sx0), bpp);
-	vga_fb_mark_dirty_rect_xy(y0 + (unsigned int)sy0,
-				  (unsigned int)(sy1 - sy0),
-				  x0 + (unsigned int)sx0, x0 + (unsigned int)sx1);
+			 (unsigned int)(sx1 - sx0), t->bpp);
+	vga_target_damage(x0 + (unsigned int)sx0,
+			  y0 + (unsigned int)sy0,
+			  (unsigned int)(sx1 - sx0),
+			  (unsigned int)(sy1 - sy0));
 }
 }

+ 17 - 1
process.c

@@ -28,7 +28,7 @@ extern char __boot_stack_top[];
 
 
 /* switch.S addresses the table entries with an immediate PCB_SIZE/PCB_CPU_TICKS
 /* switch.S addresses the table entries with an immediate PCB_SIZE/PCB_CPU_TICKS
  * and cannot see this struct; keep the two in sync. */
  * and cannot see this struct; keep the two in sync. */
-_Static_assert(sizeof(struct process) == 116,
+_Static_assert(sizeof(struct process) == 120,
 	       "struct process layout changed: update PCB_SIZE/PCB_CPU_TICKS in switch.S");
 	       "struct process layout changed: update PCB_SIZE/PCB_CPU_TICKS in switch.S");
 
 
 /* ---- interrupt flag save/restore (for short critical sections) ---- */
 /* ---- interrupt flag save/restore (for short critical sections) ---- */
@@ -438,6 +438,22 @@ void process_set_screen(struct text_screen *screen)
 	procs[current_pid].screen = screen;
 	procs[current_pid].screen = screen;
 }
 }
 
 
+/* Render target override for the running process (0 = the screen).  Lives in
+ * the PCB so a preempted layer draw can never leak into another process. */
+struct surface *process_render_target(void)
+{
+	if (!procs || current_pid >= proc_cap)
+		return 0;
+	return procs[current_pid].target;
+}
+
+void process_set_render_target(struct surface *s)
+{
+	if (!procs || current_pid >= proc_cap)
+		return;
+	procs[current_pid].target = s;
+}
+
 void process_map_mmio_all(unsigned int phys_base, unsigned int size)
 void process_map_mmio_all(unsigned int phys_base, unsigned int size)
 {
 {
 	unsigned int i, kernel_cr3 = paging_current_cr3();
 	unsigned int i, kernel_cr3 = paging_current_cr3();

+ 8 - 0
process.h

@@ -22,6 +22,7 @@
 
 
 struct text_screen;  /* forward decl (see screen.h) */
 struct text_screen;  /* forward decl (see screen.h) */
 struct sys_procinfo; /* forward decl (see syscall.h; `top` snapshot) */
 struct sys_procinfo; /* forward decl (see syscall.h; `top` snapshot) */
+struct surface;      /* forward decl (see vga.h; render target override) */
 
 
 struct process {
 struct process {
 	unsigned int esp;
 	unsigned int esp;
@@ -39,6 +40,10 @@ struct process {
 	 * PCB_SIZE there (see the _Static_assert in process.c). */
 	 * PCB_SIZE there (see the _Static_assert in process.c). */
 	unsigned int cpu_ticks;      /* ticks spent as the running process */
 	unsigned int cpu_ticks;      /* ticks spent as the running process */
 	unsigned int start_ticks;    /* get_ticks_ms() at creation */
 	unsigned int start_ticks;    /* get_ticks_ms() at creation */
+	/* Render target override: 0 = the screen.  Set while the window manager
+	 * draws into a layer, so another process's drawing can never land in
+	 * it.  Kept last so the PIT hot path offsets stay put. */
+	struct surface *target;
 };
 };
 
 
 /* User-mode virtual address layout (ring-3 processes). */
 /* User-mode virtual address layout (ring-3 processes). */
@@ -62,6 +67,9 @@ int  process_create_ptr(const char *name, void (*entry)(void *), void *arg);
 int process_create_user(const char *name, const void *image, unsigned int size,
 int process_create_user(const char *name, const void *image, unsigned int size,
 			const char *arg, const char *cwd);
 			const char *arg, const char *cwd);
 void process_set_screen(struct text_screen *screen);
 void process_set_screen(struct text_screen *screen);
+/* Render target override for the running process (0 = the screen). */
+struct surface *process_render_target(void);
+void process_set_render_target(struct surface *s);
 
 
 /* Map an MMIO range into the kernel's page directory and every live process
 /* Map an MMIO range into the kernel's page directory and every live process
  * copy (and thus every process created later, which clones the kernel map).
  * copy (and thus every process created later, which clones the kernel map).

+ 1 - 1
switch.S

@@ -30,7 +30,7 @@
 .equ PCB_CR3,   32
 .equ PCB_CR3,   32
 .equ PCB_KSTACK_TOP, 36
 .equ PCB_KSTACK_TOP, 36
 .equ PCB_CPU_TICKS, 108  /* after cwd[64] */
 .equ PCB_CPU_TICKS, 108  /* after cwd[64] */
-.equ PCB_SIZE,  116     /* sizeof(struct process): 44 + cwd[64] + 2*4 */
+.equ PCB_SIZE,  120     /* sizeof(struct process): 44 + cwd[64] + 2*4 + target */
 
 
 .equ TIMESLICE, 20
 .equ TIMESLICE, 20
 
 

+ 88 - 94
vga.c

@@ -1,9 +1,12 @@
 /* VGA: VESA LFB (from boot) with a shadow buffer and a damage-rect flush. */
 /* VGA: VESA LFB (from boot) with a shadow buffer and a damage-rect flush. */
 
 
 #include "vga.h"
 #include "vga.h"
+#include "blit.h"
 #include "pci.h"
 #include "pci.h"
 #include "gpu.h"
 #include "gpu.h"
 #include "ati.h"
 #include "ati.h"
+#include "process.h"
+#include "serial.h"
 
 
 /* Cached boot framebuffer info (set once when vga_fb_* first used). */
 /* Cached boot framebuffer info (set once when vga_fb_* first used). */
 static unsigned int vga_fb_w, vga_fb_h, vga_fb_pitch_val, vga_fb_bpp_val;
 static unsigned int vga_fb_w, vga_fb_h, vga_fb_pitch_val, vga_fb_bpp_val;
@@ -284,16 +287,65 @@ int vga_has_vesa(void)
 	return (vga_fb_w != 0 && vga_fb_lfb != 0);
 	return (vga_fb_w != 0 && vga_fb_lfb != 0);
 }
 }
 
 
+/* ---- Surfaces and the render target ---- */
+
+/* The screen surface: the shadow buffer (or the LFB when there is none).
+ * Kept in sync by vga_shadow_init() invalidating it. */
+static struct surface screen_surf;
+
+struct surface *vga_screen_surface(void)
+{
+	vga_cache_boot_fb();
+	if (!screen_surf.px) {
+		screen_surf.w = vga_fb_w;
+		screen_surf.h = vga_fb_h;
+		screen_surf.pitch = vga_shadow_buf ? vga_shadow_pitch
+						   : vga_fb_pitch_val;
+		screen_surf.bpp = vga_fb_bpp_val;
+		screen_surf.alpha = 255;
+		screen_surf.px = vga_shadow_buf ? vga_shadow_buf
+						: (unsigned char *)vga_fb_lfb;
+	}
+	return &screen_surf;
+}
+
+struct surface *vga_target(void)
+{
+	struct surface *t = process_render_target();
+
+	return t ? t : vga_screen_surface();
+}
+
+void vga_set_target(struct surface *s)
+{
+	process_set_render_target(s);
+}
+
+int vga_target_is_screen(void)
+{
+	return vga_target() == vga_screen_surface();
+}
+
+void vga_target_damage(unsigned int x, unsigned int y, unsigned int w,
+		       unsigned int h)
+{
+	if (w == 0 || h == 0 || !vga_target_is_screen())
+		return;
+	mark_dirty_rect_xy(y, h, x, x + w);
+}
+
 /* 16bpp: RGB 5:6:5.  32bpp: BGRX byte order (standard VBE / QEMU). */
 /* 16bpp: RGB 5:6:5.  32bpp: BGRX byte order (standard VBE / QEMU). */
-static void shadow_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
+static void target_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
 {
 {
-	unsigned int pitch = vga_shadow_pitch;
-	unsigned int bpp = vga_fb_bpp_val;
+	struct surface *t = vga_target();
+	unsigned int pitch, bpp;
 	unsigned char *p;
 	unsigned char *p;
 
 
-	if (x >= vga_fb_w || y >= vga_fb_h || !vga_shadow_buf)
+	if (!t->px || x >= t->w || y >= t->h)
 		return;
 		return;
-	p = vga_shadow_buf + (unsigned long)y * pitch;
+	pitch = t->pitch;
+	bpp = t->bpp;
+	p = t->px + (unsigned long)y * pitch;
 	if (bpp == 16) {
 	if (bpp == 16) {
 		unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
 		unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
 		unsigned int off = x * 2;
 		unsigned int off = x * 2;
@@ -309,70 +361,24 @@ static void shadow_put_pixel(unsigned int x, unsigned int y, unsigned int r, uns
 		p[off]     = (unsigned char)b;
 		p[off]     = (unsigned char)b;
 		p[off + 1] = (unsigned char)g;
 		p[off + 1] = (unsigned char)g;
 		p[off + 2] = (unsigned char)r;
 		p[off + 2] = (unsigned char)r;
-		p[off + 3] = 0;
+		p[off + 3] = t->alpha;
 	}
 	}
-	mark_dirty_rect_xy(y, 1, x, x + 1);
+	vga_target_damage(x, y, 1, 1);
 }
 }
 
 
 void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
 void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
 {
 {
 	vga_cache_boot_fb();
 	vga_cache_boot_fb();
-	if (!vga_shadow_buf)
-		return;
-	shadow_put_pixel(x, y, r, g, b);
+	target_put_pixel(x, y, r, g, b);
 }
 }
 
 
 void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
 void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
 		unsigned int r, unsigned int g, unsigned int b)
 		unsigned int r, unsigned int g, unsigned int b)
 {
 {
-	unsigned int row, x1, y1;
-	unsigned int pitch;
-	unsigned int bpp;
-	unsigned char *fb;
-
 	vga_cache_boot_fb();
 	vga_cache_boot_fb();
-	pitch = vga_shadow_pitch;
-	bpp = vga_fb_bpp_val;
-	fb = vga_shadow_buf;
-
-	if (!fb || w == 0 || h == 0)
-		return;
-	if (x >= vga_fb_w || y >= vga_fb_h)
+	if (!blit || !blit->fill)
 		return;
 		return;
-	x1 = x + w;
-	y1 = y + h;
-	if (x1 > vga_fb_w) x1 = vga_fb_w;
-	if (y1 > vga_fb_h) y1 = vga_fb_h;
-
-	if (bpp == 16) {
-		unsigned short val = (unsigned short)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
-		for (row = y; row < y1; row++) {
-			unsigned short *p = (unsigned short *)(fb + (unsigned long)row * pitch + x * 2);
-			unsigned int xx;
-			for (xx = x; xx < x1; xx++)
-				*p++ = val;
-		}
-	} else if (bpp == 24) {
-		for (row = y; row < y1; row++) {
-			unsigned char *p = fb + (unsigned long)row * pitch + x * 3;
-			unsigned int xx;
-			for (xx = x; xx < x1; xx++) {
-				p[0] = (unsigned char)b;
-				p[1] = (unsigned char)g;
-				p[2] = (unsigned char)r;
-				p += 3;
-			}
-		}
-	} else {
-		unsigned int val = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
-		for (row = y; row < y1; row++) {
-			unsigned int *p = (unsigned int *)(fb + (unsigned long)row * pitch + x * 4);
-			unsigned int xx;
-			for (xx = x; xx < x1; xx++)
-				*p++ = val;
-		}
-	}
-	mark_dirty_rect_xy(y, y1 - y, x, x1);
+	blit->fill(x, y, w, h, ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF));
 }
 }
 
 
 static unsigned int vga_pixel_bytes(void)
 static unsigned int vga_pixel_bytes(void)
@@ -384,6 +390,8 @@ static unsigned int vga_pixel_bytes(void)
 	return 4;
 	return 4;
 }
 }
 
 
+/* The software cursor saves and restores pixels of the *screen*: these two
+ * keep using the shadow buffer directly, never the render target. */
 void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
 void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
 		unsigned char *buf)
 		unsigned char *buf)
 {
 {
@@ -430,42 +438,13 @@ void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned
 
 
 void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
 void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
 {
 {
-	unsigned int pitch = vga_shadow_pitch;
-	unsigned int bpp = vga_fb_bpp_val;
+	struct surface *t;
 
 
 	vga_cache_boot_fb();
 	vga_cache_boot_fb();
-	if (!vga_shadow_buf)
+	t = vga_target();
+	if (!t->px)
 		return;
 		return;
-	if (bpp == 16) {
-		unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
-		unsigned short val = (unsigned short)((rgb16 & 0xFF) | ((rgb16 >> 8) << 8));
-		unsigned short *fb = (unsigned short *)vga_shadow_buf;
-		unsigned int n = (pitch / 2) * vga_fb_h;
-		unsigned int i;
-		for (i = 0; i < n; i++)
-			fb[i] = val;
-	} else if (bpp == 24) {
-		unsigned char *p = vga_shadow_buf;
-		unsigned int row, x;
-		for (row = 0; row < vga_fb_h; row++) {
-			unsigned int row_off = row * pitch;
-			for (x = 0; x < vga_fb_w; x++) {
-				unsigned int off = row_off + x * 3;
-				p[off]     = (unsigned char)b;
-				p[off + 1] = (unsigned char)g;
-				p[off + 2] = (unsigned char)r;
-			}
-		}
-	} else if (bpp == 32) {
-		unsigned int *fb = (unsigned int *)vga_shadow_buf;
-		unsigned int pixel = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
-		unsigned int stride = pitch / 4;
-		unsigned int row, x;
-		for (row = 0; row < vga_fb_h; row++)
-			for (x = 0; x < vga_fb_w; x++)
-				fb[row * stride + x] = pixel;
-	}
-	mark_dirty_rect(0, vga_fb_h);
+	vga_fill_rect(0, 0, t->w, t->h, r, g, b);
 }
 }
 
 
 /* ---- Shadow buffer init and flush ---- */
 /* ---- Shadow buffer init and flush ---- */
@@ -482,7 +461,12 @@ void vga_shadow_init(void)
 	vga_shadow_buf = (unsigned char *)VGA_SHADOW_BASE;
 	vga_shadow_buf = (unsigned char *)VGA_SHADOW_BASE;
 	vga_shadow_pitch = vga_fb_pitch_val;
 	vga_shadow_pitch = vga_fb_pitch_val;
 	vga_shadow_size = vga_fb_h * vga_shadow_pitch;
 	vga_shadow_size = vga_fb_h * vga_shadow_pitch;
+	screen_surf.px = 0;       /* re-derive from the shadow buffer */
 	damage_clear();
 	damage_clear();
+
+	serial_puts(SERIAL_COM1, "blit backend: ");
+	serial_puts(SERIAL_COM1, blit ? blit->name : "none");
+	serial_puts(SERIAL_COM1, "\r\n");
 }
 }
 
 
 /* Copy damage rects from the shadow to VRAM, at most about `max_bytes` this
 /* Copy damage rects from the shadow to VRAM, at most about `max_bytes` this
@@ -552,11 +536,11 @@ static unsigned int shadow_copy_damage(unsigned int max_bytes)
 	return bytes;
 	return bytes;
 }
 }
 
 
-/* Copy up to `max_bytes` of the pending damage to VRAM.  Called from the
- * main loop on every iteration: small updates (a cursor move is ~1 KB) reach
- * the screen within one loop instead of waiting for a fixed 30 Hz tick, while
- * a full-screen repaint is chunked so it cannot stall input. */
-void vga_fb_flush_budget(unsigned int max_bytes)
+/* Copy up to `max_bytes` of the pending damage to VRAM.  Called by the main
+ * loop on every iteration: small updates (a cursor move is ~1 KB) reach the
+ * screen within one loop instead of waiting for a fixed 30 Hz tick, while a
+ * full-screen repaint is chunked so it cannot stall input. */
+void vga_present_shadow(unsigned int max_bytes)
 {
 {
 	vga_cache_boot_fb();
 	vga_cache_boot_fb();
 	if (!vga_shadow_buf || !vga_fb_lfb)
 	if (!vga_shadow_buf || !vga_fb_lfb)
@@ -566,6 +550,16 @@ void vga_fb_flush_budget(unsigned int max_bytes)
 	shadow_copy_damage(max_bytes);
 	shadow_copy_damage(max_bytes);
 }
 }
 
 
+/* Make drawn content visible: the backend's present() (the software one copies
+ * the pending damage rects to VRAM). */
+void vga_fb_flush_budget(unsigned int max_bytes)
+{
+	vga_cache_boot_fb();
+	if (!blit || !blit->present)
+		return;
+	blit->present(max_bytes);
+}
+
 void vga_fb_flush(void)
 void vga_fb_flush(void)
 {
 {
 	vga_fb_flush_budget(0);
 	vga_fb_flush_budget(0);

+ 35 - 1
vga.h

@@ -22,7 +22,8 @@ struct vga_boot_fb_info {
 #define VGA_SHADOW_BASE 0x100000
 #define VGA_SHADOW_BASE 0x100000
 
 
 /* LFB API. RGB 0..255. All drawing goes into the shadow buffer; the main loop
 /* LFB API. RGB 0..255. All drawing goes into the shadow buffer; the main loop
- * flushes dirty areas to VRAM on every iteration (byte-budgeted). */
+ * flushes dirty areas to VRAM on every iteration (byte-budgeted).  These
+ * describe the *screen*, not the current render target (see below). */
 unsigned int vga_fb_width(void);
 unsigned int vga_fb_width(void);
 unsigned int vga_fb_height(void);
 unsigned int vga_fb_height(void);
 unsigned int vga_fb_pitch(void);
 unsigned int vga_fb_pitch(void);
@@ -34,6 +35,39 @@ void *vga_fb_ptr(void);
 unsigned long vga_fb_lfb_addr(void);
 unsigned long vga_fb_lfb_addr(void);
 int vga_has_vesa(void);
 int vga_has_vesa(void);
 
 
+/* A drawing surface: the screen's shadow buffer, a window layer or a canvas.
+ * Drawing helpers paint into the *current render target*, which is the screen
+ * unless process_set_render_target() pointed this process at a surface. */
+struct surface {
+	unsigned char *px;
+	unsigned int w, h;
+	unsigned int pitch;
+	unsigned int bpp;             /* 16, 24 or 32 */
+	/* Alpha written into the high byte of 32 bpp pixels.  The screen uses
+	 * 255 (the byte is padding there); a window layer sets what its
+	 * background fill should composite with, while glyph foregrounds are
+	 * always written opaque. */
+	unsigned char alpha;
+};
+
+/* The screen surface (shadow buffer, or the LFB when there is no shadow). */
+struct surface *vga_screen_surface(void);
+
+/* Current render target for this process: the screen by default. */
+struct surface *vga_target(void);
+void vga_set_target(struct surface *s);
+int vga_target_is_screen(void);
+
+/* Mark a rectangle of the screen dirty.  A no-op while the target is not the
+ * screen: drawing into a layer must not touch the screen's damage list, the
+ * compositor marks the rects it copies out. */
+void vga_target_damage(unsigned int x, unsigned int y, unsigned int w,
+		       unsigned int h);
+
+/* The software presenter used by the blit backend: copies up to max_bytes of
+ * the pending damage rects from the shadow buffer to VRAM. */
+void vga_present_shadow(unsigned int max_bytes);
+
 void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b);
 void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b);
 void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
 void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
 		unsigned int r, unsigned int g, unsigned int b);
 		unsigned int r, unsigned int g, unsigned int b);

+ 5 - 80
wm.c

@@ -2,7 +2,7 @@
    Slot 0 runs wm_run(); each terminal window is a separate process. */
    Slot 0 runs wm_run(); each terminal window is a separate process. */
 
 
 #include "wm.h"
 #include "wm.h"
-#include "cirrus.h"
+#include "blit.h"
 #include "fbtext.h"
 #include "fbtext.h"
 #include "idle.h"
 #include "idle.h"
 #include "io.h"
 #include "io.h"
@@ -230,68 +230,6 @@ static void wm_clamp_window(struct window *w, int nx, int ny)
 	w->y = (unsigned int)ny;
 	w->y = (unsigned int)ny;
 }
 }
 
 
-/* Overlap-safe copy.  Moving a window copies most of its area, so take four
-   bytes at a time when both rows are dword aligned (32 bpp rows always are)
-   and fall back to bytes otherwise (24 bpp at an odd x). */
-static void wm_memmove(unsigned char *dst, const unsigned char *src, unsigned int n)
-{
-	if (dst == src || n == 0)
-		return;
-
-	if ((((unsigned int)dst | (unsigned int)src) & 3) == 0) {
-		unsigned int words = n >> 2;
-		unsigned int i;
-		unsigned int *dw = (unsigned int *)dst;
-		const unsigned int *sw = (const unsigned int *)src;
-
-		if (dst < src) {
-			for (i = 0; i < words; i++)
-				dw[i] = sw[i];
-			for (i = words << 2; i < n; i++)
-				dst[i] = src[i];
-		} else {
-			/* Overlapping move down: back to front. */
-			for (i = n; i > (words << 2); i--)
-				dst[i - 1] = src[i - 1];
-			for (i = words; i-- > 0; )
-				dw[i] = sw[i];
-		}
-		return;
-	}
-
-	if (dst < src) {
-		unsigned int i;
-		for (i = 0; i < n; i++)
-			dst[i] = src[i];
-	} else {
-		while (n--)
-			dst[n] = src[n];
-	}
-}
-
-/* Copy a rectangle within the shadow buffer (to mirror a BitBLT'd drag). */
-static void wm_shadow_copy(unsigned int ox, unsigned int oy,
-			   unsigned int nx, unsigned int ny,
-			   unsigned int w, unsigned int h)
-{
-	unsigned char *fb = (unsigned char *)vga_fb_ptr();
-	unsigned int pitch = vga_fb_pitch();
-	unsigned int bpp = vga_fb_bpp();
-	unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
-	unsigned int row_bytes = w * pb;
-	unsigned int r;
-
-	if (ny > oy || (ny == oy && nx > ox)) {
-		for (r = h; r-- > 0; )
-			wm_memmove(fb + (ny + r) * pitch + nx * pb,
-				   fb + (oy + r) * pitch + ox * pb, row_bytes);
-	} else {
-		for (r = 0; r < h; r++)
-			wm_memmove(fb + (ny + r) * pitch + nx * pb,
-				   fb + (oy + r) * pitch + ox * pb, row_bytes);
-	}
-}
-
 /* Fill the intersection of rect (rx,ry,rw,rh) with clip rect (cx,cy,cw,ch). */
 /* Fill the intersection of rect (rx,ry,rw,rh) with clip rect (cx,cy,cw,ch). */
 static void wm_fill_rect_clip(unsigned int rx, unsigned int ry, unsigned int rw, unsigned int rh,
 static void wm_fill_rect_clip(unsigned int rx, unsigned int ry, unsigned int rw, unsigned int rh,
 			      unsigned int cx, unsigned int cy, unsigned int cw, unsigned int ch,
 			      unsigned int cx, unsigned int cy, unsigned int cw, unsigned int ch,
@@ -352,7 +290,7 @@ static void wm_drag_shadow(struct window *w, unsigned int ox, unsigned int oy,
 	int dy = (int)ny - (int)oy;
 	int dy = (int)ny - (int)oy;
 
 
 	mouse_cursor_erase();
 	mouse_cursor_erase();
-	wm_shadow_copy(ox, oy, nx, ny, w->pw, w->ph);
+	blit->copy(nx, ny, ox, oy, w->pw, w->ph);
 
 
 	if (dy > 0)
 	if (dy > 0)
 		wm_redraw_rect(ox, oy, w->pw, (unsigned int)dy);
 		wm_redraw_rect(ox, oy, w->pw, (unsigned int)dy);
@@ -372,15 +310,6 @@ static void wm_drag_shadow(struct window *w, unsigned int ox, unsigned int oy,
 	vga_fb_flush();
 	vga_fb_flush();
 }
 }
 
 
-/* Cirrus fast drag: also move the window in VRAM with the BitBLT engine
-   (wm_drag_shadow flushes the mirrored copy in the shadow buffer). */
-static void wm_drag_fast(struct window *w, unsigned int ox, unsigned int oy,
-			 unsigned int nx, unsigned int ny)
-{
-	cirrus_blit(ox, oy, nx, ny, w->pw, w->ph);
-	wm_drag_shadow(w, ox, oy, nx, ny);
-}
-
 static void wm_handle_mouse(void)
 static void wm_handle_mouse(void)
 {
 {
 	unsigned int buttons = mouse_buttons_state();
 	unsigned int buttons = mouse_buttons_state();
@@ -417,12 +346,8 @@ static void wm_handle_mouse(void)
 		if (w->used) {
 		if (w->used) {
 			unsigned int ox = w->x, oy = w->y;
 			unsigned int ox = w->x, oy = w->y;
 			wm_clamp_window(w, (int)mx - drag_dx, (int)my - drag_dy);
 			wm_clamp_window(w, (int)mx - drag_dx, (int)my - drag_dy);
-			if (ox != w->x || oy != w->y) {
-				if (cirrus_available())
-					wm_drag_fast(w, ox, oy, w->x, w->y);
-				else
-					wm_drag_shadow(w, ox, oy, w->x, w->y);
-			}
+			if (ox != w->x || oy != w->y)
+				wm_drag_shadow(w, ox, oy, w->x, w->y);
 		} else {
 		} else {
 			drag_active = 0;
 			drag_active = 0;
 		}
 		}
@@ -543,7 +468,7 @@ static void wm_draw_window_clip(struct window *w, unsigned int cx, unsigned int
 	wm_fill_rect_clip(w->x + 1, cony, w->pw - 2, SCREEN_ROWS * 10,
 	wm_fill_rect_clip(w->x + 1, cony, w->pw - 2, SCREEN_ROWS * 10,
 		cx, cy, cw, ch, 0, 0, 0);
 		cx, cy, cw, ch, 0, 0, 0);
 	if (g)
 	if (g)
-		gfx_blit_clip(g, w->x + 1, cony, cx, cy, cw, ch);
+		blit->canvas(g, w->x + 1, cony, cx, cy, cw, ch);
 	else
 	else
 		fbtext_draw_cells_clip(s->cells, s->cols, s->rows,
 		fbtext_draw_cells_clip(s->cells, s->cols, s->rows,
 			w->x + 1, cony, cx, cy, cw, ch);
 			w->x + 1, cony, cx, cy, cw, ch);