2
0

qxl-render.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 "sysemu/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 (is_buffer_shared(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. if (!qxl->guest_primary.data) {
  103. goto end;
  104. }
  105. qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
  106. qxl->num_dirty_rects = 1;
  107. trace_qxl_render_guest_primary_resized(
  108. width,
  109. height,
  110. qxl->guest_primary.qxl_stride,
  111. qxl->guest_primary.bytes_pp,
  112. qxl->guest_primary.bits_pp);
  113. if (qxl->guest_primary.qxl_stride > 0) {
  114. pixman_format_code_t format =
  115. qemu_default_pixman_format(qxl->guest_primary.bits_pp, true);
  116. surface = qemu_create_displaysurface_from
  117. (width,
  118. height,
  119. format,
  120. qxl->guest_primary.abs_stride,
  121. qxl->guest_primary.data);
  122. } else {
  123. surface = qemu_create_displaysurface
  124. (width,
  125. height);
  126. }
  127. dpy_gfx_replace_surface(vga->con, surface);
  128. }
  129. if (!qxl->guest_primary.data) {
  130. goto end;
  131. }
  132. for (i = 0; i < qxl->num_dirty_rects; i++) {
  133. if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
  134. break;
  135. }
  136. if (qxl->dirty[i].left < 0 ||
  137. qxl->dirty[i].top < 0 ||
  138. qxl->dirty[i].left > qxl->dirty[i].right ||
  139. qxl->dirty[i].top > qxl->dirty[i].bottom ||
  140. qxl->dirty[i].right > width ||
  141. qxl->dirty[i].bottom > height) {
  142. continue;
  143. }
  144. qxl_blit(qxl, qxl->dirty+i);
  145. dpy_gfx_update(vga->con,
  146. qxl->dirty[i].left, qxl->dirty[i].top,
  147. qxl->dirty[i].right - qxl->dirty[i].left,
  148. qxl->dirty[i].bottom - qxl->dirty[i].top);
  149. }
  150. qxl->num_dirty_rects = 0;
  151. end:
  152. if (qxl->render_update_cookie_num == 0) {
  153. graphic_hw_update_done(qxl->ssd.dcl.con);
  154. }
  155. }
  156. /*
  157. * use ssd.lock to protect render_update_cookie_num.
  158. * qxl_render_update is called by io thread or vcpu thread, and the completion
  159. * callbacks are called by spice_server thread, deferring to bh called from the
  160. * io thread.
  161. */
  162. void qxl_render_update(PCIQXLDevice *qxl)
  163. {
  164. QXLCookie *cookie;
  165. qemu_mutex_lock(&qxl->ssd.lock);
  166. if (!runstate_is_running() || !qxl->guest_primary.commands ||
  167. qxl->mode == QXL_MODE_UNDEFINED) {
  168. qxl_render_update_area_unlocked(qxl);
  169. qemu_mutex_unlock(&qxl->ssd.lock);
  170. return;
  171. }
  172. qxl->guest_primary.commands = 0;
  173. qxl->render_update_cookie_num++;
  174. qemu_mutex_unlock(&qxl->ssd.lock);
  175. cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
  176. 0);
  177. qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
  178. qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
  179. 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
  180. }
  181. void qxl_render_update_area_bh(void *opaque)
  182. {
  183. PCIQXLDevice *qxl = opaque;
  184. qemu_mutex_lock(&qxl->ssd.lock);
  185. qxl_render_update_area_unlocked(qxl);
  186. qemu_mutex_unlock(&qxl->ssd.lock);
  187. }
  188. void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
  189. {
  190. qemu_mutex_lock(&qxl->ssd.lock);
  191. trace_qxl_render_update_area_done(cookie);
  192. qemu_bh_schedule(qxl->update_area_bh);
  193. qxl->render_update_cookie_num--;
  194. qemu_mutex_unlock(&qxl->ssd.lock);
  195. g_free(cookie);
  196. }
  197. static void qxl_unpack_chunks(void *dest, size_t size, PCIQXLDevice *qxl,
  198. QXLDataChunk *chunk, uint32_t group_id)
  199. {
  200. uint32_t max_chunks = 32;
  201. size_t offset = 0;
  202. size_t bytes;
  203. for (;;) {
  204. bytes = MIN(size - offset, chunk->data_size);
  205. memcpy(dest + offset, chunk->data, bytes);
  206. offset += bytes;
  207. if (offset == size) {
  208. return;
  209. }
  210. chunk = qxl_phys2virt(qxl, chunk->next_chunk, group_id);
  211. if (!chunk) {
  212. return;
  213. }
  214. max_chunks--;
  215. if (max_chunks == 0) {
  216. return;
  217. }
  218. }
  219. }
  220. static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor,
  221. uint32_t group_id)
  222. {
  223. QEMUCursor *c;
  224. uint8_t *and_mask, *xor_mask;
  225. size_t size;
  226. c = cursor_alloc(cursor->header.width, cursor->header.height);
  227. c->hot_x = cursor->header.hot_spot_x;
  228. c->hot_y = cursor->header.hot_spot_y;
  229. switch (cursor->header.type) {
  230. case SPICE_CURSOR_TYPE_MONO:
  231. /* Assume that the full cursor is available in a single chunk. */
  232. size = 2 * cursor_get_mono_bpl(c) * c->height;
  233. if (size != cursor->data_size) {
  234. fprintf(stderr, "%s: bad monochrome cursor %ux%u with size %u\n",
  235. __func__, c->width, c->height, cursor->data_size);
  236. goto fail;
  237. }
  238. and_mask = cursor->chunk.data;
  239. xor_mask = and_mask + cursor_get_mono_bpl(c) * c->height;
  240. cursor_set_mono(c, 0xffffff, 0x000000, xor_mask, 1, and_mask);
  241. if (qxl->debug > 2) {
  242. cursor_print_ascii_art(c, "qxl/mono");
  243. }
  244. break;
  245. case SPICE_CURSOR_TYPE_ALPHA:
  246. size = sizeof(uint32_t) * cursor->header.width * cursor->header.height;
  247. qxl_unpack_chunks(c->data, size, qxl, &cursor->chunk, group_id);
  248. if (qxl->debug > 2) {
  249. cursor_print_ascii_art(c, "qxl/alpha");
  250. }
  251. break;
  252. default:
  253. fprintf(stderr, "%s: not implemented: type %d\n",
  254. __func__, cursor->header.type);
  255. goto fail;
  256. }
  257. return c;
  258. fail:
  259. cursor_put(c);
  260. return NULL;
  261. }
  262. /* called from spice server thread context only */
  263. int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
  264. {
  265. QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
  266. QXLCursor *cursor;
  267. QEMUCursor *c;
  268. if (!cmd) {
  269. return 1;
  270. }
  271. if (!dpy_cursor_define_supported(qxl->vga.con)) {
  272. return 0;
  273. }
  274. if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
  275. fprintf(stderr, "%s", __func__);
  276. qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
  277. fprintf(stderr, "\n");
  278. }
  279. switch (cmd->type) {
  280. case QXL_CURSOR_SET:
  281. cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
  282. if (!cursor) {
  283. return 1;
  284. }
  285. c = qxl_cursor(qxl, cursor, ext->group_id);
  286. if (c == NULL) {
  287. c = cursor_builtin_left_ptr();
  288. }
  289. qemu_mutex_lock(&qxl->ssd.lock);
  290. if (qxl->ssd.cursor) {
  291. cursor_put(qxl->ssd.cursor);
  292. }
  293. qxl->ssd.cursor = c;
  294. qxl->ssd.mouse_x = cmd->u.set.position.x;
  295. qxl->ssd.mouse_y = cmd->u.set.position.y;
  296. qemu_mutex_unlock(&qxl->ssd.lock);
  297. qemu_bh_schedule(qxl->ssd.cursor_bh);
  298. break;
  299. case QXL_CURSOR_MOVE:
  300. qemu_mutex_lock(&qxl->ssd.lock);
  301. qxl->ssd.mouse_x = cmd->u.position.x;
  302. qxl->ssd.mouse_y = cmd->u.position.y;
  303. qemu_mutex_unlock(&qxl->ssd.lock);
  304. qemu_bh_schedule(qxl->ssd.cursor_bh);
  305. break;
  306. }
  307. return 0;
  308. }