util/hbitmap: update orig_size on truncate
[qemu/ar7.git] / hw / display / qxl-render.c
blob14ad2b352d32e67ee2fd4ba4c493409d75a68cba
1 /*
2 * qxl local rendering (aka display on sdl/vnc)
4 * Copyright (C) 2010 Red Hat, Inc.
6 * maintained by Gerd Hoffmann <kraxel@redhat.com>
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.
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.
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/>.
22 #include "qemu/osdep.h"
23 #include "qxl.h"
24 #include "trace.h"
26 static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect)
28 DisplaySurface *surface = qemu_console_surface(qxl->vga.con);
29 uint8_t *dst = surface_data(surface);
30 uint8_t *src;
31 int len, i;
33 if (is_buffer_shared(surface)) {
34 return;
36 trace_qxl_render_blit(qxl->guest_primary.qxl_stride,
37 rect->left, rect->right, rect->top, rect->bottom);
38 src = qxl->guest_primary.data;
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;
47 dst += rect->top * qxl->guest_primary.abs_stride;
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;
52 for (i = rect->top; i < rect->bottom; i++) {
53 memcpy(dst, src, len);
54 dst += qxl->guest_primary.abs_stride;
55 src += qxl->guest_primary.qxl_stride;
59 void qxl_render_resize(PCIQXLDevice *qxl)
61 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
63 qxl->guest_primary.qxl_stride = sc->stride;
64 qxl->guest_primary.abs_stride = abs(sc->stride);
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", __func__,
82 qxl->guest_primary.surface.format);
83 qxl->guest_primary.bytes_pp = 4;
84 qxl->guest_primary.bits_pp = 32;
85 break;
89 static void qxl_set_rect_to_surface(PCIQXLDevice *qxl, QXLRect *area)
91 area->left = 0;
92 area->right = qxl->guest_primary.surface.width;
93 area->top = 0;
94 area->bottom = qxl->guest_primary.surface.height;
97 static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
99 VGACommonState *vga = &qxl->vga;
100 DisplaySurface *surface;
101 int width = qxl->guest_head0_width ?: qxl->guest_primary.surface.width;
102 int height = qxl->guest_head0_height ?: qxl->guest_primary.surface.height;
103 int i;
105 if (qxl->guest_primary.resized) {
106 qxl->guest_primary.resized = 0;
107 qxl->guest_primary.data = qxl_phys2virt(qxl,
108 qxl->guest_primary.surface.mem,
109 MEMSLOT_GROUP_GUEST);
110 if (!qxl->guest_primary.data) {
111 return;
113 qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
114 qxl->num_dirty_rects = 1;
115 trace_qxl_render_guest_primary_resized(
116 width,
117 height,
118 qxl->guest_primary.qxl_stride,
119 qxl->guest_primary.bytes_pp,
120 qxl->guest_primary.bits_pp);
121 if (qxl->guest_primary.qxl_stride > 0) {
122 pixman_format_code_t format =
123 qemu_default_pixman_format(qxl->guest_primary.bits_pp, true);
124 surface = qemu_create_displaysurface_from
125 (width,
126 height,
127 format,
128 qxl->guest_primary.abs_stride,
129 qxl->guest_primary.data);
130 } else {
131 surface = qemu_create_displaysurface
132 (width,
133 height);
135 dpy_gfx_replace_surface(vga->con, surface);
138 if (!qxl->guest_primary.data) {
139 return;
141 for (i = 0; i < qxl->num_dirty_rects; i++) {
142 if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
143 break;
145 if (qxl->dirty[i].left < 0 ||
146 qxl->dirty[i].top < 0 ||
147 qxl->dirty[i].left > qxl->dirty[i].right ||
148 qxl->dirty[i].top > qxl->dirty[i].bottom ||
149 qxl->dirty[i].right > width ||
150 qxl->dirty[i].bottom > height) {
151 continue;
153 qxl_blit(qxl, qxl->dirty+i);
154 dpy_gfx_update(vga->con,
155 qxl->dirty[i].left, qxl->dirty[i].top,
156 qxl->dirty[i].right - qxl->dirty[i].left,
157 qxl->dirty[i].bottom - qxl->dirty[i].top);
159 qxl->num_dirty_rects = 0;
163 * use ssd.lock to protect render_update_cookie_num.
164 * qxl_render_update is called by io thread or vcpu thread, and the completion
165 * callbacks are called by spice_server thread, deferring to bh called from the
166 * io thread.
168 void qxl_render_update(PCIQXLDevice *qxl)
170 QXLCookie *cookie;
172 qemu_mutex_lock(&qxl->ssd.lock);
174 if (!runstate_is_running() || !qxl->guest_primary.commands ||
175 qxl->mode == QXL_MODE_UNDEFINED) {
176 qxl_render_update_area_unlocked(qxl);
177 qemu_mutex_unlock(&qxl->ssd.lock);
178 return;
181 qxl->guest_primary.commands = 0;
182 qxl->render_update_cookie_num++;
183 qemu_mutex_unlock(&qxl->ssd.lock);
184 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
186 qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
187 qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
188 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
191 void qxl_render_update_area_bh(void *opaque)
193 PCIQXLDevice *qxl = opaque;
195 qemu_mutex_lock(&qxl->ssd.lock);
196 qxl_render_update_area_unlocked(qxl);
197 qemu_mutex_unlock(&qxl->ssd.lock);
200 void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
202 qemu_mutex_lock(&qxl->ssd.lock);
203 trace_qxl_render_update_area_done(cookie);
204 qemu_bh_schedule(qxl->update_area_bh);
205 qxl->render_update_cookie_num--;
206 qemu_mutex_unlock(&qxl->ssd.lock);
207 g_free(cookie);
210 static void qxl_unpack_chunks(void *dest, size_t size, PCIQXLDevice *qxl,
211 QXLDataChunk *chunk, uint32_t group_id)
213 uint32_t max_chunks = 32;
214 size_t offset = 0;
215 size_t bytes;
217 for (;;) {
218 bytes = MIN(size - offset, chunk->data_size);
219 memcpy(dest + offset, chunk->data, bytes);
220 offset += bytes;
221 if (offset == size) {
222 return;
224 chunk = qxl_phys2virt(qxl, chunk->next_chunk, group_id);
225 if (!chunk) {
226 return;
228 max_chunks--;
229 if (max_chunks == 0) {
230 return;
235 static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor,
236 uint32_t group_id)
238 QEMUCursor *c;
239 uint8_t *and_mask, *xor_mask;
240 size_t size;
242 c = cursor_alloc(cursor->header.width, cursor->header.height);
243 c->hot_x = cursor->header.hot_spot_x;
244 c->hot_y = cursor->header.hot_spot_y;
245 switch (cursor->header.type) {
246 case SPICE_CURSOR_TYPE_MONO:
247 /* Assume that the full cursor is available in a single chunk. */
248 size = 2 * cursor_get_mono_bpl(c) * c->height;
249 if (size != cursor->data_size) {
250 fprintf(stderr, "%s: bad monochrome cursor %ux%u with size %u\n",
251 __func__, c->width, c->height, cursor->data_size);
252 goto fail;
254 and_mask = cursor->chunk.data;
255 xor_mask = and_mask + cursor_get_mono_bpl(c) * c->height;
256 cursor_set_mono(c, 0xffffff, 0x000000, xor_mask, 1, and_mask);
257 if (qxl->debug > 2) {
258 cursor_print_ascii_art(c, "qxl/mono");
260 break;
261 case SPICE_CURSOR_TYPE_ALPHA:
262 size = sizeof(uint32_t) * cursor->header.width * cursor->header.height;
263 qxl_unpack_chunks(c->data, size, qxl, &cursor->chunk, group_id);
264 if (qxl->debug > 2) {
265 cursor_print_ascii_art(c, "qxl/alpha");
267 break;
268 default:
269 fprintf(stderr, "%s: not implemented: type %d\n",
270 __func__, cursor->header.type);
271 goto fail;
273 return c;
275 fail:
276 cursor_put(c);
277 return NULL;
281 /* called from spice server thread context only */
282 int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
284 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
285 QXLCursor *cursor;
286 QEMUCursor *c;
288 if (!cmd) {
289 return 1;
292 if (!dpy_cursor_define_supported(qxl->vga.con)) {
293 return 0;
296 if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
297 fprintf(stderr, "%s", __func__);
298 qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
299 fprintf(stderr, "\n");
301 switch (cmd->type) {
302 case QXL_CURSOR_SET:
303 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
304 if (!cursor) {
305 return 1;
307 c = qxl_cursor(qxl, cursor, ext->group_id);
308 if (c == NULL) {
309 c = cursor_builtin_left_ptr();
311 qemu_mutex_lock(&qxl->ssd.lock);
312 if (qxl->ssd.cursor) {
313 cursor_put(qxl->ssd.cursor);
315 qxl->ssd.cursor = c;
316 qxl->ssd.mouse_x = cmd->u.set.position.x;
317 qxl->ssd.mouse_y = cmd->u.set.position.y;
318 qemu_mutex_unlock(&qxl->ssd.lock);
319 qemu_bh_schedule(qxl->ssd.cursor_bh);
320 break;
321 case QXL_CURSOR_MOVE:
322 qemu_mutex_lock(&qxl->ssd.lock);
323 qxl->ssd.mouse_x = cmd->u.position.x;
324 qxl->ssd.mouse_y = cmd->u.position.y;
325 qemu_mutex_unlock(&qxl->ssd.lock);
326 qemu_bh_schedule(qxl->ssd.cursor_bh);
327 break;
329 return 0;