Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 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 | |
Paolo Bonzini | 47b43a1 | 2013-03-18 17:36:02 +0100 | [diff] [blame] | 22 | #include "qxl.h" |
Stefan Weil | b1829cd | 2013-12-07 15:09:12 +0100 | [diff] [blame] | 23 | #include "trace.h" |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 24 | |
Gerd Hoffmann | e2efc0a | 2012-02-27 11:05:09 +0100 | [diff] [blame] | 25 | static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect) |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 26 | { |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 27 | DisplaySurface *surface = qemu_console_surface(qxl->vga.con); |
| 28 | uint8_t *dst = surface_data(surface); |
Alon Levy | 4c19ebb | 2012-02-24 23:19:29 +0200 | [diff] [blame] | 29 | uint8_t *src; |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 30 | int len, i; |
| 31 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 32 | if (is_buffer_shared(surface)) { |
Alon Levy | 4c19ebb | 2012-02-24 23:19:29 +0200 | [diff] [blame] | 33 | return; |
| 34 | } |
Alon Levy | d53291c | 2012-03-18 13:46:15 +0100 | [diff] [blame] | 35 | trace_qxl_render_blit(qxl->guest_primary.qxl_stride, |
Alon Levy | 4c19ebb | 2012-02-24 23:19:29 +0200 | [diff] [blame] | 36 | rect->left, rect->right, rect->top, rect->bottom); |
| 37 | src = qxl->guest_primary.data; |
Gerd Hoffmann | e2efc0a | 2012-02-27 11:05:09 +0100 | [diff] [blame] | 38 | if (qxl->guest_primary.qxl_stride < 0) { |
| 39 | /* qxl surface is upside down, walk src scanlines |
| 40 | * in reverse order to flip it */ |
| 41 | src += (qxl->guest_primary.surface.height - rect->top - 1) * |
| 42 | qxl->guest_primary.abs_stride; |
| 43 | } else { |
| 44 | src += rect->top * qxl->guest_primary.abs_stride; |
| 45 | } |
Gerd Hoffmann | 0e2487b | 2011-10-21 15:59:07 +0200 | [diff] [blame] | 46 | dst += rect->top * qxl->guest_primary.abs_stride; |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 47 | src += rect->left * qxl->guest_primary.bytes_pp; |
| 48 | dst += rect->left * qxl->guest_primary.bytes_pp; |
| 49 | len = (rect->right - rect->left) * qxl->guest_primary.bytes_pp; |
| 50 | |
| 51 | for (i = rect->top; i < rect->bottom; i++) { |
| 52 | memcpy(dst, src, len); |
Gerd Hoffmann | 0e2487b | 2011-10-21 15:59:07 +0200 | [diff] [blame] | 53 | dst += qxl->guest_primary.abs_stride; |
Gerd Hoffmann | e2efc0a | 2012-02-27 11:05:09 +0100 | [diff] [blame] | 54 | src += qxl->guest_primary.qxl_stride; |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | void qxl_render_resize(PCIQXLDevice *qxl) |
| 59 | { |
| 60 | QXLSurfaceCreate *sc = &qxl->guest_primary.surface; |
| 61 | |
Gerd Hoffmann | 0e2487b | 2011-10-21 15:59:07 +0200 | [diff] [blame] | 62 | qxl->guest_primary.qxl_stride = sc->stride; |
| 63 | qxl->guest_primary.abs_stride = abs(sc->stride); |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 64 | qxl->guest_primary.resized++; |
| 65 | switch (sc->format) { |
| 66 | case SPICE_SURFACE_FMT_16_555: |
| 67 | qxl->guest_primary.bytes_pp = 2; |
| 68 | qxl->guest_primary.bits_pp = 15; |
| 69 | break; |
| 70 | case SPICE_SURFACE_FMT_16_565: |
| 71 | qxl->guest_primary.bytes_pp = 2; |
| 72 | qxl->guest_primary.bits_pp = 16; |
| 73 | break; |
| 74 | case SPICE_SURFACE_FMT_32_xRGB: |
| 75 | case SPICE_SURFACE_FMT_32_ARGB: |
| 76 | qxl->guest_primary.bytes_pp = 4; |
| 77 | qxl->guest_primary.bits_pp = 32; |
| 78 | break; |
| 79 | default: |
| 80 | fprintf(stderr, "%s: unhandled format: %x\n", __FUNCTION__, |
| 81 | qxl->guest_primary.surface.format); |
| 82 | qxl->guest_primary.bytes_pp = 4; |
| 83 | qxl->guest_primary.bits_pp = 32; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
Alon Levy | 81fb6f1 | 2012-02-24 23:19:31 +0200 | [diff] [blame] | 88 | static void qxl_set_rect_to_surface(PCIQXLDevice *qxl, QXLRect *area) |
| 89 | { |
| 90 | area->left = 0; |
| 91 | area->right = qxl->guest_primary.surface.width; |
| 92 | area->top = 0; |
| 93 | area->bottom = qxl->guest_primary.surface.height; |
| 94 | } |
| 95 | |
| 96 | static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl) |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 97 | { |
| 98 | VGACommonState *vga = &qxl->vga; |
Gerd Hoffmann | da229ef | 2013-02-28 10:48:02 +0100 | [diff] [blame] | 99 | DisplaySurface *surface; |
Alon Levy | 81fb6f1 | 2012-02-24 23:19:31 +0200 | [diff] [blame] | 100 | int i; |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 101 | |
| 102 | if (qxl->guest_primary.resized) { |
| 103 | qxl->guest_primary.resized = 0; |
Gerd Hoffmann | c58c7b9 | 2013-09-05 21:57:19 +0200 | [diff] [blame] | 104 | qxl->guest_primary.data = qxl_phys2virt(qxl, |
| 105 | qxl->guest_primary.surface.mem, |
| 106 | MEMSLOT_GROUP_GUEST); |
| 107 | if (!qxl->guest_primary.data) { |
| 108 | return; |
| 109 | } |
Alon Levy | 81fb6f1 | 2012-02-24 23:19:31 +0200 | [diff] [blame] | 110 | qxl_set_rect_to_surface(qxl, &qxl->dirty[0]); |
| 111 | qxl->num_dirty_rects = 1; |
Alon Levy | d53291c | 2012-03-18 13:46:15 +0100 | [diff] [blame] | 112 | trace_qxl_render_guest_primary_resized( |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 113 | qxl->guest_primary.surface.width, |
| 114 | qxl->guest_primary.surface.height, |
Gerd Hoffmann | 0e2487b | 2011-10-21 15:59:07 +0200 | [diff] [blame] | 115 | qxl->guest_primary.qxl_stride, |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 116 | qxl->guest_primary.bytes_pp, |
Alon Levy | 4c19ebb | 2012-02-24 23:19:29 +0200 | [diff] [blame] | 117 | qxl->guest_primary.bits_pp); |
Alon Levy | 4c19ebb | 2012-02-24 23:19:29 +0200 | [diff] [blame] | 118 | if (qxl->guest_primary.qxl_stride > 0) { |
Gerd Hoffmann | 30f1e66 | 2014-06-18 11:03:15 +0200 | [diff] [blame] | 119 | pixman_format_code_t format = |
| 120 | qemu_default_pixman_format(qxl->guest_primary.bits_pp, true); |
Gerd Hoffmann | da229ef | 2013-02-28 10:48:02 +0100 | [diff] [blame] | 121 | surface = qemu_create_displaysurface_from |
Gerd Hoffmann | 2f464b5 | 2012-12-10 07:41:07 +0100 | [diff] [blame] | 122 | (qxl->guest_primary.surface.width, |
| 123 | qxl->guest_primary.surface.height, |
Gerd Hoffmann | 30f1e66 | 2014-06-18 11:03:15 +0200 | [diff] [blame] | 124 | format, |
Gerd Hoffmann | 2f464b5 | 2012-12-10 07:41:07 +0100 | [diff] [blame] | 125 | qxl->guest_primary.abs_stride, |
Gerd Hoffmann | 30f1e66 | 2014-06-18 11:03:15 +0200 | [diff] [blame] | 126 | qxl->guest_primary.data); |
Alon Levy | 4c19ebb | 2012-02-24 23:19:29 +0200 | [diff] [blame] | 127 | } else { |
Gerd Hoffmann | da229ef | 2013-02-28 10:48:02 +0100 | [diff] [blame] | 128 | surface = qemu_create_displaysurface |
| 129 | (qxl->guest_primary.surface.width, |
| 130 | qxl->guest_primary.surface.height); |
Alon Levy | 4c19ebb | 2012-02-24 23:19:29 +0200 | [diff] [blame] | 131 | } |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 132 | dpy_gfx_replace_surface(vga->con, surface); |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 133 | } |
Gerd Hoffmann | c58c7b9 | 2013-09-05 21:57:19 +0200 | [diff] [blame] | 134 | |
| 135 | if (!qxl->guest_primary.data) { |
| 136 | return; |
| 137 | } |
Alon Levy | 81fb6f1 | 2012-02-24 23:19:31 +0200 | [diff] [blame] | 138 | for (i = 0; i < qxl->num_dirty_rects; i++) { |
| 139 | if (qemu_spice_rect_is_empty(qxl->dirty+i)) { |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 140 | break; |
| 141 | } |
Gerd Hoffmann | 503b3b3 | 2014-08-29 09:27:52 +0200 | [diff] [blame] | 142 | if (qxl->dirty[i].left < 0 || |
| 143 | qxl->dirty[i].top < 0 || |
| 144 | qxl->dirty[i].left > qxl->dirty[i].right || |
Gerd Hoffmann | 788fbf0 | 2014-06-10 13:51:12 +0200 | [diff] [blame] | 145 | qxl->dirty[i].top > qxl->dirty[i].bottom || |
| 146 | qxl->dirty[i].right > qxl->guest_primary.surface.width || |
| 147 | qxl->dirty[i].bottom > qxl->guest_primary.surface.height) { |
| 148 | continue; |
| 149 | } |
Gerd Hoffmann | e2efc0a | 2012-02-27 11:05:09 +0100 | [diff] [blame] | 150 | qxl_blit(qxl, qxl->dirty+i); |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 151 | dpy_gfx_update(vga->con, |
Gerd Hoffmann | a93a4a2 | 2012-09-28 15:02:08 +0200 | [diff] [blame] | 152 | qxl->dirty[i].left, qxl->dirty[i].top, |
| 153 | qxl->dirty[i].right - qxl->dirty[i].left, |
| 154 | qxl->dirty[i].bottom - qxl->dirty[i].top); |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 155 | } |
Alon Levy | 81fb6f1 | 2012-02-24 23:19:31 +0200 | [diff] [blame] | 156 | qxl->num_dirty_rects = 0; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * use ssd.lock to protect render_update_cookie_num. |
| 161 | * qxl_render_update is called by io thread or vcpu thread, and the completion |
Veres Lajos | 67cc32e | 2015-09-08 22:45:14 +0100 | [diff] [blame] | 162 | * callbacks are called by spice_server thread, deferring to bh called from the |
Alon Levy | 81fb6f1 | 2012-02-24 23:19:31 +0200 | [diff] [blame] | 163 | * io thread. |
| 164 | */ |
| 165 | void qxl_render_update(PCIQXLDevice *qxl) |
| 166 | { |
| 167 | QXLCookie *cookie; |
| 168 | |
| 169 | qemu_mutex_lock(&qxl->ssd.lock); |
| 170 | |
| 171 | if (!runstate_is_running() || !qxl->guest_primary.commands) { |
| 172 | qxl_render_update_area_unlocked(qxl); |
| 173 | qemu_mutex_unlock(&qxl->ssd.lock); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | qxl->guest_primary.commands = 0; |
| 178 | qxl->render_update_cookie_num++; |
| 179 | qemu_mutex_unlock(&qxl->ssd.lock); |
| 180 | cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA, |
| 181 | 0); |
| 182 | qxl_set_rect_to_surface(qxl, &cookie->u.render.area); |
| 183 | qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL, |
| 184 | 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie); |
| 185 | } |
| 186 | |
| 187 | void qxl_render_update_area_bh(void *opaque) |
| 188 | { |
| 189 | PCIQXLDevice *qxl = opaque; |
| 190 | |
| 191 | qemu_mutex_lock(&qxl->ssd.lock); |
| 192 | qxl_render_update_area_unlocked(qxl); |
| 193 | qemu_mutex_unlock(&qxl->ssd.lock); |
| 194 | } |
| 195 | |
| 196 | void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie) |
| 197 | { |
| 198 | qemu_mutex_lock(&qxl->ssd.lock); |
Alon Levy | d53291c | 2012-03-18 13:46:15 +0100 | [diff] [blame] | 199 | trace_qxl_render_update_area_done(cookie); |
Alon Levy | 81fb6f1 | 2012-02-24 23:19:31 +0200 | [diff] [blame] | 200 | qemu_bh_schedule(qxl->update_area_bh); |
| 201 | qxl->render_update_cookie_num--; |
| 202 | qemu_mutex_unlock(&qxl->ssd.lock); |
| 203 | g_free(cookie); |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor) |
| 207 | { |
| 208 | QEMUCursor *c; |
| 209 | uint8_t *image, *mask; |
Markus Armbruster | 66d3f19 | 2011-11-09 09:52:55 +0100 | [diff] [blame] | 210 | size_t size; |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 211 | |
| 212 | c = cursor_alloc(cursor->header.width, cursor->header.height); |
| 213 | c->hot_x = cursor->header.hot_spot_x; |
| 214 | c->hot_y = cursor->header.hot_spot_y; |
| 215 | switch (cursor->header.type) { |
| 216 | case SPICE_CURSOR_TYPE_ALPHA: |
Gerd Hoffmann | bfc1012 | 2013-06-03 10:36:54 +0200 | [diff] [blame] | 217 | size = sizeof(uint32_t) * cursor->header.width * cursor->header.height; |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 218 | memcpy(c->data, cursor->chunk.data, size); |
| 219 | if (qxl->debug > 2) { |
| 220 | cursor_print_ascii_art(c, "qxl/alpha"); |
| 221 | } |
| 222 | break; |
| 223 | case SPICE_CURSOR_TYPE_MONO: |
| 224 | mask = cursor->chunk.data; |
| 225 | image = mask + cursor_get_mono_bpl(c) * c->width; |
| 226 | cursor_set_mono(c, 0xffffff, 0x000000, image, 1, mask); |
| 227 | if (qxl->debug > 2) { |
| 228 | cursor_print_ascii_art(c, "qxl/mono"); |
| 229 | } |
| 230 | break; |
| 231 | default: |
| 232 | fprintf(stderr, "%s: not implemented: type %d\n", |
| 233 | __FUNCTION__, cursor->header.type); |
| 234 | goto fail; |
| 235 | } |
| 236 | return c; |
| 237 | |
| 238 | fail: |
| 239 | cursor_put(c); |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /* called from spice server thread context only */ |
Alon Levy | fae2afb | 2012-04-25 12:13:18 +0300 | [diff] [blame] | 245 | int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext) |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 246 | { |
| 247 | QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id); |
| 248 | QXLCursor *cursor; |
| 249 | QEMUCursor *c; |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 250 | |
Alon Levy | fae2afb | 2012-04-25 12:13:18 +0300 | [diff] [blame] | 251 | if (!cmd) { |
| 252 | return 1; |
| 253 | } |
| 254 | |
Gerd Hoffmann | c78f713 | 2013-03-05 15:24:14 +0100 | [diff] [blame] | 255 | if (!dpy_cursor_define_supported(qxl->vga.con)) { |
Alon Levy | fae2afb | 2012-04-25 12:13:18 +0300 | [diff] [blame] | 256 | return 0; |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) { |
| 260 | fprintf(stderr, "%s", __FUNCTION__); |
| 261 | qxl_log_cmd_cursor(qxl, cmd, ext->group_id); |
| 262 | fprintf(stderr, "\n"); |
| 263 | } |
| 264 | switch (cmd->type) { |
| 265 | case QXL_CURSOR_SET: |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 266 | cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id); |
Alon Levy | fae2afb | 2012-04-25 12:13:18 +0300 | [diff] [blame] | 267 | if (!cursor) { |
| 268 | return 1; |
| 269 | } |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 270 | if (cursor->chunk.data_size != cursor->data_size) { |
| 271 | fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__); |
Alon Levy | fae2afb | 2012-04-25 12:13:18 +0300 | [diff] [blame] | 272 | return 1; |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 273 | } |
| 274 | c = qxl_cursor(qxl, cursor); |
| 275 | if (c == NULL) { |
| 276 | c = cursor_builtin_left_ptr(); |
| 277 | } |
Gerd Hoffmann | 0753609 | 2011-04-27 15:50:32 +0200 | [diff] [blame] | 278 | qemu_mutex_lock(&qxl->ssd.lock); |
| 279 | if (qxl->ssd.cursor) { |
| 280 | cursor_put(qxl->ssd.cursor); |
| 281 | } |
| 282 | qxl->ssd.cursor = c; |
| 283 | qxl->ssd.mouse_x = cmd->u.set.position.x; |
| 284 | qxl->ssd.mouse_y = cmd->u.set.position.y; |
| 285 | qemu_mutex_unlock(&qxl->ssd.lock); |
Gerd Hoffmann | 0b2824e | 2014-11-04 13:59:59 +0100 | [diff] [blame] | 286 | qemu_bh_schedule(qxl->ssd.cursor_bh); |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 287 | break; |
| 288 | case QXL_CURSOR_MOVE: |
Gerd Hoffmann | 0753609 | 2011-04-27 15:50:32 +0200 | [diff] [blame] | 289 | qemu_mutex_lock(&qxl->ssd.lock); |
| 290 | qxl->ssd.mouse_x = cmd->u.position.x; |
| 291 | qxl->ssd.mouse_y = cmd->u.position.y; |
| 292 | qemu_mutex_unlock(&qxl->ssd.lock); |
Gerd Hoffmann | 0b2824e | 2014-11-04 13:59:59 +0100 | [diff] [blame] | 293 | qemu_bh_schedule(qxl->ssd.cursor_bh); |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 294 | break; |
| 295 | } |
Alon Levy | fae2afb | 2012-04-25 12:13:18 +0300 | [diff] [blame] | 296 | return 0; |
Gerd Hoffmann | a19cbfb | 2010-04-27 11:50:11 +0200 | [diff] [blame] | 297 | } |