qxl_draw.c 14 KB

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