Merge tag 'ceph-for-5.18-rc1' of https://github.com/ceph/ceph-client
[linux-block.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
7938f421 23#include <linux/iosys-map.h>
49a3f51d 24
c0f4b75c
SR
25#include <drm/drm_fourcc.h>
26
f64122c1
DA
27#include "qxl_drv.h"
28#include "qxl_object.h"
29
8002db63
DA
30static int alloc_clips(struct qxl_device *qdev,
31 struct qxl_release *release,
1b000494 32 unsigned int num_clips,
8002db63
DA
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
f64122c1
DA
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 */
43static struct qxl_rect *drawable_set_clipping(struct qxl_device *qdev,
1b000494 44 unsigned int num_clips,
8002db63 45 struct qxl_bo *clips_bo)
f64122c1 46{
7938f421 47 struct iosys_map map;
f64122c1
DA
48 struct qxl_clip_rects *dev_clips;
49 int ret;
f64122c1 50
f7ed28e1 51 ret = qxl_bo_vmap_locked(clips_bo, &map);
49a3f51d 52 if (ret)
f64122c1 53 return NULL;
49a3f51d
TZ
54 dev_clips = map.vaddr; /* TODO: Use mapping abstraction properly */
55
f64122c1
DA
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
8002db63
DA
63static int
64alloc_drawable(struct qxl_device *qdev, struct qxl_release **release)
65{
96c594ba
MY
66 return qxl_alloc_release_reserved(qdev, sizeof(struct qxl_drawable),
67 QXL_RELEASE_DRAWABLE, release, NULL);
8002db63
DA
68}
69
70static void
71free_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 */
f64122c1
DA
77static int
78make_drawable(struct qxl_device *qdev, int surface, uint8_t type,
79 const struct qxl_rect *rect,
8002db63 80 struct qxl_release *release)
f64122c1
DA
81{
82 struct qxl_drawable *drawable;
8002db63 83 int i;
f64122c1 84
8002db63
DA
85 drawable = (struct qxl_drawable *)qxl_release_map(qdev, release);
86 if (!drawable)
87 return -ENOMEM;
f64122c1 88
f64122c1
DA
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;
8002db63 114 qxl_release_unmap(qdev, release, &drawable->release_info);
f64122c1
DA
115 return 0;
116}
117
f64122c1
DA
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 */
125void qxl_draw_dirty_fb(struct qxl_device *qdev,
bf8744e4 126 struct drm_framebuffer *fb,
f64122c1 127 struct qxl_bo *bo,
1b000494 128 unsigned int flags, unsigned int color,
f64122c1 129 struct drm_clip_rect *clips,
90adda2c
GH
130 unsigned int num_clips, int inc,
131 uint32_t dumb_shadow_offset)
f64122c1
DA
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;
bf8744e4 146 int stride = fb->pitches[0];
f64122c1 147 /* depth is not actually interesting, we don't mask with it */
bf8744e4 148 int depth = fb->format->cpp[0] * 8;
7938f421 149 struct iosys_map surface_map;
f64122c1
DA
150 uint8_t *surface_base;
151 struct qxl_release *release;
f64122c1 152 struct qxl_bo *clips_bo;
8002db63 153 struct qxl_drm_image *dimage;
f64122c1
DA
154 int ret;
155
8002db63
DA
156 ret = alloc_drawable(qdev, &release);
157 if (ret)
158 return;
159
90adda2c
GH
160 clips->x1 += dumb_shadow_offset;
161 clips->x2 += dumb_shadow_offset;
162
f64122c1
DA
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;
8002db63
DA
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
f64122c1
DA
195 drawable_rect.left = left;
196 drawable_rect.right = right;
197 drawable_rect.top = top;
198 drawable_rect.bottom = bottom;
8002db63 199
f64122c1 200 ret = make_drawable(qdev, 0, QXL_DRAW_COPY, &drawable_rect,
8002db63 201 release);
f64122c1 202 if (ret)
8002db63 203 goto out_release_backoff;
f64122c1 204
f7ed28e1 205 ret = qxl_bo_vmap_locked(bo, &surface_map);
f64122c1 206 if (ret)
8002db63 207 goto out_release_backoff;
49a3f51d 208 surface_base = surface_map.vaddr; /* TODO: Use mapping abstraction properly */
f64122c1 209
8002db63 210 ret = qxl_image_init(qdev, release, dimage, surface_base,
90adda2c
GH
211 left - dumb_shadow_offset,
212 top, width, height, depth, stride);
f7ed28e1 213 qxl_bo_vunmap_locked(bo);
f64122c1 214 if (ret)
8002db63
DA
215 goto out_release_backoff;
216
6566435a 217 rects = drawable_set_clipping(qdev, num_clips, clips_bo);
85e9b88a
VA
218 if (!rects) {
219 ret = -EINVAL;
8002db63 220 goto out_release_backoff;
85e9b88a 221 }
f64122c1
DA
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);
f64122c1
DA
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
8002db63 240 drawable->u.copy.src_bitmap = qxl_bo_physical_address(qdev, dimage->bo, 0);
f64122c1 241 qxl_release_unmap(qdev, release, &drawable->release_info);
8002db63 242
f64122c1
DA
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 }
f7ed28e1 250 qxl_bo_vunmap_locked(clips_bo);
f64122c1 251
8002db63 252 qxl_release_fence_buffer_objects(release);
933db733 253 qxl_push_command_ring_release(qdev, release, QXL_CMD_DRAW, false);
8002db63
DA
254
255out_release_backoff:
256 if (ret)
257 qxl_release_backoff_reserve_list(release);
258out_free_image:
259 qxl_image_free_objects(qdev, dimage);
260out_free_clips:
261 qxl_bo_unref(&clips_bo);
262out_free_drawable:
263 /* only free drawable on error */
264 if (ret)
265 free_drawable(qdev, release);
f64122c1 266
f64122c1 267}