virgl: simplify virgl_transfer_queue_extend
[mesa.git] / src / gallium / drivers / virgl / virgl_transfer_queue.c
1 /*
2 * Copyright 2018 Chromium.
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 AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "util/u_box.h"
25 #include "util/u_inlines.h"
26
27 #include "virgl_protocol.h"
28 #include "virgl_context.h"
29 #include "virgl_screen.h"
30 #include "virgl_encode.h"
31 #include "virgl_resource.h"
32 #include "virgl_transfer_queue.h"
33
34 struct list_action_args
35 {
36 void *data;
37 struct virgl_transfer *queued;
38 struct virgl_transfer *current;
39 };
40
41 typedef bool (*compare_transfers_t)(struct virgl_transfer *queued,
42 struct virgl_transfer *current);
43
44 typedef void (*list_action_t)(struct virgl_transfer_queue *queue,
45 struct list_action_args *args);
46
47 struct list_iteration_args
48 {
49 void *data;
50 list_action_t action;
51 compare_transfers_t compare;
52 struct virgl_transfer *current;
53 enum virgl_transfer_queue_lists type;
54 };
55
56 static int
57 transfer_dim(const struct virgl_transfer *xfer)
58 {
59 switch (xfer->base.resource->target) {
60 case PIPE_BUFFER:
61 case PIPE_TEXTURE_1D:
62 return 1;
63 case PIPE_TEXTURE_2D:
64 case PIPE_TEXTURE_RECT:
65 return 2;
66 default:
67 return 3;
68 }
69 }
70
71 static void
72 box_min_max(const struct pipe_box *box, int dim, int *min, int *max)
73 {
74 switch (dim) {
75 case 0:
76 if (box->width > 0) {
77 *min = box->x;
78 *max = box->x + box->width;
79 } else {
80 *max = box->x;
81 *min = box->x + box->width;
82 }
83 break;
84 case 1:
85 if (box->height > 0) {
86 *min = box->y;
87 *max = box->y + box->height;
88 } else {
89 *max = box->y;
90 *min = box->y + box->height;
91 }
92 break;
93 default:
94 if (box->depth > 0) {
95 *min = box->z;
96 *max = box->z + box->depth;
97 } else {
98 *max = box->z;
99 *min = box->z + box->depth;
100 }
101 break;
102 }
103 }
104
105 static bool
106 transfer_overlap(const struct virgl_transfer *xfer,
107 const struct virgl_hw_res *hw_res,
108 unsigned level,
109 const struct pipe_box *box,
110 bool include_touching)
111 {
112 const int dim_count = transfer_dim(xfer);
113
114 if (xfer->hw_res != hw_res || xfer->base.level != level)
115 return false;
116
117 for (int dim = 0; dim < dim_count; dim++) {
118 int xfer_min;
119 int xfer_max;
120 int box_min;
121 int box_max;
122
123 box_min_max(&xfer->base.box, dim, &xfer_min, &xfer_max);
124 box_min_max(box, dim, &box_min, &box_max);
125
126 if (include_touching) {
127 /* touching is considered overlapping */
128 if (xfer_min > box_max || xfer_max < box_min)
129 return false;
130 } else {
131 /* touching is not considered overlapping */
132 if (xfer_min >= box_max || xfer_max <= box_min)
133 return false;
134 }
135 }
136
137 return true;
138 }
139
140 static struct virgl_transfer *
141 virgl_transfer_queue_find_pending(const struct virgl_transfer_queue *queue,
142 const struct virgl_hw_res *hw_res,
143 unsigned level,
144 const struct pipe_box *box,
145 bool include_touching)
146 {
147 struct virgl_transfer *xfer;
148 LIST_FOR_EACH_ENTRY(xfer, &queue->lists[PENDING_LIST], queue_link) {
149 if (transfer_overlap(xfer, hw_res, level, box, include_touching))
150 return xfer;
151 }
152
153 return NULL;
154 }
155
156 static bool transfers_intersect(struct virgl_transfer *queued,
157 struct virgl_transfer *current)
158 {
159 return transfer_overlap(queued, current->hw_res, current->base.level,
160 &current->base.box, true);
161 }
162
163 static void remove_transfer(struct virgl_transfer_queue *queue,
164 struct list_action_args *args)
165 {
166 struct virgl_transfer *queued = args->queued;
167 list_del(&queued->queue_link);
168 virgl_resource_destroy_transfer(queue->vctx, queued);
169 }
170
171 static void replace_unmapped_transfer(struct virgl_transfer_queue *queue,
172 struct list_action_args *args)
173 {
174 struct virgl_transfer *current = args->current;
175 struct virgl_transfer *queued = args->queued;
176
177 u_box_union_2d(&current->base.box, &current->base.box, &queued->base.box);
178 current->offset = current->base.box.x;
179
180 remove_transfer(queue, args);
181 queue->num_dwords -= (VIRGL_TRANSFER3D_SIZE + 1);
182 }
183
184 static void transfer_put(struct virgl_transfer_queue *queue,
185 struct list_action_args *args)
186 {
187 struct virgl_transfer *queued = args->queued;
188
189 queue->vs->vws->transfer_put(queue->vs->vws, queued->hw_res,
190 &queued->base.box,
191 queued->base.stride, queued->l_stride,
192 queued->offset, queued->base.level);
193
194 remove_transfer(queue, args);
195 }
196
197 static void transfer_write(struct virgl_transfer_queue *queue,
198 struct list_action_args *args)
199 {
200 struct virgl_transfer *queued = args->queued;
201 struct virgl_cmd_buf *buf = args->data;
202
203 // Takes a reference on the HW resource, which is released after
204 // the exec buffer command.
205 virgl_encode_transfer(queue->vs, buf, queued, VIRGL_TRANSFER_TO_HOST);
206
207 remove_transfer(queue, args);
208 }
209
210 static void compare_and_perform_action(struct virgl_transfer_queue *queue,
211 struct list_iteration_args *iter)
212 {
213 struct list_action_args args;
214 struct virgl_transfer *queued, *tmp;
215 enum virgl_transfer_queue_lists type = iter->type;
216
217 memset(&args, 0, sizeof(args));
218 args.current = iter->current;
219 args.data = iter->data;
220
221 LIST_FOR_EACH_ENTRY_SAFE(queued, tmp, &queue->lists[type], queue_link) {
222 if (iter->compare(queued, iter->current)) {
223 args.queued = queued;
224 iter->action(queue, &args);
225 }
226 }
227 }
228
229 static void perform_action(struct virgl_transfer_queue *queue,
230 struct list_iteration_args *iter)
231 {
232 struct list_action_args args;
233 struct virgl_transfer *queued, *tmp;
234 enum virgl_transfer_queue_lists type = iter->type;
235
236 memset(&args, 0, sizeof(args));
237 args.data = iter->data;
238
239 LIST_FOR_EACH_ENTRY_SAFE(queued, tmp, &queue->lists[type], queue_link) {
240 args.queued = queued;
241 iter->action(queue, &args);
242 }
243 }
244
245 static void add_internal(struct virgl_transfer_queue *queue,
246 struct virgl_transfer *transfer)
247 {
248 uint32_t dwords = VIRGL_TRANSFER3D_SIZE + 1;
249 if (queue->tbuf) {
250 if (queue->num_dwords + dwords >= VIRGL_MAX_TBUF_DWORDS) {
251 struct list_iteration_args iter;
252 struct virgl_winsys *vws = queue->vs->vws;
253
254 memset(&iter, 0, sizeof(iter));
255 iter.type = PENDING_LIST;
256 iter.action = transfer_write;
257 iter.data = queue->tbuf;
258 perform_action(queue, &iter);
259
260 vws->submit_cmd(vws, queue->tbuf, NULL);
261 queue->num_dwords = 0;
262 }
263 }
264
265 list_addtail(&transfer->queue_link, &queue->lists[PENDING_LIST]);
266 queue->num_dwords += dwords;
267 }
268
269
270 void virgl_transfer_queue_init(struct virgl_transfer_queue *queue,
271 struct virgl_context *vctx)
272 {
273 struct virgl_screen *vs = virgl_screen(vctx->base.screen);
274
275 queue->vs = vs;
276 queue->vctx = vctx;
277 queue->num_dwords = 0;
278
279 for (uint32_t i = 0; i < MAX_LISTS; i++)
280 list_inithead(&queue->lists[i]);
281
282 if ((vs->caps.caps.v2.capability_bits & VIRGL_CAP_TRANSFER) &&
283 vs->vws->supports_encoded_transfers)
284 queue->tbuf = vs->vws->cmd_buf_create(vs->vws, VIRGL_MAX_TBUF_DWORDS);
285 else
286 queue->tbuf = NULL;
287 }
288
289 void virgl_transfer_queue_fini(struct virgl_transfer_queue *queue)
290 {
291 struct virgl_winsys *vws = queue->vs->vws;
292 struct list_iteration_args iter;
293
294 memset(&iter, 0, sizeof(iter));
295
296 iter.action = transfer_put;
297 iter.type = PENDING_LIST;
298 perform_action(queue, &iter);
299
300 iter.action = remove_transfer;
301 iter.type = COMPLETED_LIST;
302 perform_action(queue, &iter);
303
304 if (queue->tbuf)
305 vws->cmd_buf_destroy(queue->tbuf);
306
307 queue->vs = NULL;
308 queue->vctx = NULL;
309 queue->tbuf = NULL;
310 queue->num_dwords = 0;
311 }
312
313 int virgl_transfer_queue_unmap(struct virgl_transfer_queue *queue,
314 struct virgl_transfer *transfer)
315 {
316 struct list_iteration_args iter;
317
318 /* We don't support copy transfers in the transfer queue. */
319 assert(!transfer->copy_src_hw_res);
320
321 /* Attempt to merge multiple intersecting transfers into a single one. */
322 if (transfer->base.resource->target == PIPE_BUFFER) {
323 memset(&iter, 0, sizeof(iter));
324 iter.current = transfer;
325 iter.compare = transfers_intersect;
326 iter.action = replace_unmapped_transfer;
327 iter.type = PENDING_LIST;
328 compare_and_perform_action(queue, &iter);
329 }
330
331 add_internal(queue, transfer);
332 return 0;
333 }
334
335 int virgl_transfer_queue_clear(struct virgl_transfer_queue *queue,
336 struct virgl_cmd_buf *cbuf)
337 {
338 struct list_iteration_args iter;
339
340 memset(&iter, 0, sizeof(iter));
341 iter.type = PENDING_LIST;
342 if (queue->tbuf) {
343 uint32_t prior_num_dwords = cbuf->cdw;
344 cbuf->cdw = 0;
345
346 iter.action = transfer_write;
347 iter.data = cbuf;
348 perform_action(queue, &iter);
349
350 virgl_encode_end_transfers(cbuf);
351 cbuf->cdw = prior_num_dwords;
352 } else {
353 iter.action = transfer_put;
354 perform_action(queue, &iter);
355 }
356
357 iter.action = remove_transfer;
358 iter.type = COMPLETED_LIST;
359 perform_action(queue, &iter);
360 queue->num_dwords = 0;
361
362 return 0;
363 }
364
365 bool virgl_transfer_queue_is_queued(struct virgl_transfer_queue *queue,
366 struct virgl_transfer *transfer)
367 {
368 return virgl_transfer_queue_find_pending(queue,
369 transfer->hw_res,
370 transfer->base.level,
371 &transfer->base.box,
372 false);
373 }
374
375 struct virgl_transfer *
376 virgl_transfer_queue_extend(struct virgl_transfer_queue *queue,
377 struct virgl_transfer *transfer)
378 {
379 struct virgl_transfer *queued = NULL;
380
381 /* We don't support extending from copy transfers. */
382 assert(!transfer->copy_src_hw_res);
383
384 if (transfer->base.resource->target == PIPE_BUFFER) {
385 queued = virgl_transfer_queue_find_pending(queue,
386 transfer->hw_res,
387 transfer->base.level,
388 &transfer->base.box,
389 true);
390 }
391
392 if (queued) {
393 u_box_union_2d(&queued->base.box, &queued->base.box, &transfer->base.box);
394 queued->offset = queued->base.box.x;
395 }
396
397 return queued;
398 }