1 /*
2 * qxl local rendering (aka display on sdl/vnc)
3 *
4 * Copyright (C) 2010 Red Hat, Inc.
5 *
6 * maintained by Gerd Hoffmann <kraxel@redhat.com>
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
22 #include "qemu/osdep.h"
23 #include "qxl.h"
24 #include "system/runstate.h"
25 #include "trace.h"
26
qxl_blit(PCIQXLDevice * qxl,QXLRect * rect)27 static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect)
28 {
29 DisplaySurface *surface = qemu_console_surface(qxl->vga.con);
30 uint8_t *dst = surface_data(surface);
31 uint8_t *src;
32 int len, i;
33
34 if (!surface_is_allocated(surface)) {
35 return;
36 }
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;
45 } else {
46 src += rect->top * qxl->guest_primary.abs_stride;
47 }
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;
52
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;
57 }
58 }
59
qxl_render_resize(PCIQXLDevice * qxl)60 void qxl_render_resize(PCIQXLDevice *qxl)
61 {
62 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
63
64 qxl->guest_primary.qxl_stride = sc->stride;
65 qxl->guest_primary.abs_stride = abs(sc->stride);
66 qxl->guest_primary.resized++;
67 switch (sc->format) {
68 case SPICE_SURFACE_FMT_16_555:
69 qxl->guest_primary.bytes_pp = 2;
70 qxl->guest_primary.bits_pp = 15;
71 break;
72 case SPICE_SURFACE_FMT_16_565:
73 qxl->guest_primary.bytes_pp = 2;
74 qxl->guest_primary.bits_pp = 16;
75 break;
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;
80 break;
81 default:
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;
86 break;
87 }
88 }
89
qxl_set_rect_to_surface(PCIQXLDevice * qxl,QXLRect * area)90 static void qxl_set_rect_to_surface(PCIQXLDevice *qxl, QXLRect *area)
91 {
92 area->left = 0;
93 area->right = qxl->guest_primary.surface.width;
94 area->top = 0;
95 area->bottom = qxl->guest_primary.surface.height;
96 }
97
qxl_render_update_area_unlocked(PCIQXLDevice * qxl)98 static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
99 {
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;
104 int i;
105
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 qxl->guest_primary.abs_stride
112 * height);
113 if (!qxl->guest_primary.data) {
114 goto end;
115 }
116 qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
117 qxl->num_dirty_rects = 1;
118 trace_qxl_render_guest_primary_resized(
119 width,
120 height,
121 qxl->guest_primary.qxl_stride,
122 qxl->guest_primary.bytes_pp,
123 qxl->guest_primary.bits_pp);
124 if (qxl->guest_primary.qxl_stride > 0) {
125 pixman_format_code_t format =
126 qemu_default_pixman_format(qxl->guest_primary.bits_pp, true);
127 surface = qemu_create_displaysurface_from
128 (width,
129 height,
130 format,
131 qxl->guest_primary.abs_stride,
132 qxl->guest_primary.data);
133 } else {
134 surface = qemu_create_displaysurface
135 (width,
136 height);
137 }
138 dpy_gfx_replace_surface(vga->con, surface);
139 }
140
141 if (!qxl->guest_primary.data) {
142 goto end;
143 }
144 for (i = 0; i < qxl->num_dirty_rects; i++) {
145 if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
146 break;
147 }
148 if (qxl->dirty[i].left < 0 ||
149 qxl->dirty[i].top < 0 ||
150 qxl->dirty[i].left > qxl->dirty[i].right ||
151 qxl->dirty[i].top > qxl->dirty[i].bottom ||
152 qxl->dirty[i].right > width ||
153 qxl->dirty[i].bottom > height) {
154 continue;
155 }
156 qxl_blit(qxl, qxl->dirty+i);
157 dpy_gfx_update(vga->con,
158 qxl->dirty[i].left, qxl->dirty[i].top,
159 qxl->dirty[i].right - qxl->dirty[i].left,
160 qxl->dirty[i].bottom - qxl->dirty[i].top);
161 }
162 qxl->num_dirty_rects = 0;
163
164 end:
165 if (qxl->render_update_cookie_num == 0) {
166 graphic_hw_update_done(qxl->ssd.dcl.con);
167 }
168 }
169
170 /*
171 * use ssd.lock to protect render_update_cookie_num.
172 * qxl_render_update is called by io thread or vcpu thread, and the completion
173 * callbacks are called by spice_server thread, deferring to bh called from the
174 * io thread.
175 */
qxl_render_update(PCIQXLDevice * qxl)176 void qxl_render_update(PCIQXLDevice *qxl)
177 {
178 QXLCookie *cookie;
179
180 qemu_mutex_lock(&qxl->ssd.lock);
181
182 if (!runstate_is_running() || !qxl->guest_primary.commands ||
183 qxl->mode == QXL_MODE_UNDEFINED) {
184 qxl_render_update_area_unlocked(qxl);
185 qemu_mutex_unlock(&qxl->ssd.lock);
186 graphic_hw_update_done(qxl->ssd.dcl.con);
187 return;
188 }
189
190 qxl->guest_primary.commands = 0;
191 qxl->render_update_cookie_num++;
192 qemu_mutex_unlock(&qxl->ssd.lock);
193 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
194 0);
195 qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
196 qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
197 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
198 }
199
qxl_render_update_area_bh(void * opaque)200 void qxl_render_update_area_bh(void *opaque)
201 {
202 PCIQXLDevice *qxl = opaque;
203
204 qemu_mutex_lock(&qxl->ssd.lock);
205 qxl_render_update_area_unlocked(qxl);
206 qemu_mutex_unlock(&qxl->ssd.lock);
207 }
208
qxl_render_update_area_done(PCIQXLDevice * qxl,QXLCookie * cookie)209 void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
210 {
211 qemu_mutex_lock(&qxl->ssd.lock);
212 trace_qxl_render_update_area_done(cookie);
213 qemu_bh_schedule(qxl->update_area_bh);
214 qxl->render_update_cookie_num--;
215 qemu_mutex_unlock(&qxl->ssd.lock);
216 g_free(cookie);
217 }
218
qxl_unpack_chunks(void * dest,size_t size,PCIQXLDevice * qxl,QXLDataChunk * chunk,uint32_t group_id)219 static void qxl_unpack_chunks(void *dest, size_t size, PCIQXLDevice *qxl,
220 QXLDataChunk *chunk, uint32_t group_id)
221 {
222 uint32_t max_chunks = 32;
223 size_t offset = 0;
224 size_t bytes;
225 QXLPHYSICAL next_chunk_phys = 0;
226
227 for (;;) {
228 bytes = MIN(size - offset, chunk->data_size);
229 memcpy(dest + offset, chunk->data, bytes);
230 offset += bytes;
231 if (offset == size) {
232 return;
233 }
234 next_chunk_phys = chunk->next_chunk;
235 /* fist time, only get the next chunk's data size */
236 chunk = qxl_phys2virt(qxl, next_chunk_phys, group_id,
237 sizeof(QXLDataChunk));
238 if (!chunk) {
239 return;
240 }
241 /* second time, check data size and get data */
242 chunk = qxl_phys2virt(qxl, next_chunk_phys, group_id,
243 sizeof(QXLDataChunk) + chunk->data_size);
244 if (!chunk) {
245 return;
246 }
247 max_chunks--;
248 if (max_chunks == 0) {
249 return;
250 }
251 }
252 }
253
qxl_cursor(PCIQXLDevice * qxl,QXLCursor * cursor,uint32_t group_id)254 static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor,
255 uint32_t group_id)
256 {
257 QEMUCursor *c;
258 uint8_t *and_mask, *xor_mask;
259 size_t size;
260
261 c = cursor_alloc(cursor->header.width, cursor->header.height);
262
263 if (!c) {
264 qxl_set_guest_bug(qxl, "%s: cursor %ux%u alloc error", __func__,
265 cursor->header.width, cursor->header.height);
266 goto fail;
267 }
268
269 c->hot_x = cursor->header.hot_spot_x;
270 c->hot_y = cursor->header.hot_spot_y;
271 switch (cursor->header.type) {
272 case SPICE_CURSOR_TYPE_MONO:
273 /* Assume that the full cursor is available in a single chunk. */
274 size = 2 * cursor_get_mono_bpl(c) * c->height;
275 if (size != cursor->data_size) {
276 fprintf(stderr, "%s: bad monochrome cursor %ux%u with size %u\n",
277 __func__, c->width, c->height, cursor->data_size);
278 goto fail;
279 }
280 and_mask = cursor->chunk.data;
281 xor_mask = and_mask + cursor_get_mono_bpl(c) * c->height;
282 cursor_set_mono(c, 0xffffff, 0x000000, xor_mask, 1, and_mask);
283 if (qxl->debug > 2) {
284 cursor_print_ascii_art(c, "qxl/mono");
285 }
286 break;
287 case SPICE_CURSOR_TYPE_ALPHA:
288 size = sizeof(uint32_t) * c->width * c->height;
289 qxl_unpack_chunks(c->data, size, qxl, &cursor->chunk, group_id);
290 if (qxl->debug > 2) {
291 cursor_print_ascii_art(c, "qxl/alpha");
292 }
293 break;
294 default:
295 fprintf(stderr, "%s: not implemented: type %d\n",
296 __func__, cursor->header.type);
297 goto fail;
298 }
299 return c;
300
301 fail:
302 cursor_unref(c);
303 return NULL;
304 }
305
306
307 /* called from spice server thread context only */
qxl_render_cursor(PCIQXLDevice * qxl,QXLCommandExt * ext)308 int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
309 {
310 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id,
311 sizeof(QXLCursorCmd));
312 QXLCursor *cursor;
313 QEMUCursor *c;
314
315 if (!cmd) {
316 return 1;
317 }
318
319 if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
320 fprintf(stderr, "%s", __func__);
321 qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
322 fprintf(stderr, "\n");
323 }
324 switch (cmd->type) {
325 case QXL_CURSOR_SET:
326 /* First read the QXLCursor to get QXLDataChunk::data_size ... */
327 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id,
328 sizeof(QXLCursor));
329 if (!cursor) {
330 return 1;
331 }
332 /* Then read including the chunked data following QXLCursor. */
333 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id,
334 sizeof(QXLCursor) + cursor->chunk.data_size);
335 if (!cursor) {
336 return 1;
337 }
338 c = qxl_cursor(qxl, cursor, ext->group_id);
339 if (c == NULL) {
340 c = cursor_builtin_left_ptr();
341 }
342 qemu_mutex_lock(&qxl->ssd.lock);
343 if (qxl->ssd.cursor) {
344 cursor_unref(qxl->ssd.cursor);
345 }
346 qxl->ssd.cursor = c;
347 qxl->ssd.mouse_x = cmd->u.set.position.x;
348 qxl->ssd.mouse_y = cmd->u.set.position.y;
349 qemu_mutex_unlock(&qxl->ssd.lock);
350 qemu_bh_schedule(qxl->ssd.cursor_bh);
351 break;
352 case QXL_CURSOR_MOVE:
353 qemu_mutex_lock(&qxl->ssd.lock);
354 qxl->ssd.mouse_x = cmd->u.position.x;
355 qxl->ssd.mouse_y = cmd->u.position.y;
356 qemu_mutex_unlock(&qxl->ssd.lock);
357 qemu_bh_schedule(qxl->ssd.cursor_bh);
358 break;
359 }
360 return 0;
361 }
362