1862b4a29SBALATON Zoltan /*
2862b4a29SBALATON Zoltan * QEMU ATI SVGA emulation
3862b4a29SBALATON Zoltan * 2D engine functions
4862b4a29SBALATON Zoltan *
5862b4a29SBALATON Zoltan * Copyright (c) 2019 BALATON Zoltan
6862b4a29SBALATON Zoltan *
7862b4a29SBALATON Zoltan * This work is licensed under the GNU GPL license version 2 or later.
8862b4a29SBALATON Zoltan */
9862b4a29SBALATON Zoltan
10bbfff196SMarkus Armbruster #include "qemu/osdep.h"
11862b4a29SBALATON Zoltan #include "ati_int.h"
12862b4a29SBALATON Zoltan #include "ati_regs.h"
13862b4a29SBALATON Zoltan #include "qemu/log.h"
14862b4a29SBALATON Zoltan #include "ui/pixel_ops.h"
1528cf3960SMichael S. Tsirkin #include "ui/console.h"
16862b4a29SBALATON Zoltan
17862b4a29SBALATON Zoltan /*
18862b4a29SBALATON Zoltan * NOTE:
19862b4a29SBALATON Zoltan * This is 2D _acceleration_ and supposed to be fast. Therefore, don't try to
20862b4a29SBALATON Zoltan * reinvent the wheel (unlikely to get better with a naive implementation than
21862b4a29SBALATON Zoltan * existing libraries) and avoid (poorly) reimplementing gfx primitives.
22862b4a29SBALATON Zoltan * That is unnecessary and would become a performance problem. Instead, try to
23862b4a29SBALATON Zoltan * map to and reuse existing optimised facilities (e.g. pixman) wherever
24862b4a29SBALATON Zoltan * possible.
25862b4a29SBALATON Zoltan */
26862b4a29SBALATON Zoltan
ati_bpp_from_datatype(ATIVGAState * s)27862b4a29SBALATON Zoltan static int ati_bpp_from_datatype(ATIVGAState *s)
28862b4a29SBALATON Zoltan {
29862b4a29SBALATON Zoltan switch (s->regs.dp_datatype & 0xf) {
30862b4a29SBALATON Zoltan case 2:
31862b4a29SBALATON Zoltan return 8;
32862b4a29SBALATON Zoltan case 3:
33862b4a29SBALATON Zoltan case 4:
34862b4a29SBALATON Zoltan return 16;
35862b4a29SBALATON Zoltan case 5:
36862b4a29SBALATON Zoltan return 24;
37862b4a29SBALATON Zoltan case 6:
38862b4a29SBALATON Zoltan return 32;
39862b4a29SBALATON Zoltan default:
40862b4a29SBALATON Zoltan qemu_log_mask(LOG_UNIMP, "Unknown dst datatype %d\n",
41862b4a29SBALATON Zoltan s->regs.dp_datatype & 0xf);
42862b4a29SBALATON Zoltan return 0;
43862b4a29SBALATON Zoltan }
44862b4a29SBALATON Zoltan }
45862b4a29SBALATON Zoltan
46c799d2eeSBALATON Zoltan #define DEFAULT_CNTL (s->regs.dp_gui_master_cntl & GMC_DST_PITCH_OFFSET_CNTL)
47c799d2eeSBALATON Zoltan
ati_2d_blt(ATIVGAState * s)48862b4a29SBALATON Zoltan void ati_2d_blt(ATIVGAState *s)
49862b4a29SBALATON Zoltan {
50862b4a29SBALATON Zoltan /* FIXME it is probably more complex than this and may need to be */
51862b4a29SBALATON Zoltan /* rewritten but for now as a start just to get some output: */
52862b4a29SBALATON Zoltan DisplaySurface *ds = qemu_console_surface(s->vga.con);
53862b4a29SBALATON Zoltan DPRINTF("%p %u ds: %p %d %d rop: %x\n", s->vga.vram_ptr,
54862b4a29SBALATON Zoltan s->vga.vbe_start_addr, surface_data(ds), surface_stride(ds),
55862b4a29SBALATON Zoltan surface_bits_per_pixel(ds),
56862b4a29SBALATON Zoltan (s->regs.dp_mix & GMC_ROP3_MASK) >> 16);
57ac2071c3SBALATON Zoltan unsigned dst_x = (s->regs.dp_cntl & DST_X_LEFT_TO_RIGHT ?
58584acf34SBALATON Zoltan s->regs.dst_x : s->regs.dst_x + 1 - s->regs.dst_width);
59ac2071c3SBALATON Zoltan unsigned dst_y = (s->regs.dp_cntl & DST_Y_TOP_TO_BOTTOM ?
60584acf34SBALATON Zoltan s->regs.dst_y : s->regs.dst_y + 1 - s->regs.dst_height);
61c799d2eeSBALATON Zoltan int bpp = ati_bpp_from_datatype(s);
62ac2071c3SBALATON Zoltan if (!bpp) {
63ac2071c3SBALATON Zoltan qemu_log_mask(LOG_GUEST_ERROR, "Invalid bpp\n");
64ac2071c3SBALATON Zoltan return;
65ac2071c3SBALATON Zoltan }
66c799d2eeSBALATON Zoltan int dst_stride = DEFAULT_CNTL ? s->regs.dst_pitch : s->regs.default_pitch;
67ac2071c3SBALATON Zoltan if (!dst_stride) {
68ac2071c3SBALATON Zoltan qemu_log_mask(LOG_GUEST_ERROR, "Zero dest pitch\n");
69ac2071c3SBALATON Zoltan return;
70ac2071c3SBALATON Zoltan }
71c799d2eeSBALATON Zoltan uint8_t *dst_bits = s->vga.vram_ptr + (DEFAULT_CNTL ?
72c799d2eeSBALATON Zoltan s->regs.dst_offset : s->regs.default_offset);
73c799d2eeSBALATON Zoltan
74c799d2eeSBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
75c799d2eeSBALATON Zoltan dst_bits += s->regs.crtc_offset & 0x07ffffff;
76c799d2eeSBALATON Zoltan dst_stride *= bpp;
77c799d2eeSBALATON Zoltan }
78c799d2eeSBALATON Zoltan uint8_t *end = s->vga.vram_ptr + s->vga.vram_size;
79ca1f9cbfSPrasad J Pandit if (dst_x > 0x3fff || dst_y > 0x3fff || dst_bits >= end
80ca1f9cbfSPrasad J Pandit || dst_bits + dst_x
81ca1f9cbfSPrasad J Pandit + (dst_y + s->regs.dst_height) * dst_stride >= end) {
82c799d2eeSBALATON Zoltan qemu_log_mask(LOG_UNIMP, "blt outside vram not implemented\n");
83c799d2eeSBALATON Zoltan return;
84c799d2eeSBALATON Zoltan }
85584acf34SBALATON Zoltan DPRINTF("%d %d %d, %d %d %d, (%d,%d) -> (%d,%d) %dx%d %c %c\n",
86866ad5f5SBALATON Zoltan s->regs.src_offset, s->regs.dst_offset, s->regs.default_offset,
87866ad5f5SBALATON Zoltan s->regs.src_pitch, s->regs.dst_pitch, s->regs.default_pitch,
88205ccfd7SPhilippe Mathieu-Daudé s->regs.src_x, s->regs.src_y, dst_x, dst_y,
89584acf34SBALATON Zoltan s->regs.dst_width, s->regs.dst_height,
90584acf34SBALATON Zoltan (s->regs.dp_cntl & DST_X_LEFT_TO_RIGHT ? '>' : '<'),
91584acf34SBALATON Zoltan (s->regs.dp_cntl & DST_Y_TOP_TO_BOTTOM ? 'v' : '^'));
92862b4a29SBALATON Zoltan switch (s->regs.dp_mix & GMC_ROP3_MASK) {
93862b4a29SBALATON Zoltan case ROP3_SRCCOPY:
94862b4a29SBALATON Zoltan {
9508730ee0SBALATON Zoltan bool fallback = false;
96ac2071c3SBALATON Zoltan unsigned src_x = (s->regs.dp_cntl & DST_X_LEFT_TO_RIGHT ?
97584acf34SBALATON Zoltan s->regs.src_x : s->regs.src_x + 1 - s->regs.dst_width);
98ac2071c3SBALATON Zoltan unsigned src_y = (s->regs.dp_cntl & DST_Y_TOP_TO_BOTTOM ?
99584acf34SBALATON Zoltan s->regs.src_y : s->regs.src_y + 1 - s->regs.dst_height);
100c799d2eeSBALATON Zoltan int src_stride = DEFAULT_CNTL ?
101c799d2eeSBALATON Zoltan s->regs.src_pitch : s->regs.default_pitch;
102ac2071c3SBALATON Zoltan if (!src_stride) {
103ac2071c3SBALATON Zoltan qemu_log_mask(LOG_GUEST_ERROR, "Zero source pitch\n");
104ac2071c3SBALATON Zoltan return;
105ac2071c3SBALATON Zoltan }
106c799d2eeSBALATON Zoltan uint8_t *src_bits = s->vga.vram_ptr + (DEFAULT_CNTL ?
107866ad5f5SBALATON Zoltan s->regs.src_offset : s->regs.default_offset);
108862b4a29SBALATON Zoltan
109862b4a29SBALATON Zoltan if (s->dev_id == PCI_DEVICE_ID_ATI_RAGE128_PF) {
110862b4a29SBALATON Zoltan src_bits += s->regs.crtc_offset & 0x07ffffff;
111862b4a29SBALATON Zoltan src_stride *= bpp;
112862b4a29SBALATON Zoltan }
113ca1f9cbfSPrasad J Pandit if (src_x > 0x3fff || src_y > 0x3fff || src_bits >= end
114ca1f9cbfSPrasad J Pandit || src_bits + src_x
115ca1f9cbfSPrasad J Pandit + (src_y + s->regs.dst_height) * src_stride >= end) {
116c799d2eeSBALATON Zoltan qemu_log_mask(LOG_UNIMP, "blt outside vram not implemented\n");
117c799d2eeSBALATON Zoltan return;
118c799d2eeSBALATON Zoltan }
119c799d2eeSBALATON Zoltan
120862b4a29SBALATON Zoltan src_stride /= sizeof(uint32_t);
121862b4a29SBALATON Zoltan dst_stride /= sizeof(uint32_t);
122862b4a29SBALATON Zoltan DPRINTF("pixman_blt(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)\n",
123862b4a29SBALATON Zoltan src_bits, dst_bits, src_stride, dst_stride, bpp, bpp,
124584acf34SBALATON Zoltan src_x, src_y, dst_x, dst_y,
125862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height);
126*699f15fdSMarc-André Lureau #ifdef CONFIG_PIXMAN
12708730ee0SBALATON Zoltan if ((s->use_pixman & BIT(1)) &&
12808730ee0SBALATON Zoltan s->regs.dp_cntl & DST_X_LEFT_TO_RIGHT &&
129584acf34SBALATON Zoltan s->regs.dp_cntl & DST_Y_TOP_TO_BOTTOM) {
13008730ee0SBALATON Zoltan fallback = !pixman_blt((uint32_t *)src_bits, (uint32_t *)dst_bits,
131862b4a29SBALATON Zoltan src_stride, dst_stride, bpp, bpp,
132584acf34SBALATON Zoltan src_x, src_y, dst_x, dst_y,
133862b4a29SBALATON Zoltan s->regs.dst_width, s->regs.dst_height);
13408730ee0SBALATON Zoltan } else if (s->use_pixman & BIT(1)) {
135584acf34SBALATON Zoltan /* FIXME: We only really need a temporary if src and dst overlap */
136584acf34SBALATON Zoltan int llb = s->regs.dst_width * (bpp / 8);
137584acf34SBALATON Zoltan int tmp_stride = DIV_ROUND_UP(llb, sizeof(uint32_t));
138584acf34SBALATON Zoltan uint32_t *tmp = g_malloc(tmp_stride * sizeof(uint32_t) *
139584acf34SBALATON Zoltan s->regs.dst_height);
14008730ee0SBALATON Zoltan fallback = !pixman_blt((uint32_t *)src_bits, tmp,
141584acf34SBALATON Zoltan src_stride, tmp_stride, bpp, bpp,
142584acf34SBALATON Zoltan src_x, src_y, 0, 0,
143584acf34SBALATON Zoltan s->regs.dst_width, s->regs.dst_height);
14408730ee0SBALATON Zoltan if (!fallback) {
14508730ee0SBALATON Zoltan fallback = !pixman_blt(tmp, (uint32_t *)dst_bits,
146584acf34SBALATON Zoltan tmp_stride, dst_stride, bpp, bpp,
147584acf34SBALATON Zoltan 0, 0, dst_x, dst_y,
148584acf34SBALATON Zoltan s->regs.dst_width, s->regs.dst_height);
14908730ee0SBALATON Zoltan }
150584acf34SBALATON Zoltan g_free(tmp);
151*699f15fdSMarc-André Lureau } else
152*699f15fdSMarc-André Lureau #endif
153*699f15fdSMarc-André Lureau {
15408730ee0SBALATON Zoltan fallback = true;
15508730ee0SBALATON Zoltan }
15608730ee0SBALATON Zoltan if (fallback) {
15708730ee0SBALATON Zoltan unsigned int y, i, j, bypp = bpp / 8;
15808730ee0SBALATON Zoltan unsigned int src_pitch = src_stride * sizeof(uint32_t);
15908730ee0SBALATON Zoltan unsigned int dst_pitch = dst_stride * sizeof(uint32_t);
16008730ee0SBALATON Zoltan
16108730ee0SBALATON Zoltan for (y = 0; y < s->regs.dst_height; y++) {
16208730ee0SBALATON Zoltan i = dst_x * bypp;
16308730ee0SBALATON Zoltan j = src_x * bypp;
16408730ee0SBALATON Zoltan if (s->regs.dp_cntl & DST_Y_TOP_TO_BOTTOM) {
16508730ee0SBALATON Zoltan i += (dst_y + y) * dst_pitch;
16608730ee0SBALATON Zoltan j += (src_y + y) * src_pitch;
16708730ee0SBALATON Zoltan } else {
16808730ee0SBALATON Zoltan i += (dst_y + s->regs.dst_height - 1 - y) * dst_pitch;
16908730ee0SBALATON Zoltan j += (src_y + s->regs.dst_height - 1 - y) * src_pitch;
17008730ee0SBALATON Zoltan }
17108730ee0SBALATON Zoltan memmove(&dst_bits[i], &src_bits[j], s->regs.dst_width * bypp);
17208730ee0SBALATON Zoltan }
173584acf34SBALATON Zoltan }
174862b4a29SBALATON Zoltan if (dst_bits >= s->vga.vram_ptr + s->vga.vbe_start_addr &&
175862b4a29SBALATON Zoltan dst_bits < s->vga.vram_ptr + s->vga.vbe_start_addr +
176862b4a29SBALATON Zoltan s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] * s->vga.vbe_line_offset) {
177862b4a29SBALATON Zoltan memory_region_set_dirty(&s->vga.vram, s->vga.vbe_start_addr +
178862b4a29SBALATON Zoltan s->regs.dst_offset +
179584acf34SBALATON Zoltan dst_y * surface_stride(ds),
180862b4a29SBALATON Zoltan s->regs.dst_height * surface_stride(ds));
181862b4a29SBALATON Zoltan }
182ac2071c3SBALATON Zoltan s->regs.dst_x = (s->regs.dp_cntl & DST_X_LEFT_TO_RIGHT ?
183ac2071c3SBALATON Zoltan dst_x + s->regs.dst_width : dst_x);
184ac2071c3SBALATON Zoltan s->regs.dst_y = (s->regs.dp_cntl & DST_Y_TOP_TO_BOTTOM ?
185ac2071c3SBALATON Zoltan dst_y + s->regs.dst_height : dst_y);
186862b4a29SBALATON Zoltan break;
187862b4a29SBALATON Zoltan }
188862b4a29SBALATON Zoltan case ROP3_PATCOPY:
189862b4a29SBALATON Zoltan case ROP3_BLACKNESS:
190862b4a29SBALATON Zoltan case ROP3_WHITENESS:
191862b4a29SBALATON Zoltan {
192862b4a29SBALATON Zoltan uint32_t filler = 0;
193862b4a29SBALATON Zoltan
194862b4a29SBALATON Zoltan switch (s->regs.dp_mix & GMC_ROP3_MASK) {
195862b4a29SBALATON Zoltan case ROP3_PATCOPY:
196a3812741SBALATON Zoltan filler = s->regs.dp_brush_frgd_clr;
197862b4a29SBALATON Zoltan break;
198862b4a29SBALATON Zoltan case ROP3_BLACKNESS:
199a3812741SBALATON Zoltan filler = 0xffUL << 24 | rgb_to_pixel32(s->vga.palette[0],
200a3812741SBALATON Zoltan s->vga.palette[1], s->vga.palette[2]);
201862b4a29SBALATON Zoltan break;
202862b4a29SBALATON Zoltan case ROP3_WHITENESS:
203a3812741SBALATON Zoltan filler = 0xffUL << 24 | rgb_to_pixel32(s->vga.palette[3],
204a3812741SBALATON Zoltan s->vga.palette[4], s->vga.palette[5]);
205862b4a29SBALATON Zoltan break;
206862b4a29SBALATON Zoltan }
207862b4a29SBALATON Zoltan
208c799d2eeSBALATON Zoltan dst_stride /= sizeof(uint32_t);
209862b4a29SBALATON Zoltan DPRINTF("pixman_fill(%p, %d, %d, %d, %d, %d, %d, %x)\n",
21008730ee0SBALATON Zoltan dst_bits, dst_stride, bpp, dst_x, dst_y,
21108730ee0SBALATON Zoltan s->regs.dst_width, s->regs.dst_height, filler);
212*699f15fdSMarc-André Lureau #ifdef CONFIG_PIXMAN
21308730ee0SBALATON Zoltan if (!(s->use_pixman & BIT(0)) ||
21408730ee0SBALATON Zoltan !pixman_fill((uint32_t *)dst_bits, dst_stride, bpp, dst_x, dst_y,
215*699f15fdSMarc-André Lureau s->regs.dst_width, s->regs.dst_height, filler))
216*699f15fdSMarc-André Lureau #endif
217*699f15fdSMarc-André Lureau {
21808730ee0SBALATON Zoltan /* fallback when pixman failed or we don't want to call it */
21908730ee0SBALATON Zoltan unsigned int x, y, i, bypp = bpp / 8;
22008730ee0SBALATON Zoltan unsigned int dst_pitch = dst_stride * sizeof(uint32_t);
22108730ee0SBALATON Zoltan for (y = 0; y < s->regs.dst_height; y++) {
22208730ee0SBALATON Zoltan i = dst_x * bypp + (dst_y + y) * dst_pitch;
22308730ee0SBALATON Zoltan for (x = 0; x < s->regs.dst_width; x++, i += bypp) {
22408730ee0SBALATON Zoltan stn_he_p(&dst_bits[i], bypp, filler);
22508730ee0SBALATON Zoltan }
22608730ee0SBALATON Zoltan }
22708730ee0SBALATON Zoltan }
228862b4a29SBALATON Zoltan if (dst_bits >= s->vga.vram_ptr + s->vga.vbe_start_addr &&
229862b4a29SBALATON Zoltan dst_bits < s->vga.vram_ptr + s->vga.vbe_start_addr +
230862b4a29SBALATON Zoltan s->vga.vbe_regs[VBE_DISPI_INDEX_YRES] * s->vga.vbe_line_offset) {
231862b4a29SBALATON Zoltan memory_region_set_dirty(&s->vga.vram, s->vga.vbe_start_addr +
232862b4a29SBALATON Zoltan s->regs.dst_offset +
233584acf34SBALATON Zoltan dst_y * surface_stride(ds),
234862b4a29SBALATON Zoltan s->regs.dst_height * surface_stride(ds));
235862b4a29SBALATON Zoltan }
236ac2071c3SBALATON Zoltan s->regs.dst_y = (s->regs.dp_cntl & DST_Y_TOP_TO_BOTTOM ?
237ac2071c3SBALATON Zoltan dst_y + s->regs.dst_height : dst_y);
238862b4a29SBALATON Zoltan break;
239862b4a29SBALATON Zoltan }
240862b4a29SBALATON Zoltan default:
241862b4a29SBALATON Zoltan qemu_log_mask(LOG_UNIMP, "Unimplemented ati_2d blt op %x\n",
242862b4a29SBALATON Zoltan (s->regs.dp_mix & GMC_ROP3_MASK) >> 16);
243862b4a29SBALATON Zoltan }
244862b4a29SBALATON Zoltan }
245