qxl_draw.c 14 KB

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