2 * QEMU ATI SVGA emulation
5 * Copyright (c) 2019 BALATON Zoltan
7 * This work is licensed under the GNU GPL license version 2 or later.
10 #include "qemu/osdep.h"
14 #include "ui/pixel_ops.h"
18 * This is 2D _acceleration_ and supposed to be fast. Therefore, don't try to
19 * reinvent the wheel (unlikely to get better with a naive implementation than
20 * existing libraries) and avoid (poorly) reimplementing gfx primitives.
21 * That is unnecessary and would become a performance problem. Instead, try to
22 * map to and reuse existing optimised facilities (e.g. pixman) wherever
26 static int ati_bpp_from_datatype(ATIVGAState
*s
)
28 switch (s
->regs
.dp_datatype
& 0xf) {
39 qemu_log_mask(LOG_UNIMP
, "Unknown dst datatype %d\n",
40 s
->regs
.dp_datatype
& 0xf);
45 #define DEFAULT_CNTL (s->regs.dp_gui_master_cntl & GMC_DST_PITCH_OFFSET_CNTL)
47 void ati_2d_blt(ATIVGAState
*s
)
49 /* FIXME it is probably more complex than this and may need to be */
50 /* rewritten but for now as a start just to get some output: */
51 DisplaySurface
*ds
= qemu_console_surface(s
->vga
.con
);
52 DPRINTF("%p %u ds: %p %d %d rop: %x\n", s
->vga
.vram_ptr
,
53 s
->vga
.vbe_start_addr
, surface_data(ds
), surface_stride(ds
),
54 surface_bits_per_pixel(ds
),
55 (s
->regs
.dp_mix
& GMC_ROP3_MASK
) >> 16);
56 int dst_x
= (s
->regs
.dp_cntl
& DST_X_LEFT_TO_RIGHT
?
57 s
->regs
.dst_x
: s
->regs
.dst_x
+ 1 - s
->regs
.dst_width
);
58 int dst_y
= (s
->regs
.dp_cntl
& DST_Y_TOP_TO_BOTTOM
?
59 s
->regs
.dst_y
: s
->regs
.dst_y
+ 1 - s
->regs
.dst_height
);
60 int bpp
= ati_bpp_from_datatype(s
);
61 int dst_stride
= DEFAULT_CNTL
? s
->regs
.dst_pitch
: s
->regs
.default_pitch
;
62 uint8_t *dst_bits
= s
->vga
.vram_ptr
+ (DEFAULT_CNTL
?
63 s
->regs
.dst_offset
: s
->regs
.default_offset
);
65 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
66 dst_bits
+= s
->regs
.crtc_offset
& 0x07ffffff;
69 uint8_t *end
= s
->vga
.vram_ptr
+ s
->vga
.vram_size
;
70 if (dst_bits
>= end
|| dst_bits
+ dst_x
+ (dst_y
+ s
->regs
.dst_height
) *
72 qemu_log_mask(LOG_UNIMP
, "blt outside vram not implemented\n");
75 DPRINTF("%d %d %d, %d %d %d, (%d,%d) -> (%d,%d) %dx%d %c %c\n",
76 s
->regs
.src_offset
, s
->regs
.dst_offset
, s
->regs
.default_offset
,
77 s
->regs
.src_pitch
, s
->regs
.dst_pitch
, s
->regs
.default_pitch
,
78 s
->regs
.src_x
, s
->regs
.src_y
, s
->regs
.dst_x
, s
->regs
.dst_y
,
79 s
->regs
.dst_width
, s
->regs
.dst_height
,
80 (s
->regs
.dp_cntl
& DST_X_LEFT_TO_RIGHT
? '>' : '<'),
81 (s
->regs
.dp_cntl
& DST_Y_TOP_TO_BOTTOM
? 'v' : '^'));
82 switch (s
->regs
.dp_mix
& GMC_ROP3_MASK
) {
85 int src_x
= (s
->regs
.dp_cntl
& DST_X_LEFT_TO_RIGHT
?
86 s
->regs
.src_x
: s
->regs
.src_x
+ 1 - s
->regs
.dst_width
);
87 int src_y
= (s
->regs
.dp_cntl
& DST_Y_TOP_TO_BOTTOM
?
88 s
->regs
.src_y
: s
->regs
.src_y
+ 1 - s
->regs
.dst_height
);
89 int src_stride
= DEFAULT_CNTL
?
90 s
->regs
.src_pitch
: s
->regs
.default_pitch
;
91 uint8_t *src_bits
= s
->vga
.vram_ptr
+ (DEFAULT_CNTL
?
92 s
->regs
.src_offset
: s
->regs
.default_offset
);
94 if (s
->dev_id
== PCI_DEVICE_ID_ATI_RAGE128_PF
) {
95 src_bits
+= s
->regs
.crtc_offset
& 0x07ffffff;
98 if (src_bits
>= end
|| src_bits
+ src_x
+
99 (src_y
+ s
->regs
.dst_height
) * src_stride
>= end
) {
100 qemu_log_mask(LOG_UNIMP
, "blt outside vram not implemented\n");
104 src_stride
/= sizeof(uint32_t);
105 dst_stride
/= sizeof(uint32_t);
106 DPRINTF("pixman_blt(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)\n",
107 src_bits
, dst_bits
, src_stride
, dst_stride
, bpp
, bpp
,
108 src_x
, src_y
, dst_x
, dst_y
,
109 s
->regs
.dst_width
, s
->regs
.dst_height
);
110 if (s
->regs
.dp_cntl
& DST_X_LEFT_TO_RIGHT
&&
111 s
->regs
.dp_cntl
& DST_Y_TOP_TO_BOTTOM
) {
112 pixman_blt((uint32_t *)src_bits
, (uint32_t *)dst_bits
,
113 src_stride
, dst_stride
, bpp
, bpp
,
114 src_x
, src_y
, dst_x
, dst_y
,
115 s
->regs
.dst_width
, s
->regs
.dst_height
);
117 /* FIXME: We only really need a temporary if src and dst overlap */
118 int llb
= s
->regs
.dst_width
* (bpp
/ 8);
119 int tmp_stride
= DIV_ROUND_UP(llb
, sizeof(uint32_t));
120 uint32_t *tmp
= g_malloc(tmp_stride
* sizeof(uint32_t) *
122 pixman_blt((uint32_t *)src_bits
, tmp
,
123 src_stride
, tmp_stride
, bpp
, bpp
,
125 s
->regs
.dst_width
, s
->regs
.dst_height
);
126 pixman_blt(tmp
, (uint32_t *)dst_bits
,
127 tmp_stride
, dst_stride
, bpp
, bpp
,
129 s
->regs
.dst_width
, s
->regs
.dst_height
);
132 if (dst_bits
>= s
->vga
.vram_ptr
+ s
->vga
.vbe_start_addr
&&
133 dst_bits
< s
->vga
.vram_ptr
+ s
->vga
.vbe_start_addr
+
134 s
->vga
.vbe_regs
[VBE_DISPI_INDEX_YRES
] * s
->vga
.vbe_line_offset
) {
135 memory_region_set_dirty(&s
->vga
.vram
, s
->vga
.vbe_start_addr
+
137 dst_y
* surface_stride(ds
),
138 s
->regs
.dst_height
* surface_stride(ds
));
140 s
->regs
.dst_x
+= s
->regs
.dst_width
;
141 s
->regs
.dst_y
+= s
->regs
.dst_height
;
150 switch (s
->regs
.dp_mix
& GMC_ROP3_MASK
) {
152 filler
= s
->regs
.dp_brush_frgd_clr
;
155 filler
= 0xffUL
<< 24 | rgb_to_pixel32(s
->vga
.palette
[0],
156 s
->vga
.palette
[1], s
->vga
.palette
[2]);
159 filler
= 0xffUL
<< 24 | rgb_to_pixel32(s
->vga
.palette
[3],
160 s
->vga
.palette
[4], s
->vga
.palette
[5]);
164 dst_stride
/= sizeof(uint32_t);
165 DPRINTF("pixman_fill(%p, %d, %d, %d, %d, %d, %d, %x)\n",
166 dst_bits
, dst_stride
, bpp
,
167 s
->regs
.dst_x
, s
->regs
.dst_y
,
168 s
->regs
.dst_width
, s
->regs
.dst_height
,
170 pixman_fill((uint32_t *)dst_bits
, dst_stride
, bpp
,
171 s
->regs
.dst_x
, s
->regs
.dst_y
,
172 s
->regs
.dst_width
, s
->regs
.dst_height
,
174 if (dst_bits
>= s
->vga
.vram_ptr
+ s
->vga
.vbe_start_addr
&&
175 dst_bits
< s
->vga
.vram_ptr
+ s
->vga
.vbe_start_addr
+
176 s
->vga
.vbe_regs
[VBE_DISPI_INDEX_YRES
] * s
->vga
.vbe_line_offset
) {
177 memory_region_set_dirty(&s
->vga
.vram
, s
->vga
.vbe_start_addr
+
179 dst_y
* surface_stride(ds
),
180 s
->regs
.dst_height
* surface_stride(ds
));
182 s
->regs
.dst_y
+= s
->regs
.dst_height
;
186 qemu_log_mask(LOG_UNIMP
, "Unimplemented ati_2d blt op %x\n",
187 (s
->regs
.dp_mix
& GMC_ROP3_MASK
) >> 16);