]> Git Repo - qemu.git/blame - hw/display/qxl-render.c
Merge remote-tracking branch 'remotes/kraxel/tags/pull-chardev-1' into staging
[qemu.git] / hw / display / 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 <[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
47b43a1f 22#include "qxl.h"
b1829cde 23#include "trace.h"
a19cbfb3 24
e2efc0a3 25static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect)
a19cbfb3 26{
c78f7137
GH
27 DisplaySurface *surface = qemu_console_surface(qxl->vga.con);
28 uint8_t *dst = surface_data(surface);
4c19ebb5 29 uint8_t *src;
a19cbfb3
GH
30 int len, i;
31
c78f7137 32 if (is_buffer_shared(surface)) {
4c19ebb5
AL
33 return;
34 }
d53291cf 35 trace_qxl_render_blit(qxl->guest_primary.qxl_stride,
4c19ebb5
AL
36 rect->left, rect->right, rect->top, rect->bottom);
37 src = qxl->guest_primary.data;
e2efc0a3
GH
38 if (qxl->guest_primary.qxl_stride < 0) {
39 /* qxl surface is upside down, walk src scanlines
40 * in reverse order to flip it */
41 src += (qxl->guest_primary.surface.height - rect->top - 1) *
42 qxl->guest_primary.abs_stride;
43 } else {
44 src += rect->top * qxl->guest_primary.abs_stride;
45 }
0e2487bd 46 dst += rect->top * qxl->guest_primary.abs_stride;
a19cbfb3
GH
47 src += rect->left * qxl->guest_primary.bytes_pp;
48 dst += rect->left * qxl->guest_primary.bytes_pp;
49 len = (rect->right - rect->left) * qxl->guest_primary.bytes_pp;
50
51 for (i = rect->top; i < rect->bottom; i++) {
52 memcpy(dst, src, len);
0e2487bd 53 dst += qxl->guest_primary.abs_stride;
e2efc0a3 54 src += qxl->guest_primary.qxl_stride;
a19cbfb3
GH
55 }
56}
57
58void qxl_render_resize(PCIQXLDevice *qxl)
59{
60 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
61
0e2487bd
GH
62 qxl->guest_primary.qxl_stride = sc->stride;
63 qxl->guest_primary.abs_stride = abs(sc->stride);
a19cbfb3
GH
64 qxl->guest_primary.resized++;
65 switch (sc->format) {
66 case SPICE_SURFACE_FMT_16_555:
67 qxl->guest_primary.bytes_pp = 2;
68 qxl->guest_primary.bits_pp = 15;
69 break;
70 case SPICE_SURFACE_FMT_16_565:
71 qxl->guest_primary.bytes_pp = 2;
72 qxl->guest_primary.bits_pp = 16;
73 break;
74 case SPICE_SURFACE_FMT_32_xRGB:
75 case SPICE_SURFACE_FMT_32_ARGB:
76 qxl->guest_primary.bytes_pp = 4;
77 qxl->guest_primary.bits_pp = 32;
78 break;
79 default:
80 fprintf(stderr, "%s: unhandled format: %x\n", __FUNCTION__,
81 qxl->guest_primary.surface.format);
82 qxl->guest_primary.bytes_pp = 4;
83 qxl->guest_primary.bits_pp = 32;
84 break;
85 }
86}
87
81fb6f15
AL
88static void qxl_set_rect_to_surface(PCIQXLDevice *qxl, QXLRect *area)
89{
90 area->left = 0;
91 area->right = qxl->guest_primary.surface.width;
92 area->top = 0;
93 area->bottom = qxl->guest_primary.surface.height;
94}
95
96static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
a19cbfb3
GH
97{
98 VGACommonState *vga = &qxl->vga;
da229ef3 99 DisplaySurface *surface;
81fb6f15 100 int i;
a19cbfb3
GH
101
102 if (qxl->guest_primary.resized) {
103 qxl->guest_primary.resized = 0;
c58c7b95
GH
104 qxl->guest_primary.data = qxl_phys2virt(qxl,
105 qxl->guest_primary.surface.mem,
106 MEMSLOT_GROUP_GUEST);
107 if (!qxl->guest_primary.data) {
108 return;
109 }
81fb6f15
AL
110 qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
111 qxl->num_dirty_rects = 1;
d53291cf 112 trace_qxl_render_guest_primary_resized(
a19cbfb3
GH
113 qxl->guest_primary.surface.width,
114 qxl->guest_primary.surface.height,
0e2487bd 115 qxl->guest_primary.qxl_stride,
a19cbfb3 116 qxl->guest_primary.bytes_pp,
4c19ebb5 117 qxl->guest_primary.bits_pp);
4c19ebb5 118 if (qxl->guest_primary.qxl_stride > 0) {
da229ef3 119 surface = qemu_create_displaysurface_from
2f464b5a
GH
120 (qxl->guest_primary.surface.width,
121 qxl->guest_primary.surface.height,
122 qxl->guest_primary.bits_pp,
123 qxl->guest_primary.abs_stride,
b1424e03
GH
124 qxl->guest_primary.data,
125 false);
4c19ebb5 126 } else {
da229ef3
GH
127 surface = qemu_create_displaysurface
128 (qxl->guest_primary.surface.width,
129 qxl->guest_primary.surface.height);
4c19ebb5 130 }
c78f7137 131 dpy_gfx_replace_surface(vga->con, surface);
a19cbfb3 132 }
c58c7b95
GH
133
134 if (!qxl->guest_primary.data) {
135 return;
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);
c78f7137 142 dpy_gfx_update(vga->con,
a93a4a22
GH
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 */
156void 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
178void 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
187void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
188{
189 qemu_mutex_lock(&qxl->ssd.lock);
d53291cf 190 trace_qxl_render_update_area_done(cookie);
81fb6f15
AL
191 qemu_bh_schedule(qxl->update_area_bh);
192 qxl->render_update_cookie_num--;
193 qemu_mutex_unlock(&qxl->ssd.lock);
194 g_free(cookie);
a19cbfb3
GH
195}
196
197static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
198{
199 QEMUCursor *c;
200 uint8_t *image, *mask;
66d3f196 201 size_t size;
a19cbfb3
GH
202
203 c = cursor_alloc(cursor->header.width, cursor->header.height);
204 c->hot_x = cursor->header.hot_spot_x;
205 c->hot_y = cursor->header.hot_spot_y;
206 switch (cursor->header.type) {
207 case SPICE_CURSOR_TYPE_ALPHA:
bfc10122 208 size = sizeof(uint32_t) * cursor->header.width * cursor->header.height;
a19cbfb3
GH
209 memcpy(c->data, cursor->chunk.data, size);
210 if (qxl->debug > 2) {
211 cursor_print_ascii_art(c, "qxl/alpha");
212 }
213 break;
214 case SPICE_CURSOR_TYPE_MONO:
215 mask = cursor->chunk.data;
216 image = mask + cursor_get_mono_bpl(c) * c->width;
217 cursor_set_mono(c, 0xffffff, 0x000000, image, 1, mask);
218 if (qxl->debug > 2) {
219 cursor_print_ascii_art(c, "qxl/mono");
220 }
221 break;
222 default:
223 fprintf(stderr, "%s: not implemented: type %d\n",
224 __FUNCTION__, cursor->header.type);
225 goto fail;
226 }
227 return c;
228
229fail:
230 cursor_put(c);
231 return NULL;
232}
233
234
235/* called from spice server thread context only */
fae2afb1 236int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
a19cbfb3
GH
237{
238 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
239 QXLCursor *cursor;
240 QEMUCursor *c;
a19cbfb3 241
fae2afb1
AL
242 if (!cmd) {
243 return 1;
244 }
245
c78f7137 246 if (!dpy_cursor_define_supported(qxl->vga.con)) {
fae2afb1 247 return 0;
a19cbfb3
GH
248 }
249
250 if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
251 fprintf(stderr, "%s", __FUNCTION__);
252 qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
253 fprintf(stderr, "\n");
254 }
255 switch (cmd->type) {
256 case QXL_CURSOR_SET:
a19cbfb3 257 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
fae2afb1
AL
258 if (!cursor) {
259 return 1;
260 }
a19cbfb3
GH
261 if (cursor->chunk.data_size != cursor->data_size) {
262 fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__);
fae2afb1 263 return 1;
a19cbfb3
GH
264 }
265 c = qxl_cursor(qxl, cursor);
266 if (c == NULL) {
267 c = cursor_builtin_left_ptr();
268 }
07536094
GH
269 qemu_mutex_lock(&qxl->ssd.lock);
270 if (qxl->ssd.cursor) {
271 cursor_put(qxl->ssd.cursor);
272 }
273 qxl->ssd.cursor = c;
274 qxl->ssd.mouse_x = cmd->u.set.position.x;
275 qxl->ssd.mouse_y = cmd->u.set.position.y;
276 qemu_mutex_unlock(&qxl->ssd.lock);
a19cbfb3
GH
277 break;
278 case QXL_CURSOR_MOVE:
07536094
GH
279 qemu_mutex_lock(&qxl->ssd.lock);
280 qxl->ssd.mouse_x = cmd->u.position.x;
281 qxl->ssd.mouse_y = cmd->u.position.y;
282 qemu_mutex_unlock(&qxl->ssd.lock);
a19cbfb3
GH
283 break;
284 }
fae2afb1 285 return 0;
a19cbfb3 286}
This page took 0.436171 seconds and 4 git commands to generate.