qxl: check for kmap failures
[deliverable/linux.git] / drivers / gpu / drm / qxl / qxl_draw.c
CommitLineData
f64122c1
DA
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
8002db63
DA
26static 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
f64122c1
DA
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 */
39static struct qxl_rect *drawable_set_clipping(struct qxl_device *qdev,
40 struct qxl_drawable *drawable,
41 unsigned num_clips,
8002db63 42 struct qxl_bo *clips_bo)
f64122c1
DA
43{
44 struct qxl_clip_rects *dev_clips;
45 int ret;
f64122c1 46
8002db63 47 ret = qxl_bo_kmap(clips_bo, (void **)&dev_clips);
f64122c1 48 if (ret) {
f64122c1
DA
49 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
8002db63
DA
58static int
59alloc_drawable(struct qxl_device *qdev, struct qxl_release **release)
60{
61 int ret;
62 ret = qxl_alloc_release_reserved(qdev, sizeof(struct qxl_drawable),
63 QXL_RELEASE_DRAWABLE, release,
64 NULL);
65 return ret;
66}
67
68static 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 */
f64122c1
DA
75static int
76make_drawable(struct qxl_device *qdev, int surface, uint8_t type,
77 const struct qxl_rect *rect,
8002db63 78 struct qxl_release *release)
f64122c1
DA
79{
80 struct qxl_drawable *drawable;
8002db63 81 int i;
f64122c1 82
8002db63
DA
83 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
84 if (!drawable)
85 return -ENOMEM;
f64122c1 86
f64122c1
DA
87 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;
8002db63 112 qxl_release_unmap(qdev, release, &drawable->release_info);
f64122c1
DA
113 return 0;
114}
115
8002db63
DA
116static 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,
f64122c1
DA
127 const struct qxl_fb_image *qxl_fb_image)
128{
f64122c1
DA
129 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. */
8002db63 138 ret = qxl_bo_kmap(palette_bo, (void **)&pal);
f4cceb2a
DC
139 if (ret)
140 return ret;
f64122c1
DA
141 pal->num_ents = 2;
142 pal->unique = unique++;
143 if (visual == FB_VISUAL_TRUECOLOR || visual == FB_VISUAL_DIRECTCOLOR) {
144 /* NB: this is the only used branch currently. */
145 fgcolor = pseudo_palette[fb_image->fg_color];
146 bgcolor = pseudo_palette[fb_image->bg_color];
147 } else {
148 fgcolor = fb_image->fg_color;
149 bgcolor = fb_image->bg_color;
150 }
151 pal->ents[0] = bgcolor;
152 pal->ents[1] = fgcolor;
8002db63 153 qxl_bo_kunmap(palette_bo);
f64122c1
DA
154 return 0;
155}
156
157void qxl_draw_opaque_fb(const struct qxl_fb_image *qxl_fb_image,
158 int stride /* filled in if 0 */)
159{
160 struct qxl_device *qdev = qxl_fb_image->qdev;
161 struct qxl_drawable *drawable;
162 struct qxl_rect rect;
163 const struct fb_image *fb_image = &qxl_fb_image->fb_image;
164 int x = fb_image->dx;
165 int y = fb_image->dy;
166 int width = fb_image->width;
167 int height = fb_image->height;
168 const char *src = fb_image->data;
169 int depth = fb_image->depth;
170 struct qxl_release *release;
f64122c1
DA
171 struct qxl_image *image;
172 int ret;
8002db63
DA
173 struct qxl_drm_image *dimage;
174 struct qxl_bo *palette_bo = NULL;
f64122c1
DA
175 if (stride == 0)
176 stride = depth * width / 8;
177
8002db63
DA
178 ret = alloc_drawable(qdev, &release);
179 if (ret)
180 return;
181
182 ret = qxl_image_alloc_objects(qdev, release,
183 &dimage,
184 height, stride);
185 if (ret)
186 goto out_free_drawable;
187
188 if (depth == 1) {
189 ret = alloc_palette_object(qdev, release, &palette_bo);
190 if (ret)
191 goto out_free_image;
192 }
193
194 /* do a reservation run over all the objects we just allocated */
195 ret = qxl_release_reserve_list(release, true);
196 if (ret)
197 goto out_free_palette;
198
f64122c1
DA
199 rect.left = x;
200 rect.right = x + width;
201 rect.top = y;
202 rect.bottom = y + height;
203
8002db63
DA
204 ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &rect, release);
205 if (ret) {
206 qxl_release_backoff_reserve_list(release);
207 goto out_free_palette;
208 }
f64122c1 209
8002db63
DA
210 ret = qxl_image_init(qdev, release, dimage,
211 (const uint8_t *)src, 0, 0,
212 width, height, depth, stride);
f64122c1 213 if (ret) {
8002db63 214 qxl_release_backoff_reserve_list(release);
f64122c1
DA
215 qxl_release_free(qdev, release);
216 return;
217 }
218
219 if (depth == 1) {
f64122c1 220 void *ptr;
8002db63 221 ret = qxl_palette_create_1bit(palette_bo, release, qxl_fb_image);
f64122c1 222
8002db63 223 ptr = qxl_bo_kmap_atomic_page(qdev, dimage->bo, 0);
f64122c1
DA
224 image = ptr;
225 image->u.bitmap.palette =
226 qxl_bo_physical_address(qdev, palette_bo, 0);
8002db63 227 qxl_bo_kunmap_atomic_page(qdev, dimage->bo, ptr);
f64122c1
DA
228 }
229
230 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
231
232 drawable->u.copy.src_area.top = 0;
233 drawable->u.copy.src_area.bottom = height;
234 drawable->u.copy.src_area.left = 0;
235 drawable->u.copy.src_area.right = width;
236
237 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
238 drawable->u.copy.scale_mode = 0;
239 drawable->u.copy.mask.flags = 0;
240 drawable->u.copy.mask.pos.x = 0;
241 drawable->u.copy.mask.pos.y = 0;
242 drawable->u.copy.mask.bitmap = 0;
243
244 drawable->u.copy.src_bitmap =
8002db63 245 qxl_bo_physical_address(qdev, dimage->bo, 0);
f64122c1
DA
246 qxl_release_unmap(qdev, release, &drawable->release_info);
247
f64122c1 248 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
8002db63
DA
249 qxl_release_fence_buffer_objects(release);
250
251out_free_palette:
252 if (palette_bo)
253 qxl_bo_unref(&palette_bo);
254out_free_image:
255 qxl_image_free_objects(qdev, dimage);
256out_free_drawable:
257 if (ret)
258 free_drawable(qdev, release);
f64122c1
DA
259}
260
261/* push a draw command using the given clipping rectangles as
262 * the sources from the shadow framebuffer.
263 *
264 * Right now implementing with a single draw and a clip list. Clip
265 * lists are known to be a problem performance wise, this can be solved
266 * by treating them differently in the server.
267 */
268void qxl_draw_dirty_fb(struct qxl_device *qdev,
269 struct qxl_framebuffer *qxl_fb,
270 struct qxl_bo *bo,
271 unsigned flags, unsigned color,
272 struct drm_clip_rect *clips,
273 unsigned num_clips, int inc)
274{
275 /*
276 * TODO: if flags & DRM_MODE_FB_DIRTY_ANNOTATE_FILL then we should
277 * send a fill command instead, much cheaper.
278 *
279 * See include/drm/drm_mode.h
280 */
281 struct drm_clip_rect *clips_ptr;
282 int i;
283 int left, right, top, bottom;
284 int width, height;
285 struct qxl_drawable *drawable;
286 struct qxl_rect drawable_rect;
287 struct qxl_rect *rects;
288 int stride = qxl_fb->base.pitches[0];
289 /* depth is not actually interesting, we don't mask with it */
290 int depth = qxl_fb->base.bits_per_pixel;
291 uint8_t *surface_base;
292 struct qxl_release *release;
f64122c1 293 struct qxl_bo *clips_bo;
8002db63 294 struct qxl_drm_image *dimage;
f64122c1
DA
295 int ret;
296
8002db63
DA
297 ret = alloc_drawable(qdev, &release);
298 if (ret)
299 return;
300
f64122c1
DA
301 left = clips->x1;
302 right = clips->x2;
303 top = clips->y1;
304 bottom = clips->y2;
305
306 /* skip the first clip rect */
307 for (i = 1, clips_ptr = clips + inc;
308 i < num_clips; i++, clips_ptr += inc) {
309 left = min_t(int, left, (int)clips_ptr->x1);
310 right = max_t(int, right, (int)clips_ptr->x2);
311 top = min_t(int, top, (int)clips_ptr->y1);
312 bottom = max_t(int, bottom, (int)clips_ptr->y2);
313 }
314
315 width = right - left;
316 height = bottom - top;
8002db63
DA
317
318 ret = alloc_clips(qdev, release, num_clips, &clips_bo);
319 if (ret)
320 goto out_free_drawable;
321
322 ret = qxl_image_alloc_objects(qdev, release,
323 &dimage,
324 height, stride);
325 if (ret)
326 goto out_free_clips;
327
328 /* do a reservation run over all the objects we just allocated */
329 ret = qxl_release_reserve_list(release, true);
330 if (ret)
331 goto out_free_image;
332
f64122c1
DA
333 drawable_rect.left = left;
334 drawable_rect.right = right;
335 drawable_rect.top = top;
336 drawable_rect.bottom = bottom;
8002db63 337
f64122c1 338 ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &drawable_rect,
8002db63 339 release);
f64122c1 340 if (ret)
8002db63 341 goto out_release_backoff;
f64122c1
DA
342
343 ret = qxl_bo_kmap(bo, (void **)&surface_base);
344 if (ret)
8002db63 345 goto out_release_backoff;
f64122c1 346
8002db63
DA
347
348 ret = qxl_image_init(qdev, release, dimage, surface_base,
349 left, top, width, height, depth, stride);
f64122c1
DA
350 qxl_bo_kunmap(bo);
351 if (ret)
8002db63
DA
352 goto out_release_backoff;
353
354 rects = drawable_set_clipping(qdev, drawable, num_clips, clips_bo);
355 if (!rects)
356 goto out_release_backoff;
f64122c1 357
f64122c1
DA
358 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
359
360 drawable->clip.type = SPICE_CLIP_TYPE_RECTS;
361 drawable->clip.data = qxl_bo_physical_address(qdev,
362 clips_bo, 0);
f64122c1
DA
363
364 drawable->u.copy.src_area.top = 0;
365 drawable->u.copy.src_area.bottom = height;
366 drawable->u.copy.src_area.left = 0;
367 drawable->u.copy.src_area.right = width;
368
369 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
370 drawable->u.copy.scale_mode = 0;
371 drawable->u.copy.mask.flags = 0;
372 drawable->u.copy.mask.pos.x = 0;
373 drawable->u.copy.mask.pos.y = 0;
374 drawable->u.copy.mask.bitmap = 0;
375
8002db63 376 drawable->u.copy.src_bitmap = qxl_bo_physical_address(qdev, dimage->bo, 0);
f64122c1 377 qxl_release_unmap(qdev, release, &drawable->release_info);
8002db63 378
f64122c1
DA
379 clips_ptr = clips;
380 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
381 rects[i].left = clips_ptr->x1;
382 rects[i].right = clips_ptr->x2;
383 rects[i].top = clips_ptr->y1;
384 rects[i].bottom = clips_ptr->y2;
385 }
386 qxl_bo_kunmap(clips_bo);
f64122c1 387
f64122c1 388 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
8002db63
DA
389 qxl_release_fence_buffer_objects(release);
390
391out_release_backoff:
392 if (ret)
393 qxl_release_backoff_reserve_list(release);
394out_free_image:
395 qxl_image_free_objects(qdev, dimage);
396out_free_clips:
397 qxl_bo_unref(&clips_bo);
398out_free_drawable:
399 /* only free drawable on error */
400 if (ret)
401 free_drawable(qdev, release);
f64122c1 402
f64122c1
DA
403}
404
405void qxl_draw_copyarea(struct qxl_device *qdev,
406 u32 width, u32 height,
407 u32 sx, u32 sy,
408 u32 dx, u32 dy)
409{
410 struct qxl_drawable *drawable;
411 struct qxl_rect rect;
412 struct qxl_release *release;
413 int ret;
414
8002db63
DA
415 ret = alloc_drawable(qdev, &release);
416 if (ret)
417 return;
418
419 /* do a reservation run over all the objects we just allocated */
420 ret = qxl_release_reserve_list(release, true);
421 if (ret)
422 goto out_free_release;
423
f64122c1
DA
424 rect.left = dx;
425 rect.top = dy;
426 rect.right = dx + width;
427 rect.bottom = dy + height;
8002db63
DA
428 ret = make_drawable(qdev, 0, QXL_COPY_BITS, &rect, release);
429 if (ret) {
430 qxl_release_backoff_reserve_list(release);
431 goto out_free_release;
432 }
f64122c1
DA
433
434 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
435 drawable->u.copy_bits.src_pos.x = sx;
436 drawable->u.copy_bits.src_pos.y = sy;
f64122c1 437 qxl_release_unmap(qdev, release, &drawable->release_info);
8002db63 438
f64122c1 439 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
8002db63
DA
440 qxl_release_fence_buffer_objects(release);
441
442out_free_release:
443 if (ret)
444 free_drawable(qdev, release);
f64122c1
DA
445}
446
447void qxl_draw_fill(struct qxl_draw_fill *qxl_draw_fill_rec)
448{
449 struct qxl_device *qdev = qxl_draw_fill_rec->qdev;
450 struct qxl_rect rect = qxl_draw_fill_rec->rect;
451 uint32_t color = qxl_draw_fill_rec->color;
452 uint16_t rop = qxl_draw_fill_rec->rop;
453 struct qxl_drawable *drawable;
454 struct qxl_release *release;
455 int ret;
456
8002db63 457 ret = alloc_drawable(qdev, &release);
f64122c1
DA
458 if (ret)
459 return;
460
8002db63
DA
461 /* do a reservation run over all the objects we just allocated */
462 ret = qxl_release_reserve_list(release, true);
463 if (ret)
464 goto out_free_release;
465
466 ret = make_drawable(qdev, 0, QXL_DRAW_FILL, &rect, release);
467 if (ret) {
468 qxl_release_backoff_reserve_list(release);
469 goto out_free_release;
470 }
471
f64122c1
DA
472 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
473 drawable->u.fill.brush.type = SPICE_BRUSH_TYPE_SOLID;
474 drawable->u.fill.brush.u.color = color;
475 drawable->u.fill.rop_descriptor = rop;
476 drawable->u.fill.mask.flags = 0;
477 drawable->u.fill.mask.pos.x = 0;
478 drawable->u.fill.mask.pos.y = 0;
479 drawable->u.fill.mask.bitmap = 0;
480
481 qxl_release_unmap(qdev, release, &drawable->release_info);
8002db63 482
f64122c1 483 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
8002db63
DA
484 qxl_release_fence_buffer_objects(release);
485
486out_free_release:
487 if (ret)
488 free_drawable(qdev, release);
f64122c1 489}
This page took 0.216949 seconds and 5 git commands to generate.