qxl_draw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 "qxl_drv.h"
  23. #include "qxl_object.h"
  24. /* returns a pointer to the already allocated qxl_rect array inside
  25. * the qxl_clip_rects. This is *not* the same as the memory allocated
  26. * on the device, it is offset to qxl_clip_rects.chunk.data */
  27. static struct qxl_rect *drawable_set_clipping(struct qxl_device *qdev,
  28. struct qxl_drawable *drawable,
  29. unsigned num_clips,
  30. struct qxl_bo **clips_bo,
  31. struct qxl_release *release)
  32. {
  33. struct qxl_clip_rects *dev_clips;
  34. int ret;
  35. int size = sizeof(*dev_clips) + sizeof(struct qxl_rect) * num_clips;
  36. ret = qxl_alloc_bo_reserved(qdev, size, clips_bo);
  37. if (ret)
  38. return NULL;
  39. ret = qxl_bo_kmap(*clips_bo, (void **)&dev_clips);
  40. if (ret) {
  41. qxl_bo_unref(clips_bo);
  42. return NULL;
  43. }
  44. dev_clips->num_rects = num_clips;
  45. dev_clips->chunk.next_chunk = 0;
  46. dev_clips->chunk.prev_chunk = 0;
  47. dev_clips->chunk.data_size = sizeof(struct qxl_rect) * num_clips;
  48. return (struct qxl_rect *)dev_clips->chunk.data;
  49. }
  50. static int
  51. make_drawable(struct qxl_device *qdev, int surface, uint8_t type,
  52. const struct qxl_rect *rect,
  53. struct qxl_release **release)
  54. {
  55. struct qxl_drawable *drawable;
  56. int i, ret;
  57. ret = qxl_alloc_release_reserved(qdev, sizeof(*drawable),
  58. QXL_RELEASE_DRAWABLE, release,
  59. NULL);
  60. if (ret)
  61. return ret;
  62. drawable = (struct qxl_drawable *)qxl_release_map(qdev, *release);
  63. drawable->type = type;
  64. drawable->surface_id = surface; /* Only primary for now */
  65. drawable->effect = QXL_EFFECT_OPAQUE;
  66. drawable->self_bitmap = 0;
  67. drawable->self_bitmap_area.top = 0;
  68. drawable->self_bitmap_area.left = 0;
  69. drawable->self_bitmap_area.bottom = 0;
  70. drawable->self_bitmap_area.right = 0;
  71. /* FIXME: add clipping */
  72. drawable->clip.type = SPICE_CLIP_TYPE_NONE;
  73. /*
  74. * surfaces_dest[i] should apparently be filled out with the
  75. * surfaces that we depend on, and surface_rects should be
  76. * filled with the rectangles of those surfaces that we
  77. * are going to use.
  78. */
  79. for (i = 0; i < 3; ++i)
  80. drawable->surfaces_dest[i] = -1;
  81. if (rect)
  82. drawable->bbox = *rect;
  83. drawable->mm_time = qdev->rom->mm_clock;
  84. qxl_release_unmap(qdev, *release, &drawable->release_info);
  85. return 0;
  86. }
  87. static int qxl_palette_create_1bit(struct qxl_bo **palette_bo,
  88. const struct qxl_fb_image *qxl_fb_image)
  89. {
  90. struct qxl_device *qdev = qxl_fb_image->qdev;
  91. const struct fb_image *fb_image = &qxl_fb_image->fb_image;
  92. uint32_t visual = qxl_fb_image->visual;
  93. const uint32_t *pseudo_palette = qxl_fb_image->pseudo_palette;
  94. struct qxl_palette *pal;
  95. int ret;
  96. uint32_t fgcolor, bgcolor;
  97. static uint64_t unique; /* we make no attempt to actually set this
  98. * correctly globaly, since that would require
  99. * tracking all of our palettes. */
  100. ret = qxl_alloc_bo_reserved(qdev,
  101. sizeof(struct qxl_palette) + sizeof(uint32_t) * 2,
  102. palette_bo);
  103. ret = qxl_bo_kmap(*palette_bo, (void **)&pal);
  104. pal->num_ents = 2;
  105. pal->unique = unique++;
  106. if (visual == FB_VISUAL_TRUECOLOR || visual == FB_VISUAL_DIRECTCOLOR) {
  107. /* NB: this is the only used branch currently. */
  108. fgcolor = pseudo_palette[fb_image->fg_color];
  109. bgcolor = pseudo_palette[fb_image->bg_color];
  110. } else {
  111. fgcolor = fb_image->fg_color;
  112. bgcolor = fb_image->bg_color;
  113. }
  114. pal->ents[0] = bgcolor;
  115. pal->ents[1] = fgcolor;
  116. qxl_bo_kunmap(*palette_bo);
  117. return 0;
  118. }
  119. void qxl_draw_opaque_fb(const struct qxl_fb_image *qxl_fb_image,
  120. int stride /* filled in if 0 */)
  121. {
  122. struct qxl_device *qdev = qxl_fb_image->qdev;
  123. struct qxl_drawable *drawable;
  124. struct qxl_rect rect;
  125. const struct fb_image *fb_image = &qxl_fb_image->fb_image;
  126. int x = fb_image->dx;
  127. int y = fb_image->dy;
  128. int width = fb_image->width;
  129. int height = fb_image->height;
  130. const char *src = fb_image->data;
  131. int depth = fb_image->depth;
  132. struct qxl_release *release;
  133. struct qxl_bo *image_bo;
  134. struct qxl_image *image;
  135. int ret;
  136. if (stride == 0)
  137. stride = depth * width / 8;
  138. rect.left = x;
  139. rect.right = x + width;
  140. rect.top = y;
  141. rect.bottom = y + height;
  142. ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &rect, &release);
  143. if (ret)
  144. return;
  145. ret = qxl_image_create(qdev, release, &image_bo,
  146. (const uint8_t *)src, 0, 0,
  147. width, height, depth, stride);
  148. if (ret) {
  149. qxl_release_unreserve(qdev, release);
  150. qxl_release_free(qdev, release);
  151. return;
  152. }
  153. if (depth == 1) {
  154. struct qxl_bo *palette_bo;
  155. void *ptr;
  156. ret = qxl_palette_create_1bit(&palette_bo, qxl_fb_image);
  157. qxl_release_add_res(qdev, release, palette_bo);
  158. ptr = qxl_bo_kmap_atomic_page(qdev, image_bo, 0);
  159. image = ptr;
  160. image->u.bitmap.palette =
  161. qxl_bo_physical_address(qdev, palette_bo, 0);
  162. qxl_bo_kunmap_atomic_page(qdev, image_bo, ptr);
  163. qxl_bo_unreserve(palette_bo);
  164. qxl_bo_unref(&palette_bo);
  165. }
  166. drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
  167. drawable->u.copy.src_area.top = 0;
  168. drawable->u.copy.src_area.bottom = height;
  169. drawable->u.copy.src_area.left = 0;
  170. drawable->u.copy.src_area.right = width;
  171. drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
  172. drawable->u.copy.scale_mode = 0;
  173. drawable->u.copy.mask.flags = 0;
  174. drawable->u.copy.mask.pos.x = 0;
  175. drawable->u.copy.mask.pos.y = 0;
  176. drawable->u.copy.mask.bitmap = 0;
  177. drawable->u.copy.src_bitmap =
  178. qxl_bo_physical_address(qdev, image_bo, 0);
  179. qxl_release_unmap(qdev, release, &drawable->release_info);
  180. qxl_release_add_res(qdev, release, image_bo);
  181. qxl_bo_unreserve(image_bo);
  182. qxl_bo_unref(&image_bo);
  183. qxl_fence_releaseable(qdev, release);
  184. qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
  185. qxl_release_unreserve(qdev, release);
  186. }
  187. /* push a draw command using the given clipping rectangles as
  188. * the sources from the shadow framebuffer.
  189. *
  190. * Right now implementing with a single draw and a clip list. Clip
  191. * lists are known to be a problem performance wise, this can be solved
  192. * by treating them differently in the server.
  193. */
  194. void qxl_draw_dirty_fb(struct qxl_device *qdev,
  195. struct qxl_framebuffer *qxl_fb,
  196. struct qxl_bo *bo,
  197. unsigned flags, unsigned color,
  198. struct drm_clip_rect *clips,
  199. unsigned num_clips, int inc)
  200. {
  201. /*
  202. * TODO: if flags & DRM_MODE_FB_DIRTY_ANNOTATE_FILL then we should
  203. * send a fill command instead, much cheaper.
  204. *
  205. * See include/drm/drm_mode.h
  206. */
  207. struct drm_clip_rect *clips_ptr;
  208. int i;
  209. int left, right, top, bottom;
  210. int width, height;
  211. struct qxl_drawable *drawable;
  212. struct qxl_rect drawable_rect;
  213. struct qxl_rect *rects;
  214. int stride = qxl_fb->base.pitches[0];
  215. /* depth is not actually interesting, we don't mask with it */
  216. int depth = qxl_fb->base.bits_per_pixel;
  217. uint8_t *surface_base;
  218. struct qxl_release *release;
  219. struct qxl_bo *image_bo;
  220. struct qxl_bo *clips_bo;
  221. int ret;
  222. left = clips->x1;
  223. right = clips->x2;
  224. top = clips->y1;
  225. bottom = clips->y2;
  226. /* skip the first clip rect */
  227. for (i = 1, clips_ptr = clips + inc;
  228. i < num_clips; i++, clips_ptr += inc) {
  229. left = min_t(int, left, (int)clips_ptr->x1);
  230. right = max_t(int, right, (int)clips_ptr->x2);
  231. top = min_t(int, top, (int)clips_ptr->y1);
  232. bottom = max_t(int, bottom, (int)clips_ptr->y2);
  233. }
  234. width = right - left;
  235. height = bottom - top;
  236. drawable_rect.left = left;
  237. drawable_rect.right = right;
  238. drawable_rect.top = top;
  239. drawable_rect.bottom = bottom;
  240. ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &drawable_rect,
  241. &release);
  242. if (ret)
  243. return;
  244. ret = qxl_bo_kmap(bo, (void **)&surface_base);
  245. if (ret)
  246. goto out_unref;
  247. ret = qxl_image_create(qdev, release, &image_bo, surface_base,
  248. left, top, width, height, depth, stride);
  249. qxl_bo_kunmap(bo);
  250. if (ret)
  251. goto out_unref;
  252. rects = drawable_set_clipping(qdev, drawable, num_clips, &clips_bo, release);
  253. if (!rects) {
  254. qxl_bo_unref(&image_bo);
  255. goto out_unref;
  256. }
  257. drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
  258. drawable->clip.type = SPICE_CLIP_TYPE_RECTS;
  259. drawable->clip.data = qxl_bo_physical_address(qdev,
  260. clips_bo, 0);
  261. qxl_release_add_res(qdev, release, clips_bo);
  262. drawable->u.copy.src_area.top = 0;
  263. drawable->u.copy.src_area.bottom = height;
  264. drawable->u.copy.src_area.left = 0;
  265. drawable->u.copy.src_area.right = width;
  266. drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
  267. drawable->u.copy.scale_mode = 0;
  268. drawable->u.copy.mask.flags = 0;
  269. drawable->u.copy.mask.pos.x = 0;
  270. drawable->u.copy.mask.pos.y = 0;
  271. drawable->u.copy.mask.bitmap = 0;
  272. drawable->u.copy.src_bitmap = qxl_bo_physical_address(qdev, image_bo, 0);
  273. qxl_release_unmap(qdev, release, &drawable->release_info);
  274. qxl_release_add_res(qdev, release, image_bo);
  275. qxl_bo_unreserve(image_bo);
  276. qxl_bo_unref(&image_bo);
  277. clips_ptr = clips;
  278. for (i = 0; i < num_clips; i++, clips_ptr += inc) {
  279. rects[i].left = clips_ptr->x1;
  280. rects[i].right = clips_ptr->x2;
  281. rects[i].top = clips_ptr->y1;
  282. rects[i].bottom = clips_ptr->y2;
  283. }
  284. qxl_bo_kunmap(clips_bo);
  285. qxl_bo_unreserve(clips_bo);
  286. qxl_bo_unref(&clips_bo);
  287. qxl_fence_releaseable(qdev, release);
  288. qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
  289. qxl_release_unreserve(qdev, release);
  290. return;
  291. out_unref:
  292. qxl_release_unreserve(qdev, release);
  293. qxl_release_free(qdev, release);
  294. }
  295. void qxl_draw_copyarea(struct qxl_device *qdev,
  296. u32 width, u32 height,
  297. u32 sx, u32 sy,
  298. u32 dx, u32 dy)
  299. {
  300. struct qxl_drawable *drawable;
  301. struct qxl_rect rect;
  302. struct qxl_release *release;
  303. int ret;
  304. rect.left = dx;
  305. rect.top = dy;
  306. rect.right = dx + width;
  307. rect.bottom = dy + height;
  308. ret = make_drawable(qdev, 0, QXL_COPY_BITS, &rect, &release);
  309. if (ret)
  310. return;
  311. drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
  312. drawable->u.copy_bits.src_pos.x = sx;
  313. drawable->u.copy_bits.src_pos.y = sy;
  314. qxl_release_unmap(qdev, release, &drawable->release_info);
  315. qxl_fence_releaseable(qdev, release);
  316. qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
  317. qxl_release_unreserve(qdev, release);
  318. }
  319. void qxl_draw_fill(struct qxl_draw_fill *qxl_draw_fill_rec)
  320. {
  321. struct qxl_device *qdev = qxl_draw_fill_rec->qdev;
  322. struct qxl_rect rect = qxl_draw_fill_rec->rect;
  323. uint32_t color = qxl_draw_fill_rec->color;
  324. uint16_t rop = qxl_draw_fill_rec->rop;
  325. struct qxl_drawable *drawable;
  326. struct qxl_release *release;
  327. int ret;
  328. ret = make_drawable(qdev, 0, QXL_DRAW_FILL, &rect, &release);
  329. if (ret)
  330. return;
  331. drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
  332. drawable->u.fill.brush.type = SPICE_BRUSH_TYPE_SOLID;
  333. drawable->u.fill.brush.u.color = color;
  334. drawable->u.fill.rop_descriptor = rop;
  335. drawable->u.fill.mask.flags = 0;
  336. drawable->u.fill.mask.pos.x = 0;
  337. drawable->u.fill.mask.pos.y = 0;
  338. drawable->u.fill.mask.bitmap = 0;
  339. qxl_release_unmap(qdev, release, &drawable->release_info);
  340. qxl_fence_releaseable(qdev, release);
  341. qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
  342. qxl_release_unreserve(qdev, release);
  343. }