tap: factor out common tap initialization
[qemu/ar7.git] / hw / qxl-render.c
blob88e63f8085469fd220c54cb1be3b9a4b26bbd4a6
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 "qxl.h"
24 static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect)
26 uint8_t *src;
27 uint8_t *dst = ds_get_data(qxl->vga.ds);
28 int len, i;
30 if (is_buffer_shared(qxl->vga.ds->surface)) {
31 return;
33 if (!qxl->guest_primary.data) {
34 trace_qxl_render_blit_guest_primary_initialized();
35 qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
37 trace_qxl_render_blit(qxl->guest_primary.qxl_stride,
38 rect->left, rect->right, rect->top, rect->bottom);
39 src = qxl->guest_primary.data;
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;
48 dst += rect->top * qxl->guest_primary.abs_stride;
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;
53 for (i = rect->top; i < rect->bottom; i++) {
54 memcpy(dst, src, len);
55 dst += qxl->guest_primary.abs_stride;
56 src += qxl->guest_primary.qxl_stride;
60 void qxl_render_resize(PCIQXLDevice *qxl)
62 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
64 qxl->guest_primary.qxl_stride = sc->stride;
65 qxl->guest_primary.abs_stride = abs(sc->stride);
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:
82 fprintf(stderr, "%s: unhandled format: %x\n", __FUNCTION__,
83 qxl->guest_primary.surface.format);
84 qxl->guest_primary.bytes_pp = 4;
85 qxl->guest_primary.bits_pp = 32;
86 break;
90 static void qxl_set_rect_to_surface(PCIQXLDevice *qxl, QXLRect *area)
92 area->left = 0;
93 area->right = qxl->guest_primary.surface.width;
94 area->top = 0;
95 area->bottom = qxl->guest_primary.surface.height;
98 static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
100 VGACommonState *vga = &qxl->vga;
101 int i;
103 if (qxl->guest_primary.resized) {
104 qxl->guest_primary.resized = 0;
105 qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
106 qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
107 qxl->num_dirty_rects = 1;
108 trace_qxl_render_guest_primary_resized(
109 qxl->guest_primary.surface.width,
110 qxl->guest_primary.surface.height,
111 qxl->guest_primary.qxl_stride,
112 qxl->guest_primary.bytes_pp,
113 qxl->guest_primary.bits_pp);
114 if (qxl->guest_primary.qxl_stride > 0) {
115 qemu_free_displaysurface(vga->ds);
116 vga->ds->surface = qemu_create_displaysurface_from
117 (qxl->guest_primary.surface.width,
118 qxl->guest_primary.surface.height,
119 qxl->guest_primary.bits_pp,
120 qxl->guest_primary.abs_stride,
121 qxl->guest_primary.data);
122 } else {
123 qemu_resize_displaysurface(vga->ds,
124 qxl->guest_primary.surface.width,
125 qxl->guest_primary.surface.height);
127 dpy_gfx_resize(vga->ds);
129 for (i = 0; i < qxl->num_dirty_rects; i++) {
130 if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
131 break;
133 qxl_blit(qxl, qxl->dirty+i);
134 dpy_gfx_update(vga->ds,
135 qxl->dirty[i].left, qxl->dirty[i].top,
136 qxl->dirty[i].right - qxl->dirty[i].left,
137 qxl->dirty[i].bottom - qxl->dirty[i].top);
139 qxl->num_dirty_rects = 0;
143 * use ssd.lock to protect render_update_cookie_num.
144 * qxl_render_update is called by io thread or vcpu thread, and the completion
145 * callbacks are called by spice_server thread, defering to bh called from the
146 * io thread.
148 void qxl_render_update(PCIQXLDevice *qxl)
150 QXLCookie *cookie;
152 qemu_mutex_lock(&qxl->ssd.lock);
154 if (!runstate_is_running() || !qxl->guest_primary.commands) {
155 qxl_render_update_area_unlocked(qxl);
156 qemu_mutex_unlock(&qxl->ssd.lock);
157 return;
160 qxl->guest_primary.commands = 0;
161 qxl->render_update_cookie_num++;
162 qemu_mutex_unlock(&qxl->ssd.lock);
163 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
165 qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
166 qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
167 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
170 void qxl_render_update_area_bh(void *opaque)
172 PCIQXLDevice *qxl = opaque;
174 qemu_mutex_lock(&qxl->ssd.lock);
175 qxl_render_update_area_unlocked(qxl);
176 qemu_mutex_unlock(&qxl->ssd.lock);
179 void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
181 qemu_mutex_lock(&qxl->ssd.lock);
182 trace_qxl_render_update_area_done(cookie);
183 qemu_bh_schedule(qxl->update_area_bh);
184 qxl->render_update_cookie_num--;
185 qemu_mutex_unlock(&qxl->ssd.lock);
186 g_free(cookie);
189 static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
191 QEMUCursor *c;
192 uint8_t *image, *mask;
193 size_t size;
195 c = cursor_alloc(cursor->header.width, cursor->header.height);
196 c->hot_x = cursor->header.hot_spot_x;
197 c->hot_y = cursor->header.hot_spot_y;
198 switch (cursor->header.type) {
199 case SPICE_CURSOR_TYPE_ALPHA:
200 size = cursor->header.width * cursor->header.height * sizeof(uint32_t);
201 memcpy(c->data, cursor->chunk.data, size);
202 if (qxl->debug > 2) {
203 cursor_print_ascii_art(c, "qxl/alpha");
205 break;
206 case SPICE_CURSOR_TYPE_MONO:
207 mask = cursor->chunk.data;
208 image = mask + cursor_get_mono_bpl(c) * c->width;
209 cursor_set_mono(c, 0xffffff, 0x000000, image, 1, mask);
210 if (qxl->debug > 2) {
211 cursor_print_ascii_art(c, "qxl/mono");
213 break;
214 default:
215 fprintf(stderr, "%s: not implemented: type %d\n",
216 __FUNCTION__, cursor->header.type);
217 goto fail;
219 return c;
221 fail:
222 cursor_put(c);
223 return NULL;
227 /* called from spice server thread context only */
228 int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
230 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
231 QXLCursor *cursor;
232 QEMUCursor *c;
234 if (!cmd) {
235 return 1;
238 if (!dpy_cursor_define_supported(qxl->ssd.ds)) {
239 return 0;
242 if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
243 fprintf(stderr, "%s", __FUNCTION__);
244 qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
245 fprintf(stderr, "\n");
247 switch (cmd->type) {
248 case QXL_CURSOR_SET:
249 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
250 if (!cursor) {
251 return 1;
253 if (cursor->chunk.data_size != cursor->data_size) {
254 fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__);
255 return 1;
257 c = qxl_cursor(qxl, cursor);
258 if (c == NULL) {
259 c = cursor_builtin_left_ptr();
261 qemu_mutex_lock(&qxl->ssd.lock);
262 if (qxl->ssd.cursor) {
263 cursor_put(qxl->ssd.cursor);
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);
269 break;
270 case QXL_CURSOR_MOVE:
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);
275 break;
277 return 0;