]>
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 <[email protected]> | |
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 | ||
47df5154 | 22 | #include "qemu/osdep.h" |
47b43a1f | 23 | #include "qxl.h" |
54d31236 | 24 | #include "sysemu/runstate.h" |
b1829cde | 25 | #include "trace.h" |
a19cbfb3 | 26 | |
e2efc0a3 | 27 | static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect) |
a19cbfb3 | 28 | { |
c78f7137 GH |
29 | DisplaySurface *surface = qemu_console_surface(qxl->vga.con); |
30 | uint8_t *dst = surface_data(surface); | |
4c19ebb5 | 31 | uint8_t *src; |
a19cbfb3 GH |
32 | int len, i; |
33 | ||
c78f7137 | 34 | if (is_buffer_shared(surface)) { |
4c19ebb5 AL |
35 | return; |
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 | ||
60 | void 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: | |
a89f364a | 82 | fprintf(stderr, "%s: unhandled format: %x\n", __func__, |
a19cbfb3 GH |
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 |
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) | |
a19cbfb3 GH |
99 | { |
100 | VGACommonState *vga = &qxl->vga; | |
da229ef3 | 101 | DisplaySurface *surface; |
979f7ef8 GH |
102 | int width = qxl->guest_head0_width ?: qxl->guest_primary.surface.width; |
103 | int height = qxl->guest_head0_height ?: qxl->guest_primary.surface.height; | |
81fb6f15 | 104 | int i; |
a19cbfb3 GH |
105 | |
106 | if (qxl->guest_primary.resized) { | |
107 | qxl->guest_primary.resized = 0; | |
c58c7b95 GH |
108 | qxl->guest_primary.data = qxl_phys2virt(qxl, |
109 | qxl->guest_primary.surface.mem, | |
110 | MEMSLOT_GROUP_GUEST); | |
111 | if (!qxl->guest_primary.data) { | |
112 | return; | |
113 | } | |
81fb6f15 AL |
114 | qxl_set_rect_to_surface(qxl, &qxl->dirty[0]); |
115 | qxl->num_dirty_rects = 1; | |
d53291cf | 116 | trace_qxl_render_guest_primary_resized( |
979f7ef8 GH |
117 | width, |
118 | height, | |
0e2487bd | 119 | qxl->guest_primary.qxl_stride, |
a19cbfb3 | 120 | qxl->guest_primary.bytes_pp, |
4c19ebb5 | 121 | qxl->guest_primary.bits_pp); |
4c19ebb5 | 122 | if (qxl->guest_primary.qxl_stride > 0) { |
30f1e661 GH |
123 | pixman_format_code_t format = |
124 | qemu_default_pixman_format(qxl->guest_primary.bits_pp, true); | |
da229ef3 | 125 | surface = qemu_create_displaysurface_from |
979f7ef8 GH |
126 | (width, |
127 | height, | |
30f1e661 | 128 | format, |
2f464b5a | 129 | qxl->guest_primary.abs_stride, |
30f1e661 | 130 | qxl->guest_primary.data); |
4c19ebb5 | 131 | } else { |
da229ef3 | 132 | surface = qemu_create_displaysurface |
979f7ef8 GH |
133 | (width, |
134 | height); | |
4c19ebb5 | 135 | } |
c78f7137 | 136 | dpy_gfx_replace_surface(vga->con, surface); |
a19cbfb3 | 137 | } |
c58c7b95 GH |
138 | |
139 | if (!qxl->guest_primary.data) { | |
140 | return; | |
141 | } | |
81fb6f15 AL |
142 | for (i = 0; i < qxl->num_dirty_rects; i++) { |
143 | if (qemu_spice_rect_is_empty(qxl->dirty+i)) { | |
a19cbfb3 GH |
144 | break; |
145 | } | |
503b3b33 GH |
146 | if (qxl->dirty[i].left < 0 || |
147 | qxl->dirty[i].top < 0 || | |
148 | qxl->dirty[i].left > qxl->dirty[i].right || | |
788fbf04 | 149 | qxl->dirty[i].top > qxl->dirty[i].bottom || |
979f7ef8 GH |
150 | qxl->dirty[i].right > width || |
151 | qxl->dirty[i].bottom > height) { | |
788fbf04 GH |
152 | continue; |
153 | } | |
e2efc0a3 | 154 | qxl_blit(qxl, qxl->dirty+i); |
c78f7137 | 155 | dpy_gfx_update(vga->con, |
a93a4a22 GH |
156 | qxl->dirty[i].left, qxl->dirty[i].top, |
157 | qxl->dirty[i].right - qxl->dirty[i].left, | |
158 | qxl->dirty[i].bottom - qxl->dirty[i].top); | |
a19cbfb3 | 159 | } |
81fb6f15 AL |
160 | qxl->num_dirty_rects = 0; |
161 | } | |
162 | ||
163 | /* | |
164 | * use ssd.lock to protect render_update_cookie_num. | |
165 | * qxl_render_update is called by io thread or vcpu thread, and the completion | |
67cc32eb | 166 | * callbacks are called by spice_server thread, deferring to bh called from the |
81fb6f15 AL |
167 | * io thread. |
168 | */ | |
169 | void qxl_render_update(PCIQXLDevice *qxl) | |
170 | { | |
171 | QXLCookie *cookie; | |
172 | ||
173 | qemu_mutex_lock(&qxl->ssd.lock); | |
174 | ||
5bd5c27c GH |
175 | if (!runstate_is_running() || !qxl->guest_primary.commands || |
176 | qxl->mode == QXL_MODE_UNDEFINED) { | |
81fb6f15 AL |
177 | qxl_render_update_area_unlocked(qxl); |
178 | qemu_mutex_unlock(&qxl->ssd.lock); | |
179 | return; | |
180 | } | |
181 | ||
182 | qxl->guest_primary.commands = 0; | |
183 | qxl->render_update_cookie_num++; | |
184 | qemu_mutex_unlock(&qxl->ssd.lock); | |
185 | cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA, | |
186 | 0); | |
187 | qxl_set_rect_to_surface(qxl, &cookie->u.render.area); | |
188 | qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL, | |
189 | 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie); | |
190 | } | |
191 | ||
192 | void qxl_render_update_area_bh(void *opaque) | |
193 | { | |
194 | PCIQXLDevice *qxl = opaque; | |
195 | ||
196 | qemu_mutex_lock(&qxl->ssd.lock); | |
197 | qxl_render_update_area_unlocked(qxl); | |
198 | qemu_mutex_unlock(&qxl->ssd.lock); | |
199 | } | |
200 | ||
201 | void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie) | |
202 | { | |
203 | qemu_mutex_lock(&qxl->ssd.lock); | |
d53291cf | 204 | trace_qxl_render_update_area_done(cookie); |
81fb6f15 AL |
205 | qemu_bh_schedule(qxl->update_area_bh); |
206 | qxl->render_update_cookie_num--; | |
207 | qemu_mutex_unlock(&qxl->ssd.lock); | |
208 | g_free(cookie); | |
a19cbfb3 GH |
209 | } |
210 | ||
b21330b5 GH |
211 | static void qxl_unpack_chunks(void *dest, size_t size, PCIQXLDevice *qxl, |
212 | QXLDataChunk *chunk, uint32_t group_id) | |
213 | { | |
214 | uint32_t max_chunks = 32; | |
215 | size_t offset = 0; | |
216 | size_t bytes; | |
217 | ||
218 | for (;;) { | |
219 | bytes = MIN(size - offset, chunk->data_size); | |
220 | memcpy(dest + offset, chunk->data, bytes); | |
221 | offset += bytes; | |
222 | if (offset == size) { | |
223 | return; | |
224 | } | |
225 | chunk = qxl_phys2virt(qxl, chunk->next_chunk, group_id); | |
226 | if (!chunk) { | |
227 | return; | |
228 | } | |
229 | max_chunks--; | |
230 | if (max_chunks == 0) { | |
231 | return; | |
232 | } | |
233 | } | |
234 | } | |
235 | ||
236 | static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor, | |
237 | uint32_t group_id) | |
a19cbfb3 GH |
238 | { |
239 | QEMUCursor *c; | |
36ffc122 | 240 | uint8_t *and_mask, *xor_mask; |
66d3f196 | 241 | size_t size; |
a19cbfb3 GH |
242 | |
243 | c = cursor_alloc(cursor->header.width, cursor->header.height); | |
244 | c->hot_x = cursor->header.hot_spot_x; | |
245 | c->hot_y = cursor->header.hot_spot_y; | |
246 | switch (cursor->header.type) { | |
36ffc122 PW |
247 | case SPICE_CURSOR_TYPE_MONO: |
248 | /* Assume that the full cursor is available in a single chunk. */ | |
249 | size = 2 * cursor_get_mono_bpl(c) * c->height; | |
250 | if (size != cursor->data_size) { | |
251 | fprintf(stderr, "%s: bad monochrome cursor %ux%u with size %u\n", | |
252 | __func__, c->width, c->height, cursor->data_size); | |
253 | goto fail; | |
254 | } | |
255 | and_mask = cursor->chunk.data; | |
256 | xor_mask = and_mask + cursor_get_mono_bpl(c) * c->height; | |
257 | cursor_set_mono(c, 0xffffff, 0x000000, xor_mask, 1, and_mask); | |
258 | if (qxl->debug > 2) { | |
259 | cursor_print_ascii_art(c, "qxl/mono"); | |
260 | } | |
261 | break; | |
a19cbfb3 | 262 | case SPICE_CURSOR_TYPE_ALPHA: |
bfc10122 | 263 | size = sizeof(uint32_t) * cursor->header.width * cursor->header.height; |
b21330b5 | 264 | qxl_unpack_chunks(c->data, size, qxl, &cursor->chunk, group_id); |
a19cbfb3 GH |
265 | if (qxl->debug > 2) { |
266 | cursor_print_ascii_art(c, "qxl/alpha"); | |
267 | } | |
268 | break; | |
a19cbfb3 GH |
269 | default: |
270 | fprintf(stderr, "%s: not implemented: type %d\n", | |
a89f364a | 271 | __func__, cursor->header.type); |
a19cbfb3 GH |
272 | goto fail; |
273 | } | |
274 | return c; | |
275 | ||
276 | fail: | |
277 | cursor_put(c); | |
278 | return NULL; | |
279 | } | |
280 | ||
281 | ||
282 | /* called from spice server thread context only */ | |
fae2afb1 | 283 | int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext) |
a19cbfb3 GH |
284 | { |
285 | QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id); | |
286 | QXLCursor *cursor; | |
287 | QEMUCursor *c; | |
a19cbfb3 | 288 | |
fae2afb1 AL |
289 | if (!cmd) { |
290 | return 1; | |
291 | } | |
292 | ||
c78f7137 | 293 | if (!dpy_cursor_define_supported(qxl->vga.con)) { |
fae2afb1 | 294 | return 0; |
a19cbfb3 GH |
295 | } |
296 | ||
297 | if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) { | |
a89f364a | 298 | fprintf(stderr, "%s", __func__); |
a19cbfb3 GH |
299 | qxl_log_cmd_cursor(qxl, cmd, ext->group_id); |
300 | fprintf(stderr, "\n"); | |
301 | } | |
302 | switch (cmd->type) { | |
303 | case QXL_CURSOR_SET: | |
a19cbfb3 | 304 | cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id); |
fae2afb1 AL |
305 | if (!cursor) { |
306 | return 1; | |
307 | } | |
b21330b5 | 308 | c = qxl_cursor(qxl, cursor, ext->group_id); |
a19cbfb3 GH |
309 | if (c == NULL) { |
310 | c = cursor_builtin_left_ptr(); | |
311 | } | |
07536094 GH |
312 | qemu_mutex_lock(&qxl->ssd.lock); |
313 | if (qxl->ssd.cursor) { | |
314 | cursor_put(qxl->ssd.cursor); | |
315 | } | |
316 | qxl->ssd.cursor = c; | |
317 | qxl->ssd.mouse_x = cmd->u.set.position.x; | |
318 | qxl->ssd.mouse_y = cmd->u.set.position.y; | |
319 | qemu_mutex_unlock(&qxl->ssd.lock); | |
0b2824e5 | 320 | qemu_bh_schedule(qxl->ssd.cursor_bh); |
a19cbfb3 GH |
321 | break; |
322 | case QXL_CURSOR_MOVE: | |
07536094 GH |
323 | qemu_mutex_lock(&qxl->ssd.lock); |
324 | qxl->ssd.mouse_x = cmd->u.position.x; | |
325 | qxl->ssd.mouse_y = cmd->u.position.y; | |
326 | qemu_mutex_unlock(&qxl->ssd.lock); | |
0b2824e5 | 327 | qemu_bh_schedule(qxl->ssd.cursor_bh); |
a19cbfb3 GH |
328 | break; |
329 | } | |
fae2afb1 | 330 | return 0; |
a19cbfb3 | 331 | } |