2
0

qxl-render.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. return;
  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. return;
  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. }
  152. /*
  153. * use ssd.lock to protect render_update_cookie_num.
  154. * qxl_render_update is called by io thread or vcpu thread, and the completion
  155. * callbacks are called by spice_server thread, deferring to bh called from the
  156. * io thread.
  157. */
  158. void qxl_render_update(PCIQXLDevice *qxl)
  159. {
  160. QXLCookie *cookie;
  161. qemu_mutex_lock(&qxl->ssd.lock);
  162. if (!runstate_is_running() || !qxl->guest_primary.commands ||
  163. qxl->mode == QXL_MODE_UNDEFINED) {
  164. qxl_render_update_area_unlocked(qxl);
  165. qemu_mutex_unlock(&qxl->ssd.lock);
  166. return;
  167. }
  168. qxl->guest_primary.commands = 0;
  169. qxl->render_update_cookie_num++;
  170. qemu_mutex_unlock(&qxl->ssd.lock);
  171. cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
  172. 0);
  173. qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
  174. qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
  175. 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
  176. }
  177. void qxl_render_update_area_bh(void *opaque)
  178. {
  179. PCIQXLDevice *qxl = opaque;
  180. qemu_mutex_lock(&qxl->ssd.lock);
  181. qxl_render_update_area_unlocked(qxl);
  182. qemu_mutex_unlock(&qxl->ssd.lock);
  183. }
  184. void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
  185. {
  186. qemu_mutex_lock(&qxl->ssd.lock);
  187. trace_qxl_render_update_area_done(cookie);
  188. qemu_bh_schedule(qxl->update_area_bh);
  189. qxl->render_update_cookie_num--;
  190. qemu_mutex_unlock(&qxl->ssd.lock);
  191. g_free(cookie);
  192. }
  193. static void qxl_unpack_chunks(void *dest, size_t size, PCIQXLDevice *qxl,
  194. QXLDataChunk *chunk, uint32_t group_id)
  195. {
  196. uint32_t max_chunks = 32;
  197. size_t offset = 0;
  198. size_t bytes;
  199. for (;;) {
  200. bytes = MIN(size - offset, chunk->data_size);
  201. memcpy(dest + offset, chunk->data, bytes);
  202. offset += bytes;
  203. if (offset == size) {
  204. return;
  205. }
  206. chunk = qxl_phys2virt(qxl, chunk->next_chunk, group_id);
  207. if (!chunk) {
  208. return;
  209. }
  210. max_chunks--;
  211. if (max_chunks == 0) {
  212. return;
  213. }
  214. }
  215. }
  216. static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor,
  217. uint32_t group_id)
  218. {
  219. QEMUCursor *c;
  220. uint8_t *and_mask, *xor_mask;
  221. size_t size;
  222. c = cursor_alloc(cursor->header.width, cursor->header.height);
  223. c->hot_x = cursor->header.hot_spot_x;
  224. c->hot_y = cursor->header.hot_spot_y;
  225. switch (cursor->header.type) {
  226. case SPICE_CURSOR_TYPE_MONO:
  227. /* Assume that the full cursor is available in a single chunk. */
  228. size = 2 * cursor_get_mono_bpl(c) * c->height;
  229. if (size != cursor->data_size) {
  230. fprintf(stderr, "%s: bad monochrome cursor %ux%u with size %u\n",
  231. __func__, c->width, c->height, cursor->data_size);
  232. goto fail;
  233. }
  234. and_mask = cursor->chunk.data;
  235. xor_mask = and_mask + cursor_get_mono_bpl(c) * c->height;
  236. cursor_set_mono(c, 0xffffff, 0x000000, xor_mask, 1, and_mask);
  237. if (qxl->debug > 2) {
  238. cursor_print_ascii_art(c, "qxl/mono");
  239. }
  240. break;
  241. case SPICE_CURSOR_TYPE_ALPHA:
  242. size = sizeof(uint32_t) * cursor->header.width * cursor->header.height;
  243. qxl_unpack_chunks(c->data, size, qxl, &cursor->chunk, group_id);
  244. if (qxl->debug > 2) {
  245. cursor_print_ascii_art(c, "qxl/alpha");
  246. }
  247. break;
  248. default:
  249. fprintf(stderr, "%s: not implemented: type %d\n",
  250. __func__, cursor->header.type);
  251. goto fail;
  252. }
  253. return c;
  254. fail:
  255. cursor_put(c);
  256. return NULL;
  257. }
  258. /* called from spice server thread context only */
  259. int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
  260. {
  261. QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
  262. QXLCursor *cursor;
  263. QEMUCursor *c;
  264. if (!cmd) {
  265. return 1;
  266. }
  267. if (!dpy_cursor_define_supported(qxl->vga.con)) {
  268. return 0;
  269. }
  270. if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
  271. fprintf(stderr, "%s", __func__);
  272. qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
  273. fprintf(stderr, "\n");
  274. }
  275. switch (cmd->type) {
  276. case QXL_CURSOR_SET:
  277. cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
  278. if (!cursor) {
  279. return 1;
  280. }
  281. c = qxl_cursor(qxl, cursor, ext->group_id);
  282. if (c == NULL) {
  283. c = cursor_builtin_left_ptr();
  284. }
  285. qemu_mutex_lock(&qxl->ssd.lock);
  286. if (qxl->ssd.cursor) {
  287. cursor_put(qxl->ssd.cursor);
  288. }
  289. qxl->ssd.cursor = c;
  290. qxl->ssd.mouse_x = cmd->u.set.position.x;
  291. qxl->ssd.mouse_y = cmd->u.set.position.y;
  292. qemu_mutex_unlock(&qxl->ssd.lock);
  293. qemu_bh_schedule(qxl->ssd.cursor_bh);
  294. break;
  295. case QXL_CURSOR_MOVE:
  296. qemu_mutex_lock(&qxl->ssd.lock);
  297. qxl->ssd.mouse_x = cmd->u.position.x;
  298. qxl->ssd.mouse_y = cmd->u.position.y;
  299. qemu_mutex_unlock(&qxl->ssd.lock);
  300. qemu_bh_schedule(qxl->ssd.cursor_bh);
  301. break;
  302. }
  303. return 0;
  304. }