qxl-render.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. static void qxl_blit(PCIQXLDevice *qxl, QXLRect *rect)
  23. {
  24. uint8_t *src;
  25. uint8_t *dst = qxl->vga.ds->surface->data;
  26. int len, i;
  27. if (is_buffer_shared(qxl->vga.ds->surface)) {
  28. return;
  29. }
  30. if (!qxl->guest_primary.data) {
  31. trace_qxl_render_blit_guest_primary_initialized();
  32. qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
  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", __FUNCTION__,
  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. int i;
  94. if (qxl->guest_primary.resized) {
  95. qxl->guest_primary.resized = 0;
  96. qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
  97. qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
  98. qxl->num_dirty_rects = 1;
  99. trace_qxl_render_guest_primary_resized(
  100. qxl->guest_primary.surface.width,
  101. qxl->guest_primary.surface.height,
  102. qxl->guest_primary.qxl_stride,
  103. qxl->guest_primary.bytes_pp,
  104. qxl->guest_primary.bits_pp);
  105. if (qxl->guest_primary.qxl_stride > 0) {
  106. qemu_free_displaysurface(vga->ds);
  107. qemu_create_displaysurface_from(qxl->guest_primary.surface.width,
  108. qxl->guest_primary.surface.height,
  109. qxl->guest_primary.bits_pp,
  110. qxl->guest_primary.abs_stride,
  111. qxl->guest_primary.data);
  112. } else {
  113. qemu_resize_displaysurface(vga->ds,
  114. qxl->guest_primary.surface.width,
  115. qxl->guest_primary.surface.height);
  116. }
  117. dpy_resize(vga->ds);
  118. }
  119. for (i = 0; i < qxl->num_dirty_rects; i++) {
  120. if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
  121. break;
  122. }
  123. qxl_blit(qxl, qxl->dirty+i);
  124. dpy_update(vga->ds,
  125. qxl->dirty[i].left, qxl->dirty[i].top,
  126. qxl->dirty[i].right - qxl->dirty[i].left,
  127. qxl->dirty[i].bottom - qxl->dirty[i].top);
  128. }
  129. qxl->num_dirty_rects = 0;
  130. }
  131. /*
  132. * use ssd.lock to protect render_update_cookie_num.
  133. * qxl_render_update is called by io thread or vcpu thread, and the completion
  134. * callbacks are called by spice_server thread, defering to bh called from the
  135. * io thread.
  136. */
  137. void qxl_render_update(PCIQXLDevice *qxl)
  138. {
  139. QXLCookie *cookie;
  140. qemu_mutex_lock(&qxl->ssd.lock);
  141. if (!runstate_is_running() || !qxl->guest_primary.commands) {
  142. qxl_render_update_area_unlocked(qxl);
  143. qemu_mutex_unlock(&qxl->ssd.lock);
  144. return;
  145. }
  146. qxl->guest_primary.commands = 0;
  147. qxl->render_update_cookie_num++;
  148. qemu_mutex_unlock(&qxl->ssd.lock);
  149. cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
  150. 0);
  151. qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
  152. qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
  153. 0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
  154. }
  155. void qxl_render_update_area_bh(void *opaque)
  156. {
  157. PCIQXLDevice *qxl = opaque;
  158. qemu_mutex_lock(&qxl->ssd.lock);
  159. qxl_render_update_area_unlocked(qxl);
  160. qemu_mutex_unlock(&qxl->ssd.lock);
  161. }
  162. void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
  163. {
  164. qemu_mutex_lock(&qxl->ssd.lock);
  165. trace_qxl_render_update_area_done(cookie);
  166. qemu_bh_schedule(qxl->update_area_bh);
  167. qxl->render_update_cookie_num--;
  168. qemu_mutex_unlock(&qxl->ssd.lock);
  169. g_free(cookie);
  170. }
  171. static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
  172. {
  173. QEMUCursor *c;
  174. uint8_t *image, *mask;
  175. size_t size;
  176. c = cursor_alloc(cursor->header.width, cursor->header.height);
  177. c->hot_x = cursor->header.hot_spot_x;
  178. c->hot_y = cursor->header.hot_spot_y;
  179. switch (cursor->header.type) {
  180. case SPICE_CURSOR_TYPE_ALPHA:
  181. size = cursor->header.width * cursor->header.height * sizeof(uint32_t);
  182. memcpy(c->data, cursor->chunk.data, size);
  183. if (qxl->debug > 2) {
  184. cursor_print_ascii_art(c, "qxl/alpha");
  185. }
  186. break;
  187. case SPICE_CURSOR_TYPE_MONO:
  188. mask = cursor->chunk.data;
  189. image = mask + cursor_get_mono_bpl(c) * c->width;
  190. cursor_set_mono(c, 0xffffff, 0x000000, image, 1, mask);
  191. if (qxl->debug > 2) {
  192. cursor_print_ascii_art(c, "qxl/mono");
  193. }
  194. break;
  195. default:
  196. fprintf(stderr, "%s: not implemented: type %d\n",
  197. __FUNCTION__, cursor->header.type);
  198. goto fail;
  199. }
  200. return c;
  201. fail:
  202. cursor_put(c);
  203. return NULL;
  204. }
  205. /* called from spice server thread context only */
  206. int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
  207. {
  208. QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
  209. QXLCursor *cursor;
  210. QEMUCursor *c;
  211. if (!cmd) {
  212. return 1;
  213. }
  214. if (!qxl->ssd.ds->mouse_set || !qxl->ssd.ds->cursor_define) {
  215. return 0;
  216. }
  217. if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
  218. fprintf(stderr, "%s", __FUNCTION__);
  219. qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
  220. fprintf(stderr, "\n");
  221. }
  222. switch (cmd->type) {
  223. case QXL_CURSOR_SET:
  224. cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
  225. if (!cursor) {
  226. return 1;
  227. }
  228. if (cursor->chunk.data_size != cursor->data_size) {
  229. fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__);
  230. return 1;
  231. }
  232. c = qxl_cursor(qxl, cursor);
  233. if (c == NULL) {
  234. c = cursor_builtin_left_ptr();
  235. }
  236. qemu_mutex_lock(&qxl->ssd.lock);
  237. if (qxl->ssd.cursor) {
  238. cursor_put(qxl->ssd.cursor);
  239. }
  240. qxl->ssd.cursor = c;
  241. qxl->ssd.mouse_x = cmd->u.set.position.x;
  242. qxl->ssd.mouse_y = cmd->u.set.position.y;
  243. qemu_mutex_unlock(&qxl->ssd.lock);
  244. break;
  245. case QXL_CURSOR_MOVE:
  246. qemu_mutex_lock(&qxl->ssd.lock);
  247. qxl->ssd.mouse_x = cmd->u.position.x;
  248. qxl->ssd.mouse_y = cmd->u.position.y;
  249. qemu_mutex_unlock(&qxl->ssd.lock);
  250. break;
  251. }
  252. return 0;
  253. }