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