]> git.proxmox.com Git - qemu.git/blame - hw/qxl-render.c
update VERSION for 1.4.1
[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
e2efc0a3 24static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect)
a19cbfb3 25{
4c19ebb5 26 uint8_t *src;
b6e9f637 27 uint8_t *dst = ds_get_data(qxl->vga.ds);
a19cbfb3
GH
28 int len, i;
29
e2efc0a3 30 if (is_buffer_shared(qxl->vga.ds->surface)) {
4c19ebb5
AL
31 return;
32 }
33 if (!qxl->guest_primary.data) {
d53291cf 34 trace_qxl_render_blit_guest_primary_initialized();
4c19ebb5
AL
35 qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
36 }
d53291cf 37 trace_qxl_render_blit(qxl->guest_primary.qxl_stride,
4c19ebb5
AL
38 rect->left, rect->right, rect->top, rect->bottom);
39 src = qxl->guest_primary.data;
e2efc0a3
GH
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 }
0e2487bd 48 dst += rect->top * qxl->guest_primary.abs_stride;
a19cbfb3
GH
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);
0e2487bd 55 dst += qxl->guest_primary.abs_stride;
e2efc0a3 56 src += qxl->guest_primary.qxl_stride;
a19cbfb3
GH
57 }
58}
59
60void qxl_render_resize(PCIQXLDevice *qxl)
61{
62 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
63
0e2487bd
GH
64 qxl->guest_primary.qxl_stride = sc->stride;
65 qxl->guest_primary.abs_stride = abs(sc->stride);
a19cbfb3
GH
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
81fb6f15
AL
90static 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
98static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
a19cbfb3
GH
99{
100 VGACommonState *vga = &qxl->vga;
81fb6f15 101 int i;
a19cbfb3
GH
102
103 if (qxl->guest_primary.resized) {
104 qxl->guest_primary.resized = 0;
b1950430 105 qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
81fb6f15
AL
106 qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
107 qxl->num_dirty_rects = 1;
d53291cf 108 trace_qxl_render_guest_primary_resized(
a19cbfb3
GH
109 qxl->guest_primary.surface.width,
110 qxl->guest_primary.surface.height,
0e2487bd 111 qxl->guest_primary.qxl_stride,
a19cbfb3 112 qxl->guest_primary.bytes_pp,
4c19ebb5 113 qxl->guest_primary.bits_pp);
4c19ebb5
AL
114 if (qxl->guest_primary.qxl_stride > 0) {
115 qemu_free_displaysurface(vga->ds);
2f464b5a
GH
116 vga->ds->surface = qemu_create_displaysurface_from
117 (qxl->guest_primary.surface.width,
118 qxl->guest_primary.surface.height,
119 qxl->guest_primary.bits_pp,
120 qxl->guest_primary.abs_stride,
d49fed4c
GH
121 qxl->guest_primary.data,
122 false);
4c19ebb5
AL
123 } else {
124 qemu_resize_displaysurface(vga->ds,
125 qxl->guest_primary.surface.width,
126 qxl->guest_primary.surface.height);
127 }
a93a4a22 128 dpy_gfx_resize(vga->ds);
a19cbfb3 129 }
81fb6f15
AL
130 for (i = 0; i < qxl->num_dirty_rects; i++) {
131 if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
a19cbfb3
GH
132 break;
133 }
e2efc0a3 134 qxl_blit(qxl, qxl->dirty+i);
a93a4a22
GH
135 dpy_gfx_update(vga->ds,
136 qxl->dirty[i].left, qxl->dirty[i].top,
137 qxl->dirty[i].right - qxl->dirty[i].left,
138 qxl->dirty[i].bottom - qxl->dirty[i].top);
a19cbfb3 139 }
81fb6f15
AL
140 qxl->num_dirty_rects = 0;
141}
142
143/*
144 * use ssd.lock to protect render_update_cookie_num.
145 * qxl_render_update is called by io thread or vcpu thread, and the completion
146 * callbacks are called by spice_server thread, defering to bh called from the
147 * io thread.
148 */
149void qxl_render_update(PCIQXLDevice *qxl)
150{
151 QXLCookie *cookie;
152
153 qemu_mutex_lock(&qxl->ssd.lock);
154
155 if (!runstate_is_running() || !qxl->guest_primary.commands) {
156 qxl_render_update_area_unlocked(qxl);
157 qemu_mutex_unlock(&qxl->ssd.lock);
158 return;
159 }
160
161 qxl->guest_primary.commands = 0;
162 qxl->render_update_cookie_num++;
163 qemu_mutex_unlock(&qxl->ssd.lock);
164 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
165 0);
166 qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
167 qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
168 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
169}
170
171void qxl_render_update_area_bh(void *opaque)
172{
173 PCIQXLDevice *qxl = opaque;
174
175 qemu_mutex_lock(&qxl->ssd.lock);
176 qxl_render_update_area_unlocked(qxl);
177 qemu_mutex_unlock(&qxl->ssd.lock);
178}
179
180void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
181{
182 qemu_mutex_lock(&qxl->ssd.lock);
d53291cf 183 trace_qxl_render_update_area_done(cookie);
81fb6f15
AL
184 qemu_bh_schedule(qxl->update_area_bh);
185 qxl->render_update_cookie_num--;
186 qemu_mutex_unlock(&qxl->ssd.lock);
187 g_free(cookie);
a19cbfb3
GH
188}
189
190static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
191{
192 QEMUCursor *c;
193 uint8_t *image, *mask;
66d3f196 194 size_t size;
a19cbfb3
GH
195
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");
205 }
206 break;
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");
213 }
214 break;
215 default:
216 fprintf(stderr, "%s: not implemented: type %d\n",
217 __FUNCTION__, cursor->header.type);
218 goto fail;
219 }
220 return c;
221
222fail:
223 cursor_put(c);
224 return NULL;
225}
226
227
228/* called from spice server thread context only */
fae2afb1 229int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
a19cbfb3
GH
230{
231 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
232 QXLCursor *cursor;
233 QEMUCursor *c;
a19cbfb3 234
fae2afb1
AL
235 if (!cmd) {
236 return 1;
237 }
238
bf2fde70 239 if (!dpy_cursor_define_supported(qxl->ssd.ds)) {
fae2afb1 240 return 0;
a19cbfb3
GH
241 }
242
243 if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
244 fprintf(stderr, "%s", __FUNCTION__);
245 qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
246 fprintf(stderr, "\n");
247 }
248 switch (cmd->type) {
249 case QXL_CURSOR_SET:
a19cbfb3 250 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
fae2afb1
AL
251 if (!cursor) {
252 return 1;
253 }
a19cbfb3
GH
254 if (cursor->chunk.data_size != cursor->data_size) {
255 fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__);
fae2afb1 256 return 1;
a19cbfb3
GH
257 }
258 c = qxl_cursor(qxl, cursor);
259 if (c == NULL) {
260 c = cursor_builtin_left_ptr();
261 }
07536094
GH
262 qemu_mutex_lock(&qxl->ssd.lock);
263 if (qxl->ssd.cursor) {
264 cursor_put(qxl->ssd.cursor);
265 }
266 qxl->ssd.cursor = c;
267 qxl->ssd.mouse_x = cmd->u.set.position.x;
268 qxl->ssd.mouse_y = cmd->u.set.position.y;
269 qemu_mutex_unlock(&qxl->ssd.lock);
a19cbfb3
GH
270 break;
271 case QXL_CURSOR_MOVE:
07536094
GH
272 qemu_mutex_lock(&qxl->ssd.lock);
273 qxl->ssd.mouse_x = cmd->u.position.x;
274 qxl->ssd.mouse_y = cmd->u.position.y;
275 qemu_mutex_unlock(&qxl->ssd.lock);
a19cbfb3
GH
276 break;
277 }
fae2afb1 278 return 0;
a19cbfb3 279}