]> git.proxmox.com Git - qemu.git/blame - hw/qxl-render.c
qxl: introduce QXLCookie
[qemu.git] / hw / qxl-render.c
CommitLineData
a19cbfb3
GH
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
24static void qxl_flip(PCIQXLDevice *qxl, QXLRect *rect)
25{
4c19ebb5
AL
26 uint8_t *src;
27 uint8_t *dst = qxl->vga.ds->surface->data;
a19cbfb3
GH
28 int len, i;
29
4c19ebb5
AL
30 if (qxl->guest_primary.qxl_stride > 0) {
31 return;
32 }
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);
36 }
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;
a19cbfb3 41 src += (qxl->guest_primary.surface.height - rect->top - 1) *
0e2487bd
GH
42 qxl->guest_primary.abs_stride;
43 dst += rect->top * qxl->guest_primary.abs_stride;
a19cbfb3
GH
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;
47
48 for (i = rect->top; i < rect->bottom; i++) {
49 memcpy(dst, src, len);
0e2487bd
GH
50 dst += qxl->guest_primary.abs_stride;
51 src -= qxl->guest_primary.abs_stride;
a19cbfb3
GH
52 }
53}
54
55void qxl_render_resize(PCIQXLDevice *qxl)
56{
57 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
58
0e2487bd
GH
59 qxl->guest_primary.qxl_stride = sc->stride;
60 qxl->guest_primary.abs_stride = abs(sc->stride);
a19cbfb3
GH
61 qxl->guest_primary.resized++;
62 switch (sc->format) {
63 case SPICE_SURFACE_FMT_16_555:
64 qxl->guest_primary.bytes_pp = 2;
65 qxl->guest_primary.bits_pp = 15;
66 break;
67 case SPICE_SURFACE_FMT_16_565:
68 qxl->guest_primary.bytes_pp = 2;
69 qxl->guest_primary.bits_pp = 16;
70 break;
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;
75 break;
76 default:
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;
81 break;
82 }
83}
84
85void qxl_render_update(PCIQXLDevice *qxl)
86{
87 VGACommonState *vga = &qxl->vga;
88 QXLRect dirty[32], update;
a053f1b1 89 int i, redraw = 0;
4c19ebb5 90 DisplaySurface *surface = vga->ds->surface;
a19cbfb3
GH
91
92 if (qxl->guest_primary.resized) {
93 qxl->guest_primary.resized = 0;
94
b1950430 95 qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
4c19ebb5 96 dprint(qxl, 1, "%s: %dx%d, stride %d, bpp %d, depth %d\n",
a19cbfb3
GH
97 __FUNCTION__,
98 qxl->guest_primary.surface.width,
99 qxl->guest_primary.surface.height,
0e2487bd 100 qxl->guest_primary.qxl_stride,
a19cbfb3 101 qxl->guest_primary.bytes_pp,
4c19ebb5
AL
102 qxl->guest_primary.bits_pp);
103 }
104 if (surface->width != qxl->guest_primary.surface.width ||
105 surface->height != qxl->guest_primary.surface.height) {
106 dprint(qxl, 1, "%s: resizing displaysurface to guest_primary\n",
107 __func__);
108 if (qxl->guest_primary.qxl_stride > 0) {
109 qemu_free_displaysurface(vga->ds);
a19cbfb3
GH
110 qemu_create_displaysurface_from(qxl->guest_primary.surface.width,
111 qxl->guest_primary.surface.height,
112 qxl->guest_primary.bits_pp,
0e2487bd 113 qxl->guest_primary.abs_stride,
4c19ebb5
AL
114 qxl->guest_primary.data);
115 } else {
116 qemu_resize_displaysurface(vga->ds,
117 qxl->guest_primary.surface.width,
118 qxl->guest_primary.surface.height);
119 }
a19cbfb3 120 }
a19cbfb3
GH
121 update.left = 0;
122 update.right = qxl->guest_primary.surface.width;
123 update.top = 0;
124 update.bottom = qxl->guest_primary.surface.height;
125
126 memset(dirty, 0, sizeof(dirty));
7844e448
GH
127 if (runstate_is_running() && qxl->guest_primary.commands) {
128 qxl->guest_primary.commands = 0;
129 qxl_spice_update_area(qxl, 0, &update,
2e1a98c9 130 dirty, ARRAY_SIZE(dirty), 1, QXL_SYNC, NULL);
7844e448 131 }
a053f1b1
GH
132 if (redraw) {
133 memset(dirty, 0, sizeof(dirty));
134 dirty[0] = update;
135 }
a19cbfb3
GH
136 for (i = 0; i < ARRAY_SIZE(dirty); i++) {
137 if (qemu_spice_rect_is_empty(dirty+i)) {
138 break;
139 }
4c19ebb5 140 qxl_flip(qxl, dirty+i);
a19cbfb3
GH
141 dpy_update(vga->ds,
142 dirty[i].left, dirty[i].top,
143 dirty[i].right - dirty[i].left,
144 dirty[i].bottom - dirty[i].top);
145 }
146}
147
148static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
149{
150 QEMUCursor *c;
151 uint8_t *image, *mask;
66d3f196 152 size_t size;
a19cbfb3
GH
153
154 c = cursor_alloc(cursor->header.width, cursor->header.height);
155 c->hot_x = cursor->header.hot_spot_x;
156 c->hot_y = cursor->header.hot_spot_y;
157 switch (cursor->header.type) {
158 case SPICE_CURSOR_TYPE_ALPHA:
159 size = cursor->header.width * cursor->header.height * sizeof(uint32_t);
160 memcpy(c->data, cursor->chunk.data, size);
161 if (qxl->debug > 2) {
162 cursor_print_ascii_art(c, "qxl/alpha");
163 }
164 break;
165 case SPICE_CURSOR_TYPE_MONO:
166 mask = cursor->chunk.data;
167 image = mask + cursor_get_mono_bpl(c) * c->width;
168 cursor_set_mono(c, 0xffffff, 0x000000, image, 1, mask);
169 if (qxl->debug > 2) {
170 cursor_print_ascii_art(c, "qxl/mono");
171 }
172 break;
173 default:
174 fprintf(stderr, "%s: not implemented: type %d\n",
175 __FUNCTION__, cursor->header.type);
176 goto fail;
177 }
178 return c;
179
180fail:
181 cursor_put(c);
182 return NULL;
183}
184
185
186/* called from spice server thread context only */
187void qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
188{
189 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
190 QXLCursor *cursor;
191 QEMUCursor *c;
a19cbfb3
GH
192
193 if (!qxl->ssd.ds->mouse_set || !qxl->ssd.ds->cursor_define) {
194 return;
195 }
196
197 if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
198 fprintf(stderr, "%s", __FUNCTION__);
199 qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
200 fprintf(stderr, "\n");
201 }
202 switch (cmd->type) {
203 case QXL_CURSOR_SET:
a19cbfb3
GH
204 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
205 if (cursor->chunk.data_size != cursor->data_size) {
206 fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__);
207 return;
208 }
209 c = qxl_cursor(qxl, cursor);
210 if (c == NULL) {
211 c = cursor_builtin_left_ptr();
212 }
07536094
GH
213 qemu_mutex_lock(&qxl->ssd.lock);
214 if (qxl->ssd.cursor) {
215 cursor_put(qxl->ssd.cursor);
216 }
217 qxl->ssd.cursor = c;
218 qxl->ssd.mouse_x = cmd->u.set.position.x;
219 qxl->ssd.mouse_y = cmd->u.set.position.y;
220 qemu_mutex_unlock(&qxl->ssd.lock);
a19cbfb3
GH
221 break;
222 case QXL_CURSOR_MOVE:
07536094
GH
223 qemu_mutex_lock(&qxl->ssd.lock);
224 qxl->ssd.mouse_x = cmd->u.position.x;
225 qxl->ssd.mouse_y = cmd->u.position.y;
226 qemu_mutex_unlock(&qxl->ssd.lock);
a19cbfb3
GH
227 break;
228 }
229}