1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright 2011 Red Hat, Inc.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun * on the rights to use, copy, modify, merge, publish, distribute, sub
8*4882a593Smuzhiyun * license, and/or sell copies of the Software, and to permit persons to whom
9*4882a593Smuzhiyun * the Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun * Software.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18*4882a593Smuzhiyun * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19*4882a593Smuzhiyun * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20*4882a593Smuzhiyun * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include <drm/drm_fourcc.h>
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #include "qxl_drv.h"
26*4882a593Smuzhiyun #include "qxl_object.h"
27*4882a593Smuzhiyun
alloc_clips(struct qxl_device * qdev,struct qxl_release * release,unsigned int num_clips,struct qxl_bo ** clips_bo)28*4882a593Smuzhiyun static int alloc_clips(struct qxl_device *qdev,
29*4882a593Smuzhiyun struct qxl_release *release,
30*4882a593Smuzhiyun unsigned int num_clips,
31*4882a593Smuzhiyun struct qxl_bo **clips_bo)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun int size = sizeof(struct qxl_clip_rects) + sizeof(struct qxl_rect) * num_clips;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun return qxl_alloc_bo_reserved(qdev, release, size, clips_bo);
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /* returns a pointer to the already allocated qxl_rect array inside
39*4882a593Smuzhiyun * the qxl_clip_rects. This is *not* the same as the memory allocated
40*4882a593Smuzhiyun * 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)41*4882a593Smuzhiyun static struct qxl_rect *drawable_set_clipping(struct qxl_device *qdev,
42*4882a593Smuzhiyun unsigned int num_clips,
43*4882a593Smuzhiyun struct qxl_bo *clips_bo)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun struct qxl_clip_rects *dev_clips;
46*4882a593Smuzhiyun int ret;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun ret = qxl_bo_kmap(clips_bo, (void **)&dev_clips);
49*4882a593Smuzhiyun if (ret) {
50*4882a593Smuzhiyun return NULL;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun dev_clips->num_rects = num_clips;
53*4882a593Smuzhiyun dev_clips->chunk.next_chunk = 0;
54*4882a593Smuzhiyun dev_clips->chunk.prev_chunk = 0;
55*4882a593Smuzhiyun dev_clips->chunk.data_size = sizeof(struct qxl_rect) * num_clips;
56*4882a593Smuzhiyun return (struct qxl_rect *)dev_clips->chunk.data;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun static int
alloc_drawable(struct qxl_device * qdev,struct qxl_release ** release)60*4882a593Smuzhiyun alloc_drawable(struct qxl_device *qdev, struct qxl_release **release)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun return qxl_alloc_release_reserved(qdev, sizeof(struct qxl_drawable),
63*4882a593Smuzhiyun QXL_RELEASE_DRAWABLE, release, NULL);
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun static void
free_drawable(struct qxl_device * qdev,struct qxl_release * release)67*4882a593Smuzhiyun free_drawable(struct qxl_device *qdev, struct qxl_release *release)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun qxl_release_free(qdev, release);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /* release needs to be reserved at this point */
73*4882a593Smuzhiyun static int
make_drawable(struct qxl_device * qdev,int surface,uint8_t type,const struct qxl_rect * rect,struct qxl_release * release)74*4882a593Smuzhiyun make_drawable(struct qxl_device *qdev, int surface, uint8_t type,
75*4882a593Smuzhiyun const struct qxl_rect *rect,
76*4882a593Smuzhiyun struct qxl_release *release)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun struct qxl_drawable *drawable;
79*4882a593Smuzhiyun int i;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
82*4882a593Smuzhiyun if (!drawable)
83*4882a593Smuzhiyun return -ENOMEM;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun drawable->type = type;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun drawable->surface_id = surface; /* Only primary for now */
88*4882a593Smuzhiyun drawable->effect = QXL_EFFECT_OPAQUE;
89*4882a593Smuzhiyun drawable->self_bitmap = 0;
90*4882a593Smuzhiyun drawable->self_bitmap_area.top = 0;
91*4882a593Smuzhiyun drawable->self_bitmap_area.left = 0;
92*4882a593Smuzhiyun drawable->self_bitmap_area.bottom = 0;
93*4882a593Smuzhiyun drawable->self_bitmap_area.right = 0;
94*4882a593Smuzhiyun /* FIXME: add clipping */
95*4882a593Smuzhiyun drawable->clip.type = SPICE_CLIP_TYPE_NONE;
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun * surfaces_dest[i] should apparently be filled out with the
99*4882a593Smuzhiyun * surfaces that we depend on, and surface_rects should be
100*4882a593Smuzhiyun * filled with the rectangles of those surfaces that we
101*4882a593Smuzhiyun * are going to use.
102*4882a593Smuzhiyun */
103*4882a593Smuzhiyun for (i = 0; i < 3; ++i)
104*4882a593Smuzhiyun drawable->surfaces_dest[i] = -1;
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun if (rect)
107*4882a593Smuzhiyun drawable->bbox = *rect;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun drawable->mm_time = qdev->rom->mm_clock;
110*4882a593Smuzhiyun qxl_release_unmap(qdev, release, &drawable->release_info);
111*4882a593Smuzhiyun return 0;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* push a draw command using the given clipping rectangles as
115*4882a593Smuzhiyun * the sources from the shadow framebuffer.
116*4882a593Smuzhiyun *
117*4882a593Smuzhiyun * Right now implementing with a single draw and a clip list. Clip
118*4882a593Smuzhiyun * lists are known to be a problem performance wise, this can be solved
119*4882a593Smuzhiyun * by treating them differently in the server.
120*4882a593Smuzhiyun */
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)121*4882a593Smuzhiyun void qxl_draw_dirty_fb(struct qxl_device *qdev,
122*4882a593Smuzhiyun struct drm_framebuffer *fb,
123*4882a593Smuzhiyun struct qxl_bo *bo,
124*4882a593Smuzhiyun unsigned int flags, unsigned int color,
125*4882a593Smuzhiyun struct drm_clip_rect *clips,
126*4882a593Smuzhiyun unsigned int num_clips, int inc,
127*4882a593Smuzhiyun uint32_t dumb_shadow_offset)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun /*
130*4882a593Smuzhiyun * TODO: if flags & DRM_MODE_FB_DIRTY_ANNOTATE_FILL then we should
131*4882a593Smuzhiyun * send a fill command instead, much cheaper.
132*4882a593Smuzhiyun *
133*4882a593Smuzhiyun * See include/drm/drm_mode.h
134*4882a593Smuzhiyun */
135*4882a593Smuzhiyun struct drm_clip_rect *clips_ptr;
136*4882a593Smuzhiyun int i;
137*4882a593Smuzhiyun int left, right, top, bottom;
138*4882a593Smuzhiyun int width, height;
139*4882a593Smuzhiyun struct qxl_drawable *drawable;
140*4882a593Smuzhiyun struct qxl_rect drawable_rect;
141*4882a593Smuzhiyun struct qxl_rect *rects;
142*4882a593Smuzhiyun int stride = fb->pitches[0];
143*4882a593Smuzhiyun /* depth is not actually interesting, we don't mask with it */
144*4882a593Smuzhiyun int depth = fb->format->cpp[0] * 8;
145*4882a593Smuzhiyun uint8_t *surface_base;
146*4882a593Smuzhiyun struct qxl_release *release;
147*4882a593Smuzhiyun struct qxl_bo *clips_bo;
148*4882a593Smuzhiyun struct qxl_drm_image *dimage;
149*4882a593Smuzhiyun int ret;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun ret = alloc_drawable(qdev, &release);
152*4882a593Smuzhiyun if (ret)
153*4882a593Smuzhiyun return;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun clips->x1 += dumb_shadow_offset;
156*4882a593Smuzhiyun clips->x2 += dumb_shadow_offset;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun left = clips->x1;
159*4882a593Smuzhiyun right = clips->x2;
160*4882a593Smuzhiyun top = clips->y1;
161*4882a593Smuzhiyun bottom = clips->y2;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun /* skip the first clip rect */
164*4882a593Smuzhiyun for (i = 1, clips_ptr = clips + inc;
165*4882a593Smuzhiyun i < num_clips; i++, clips_ptr += inc) {
166*4882a593Smuzhiyun left = min_t(int, left, (int)clips_ptr->x1);
167*4882a593Smuzhiyun right = max_t(int, right, (int)clips_ptr->x2);
168*4882a593Smuzhiyun top = min_t(int, top, (int)clips_ptr->y1);
169*4882a593Smuzhiyun bottom = max_t(int, bottom, (int)clips_ptr->y2);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun width = right - left;
173*4882a593Smuzhiyun height = bottom - top;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun ret = alloc_clips(qdev, release, num_clips, &clips_bo);
176*4882a593Smuzhiyun if (ret)
177*4882a593Smuzhiyun goto out_free_drawable;
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun ret = qxl_image_alloc_objects(qdev, release,
180*4882a593Smuzhiyun &dimage,
181*4882a593Smuzhiyun height, stride);
182*4882a593Smuzhiyun if (ret)
183*4882a593Smuzhiyun goto out_free_clips;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /* do a reservation run over all the objects we just allocated */
186*4882a593Smuzhiyun ret = qxl_release_reserve_list(release, true);
187*4882a593Smuzhiyun if (ret)
188*4882a593Smuzhiyun goto out_free_image;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun drawable_rect.left = left;
191*4882a593Smuzhiyun drawable_rect.right = right;
192*4882a593Smuzhiyun drawable_rect.top = top;
193*4882a593Smuzhiyun drawable_rect.bottom = bottom;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &drawable_rect,
196*4882a593Smuzhiyun release);
197*4882a593Smuzhiyun if (ret)
198*4882a593Smuzhiyun goto out_release_backoff;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun ret = qxl_bo_kmap(bo, (void **)&surface_base);
201*4882a593Smuzhiyun if (ret)
202*4882a593Smuzhiyun goto out_release_backoff;
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun ret = qxl_image_init(qdev, release, dimage, surface_base,
205*4882a593Smuzhiyun left - dumb_shadow_offset,
206*4882a593Smuzhiyun top, width, height, depth, stride);
207*4882a593Smuzhiyun qxl_bo_kunmap(bo);
208*4882a593Smuzhiyun if (ret)
209*4882a593Smuzhiyun goto out_release_backoff;
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun rects = drawable_set_clipping(qdev, num_clips, clips_bo);
212*4882a593Smuzhiyun if (!rects) {
213*4882a593Smuzhiyun ret = -EINVAL;
214*4882a593Smuzhiyun goto out_release_backoff;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun drawable->clip.type = SPICE_CLIP_TYPE_RECTS;
219*4882a593Smuzhiyun drawable->clip.data = qxl_bo_physical_address(qdev,
220*4882a593Smuzhiyun clips_bo, 0);
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun drawable->u.copy.src_area.top = 0;
223*4882a593Smuzhiyun drawable->u.copy.src_area.bottom = height;
224*4882a593Smuzhiyun drawable->u.copy.src_area.left = 0;
225*4882a593Smuzhiyun drawable->u.copy.src_area.right = width;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
228*4882a593Smuzhiyun drawable->u.copy.scale_mode = 0;
229*4882a593Smuzhiyun drawable->u.copy.mask.flags = 0;
230*4882a593Smuzhiyun drawable->u.copy.mask.pos.x = 0;
231*4882a593Smuzhiyun drawable->u.copy.mask.pos.y = 0;
232*4882a593Smuzhiyun drawable->u.copy.mask.bitmap = 0;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun drawable->u.copy.src_bitmap = qxl_bo_physical_address(qdev, dimage->bo, 0);
235*4882a593Smuzhiyun qxl_release_unmap(qdev, release, &drawable->release_info);
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun clips_ptr = clips;
238*4882a593Smuzhiyun for (i = 0; i < num_clips; i++, clips_ptr += inc) {
239*4882a593Smuzhiyun rects[i].left = clips_ptr->x1;
240*4882a593Smuzhiyun rects[i].right = clips_ptr->x2;
241*4882a593Smuzhiyun rects[i].top = clips_ptr->y1;
242*4882a593Smuzhiyun rects[i].bottom = clips_ptr->y2;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun qxl_bo_kunmap(clips_bo);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun qxl_release_fence_buffer_objects(release);
247*4882a593Smuzhiyun qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun out_release_backoff:
250*4882a593Smuzhiyun if (ret)
251*4882a593Smuzhiyun qxl_release_backoff_reserve_list(release);
252*4882a593Smuzhiyun out_free_image:
253*4882a593Smuzhiyun qxl_image_free_objects(qdev, dimage);
254*4882a593Smuzhiyun out_free_clips:
255*4882a593Smuzhiyun qxl_bo_unref(&clips_bo);
256*4882a593Smuzhiyun out_free_drawable:
257*4882a593Smuzhiyun /* only free drawable on error */
258*4882a593Smuzhiyun if (ret)
259*4882a593Smuzhiyun free_drawable(qdev, release);
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun }
262