]> git.proxmox.com Git - qemu.git/blame - hw/qxl-render.c
console: stop using DisplayState in gfx hardware emulation
[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
83c9f4ca 22#include "hw/qxl.h"
a19cbfb3 23
e2efc0a3 24static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect)
a19cbfb3 25{
c78f7137
GH
26 DisplaySurface *surface = qemu_console_surface(qxl->vga.con);
27 uint8_t *dst = surface_data(surface);
4c19ebb5 28 uint8_t *src;
a19cbfb3
GH
29 int len, i;
30
c78f7137 31 if (is_buffer_shared(surface)) {
4c19ebb5
AL
32 return;
33 }
34 if (!qxl->guest_primary.data) {
d53291cf 35 trace_qxl_render_blit_guest_primary_initialized();
4c19ebb5
AL
36 qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
37 }
d53291cf 38 trace_qxl_render_blit(qxl->guest_primary.qxl_stride,
4c19ebb5
AL
39 rect->left, rect->right, rect->top, rect->bottom);
40 src = qxl->guest_primary.data;
e2efc0a3
GH
41 if (qxl->guest_primary.qxl_stride < 0) {
42 /* qxl surface is upside down, walk src scanlines
43 * in reverse order to flip it */
44 src += (qxl->guest_primary.surface.height - rect->top - 1) *
45 qxl->guest_primary.abs_stride;
46 } else {
47 src += rect->top * qxl->guest_primary.abs_stride;
48 }
0e2487bd 49 dst += rect->top * qxl->guest_primary.abs_stride;
a19cbfb3
GH
50 src += rect->left * qxl->guest_primary.bytes_pp;
51 dst += rect->left * qxl->guest_primary.bytes_pp;
52 len = (rect->right - rect->left) * qxl->guest_primary.bytes_pp;
53
54 for (i = rect->top; i < rect->bottom; i++) {
55 memcpy(dst, src, len);
0e2487bd 56 dst += qxl->guest_primary.abs_stride;
e2efc0a3 57 src += qxl->guest_primary.qxl_stride;
a19cbfb3
GH
58 }
59}
60
61void qxl_render_resize(PCIQXLDevice *qxl)
62{
63 QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
64
0e2487bd
GH
65 qxl->guest_primary.qxl_stride = sc->stride;
66 qxl->guest_primary.abs_stride = abs(sc->stride);
a19cbfb3
GH
67 qxl->guest_primary.resized++;
68 switch (sc->format) {
69 case SPICE_SURFACE_FMT_16_555:
70 qxl->guest_primary.bytes_pp = 2;
71 qxl->guest_primary.bits_pp = 15;
72 break;
73 case SPICE_SURFACE_FMT_16_565:
74 qxl->guest_primary.bytes_pp = 2;
75 qxl->guest_primary.bits_pp = 16;
76 break;
77 case SPICE_SURFACE_FMT_32_xRGB:
78 case SPICE_SURFACE_FMT_32_ARGB:
79 qxl->guest_primary.bytes_pp = 4;
80 qxl->guest_primary.bits_pp = 32;
81 break;
82 default:
83 fprintf(stderr, "%s: unhandled format: %x\n", __FUNCTION__,
84 qxl->guest_primary.surface.format);
85 qxl->guest_primary.bytes_pp = 4;
86 qxl->guest_primary.bits_pp = 32;
87 break;
88 }
89}
90
81fb6f15
AL
91static void qxl_set_rect_to_surface(PCIQXLDevice *qxl, QXLRect *area)
92{
93 area->left = 0;
94 area->right = qxl->guest_primary.surface.width;
95 area->top = 0;
96 area->bottom = qxl->guest_primary.surface.height;
97}
98
99static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
a19cbfb3
GH
100{
101 VGACommonState *vga = &qxl->vga;
da229ef3 102 DisplaySurface *surface;
81fb6f15 103 int i;
a19cbfb3
GH
104
105 if (qxl->guest_primary.resized) {
106 qxl->guest_primary.resized = 0;
b1950430 107 qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
81fb6f15
AL
108 qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
109 qxl->num_dirty_rects = 1;
d53291cf 110 trace_qxl_render_guest_primary_resized(
a19cbfb3
GH
111 qxl->guest_primary.surface.width,
112 qxl->guest_primary.surface.height,
0e2487bd 113 qxl->guest_primary.qxl_stride,
a19cbfb3 114 qxl->guest_primary.bytes_pp,
4c19ebb5 115 qxl->guest_primary.bits_pp);
4c19ebb5 116 if (qxl->guest_primary.qxl_stride > 0) {
da229ef3 117 surface = qemu_create_displaysurface_from
2f464b5a
GH
118 (qxl->guest_primary.surface.width,
119 qxl->guest_primary.surface.height,
120 qxl->guest_primary.bits_pp,
121 qxl->guest_primary.abs_stride,
b1424e03
GH
122 qxl->guest_primary.data,
123 false);
4c19ebb5 124 } else {
da229ef3
GH
125 surface = qemu_create_displaysurface
126 (qxl->guest_primary.surface.width,
127 qxl->guest_primary.surface.height);
4c19ebb5 128 }
c78f7137 129 dpy_gfx_replace_surface(vga->con, surface);
a19cbfb3 130 }
81fb6f15
AL
131 for (i = 0; i < qxl->num_dirty_rects; i++) {
132 if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
a19cbfb3
GH
133 break;
134 }
e2efc0a3 135 qxl_blit(qxl, qxl->dirty+i);
c78f7137 136 dpy_gfx_update(vga->con,
a93a4a22
GH
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);
a19cbfb3 140 }
81fb6f15
AL
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 */
150void 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
172void 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
181void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
182{
183 qemu_mutex_lock(&qxl->ssd.lock);
d53291cf 184 trace_qxl_render_update_area_done(cookie);
81fb6f15
AL
185 qemu_bh_schedule(qxl->update_area_bh);
186 qxl->render_update_cookie_num--;
187 qemu_mutex_unlock(&qxl->ssd.lock);
188 g_free(cookie);
a19cbfb3
GH
189}
190
191static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
192{
193 QEMUCursor *c;
194 uint8_t *image, *mask;
66d3f196 195 size_t size;
a19cbfb3
GH
196
197 c = cursor_alloc(cursor->header.width, cursor->header.height);
198 c->hot_x = cursor->header.hot_spot_x;
199 c->hot_y = cursor->header.hot_spot_y;
200 switch (cursor->header.type) {
201 case SPICE_CURSOR_TYPE_ALPHA:
202 size = cursor->header.width * cursor->header.height * sizeof(uint32_t);
203 memcpy(c->data, cursor->chunk.data, size);
204 if (qxl->debug > 2) {
205 cursor_print_ascii_art(c, "qxl/alpha");
206 }
207 break;
208 case SPICE_CURSOR_TYPE_MONO:
209 mask = cursor->chunk.data;
210 image = mask + cursor_get_mono_bpl(c) * c->width;
211 cursor_set_mono(c, 0xffffff, 0x000000, image, 1, mask);
212 if (qxl->debug > 2) {
213 cursor_print_ascii_art(c, "qxl/mono");
214 }
215 break;
216 default:
217 fprintf(stderr, "%s: not implemented: type %d\n",
218 __FUNCTION__, cursor->header.type);
219 goto fail;
220 }
221 return c;
222
223fail:
224 cursor_put(c);
225 return NULL;
226}
227
228
229/* called from spice server thread context only */
fae2afb1 230int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
a19cbfb3
GH
231{
232 QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
233 QXLCursor *cursor;
234 QEMUCursor *c;
a19cbfb3 235
fae2afb1
AL
236 if (!cmd) {
237 return 1;
238 }
239
c78f7137 240 if (!dpy_cursor_define_supported(qxl->vga.con)) {
fae2afb1 241 return 0;
a19cbfb3
GH
242 }
243
244 if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
245 fprintf(stderr, "%s", __FUNCTION__);
246 qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
247 fprintf(stderr, "\n");
248 }
249 switch (cmd->type) {
250 case QXL_CURSOR_SET:
a19cbfb3 251 cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
fae2afb1
AL
252 if (!cursor) {
253 return 1;
254 }
a19cbfb3
GH
255 if (cursor->chunk.data_size != cursor->data_size) {
256 fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__);
fae2afb1 257 return 1;
a19cbfb3
GH
258 }
259 c = qxl_cursor(qxl, cursor);
260 if (c == NULL) {
261 c = cursor_builtin_left_ptr();
262 }
07536094
GH
263 qemu_mutex_lock(&qxl->ssd.lock);
264 if (qxl->ssd.cursor) {
265 cursor_put(qxl->ssd.cursor);
266 }
267 qxl->ssd.cursor = c;
268 qxl->ssd.mouse_x = cmd->u.set.position.x;
269 qxl->ssd.mouse_y = cmd->u.set.position.y;
270 qemu_mutex_unlock(&qxl->ssd.lock);
a19cbfb3
GH
271 break;
272 case QXL_CURSOR_MOVE:
07536094
GH
273 qemu_mutex_lock(&qxl->ssd.lock);
274 qxl->ssd.mouse_x = cmd->u.position.x;
275 qxl->ssd.mouse_y = cmd->u.position.y;
276 qemu_mutex_unlock(&qxl->ssd.lock);
a19cbfb3
GH
277 break;
278 }
fae2afb1 279 return 0;
a19cbfb3 280}