qxl_draw.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright 2011 Red Hat, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * on the rights to use, copy, modify, merge, publish, distribute, sub
  8. * license, and/or sell copies of the Software, and to permit persons to whom
  9. * the Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <linux/iosys-map.h>
  23. #include <drm/drm_fourcc.h>
  24. #include <drm/drm_framebuffer.h>
  25. #include "qxl_drv.h"
  26. #include "qxl_object.h"
  27. static int alloc_clips(struct qxl_device *qdev,
  28. struct qxl_release *release,
  29. unsigned int num_clips,
  30. struct qxl_bo **clips_bo)
  31. {
  32. int size = sizeof(struct qxl_clip_rects) + sizeof(struct qxl_rect) * num_clips;
  33. return qxl_alloc_bo_reserved(qdev, release, size, clips_bo);
  34. }
  35. /* returns a pointer to the already allocated qxl_rect array inside
  36. * the qxl_clip_rects. This is *not* the same as the memory allocated
  37. * on the device, it is offset to qxl_clip_rects.chunk.data */
  38. static struct qxl_rect *drawable_set_clipping(struct qxl_device *qdev,
  39. unsigned int num_clips,
  40. struct qxl_bo *clips_bo)
  41. {
  42. struct iosys_map map;
  43. struct qxl_clip_rects *dev_clips;
  44. int ret;
  45. ret = qxl_bo_vmap_locked(clips_bo, &map);
  46. if (ret)
  47. return NULL;
  48. dev_clips = map.vaddr; /* TODO: Use mapping abstraction properly */
  49. dev_clips->num_rects = num_clips;
  50. dev_clips->chunk.next_chunk = 0;
  51. dev_clips->chunk.prev_chunk = 0;
  52. dev_clips->chunk.data_size = sizeof(struct qxl_rect) * num_clips;
  53. return (struct qxl_rect *)dev_clips->chunk.data;
  54. }
  55. static int
  56. alloc_drawable(struct qxl_device *qdev, struct qxl_release **release)
  57. {
  58. return qxl_alloc_release_reserved(qdev, sizeof(struct qxl_drawable),
  59. QXL_RELEASE_DRAWABLE, release, NULL);
  60. }
  61. static void
  62. free_drawable(struct qxl_device *qdev, struct qxl_release *release)
  63. {
  64. qxl_release_free(qdev, release);
  65. }
  66. /* release needs to be reserved at this point */
  67. static int
  68. make_drawable(struct qxl_device *qdev, int surface, uint8_t type,
  69. const struct qxl_rect *rect,
  70. struct qxl_release *release)
  71. {
  72. struct qxl_drawable *drawable;
  73. int i;
  74. drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
  75. if (!drawable)
  76. return -ENOMEM;
  77. drawable->type = type;
  78. drawable->surface_id = surface; /* Only primary for now */
  79. drawable->effect = QXL_EFFECT_OPAQUE;
  80. drawable->self_bitmap = 0;
  81. drawable->self_bitmap_area.top = 0;
  82. drawable->self_bitmap_area.left = 0;
  83. drawable->self_bitmap_area.bottom = 0;
  84. drawable->self_bitmap_area.right = 0;
  85. /* FIXME: add clipping */
  86. drawable->clip.type = SPICE_CLIP_TYPE_NONE;
  87. /*
  88. * surfaces_dest[i] should apparently be filled out with the
  89. * surfaces that we depend on, and surface_rects should be
  90. * filled with the rectangles of those surfaces that we
  91. * are going to use.
  92. */
  93. for (i = 0; i < 3; ++i)
  94. drawable->surfaces_dest[i] = -1;
  95. if (rect)
  96. drawable->bbox = *rect;
  97. drawable->mm_time = qdev->rom->mm_clock;
  98. qxl_release_unmap(qdev, release, &drawable->release_info);
  99. return 0;
  100. }
  101. /* push a draw command using the given clipping rectangles as
  102. * the sources from the shadow framebuffer.
  103. *
  104. * Right now implementing with a single draw and a clip list. Clip
  105. * lists are known to be a problem performance wise, this can be solved
  106. * by treating them differently in the server.
  107. */
  108. void qxl_draw_dirty_fb(struct qxl_device *qdev,
  109. struct drm_framebuffer *fb,
  110. struct qxl_bo *bo,
  111. unsigned int flags, unsigned int color,
  112. struct drm_clip_rect *clips,
  113. unsigned int num_clips, int inc,
  114. uint32_t dumb_shadow_offset)
  115. {
  116. /*
  117. * TODO: if flags & DRM_MODE_FB_DIRTY_ANNOTATE_FILL then we should
  118. * send a fill command instead, much cheaper.
  119. *
  120. * See include/drm/drm_mode.h
  121. */
  122. struct drm_clip_rect *clips_ptr;
  123. int i;
  124. int left, right, top, bottom;
  125. int width, height;
  126. struct qxl_drawable *drawable;
  127. struct qxl_rect drawable_rect;
  128. struct qxl_rect *rects;
  129. int stride = fb->pitches[0];
  130. /* depth is not actually interesting, we don't mask with it */
  131. int depth = fb->format->cpp[0] * 8;
  132. struct iosys_map surface_map;
  133. uint8_t *surface_base;
  134. struct qxl_release *release;
  135. struct qxl_bo *clips_bo;
  136. struct qxl_drm_image *dimage;
  137. int ret;
  138. ret = alloc_drawable(qdev, &release);
  139. if (ret)
  140. return;
  141. clips->x1 += dumb_shadow_offset;
  142. clips->x2 += dumb_shadow_offset;
  143. left = clips->x1;
  144. right = clips->x2;
  145. top = clips->y1;
  146. bottom = clips->y2;
  147. /* skip the first clip rect */
  148. for (i = 1, clips_ptr = clips + inc;
  149. i < num_clips; i++, clips_ptr += inc) {
  150. left = min_t(int, left, (int)clips_ptr->x1);
  151. right = max_t(int, right, (int)clips_ptr->x2);
  152. top = min_t(int, top, (int)clips_ptr->y1);
  153. bottom = max_t(int, bottom, (int)clips_ptr->y2);
  154. }
  155. width = right - left;
  156. height = bottom - top;
  157. ret = alloc_clips(qdev, release, num_clips, &clips_bo);
  158. if (ret)
  159. goto out_free_drawable;
  160. ret = qxl_image_alloc_objects(qdev, release,
  161. &dimage,
  162. height, stride);
  163. if (ret)
  164. goto out_free_clips;
  165. /* do a reservation run over all the objects we just allocated */
  166. ret = qxl_release_reserve_list(release, true);
  167. if (ret)
  168. goto out_free_image;
  169. drawable_rect.left = left;
  170. drawable_rect.right = right;
  171. drawable_rect.top = top;
  172. drawable_rect.bottom = bottom;
  173. ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &drawable_rect,
  174. release);
  175. if (ret)
  176. goto out_release_backoff;
  177. ret = qxl_bo_vmap_locked(bo, &surface_map);
  178. if (ret)
  179. goto out_release_backoff;
  180. surface_base = surface_map.vaddr; /* TODO: Use mapping abstraction properly */
  181. ret = qxl_image_init(qdev, release, dimage, surface_base,
  182. left - dumb_shadow_offset,
  183. top, width, height, depth, stride);
  184. qxl_bo_vunmap_locked(bo);
  185. if (ret)
  186. goto out_release_backoff;
  187. rects = drawable_set_clipping(qdev, num_clips, clips_bo);
  188. if (!rects) {
  189. ret = -EINVAL;
  190. goto out_release_backoff;
  191. }
  192. drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
  193. drawable->clip.type = SPICE_CLIP_TYPE_RECTS;
  194. drawable->clip.data = qxl_bo_physical_address(qdev,
  195. clips_bo, 0);
  196. drawable->u.copy.src_area.top = 0;
  197. drawable->u.copy.src_area.bottom = height;
  198. drawable->u.copy.src_area.left = 0;
  199. drawable->u.copy.src_area.right = width;
  200. drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
  201. drawable->u.copy.scale_mode = 0;
  202. drawable->u.copy.mask.flags = 0;
  203. drawable->u.copy.mask.pos.x = 0;
  204. drawable->u.copy.mask.pos.y = 0;
  205. drawable->u.copy.mask.bitmap = 0;
  206. drawable->u.copy.src_bitmap = qxl_bo_physical_address(qdev, dimage->bo, 0);
  207. qxl_release_unmap(qdev, release, &drawable->release_info);
  208. clips_ptr = clips;
  209. for (i = 0; i < num_clips; i++, clips_ptr += inc) {
  210. rects[i].left = clips_ptr->x1;
  211. rects[i].right = clips_ptr->x2;
  212. rects[i].top = clips_ptr->y1;
  213. rects[i].bottom = clips_ptr->y2;
  214. }
  215. qxl_bo_vunmap_locked(clips_bo);
  216. qxl_release_fence_buffer_objects(release);
  217. qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
  218. out_release_backoff:
  219. if (ret)
  220. qxl_release_backoff_reserve_list(release);
  221. out_free_image:
  222. qxl_image_free_objects(qdev, dimage);
  223. out_free_clips:
  224. qxl_bo_unref(&clips_bo);
  225. out_free_drawable:
  226. /* only free drawable on error */
  227. if (ret)
  228. free_drawable(qdev, release);
  229. }