qxl_draw.c 14 KB

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