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 <linux/iosys-map.h>
24
25 #include <drm/drm_fourcc.h>
26
27 #include "qxl_drv.h"
28 #include "qxl_object.h"
29
alloc_clips(struct qxl_device * qdev,struct qxl_release * release,unsigned int num_clips,struct qxl_bo ** clips_bo)30 static int alloc_clips(struct qxl_device *qdev,
31 struct qxl_release *release,
32 unsigned int num_clips,
33 struct qxl_bo **clips_bo)
34 {
35 int size = sizeof(struct qxl_clip_rects) + sizeof(struct qxl_rect) * num_clips;
36
37 return qxl_alloc_bo_reserved(qdev, release, size, clips_bo);
38 }
39
40 /* returns a pointer to the already allocated qxl_rect array inside
41 * the qxl_clip_rects. This is *not* the same as the memory allocated
42 * on the device, it is offset to qxl_clip_rects.chunk.data */
drawable_set_clipping(struct qxl_device * qdev,unsigned int num_clips,struct qxl_bo * clips_bo)43 static struct qxl_rect *drawable_set_clipping(struct qxl_device *qdev,
44 unsigned int num_clips,
45 struct qxl_bo *clips_bo)
46 {
47 struct iosys_map map;
48 struct qxl_clip_rects *dev_clips;
49 int ret;
50
51 ret = qxl_bo_vmap_locked(clips_bo, &map);
52 if (ret)
53 return NULL;
54 dev_clips = map.vaddr; /* TODO: Use mapping abstraction properly */
55
56 dev_clips->num_rects = num_clips;
57 dev_clips->chunk.next_chunk = 0;
58 dev_clips->chunk.prev_chunk = 0;
59 dev_clips->chunk.data_size = sizeof(struct qxl_rect) * num_clips;
60 return (struct qxl_rect *)dev_clips->chunk.data;
61 }
62
63 static int
alloc_drawable(struct qxl_device * qdev,struct qxl_release ** release)64 alloc_drawable(struct qxl_device *qdev, struct qxl_release **release)
65 {
66 return qxl_alloc_release_reserved(qdev, sizeof(struct qxl_drawable),
67 QXL_RELEASE_DRAWABLE, release, NULL);
68 }
69
70 static void
free_drawable(struct qxl_device * qdev,struct qxl_release * release)71 free_drawable(struct qxl_device *qdev, struct qxl_release *release)
72 {
73 qxl_release_free(qdev, release);
74 }
75
76 /* release needs to be reserved at this point */
77 static int
make_drawable(struct qxl_device * qdev,int surface,uint8_t type,const struct qxl_rect * rect,struct qxl_release * release)78 make_drawable(struct qxl_device *qdev, int surface, uint8_t type,
79 const struct qxl_rect *rect,
80 struct qxl_release *release)
81 {
82 struct qxl_drawable *drawable;
83 int i;
84
85 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
86 if (!drawable)
87 return -ENOMEM;
88
89 drawable->type = type;
90
91 drawable->surface_id = surface; /* Only primary for now */
92 drawable->effect = QXL_EFFECT_OPAQUE;
93 drawable->self_bitmap = 0;
94 drawable->self_bitmap_area.top = 0;
95 drawable->self_bitmap_area.left = 0;
96 drawable->self_bitmap_area.bottom = 0;
97 drawable->self_bitmap_area.right = 0;
98 /* FIXME: add clipping */
99 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
100
101 /*
102 * surfaces_dest[i] should apparently be filled out with the
103 * surfaces that we depend on, and surface_rects should be
104 * filled with the rectangles of those surfaces that we
105 * are going to use.
106 */
107 for (i = 0; i < 3; ++i)
108 drawable->surfaces_dest[i] = -1;
109
110 if (rect)
111 drawable->bbox = *rect;
112
113 drawable->mm_time = qdev->rom->mm_clock;
114 qxl_release_unmap(qdev, release, &drawable->release_info);
115 return 0;
116 }
117
118 /* push a draw command using the given clipping rectangles as
119 * the sources from the shadow framebuffer.
120 *
121 * Right now implementing with a single draw and a clip list. Clip
122 * lists are known to be a problem performance wise, this can be solved
123 * by treating them differently in the server.
124 */
qxl_draw_dirty_fb(struct qxl_device * qdev,struct drm_framebuffer * fb,struct qxl_bo * bo,unsigned int flags,unsigned int color,struct drm_clip_rect * clips,unsigned int num_clips,int inc,uint32_t dumb_shadow_offset)125 void qxl_draw_dirty_fb(struct qxl_device *qdev,
126 struct drm_framebuffer *fb,
127 struct qxl_bo *bo,
128 unsigned int flags, unsigned int color,
129 struct drm_clip_rect *clips,
130 unsigned int num_clips, int inc,
131 uint32_t dumb_shadow_offset)
132 {
133 /*
134 * TODO: if flags & DRM_MODE_FB_DIRTY_ANNOTATE_FILL then we should
135 * send a fill command instead, much cheaper.
136 *
137 * See include/drm/drm_mode.h
138 */
139 struct drm_clip_rect *clips_ptr;
140 int i;
141 int left, right, top, bottom;
142 int width, height;
143 struct qxl_drawable *drawable;
144 struct qxl_rect drawable_rect;
145 struct qxl_rect *rects;
146 int stride = fb->pitches[0];
147 /* depth is not actually interesting, we don't mask with it */
148 int depth = fb->format->cpp[0] * 8;
149 struct iosys_map surface_map;
150 uint8_t *surface_base;
151 struct qxl_release *release;
152 struct qxl_bo *clips_bo;
153 struct qxl_drm_image *dimage;
154 int ret;
155
156 ret = alloc_drawable(qdev, &release);
157 if (ret)
158 return;
159
160 clips->x1 += dumb_shadow_offset;
161 clips->x2 += dumb_shadow_offset;
162
163 left = clips->x1;
164 right = clips->x2;
165 top = clips->y1;
166 bottom = clips->y2;
167
168 /* skip the first clip rect */
169 for (i = 1, clips_ptr = clips + inc;
170 i < num_clips; i++, clips_ptr += inc) {
171 left = min_t(int, left, (int)clips_ptr->x1);
172 right = max_t(int, right, (int)clips_ptr->x2);
173 top = min_t(int, top, (int)clips_ptr->y1);
174 bottom = max_t(int, bottom, (int)clips_ptr->y2);
175 }
176
177 width = right - left;
178 height = bottom - top;
179
180 ret = alloc_clips(qdev, release, num_clips, &clips_bo);
181 if (ret)
182 goto out_free_drawable;
183
184 ret = qxl_image_alloc_objects(qdev, release,
185 &dimage,
186 height, stride);
187 if (ret)
188 goto out_free_clips;
189
190 /* do a reservation run over all the objects we just allocated */
191 ret = qxl_release_reserve_list(release, true);
192 if (ret)
193 goto out_free_image;
194
195 drawable_rect.left = left;
196 drawable_rect.right = right;
197 drawable_rect.top = top;
198 drawable_rect.bottom = bottom;
199
200 ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &drawable_rect,
201 release);
202 if (ret)
203 goto out_release_backoff;
204
205 ret = qxl_bo_vmap_locked(bo, &surface_map);
206 if (ret)
207 goto out_release_backoff;
208 surface_base = surface_map.vaddr; /* TODO: Use mapping abstraction properly */
209
210 ret = qxl_image_init(qdev, release, dimage, surface_base,
211 left - dumb_shadow_offset,
212 top, width, height, depth, stride);
213 qxl_bo_vunmap_locked(bo);
214 if (ret)
215 goto out_release_backoff;
216
217 rects = drawable_set_clipping(qdev, num_clips, clips_bo);
218 if (!rects) {
219 ret = -EINVAL;
220 goto out_release_backoff;
221 }
222 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
223
224 drawable->clip.type = SPICE_CLIP_TYPE_RECTS;
225 drawable->clip.data = qxl_bo_physical_address(qdev,
226 clips_bo, 0);
227
228 drawable->u.copy.src_area.top = 0;
229 drawable->u.copy.src_area.bottom = height;
230 drawable->u.copy.src_area.left = 0;
231 drawable->u.copy.src_area.right = width;
232
233 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
234 drawable->u.copy.scale_mode = 0;
235 drawable->u.copy.mask.flags = 0;
236 drawable->u.copy.mask.pos.x = 0;
237 drawable->u.copy.mask.pos.y = 0;
238 drawable->u.copy.mask.bitmap = 0;
239
240 drawable->u.copy.src_bitmap = qxl_bo_physical_address(qdev, dimage->bo, 0);
241 qxl_release_unmap(qdev, release, &drawable->release_info);
242
243 clips_ptr = clips;
244 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
245 rects[i].left = clips_ptr->x1;
246 rects[i].right = clips_ptr->x2;
247 rects[i].top = clips_ptr->y1;
248 rects[i].bottom = clips_ptr->y2;
249 }
250 qxl_bo_vunmap_locked(clips_bo);
251
252 qxl_release_fence_buffer_objects(release);
253 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
254
255 out_release_backoff:
256 if (ret)
257 qxl_release_backoff_reserve_list(release);
258 out_free_image:
259 qxl_image_free_objects(qdev, dimage);
260 out_free_clips:
261 qxl_bo_unref(&clips_bo);
262 out_free_drawable:
263 /* only free drawable on error */
264 if (ret)
265 free_drawable(qdev, release);
266
267 }
268