master
c 383 lines 12.6 KB
Raw
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
27 static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect)
28 {
29 DisplaySurface *surface = qemu_console_surface(qxl->vga.con);
30 int dst_stride = surface_stride(surface);
31 uint8_t *dst = surface_data(surface);
32 uint8_t *src;
33 int len, i;
34
35 if (!surface_is_allocated(surface)) {
36 return;
37 }
38 trace_qxl_render_blit(qxl->guest_primary.qxl_stride,
39 rect->left, rect->right, rect->top, rect->bottom);
40 src = qxl->guest_primary.data;
41 if (qxl->guest_primary.qxl_stride < 0) {
42 /* qxl surface is upside down, walk src scanlines
43 * in reverse order to flip it */
44 src += (qxl->guest_primary.surface.height - rect->top - 1) *
45 qxl->guest_primary.abs_stride;
46 } else {
47 src += rect->top * qxl->guest_primary.abs_stride;
48 }
49 dst += rect->top * dst_stride;
50 src += rect->left * qxl->guest_primary.bytes_pp;
51 dst += rect->left * qxl->guest_primary.bytes_pp;
52 len = (rect->right - rect->left) * qxl->guest_primary.bytes_pp;
53
54 for (i = rect->top; i < rect->bottom; i++) {
55 memcpy(dst, src, len);
56 dst += dst_stride;
57 src += qxl->guest_primary.qxl_stride;
58 }
59 }
60
61 void qxl_render_resize(PCIQXLDevice *qxl)
62 {
63 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
64
65 qxl->guest_primary.qxl_stride = le32_to_cpu(sc->stride);
66 qxl->guest_primary.abs_stride = abs(qxl->guest_primary.qxl_stride);
67 qxl->guest_primary.resized++;
68 /* fallback to default bpp if format is unknown */
69 qxl_format_bpp(qxl, le32_to_cpu(sc->format),
70 &qxl->guest_primary.bytes_pp,
71 &qxl->guest_primary.bits_pp);
72 }
73
74 static void qxl_set_rect_to_surface(PCIQXLDevice *qxl, QXLRect *area)
75 {
76 area->left = 0;
77 area->right = qxl->guest_primary.surface.width;
78 area->top = 0;
79 area->bottom = qxl->guest_primary.surface.height;
80 }
81
82 static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
83 {
84 VGACommonState *vga = &qxl->vga;
85 DisplaySurface *surface;
86 int width = qxl->guest_head0_width ?: qxl->guest_primary.surface.width;
87 int height = qxl->guest_head0_height ?: qxl->guest_primary.surface.height;
88 uint64_t map_height;
89 int i;
90
91 if (width <= 0 || height <= 0) {
92 goto end;
93 }
94
95 if (qxl->guest_primary.bytes_pp > 0) {
96 int max_width = qxl->guest_primary.abs_stride
97 / qxl->guest_primary.bytes_pp;
98 width = MIN(width, max_width);
99 }
100
101 if (qxl->guest_primary.qxl_stride < 0) {
102 /* qxl_blit() uses the primary height to find the first scanline. */
103 height = MIN(height, (int)qxl->guest_primary.surface.height);
104 }
105
106 if (qxl->guest_primary.abs_stride > 0) {
107 int max_height = qxl->vgamem_size / qxl->guest_primary.abs_stride;
108 height = MIN(height, max_height);
109 }
110
111 /*
112 * height limits the visible update, while map_height is the guest memory
113 * span validated by qxl_phys2virt(). With a negative stride qxl_blit()
114 * addresses scanlines from the declared primary height, so a shorter
115 * monitor still requires validating the full primary surface.
116 */
117 map_height = qxl->guest_primary.qxl_stride < 0 ?
118 qxl->guest_primary.surface.height : height;
119
120 if (qxl->guest_primary.resized) {
121 qxl->guest_primary.resized = 0;
122 qxl->guest_primary.data = qxl_phys2virt(qxl,
123 qxl->guest_primary.surface.mem,
124 MEMSLOT_GROUP_GUEST,
125 qxl->guest_primary.abs_stride
126 * map_height);
127 if (!qxl->guest_primary.data) {
128 goto end;
129 }
130 qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
131 qxl->num_dirty_rects = 1;
132 trace_qxl_render_guest_primary_resized(
133 width,
134 height,
135 qxl->guest_primary.qxl_stride,
136 qxl->guest_primary.bytes_pp,
137 qxl->guest_primary.bits_pp);
138 if (qxl->guest_primary.qxl_stride > 0) {
139 pixman_format_code_t format =
140 qemu_default_pixman_format(qxl->guest_primary.bits_pp, true);
141 surface = qemu_create_displaysurface_from
142 (width,
143 height,
144 format,
145 qxl->guest_primary.abs_stride,
146 qxl->guest_primary.data);
147 } else {
148 surface = qemu_create_displaysurface
149 (width,
150 height);
151 }
152 qemu_console_set_surface(vga->con, surface);
153 }
154
155 if (!qxl->guest_primary.data) {
156 goto end;
157 }
158 for (i = 0; i < qxl->num_dirty_rects; i++) {
159 if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
160 break;
161 }
162 if (qxl->dirty[i].left < 0 ||
163 qxl->dirty[i].top < 0 ||
164 qxl->dirty[i].left > qxl->dirty[i].right ||
165 qxl->dirty[i].top > qxl->dirty[i].bottom ||
166 qxl->dirty[i].right > width ||
167 qxl->dirty[i].bottom > height) {
168 continue;
169 }
170 qxl_blit(qxl, qxl->dirty+i);
171 qemu_console_update(vga->con,
172 qxl->dirty[i].left, qxl->dirty[i].top,
173 qxl->dirty[i].right - qxl->dirty[i].left,
174 qxl->dirty[i].bottom - qxl->dirty[i].top);
175 }
176 qxl->num_dirty_rects = 0;
177
178 end:
179 if (qxl->render_update_cookie_num == 0) {
180 qemu_console_hw_update_done(qxl->ssd.dcl.con);
181 }
182 }
183
184 /*
185 * use ssd.lock to protect render_update_cookie_num.
186 * qxl_render_update is called by io thread or vcpu thread, and the completion
187 * callbacks are called by spice_server thread, deferring to bh called from the
188 * io thread.
189 */
190 bool qxl_render_update(PCIQXLDevice *qxl)
191 {
192 QXLCookie *cookie;
193
194 qemu_mutex_lock(&qxl->ssd.lock);
195
196 if (!runstate_is_running() || !qxl->guest_primary.commands ||
197 qxl->mode == QXL_MODE_UNDEFINED) {
198 qxl_render_update_area_unlocked(qxl);
199 qemu_mutex_unlock(&qxl->ssd.lock);
200 return true;
201 }
202
203 qxl->guest_primary.commands = 0;
204 qxl->render_update_cookie_num++;
205 qemu_mutex_unlock(&qxl->ssd.lock);
206 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
207 0);
208 qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
209 qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
210 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
211 return false;
212 }
213
214 void qxl_render_update_area_bh(void *opaque)
215 {
216 PCIQXLDevice *qxl = opaque;
217
218 qemu_mutex_lock(&qxl->ssd.lock);
219 qxl_render_update_area_unlocked(qxl);
220 qemu_mutex_unlock(&qxl->ssd.lock);
221 }
222
223 void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
224 {
225 qemu_mutex_lock(&qxl->ssd.lock);
226 trace_qxl_render_update_area_done(cookie);
227 qemu_bh_schedule(qxl->update_area_bh);
228 qxl->render_update_cookie_num--;
229 qemu_mutex_unlock(&qxl->ssd.lock);
230 g_free(cookie);
231 }
232
233 static void qxl_unpack_chunks(void *dest, size_t size, PCIQXLDevice *qxl,
234 QXLDataChunk *chunk, uint32_t group_id,
235 uint32_t chunk_data_size)
236 {
237 uint32_t max_chunks = 32;
238 size_t offset = 0;
239 size_t bytes;
240 QXLPHYSICAL next_chunk_phys = 0;
241
242 for (;;) {
243 bytes = MIN(size - offset, chunk_data_size);
244 memcpy(dest + offset, chunk->data, bytes);
245 offset += bytes;
246 if (offset == size) {
247 return;
248 }
249 next_chunk_phys = chunk->next_chunk;
250 chunk = qxl_phys2virt(qxl, next_chunk_phys, group_id,
251 sizeof(QXLDataChunk));
252 if (!chunk) {
253 return;
254 }
255 chunk_data_size = chunk->data_size;
256 chunk = qxl_phys2virt(qxl, next_chunk_phys, group_id,
257 sizeof(QXLDataChunk) + chunk_data_size);
258 if (!chunk) {
259 return;
260 }
261 max_chunks--;
262 if (max_chunks == 0) {
263 return;
264 }
265 }
266 }
267
268 static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor,
269 uint32_t group_id, uint32_t chunk_data_size)
270 {
271 QEMUCursor *c;
272 uint8_t *and_mask, *xor_mask;
273 size_t size;
274
275 c = cursor_alloc(cursor->header.width, cursor->header.height);
276
277 if (!c) {
278 qxl_set_guest_bug(qxl, "%s: cursor %ux%u alloc error", __func__,
279 cursor->header.width, cursor->header.height);
280 goto fail;
281 }
282
283 c->hot_x = cursor->header.hot_spot_x;
284 c->hot_y = cursor->header.hot_spot_y;
285 switch (cursor->header.type) {
286 case SPICE_CURSOR_TYPE_MONO:
287 /* Assume that the full cursor is available in a single chunk. */
288 size = 2 * cursor_get_mono_bpl(c) * c->height;
289 if (size != cursor->data_size || chunk_data_size < size) {
290 qxl_set_guest_bug(qxl, "%s: bad monochrome cursor %ux%u"
291 " data_size %u chunk_size %u",
292 __func__, c->width, c->height,
293 cursor->data_size, chunk_data_size);
294 goto fail;
295 }
296 and_mask = cursor->chunk.data;
297 xor_mask = and_mask + cursor_get_mono_bpl(c) * c->height;
298 cursor_set_mono(c, 0xffffff, 0x000000, xor_mask, 1, and_mask);
299 if (qxl->debug > 2) {
300 cursor_print_ascii_art(c, "qxl/mono");
301 }
302 break;
303 case SPICE_CURSOR_TYPE_ALPHA:
304 size = sizeof(uint32_t) * c->width * c->height;
305 qxl_unpack_chunks(c->data, size, qxl, &cursor->chunk, group_id,
306 chunk_data_size);
307 if (qxl->debug > 2) {
308 cursor_print_ascii_art(c, "qxl/alpha");
309 }
310 break;
311 default:
312 fprintf(stderr, "%s: not implemented: type %d\n",
313 __func__, cursor->header.type);
314 goto fail;
315 }
316 return c;
317
318 fail:
319 cursor_unref(c);
320 return NULL;
321 }
322
323
324 /* called from spice server thread context only */
325 int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
326 {
327 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id,
328 sizeof(QXLCursorCmd));
329 QXLCursor *cursor;
330 QEMUCursor *c;
331
332 if (!cmd) {
333 return 1;
334 }
335
336 if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
337 fprintf(stderr, "%s", __func__);
338 qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
339 fprintf(stderr, "\n");
340 }
341 switch (cmd->type) {
342 case QXL_CURSOR_SET:
343 {
344 uint32_t chunk_data_size;
345
346 /* First read the QXLCursor to get QXLDataChunk::data_size ... */
347 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id,
348 sizeof(QXLCursor));
349 if (!cursor) {
350 return 1;
351 }
352 chunk_data_size = cursor->chunk.data_size;
353 /* Then read including the chunked data following QXLCursor. */
354 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id,
355 sizeof(QXLCursor) + chunk_data_size);
356 if (!cursor) {
357 return 1;
358 }
359 c = qxl_cursor(qxl, cursor, ext->group_id, chunk_data_size);
360 if (c == NULL) {
361 c = cursor_builtin_left_ptr();
362 }
363 qemu_mutex_lock(&qxl->ssd.lock);
364 if (qxl->ssd.cursor) {
365 cursor_unref(qxl->ssd.cursor);
366 }
367 qxl->ssd.cursor = c;
368 qxl->ssd.mouse_x = cmd->u.set.position.x;
369 qxl->ssd.mouse_y = cmd->u.set.position.y;
370 qemu_mutex_unlock(&qxl->ssd.lock);
371 qemu_bh_schedule(qxl->ssd.cursor_bh);
372 break;
373 }
374 case QXL_CURSOR_MOVE:
375 qemu_mutex_lock(&qxl->ssd.lock);
376 qxl->ssd.mouse_x = cmd->u.position.x;
377 qxl->ssd.mouse_y = cmd->u.position.y;
378 qemu_mutex_unlock(&qxl->ssd.lock);
379 qemu_bh_schedule(qxl->ssd.cursor_bh);
380 break;
381 }
382 return 0;
383 }