]> git.proxmox.com Git - qemu.git/blame - hw/qxl-render.c
qxl: stride fixup
[qemu.git] / hw / 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 <kraxel@redhat.com>
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
24static void qxl_flip(PCIQXLDevice *qxl, QXLRect *rect)
25{
26 uint8_t *src = qxl->guest_primary.data;
27 uint8_t *dst = qxl->guest_primary.flipped;
28 int len, i;
29
30 src += (qxl->guest_primary.surface.height - rect->top - 1) *
0e2487bd
GH
31 qxl->guest_primary.abs_stride;
32 dst += rect->top * qxl->guest_primary.abs_stride;
a19cbfb3
GH
33 src += rect->left * qxl->guest_primary.bytes_pp;
34 dst += rect->left * qxl->guest_primary.bytes_pp;
35 len = (rect->right - rect->left) * qxl->guest_primary.bytes_pp;
36
37 for (i = rect->top; i < rect->bottom; i++) {
38 memcpy(dst, src, len);
0e2487bd
GH
39 dst += qxl->guest_primary.abs_stride;
40 src -= qxl->guest_primary.abs_stride;
a19cbfb3
GH
41 }
42}
43
44void qxl_render_resize(PCIQXLDevice *qxl)
45{
46 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
47
0e2487bd
GH
48 qxl->guest_primary.qxl_stride = sc->stride;
49 qxl->guest_primary.abs_stride = abs(sc->stride);
a19cbfb3
GH
50 qxl->guest_primary.resized++;
51 switch (sc->format) {
52 case SPICE_SURFACE_FMT_16_555:
53 qxl->guest_primary.bytes_pp = 2;
54 qxl->guest_primary.bits_pp = 15;
55 break;
56 case SPICE_SURFACE_FMT_16_565:
57 qxl->guest_primary.bytes_pp = 2;
58 qxl->guest_primary.bits_pp = 16;
59 break;
60 case SPICE_SURFACE_FMT_32_xRGB:
61 case SPICE_SURFACE_FMT_32_ARGB:
62 qxl->guest_primary.bytes_pp = 4;
63 qxl->guest_primary.bits_pp = 32;
64 break;
65 default:
66 fprintf(stderr, "%s: unhandled format: %x\n", __FUNCTION__,
67 qxl->guest_primary.surface.format);
68 qxl->guest_primary.bytes_pp = 4;
69 qxl->guest_primary.bits_pp = 32;
70 break;
71 }
72}
73
74void qxl_render_update(PCIQXLDevice *qxl)
75{
76 VGACommonState *vga = &qxl->vga;
77 QXLRect dirty[32], update;
78 void *ptr;
79 int i;
80
81 if (qxl->guest_primary.resized) {
82 qxl->guest_primary.resized = 0;
83
84 if (qxl->guest_primary.flipped) {
7267c094 85 g_free(qxl->guest_primary.flipped);
a19cbfb3
GH
86 qxl->guest_primary.flipped = NULL;
87 }
88 qemu_free_displaysurface(vga->ds);
89
b1950430 90 qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
0e2487bd 91 if (qxl->guest_primary.qxl_stride < 0) {
a19cbfb3 92 /* spice surface is upside down -> need extra buffer to flip */
0e2487bd
GH
93 qxl->guest_primary.flipped =
94 g_malloc(qxl->guest_primary.surface.width *
95 qxl->guest_primary.abs_stride);
a19cbfb3
GH
96 ptr = qxl->guest_primary.flipped;
97 } else {
98 ptr = qxl->guest_primary.data;
99 }
100 dprint(qxl, 1, "%s: %dx%d, stride %d, bpp %d, depth %d, flip %s\n",
101 __FUNCTION__,
102 qxl->guest_primary.surface.width,
103 qxl->guest_primary.surface.height,
0e2487bd 104 qxl->guest_primary.qxl_stride,
a19cbfb3
GH
105 qxl->guest_primary.bytes_pp,
106 qxl->guest_primary.bits_pp,
107 qxl->guest_primary.flipped ? "yes" : "no");
108 vga->ds->surface =
109 qemu_create_displaysurface_from(qxl->guest_primary.surface.width,
110 qxl->guest_primary.surface.height,
111 qxl->guest_primary.bits_pp,
0e2487bd 112 qxl->guest_primary.abs_stride,
a19cbfb3
GH
113 ptr);
114 dpy_resize(vga->ds);
115 }
116
117 if (!qxl->guest_primary.commands) {
118 return;
119 }
120 qxl->guest_primary.commands = 0;
121
122 update.left = 0;
123 update.right = qxl->guest_primary.surface.width;
124 update.top = 0;
125 update.bottom = qxl->guest_primary.surface.height;
126
127 memset(dirty, 0, sizeof(dirty));
aee32bf3 128 qxl_spice_update_area(qxl, 0, &update,
5ff4e36c 129 dirty, ARRAY_SIZE(dirty), 1, QXL_SYNC);
a19cbfb3
GH
130
131 for (i = 0; i < ARRAY_SIZE(dirty); i++) {
132 if (qemu_spice_rect_is_empty(dirty+i)) {
133 break;
134 }
135 if (qxl->guest_primary.flipped) {
136 qxl_flip(qxl, dirty+i);
137 }
138 dpy_update(vga->ds,
139 dirty[i].left, dirty[i].top,
140 dirty[i].right - dirty[i].left,
141 dirty[i].bottom - dirty[i].top);
142 }
143}
144
145static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
146{
147 QEMUCursor *c;
148 uint8_t *image, *mask;
149 int size;
150
151 c = cursor_alloc(cursor->header.width, cursor->header.height);
152 c->hot_x = cursor->header.hot_spot_x;
153 c->hot_y = cursor->header.hot_spot_y;
154 switch (cursor->header.type) {
155 case SPICE_CURSOR_TYPE_ALPHA:
156 size = cursor->header.width * cursor->header.height * sizeof(uint32_t);
157 memcpy(c->data, cursor->chunk.data, size);
158 if (qxl->debug > 2) {
159 cursor_print_ascii_art(c, "qxl/alpha");
160 }
161 break;
162 case SPICE_CURSOR_TYPE_MONO:
163 mask = cursor->chunk.data;
164 image = mask + cursor_get_mono_bpl(c) * c->width;
165 cursor_set_mono(c, 0xffffff, 0x000000, image, 1, mask);
166 if (qxl->debug > 2) {
167 cursor_print_ascii_art(c, "qxl/mono");
168 }
169 break;
170 default:
171 fprintf(stderr, "%s: not implemented: type %d\n",
172 __FUNCTION__, cursor->header.type);
173 goto fail;
174 }
175 return c;
176
177fail:
178 cursor_put(c);
179 return NULL;
180}
181
182
183/* called from spice server thread context only */
184void qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
185{
186 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
187 QXLCursor *cursor;
188 QEMUCursor *c;
a19cbfb3
GH
189
190 if (!qxl->ssd.ds->mouse_set || !qxl->ssd.ds->cursor_define) {
191 return;
192 }
193
194 if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
195 fprintf(stderr, "%s", __FUNCTION__);
196 qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
197 fprintf(stderr, "\n");
198 }
199 switch (cmd->type) {
200 case QXL_CURSOR_SET:
a19cbfb3
GH
201 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
202 if (cursor->chunk.data_size != cursor->data_size) {
203 fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__);
204 return;
205 }
206 c = qxl_cursor(qxl, cursor);
207 if (c == NULL) {
208 c = cursor_builtin_left_ptr();
209 }
07536094
GH
210 qemu_mutex_lock(&qxl->ssd.lock);
211 if (qxl->ssd.cursor) {
212 cursor_put(qxl->ssd.cursor);
213 }
214 qxl->ssd.cursor = c;
215 qxl->ssd.mouse_x = cmd->u.set.position.x;
216 qxl->ssd.mouse_y = cmd->u.set.position.y;
217 qemu_mutex_unlock(&qxl->ssd.lock);
a19cbfb3
GH
218 break;
219 case QXL_CURSOR_MOVE:
07536094
GH
220 qemu_mutex_lock(&qxl->ssd.lock);
221 qxl->ssd.mouse_x = cmd->u.position.x;
222 qxl->ssd.mouse_y = cmd->u.position.y;
223 qemu_mutex_unlock(&qxl->ssd.lock);
a19cbfb3
GH
224 break;
225 }
226}