]> Git Repo - qemu.git/blob - hw/qxl-render.c
qxl: introduce QXLCookie
[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 void qxl_render_update(PCIQXLDevice *qxl)
86 {
87     VGACommonState *vga = &qxl->vga;
88     QXLRect dirty[32], update;
89     int i, redraw = 0;
90     DisplaySurface *surface = vga->ds->surface;
91
92     if (qxl->guest_primary.resized) {
93         qxl->guest_primary.resized = 0;
94
95         qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
96         dprint(qxl, 1, "%s: %dx%d, stride %d, bpp %d, depth %d\n",
97                __FUNCTION__,
98                qxl->guest_primary.surface.width,
99                qxl->guest_primary.surface.height,
100                qxl->guest_primary.qxl_stride,
101                qxl->guest_primary.bytes_pp,
102                qxl->guest_primary.bits_pp);
103     }
104     if (surface->width != qxl->guest_primary.surface.width ||
105         surface->height != qxl->guest_primary.surface.height) {
106         dprint(qxl, 1, "%s: resizing displaysurface to guest_primary\n",
107                __func__);
108         if (qxl->guest_primary.qxl_stride > 0) {
109             qemu_free_displaysurface(vga->ds);
110             qemu_create_displaysurface_from(qxl->guest_primary.surface.width,
111                                             qxl->guest_primary.surface.height,
112                                             qxl->guest_primary.bits_pp,
113                                             qxl->guest_primary.abs_stride,
114                                             qxl->guest_primary.data);
115         } else {
116             qemu_resize_displaysurface(vga->ds,
117                     qxl->guest_primary.surface.width,
118                     qxl->guest_primary.surface.height);
119         }
120     }
121     update.left   = 0;
122     update.right  = qxl->guest_primary.surface.width;
123     update.top    = 0;
124     update.bottom = qxl->guest_primary.surface.height;
125
126     memset(dirty, 0, sizeof(dirty));
127     if (runstate_is_running() && qxl->guest_primary.commands) {
128         qxl->guest_primary.commands = 0;
129         qxl_spice_update_area(qxl, 0, &update,
130                               dirty, ARRAY_SIZE(dirty), 1, QXL_SYNC, NULL);
131     }
132     if (redraw) {
133         memset(dirty, 0, sizeof(dirty));
134         dirty[0] = update;
135     }
136     for (i = 0; i < ARRAY_SIZE(dirty); i++) {
137         if (qemu_spice_rect_is_empty(dirty+i)) {
138             break;
139         }
140         qxl_flip(qxl, dirty+i);
141         dpy_update(vga->ds,
142                    dirty[i].left, dirty[i].top,
143                    dirty[i].right - dirty[i].left,
144                    dirty[i].bottom - dirty[i].top);
145     }
146 }
147
148 static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
149 {
150     QEMUCursor *c;
151     uint8_t *image, *mask;
152     size_t size;
153
154     c = cursor_alloc(cursor->header.width, cursor->header.height);
155     c->hot_x = cursor->header.hot_spot_x;
156     c->hot_y = cursor->header.hot_spot_y;
157     switch (cursor->header.type) {
158     case SPICE_CURSOR_TYPE_ALPHA:
159         size = cursor->header.width * cursor->header.height * sizeof(uint32_t);
160         memcpy(c->data, cursor->chunk.data, size);
161         if (qxl->debug > 2) {
162             cursor_print_ascii_art(c, "qxl/alpha");
163         }
164         break;
165     case SPICE_CURSOR_TYPE_MONO:
166         mask  = cursor->chunk.data;
167         image = mask + cursor_get_mono_bpl(c) * c->width;
168         cursor_set_mono(c, 0xffffff, 0x000000, image, 1, mask);
169         if (qxl->debug > 2) {
170             cursor_print_ascii_art(c, "qxl/mono");
171         }
172         break;
173     default:
174         fprintf(stderr, "%s: not implemented: type %d\n",
175                 __FUNCTION__, cursor->header.type);
176         goto fail;
177     }
178     return c;
179
180 fail:
181     cursor_put(c);
182     return NULL;
183 }
184
185
186 /* called from spice server thread context only */
187 void qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
188 {
189     QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
190     QXLCursor *cursor;
191     QEMUCursor *c;
192
193     if (!qxl->ssd.ds->mouse_set || !qxl->ssd.ds->cursor_define) {
194         return;
195     }
196
197     if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
198         fprintf(stderr, "%s", __FUNCTION__);
199         qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
200         fprintf(stderr, "\n");
201     }
202     switch (cmd->type) {
203     case QXL_CURSOR_SET:
204         cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
205         if (cursor->chunk.data_size != cursor->data_size) {
206             fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__);
207             return;
208         }
209         c = qxl_cursor(qxl, cursor);
210         if (c == NULL) {
211             c = cursor_builtin_left_ptr();
212         }
213         qemu_mutex_lock(&qxl->ssd.lock);
214         if (qxl->ssd.cursor) {
215             cursor_put(qxl->ssd.cursor);
216         }
217         qxl->ssd.cursor = c;
218         qxl->ssd.mouse_x = cmd->u.set.position.x;
219         qxl->ssd.mouse_y = cmd->u.set.position.y;
220         qemu_mutex_unlock(&qxl->ssd.lock);
221         break;
222     case QXL_CURSOR_MOVE:
223         qemu_mutex_lock(&qxl->ssd.lock);
224         qxl->ssd.mouse_x = cmd->u.position.x;
225         qxl->ssd.mouse_y = cmd->u.position.y;
226         qemu_mutex_unlock(&qxl->ssd.lock);
227         break;
228     }
229 }
This page took 0.040809 seconds and 4 git commands to generate.