qxl-render.c 7.3 KB

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