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