qxl-render.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. qxl_blit(qxl, qxl->dirty+i);
  132. dpy_gfx_update(vga->con,
  133. qxl->dirty[i].left, qxl->dirty[i].top,
  134. qxl->dirty[i].right - qxl->dirty[i].left,
  135. qxl->dirty[i].bottom - qxl->dirty[i].top);
  136. }
  137. qxl->num_dirty_rects = 0;
  138. }
  139. /*
  140. * use ssd.lock to protect render_update_cookie_num.
  141. * qxl_render_update is called by io thread or vcpu thread, and the completion
  142. * callbacks are called by spice_server thread, defering to bh called from the
  143. * io thread.
  144. */
  145. void qxl_render_update(PCIQXLDevice *qxl)
  146. {
  147. QXLCookie *cookie;
  148. qemu_mutex_lock(&qxl->ssd.lock);
  149. if (!runstate_is_running() || !qxl->guest_primary.commands) {
  150. qxl_render_update_area_unlocked(qxl);
  151. qemu_mutex_unlock(&qxl->ssd.lock);
  152. return;
  153. }
  154. qxl->guest_primary.commands = 0;
  155. qxl->render_update_cookie_num++;
  156. qemu_mutex_unlock(&qxl->ssd.lock);
  157. cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
  158. 0);
  159. qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
  160. qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
  161. 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
  162. }
  163. void qxl_render_update_area_bh(void *opaque)
  164. {
  165. PCIQXLDevice *qxl = opaque;
  166. qemu_mutex_lock(&qxl->ssd.lock);
  167. qxl_render_update_area_unlocked(qxl);
  168. qemu_mutex_unlock(&qxl->ssd.lock);
  169. }
  170. void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
  171. {
  172. qemu_mutex_lock(&qxl->ssd.lock);
  173. trace_qxl_render_update_area_done(cookie);
  174. qemu_bh_schedule(qxl->update_area_bh);
  175. qxl->render_update_cookie_num--;
  176. qemu_mutex_unlock(&qxl->ssd.lock);
  177. g_free(cookie);
  178. }
  179. static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
  180. {
  181. QEMUCursor *c;
  182. uint8_t *image, *mask;
  183. size_t size;
  184. c = cursor_alloc(cursor->header.width, cursor->header.height);
  185. c->hot_x = cursor->header.hot_spot_x;
  186. c->hot_y = cursor->header.hot_spot_y;
  187. switch (cursor->header.type) {
  188. case SPICE_CURSOR_TYPE_ALPHA:
  189. size = sizeof(uint32_t) * cursor->header.width * cursor->header.height;
  190. memcpy(c->data, cursor->chunk.data, size);
  191. if (qxl->debug > 2) {
  192. cursor_print_ascii_art(c, "qxl/alpha");
  193. }
  194. break;
  195. case SPICE_CURSOR_TYPE_MONO:
  196. mask = cursor->chunk.data;
  197. image = mask + cursor_get_mono_bpl(c) * c->width;
  198. cursor_set_mono(c, 0xffffff, 0x000000, image, 1, mask);
  199. if (qxl->debug > 2) {
  200. cursor_print_ascii_art(c, "qxl/mono");
  201. }
  202. break;
  203. default:
  204. fprintf(stderr, "%s: not implemented: type %d\n",
  205. __FUNCTION__, cursor->header.type);
  206. goto fail;
  207. }
  208. return c;
  209. fail:
  210. cursor_put(c);
  211. return NULL;
  212. }
  213. /* called from spice server thread context only */
  214. int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
  215. {
  216. QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
  217. QXLCursor *cursor;
  218. QEMUCursor *c;
  219. if (!cmd) {
  220. return 1;
  221. }
  222. if (!dpy_cursor_define_supported(qxl->vga.con)) {
  223. return 0;
  224. }
  225. if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
  226. fprintf(stderr, "%s", __FUNCTION__);
  227. qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
  228. fprintf(stderr, "\n");
  229. }
  230. switch (cmd->type) {
  231. case QXL_CURSOR_SET:
  232. cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
  233. if (!cursor) {
  234. return 1;
  235. }
  236. if (cursor->chunk.data_size != cursor->data_size) {
  237. fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__);
  238. return 1;
  239. }
  240. c = qxl_cursor(qxl, cursor);
  241. if (c == NULL) {
  242. c = cursor_builtin_left_ptr();
  243. }
  244. qemu_mutex_lock(&qxl->ssd.lock);
  245. if (qxl->ssd.cursor) {
  246. cursor_put(qxl->ssd.cursor);
  247. }
  248. qxl->ssd.cursor = c;
  249. qxl->ssd.mouse_x = cmd->u.set.position.x;
  250. qxl->ssd.mouse_y = cmd->u.set.position.y;
  251. qemu_mutex_unlock(&qxl->ssd.lock);
  252. break;
  253. case QXL_CURSOR_MOVE:
  254. qemu_mutex_lock(&qxl->ssd.lock);
  255. qxl->ssd.mouse_x = cmd->u.position.x;
  256. qxl->ssd.mouse_y = cmd->u.position.y;
  257. qemu_mutex_unlock(&qxl->ssd.lock);
  258. break;
  259. }
  260. return 0;
  261. }