blob: 56e1d633875e2772b6a3f767e3af44e013f3519d [file] [log] [blame]
Dave Airlief64122c2013-02-25 14:47:55 +10001/*
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
23#include "qxl_drv.h"
24#include "qxl_object.h"
25
Dave Airlie8002db62013-07-23 14:16:42 +100026static int alloc_clips(struct qxl_device *qdev,
27 struct qxl_release *release,
28 unsigned num_clips,
29 struct qxl_bo **clips_bo)
30{
31 int size = sizeof(struct qxl_clip_rects) + sizeof(struct qxl_rect) * num_clips;
32
33 return qxl_alloc_bo_reserved(qdev, release, size, clips_bo);
34}
35
Dave Airlief64122c2013-02-25 14:47:55 +100036/* returns a pointer to the already allocated qxl_rect array inside
37 * the qxl_clip_rects. This is *not* the same as the memory allocated
38 * on the device, it is offset to qxl_clip_rects.chunk.data */
39static struct qxl_rect *drawable_set_clipping(struct qxl_device *qdev,
40 struct qxl_drawable *drawable,
41 unsigned num_clips,
Dave Airlie8002db62013-07-23 14:16:42 +100042 struct qxl_bo *clips_bo)
Dave Airlief64122c2013-02-25 14:47:55 +100043{
44 struct qxl_clip_rects *dev_clips;
45 int ret;
Dave Airlief64122c2013-02-25 14:47:55 +100046
Dave Airlie8002db62013-07-23 14:16:42 +100047 ret = qxl_bo_kmap(clips_bo, (void **)&dev_clips);
Dave Airlief64122c2013-02-25 14:47:55 +100048 if (ret) {
Dave Airlief64122c2013-02-25 14:47:55 +100049 return NULL;
50 }
51 dev_clips->num_rects = num_clips;
52 dev_clips->chunk.next_chunk = 0;
53 dev_clips->chunk.prev_chunk = 0;
54 dev_clips->chunk.data_size = sizeof(struct qxl_rect) * num_clips;
55 return (struct qxl_rect *)dev_clips->chunk.data;
56}
57
58static int
Dave Airlie8002db62013-07-23 14:16:42 +100059alloc_drawable(struct qxl_device *qdev, struct qxl_release **release)
Dave Airlief64122c2013-02-25 14:47:55 +100060{
Dave Airlie8002db62013-07-23 14:16:42 +100061 int ret;
62 ret = qxl_alloc_release_reserved(qdev, sizeof(struct qxl_drawable),
Dave Airlief64122c2013-02-25 14:47:55 +100063 QXL_RELEASE_DRAWABLE, release,
64 NULL);
Dave Airlie8002db62013-07-23 14:16:42 +100065 return ret;
66}
Dave Airlief64122c2013-02-25 14:47:55 +100067
Dave Airlie8002db62013-07-23 14:16:42 +100068static void
69free_drawable(struct qxl_device *qdev, struct qxl_release *release)
70{
71 qxl_release_free(qdev, release);
72}
73
74/* release needs to be reserved at this point */
75static int
76make_drawable(struct qxl_device *qdev, int surface, uint8_t type,
77 const struct qxl_rect *rect,
78 struct qxl_release *release)
79{
80 struct qxl_drawable *drawable;
81 int i;
82
83 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
84 if (!drawable)
85 return -ENOMEM;
86
Dave Airlief64122c2013-02-25 14:47:55 +100087 drawable->type = type;
88
89 drawable->surface_id = surface; /* Only primary for now */
90 drawable->effect = QXL_EFFECT_OPAQUE;
91 drawable->self_bitmap = 0;
92 drawable->self_bitmap_area.top = 0;
93 drawable->self_bitmap_area.left = 0;
94 drawable->self_bitmap_area.bottom = 0;
95 drawable->self_bitmap_area.right = 0;
96 /* FIXME: add clipping */
97 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
98
99 /*
100 * surfaces_dest[i] should apparently be filled out with the
101 * surfaces that we depend on, and surface_rects should be
102 * filled with the rectangles of those surfaces that we
103 * are going to use.
104 */
105 for (i = 0; i < 3; ++i)
106 drawable->surfaces_dest[i] = -1;
107
108 if (rect)
109 drawable->bbox = *rect;
110
111 drawable->mm_time = qdev->rom->mm_clock;
Dave Airlie8002db62013-07-23 14:16:42 +1000112 qxl_release_unmap(qdev, release, &drawable->release_info);
Dave Airlief64122c2013-02-25 14:47:55 +1000113 return 0;
114}
115
Dave Airlie8002db62013-07-23 14:16:42 +1000116static int alloc_palette_object(struct qxl_device *qdev,
117 struct qxl_release *release,
118 struct qxl_bo **palette_bo)
119{
120 return qxl_alloc_bo_reserved(qdev, release,
121 sizeof(struct qxl_palette) + sizeof(uint32_t) * 2,
122 palette_bo);
123}
124
125static int qxl_palette_create_1bit(struct qxl_bo *palette_bo,
126 struct qxl_release *release,
Dave Airlief64122c2013-02-25 14:47:55 +1000127 const struct qxl_fb_image *qxl_fb_image)
128{
Dave Airlief64122c2013-02-25 14:47:55 +1000129 const struct fb_image *fb_image = &qxl_fb_image->fb_image;
130 uint32_t visual = qxl_fb_image->visual;
131 const uint32_t *pseudo_palette = qxl_fb_image->pseudo_palette;
132 struct qxl_palette *pal;
133 int ret;
134 uint32_t fgcolor, bgcolor;
135 static uint64_t unique; /* we make no attempt to actually set this
136 * correctly globaly, since that would require
137 * tracking all of our palettes. */
Dave Airlie8002db62013-07-23 14:16:42 +1000138 ret = qxl_bo_kmap(palette_bo, (void **)&pal);
Dave Airlief64122c2013-02-25 14:47:55 +1000139 pal->num_ents = 2;
140 pal->unique = unique++;
141 if (visual == FB_VISUAL_TRUECOLOR || visual == FB_VISUAL_DIRECTCOLOR) {
142 /* NB: this is the only used branch currently. */
143 fgcolor = pseudo_palette[fb_image->fg_color];
144 bgcolor = pseudo_palette[fb_image->bg_color];
145 } else {
146 fgcolor = fb_image->fg_color;
147 bgcolor = fb_image->bg_color;
148 }
149 pal->ents[0] = bgcolor;
150 pal->ents[1] = fgcolor;
Dave Airlie8002db62013-07-23 14:16:42 +1000151 qxl_bo_kunmap(palette_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000152 return 0;
153}
154
155void qxl_draw_opaque_fb(const struct qxl_fb_image *qxl_fb_image,
156 int stride /* filled in if 0 */)
157{
158 struct qxl_device *qdev = qxl_fb_image->qdev;
159 struct qxl_drawable *drawable;
160 struct qxl_rect rect;
161 const struct fb_image *fb_image = &qxl_fb_image->fb_image;
162 int x = fb_image->dx;
163 int y = fb_image->dy;
164 int width = fb_image->width;
165 int height = fb_image->height;
166 const char *src = fb_image->data;
167 int depth = fb_image->depth;
168 struct qxl_release *release;
Dave Airlief64122c2013-02-25 14:47:55 +1000169 struct qxl_image *image;
170 int ret;
Dave Airlie8002db62013-07-23 14:16:42 +1000171 struct qxl_drm_image *dimage;
172 struct qxl_bo *palette_bo = NULL;
Dave Airlief64122c2013-02-25 14:47:55 +1000173 if (stride == 0)
174 stride = depth * width / 8;
175
Dave Airlie8002db62013-07-23 14:16:42 +1000176 ret = alloc_drawable(qdev, &release);
177 if (ret)
178 return;
179
180 ret = qxl_image_alloc_objects(qdev, release,
181 &dimage,
182 height, stride);
183 if (ret)
184 goto out_free_drawable;
185
186 if (depth == 1) {
187 ret = alloc_palette_object(qdev, release, &palette_bo);
188 if (ret)
189 goto out_free_image;
190 }
191
192 /* do a reservation run over all the objects we just allocated */
193 ret = qxl_release_reserve_list(release, true);
194 if (ret)
195 goto out_free_palette;
196
Dave Airlief64122c2013-02-25 14:47:55 +1000197 rect.left = x;
198 rect.right = x + width;
199 rect.top = y;
200 rect.bottom = y + height;
201
Dave Airlie8002db62013-07-23 14:16:42 +1000202 ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &rect, release);
Dave Airlief64122c2013-02-25 14:47:55 +1000203 if (ret) {
Dave Airlie8002db62013-07-23 14:16:42 +1000204 qxl_release_backoff_reserve_list(release);
205 goto out_free_palette;
206 }
207
208 ret = qxl_image_init(qdev, release, dimage,
209 (const uint8_t *)src, 0, 0,
210 width, height, depth, stride);
211 if (ret) {
212 qxl_release_backoff_reserve_list(release);
Dave Airlief64122c2013-02-25 14:47:55 +1000213 qxl_release_free(qdev, release);
214 return;
215 }
216
217 if (depth == 1) {
Dave Airlief64122c2013-02-25 14:47:55 +1000218 void *ptr;
Dave Airlie8002db62013-07-23 14:16:42 +1000219 ret = qxl_palette_create_1bit(palette_bo, release, qxl_fb_image);
Dave Airlief64122c2013-02-25 14:47:55 +1000220
Dave Airlie8002db62013-07-23 14:16:42 +1000221 ptr = qxl_bo_kmap_atomic_page(qdev, dimage->bo, 0);
Dave Airlief64122c2013-02-25 14:47:55 +1000222 image = ptr;
223 image->u.bitmap.palette =
224 qxl_bo_physical_address(qdev, palette_bo, 0);
Dave Airlie8002db62013-07-23 14:16:42 +1000225 qxl_bo_kunmap_atomic_page(qdev, dimage->bo, ptr);
Dave Airlief64122c2013-02-25 14:47:55 +1000226 }
227
228 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
229
230 drawable->u.copy.src_area.top = 0;
231 drawable->u.copy.src_area.bottom = height;
232 drawable->u.copy.src_area.left = 0;
233 drawable->u.copy.src_area.right = width;
234
235 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
236 drawable->u.copy.scale_mode = 0;
237 drawable->u.copy.mask.flags = 0;
238 drawable->u.copy.mask.pos.x = 0;
239 drawable->u.copy.mask.pos.y = 0;
240 drawable->u.copy.mask.bitmap = 0;
241
242 drawable->u.copy.src_bitmap =
Dave Airlie8002db62013-07-23 14:16:42 +1000243 qxl_bo_physical_address(qdev, dimage->bo, 0);
Dave Airlief64122c2013-02-25 14:47:55 +1000244 qxl_release_unmap(qdev, release, &drawable->release_info);
245
Dave Airlief64122c2013-02-25 14:47:55 +1000246 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000247 qxl_release_fence_buffer_objects(release);
248
249out_free_palette:
250 if (palette_bo)
251 qxl_bo_unref(&palette_bo);
252out_free_image:
253 qxl_image_free_objects(qdev, dimage);
254out_free_drawable:
255 if (ret)
256 free_drawable(qdev, release);
Dave Airlief64122c2013-02-25 14:47:55 +1000257}
258
259/* push a draw command using the given clipping rectangles as
260 * the sources from the shadow framebuffer.
261 *
262 * Right now implementing with a single draw and a clip list. Clip
263 * lists are known to be a problem performance wise, this can be solved
264 * by treating them differently in the server.
265 */
266void qxl_draw_dirty_fb(struct qxl_device *qdev,
267 struct qxl_framebuffer *qxl_fb,
268 struct qxl_bo *bo,
269 unsigned flags, unsigned color,
270 struct drm_clip_rect *clips,
271 unsigned num_clips, int inc)
272{
273 /*
274 * TODO: if flags & DRM_MODE_FB_DIRTY_ANNOTATE_FILL then we should
275 * send a fill command instead, much cheaper.
276 *
277 * See include/drm/drm_mode.h
278 */
279 struct drm_clip_rect *clips_ptr;
280 int i;
281 int left, right, top, bottom;
282 int width, height;
283 struct qxl_drawable *drawable;
284 struct qxl_rect drawable_rect;
285 struct qxl_rect *rects;
286 int stride = qxl_fb->base.pitches[0];
287 /* depth is not actually interesting, we don't mask with it */
288 int depth = qxl_fb->base.bits_per_pixel;
289 uint8_t *surface_base;
290 struct qxl_release *release;
Dave Airlief64122c2013-02-25 14:47:55 +1000291 struct qxl_bo *clips_bo;
Dave Airlie8002db62013-07-23 14:16:42 +1000292 struct qxl_drm_image *dimage;
Dave Airlief64122c2013-02-25 14:47:55 +1000293 int ret;
294
Dave Airlie8002db62013-07-23 14:16:42 +1000295 ret = alloc_drawable(qdev, &release);
296 if (ret)
297 return;
298
Dave Airlief64122c2013-02-25 14:47:55 +1000299 left = clips->x1;
300 right = clips->x2;
301 top = clips->y1;
302 bottom = clips->y2;
303
304 /* skip the first clip rect */
305 for (i = 1, clips_ptr = clips + inc;
306 i < num_clips; i++, clips_ptr += inc) {
307 left = min_t(int, left, (int)clips_ptr->x1);
308 right = max_t(int, right, (int)clips_ptr->x2);
309 top = min_t(int, top, (int)clips_ptr->y1);
310 bottom = max_t(int, bottom, (int)clips_ptr->y2);
311 }
312
313 width = right - left;
314 height = bottom - top;
Dave Airlie8002db62013-07-23 14:16:42 +1000315
316 ret = alloc_clips(qdev, release, num_clips, &clips_bo);
317 if (ret)
318 goto out_free_drawable;
319
320 ret = qxl_image_alloc_objects(qdev, release,
321 &dimage,
322 height, stride);
323 if (ret)
324 goto out_free_clips;
325
326 /* do a reservation run over all the objects we just allocated */
327 ret = qxl_release_reserve_list(release, true);
328 if (ret)
329 goto out_free_image;
330
Dave Airlief64122c2013-02-25 14:47:55 +1000331 drawable_rect.left = left;
332 drawable_rect.right = right;
333 drawable_rect.top = top;
334 drawable_rect.bottom = bottom;
Dave Airlie8002db62013-07-23 14:16:42 +1000335
Dave Airlief64122c2013-02-25 14:47:55 +1000336 ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &drawable_rect,
Dave Airlie8002db62013-07-23 14:16:42 +1000337 release);
Dave Airlief64122c2013-02-25 14:47:55 +1000338 if (ret)
Dave Airlie8002db62013-07-23 14:16:42 +1000339 goto out_release_backoff;
Dave Airlief64122c2013-02-25 14:47:55 +1000340
341 ret = qxl_bo_kmap(bo, (void **)&surface_base);
342 if (ret)
Dave Airlie8002db62013-07-23 14:16:42 +1000343 goto out_release_backoff;
Dave Airlief64122c2013-02-25 14:47:55 +1000344
Dave Airlie8002db62013-07-23 14:16:42 +1000345
346 ret = qxl_image_init(qdev, release, dimage, surface_base,
347 left, top, width, height, depth, stride);
Dave Airlief64122c2013-02-25 14:47:55 +1000348 qxl_bo_kunmap(bo);
349 if (ret)
Dave Airlie8002db62013-07-23 14:16:42 +1000350 goto out_release_backoff;
Dave Airlief64122c2013-02-25 14:47:55 +1000351
Dave Airlie8002db62013-07-23 14:16:42 +1000352 rects = drawable_set_clipping(qdev, drawable, num_clips, clips_bo);
353 if (!rects)
354 goto out_release_backoff;
355
Dave Airlief64122c2013-02-25 14:47:55 +1000356 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
357
358 drawable->clip.type = SPICE_CLIP_TYPE_RECTS;
359 drawable->clip.data = qxl_bo_physical_address(qdev,
360 clips_bo, 0);
Dave Airlief64122c2013-02-25 14:47:55 +1000361
362 drawable->u.copy.src_area.top = 0;
363 drawable->u.copy.src_area.bottom = height;
364 drawable->u.copy.src_area.left = 0;
365 drawable->u.copy.src_area.right = width;
366
367 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
368 drawable->u.copy.scale_mode = 0;
369 drawable->u.copy.mask.flags = 0;
370 drawable->u.copy.mask.pos.x = 0;
371 drawable->u.copy.mask.pos.y = 0;
372 drawable->u.copy.mask.bitmap = 0;
373
Dave Airlie8002db62013-07-23 14:16:42 +1000374 drawable->u.copy.src_bitmap = qxl_bo_physical_address(qdev, dimage->bo, 0);
Dave Airlief64122c2013-02-25 14:47:55 +1000375 qxl_release_unmap(qdev, release, &drawable->release_info);
Dave Airlie8002db62013-07-23 14:16:42 +1000376
Dave Airlief64122c2013-02-25 14:47:55 +1000377 clips_ptr = clips;
378 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
379 rects[i].left = clips_ptr->x1;
380 rects[i].right = clips_ptr->x2;
381 rects[i].top = clips_ptr->y1;
382 rects[i].bottom = clips_ptr->y2;
383 }
384 qxl_bo_kunmap(clips_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000385
Dave Airlief64122c2013-02-25 14:47:55 +1000386 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000387 qxl_release_fence_buffer_objects(release);
Dave Airlief64122c2013-02-25 14:47:55 +1000388
Dave Airlie8002db62013-07-23 14:16:42 +1000389out_release_backoff:
390 if (ret)
391 qxl_release_backoff_reserve_list(release);
392out_free_image:
393 qxl_image_free_objects(qdev, dimage);
394out_free_clips:
395 qxl_bo_unref(&clips_bo);
396out_free_drawable:
397 /* only free drawable on error */
398 if (ret)
399 free_drawable(qdev, release);
400
Dave Airlief64122c2013-02-25 14:47:55 +1000401}
402
403void qxl_draw_copyarea(struct qxl_device *qdev,
404 u32 width, u32 height,
405 u32 sx, u32 sy,
406 u32 dx, u32 dy)
407{
408 struct qxl_drawable *drawable;
409 struct qxl_rect rect;
410 struct qxl_release *release;
411 int ret;
412
Dave Airlie8002db62013-07-23 14:16:42 +1000413 ret = alloc_drawable(qdev, &release);
414 if (ret)
415 return;
416
417 /* do a reservation run over all the objects we just allocated */
418 ret = qxl_release_reserve_list(release, true);
419 if (ret)
420 goto out_free_release;
421
Dave Airlief64122c2013-02-25 14:47:55 +1000422 rect.left = dx;
423 rect.top = dy;
424 rect.right = dx + width;
425 rect.bottom = dy + height;
Dave Airlie8002db62013-07-23 14:16:42 +1000426 ret = make_drawable(qdev, 0, QXL_COPY_BITS, &rect, release);
427 if (ret) {
428 qxl_release_backoff_reserve_list(release);
429 goto out_free_release;
430 }
Dave Airlief64122c2013-02-25 14:47:55 +1000431
432 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
433 drawable->u.copy_bits.src_pos.x = sx;
434 drawable->u.copy_bits.src_pos.y = sy;
Dave Airlief64122c2013-02-25 14:47:55 +1000435 qxl_release_unmap(qdev, release, &drawable->release_info);
Dave Airlie8002db62013-07-23 14:16:42 +1000436
Dave Airlief64122c2013-02-25 14:47:55 +1000437 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000438 qxl_release_fence_buffer_objects(release);
439
440out_free_release:
441 if (ret)
442 free_drawable(qdev, release);
Dave Airlief64122c2013-02-25 14:47:55 +1000443}
444
445void qxl_draw_fill(struct qxl_draw_fill *qxl_draw_fill_rec)
446{
447 struct qxl_device *qdev = qxl_draw_fill_rec->qdev;
448 struct qxl_rect rect = qxl_draw_fill_rec->rect;
449 uint32_t color = qxl_draw_fill_rec->color;
450 uint16_t rop = qxl_draw_fill_rec->rop;
451 struct qxl_drawable *drawable;
452 struct qxl_release *release;
453 int ret;
454
Dave Airlie8002db62013-07-23 14:16:42 +1000455 ret = alloc_drawable(qdev, &release);
Dave Airlief64122c2013-02-25 14:47:55 +1000456 if (ret)
457 return;
458
Dave Airlie8002db62013-07-23 14:16:42 +1000459 /* do a reservation run over all the objects we just allocated */
460 ret = qxl_release_reserve_list(release, true);
461 if (ret)
462 goto out_free_release;
463
464 ret = make_drawable(qdev, 0, QXL_DRAW_FILL, &rect, release);
465 if (ret) {
466 qxl_release_backoff_reserve_list(release);
467 goto out_free_release;
468 }
469
Dave Airlief64122c2013-02-25 14:47:55 +1000470 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
471 drawable->u.fill.brush.type = SPICE_BRUSH_TYPE_SOLID;
472 drawable->u.fill.brush.u.color = color;
473 drawable->u.fill.rop_descriptor = rop;
474 drawable->u.fill.mask.flags = 0;
475 drawable->u.fill.mask.pos.x = 0;
476 drawable->u.fill.mask.pos.y = 0;
477 drawable->u.fill.mask.bitmap = 0;
478
479 qxl_release_unmap(qdev, release, &drawable->release_info);
Dave Airlie8002db62013-07-23 14:16:42 +1000480
Dave Airlief64122c2013-02-25 14:47:55 +1000481 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000482 qxl_release_fence_buffer_objects(release);
483
484out_free_release:
485 if (ret)
486 free_drawable(qdev, release);
Dave Airlief64122c2013-02-25 14:47:55 +1000487}