blob: 14ad2b352d32e67ee2fd4ba4c493409d75a68cba [file] [log] [blame]
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001/*
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
Peter Maydell47df5152016-01-26 18:17:13 +000022#include "qemu/osdep.h"
Paolo Bonzini47b43a12013-03-18 17:36:02 +010023#include "qxl.h"
Stefan Weilb1829cd2013-12-07 15:09:12 +010024#include "trace.h"
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020025
Gerd Hoffmanne2efc0a2012-02-27 11:05:09 +010026static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020027{
Gerd Hoffmannc78f7132013-03-05 15:24:14 +010028 DisplaySurface *surface = qemu_console_surface(qxl->vga.con);
29 uint8_t *dst = surface_data(surface);
Alon Levy4c19ebb2012-02-24 23:19:29 +020030 uint8_t *src;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020031 int len, i;
32
Gerd Hoffmannc78f7132013-03-05 15:24:14 +010033 if (is_buffer_shared(surface)) {
Alon Levy4c19ebb2012-02-24 23:19:29 +020034 return;
35 }
Alon Levyd53291c2012-03-18 13:46:15 +010036 trace_qxl_render_blit(qxl->guest_primary.qxl_stride,
Alon Levy4c19ebb2012-02-24 23:19:29 +020037 rect->left, rect->right, rect->top, rect->bottom);
38 src = qxl->guest_primary.data;
Gerd Hoffmanne2efc0a2012-02-27 11:05:09 +010039 if (qxl->guest_primary.qxl_stride < 0) {
40 /* qxl surface is upside down, walk src scanlines
41 * in reverse order to flip it */
42 src += (qxl->guest_primary.surface.height - rect->top - 1) *
43 qxl->guest_primary.abs_stride;
44 } else {
45 src += rect->top * qxl->guest_primary.abs_stride;
46 }
Gerd Hoffmann0e2487b2011-10-21 15:59:07 +020047 dst += rect->top * qxl->guest_primary.abs_stride;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020048 src += rect->left * qxl->guest_primary.bytes_pp;
49 dst += rect->left * qxl->guest_primary.bytes_pp;
50 len = (rect->right - rect->left) * qxl->guest_primary.bytes_pp;
51
52 for (i = rect->top; i < rect->bottom; i++) {
53 memcpy(dst, src, len);
Gerd Hoffmann0e2487b2011-10-21 15:59:07 +020054 dst += qxl->guest_primary.abs_stride;
Gerd Hoffmanne2efc0a2012-02-27 11:05:09 +010055 src += qxl->guest_primary.qxl_stride;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020056 }
57}
58
59void qxl_render_resize(PCIQXLDevice *qxl)
60{
61 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
62
Gerd Hoffmann0e2487b2011-10-21 15:59:07 +020063 qxl->guest_primary.qxl_stride = sc->stride;
64 qxl->guest_primary.abs_stride = abs(sc->stride);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020065 qxl->guest_primary.resized++;
66 switch (sc->format) {
67 case SPICE_SURFACE_FMT_16_555:
68 qxl->guest_primary.bytes_pp = 2;
69 qxl->guest_primary.bits_pp = 15;
70 break;
71 case SPICE_SURFACE_FMT_16_565:
72 qxl->guest_primary.bytes_pp = 2;
73 qxl->guest_primary.bits_pp = 16;
74 break;
75 case SPICE_SURFACE_FMT_32_xRGB:
76 case SPICE_SURFACE_FMT_32_ARGB:
77 qxl->guest_primary.bytes_pp = 4;
78 qxl->guest_primary.bits_pp = 32;
79 break;
80 default:
Alistair Francisa89f3642017-11-08 14:56:31 -080081 fprintf(stderr, "%s: unhandled format: %x\n", __func__,
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020082 qxl->guest_primary.surface.format);
83 qxl->guest_primary.bytes_pp = 4;
84 qxl->guest_primary.bits_pp = 32;
85 break;
86 }
87}
88
Alon Levy81fb6f12012-02-24 23:19:31 +020089static void qxl_set_rect_to_surface(PCIQXLDevice *qxl, QXLRect *area)
90{
91 area->left = 0;
92 area->right = qxl->guest_primary.surface.width;
93 area->top = 0;
94 area->bottom = qxl->guest_primary.surface.height;
95}
96
97static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +020098{
99 VGACommonState *vga = &qxl->vga;
Gerd Hoffmannda229ef2013-02-28 10:48:02 +0100100 DisplaySurface *surface;
Gerd Hoffmann979f7ef2018-09-19 12:30:57 +0200101 int width = qxl->guest_head0_width ?: qxl->guest_primary.surface.width;
102 int height = qxl->guest_head0_height ?: qxl->guest_primary.surface.height;
Alon Levy81fb6f12012-02-24 23:19:31 +0200103 int i;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200104
105 if (qxl->guest_primary.resized) {
106 qxl->guest_primary.resized = 0;
Gerd Hoffmannc58c7b92013-09-05 21:57:19 +0200107 qxl->guest_primary.data = qxl_phys2virt(qxl,
108 qxl->guest_primary.surface.mem,
109 MEMSLOT_GROUP_GUEST);
110 if (!qxl->guest_primary.data) {
111 return;
112 }
Alon Levy81fb6f12012-02-24 23:19:31 +0200113 qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
114 qxl->num_dirty_rects = 1;
Alon Levyd53291c2012-03-18 13:46:15 +0100115 trace_qxl_render_guest_primary_resized(
Gerd Hoffmann979f7ef2018-09-19 12:30:57 +0200116 width,
117 height,
Gerd Hoffmann0e2487b2011-10-21 15:59:07 +0200118 qxl->guest_primary.qxl_stride,
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200119 qxl->guest_primary.bytes_pp,
Alon Levy4c19ebb2012-02-24 23:19:29 +0200120 qxl->guest_primary.bits_pp);
Alon Levy4c19ebb2012-02-24 23:19:29 +0200121 if (qxl->guest_primary.qxl_stride > 0) {
Gerd Hoffmann30f1e662014-06-18 11:03:15 +0200122 pixman_format_code_t format =
123 qemu_default_pixman_format(qxl->guest_primary.bits_pp, true);
Gerd Hoffmannda229ef2013-02-28 10:48:02 +0100124 surface = qemu_create_displaysurface_from
Gerd Hoffmann979f7ef2018-09-19 12:30:57 +0200125 (width,
126 height,
Gerd Hoffmann30f1e662014-06-18 11:03:15 +0200127 format,
Gerd Hoffmann2f464b52012-12-10 07:41:07 +0100128 qxl->guest_primary.abs_stride,
Gerd Hoffmann30f1e662014-06-18 11:03:15 +0200129 qxl->guest_primary.data);
Alon Levy4c19ebb2012-02-24 23:19:29 +0200130 } else {
Gerd Hoffmannda229ef2013-02-28 10:48:02 +0100131 surface = qemu_create_displaysurface
Gerd Hoffmann979f7ef2018-09-19 12:30:57 +0200132 (width,
133 height);
Alon Levy4c19ebb2012-02-24 23:19:29 +0200134 }
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100135 dpy_gfx_replace_surface(vga->con, surface);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200136 }
Gerd Hoffmannc58c7b92013-09-05 21:57:19 +0200137
138 if (!qxl->guest_primary.data) {
139 return;
140 }
Alon Levy81fb6f12012-02-24 23:19:31 +0200141 for (i = 0; i < qxl->num_dirty_rects; i++) {
142 if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200143 break;
144 }
Gerd Hoffmann503b3b32014-08-29 09:27:52 +0200145 if (qxl->dirty[i].left < 0 ||
146 qxl->dirty[i].top < 0 ||
147 qxl->dirty[i].left > qxl->dirty[i].right ||
Gerd Hoffmann788fbf02014-06-10 13:51:12 +0200148 qxl->dirty[i].top > qxl->dirty[i].bottom ||
Gerd Hoffmann979f7ef2018-09-19 12:30:57 +0200149 qxl->dirty[i].right > width ||
150 qxl->dirty[i].bottom > height) {
Gerd Hoffmann788fbf02014-06-10 13:51:12 +0200151 continue;
152 }
Gerd Hoffmanne2efc0a2012-02-27 11:05:09 +0100153 qxl_blit(qxl, qxl->dirty+i);
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100154 dpy_gfx_update(vga->con,
Gerd Hoffmanna93a4a22012-09-28 15:02:08 +0200155 qxl->dirty[i].left, qxl->dirty[i].top,
156 qxl->dirty[i].right - qxl->dirty[i].left,
157 qxl->dirty[i].bottom - qxl->dirty[i].top);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200158 }
Alon Levy81fb6f12012-02-24 23:19:31 +0200159 qxl->num_dirty_rects = 0;
160}
161
162/*
163 * use ssd.lock to protect render_update_cookie_num.
164 * qxl_render_update is called by io thread or vcpu thread, and the completion
Veres Lajos67cc32e2015-09-08 22:45:14 +0100165 * callbacks are called by spice_server thread, deferring to bh called from the
Alon Levy81fb6f12012-02-24 23:19:31 +0200166 * io thread.
167 */
168void qxl_render_update(PCIQXLDevice *qxl)
169{
170 QXLCookie *cookie;
171
172 qemu_mutex_lock(&qxl->ssd.lock);
173
Gerd Hoffmann5bd5c272018-04-27 13:55:28 +0200174 if (!runstate_is_running() || !qxl->guest_primary.commands ||
175 qxl->mode == QXL_MODE_UNDEFINED) {
Alon Levy81fb6f12012-02-24 23:19:31 +0200176 qxl_render_update_area_unlocked(qxl);
177 qemu_mutex_unlock(&qxl->ssd.lock);
178 return;
179 }
180
181 qxl->guest_primary.commands = 0;
182 qxl->render_update_cookie_num++;
183 qemu_mutex_unlock(&qxl->ssd.lock);
184 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
185 0);
186 qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
187 qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
188 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
189}
190
191void qxl_render_update_area_bh(void *opaque)
192{
193 PCIQXLDevice *qxl = opaque;
194
195 qemu_mutex_lock(&qxl->ssd.lock);
196 qxl_render_update_area_unlocked(qxl);
197 qemu_mutex_unlock(&qxl->ssd.lock);
198}
199
200void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
201{
202 qemu_mutex_lock(&qxl->ssd.lock);
Alon Levyd53291c2012-03-18 13:46:15 +0100203 trace_qxl_render_update_area_done(cookie);
Alon Levy81fb6f12012-02-24 23:19:31 +0200204 qemu_bh_schedule(qxl->update_area_bh);
205 qxl->render_update_cookie_num--;
206 qemu_mutex_unlock(&qxl->ssd.lock);
207 g_free(cookie);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200208}
209
Gerd Hoffmannb21330b2017-08-28 14:39:33 +0200210static void qxl_unpack_chunks(void *dest, size_t size, PCIQXLDevice *qxl,
211 QXLDataChunk *chunk, uint32_t group_id)
212{
213 uint32_t max_chunks = 32;
214 size_t offset = 0;
215 size_t bytes;
216
217 for (;;) {
218 bytes = MIN(size - offset, chunk->data_size);
219 memcpy(dest + offset, chunk->data, bytes);
220 offset += bytes;
221 if (offset == size) {
222 return;
223 }
224 chunk = qxl_phys2virt(qxl, chunk->next_chunk, group_id);
225 if (!chunk) {
226 return;
227 }
228 max_chunks--;
229 if (max_chunks == 0) {
230 return;
231 }
232 }
233}
234
235static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor,
236 uint32_t group_id)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200237{
238 QEMUCursor *c;
Peter Wu36ffc122018-09-03 16:54:47 +0200239 uint8_t *and_mask, *xor_mask;
Markus Armbruster66d3f192011-11-09 09:52:55 +0100240 size_t size;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200241
242 c = cursor_alloc(cursor->header.width, cursor->header.height);
243 c->hot_x = cursor->header.hot_spot_x;
244 c->hot_y = cursor->header.hot_spot_y;
245 switch (cursor->header.type) {
Peter Wu36ffc122018-09-03 16:54:47 +0200246 case SPICE_CURSOR_TYPE_MONO:
247 /* Assume that the full cursor is available in a single chunk. */
248 size = 2 * cursor_get_mono_bpl(c) * c->height;
249 if (size != cursor->data_size) {
250 fprintf(stderr, "%s: bad monochrome cursor %ux%u with size %u\n",
251 __func__, c->width, c->height, cursor->data_size);
252 goto fail;
253 }
254 and_mask = cursor->chunk.data;
255 xor_mask = and_mask + cursor_get_mono_bpl(c) * c->height;
256 cursor_set_mono(c, 0xffffff, 0x000000, xor_mask, 1, and_mask);
257 if (qxl->debug > 2) {
258 cursor_print_ascii_art(c, "qxl/mono");
259 }
260 break;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200261 case SPICE_CURSOR_TYPE_ALPHA:
Gerd Hoffmannbfc10122013-06-03 10:36:54 +0200262 size = sizeof(uint32_t) * cursor->header.width * cursor->header.height;
Gerd Hoffmannb21330b2017-08-28 14:39:33 +0200263 qxl_unpack_chunks(c->data, size, qxl, &cursor->chunk, group_id);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200264 if (qxl->debug > 2) {
265 cursor_print_ascii_art(c, "qxl/alpha");
266 }
267 break;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200268 default:
269 fprintf(stderr, "%s: not implemented: type %d\n",
Alistair Francisa89f3642017-11-08 14:56:31 -0800270 __func__, cursor->header.type);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200271 goto fail;
272 }
273 return c;
274
275fail:
276 cursor_put(c);
277 return NULL;
278}
279
280
281/* called from spice server thread context only */
Alon Levyfae2afb2012-04-25 12:13:18 +0300282int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200283{
284 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
285 QXLCursor *cursor;
286 QEMUCursor *c;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200287
Alon Levyfae2afb2012-04-25 12:13:18 +0300288 if (!cmd) {
289 return 1;
290 }
291
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100292 if (!dpy_cursor_define_supported(qxl->vga.con)) {
Alon Levyfae2afb2012-04-25 12:13:18 +0300293 return 0;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200294 }
295
296 if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
Alistair Francisa89f3642017-11-08 14:56:31 -0800297 fprintf(stderr, "%s", __func__);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200298 qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
299 fprintf(stderr, "\n");
300 }
301 switch (cmd->type) {
302 case QXL_CURSOR_SET:
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200303 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
Alon Levyfae2afb2012-04-25 12:13:18 +0300304 if (!cursor) {
305 return 1;
306 }
Gerd Hoffmannb21330b2017-08-28 14:39:33 +0200307 c = qxl_cursor(qxl, cursor, ext->group_id);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200308 if (c == NULL) {
309 c = cursor_builtin_left_ptr();
310 }
Gerd Hoffmann07536092011-04-27 15:50:32 +0200311 qemu_mutex_lock(&qxl->ssd.lock);
312 if (qxl->ssd.cursor) {
313 cursor_put(qxl->ssd.cursor);
314 }
315 qxl->ssd.cursor = c;
316 qxl->ssd.mouse_x = cmd->u.set.position.x;
317 qxl->ssd.mouse_y = cmd->u.set.position.y;
318 qemu_mutex_unlock(&qxl->ssd.lock);
Gerd Hoffmann0b2824e2014-11-04 13:59:59 +0100319 qemu_bh_schedule(qxl->ssd.cursor_bh);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200320 break;
321 case QXL_CURSOR_MOVE:
Gerd Hoffmann07536092011-04-27 15:50:32 +0200322 qemu_mutex_lock(&qxl->ssd.lock);
323 qxl->ssd.mouse_x = cmd->u.position.x;
324 qxl->ssd.mouse_y = cmd->u.position.y;
325 qemu_mutex_unlock(&qxl->ssd.lock);
Gerd Hoffmann0b2824e2014-11-04 13:59:59 +0100326 qemu_bh_schedule(qxl->ssd.cursor_bh);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200327 break;
328 }
Alon Levyfae2afb2012-04-25 12:13:18 +0300329 return 0;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200330}