qxl-render.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #include "qemu/osdep.h"
  22. #include "qxl.h"
  23. #include "system/runstate.h"
  24. #include "trace.h"
  25. static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect)
  26. {
  27. DisplaySurface *surface = qemu_console_surface(qxl->vga.con);
  28. uint8_t *dst = surface_data(surface);
  29. uint8_t *src;
  30. int len, i;
  31. if (!surface_is_allocated(surface)) {
  32. return;
  33. }
  34. trace_qxl_render_blit(qxl->guest_primary.qxl_stride,
  35. rect->left, rect->right, rect->top, rect->bottom);
  36. src = qxl->guest_primary.data;
  37. if (qxl->guest_primary.qxl_stride < 0) {
  38. /* qxl surface is upside down, walk src scanlines
  39. * in reverse order to flip it */
  40. src += (qxl->guest_primary.surface.height - rect->top - 1) *
  41. qxl->guest_primary.abs_stride;
  42. } else {
  43. src += rect->top * qxl->guest_primary.abs_stride;
  44. }
  45. dst += rect->top * qxl->guest_primary.abs_stride;
  46. src += rect->left * qxl->guest_primary.bytes_pp;
  47. dst += rect->left * qxl->guest_primary.bytes_pp;
  48. len = (rect->right - rect->left) * qxl->guest_primary.bytes_pp;
  49. for (i = rect->top; i < rect->bottom; i++) {
  50. memcpy(dst, src, len);
  51. dst += qxl->guest_primary.abs_stride;
  52. src += qxl->guest_primary.qxl_stride;
  53. }
  54. }
  55. void qxl_render_resize(PCIQXLDevice *qxl)
  56. {
  57. QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
  58. qxl->guest_primary.qxl_stride = sc->stride;
  59. qxl->guest_primary.abs_stride = abs(sc->stride);
  60. qxl->guest_primary.resized++;
  61. switch (sc->format) {
  62. case SPICE_SURFACE_FMT_16_555:
  63. qxl->guest_primary.bytes_pp = 2;
  64. qxl->guest_primary.bits_pp = 15;
  65. break;
  66. case SPICE_SURFACE_FMT_16_565:
  67. qxl->guest_primary.bytes_pp = 2;
  68. qxl->guest_primary.bits_pp = 16;
  69. break;
  70. case SPICE_SURFACE_FMT_32_xRGB:
  71. case SPICE_SURFACE_FMT_32_ARGB:
  72. qxl->guest_primary.bytes_pp = 4;
  73. qxl->guest_primary.bits_pp = 32;
  74. break;
  75. default:
  76. fprintf(stderr, "%s: unhandled format: %x\n", __func__,
  77. qxl->guest_primary.surface.format);
  78. qxl->guest_primary.bytes_pp = 4;
  79. qxl->guest_primary.bits_pp = 32;
  80. break;
  81. }
  82. }
  83. static void qxl_set_rect_to_surface(PCIQXLDevice *qxl, QXLRect *area)
  84. {
  85. area->left = 0;
  86. area->right = qxl->guest_primary.surface.width;
  87. area->top = 0;
  88. area->bottom = qxl->guest_primary.surface.height;
  89. }
  90. static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
  91. {
  92. VGACommonState *vga = &qxl->vga;
  93. DisplaySurface *surface;
  94. int width = qxl->guest_head0_width ?: qxl->guest_primary.surface.width;
  95. int height = qxl->guest_head0_height ?: qxl->guest_primary.surface.height;
  96. int i;
  97. if (qxl->guest_primary.resized) {
  98. qxl->guest_primary.resized = 0;
  99. qxl->guest_primary.data = qxl_phys2virt(qxl,
  100. qxl->guest_primary.surface.mem,
  101. MEMSLOT_GROUP_GUEST,
  102. qxl->guest_primary.abs_stride
  103. * height);
  104. if (!qxl->guest_primary.data) {
  105. goto end;
  106. }
  107. qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
  108. qxl->num_dirty_rects = 1;
  109. trace_qxl_render_guest_primary_resized(
  110. width,
  111. height,
  112. qxl->guest_primary.qxl_stride,
  113. qxl->guest_primary.bytes_pp,
  114. qxl->guest_primary.bits_pp);
  115. if (qxl->guest_primary.qxl_stride > 0) {
  116. pixman_format_code_t format =
  117. qemu_default_pixman_format(qxl->guest_primary.bits_pp, true);
  118. surface = qemu_create_displaysurface_from
  119. (width,
  120. height,
  121. format,
  122. qxl->guest_primary.abs_stride,
  123. qxl->guest_primary.data);
  124. } else {
  125. surface = qemu_create_displaysurface
  126. (width,
  127. height);
  128. }
  129. dpy_gfx_replace_surface(vga->con, surface);
  130. }
  131. if (!qxl->guest_primary.data) {
  132. goto end;
  133. }
  134. for (i = 0; i < qxl->num_dirty_rects; i++) {
  135. if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
  136. break;
  137. }
  138. if (qxl->dirty[i].left < 0 ||
  139. qxl->dirty[i].top < 0 ||
  140. qxl->dirty[i].left > qxl->dirty[i].right ||
  141. qxl->dirty[i].top > qxl->dirty[i].bottom ||
  142. qxl->dirty[i].right > width ||
  143. qxl->dirty[i].bottom > height) {
  144. continue;
  145. }
  146. qxl_blit(qxl, qxl->dirty+i);
  147. dpy_gfx_update(vga->con,
  148. qxl->dirty[i].left, qxl->dirty[i].top,
  149. qxl->dirty[i].right - qxl->dirty[i].left,
  150. qxl->dirty[i].bottom - qxl->dirty[i].top);
  151. }
  152. qxl->num_dirty_rects = 0;
  153. end:
  154. if (qxl->render_update_cookie_num == 0) {
  155. graphic_hw_update_done(qxl->ssd.dcl.con);
  156. }
  157. }
  158. /*
  159. * use ssd.lock to protect render_update_cookie_num.
  160. * qxl_render_update is called by io thread or vcpu thread, and the completion
  161. * callbacks are called by spice_server thread, deferring to bh called from the
  162. * io thread.
  163. */
  164. void qxl_render_update(PCIQXLDevice *qxl)
  165. {
  166. QXLCookie *cookie;
  167. qemu_mutex_lock(&qxl->ssd.lock);
  168. if (!runstate_is_running() || !qxl->guest_primary.commands ||
  169. qxl->mode == QXL_MODE_UNDEFINED) {
  170. qxl_render_update_area_unlocked(qxl);
  171. qemu_mutex_unlock(&qxl->ssd.lock);
  172. graphic_hw_update_done(qxl->ssd.dcl.con);
  173. return;
  174. }
  175. qxl->guest_primary.commands = 0;
  176. qxl->render_update_cookie_num++;
  177. qemu_mutex_unlock(&qxl->ssd.lock);
  178. cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
  179. 0);
  180. qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
  181. qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
  182. 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
  183. }
  184. void qxl_render_update_area_bh(void *opaque)
  185. {
  186. PCIQXLDevice *qxl = opaque;
  187. qemu_mutex_lock(&qxl->ssd.lock);
  188. qxl_render_update_area_unlocked(qxl);
  189. qemu_mutex_unlock(&qxl->ssd.lock);
  190. }
  191. void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
  192. {
  193. qemu_mutex_lock(&qxl->ssd.lock);
  194. trace_qxl_render_update_area_done(cookie);
  195. qemu_bh_schedule(qxl->update_area_bh);
  196. qxl->render_update_cookie_num--;
  197. qemu_mutex_unlock(&qxl->ssd.lock);
  198. g_free(cookie);
  199. }
  200. static void qxl_unpack_chunks(void *dest, size_t size, PCIQXLDevice *qxl,
  201. QXLDataChunk *chunk, uint32_t group_id)
  202. {
  203. uint32_t max_chunks = 32;
  204. size_t offset = 0;
  205. size_t bytes;
  206. for (;;) {
  207. bytes = MIN(size - offset, chunk->data_size);
  208. memcpy(dest + offset, chunk->data, bytes);
  209. offset += bytes;
  210. if (offset == size) {
  211. return;
  212. }
  213. chunk = qxl_phys2virt(qxl, chunk->next_chunk, group_id,
  214. sizeof(QXLDataChunk) + chunk->data_size);
  215. if (!chunk) {
  216. return;
  217. }
  218. max_chunks--;
  219. if (max_chunks == 0) {
  220. return;
  221. }
  222. }
  223. }
  224. static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor,
  225. uint32_t group_id)
  226. {
  227. QEMUCursor *c;
  228. uint8_t *and_mask, *xor_mask;
  229. size_t size;
  230. c = cursor_alloc(cursor->header.width, cursor->header.height);
  231. if (!c) {
  232. qxl_set_guest_bug(qxl, "%s: cursor %ux%u alloc error", __func__,
  233. cursor->header.width, cursor->header.height);
  234. goto fail;
  235. }
  236. c->hot_x = cursor->header.hot_spot_x;
  237. c->hot_y = cursor->header.hot_spot_y;
  238. switch (cursor->header.type) {
  239. case SPICE_CURSOR_TYPE_MONO:
  240. /* Assume that the full cursor is available in a single chunk. */
  241. size = 2 * cursor_get_mono_bpl(c) * c->height;
  242. if (size != cursor->data_size) {
  243. fprintf(stderr, "%s: bad monochrome cursor %ux%u with size %u\n",
  244. __func__, c->width, c->height, cursor->data_size);
  245. goto fail;
  246. }
  247. and_mask = cursor->chunk.data;
  248. xor_mask = and_mask + cursor_get_mono_bpl(c) * c->height;
  249. cursor_set_mono(c, 0xffffff, 0x000000, xor_mask, 1, and_mask);
  250. if (qxl->debug > 2) {
  251. cursor_print_ascii_art(c, "qxl/mono");
  252. }
  253. break;
  254. case SPICE_CURSOR_TYPE_ALPHA:
  255. size = sizeof(uint32_t) * c->width * c->height;
  256. qxl_unpack_chunks(c->data, size, qxl, &cursor->chunk, group_id);
  257. if (qxl->debug > 2) {
  258. cursor_print_ascii_art(c, "qxl/alpha");
  259. }
  260. break;
  261. default:
  262. fprintf(stderr, "%s: not implemented: type %d\n",
  263. __func__, cursor->header.type);
  264. goto fail;
  265. }
  266. return c;
  267. fail:
  268. cursor_unref(c);
  269. return NULL;
  270. }
  271. /* called from spice server thread context only */
  272. int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
  273. {
  274. QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id,
  275. sizeof(QXLCursorCmd));
  276. QXLCursor *cursor;
  277. QEMUCursor *c;
  278. if (!cmd) {
  279. return 1;
  280. }
  281. if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
  282. fprintf(stderr, "%s", __func__);
  283. qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
  284. fprintf(stderr, "\n");
  285. }
  286. switch (cmd->type) {
  287. case QXL_CURSOR_SET:
  288. /* First read the QXLCursor to get QXLDataChunk::data_size ... */
  289. cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id,
  290. sizeof(QXLCursor));
  291. if (!cursor) {
  292. return 1;
  293. }
  294. /* Then read including the chunked data following QXLCursor. */
  295. cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id,
  296. sizeof(QXLCursor) + cursor->chunk.data_size);
  297. if (!cursor) {
  298. return 1;
  299. }
  300. c = qxl_cursor(qxl, cursor, ext->group_id);
  301. if (c == NULL) {
  302. c = cursor_builtin_left_ptr();
  303. }
  304. qemu_mutex_lock(&qxl->ssd.lock);
  305. if (qxl->ssd.cursor) {
  306. cursor_unref(qxl->ssd.cursor);
  307. }
  308. qxl->ssd.cursor = c;
  309. qxl->ssd.mouse_x = cmd->u.set.position.x;
  310. qxl->ssd.mouse_y = cmd->u.set.position.y;
  311. qemu_mutex_unlock(&qxl->ssd.lock);
  312. qemu_bh_schedule(qxl->ssd.cursor_bh);
  313. break;
  314. case QXL_CURSOR_MOVE:
  315. qemu_mutex_lock(&qxl->ssd.lock);
  316. qxl->ssd.mouse_x = cmd->u.position.x;
  317. qxl->ssd.mouse_y = cmd->u.position.y;
  318. qemu_mutex_unlock(&qxl->ssd.lock);
  319. qemu_bh_schedule(qxl->ssd.cursor_bh);
  320. break;
  321. }
  322. return 0;
  323. }