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