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