2 * Copyright 2011 Red Hat, Inc.
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:
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
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.
24 #include "qxl_object.h"
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)
31 int size = sizeof(struct qxl_clip_rects) + sizeof(struct qxl_rect) * num_clips;
33 return qxl_alloc_bo_reserved(qdev, release, size, clips_bo);
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)
43 struct qxl_clip_rects *dev_clips;
46 ret = qxl_bo_kmap(clips_bo, (void **)&dev_clips);
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;
58 alloc_drawable(struct qxl_device *qdev, struct qxl_release **release)
60 return qxl_alloc_release_reserved(qdev, sizeof(struct qxl_drawable),
61 QXL_RELEASE_DRAWABLE, release, NULL);
65 free_drawable(struct qxl_device *qdev, struct qxl_release *release)
67 qxl_release_free(qdev, release);
70 /* release needs to be reserved at this point */
72 make_drawable(struct qxl_device *qdev, int surface, uint8_t type,
73 const struct qxl_rect *rect,
74 struct qxl_release *release)
76 struct qxl_drawable *drawable;
79 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
83 drawable->type = type;
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;
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
101 for (i = 0; i < 3; ++i)
102 drawable->surfaces_dest[i] = -1;
105 drawable->bbox = *rect;
107 drawable->mm_time = qdev->rom->mm_clock;
108 qxl_release_unmap(qdev, release, &drawable->release_info);
112 /* push a draw command using the given clipping rectangles as
113 * the sources from the shadow framebuffer.
115 * Right now implementing with a single draw and a clip list. Clip
116 * lists are known to be a problem performance wise, this can be solved
117 * by treating them differently in the server.
119 void qxl_draw_dirty_fb(struct qxl_device *qdev,
120 struct drm_framebuffer *fb,
122 unsigned int flags, unsigned int color,
123 struct drm_clip_rect *clips,
124 unsigned int num_clips, int inc,
125 uint32_t dumb_shadow_offset)
128 * TODO: if flags & DRM_MODE_FB_DIRTY_ANNOTATE_FILL then we should
129 * send a fill command instead, much cheaper.
131 * See include/drm/drm_mode.h
133 struct drm_clip_rect *clips_ptr;
135 int left, right, top, bottom;
137 struct qxl_drawable *drawable;
138 struct qxl_rect drawable_rect;
139 struct qxl_rect *rects;
140 int stride = fb->pitches[0];
141 /* depth is not actually interesting, we don't mask with it */
142 int depth = fb->format->cpp[0] * 8;
143 uint8_t *surface_base;
144 struct qxl_release *release;
145 struct qxl_bo *clips_bo;
146 struct qxl_drm_image *dimage;
149 ret = alloc_drawable(qdev, &release);
153 clips->x1 += dumb_shadow_offset;
154 clips->x2 += dumb_shadow_offset;
161 /* skip the first clip rect */
162 for (i = 1, clips_ptr = clips + inc;
163 i < num_clips; i++, clips_ptr += inc) {
164 left = min_t(int, left, (int)clips_ptr->x1);
165 right = max_t(int, right, (int)clips_ptr->x2);
166 top = min_t(int, top, (int)clips_ptr->y1);
167 bottom = max_t(int, bottom, (int)clips_ptr->y2);
170 width = right - left;
171 height = bottom - top;
173 ret = alloc_clips(qdev, release, num_clips, &clips_bo);
175 goto out_free_drawable;
177 ret = qxl_image_alloc_objects(qdev, release,
183 /* do a reservation run over all the objects we just allocated */
184 ret = qxl_release_reserve_list(release, true);
188 drawable_rect.left = left;
189 drawable_rect.right = right;
190 drawable_rect.top = top;
191 drawable_rect.bottom = bottom;
193 ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &drawable_rect,
196 goto out_release_backoff;
198 ret = qxl_bo_kmap(bo, (void **)&surface_base);
200 goto out_release_backoff;
202 ret = qxl_image_init(qdev, release, dimage, surface_base,
203 left - dumb_shadow_offset,
204 top, width, height, depth, stride);
207 goto out_release_backoff;
209 rects = drawable_set_clipping(qdev, num_clips, clips_bo);
211 goto out_release_backoff;
213 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
215 drawable->clip.type = SPICE_CLIP_TYPE_RECTS;
216 drawable->clip.data = qxl_bo_physical_address(qdev,
219 drawable->u.copy.src_area.top = 0;
220 drawable->u.copy.src_area.bottom = height;
221 drawable->u.copy.src_area.left = 0;
222 drawable->u.copy.src_area.right = width;
224 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
225 drawable->u.copy.scale_mode = 0;
226 drawable->u.copy.mask.flags = 0;
227 drawable->u.copy.mask.pos.x = 0;
228 drawable->u.copy.mask.pos.y = 0;
229 drawable->u.copy.mask.bitmap = 0;
231 drawable->u.copy.src_bitmap = qxl_bo_physical_address(qdev, dimage->bo, 0);
232 qxl_release_unmap(qdev, release, &drawable->release_info);
235 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
236 rects[i].left = clips_ptr->x1;
237 rects[i].right = clips_ptr->x2;
238 rects[i].top = clips_ptr->y1;
239 rects[i].bottom = clips_ptr->y2;
241 qxl_bo_kunmap(clips_bo);
243 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
244 qxl_release_fence_buffer_objects(release);
248 qxl_release_backoff_reserve_list(release);
250 qxl_image_free_objects(qdev, dimage);
252 qxl_bo_unref(&clips_bo);
254 /* only free drawable on error */
256 free_drawable(qdev, release);