2 * Copyright 2018 Chromium.
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 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.
24 #include "util/u_box.h"
25 #include "util/u_inlines.h"
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"
34 struct list_action_args
37 struct virgl_transfer
*queued
;
38 struct virgl_transfer
*current
;
41 typedef bool (*compare_transfers_t
)(struct virgl_transfer
*queued
,
42 struct virgl_transfer
*current
);
44 typedef void (*list_action_t
)(struct virgl_transfer_queue
*queue
,
45 struct list_action_args
*args
);
47 struct list_iteration_args
51 compare_transfers_t compare
;
52 struct virgl_transfer
*current
;
53 enum virgl_transfer_queue_lists type
;
57 transfer_dim(const struct virgl_transfer
*xfer
)
59 switch (xfer
->base
.resource
->target
) {
64 case PIPE_TEXTURE_RECT
:
72 box_min_max(const struct pipe_box
*box
, int dim
, int *min
, int *max
)
78 *max
= box
->x
+ box
->width
;
81 *min
= box
->x
+ box
->width
;
85 if (box
->height
> 0) {
87 *max
= box
->y
+ box
->height
;
90 *min
= box
->y
+ box
->height
;
96 *max
= box
->z
+ box
->depth
;
99 *min
= box
->z
+ box
->depth
;
106 transfer_overlap(const struct virgl_transfer
*xfer
,
107 const struct virgl_hw_res
*hw_res
,
109 const struct pipe_box
*box
,
110 bool include_touching
)
112 const int dim_count
= transfer_dim(xfer
);
114 if (xfer
->hw_res
!= hw_res
|| xfer
->base
.level
!= level
)
117 for (int dim
= 0; dim
< dim_count
; dim
++) {
123 box_min_max(&xfer
->base
.box
, dim
, &xfer_min
, &xfer_max
);
124 box_min_max(box
, dim
, &box_min
, &box_max
);
126 if (include_touching
) {
127 /* touching is considered overlapping */
128 if (xfer_min
> box_max
|| xfer_max
< box_min
)
131 /* touching is not considered overlapping */
132 if (xfer_min
>= box_max
|| xfer_max
<= box_min
)
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
,
144 const struct pipe_box
*box
,
145 bool include_touching
)
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
))
156 static bool transfers_intersect(struct virgl_transfer
*queued
,
157 struct virgl_transfer
*current
)
159 return transfer_overlap(queued
, current
->hw_res
, current
->base
.level
,
160 ¤t
->base
.box
, true);
163 static void remove_transfer(struct virgl_transfer_queue
*queue
,
164 struct list_action_args
*args
)
166 struct virgl_transfer
*queued
= args
->queued
;
167 list_del(&queued
->queue_link
);
168 virgl_resource_destroy_transfer(queue
->vctx
, queued
);
171 static void replace_unmapped_transfer(struct virgl_transfer_queue
*queue
,
172 struct list_action_args
*args
)
174 struct virgl_transfer
*current
= args
->current
;
175 struct virgl_transfer
*queued
= args
->queued
;
177 u_box_union_2d(¤t
->base
.box
, ¤t
->base
.box
, &queued
->base
.box
);
178 current
->offset
= current
->base
.box
.x
;
180 remove_transfer(queue
, args
);
181 queue
->num_dwords
-= (VIRGL_TRANSFER3D_SIZE
+ 1);
184 static void transfer_put(struct virgl_transfer_queue
*queue
,
185 struct list_action_args
*args
)
187 struct virgl_transfer
*queued
= args
->queued
;
189 queue
->vs
->vws
->transfer_put(queue
->vs
->vws
, queued
->hw_res
,
191 queued
->base
.stride
, queued
->l_stride
,
192 queued
->offset
, queued
->base
.level
);
194 remove_transfer(queue
, args
);
197 static void transfer_write(struct virgl_transfer_queue
*queue
,
198 struct list_action_args
*args
)
200 struct virgl_transfer
*queued
= args
->queued
;
201 struct virgl_cmd_buf
*buf
= args
->data
;
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
);
207 remove_transfer(queue
, args
);
210 static void compare_and_perform_action(struct virgl_transfer_queue
*queue
,
211 struct list_iteration_args
*iter
)
213 struct list_action_args args
;
214 struct virgl_transfer
*queued
, *tmp
;
215 enum virgl_transfer_queue_lists type
= iter
->type
;
217 memset(&args
, 0, sizeof(args
));
218 args
.current
= iter
->current
;
219 args
.data
= iter
->data
;
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
);
229 static void perform_action(struct virgl_transfer_queue
*queue
,
230 struct list_iteration_args
*iter
)
232 struct list_action_args args
;
233 struct virgl_transfer
*queued
, *tmp
;
234 enum virgl_transfer_queue_lists type
= iter
->type
;
236 memset(&args
, 0, sizeof(args
));
237 args
.data
= iter
->data
;
239 LIST_FOR_EACH_ENTRY_SAFE(queued
, tmp
, &queue
->lists
[type
], queue_link
) {
240 args
.queued
= queued
;
241 iter
->action(queue
, &args
);
245 static void add_internal(struct virgl_transfer_queue
*queue
,
246 struct virgl_transfer
*transfer
)
248 uint32_t dwords
= VIRGL_TRANSFER3D_SIZE
+ 1;
250 if (queue
->num_dwords
+ dwords
>= VIRGL_MAX_TBUF_DWORDS
) {
251 struct list_iteration_args iter
;
252 struct virgl_winsys
*vws
= queue
->vs
->vws
;
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
);
260 vws
->submit_cmd(vws
, queue
->tbuf
, NULL
);
261 queue
->num_dwords
= 0;
265 list_addtail(&transfer
->queue_link
, &queue
->lists
[PENDING_LIST
]);
266 queue
->num_dwords
+= dwords
;
270 void virgl_transfer_queue_init(struct virgl_transfer_queue
*queue
,
271 struct virgl_context
*vctx
)
273 struct virgl_screen
*vs
= virgl_screen(vctx
->base
.screen
);
277 queue
->num_dwords
= 0;
279 for (uint32_t i
= 0; i
< MAX_LISTS
; i
++)
280 list_inithead(&queue
->lists
[i
]);
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
);
289 void virgl_transfer_queue_fini(struct virgl_transfer_queue
*queue
)
291 struct virgl_winsys
*vws
= queue
->vs
->vws
;
292 struct list_iteration_args iter
;
294 memset(&iter
, 0, sizeof(iter
));
296 iter
.action
= transfer_put
;
297 iter
.type
= PENDING_LIST
;
298 perform_action(queue
, &iter
);
300 iter
.action
= remove_transfer
;
301 iter
.type
= COMPLETED_LIST
;
302 perform_action(queue
, &iter
);
305 vws
->cmd_buf_destroy(queue
->tbuf
);
310 queue
->num_dwords
= 0;
313 int virgl_transfer_queue_unmap(struct virgl_transfer_queue
*queue
,
314 struct virgl_transfer
*transfer
)
316 struct list_iteration_args iter
;
318 /* We don't support copy transfers in the transfer queue. */
319 assert(!transfer
->copy_src_hw_res
);
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
);
331 add_internal(queue
, transfer
);
335 int virgl_transfer_queue_clear(struct virgl_transfer_queue
*queue
,
336 struct virgl_cmd_buf
*cbuf
)
338 struct list_iteration_args iter
;
340 memset(&iter
, 0, sizeof(iter
));
341 iter
.type
= PENDING_LIST
;
343 uint32_t prior_num_dwords
= cbuf
->cdw
;
346 iter
.action
= transfer_write
;
348 perform_action(queue
, &iter
);
350 virgl_encode_end_transfers(cbuf
);
351 cbuf
->cdw
= prior_num_dwords
;
353 iter
.action
= transfer_put
;
354 perform_action(queue
, &iter
);
357 iter
.action
= remove_transfer
;
358 iter
.type
= COMPLETED_LIST
;
359 perform_action(queue
, &iter
);
360 queue
->num_dwords
= 0;
365 bool virgl_transfer_queue_is_queued(struct virgl_transfer_queue
*queue
,
366 struct virgl_transfer
*transfer
)
368 return virgl_transfer_queue_find_pending(queue
,
370 transfer
->base
.level
,
375 struct virgl_transfer
*
376 virgl_transfer_queue_extend(struct virgl_transfer_queue
*queue
,
377 struct virgl_transfer
*transfer
)
379 struct virgl_transfer
*queued
= NULL
;
381 /* We don't support extending from copy transfers. */
382 assert(!transfer
->copy_src_hw_res
);
384 if (transfer
->base
.resource
->target
== PIPE_BUFFER
) {
385 queued
= virgl_transfer_queue_find_pending(queue
,
387 transfer
->base
.level
,
393 u_box_union_2d(&queued
->base
.box
, &queued
->base
.box
, &transfer
->base
.box
);
394 queued
->offset
= queued
->base
.box
.x
;