qxl_draw.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. static int alloc_clips(struct qxl_device *qdev,
  25. struct qxl_release *release,
  26. unsigned num_clips,
  27. struct qxl_bo **clips_bo)
  28. {
  29. int size = sizeof(struct qxl_clip_rects) + sizeof(struct qxl_rect) * num_clips;
  30. return qxl_alloc_bo_reserved(qdev, release, size, clips_bo);
  31. }
  32. /* returns a pointer to the already allocated qxl_rect array inside
  33. * the qxl_clip_rects. This is *not* the same as the memory allocated
  34. * on the device, it is offset to qxl_clip_rects.chunk.data */
  35. static struct qxl_rect *drawable_set_clipping(struct qxl_device *qdev,
  36. struct qxl_drawable *drawable,
  37. unsigned num_clips,
  38. struct qxl_bo *clips_bo)
  39. {
  40. struct qxl_clip_rects *dev_clips;
  41. int ret;
  42. ret = qxl_bo_kmap(clips_bo, (void **)&dev_clips);
  43. if (ret) {
  44. return NULL;
  45. }
  46. dev_clips->num_rects = num_clips;
  47. dev_clips->chunk.next_chunk = 0;
  48. dev_clips->chunk.prev_chunk = 0;
  49. dev_clips->chunk.data_size = sizeof(struct qxl_rect) * num_clips;
  50. return (struct qxl_rect *)dev_clips->chunk.data;
  51. }
  52. static int
  53. alloc_drawable(struct qxl_device *qdev, struct qxl_release **release)
  54. {
  55. int ret;
  56. ret = qxl_alloc_release_reserved(qdev, sizeof(struct qxl_drawable),
  57. QXL_RELEASE_DRAWABLE, release,
  58. NULL);
  59. return ret;
  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. static int alloc_palette_object(struct qxl_device *qdev,
  102. struct qxl_release *release,
  103. struct qxl_bo **palette_bo)
  104. {
  105. return qxl_alloc_bo_reserved(qdev, release,
  106. sizeof(struct qxl_palette) + sizeof(uint32_t) * 2,
  107. palette_bo);
  108. }
  109. static int qxl_palette_create_1bit(struct qxl_bo *palette_bo,
  110. struct qxl_release *release,
  111. const struct qxl_fb_image *qxl_fb_image)
  112. {
  113. const struct fb_image *fb_image = &qxl_fb_image->fb_image;
  114. uint32_t visual = qxl_fb_image->visual;
  115. const uint32_t *pseudo_palette = qxl_fb_image->pseudo_palette;
  116. struct qxl_palette *pal;
  117. int ret;
  118. uint32_t fgcolor, bgcolor;
  119. static uint64_t unique; /* we make no attempt to actually set this
  120. * correctly globaly, since that would require
  121. * tracking all of our palettes. */
  122. ret = qxl_bo_kmap(palette_bo, (void **)&pal);
  123. if (ret)
  124. return ret;
  125. pal->num_ents = 2;
  126. pal->unique = unique++;
  127. if (visual == FB_VISUAL_TRUECOLOR || visual == FB_VISUAL_DIRECTCOLOR) {
  128. /* NB: this is the only used branch currently. */
  129. fgcolor = pseudo_palette[fb_image->fg_color];
  130. bgcolor = pseudo_palette[fb_image->bg_color];
  131. } else {
  132. fgcolor = fb_image->fg_color;
  133. bgcolor = fb_image->bg_color;
  134. }
  135. pal->ents[0] = bgcolor;
  136. pal->ents[1] = fgcolor;
  137. qxl_bo_kunmap(palette_bo);
  138. return 0;
  139. }
  140. void qxl_draw_opaque_fb(const struct qxl_fb_image *qxl_fb_image,
  141. int stride /* filled in if 0 */)
  142. {
  143. struct qxl_device *qdev = qxl_fb_image->qdev;
  144. struct qxl_drawable *drawable;
  145. struct qxl_rect rect;
  146. const struct fb_image *fb_image = &qxl_fb_image->fb_image;
  147. int x = fb_image->dx;
  148. int y = fb_image->dy;
  149. int width = fb_image->width;
  150. int height = fb_image->height;
  151. const char *src = fb_image->data;
  152. int depth = fb_image->depth;
  153. struct qxl_release *release;
  154. struct qxl_image *image;
  155. int ret;
  156. struct qxl_drm_image *dimage;
  157. struct qxl_bo *palette_bo = NULL;
  158. if (stride == 0)
  159. stride = depth * width / 8;
  160. ret = alloc_drawable(qdev, &release);
  161. if (ret)
  162. return;
  163. ret = qxl_image_alloc_objects(qdev, release,
  164. &dimage,
  165. height, stride);
  166. if (ret)
  167. goto out_free_drawable;
  168. if (depth == 1) {
  169. ret = alloc_palette_object(qdev, release, &palette_bo);
  170. if (ret)
  171. goto out_free_image;
  172. }
  173. /* do a reservation run over all the objects we just allocated */
  174. ret = qxl_release_reserve_list(release, true);
  175. if (ret)
  176. goto out_free_palette;
  177. rect.left = x;
  178. rect.right = x + width;
  179. rect.top = y;
  180. rect.bottom = y + height;
  181. ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &rect, release);
  182. if (ret) {
  183. qxl_release_backoff_reserve_list(release);
  184. goto out_free_palette;
  185. }
  186. ret = qxl_image_init(qdev, release, dimage,
  187. (const uint8_t *)src, 0, 0,
  188. width, height, depth, stride);
  189. if (ret) {
  190. qxl_release_backoff_reserve_list(release);
  191. qxl_release_free(qdev, release);
  192. return;
  193. }
  194. if (depth == 1) {
  195. void *ptr;
  196. ret = qxl_palette_create_1bit(palette_bo, release, qxl_fb_image);
  197. ptr = qxl_bo_kmap_atomic_page(qdev, dimage->bo, 0);
  198. image = ptr;
  199. image->u.bitmap.palette =
  200. qxl_bo_physical_address(qdev, palette_bo, 0);
  201. qxl_bo_kunmap_atomic_page(qdev, dimage->bo, ptr);
  202. }
  203. drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
  204. drawable->u.copy.src_area.top = 0;
  205. drawable->u.copy.src_area.bottom = height;
  206. drawable->u.copy.src_area.left = 0;
  207. drawable->u.copy.src_area.right = width;
  208. drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
  209. drawable->u.copy.scale_mode = 0;
  210. drawable->u.copy.mask.flags = 0;
  211. drawable->u.copy.mask.pos.x = 0;
  212. drawable->u.copy.mask.pos.y = 0;
  213. drawable->u.copy.mask.bitmap = 0;
  214. drawable->u.copy.src_bitmap =
  215. qxl_bo_physical_address(qdev, dimage->bo, 0);
  216. qxl_release_unmap(qdev, release, &drawable->release_info);
  217. qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
  218. qxl_release_fence_buffer_objects(release);
  219. out_free_palette:
  220. if (palette_bo)
  221. qxl_bo_unref(&palette_bo);
  222. out_free_image:
  223. qxl_image_free_objects(qdev, dimage);
  224. out_free_drawable:
  225. if (ret)
  226. free_drawable(qdev, release);
  227. }
  228. /* push a draw command using the given clipping rectangles as
  229. * the sources from the shadow framebuffer.
  230. *
  231. * Right now implementing with a single draw and a clip list. Clip
  232. * lists are known to be a problem performance wise, this can be solved
  233. * by treating them differently in the server.
  234. */
  235. void qxl_draw_dirty_fb(struct qxl_device *qdev,
  236. struct qxl_framebuffer *qxl_fb,
  237. struct qxl_bo *bo,
  238. unsigned flags, unsigned color,
  239. struct drm_clip_rect *clips,
  240. unsigned num_clips, int inc)
  241. {
  242. /*
  243. * TODO: if flags & DRM_MODE_FB_DIRTY_ANNOTATE_FILL then we should
  244. * send a fill command instead, much cheaper.
  245. *
  246. * See include/drm/drm_mode.h
  247. */
  248. struct drm_clip_rect *clips_ptr;
  249. int i;
  250. int left, right, top, bottom;
  251. int width, height;
  252. struct qxl_drawable *drawable;
  253. struct qxl_rect drawable_rect;
  254. struct qxl_rect *rects;
  255. int stride = qxl_fb->base.pitches[0];
  256. /* depth is not actually interesting, we don't mask with it */
  257. int depth = qxl_fb->base.bits_per_pixel;
  258. uint8_t *surface_base;
  259. struct qxl_release *release;
  260. struct qxl_bo *clips_bo;
  261. struct qxl_drm_image *dimage;
  262. int ret;
  263. ret = alloc_drawable(qdev, &release);
  264. if (ret)
  265. return;
  266. left = clips->x1;
  267. right = clips->x2;
  268. top = clips->y1;
  269. bottom = clips->y2;
  270. /* skip the first clip rect */
  271. for (i = 1, clips_ptr = clips + inc;
  272. i < num_clips; i++, clips_ptr += inc) {
  273. left = min_t(int, left, (int)clips_ptr->x1);
  274. right = max_t(int, right, (int)clips_ptr->x2);
  275. top = min_t(int, top, (int)clips_ptr->y1);
  276. bottom = max_t(int, bottom, (int)clips_ptr->y2);
  277. }
  278. width = right - left;
  279. height = bottom - top;
  280. ret = alloc_clips(qdev, release, num_clips, &clips_bo);
  281. if (ret)
  282. goto out_free_drawable;
  283. ret = qxl_image_alloc_objects(qdev, release,
  284. &dimage,
  285. height, stride);
  286. if (ret)
  287. goto out_free_clips;
  288. /* do a reservation run over all the objects we just allocated */
  289. ret = qxl_release_reserve_list(release, true);
  290. if (ret)
  291. goto out_free_image;
  292. drawable_rect.left = left;
  293. drawable_rect.right = right;
  294. drawable_rect.top = top;
  295. drawable_rect.bottom = bottom;
  296. ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &drawable_rect,
  297. release);
  298. if (ret)
  299. goto out_release_backoff;
  300. ret = qxl_bo_kmap(bo, (void **)&surface_base);
  301. if (ret)
  302. goto out_release_backoff;
  303. ret = qxl_image_init(qdev, release, dimage, surface_base,
  304. left, top, width, height, depth, stride);
  305. qxl_bo_kunmap(bo);
  306. if (ret)
  307. goto out_release_backoff;
  308. rects = drawable_set_clipping(qdev, drawable, num_clips, clips_bo);
  309. if (!rects)
  310. goto out_release_backoff;
  311. drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
  312. drawable->clip.type = SPICE_CLIP_TYPE_RECTS;
  313. drawable->clip.data = qxl_bo_physical_address(qdev,
  314. clips_bo, 0);
  315. drawable->u.copy.src_area.top = 0;
  316. drawable->u.copy.src_area.bottom = height;
  317. drawable->u.copy.src_area.left = 0;
  318. drawable->u.copy.src_area.right = width;
  319. drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
  320. drawable->u.copy.scale_mode = 0;
  321. drawable->u.copy.mask.flags = 0;
  322. drawable->u.copy.mask.pos.x = 0;
  323. drawable->u.copy.mask.pos.y = 0;
  324. drawable->u.copy.mask.bitmap = 0;
  325. drawable->u.copy.src_bitmap = qxl_bo_physical_address(qdev, dimage->bo, 0);
  326. qxl_release_unmap(qdev, release, &drawable->release_info);
  327. clips_ptr = clips;
  328. for (i = 0; i < num_clips; i++, clips_ptr += inc) {
  329. rects[i].left = clips_ptr->x1;
  330. rects[i].right = clips_ptr->x2;
  331. rects[i].top = clips_ptr->y1;
  332. rects[i].bottom = clips_ptr->y2;
  333. }
  334. qxl_bo_kunmap(clips_bo);
  335. qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
  336. qxl_release_fence_buffer_objects(release);
  337. out_release_backoff:
  338. if (ret)
  339. qxl_release_backoff_reserve_list(release);
  340. out_free_image:
  341. qxl_image_free_objects(qdev, dimage);
  342. out_free_clips:
  343. qxl_bo_unref(&clips_bo);
  344. out_free_drawable:
  345. /* only free drawable on error */
  346. if (ret)
  347. free_drawable(qdev, release);
  348. }
  349. void qxl_draw_copyarea(struct qxl_device *qdev,
  350. u32 width, u32 height,
  351. u32 sx, u32 sy,
  352. u32 dx, u32 dy)
  353. {
  354. struct qxl_drawable *drawable;
  355. struct qxl_rect rect;
  356. struct qxl_release *release;
  357. int ret;
  358. ret = alloc_drawable(qdev, &release);
  359. if (ret)
  360. return;
  361. /* do a reservation run over all the objects we just allocated */
  362. ret = qxl_release_reserve_list(release, true);
  363. if (ret)
  364. goto out_free_release;
  365. rect.left = dx;
  366. rect.top = dy;
  367. rect.right = dx + width;
  368. rect.bottom = dy + height;
  369. ret = make_drawable(qdev, 0, QXL_COPY_BITS, &rect, release);
  370. if (ret) {
  371. qxl_release_backoff_reserve_list(release);
  372. goto out_free_release;
  373. }
  374. drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
  375. drawable->u.copy_bits.src_pos.x = sx;
  376. drawable->u.copy_bits.src_pos.y = sy;
  377. qxl_release_unmap(qdev, release, &drawable->release_info);
  378. qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
  379. qxl_release_fence_buffer_objects(release);
  380. out_free_release:
  381. if (ret)
  382. free_drawable(qdev, release);
  383. }
  384. void qxl_draw_fill(struct qxl_draw_fill *qxl_draw_fill_rec)
  385. {
  386. struct qxl_device *qdev = qxl_draw_fill_rec->qdev;
  387. struct qxl_rect rect = qxl_draw_fill_rec->rect;
  388. uint32_t color = qxl_draw_fill_rec->color;
  389. uint16_t rop = qxl_draw_fill_rec->rop;
  390. struct qxl_drawable *drawable;
  391. struct qxl_release *release;
  392. int ret;
  393. ret = alloc_drawable(qdev, &release);
  394. if (ret)
  395. return;
  396. /* do a reservation run over all the objects we just allocated */
  397. ret = qxl_release_reserve_list(release, true);
  398. if (ret)
  399. goto out_free_release;
  400. ret = make_drawable(qdev, 0, QXL_DRAW_FILL, &rect, release);
  401. if (ret) {
  402. qxl_release_backoff_reserve_list(release);
  403. goto out_free_release;
  404. }
  405. drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
  406. drawable->u.fill.brush.type = SPICE_BRUSH_TYPE_SOLID;
  407. drawable->u.fill.brush.u.color = color;
  408. drawable->u.fill.rop_descriptor = rop;
  409. drawable->u.fill.mask.flags = 0;
  410. drawable->u.fill.mask.pos.x = 0;
  411. drawable->u.fill.mask.pos.y = 0;
  412. drawable->u.fill.mask.bitmap = 0;
  413. qxl_release_unmap(qdev, release, &drawable->release_info);
  414. qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
  415. qxl_release_fence_buffer_objects(release);
  416. out_free_release:
  417. if (ret)
  418. free_drawable(qdev, release);
  419. }