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
;
56 transfer_dim(const struct virgl_transfer
*xfer
)
58 switch (xfer
->base
.resource
->target
) {
63 case PIPE_TEXTURE_RECT
:
71 box_min_max(const struct pipe_box
*box
, int dim
, int *min
, int *max
)
77 *max
= box
->x
+ box
->width
;
80 *min
= box
->x
+ box
->width
;
84 if (box
->height
> 0) {
86 *max
= box
->y
+ box
->height
;
89 *min
= box
->y
+ box
->height
;
95 *max
= box
->z
+ box
->depth
;
98 *min
= box
->z
+ box
->depth
;
105 transfer_overlap(const struct virgl_transfer
*xfer
,
106 const struct virgl_hw_res
*hw_res
,
108 const struct pipe_box
*box
,
109 bool include_touching
)
111 const int dim_count
= transfer_dim(xfer
);
113 if (xfer
->hw_res
!= hw_res
|| xfer
->base
.level
!= level
)
116 for (int dim
= 0; dim
< dim_count
; dim
++) {
122 box_min_max(&xfer
->base
.box
, dim
, &xfer_min
, &xfer_max
);
123 box_min_max(box
, dim
, &box_min
, &box_max
);
125 if (include_touching
) {
126 /* touching is considered overlapping */
127 if (xfer_min
> box_max
|| xfer_max
< box_min
)
130 /* touching is not considered overlapping */
131 if (xfer_min
>= box_max
|| xfer_max
<= box_min
)
139 static struct virgl_transfer
*
140 virgl_transfer_queue_find_overlap(const struct virgl_transfer_queue
*queue
,
141 const struct virgl_hw_res
*hw_res
,
143 const struct pipe_box
*box
,
144 bool include_touching
)
146 struct virgl_transfer
*xfer
;
147 LIST_FOR_EACH_ENTRY(xfer
, &queue
->transfer_list
, queue_link
) {
148 if (transfer_overlap(xfer
, hw_res
, level
, box
, include_touching
))
155 static bool transfers_intersect(struct virgl_transfer
*queued
,
156 struct virgl_transfer
*current
)
158 return transfer_overlap(queued
, current
->hw_res
, current
->base
.level
,
159 ¤t
->base
.box
, true);
162 static void remove_transfer(struct virgl_transfer_queue
*queue
,
163 struct virgl_transfer
*queued
)
165 list_del(&queued
->queue_link
);
166 virgl_resource_destroy_transfer(queue
->vctx
, queued
);
169 static void replace_unmapped_transfer(struct virgl_transfer_queue
*queue
,
170 struct list_action_args
*args
)
172 struct virgl_transfer
*current
= args
->current
;
173 struct virgl_transfer
*queued
= args
->queued
;
175 u_box_union_2d(¤t
->base
.box
, ¤t
->base
.box
, &queued
->base
.box
);
176 current
->offset
= current
->base
.box
.x
;
178 remove_transfer(queue
, queued
);
179 queue
->num_dwords
-= (VIRGL_TRANSFER3D_SIZE
+ 1);
182 static void transfer_put(struct virgl_transfer_queue
*queue
,
183 struct list_action_args
*args
)
185 struct virgl_transfer
*queued
= args
->queued
;
187 queue
->vs
->vws
->transfer_put(queue
->vs
->vws
, queued
->hw_res
,
189 queued
->base
.stride
, queued
->l_stride
,
190 queued
->offset
, queued
->base
.level
);
192 remove_transfer(queue
, queued
);
195 static void transfer_write(struct virgl_transfer_queue
*queue
,
196 struct list_action_args
*args
)
198 struct virgl_transfer
*queued
= args
->queued
;
199 struct virgl_cmd_buf
*buf
= args
->data
;
201 // Takes a reference on the HW resource, which is released after
202 // the exec buffer command.
203 virgl_encode_transfer(queue
->vs
, buf
, queued
, VIRGL_TRANSFER_TO_HOST
);
205 remove_transfer(queue
, queued
);
208 static void compare_and_perform_action(struct virgl_transfer_queue
*queue
,
209 struct list_iteration_args
*iter
)
211 struct list_action_args args
;
212 struct virgl_transfer
*queued
, *tmp
;
214 memset(&args
, 0, sizeof(args
));
215 args
.current
= iter
->current
;
216 args
.data
= iter
->data
;
218 LIST_FOR_EACH_ENTRY_SAFE(queued
, tmp
, &queue
->transfer_list
, queue_link
) {
219 if (iter
->compare(queued
, iter
->current
)) {
220 args
.queued
= queued
;
221 iter
->action(queue
, &args
);
226 static void perform_action(struct virgl_transfer_queue
*queue
,
227 struct list_iteration_args
*iter
)
229 struct list_action_args args
;
230 struct virgl_transfer
*queued
, *tmp
;
232 memset(&args
, 0, sizeof(args
));
233 args
.data
= iter
->data
;
235 LIST_FOR_EACH_ENTRY_SAFE(queued
, tmp
, &queue
->transfer_list
, queue_link
) {
236 args
.queued
= queued
;
237 iter
->action(queue
, &args
);
241 static void add_internal(struct virgl_transfer_queue
*queue
,
242 struct virgl_transfer
*transfer
)
244 uint32_t dwords
= VIRGL_TRANSFER3D_SIZE
+ 1;
246 if (queue
->num_dwords
+ dwords
>= VIRGL_MAX_TBUF_DWORDS
) {
247 struct list_iteration_args iter
;
248 struct virgl_winsys
*vws
= queue
->vs
->vws
;
250 memset(&iter
, 0, sizeof(iter
));
251 iter
.action
= transfer_write
;
252 iter
.data
= queue
->tbuf
;
253 perform_action(queue
, &iter
);
255 vws
->submit_cmd(vws
, queue
->tbuf
, NULL
);
256 queue
->num_dwords
= 0;
260 list_addtail(&transfer
->queue_link
, &queue
->transfer_list
);
261 queue
->num_dwords
+= dwords
;
264 void virgl_transfer_queue_init(struct virgl_transfer_queue
*queue
,
265 struct virgl_context
*vctx
)
267 struct virgl_screen
*vs
= virgl_screen(vctx
->base
.screen
);
271 queue
->num_dwords
= 0;
273 list_inithead(&queue
->transfer_list
);
275 if ((vs
->caps
.caps
.v2
.capability_bits
& VIRGL_CAP_TRANSFER
) &&
276 vs
->vws
->supports_encoded_transfers
)
277 queue
->tbuf
= vs
->vws
->cmd_buf_create(vs
->vws
, VIRGL_MAX_TBUF_DWORDS
);
282 void virgl_transfer_queue_fini(struct virgl_transfer_queue
*queue
)
284 struct virgl_winsys
*vws
= queue
->vs
->vws
;
285 struct list_iteration_args iter
;
287 memset(&iter
, 0, sizeof(iter
));
289 iter
.action
= transfer_put
;
290 perform_action(queue
, &iter
);
293 vws
->cmd_buf_destroy(queue
->tbuf
);
298 queue
->num_dwords
= 0;
301 int virgl_transfer_queue_unmap(struct virgl_transfer_queue
*queue
,
302 struct virgl_transfer
*transfer
)
304 struct list_iteration_args iter
;
306 /* We don't support copy transfers in the transfer queue. */
307 assert(!transfer
->copy_src_hw_res
);
309 /* Attempt to merge multiple intersecting transfers into a single one. */
310 if (transfer
->base
.resource
->target
== PIPE_BUFFER
) {
311 memset(&iter
, 0, sizeof(iter
));
312 iter
.current
= transfer
;
313 iter
.compare
= transfers_intersect
;
314 iter
.action
= replace_unmapped_transfer
;
315 compare_and_perform_action(queue
, &iter
);
318 add_internal(queue
, transfer
);
322 int virgl_transfer_queue_clear(struct virgl_transfer_queue
*queue
,
323 struct virgl_cmd_buf
*cbuf
)
325 struct list_iteration_args iter
;
327 memset(&iter
, 0, sizeof(iter
));
329 uint32_t prior_num_dwords
= cbuf
->cdw
;
332 iter
.action
= transfer_write
;
334 perform_action(queue
, &iter
);
336 virgl_encode_end_transfers(cbuf
);
337 cbuf
->cdw
= prior_num_dwords
;
339 iter
.action
= transfer_put
;
340 perform_action(queue
, &iter
);
343 queue
->num_dwords
= 0;
348 bool virgl_transfer_queue_is_queued(struct virgl_transfer_queue
*queue
,
349 struct virgl_transfer
*transfer
)
351 return virgl_transfer_queue_find_overlap(queue
,
353 transfer
->base
.level
,
359 virgl_transfer_queue_extend_buffer(struct virgl_transfer_queue
*queue
,
360 const struct virgl_hw_res
*hw_res
,
361 unsigned offset
, unsigned size
,
364 struct virgl_transfer
*queued
;
367 u_box_1d(offset
, size
, &box
);
368 queued
= virgl_transfer_queue_find_overlap(queue
, hw_res
, 0, &box
, true);
372 assert(queued
->base
.resource
->target
== PIPE_BUFFER
);
373 assert(queued
->hw_res_map
);
375 memcpy(queued
->hw_res_map
+ offset
, data
, size
);
376 u_box_union_2d(&queued
->base
.box
, &queued
->base
.box
, &box
);
377 queued
->offset
= queued
->base
.box
.x
;