qxl-render.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_flip(PCIQXLDevice *qxl, QXLRect *rect)
  23. {
  24. uint8_t *src = qxl->guest_primary.data;
  25. uint8_t *dst = qxl->guest_primary.flipped;
  26. int len, i;
  27. src += (qxl->guest_primary.surface.height - rect->top - 1) *
  28. qxl->guest_primary.abs_stride;
  29. dst += rect->top * qxl->guest_primary.abs_stride;
  30. src += rect->left * qxl->guest_primary.bytes_pp;
  31. dst += rect->left * qxl->guest_primary.bytes_pp;
  32. len = (rect->right - rect->left) * qxl->guest_primary.bytes_pp;
  33. for (i = rect->top; i < rect->bottom; i++) {
  34. memcpy(dst, src, len);
  35. dst += qxl->guest_primary.abs_stride;
  36. src -= qxl->guest_primary.abs_stride;
  37. }
  38. }
  39. void qxl_render_resize(PCIQXLDevice *qxl)
  40. {
  41. QXLSurfaceCreate *sc = &qxl->guest_primary.surface;
  42. qxl->guest_primary.qxl_stride = sc->stride;
  43. qxl->guest_primary.abs_stride = abs(sc->stride);
  44. qxl->guest_primary.resized++;
  45. switch (sc->format) {
  46. case SPICE_SURFACE_FMT_16_555:
  47. qxl->guest_primary.bytes_pp = 2;
  48. qxl->guest_primary.bits_pp = 15;
  49. break;
  50. case SPICE_SURFACE_FMT_16_565:
  51. qxl->guest_primary.bytes_pp = 2;
  52. qxl->guest_primary.bits_pp = 16;
  53. break;
  54. case SPICE_SURFACE_FMT_32_xRGB:
  55. case SPICE_SURFACE_FMT_32_ARGB:
  56. qxl->guest_primary.bytes_pp = 4;
  57. qxl->guest_primary.bits_pp = 32;
  58. break;
  59. default:
  60. fprintf(stderr, "%s: unhandled format: %x\n", __FUNCTION__,
  61. qxl->guest_primary.surface.format);
  62. qxl->guest_primary.bytes_pp = 4;
  63. qxl->guest_primary.bits_pp = 32;
  64. break;
  65. }
  66. }
  67. void qxl_render_update(PCIQXLDevice *qxl)
  68. {
  69. VGACommonState *vga = &qxl->vga;
  70. QXLRect dirty[32], update;
  71. void *ptr;
  72. int i, redraw = 0;
  73. if (!is_buffer_shared(vga->ds->surface)) {
  74. dprint(qxl, 1, "%s: restoring shared displaysurface\n", __func__);
  75. qxl->guest_primary.resized++;
  76. qxl->guest_primary.commands++;
  77. redraw = 1;
  78. }
  79. if (qxl->guest_primary.resized) {
  80. qxl->guest_primary.resized = 0;
  81. if (qxl->guest_primary.flipped) {
  82. g_free(qxl->guest_primary.flipped);
  83. qxl->guest_primary.flipped = NULL;
  84. }
  85. qemu_free_displaysurface(vga->ds);
  86. qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
  87. if (qxl->guest_primary.qxl_stride < 0) {
  88. /* spice surface is upside down -> need extra buffer to flip */
  89. qxl->guest_primary.flipped =
  90. g_malloc(qxl->guest_primary.surface.width *
  91. qxl->guest_primary.abs_stride);
  92. ptr = qxl->guest_primary.flipped;
  93. } else {
  94. ptr = qxl->guest_primary.data;
  95. }
  96. dprint(qxl, 1, "%s: %dx%d, stride %d, bpp %d, depth %d, flip %s\n",
  97. __FUNCTION__,
  98. qxl->guest_primary.surface.width,
  99. qxl->guest_primary.surface.height,
  100. qxl->guest_primary.qxl_stride,
  101. qxl->guest_primary.bytes_pp,
  102. qxl->guest_primary.bits_pp,
  103. qxl->guest_primary.flipped ? "yes" : "no");
  104. vga->ds->surface =
  105. qemu_create_displaysurface_from(qxl->guest_primary.surface.width,
  106. qxl->guest_primary.surface.height,
  107. qxl->guest_primary.bits_pp,
  108. qxl->guest_primary.abs_stride,
  109. ptr);
  110. dpy_resize(vga->ds);
  111. }
  112. if (!qxl->guest_primary.commands) {
  113. return;
  114. }
  115. qxl->guest_primary.commands = 0;
  116. update.left = 0;
  117. update.right = qxl->guest_primary.surface.width;
  118. update.top = 0;
  119. update.bottom = qxl->guest_primary.surface.height;
  120. memset(dirty, 0, sizeof(dirty));
  121. qxl_spice_update_area(qxl, 0, &update,
  122. dirty, ARRAY_SIZE(dirty), 1, QXL_SYNC);
  123. if (redraw) {
  124. memset(dirty, 0, sizeof(dirty));
  125. dirty[0] = update;
  126. }
  127. for (i = 0; i < ARRAY_SIZE(dirty); i++) {
  128. if (qemu_spice_rect_is_empty(dirty+i)) {
  129. break;
  130. }
  131. if (qxl->guest_primary.flipped) {
  132. qxl_flip(qxl, dirty+i);
  133. }
  134. dpy_update(vga->ds,
  135. dirty[i].left, dirty[i].top,
  136. dirty[i].right - dirty[i].left,
  137. dirty[i].bottom - dirty[i].top);
  138. }
  139. }
  140. static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
  141. {
  142. QEMUCursor *c;
  143. uint8_t *image, *mask;
  144. int size;
  145. c = cursor_alloc(cursor->header.width, cursor->header.height);
  146. c->hot_x = cursor->header.hot_spot_x;
  147. c->hot_y = cursor->header.hot_spot_y;
  148. switch (cursor->header.type) {
  149. case SPICE_CURSOR_TYPE_ALPHA:
  150. size = cursor->header.width * cursor->header.height * sizeof(uint32_t);
  151. memcpy(c->data, cursor->chunk.data, size);
  152. if (qxl->debug > 2) {
  153. cursor_print_ascii_art(c, "qxl/alpha");
  154. }
  155. break;
  156. case SPICE_CURSOR_TYPE_MONO:
  157. mask = cursor->chunk.data;
  158. image = mask + cursor_get_mono_bpl(c) * c->width;
  159. cursor_set_mono(c, 0xffffff, 0x000000, image, 1, mask);
  160. if (qxl->debug > 2) {
  161. cursor_print_ascii_art(c, "qxl/mono");
  162. }
  163. break;
  164. default:
  165. fprintf(stderr, "%s: not implemented: type %d\n",
  166. __FUNCTION__, cursor->header.type);
  167. goto fail;
  168. }
  169. return c;
  170. fail:
  171. cursor_put(c);
  172. return NULL;
  173. }
  174. /* called from spice server thread context only */
  175. void qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
  176. {
  177. QXLCursorCmd *cmd = qxl_phys2virt(qxl, ext->cmd.data, ext->group_id);
  178. QXLCursor *cursor;
  179. QEMUCursor *c;
  180. if (!qxl->ssd.ds->mouse_set || !qxl->ssd.ds->cursor_define) {
  181. return;
  182. }
  183. if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
  184. fprintf(stderr, "%s", __FUNCTION__);
  185. qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
  186. fprintf(stderr, "\n");
  187. }
  188. switch (cmd->type) {
  189. case QXL_CURSOR_SET:
  190. cursor = qxl_phys2virt(qxl, cmd->u.set.shape, ext->group_id);
  191. if (cursor->chunk.data_size != cursor->data_size) {
  192. fprintf(stderr, "%s: multiple chunks\n", __FUNCTION__);
  193. return;
  194. }
  195. c = qxl_cursor(qxl, cursor);
  196. if (c == NULL) {
  197. c = cursor_builtin_left_ptr();
  198. }
  199. qemu_mutex_lock(&qxl->ssd.lock);
  200. if (qxl->ssd.cursor) {
  201. cursor_put(qxl->ssd.cursor);
  202. }
  203. qxl->ssd.cursor = c;
  204. qxl->ssd.mouse_x = cmd->u.set.position.x;
  205. qxl->ssd.mouse_y = cmd->u.set.position.y;
  206. qemu_mutex_unlock(&qxl->ssd.lock);
  207. break;
  208. case QXL_CURSOR_MOVE:
  209. qemu_mutex_lock(&qxl->ssd.lock);
  210. qxl->ssd.mouse_x = cmd->u.position.x;
  211. qxl->ssd.mouse_y = cmd->u.position.y;
  212. qemu_mutex_unlock(&qxl->ssd.lock);
  213. break;
  214. }
  215. }