2 * qxl local rendering (aka display on sdl/vnc)
4 * Copyright (C) 2010 Red Hat, Inc.
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.
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.
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/>.
24 static void qxl_flip(PCIQXLDevice *qxl, QXLRect *rect)
27 uint8_t *dst = qxl->vga.ds->surface->data;
30 if (qxl->guest_primary.qxl_stride > 0) {
33 if (!qxl->guest_primary.data) {
34 dprint(qxl, 1, "%s: initializing guest_primary.data\n", __func__);
35 qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
37 dprint(qxl, 1, "%s: stride %d, [%d, %d, %d, %d]\n", __func__,
38 qxl->guest_primary.qxl_stride,
39 rect->left, rect->right, rect->top, rect->bottom);
40 src = qxl->guest_primary.data;
41 src += (qxl->guest_primary.surface.height - rect->top - 1) *
42 qxl->guest_primary.abs_stride;
43 dst += rect->top * qxl->guest_primary.abs_stride;
44 src += rect->left * qxl->guest_primary.bytes_pp;
45 dst += rect->left * qxl->guest_primary.bytes_pp;
46 len = (rect->right - rect->left) * qxl->guest_primary.bytes_pp;
48 for (i = rect->top; i < rect->bottom; i++) {
49 memcpy(dst, src, len);
50 dst += qxl->guest_primary.abs_stride;
51 src -= qxl->guest_primary.abs_stride;
55 void qxl_render_resize(PCIQXLDevice *qxl)
57 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
59 qxl->guest_primary.qxl_stride = sc->stride;
60 qxl->guest_primary.abs_stride = abs(sc->stride);
61 qxl->guest_primary.resized++;
63 case SPICE_SURFACE_FMT_16_555:
64 qxl->guest_primary.bytes_pp = 2;
65 qxl->guest_primary.bits_pp = 15;
67 case SPICE_SURFACE_FMT_16_565:
68 qxl->guest_primary.bytes_pp = 2;
69 qxl->guest_primary.bits_pp = 16;
71 case SPICE_SURFACE_FMT_32_xRGB:
72 case SPICE_SURFACE_FMT_32_ARGB:
73 qxl->guest_primary.bytes_pp = 4;
74 qxl->guest_primary.bits_pp = 32;
77 fprintf(stderr, "%s: unhandled format: %x\n", __FUNCTION__,
78 qxl->guest_primary.surface.format);
79 qxl->guest_primary.bytes_pp = 4;
80 qxl->guest_primary.bits_pp = 32;
85 static void qxl_set_rect_to_surface(PCIQXLDevice *qxl, QXLRect *area)
88 area->right = qxl->guest_primary.surface.width;
90 area->bottom = qxl->guest_primary.surface.height;
93 static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
95 VGACommonState *vga = &qxl->vga;
97 DisplaySurface *surface = vga->ds->surface;
99 if (qxl->guest_primary.resized) {
100 qxl->guest_primary.resized = 0;
101 qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
102 qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
103 qxl->num_dirty_rects = 1;
104 dprint(qxl, 1, "%s: %dx%d, stride %d, bpp %d, depth %d\n",
106 qxl->guest_primary.surface.width,
107 qxl->guest_primary.surface.height,
108 qxl->guest_primary.qxl_stride,
109 qxl->guest_primary.bytes_pp,
110 qxl->guest_primary.bits_pp);
112 if (surface->width != qxl->guest_primary.surface.width ||
113 surface->height != qxl->guest_primary.surface.height) {
114 if (qxl->guest_primary.qxl_stride > 0) {
115 dprint(qxl, 1, "%s: using guest_primary for displaysurface\n",
117 qemu_free_displaysurface(vga->ds);
118 qemu_create_displaysurface_from(qxl->guest_primary.surface.width,
119 qxl->guest_primary.surface.height,
120 qxl->guest_primary.bits_pp,
121 qxl->guest_primary.abs_stride,
122 qxl->guest_primary.data);
124 dprint(qxl, 1, "%s: resizing displaysurface to guest_primary\n",
126 qemu_resize_displaysurface(vga->ds,
127 qxl->guest_primary.surface.width,
128 qxl->guest_primary.surface.height);
131 for (i = 0; i < qxl->num_dirty_rects; i++) {
132 if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
135 qxl_flip(qxl, qxl->dirty+i);
137 qxl->dirty[i].left, qxl->dirty[i].top,
138 qxl->dirty[i].right - qxl->dirty[i].left,
139 qxl->dirty[i].bottom - qxl->dirty[i].top);
141 qxl->num_dirty_rects = 0;
145 * use ssd.lock to protect render_update_cookie_num.
146 * qxl_render_update is called by io thread or vcpu thread, and the completion
147 * callbacks are called by spice_server thread, defering to bh called from the
150 void qxl_render_update(PCIQXLDevice *qxl)
154 qemu_mutex_lock(&qxl->ssd.lock);
156 if (!runstate_is_running() || !qxl->guest_primary.commands) {
157 qxl_render_update_area_unlocked(qxl);
158 qemu_mutex_unlock(&qxl->ssd.lock);
162 qxl->guest_primary.commands = 0;
163 qxl->render_update_cookie_num++;
164 qemu_mutex_unlock(&qxl->ssd.lock);
165 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
167 qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
168 qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
169 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
172 void qxl_render_update_area_bh(void *opaque)
174 PCIQXLDevice *qxl = opaque;
176 qemu_mutex_lock(&qxl->ssd.lock);
177 qxl_render_update_area_unlocked(qxl);
178 qemu_mutex_unlock(&qxl->ssd.lock);
181 void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
183 qemu_mutex_lock(&qxl->ssd.lock);
184 qemu_bh_schedule(qxl->update_area_bh);
185 qxl->render_update_cookie_num--;
186 qemu_mutex_unlock(&qxl->ssd.lock);
190 static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
193 uint8_t *image, *mask;
196 c = cursor_alloc(cursor->header.width, cursor->header.height);
197 c->hot_x = cursor->header.hot_spot_x;
198 c->hot_y = cursor->header.hot_spot_y;
199 switch (cursor->header.type) {
200 case SPICE_CURSOR_TYPE_ALPHA:
201 size = cursor->header.width * cursor->header.height * sizeof(uint32_t);
202 memcpy(c->data, cursor->chunk.data, size);
203 if (qxl->debug > 2) {
204 cursor_print_ascii_art(c, "qxl/alpha");
207 case SPICE_CURSOR_TYPE_MONO:
208 mask = cursor->chunk.data;
209 image = mask + cursor_get_mono_bpl(c) * c->width;
210 cursor_set_mono(c, 0xffffff, 0x000000, image, 1, mask);
211 if (qxl->debug > 2) {
212 cursor_print_ascii_art(c, "qxl/mono");
216 fprintf(stderr, "%s: not implemented: type %d\n",
217 __FUNCTION__, cursor->header.type);
228 /* called from spice server thread context only */
229 void qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
231 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
235 if (!qxl->ssd.ds->mouse_set || !qxl->ssd.ds->cursor_define) {
239 if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
240 fprintf(stderr, "%s", __FUNCTION__);
241 qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
242 fprintf(stderr, "\n");
246 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
247 if (cursor->chunk.data_size != cursor->data_size) {
248 fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__);
251 c = qxl_cursor(qxl, cursor);
253 c = cursor_builtin_left_ptr();
255 qemu_mutex_lock(&qxl->ssd.lock);
256 if (qxl->ssd.cursor) {
257 cursor_put(qxl->ssd.cursor);
260 qxl->ssd.mouse_x = cmd->u.set.position.x;
261 qxl->ssd.mouse_y = cmd->u.set.position.y;
262 qemu_mutex_unlock(&qxl->ssd.lock);
264 case QXL_CURSOR_MOVE:
265 qemu_mutex_lock(&qxl->ssd.lock);
266 qxl->ssd.mouse_x = cmd->u.position.x;
267 qxl->ssd.mouse_y = cmd->u.position.y;
268 qemu_mutex_unlock(&qxl->ssd.lock);