qxl-render.c 8.8 KB

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