qxl-render.c 9.3 KB

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