]> git.proxmox.com Git - qemu.git/blob - hw/qxl-render.c
b66c168ef676891d5b9c76a814cd936f6d8b397a
[qemu.git] / hw / 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
24 static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect)
25 {
26 uint8_t *src;
27 uint8_t *dst = qxl->vga.ds->surface->data;
28 int len, i;
29
30 if (is_buffer_shared(qxl->vga.ds->surface)) {
31 return;
32 }
33 if (!qxl->guest_primary.data) {
34 trace_qxl_render_blit_guest_primary_initialized();
35 qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
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
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", __FUNCTION__,
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
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
98 static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
99 {
100 VGACommonState *vga = &qxl->vga;
101 int i;
102
103 if (qxl->guest_primary.resized) {
104 qxl->guest_primary.resized = 0;
105 qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
106 qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
107 qxl->num_dirty_rects = 1;
108 trace_qxl_render_guest_primary_resized(
109 qxl->guest_primary.surface.width,
110 qxl->guest_primary.surface.height,
111 qxl->guest_primary.qxl_stride,
112 qxl->guest_primary.bytes_pp,
113 qxl->guest_primary.bits_pp);
114 if (qxl->guest_primary.qxl_stride > 0) {
115 qemu_free_displaysurface(vga->ds);
116 qemu_create_displaysurface_from(qxl->guest_primary.surface.width,
117 qxl->guest_primary.surface.height,
118 qxl->guest_primary.bits_pp,
119 qxl->guest_primary.abs_stride,
120 qxl->guest_primary.data);
121 } else {
122 qemu_resize_displaysurface(vga->ds,
123 qxl->guest_primary.surface.width,
124 qxl->guest_primary.surface.height);
125 }
126 dpy_resize(vga->ds);
127 }
128 for (i = 0; i < qxl->num_dirty_rects; i++) {
129 if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
130 break;
131 }
132 qxl_blit(qxl, qxl->dirty+i);
133 dpy_update(vga->ds,
134 qxl->dirty[i].left, qxl->dirty[i].top,
135 qxl->dirty[i].right - qxl->dirty[i].left,
136 qxl->dirty[i].bottom - qxl->dirty[i].top);
137 }
138 qxl->num_dirty_rects = 0;
139 }
140
141 /*
142 * use ssd.lock to protect render_update_cookie_num.
143 * qxl_render_update is called by io thread or vcpu thread, and the completion
144 * callbacks are called by spice_server thread, defering to bh called from the
145 * io thread.
146 */
147 void qxl_render_update(PCIQXLDevice *qxl)
148 {
149 QXLCookie *cookie;
150
151 qemu_mutex_lock(&qxl->ssd.lock);
152
153 if (!runstate_is_running() || !qxl->guest_primary.commands) {
154 qxl_render_update_area_unlocked(qxl);
155 qemu_mutex_unlock(&qxl->ssd.lock);
156 return;
157 }
158
159 qxl->guest_primary.commands = 0;
160 qxl->render_update_cookie_num++;
161 qemu_mutex_unlock(&qxl->ssd.lock);
162 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
163 0);
164 qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
165 qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
166 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
167 }
168
169 void qxl_render_update_area_bh(void *opaque)
170 {
171 PCIQXLDevice *qxl = opaque;
172
173 qemu_mutex_lock(&qxl->ssd.lock);
174 qxl_render_update_area_unlocked(qxl);
175 qemu_mutex_unlock(&qxl->ssd.lock);
176 }
177
178 void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
179 {
180 qemu_mutex_lock(&qxl->ssd.lock);
181 trace_qxl_render_update_area_done(cookie);
182 qemu_bh_schedule(qxl->update_area_bh);
183 qxl->render_update_cookie_num--;
184 qemu_mutex_unlock(&qxl->ssd.lock);
185 g_free(cookie);
186 }
187
188 static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
189 {
190 QEMUCursor *c;
191 uint8_t *image, *mask;
192 size_t size;
193
194 c = cursor_alloc(cursor->header.width, cursor->header.height);
195 c->hot_x = cursor->header.hot_spot_x;
196 c->hot_y = cursor->header.hot_spot_y;
197 switch (cursor->header.type) {
198 case SPICE_CURSOR_TYPE_ALPHA:
199 size = cursor->header.width * cursor->header.height * sizeof(uint32_t);
200 memcpy(c->data, cursor->chunk.data, size);
201 if (qxl->debug > 2) {
202 cursor_print_ascii_art(c, "qxl/alpha");
203 }
204 break;
205 case SPICE_CURSOR_TYPE_MONO:
206 mask = cursor->chunk.data;
207 image = mask + cursor_get_mono_bpl(c) * c->width;
208 cursor_set_mono(c, 0xffffff, 0x000000, image, 1, mask);
209 if (qxl->debug > 2) {
210 cursor_print_ascii_art(c, "qxl/mono");
211 }
212 break;
213 default:
214 fprintf(stderr, "%s: not implemented: type %d\n",
215 __FUNCTION__, cursor->header.type);
216 goto fail;
217 }
218 return c;
219
220 fail:
221 cursor_put(c);
222 return NULL;
223 }
224
225
226 /* called from spice server thread context only */
227 int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
228 {
229 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
230 QXLCursor *cursor;
231 QEMUCursor *c;
232
233 if (!cmd) {
234 return 1;
235 }
236
237 if (!qxl->ssd.ds->mouse_set || !qxl->ssd.ds->cursor_define) {
238 return 0;
239 }
240
241 if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
242 fprintf(stderr, "%s", __FUNCTION__);
243 qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
244 fprintf(stderr, "\n");
245 }
246 switch (cmd->type) {
247 case QXL_CURSOR_SET:
248 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
249 if (!cursor) {
250 return 1;
251 }
252 if (cursor->chunk.data_size != cursor->data_size) {
253 fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__);
254 return 1;
255 }
256 c = qxl_cursor(qxl, cursor);
257 if (c == NULL) {
258 c = cursor_builtin_left_ptr();
259 }
260 qemu_mutex_lock(&qxl->ssd.lock);
261 if (qxl->ssd.cursor) {
262 cursor_put(qxl->ssd.cursor);
263 }
264 qxl->ssd.cursor = c;
265 qxl->ssd.mouse_x = cmd->u.set.position.x;
266 qxl->ssd.mouse_y = cmd->u.set.position.y;
267 qemu_mutex_unlock(&qxl->ssd.lock);
268 break;
269 case QXL_CURSOR_MOVE:
270 qemu_mutex_lock(&qxl->ssd.lock);
271 qxl->ssd.mouse_x = cmd->u.position.x;
272 qxl->ssd.mouse_y = cmd->u.position.y;
273 qemu_mutex_unlock(&qxl->ssd.lock);
274 break;
275 }
276 return 0;
277 }