]> git.proxmox.com Git - mirror_qemu.git/blob - hw/display/qxl-render.c
Merge remote-tracking branch 'remotes/ehabkost/tags/machine-next-pull-request' into...
[mirror_qemu.git] / hw / display / qxl-render.c
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 "trace.h"
25
26 static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect)
27 {
28 DisplaySurface *surface = qemu_console_surface(qxl->vga.con);
29 uint8_t *dst = surface_data(surface);
30 uint8_t *src;
31 int len, i;
32
33 if (is_buffer_shared(surface)) {
34 return;
35 }
36 trace_qxl_render_blit(qxl->guest_primary.qxl_stride,
37 rect->left, rect->right, rect->top, rect->bottom);
38 src = qxl->guest_primary.data;
39 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 }
47 dst += rect->top * qxl->guest_primary.abs_stride;
48 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);
54 dst += qxl->guest_primary.abs_stride;
55 src += qxl->guest_primary.qxl_stride;
56 }
57 }
58
59 void qxl_render_resize(PCIQXLDevice *qxl)
60 {
61 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
62
63 qxl->guest_primary.qxl_stride = sc->stride;
64 qxl->guest_primary.abs_stride = abs(sc->stride);
65 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:
81 fprintf(stderr, "%s: unhandled format: %x\n", __func__,
82 qxl->guest_primary.surface.format);
83 qxl->guest_primary.bytes_pp = 4;
84 qxl->guest_primary.bits_pp = 32;
85 break;
86 }
87 }
88
89 static 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
97 static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
98 {
99 VGACommonState *vga = &qxl->vga;
100 DisplaySurface *surface;
101 int i;
102
103 if (qxl->guest_primary.resized) {
104 qxl->guest_primary.resized = 0;
105 qxl->guest_primary.data = qxl_phys2virt(qxl,
106 qxl->guest_primary.surface.mem,
107 MEMSLOT_GROUP_GUEST);
108 if (!qxl->guest_primary.data) {
109 return;
110 }
111 qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
112 qxl->num_dirty_rects = 1;
113 trace_qxl_render_guest_primary_resized(
114 qxl->guest_primary.surface.width,
115 qxl->guest_primary.surface.height,
116 qxl->guest_primary.qxl_stride,
117 qxl->guest_primary.bytes_pp,
118 qxl->guest_primary.bits_pp);
119 if (qxl->guest_primary.qxl_stride > 0) {
120 pixman_format_code_t format =
121 qemu_default_pixman_format(qxl->guest_primary.bits_pp, true);
122 surface = qemu_create_displaysurface_from
123 (qxl->guest_primary.surface.width,
124 qxl->guest_primary.surface.height,
125 format,
126 qxl->guest_primary.abs_stride,
127 qxl->guest_primary.data);
128 } else {
129 surface = qemu_create_displaysurface
130 (qxl->guest_primary.surface.width,
131 qxl->guest_primary.surface.height);
132 }
133 dpy_gfx_replace_surface(vga->con, surface);
134 }
135
136 if (!qxl->guest_primary.data) {
137 return;
138 }
139 for (i = 0; i < qxl->num_dirty_rects; i++) {
140 if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
141 break;
142 }
143 if (qxl->dirty[i].left < 0 ||
144 qxl->dirty[i].top < 0 ||
145 qxl->dirty[i].left > qxl->dirty[i].right ||
146 qxl->dirty[i].top > qxl->dirty[i].bottom ||
147 qxl->dirty[i].right > qxl->guest_primary.surface.width ||
148 qxl->dirty[i].bottom > qxl->guest_primary.surface.height) {
149 continue;
150 }
151 qxl_blit(qxl, qxl->dirty+i);
152 dpy_gfx_update(vga->con,
153 qxl->dirty[i].left, qxl->dirty[i].top,
154 qxl->dirty[i].right - qxl->dirty[i].left,
155 qxl->dirty[i].bottom - qxl->dirty[i].top);
156 }
157 qxl->num_dirty_rects = 0;
158 }
159
160 /*
161 * use ssd.lock to protect render_update_cookie_num.
162 * qxl_render_update is called by io thread or vcpu thread, and the completion
163 * callbacks are called by spice_server thread, deferring to bh called from the
164 * io thread.
165 */
166 void qxl_render_update(PCIQXLDevice *qxl)
167 {
168 QXLCookie *cookie;
169
170 qemu_mutex_lock(&qxl->ssd.lock);
171
172 if (!runstate_is_running() || !qxl->guest_primary.commands ||
173 qxl->mode == QXL_MODE_UNDEFINED) {
174 qxl_render_update_area_unlocked(qxl);
175 qemu_mutex_unlock(&qxl->ssd.lock);
176 return;
177 }
178
179 qxl->guest_primary.commands = 0;
180 qxl->render_update_cookie_num++;
181 qemu_mutex_unlock(&qxl->ssd.lock);
182 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
183 0);
184 qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
185 qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
186 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
187 }
188
189 void qxl_render_update_area_bh(void *opaque)
190 {
191 PCIQXLDevice *qxl = opaque;
192
193 qemu_mutex_lock(&qxl->ssd.lock);
194 qxl_render_update_area_unlocked(qxl);
195 qemu_mutex_unlock(&qxl->ssd.lock);
196 }
197
198 void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
199 {
200 qemu_mutex_lock(&qxl->ssd.lock);
201 trace_qxl_render_update_area_done(cookie);
202 qemu_bh_schedule(qxl->update_area_bh);
203 qxl->render_update_cookie_num--;
204 qemu_mutex_unlock(&qxl->ssd.lock);
205 g_free(cookie);
206 }
207
208 static void qxl_unpack_chunks(void *dest, size_t size, PCIQXLDevice *qxl,
209 QXLDataChunk *chunk, uint32_t group_id)
210 {
211 uint32_t max_chunks = 32;
212 size_t offset = 0;
213 size_t bytes;
214
215 for (;;) {
216 bytes = MIN(size - offset, chunk->data_size);
217 memcpy(dest + offset, chunk->data, bytes);
218 offset += bytes;
219 if (offset == size) {
220 return;
221 }
222 chunk = qxl_phys2virt(qxl, chunk->next_chunk, group_id);
223 if (!chunk) {
224 return;
225 }
226 max_chunks--;
227 if (max_chunks == 0) {
228 return;
229 }
230 }
231 }
232
233 static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor,
234 uint32_t group_id)
235 {
236 QEMUCursor *c;
237 size_t size;
238
239 c = cursor_alloc(cursor->header.width, cursor->header.height);
240 c->hot_x = cursor->header.hot_spot_x;
241 c->hot_y = cursor->header.hot_spot_y;
242 switch (cursor->header.type) {
243 case SPICE_CURSOR_TYPE_ALPHA:
244 size = sizeof(uint32_t) * cursor->header.width * cursor->header.height;
245 qxl_unpack_chunks(c->data, size, qxl, &cursor->chunk, group_id);
246 if (qxl->debug > 2) {
247 cursor_print_ascii_art(c, "qxl/alpha");
248 }
249 break;
250 default:
251 fprintf(stderr, "%s: not implemented: type %d\n",
252 __func__, cursor->header.type);
253 goto fail;
254 }
255 return c;
256
257 fail:
258 cursor_put(c);
259 return NULL;
260 }
261
262
263 /* called from spice server thread context only */
264 int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
265 {
266 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
267 QXLCursor *cursor;
268 QEMUCursor *c;
269
270 if (!cmd) {
271 return 1;
272 }
273
274 if (!dpy_cursor_define_supported(qxl->vga.con)) {
275 return 0;
276 }
277
278 if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
279 fprintf(stderr, "%s", __func__);
280 qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
281 fprintf(stderr, "\n");
282 }
283 switch (cmd->type) {
284 case QXL_CURSOR_SET:
285 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
286 if (!cursor) {
287 return 1;
288 }
289 c = qxl_cursor(qxl, cursor, ext->group_id);
290 if (c == NULL) {
291 c = cursor_builtin_left_ptr();
292 }
293 qemu_mutex_lock(&qxl->ssd.lock);
294 if (qxl->ssd.cursor) {
295 cursor_put(qxl->ssd.cursor);
296 }
297 qxl->ssd.cursor = c;
298 qxl->ssd.mouse_x = cmd->u.set.position.x;
299 qxl->ssd.mouse_y = cmd->u.set.position.y;
300 qemu_mutex_unlock(&qxl->ssd.lock);
301 qemu_bh_schedule(qxl->ssd.cursor_bh);
302 break;
303 case QXL_CURSOR_MOVE:
304 qemu_mutex_lock(&qxl->ssd.lock);
305 qxl->ssd.mouse_x = cmd->u.position.x;
306 qxl->ssd.mouse_y = cmd->u.position.y;
307 qemu_mutex_unlock(&qxl->ssd.lock);
308 qemu_bh_schedule(qxl->ssd.cursor_bh);
309 break;
310 }
311 return 0;
312 }