]> Git Repo - qemu.git/blob - hw/qxl-render.c
qxl: make qxl_render_update async
[qemu.git] / hw / qxl-render.c
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
22 #include "qxl.h"
23
24 static void qxl_flip(PCIQXLDevice *qxl, QXLRect *rect)
25 {
26     uint8_t *src;
27     uint8_t *dst = qxl->vga.ds->surface->data;
28     int len, i;
29
30     if (qxl->guest_primary.qxl_stride > 0) {
31         return;
32     }
33     if (!qxl->guest_primary.data) {
34         dprint(qxl, 1, "%s: initializing guest_primary.data\n", __func__);
35         qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
36     }
37     dprint(qxl, 1, "%s: stride %d, [%d, %d, %d, %d]\n", __func__,
38             qxl->guest_primary.qxl_stride,
39             rect->left, rect->right, rect->top, rect->bottom);
40     src = qxl->guest_primary.data;
41     src += (qxl->guest_primary.surface.height - rect->top - 1) *
42         qxl->guest_primary.abs_stride;
43     dst += rect->top  * qxl->guest_primary.abs_stride;
44     src += rect->left * qxl->guest_primary.bytes_pp;
45     dst += rect->left * qxl->guest_primary.bytes_pp;
46     len  = (rect->right - rect->left) * qxl->guest_primary.bytes_pp;
47
48     for (i = rect->top; i < rect->bottom; i++) {
49         memcpy(dst, src, len);
50         dst += qxl->guest_primary.abs_stride;
51         src -= qxl->guest_primary.abs_stride;
52     }
53 }
54
55 void qxl_render_resize(PCIQXLDevice *qxl)
56 {
57     QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
58
59     qxl->guest_primary.qxl_stride = sc->stride;
60     qxl->guest_primary.abs_stride = abs(sc->stride);
61     qxl->guest_primary.resized++;
62     switch (sc->format) {
63     case SPICE_SURFACE_FMT_16_555:
64         qxl->guest_primary.bytes_pp = 2;
65         qxl->guest_primary.bits_pp = 15;
66         break;
67     case SPICE_SURFACE_FMT_16_565:
68         qxl->guest_primary.bytes_pp = 2;
69         qxl->guest_primary.bits_pp = 16;
70         break;
71     case SPICE_SURFACE_FMT_32_xRGB:
72     case SPICE_SURFACE_FMT_32_ARGB:
73         qxl->guest_primary.bytes_pp = 4;
74         qxl->guest_primary.bits_pp = 32;
75         break;
76     default:
77         fprintf(stderr, "%s: unhandled format: %x\n", __FUNCTION__,
78                 qxl->guest_primary.surface.format);
79         qxl->guest_primary.bytes_pp = 4;
80         qxl->guest_primary.bits_pp = 32;
81         break;
82     }
83 }
84
85 static void qxl_set_rect_to_surface(PCIQXLDevice *qxl, QXLRect *area)
86 {
87     area->left   = 0;
88     area->right  = qxl->guest_primary.surface.width;
89     area->top    = 0;
90     area->bottom = qxl->guest_primary.surface.height;
91 }
92
93 static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
94 {
95     VGACommonState *vga = &qxl->vga;
96     int i;
97     DisplaySurface *surface = vga->ds->surface;
98
99     if (qxl->guest_primary.resized) {
100         qxl->guest_primary.resized = 0;
101         qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
102         qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
103         qxl->num_dirty_rects = 1;
104         dprint(qxl, 1, "%s: %dx%d, stride %d, bpp %d, depth %d\n",
105                __FUNCTION__,
106                qxl->guest_primary.surface.width,
107                qxl->guest_primary.surface.height,
108                qxl->guest_primary.qxl_stride,
109                qxl->guest_primary.bytes_pp,
110                qxl->guest_primary.bits_pp);
111     }
112     if (surface->width != qxl->guest_primary.surface.width ||
113         surface->height != qxl->guest_primary.surface.height) {
114         if (qxl->guest_primary.qxl_stride > 0) {
115             dprint(qxl, 1, "%s: using guest_primary for displaysurface\n",
116                    __func__);
117             qemu_free_displaysurface(vga->ds);
118             qemu_create_displaysurface_from(qxl->guest_primary.surface.width,
119                                             qxl->guest_primary.surface.height,
120                                             qxl->guest_primary.bits_pp,
121                                             qxl->guest_primary.abs_stride,
122                                             qxl->guest_primary.data);
123         } else {
124             dprint(qxl, 1, "%s: resizing displaysurface to guest_primary\n",
125                    __func__);
126             qemu_resize_displaysurface(vga->ds,
127                     qxl->guest_primary.surface.width,
128                     qxl->guest_primary.surface.height);
129         }
130     }
131     for (i = 0; i < qxl->num_dirty_rects; i++) {
132         if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
133             break;
134         }
135         qxl_flip(qxl, qxl->dirty+i);
136         dpy_update(vga->ds,
137                    qxl->dirty[i].left, qxl->dirty[i].top,
138                    qxl->dirty[i].right - qxl->dirty[i].left,
139                    qxl->dirty[i].bottom - qxl->dirty[i].top);
140     }
141     qxl->num_dirty_rects = 0;
142 }
143
144 /*
145  * use ssd.lock to protect render_update_cookie_num.
146  * qxl_render_update is called by io thread or vcpu thread, and the completion
147  * callbacks are called by spice_server thread, defering to bh called from the
148  * io thread.
149  */
150 void qxl_render_update(PCIQXLDevice *qxl)
151 {
152     QXLCookie *cookie;
153
154     qemu_mutex_lock(&qxl->ssd.lock);
155
156     if (!runstate_is_running() || !qxl->guest_primary.commands) {
157         qxl_render_update_area_unlocked(qxl);
158         qemu_mutex_unlock(&qxl->ssd.lock);
159         return;
160     }
161
162     qxl->guest_primary.commands = 0;
163     qxl->render_update_cookie_num++;
164     qemu_mutex_unlock(&qxl->ssd.lock);
165     cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
166                             0);
167     qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
168     qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
169                           0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
170 }
171
172 void qxl_render_update_area_bh(void *opaque)
173 {
174     PCIQXLDevice *qxl = opaque;
175
176     qemu_mutex_lock(&qxl->ssd.lock);
177     qxl_render_update_area_unlocked(qxl);
178     qemu_mutex_unlock(&qxl->ssd.lock);
179 }
180
181 void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
182 {
183     qemu_mutex_lock(&qxl->ssd.lock);
184     qemu_bh_schedule(qxl->update_area_bh);
185     qxl->render_update_cookie_num--;
186     qemu_mutex_unlock(&qxl->ssd.lock);
187     g_free(cookie);
188 }
189
190 static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
191 {
192     QEMUCursor *c;
193     uint8_t *image, *mask;
194     size_t size;
195
196     c = cursor_alloc(cursor->header.width, cursor->header.height);
197     c->hot_x = cursor->header.hot_spot_x;
198     c->hot_y = cursor->header.hot_spot_y;
199     switch (cursor->header.type) {
200     case SPICE_CURSOR_TYPE_ALPHA:
201         size = cursor->header.width * cursor->header.height * sizeof(uint32_t);
202         memcpy(c->data, cursor->chunk.data, size);
203         if (qxl->debug > 2) {
204             cursor_print_ascii_art(c, "qxl/alpha");
205         }
206         break;
207     case SPICE_CURSOR_TYPE_MONO:
208         mask  = cursor->chunk.data;
209         image = mask + cursor_get_mono_bpl(c) * c->width;
210         cursor_set_mono(c, 0xffffff, 0x000000, image, 1, mask);
211         if (qxl->debug > 2) {
212             cursor_print_ascii_art(c, "qxl/mono");
213         }
214         break;
215     default:
216         fprintf(stderr, "%s: not implemented: type %d\n",
217                 __FUNCTION__, cursor->header.type);
218         goto fail;
219     }
220     return c;
221
222 fail:
223     cursor_put(c);
224     return NULL;
225 }
226
227
228 /* called from spice server thread context only */
229 void qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
230 {
231     QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
232     QXLCursor *cursor;
233     QEMUCursor *c;
234
235     if (!qxl->ssd.ds->mouse_set || !qxl->ssd.ds->cursor_define) {
236         return;
237     }
238
239     if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
240         fprintf(stderr, "%s", __FUNCTION__);
241         qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
242         fprintf(stderr, "\n");
243     }
244     switch (cmd->type) {
245     case QXL_CURSOR_SET:
246         cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
247         if (cursor->chunk.data_size != cursor->data_size) {
248             fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__);
249             return;
250         }
251         c = qxl_cursor(qxl, cursor);
252         if (c == NULL) {
253             c = cursor_builtin_left_ptr();
254         }
255         qemu_mutex_lock(&qxl->ssd.lock);
256         if (qxl->ssd.cursor) {
257             cursor_put(qxl->ssd.cursor);
258         }
259         qxl->ssd.cursor = c;
260         qxl->ssd.mouse_x = cmd->u.set.position.x;
261         qxl->ssd.mouse_y = cmd->u.set.position.y;
262         qemu_mutex_unlock(&qxl->ssd.lock);
263         break;
264     case QXL_CURSOR_MOVE:
265         qemu_mutex_lock(&qxl->ssd.lock);
266         qxl->ssd.mouse_x = cmd->u.position.x;
267         qxl->ssd.mouse_y = cmd->u.position.y;
268         qemu_mutex_unlock(&qxl->ssd.lock);
269         break;
270     }
271 }
This page took 0.037877 seconds and 4 git commands to generate.