2 * qxl local rendering (aka display on sdl/vnc)
4 * Copyright (C) 2010 Red Hat, Inc.
6 * maintained by Gerd Hoffmann <kraxel@redhat.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 or
11 * (at your option) version 3 of the License.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
24 #include "sysemu/runstate.h"
27 static void qxl_blit(PCIQXLDevice
*qxl
, QXLRect
*rect
)
29 DisplaySurface
*surface
= qemu_console_surface(qxl
->vga
.con
);
30 uint8_t *dst
= surface_data(surface
);
34 if (is_buffer_shared(surface
)) {
37 trace_qxl_render_blit(qxl
->guest_primary
.qxl_stride
,
38 rect
->left
, rect
->right
, rect
->top
, rect
->bottom
);
39 src
= qxl
->guest_primary
.data
;
40 if (qxl
->guest_primary
.qxl_stride
< 0) {
41 /* qxl surface is upside down, walk src scanlines
42 * in reverse order to flip it */
43 src
+= (qxl
->guest_primary
.surface
.height
- rect
->top
- 1) *
44 qxl
->guest_primary
.abs_stride
;
46 src
+= rect
->top
* qxl
->guest_primary
.abs_stride
;
48 dst
+= rect
->top
* qxl
->guest_primary
.abs_stride
;
49 src
+= rect
->left
* qxl
->guest_primary
.bytes_pp
;
50 dst
+= rect
->left
* qxl
->guest_primary
.bytes_pp
;
51 len
= (rect
->right
- rect
->left
) * qxl
->guest_primary
.bytes_pp
;
53 for (i
= rect
->top
; i
< rect
->bottom
; i
++) {
54 memcpy(dst
, src
, len
);
55 dst
+= qxl
->guest_primary
.abs_stride
;
56 src
+= qxl
->guest_primary
.qxl_stride
;
60 void qxl_render_resize(PCIQXLDevice
*qxl
)
62 QXLSurfaceCreate
*sc
= &qxl
->guest_primary
.surface
;
64 qxl
->guest_primary
.qxl_stride
= sc
->stride
;
65 qxl
->guest_primary
.abs_stride
= abs(sc
->stride
);
66 qxl
->guest_primary
.resized
++;
68 case SPICE_SURFACE_FMT_16_555
:
69 qxl
->guest_primary
.bytes_pp
= 2;
70 qxl
->guest_primary
.bits_pp
= 15;
72 case SPICE_SURFACE_FMT_16_565
:
73 qxl
->guest_primary
.bytes_pp
= 2;
74 qxl
->guest_primary
.bits_pp
= 16;
76 case SPICE_SURFACE_FMT_32_xRGB
:
77 case SPICE_SURFACE_FMT_32_ARGB
:
78 qxl
->guest_primary
.bytes_pp
= 4;
79 qxl
->guest_primary
.bits_pp
= 32;
82 fprintf(stderr
, "%s: unhandled format: %x\n", __func__
,
83 qxl
->guest_primary
.surface
.format
);
84 qxl
->guest_primary
.bytes_pp
= 4;
85 qxl
->guest_primary
.bits_pp
= 32;
90 static void qxl_set_rect_to_surface(PCIQXLDevice
*qxl
, QXLRect
*area
)
93 area
->right
= qxl
->guest_primary
.surface
.width
;
95 area
->bottom
= qxl
->guest_primary
.surface
.height
;
98 static void qxl_render_update_area_unlocked(PCIQXLDevice
*qxl
)
100 VGACommonState
*vga
= &qxl
->vga
;
101 DisplaySurface
*surface
;
102 int width
= qxl
->guest_head0_width
?: qxl
->guest_primary
.surface
.width
;
103 int height
= qxl
->guest_head0_height
?: qxl
->guest_primary
.surface
.height
;
106 if (qxl
->guest_primary
.resized
) {
107 qxl
->guest_primary
.resized
= 0;
108 qxl
->guest_primary
.data
= qxl_phys2virt(qxl
,
109 qxl
->guest_primary
.surface
.mem
,
110 MEMSLOT_GROUP_GUEST
);
111 if (!qxl
->guest_primary
.data
) {
114 qxl_set_rect_to_surface(qxl
, &qxl
->dirty
[0]);
115 qxl
->num_dirty_rects
= 1;
116 trace_qxl_render_guest_primary_resized(
119 qxl
->guest_primary
.qxl_stride
,
120 qxl
->guest_primary
.bytes_pp
,
121 qxl
->guest_primary
.bits_pp
);
122 if (qxl
->guest_primary
.qxl_stride
> 0) {
123 pixman_format_code_t format
=
124 qemu_default_pixman_format(qxl
->guest_primary
.bits_pp
, true);
125 surface
= qemu_create_displaysurface_from
129 qxl
->guest_primary
.abs_stride
,
130 qxl
->guest_primary
.data
);
132 surface
= qemu_create_displaysurface
136 dpy_gfx_replace_surface(vga
->con
, surface
);
139 if (!qxl
->guest_primary
.data
) {
142 for (i
= 0; i
< qxl
->num_dirty_rects
; i
++) {
143 if (qemu_spice_rect_is_empty(qxl
->dirty
+i
)) {
146 if (qxl
->dirty
[i
].left
< 0 ||
147 qxl
->dirty
[i
].top
< 0 ||
148 qxl
->dirty
[i
].left
> qxl
->dirty
[i
].right
||
149 qxl
->dirty
[i
].top
> qxl
->dirty
[i
].bottom
||
150 qxl
->dirty
[i
].right
> width
||
151 qxl
->dirty
[i
].bottom
> height
) {
154 qxl_blit(qxl
, qxl
->dirty
+i
);
155 dpy_gfx_update(vga
->con
,
156 qxl
->dirty
[i
].left
, qxl
->dirty
[i
].top
,
157 qxl
->dirty
[i
].right
- qxl
->dirty
[i
].left
,
158 qxl
->dirty
[i
].bottom
- qxl
->dirty
[i
].top
);
160 qxl
->num_dirty_rects
= 0;
163 if (qxl
->render_update_cookie_num
== 0) {
164 graphic_hw_update_done(qxl
->ssd
.dcl
.con
);
169 * use ssd.lock to protect render_update_cookie_num.
170 * qxl_render_update is called by io thread or vcpu thread, and the completion
171 * callbacks are called by spice_server thread, deferring to bh called from the
174 void qxl_render_update(PCIQXLDevice
*qxl
)
178 qemu_mutex_lock(&qxl
->ssd
.lock
);
180 if (!runstate_is_running() || !qxl
->guest_primary
.commands
||
181 qxl
->mode
== QXL_MODE_UNDEFINED
) {
182 qxl_render_update_area_unlocked(qxl
);
183 qemu_mutex_unlock(&qxl
->ssd
.lock
);
187 qxl
->guest_primary
.commands
= 0;
188 qxl
->render_update_cookie_num
++;
189 qemu_mutex_unlock(&qxl
->ssd
.lock
);
190 cookie
= qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA
,
192 qxl_set_rect_to_surface(qxl
, &cookie
->u
.render
.area
);
193 qxl_spice_update_area(qxl
, 0, &cookie
->u
.render
.area
, NULL
,
194 0, 1 /* clear_dirty_region */, QXL_ASYNC
, cookie
);
197 void qxl_render_update_area_bh(void *opaque
)
199 PCIQXLDevice
*qxl
= opaque
;
201 qemu_mutex_lock(&qxl
->ssd
.lock
);
202 qxl_render_update_area_unlocked(qxl
);
203 qemu_mutex_unlock(&qxl
->ssd
.lock
);
206 void qxl_render_update_area_done(PCIQXLDevice
*qxl
, QXLCookie
*cookie
)
208 qemu_mutex_lock(&qxl
->ssd
.lock
);
209 trace_qxl_render_update_area_done(cookie
);
210 qemu_bh_schedule(qxl
->update_area_bh
);
211 qxl
->render_update_cookie_num
--;
212 qemu_mutex_unlock(&qxl
->ssd
.lock
);
216 static void qxl_unpack_chunks(void *dest
, size_t size
, PCIQXLDevice
*qxl
,
217 QXLDataChunk
*chunk
, uint32_t group_id
)
219 uint32_t max_chunks
= 32;
224 bytes
= MIN(size
- offset
, chunk
->data_size
);
225 memcpy(dest
+ offset
, chunk
->data
, bytes
);
227 if (offset
== size
) {
230 chunk
= qxl_phys2virt(qxl
, chunk
->next_chunk
, group_id
);
235 if (max_chunks
== 0) {
241 static QEMUCursor
*qxl_cursor(PCIQXLDevice
*qxl
, QXLCursor
*cursor
,
245 uint8_t *and_mask
, *xor_mask
;
248 c
= cursor_alloc(cursor
->header
.width
, cursor
->header
.height
);
249 c
->hot_x
= cursor
->header
.hot_spot_x
;
250 c
->hot_y
= cursor
->header
.hot_spot_y
;
251 switch (cursor
->header
.type
) {
252 case SPICE_CURSOR_TYPE_MONO
:
253 /* Assume that the full cursor is available in a single chunk. */
254 size
= 2 * cursor_get_mono_bpl(c
) * c
->height
;
255 if (size
!= cursor
->data_size
) {
256 fprintf(stderr
, "%s: bad monochrome cursor %ux%u with size %u\n",
257 __func__
, c
->width
, c
->height
, cursor
->data_size
);
260 and_mask
= cursor
->chunk
.data
;
261 xor_mask
= and_mask
+ cursor_get_mono_bpl(c
) * c
->height
;
262 cursor_set_mono(c
, 0xffffff, 0x000000, xor_mask
, 1, and_mask
);
263 if (qxl
->debug
> 2) {
264 cursor_print_ascii_art(c
, "qxl/mono");
267 case SPICE_CURSOR_TYPE_ALPHA
:
268 size
= sizeof(uint32_t) * cursor
->header
.width
* cursor
->header
.height
;
269 qxl_unpack_chunks(c
->data
, size
, qxl
, &cursor
->chunk
, group_id
);
270 if (qxl
->debug
> 2) {
271 cursor_print_ascii_art(c
, "qxl/alpha");
275 fprintf(stderr
, "%s: not implemented: type %d\n",
276 __func__
, cursor
->header
.type
);
287 /* called from spice server thread context only */
288 int qxl_render_cursor(PCIQXLDevice
*qxl
, QXLCommandExt
*ext
)
290 QXLCursorCmd
*cmd
= qxl_phys2virt(qxl
, ext
->cmd
.data
, ext
->group_id
);
298 if (!dpy_cursor_define_supported(qxl
->vga
.con
)) {
302 if (qxl
->debug
> 1 && cmd
->type
!= QXL_CURSOR_MOVE
) {
303 fprintf(stderr
, "%s", __func__
);
304 qxl_log_cmd_cursor(qxl
, cmd
, ext
->group_id
);
305 fprintf(stderr
, "\n");
309 cursor
= qxl_phys2virt(qxl
, cmd
->u
.set
.shape
, ext
->group_id
);
313 c
= qxl_cursor(qxl
, cursor
, ext
->group_id
);
315 c
= cursor_builtin_left_ptr();
317 qemu_mutex_lock(&qxl
->ssd
.lock
);
318 if (qxl
->ssd
.cursor
) {
319 cursor_put(qxl
->ssd
.cursor
);
322 qxl
->ssd
.mouse_x
= cmd
->u
.set
.position
.x
;
323 qxl
->ssd
.mouse_y
= cmd
->u
.set
.position
.y
;
324 qemu_mutex_unlock(&qxl
->ssd
.lock
);
325 qemu_bh_schedule(qxl
->ssd
.cursor_bh
);
327 case QXL_CURSOR_MOVE
:
328 qemu_mutex_lock(&qxl
->ssd
.lock
);
329 qxl
->ssd
.mouse_x
= cmd
->u
.position
.x
;
330 qxl
->ssd
.mouse_y
= cmd
->u
.position
.y
;
331 qemu_mutex_unlock(&qxl
->ssd
.lock
);
332 qemu_bh_schedule(qxl
->ssd
.cursor_bh
);