2 * Copyright (c) 2012-2015 Etnaviv Project
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 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Wladimir J. van der Laan <laanwj@gmail.com>
27 #include "etnaviv_resource.h"
29 #include "hw/common.xml.h"
31 #include "etnaviv_context.h"
32 #include "etnaviv_debug.h"
33 #include "etnaviv_screen.h"
34 #include "etnaviv_translate.h"
36 #include "util/hash_table.h"
37 #include "util/u_inlines.h"
38 #include "util/u_memory.h"
40 #include "drm-uapi/drm_fourcc.h"
42 static enum etna_surface_layout
modifier_to_layout(uint64_t modifier
)
45 case DRM_FORMAT_MOD_VIVANTE_TILED
:
46 return ETNA_LAYOUT_TILED
;
47 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED
:
48 return ETNA_LAYOUT_SUPER_TILED
;
49 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED
:
50 return ETNA_LAYOUT_MULTI_TILED
;
51 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED
:
52 return ETNA_LAYOUT_MULTI_SUPERTILED
;
53 case DRM_FORMAT_MOD_LINEAR
:
55 return ETNA_LAYOUT_LINEAR
;
59 static uint64_t layout_to_modifier(enum etna_surface_layout layout
)
62 case ETNA_LAYOUT_TILED
:
63 return DRM_FORMAT_MOD_VIVANTE_TILED
;
64 case ETNA_LAYOUT_SUPER_TILED
:
65 return DRM_FORMAT_MOD_VIVANTE_SUPER_TILED
;
66 case ETNA_LAYOUT_MULTI_TILED
:
67 return DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED
;
68 case ETNA_LAYOUT_MULTI_SUPERTILED
:
69 return DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED
;
70 case ETNA_LAYOUT_LINEAR
:
71 return DRM_FORMAT_MOD_LINEAR
;
73 return DRM_FORMAT_MOD_INVALID
;
77 /* A tile is 4x4 pixels, having 'screen->specs.bits_per_tile' of tile status.
78 * So, in a buffer of N pixels, there are N / (4 * 4) tiles.
79 * We need N * screen->specs.bits_per_tile / (4 * 4) bits of tile status, or
80 * N * screen->specs.bits_per_tile / (4 * 4 * 8) bytes.
83 etna_screen_resource_alloc_ts(struct pipe_screen
*pscreen
,
84 struct etna_resource
*rsc
)
86 struct etna_screen
*screen
= etna_screen(pscreen
);
87 size_t rt_ts_size
, ts_layer_stride
;
88 size_t ts_bits_per_tile
, bytes_per_tile
;
89 uint8_t ts_mode
= TS_MODE_128B
; /* only used by halti5 */
90 int8_t ts_compress_fmt
;
94 /* pre-v4 compression is largely useless, so disable it when not wanted for MSAA
95 * v4 compression can be enabled everywhere without any known drawback,
96 * except that in-place resolve must go through a slower path
98 ts_compress_fmt
= (screen
->specs
.v4_compression
|| rsc
->base
.nr_samples
> 1) ?
99 translate_ts_format(rsc
->base
.format
) : -1;
101 if (screen
->specs
.halti
>= 5) {
102 /* enable 256B ts mode with compression, as it improves performance
103 * the size of the resource might also determine if we want to use it or not
105 if (ts_compress_fmt
>= 0)
106 ts_mode
= TS_MODE_256B
;
108 ts_bits_per_tile
= 4;
109 bytes_per_tile
= ts_mode
== TS_MODE_256B
? 256 : 128;
111 ts_bits_per_tile
= screen
->specs
.bits_per_tile
;
115 ts_layer_stride
= align(DIV_ROUND_UP(rsc
->levels
[0].layer_stride
,
116 bytes_per_tile
* 8 / ts_bits_per_tile
),
117 0x100 * screen
->specs
.pixel_pipes
);
118 rt_ts_size
= ts_layer_stride
* rsc
->base
.array_size
;
122 DBG_F(ETNA_DBG_RESOURCE_MSGS
, "%p: Allocating tile status of size %zu",
125 struct etna_bo
*rt_ts
;
126 rt_ts
= etna_bo_new(screen
->dev
, rt_ts_size
, DRM_ETNA_GEM_CACHE_WC
);
128 if (unlikely(!rt_ts
)) {
129 BUG("Problem allocating tile status for resource");
134 rsc
->levels
[0].ts_offset
= 0;
135 rsc
->levels
[0].ts_layer_stride
= ts_layer_stride
;
136 rsc
->levels
[0].ts_size
= rt_ts_size
;
137 rsc
->levels
[0].ts_mode
= ts_mode
;
138 rsc
->levels
[0].ts_compress_fmt
= ts_compress_fmt
;
144 etna_screen_can_create_resource(struct pipe_screen
*pscreen
,
145 const struct pipe_resource
*templat
)
147 struct etna_screen
*screen
= etna_screen(pscreen
);
148 if (!translate_samples_to_xyscale(templat
->nr_samples
, NULL
, NULL
, NULL
))
151 /* templat->bind is not set here, so we must use the minimum sizes */
153 MIN2(screen
->specs
.max_rendertarget_size
, screen
->specs
.max_texture_size
);
155 if (templat
->width0
> max_size
|| templat
->height0
> max_size
)
162 setup_miptree(struct etna_resource
*rsc
, unsigned paddingX
, unsigned paddingY
,
163 unsigned msaa_xscale
, unsigned msaa_yscale
)
165 struct pipe_resource
*prsc
= &rsc
->base
;
166 unsigned level
, size
= 0;
167 unsigned width
= prsc
->width0
;
168 unsigned height
= prsc
->height0
;
169 unsigned depth
= prsc
->depth0
;
171 for (level
= 0; level
<= prsc
->last_level
; level
++) {
172 struct etna_resource_level
*mip
= &rsc
->levels
[level
];
175 mip
->height
= height
;
176 mip
->padded_width
= align(width
* msaa_xscale
, paddingX
);
177 mip
->padded_height
= align(height
* msaa_yscale
, paddingY
);
178 mip
->stride
= util_format_get_stride(prsc
->format
, mip
->padded_width
);
180 mip
->layer_stride
= mip
->stride
* util_format_get_nblocksy(prsc
->format
, mip
->padded_height
);
181 mip
->size
= prsc
->array_size
* mip
->layer_stride
;
183 /* align levels to 64 bytes to be able to render to them */
184 size
+= align(mip
->size
, ETNA_PE_ALIGNMENT
) * depth
;
186 width
= u_minify(width
, 1);
187 height
= u_minify(height
, 1);
188 depth
= u_minify(depth
, 1);
194 /* Is rs alignment needed? */
195 static bool is_rs_align(struct etna_screen
*screen
,
196 const struct pipe_resource
*tmpl
)
198 return screen
->specs
.use_blt
? false : (
199 VIV_FEATURE(screen
, chipMinorFeatures1
, TEXTURE_HALIGN
) ||
200 !etna_resource_sampler_only(tmpl
));
203 /* Create a new resource object, using the given template info */
204 struct pipe_resource
*
205 etna_resource_alloc(struct pipe_screen
*pscreen
, unsigned layout
,
206 enum etna_resource_addressing_mode mode
, uint64_t modifier
,
207 const struct pipe_resource
*templat
)
209 struct etna_screen
*screen
= etna_screen(pscreen
);
210 struct etna_resource
*rsc
;
213 DBG_F(ETNA_DBG_RESOURCE_MSGS
,
214 "target=%d, format=%s, %ux%ux%u, array_size=%u, "
215 "last_level=%u, nr_samples=%u, usage=%u, bind=%x, flags=%x",
216 templat
->target
, util_format_name(templat
->format
), templat
->width0
,
217 templat
->height0
, templat
->depth0
, templat
->array_size
,
218 templat
->last_level
, templat
->nr_samples
, templat
->usage
,
219 templat
->bind
, templat
->flags
);
221 /* Determine scaling for antialiasing, allow override using debug flag */
222 int nr_samples
= templat
->nr_samples
;
223 if ((templat
->bind
& (PIPE_BIND_RENDER_TARGET
| PIPE_BIND_DEPTH_STENCIL
)) &&
224 !(templat
->bind
& PIPE_BIND_SAMPLER_VIEW
)) {
225 if (DBG_ENABLED(ETNA_DBG_MSAA_2X
))
227 if (DBG_ENABLED(ETNA_DBG_MSAA_4X
))
231 int msaa_xscale
= 1, msaa_yscale
= 1;
232 if (!translate_samples_to_xyscale(nr_samples
, &msaa_xscale
, &msaa_yscale
, NULL
)) {
233 /* Number of samples not supported */
237 /* Determine needed padding (alignment of height/width) */
238 unsigned paddingX
= 0, paddingY
= 0;
239 unsigned halign
= TEXTURE_HALIGN_FOUR
;
240 if (!util_format_is_compressed(templat
->format
)) {
241 /* If we have the TEXTURE_HALIGN feature, we can always align to the
242 * resolve engine's width. If not, we must not align resources used
243 * only for textures. If this GPU uses the BLT engine, never do RS align.
245 etna_layout_multiple(layout
, screen
->specs
.pixel_pipes
,
246 is_rs_align (screen
, templat
),
247 &paddingX
, &paddingY
, &halign
);
248 assert(paddingX
&& paddingY
);
250 /* Compressed textures are padded to their block size, but we don't have
251 * to do anything special for that. */
256 if (!screen
->specs
.use_blt
&& templat
->target
!= PIPE_BUFFER
&& layout
== ETNA_LAYOUT_LINEAR
)
257 paddingY
= align(paddingY
, ETNA_RS_HEIGHT_MASK
+ 1);
259 if (templat
->bind
& PIPE_BIND_SCANOUT
&& screen
->ro
->kms_fd
>= 0) {
260 struct pipe_resource scanout_templat
= *templat
;
261 struct renderonly_scanout
*scanout
;
262 struct winsys_handle handle
;
264 /* pad scanout buffer size to be compatible with the RS */
265 if (!screen
->specs
.use_blt
&& modifier
== DRM_FORMAT_MOD_LINEAR
) {
266 paddingX
= align(paddingX
, ETNA_RS_WIDTH_MASK
+ 1);
267 paddingY
= align(paddingY
, ETNA_RS_HEIGHT_MASK
+ 1);
270 scanout_templat
.width0
= align(scanout_templat
.width0
, paddingX
);
271 scanout_templat
.height0
= align(scanout_templat
.height0
, paddingY
);
273 scanout
= renderonly_scanout_for_resource(&scanout_templat
,
274 screen
->ro
, &handle
);
278 assert(handle
.type
== WINSYS_HANDLE_TYPE_FD
);
279 handle
.modifier
= modifier
;
280 rsc
= etna_resource(pscreen
->resource_from_handle(pscreen
, templat
,
282 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
));
283 close(handle
.handle
);
287 rsc
->scanout
= scanout
;
292 rsc
= CALLOC_STRUCT(etna_resource
);
296 rsc
->base
= *templat
;
297 rsc
->base
.screen
= pscreen
;
298 rsc
->base
.nr_samples
= nr_samples
;
299 rsc
->layout
= layout
;
300 rsc
->halign
= halign
;
301 rsc
->addressing_mode
= mode
;
303 pipe_reference_init(&rsc
->base
.reference
, 1);
305 size
= setup_miptree(rsc
, paddingX
, paddingY
, msaa_xscale
, msaa_yscale
);
307 uint32_t flags
= DRM_ETNA_GEM_CACHE_WC
;
308 if (templat
->bind
& PIPE_BIND_VERTEX_BUFFER
)
309 flags
|= DRM_ETNA_GEM_FORCE_MMU
;
310 struct etna_bo
*bo
= etna_bo_new(screen
->dev
, size
, flags
);
311 if (unlikely(bo
== NULL
)) {
312 BUG("Problem allocating video memory for resource");
317 rsc
->ts_bo
= 0; /* TS is only created when first bound to surface */
319 if (DBG_ENABLED(ETNA_DBG_ZERO
)) {
320 void *map
= etna_bo_map(bo
);
321 memset(map
, 0, size
);
324 rsc
->pending_ctx
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
325 _mesa_key_pointer_equal
);
326 if (!rsc
->pending_ctx
)
336 static struct pipe_resource
*
337 etna_resource_create(struct pipe_screen
*pscreen
,
338 const struct pipe_resource
*templat
)
340 struct etna_screen
*screen
= etna_screen(pscreen
);
342 /* Figure out what tiling and address mode to use -- for now, assume that
343 * texture cannot be linear. there is a capability LINEAR_TEXTURE_SUPPORT
344 * (supported on gc880 and gc2000 at least), but not sure how it works.
345 * Buffers always have LINEAR layout.
347 unsigned layout
= ETNA_LAYOUT_LINEAR
;
348 enum etna_resource_addressing_mode mode
= ETNA_ADDRESSING_MODE_TILED
;
350 if (etna_resource_sampler_only(templat
)) {
351 /* The buffer is only used for texturing, so create something
352 * directly compatible with the sampler. Such a buffer can
353 * never be rendered to. */
354 layout
= ETNA_LAYOUT_TILED
;
356 if (util_format_is_compressed(templat
->format
))
357 layout
= ETNA_LAYOUT_LINEAR
;
358 } else if (templat
->target
!= PIPE_BUFFER
) {
359 bool want_multitiled
= false;
360 bool want_supertiled
= screen
->specs
.can_supertile
;
362 /* When this GPU supports single-buffer rendering, don't ever enable
363 * multi-tiling. This replicates the blob behavior on GC3000.
365 if (!screen
->specs
.single_buffer
)
366 want_multitiled
= screen
->specs
.pixel_pipes
> 1;
368 /* Keep single byte blocksized resources as tiled, since we
369 * are unable to use the RS blit to de-tile them. However,
370 * if they're used as a render target or depth/stencil, they
371 * must be multi-tiled for GPUs with multiple pixel pipes.
372 * Ignore depth/stencil here, but it is an error for a render
375 if (util_format_get_blocksize(templat
->format
) == 1 &&
376 !(templat
->bind
& PIPE_BIND_DEPTH_STENCIL
)) {
377 assert(!(templat
->bind
& PIPE_BIND_RENDER_TARGET
&& want_multitiled
));
378 want_multitiled
= want_supertiled
= false;
381 layout
= ETNA_LAYOUT_BIT_TILE
;
383 layout
|= ETNA_LAYOUT_BIT_MULTI
;
385 layout
|= ETNA_LAYOUT_BIT_SUPER
;
388 if (templat
->target
== PIPE_TEXTURE_3D
)
389 layout
= ETNA_LAYOUT_LINEAR
;
391 /* modifier is only used for scanout surfaces, so safe to use LINEAR here */
392 return etna_resource_alloc(pscreen
, layout
, mode
, DRM_FORMAT_MOD_LINEAR
, templat
);
395 enum modifier_priority
{
396 MODIFIER_PRIORITY_INVALID
= 0,
397 MODIFIER_PRIORITY_LINEAR
,
398 MODIFIER_PRIORITY_SPLIT_TILED
,
399 MODIFIER_PRIORITY_SPLIT_SUPER_TILED
,
400 MODIFIER_PRIORITY_TILED
,
401 MODIFIER_PRIORITY_SUPER_TILED
,
404 const uint64_t priority_to_modifier
[] = {
405 [MODIFIER_PRIORITY_INVALID
] = DRM_FORMAT_MOD_INVALID
,
406 [MODIFIER_PRIORITY_LINEAR
] = DRM_FORMAT_MOD_LINEAR
,
407 [MODIFIER_PRIORITY_SPLIT_TILED
] = DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED
,
408 [MODIFIER_PRIORITY_SPLIT_SUPER_TILED
] = DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED
,
409 [MODIFIER_PRIORITY_TILED
] = DRM_FORMAT_MOD_VIVANTE_TILED
,
410 [MODIFIER_PRIORITY_SUPER_TILED
] = DRM_FORMAT_MOD_VIVANTE_SUPER_TILED
,
414 select_best_modifier(const struct etna_screen
* screen
,
415 const uint64_t *modifiers
, const unsigned count
)
417 enum modifier_priority prio
= MODIFIER_PRIORITY_INVALID
;
419 for (int i
= 0; i
< count
; i
++) {
420 switch (modifiers
[i
]) {
421 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED
:
422 if ((screen
->specs
.pixel_pipes
> 1 && !screen
->specs
.single_buffer
) ||
423 !screen
->specs
.can_supertile
)
425 prio
= MAX2(prio
, MODIFIER_PRIORITY_SUPER_TILED
);
427 case DRM_FORMAT_MOD_VIVANTE_TILED
:
428 if (screen
->specs
.pixel_pipes
> 1 && !screen
->specs
.single_buffer
)
430 prio
= MAX2(prio
, MODIFIER_PRIORITY_TILED
);
432 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED
:
433 if ((screen
->specs
.pixel_pipes
< 2) || !screen
->specs
.can_supertile
)
435 prio
= MAX2(prio
, MODIFIER_PRIORITY_SPLIT_SUPER_TILED
);
437 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED
:
438 if (screen
->specs
.pixel_pipes
< 2)
440 prio
= MAX2(prio
, MODIFIER_PRIORITY_SPLIT_TILED
);
442 case DRM_FORMAT_MOD_LINEAR
:
443 prio
= MAX2(prio
, MODIFIER_PRIORITY_LINEAR
);
445 case DRM_FORMAT_MOD_INVALID
:
451 return priority_to_modifier
[prio
];
454 static struct pipe_resource
*
455 etna_resource_create_modifiers(struct pipe_screen
*pscreen
,
456 const struct pipe_resource
*templat
,
457 const uint64_t *modifiers
, int count
)
459 struct etna_screen
*screen
= etna_screen(pscreen
);
460 struct pipe_resource tmpl
= *templat
;
461 uint64_t modifier
= select_best_modifier(screen
, modifiers
, count
);
463 if (modifier
== DRM_FORMAT_MOD_INVALID
)
467 * We currently assume that all buffers allocated through this interface
468 * should be scanout enabled.
470 tmpl
.bind
|= PIPE_BIND_SCANOUT
;
472 return etna_resource_alloc(pscreen
, modifier_to_layout(modifier
),
473 ETNA_ADDRESSING_MODE_TILED
, modifier
, &tmpl
);
477 etna_resource_changed(struct pipe_screen
*pscreen
, struct pipe_resource
*prsc
)
479 struct etna_resource
*res
= etna_resource(prsc
);
482 etna_resource(res
->external
)->seqno
++;
488 etna_resource_destroy(struct pipe_screen
*pscreen
, struct pipe_resource
*prsc
)
490 struct etna_screen
*screen
= etna_screen(pscreen
);
491 struct etna_resource
*rsc
= etna_resource(prsc
);
493 mtx_lock(&screen
->lock
);
494 _mesa_set_remove_key(screen
->used_resources
, rsc
);
495 _mesa_set_destroy(rsc
->pending_ctx
, NULL
);
496 mtx_unlock(&screen
->lock
);
499 etna_bo_del(rsc
->bo
);
502 etna_bo_del(rsc
->ts_bo
);
505 renderonly_scanout_destroy(rsc
->scanout
, etna_screen(pscreen
)->ro
);
507 pipe_resource_reference(&rsc
->texture
, NULL
);
508 pipe_resource_reference(&rsc
->external
, NULL
);
510 for (unsigned i
= 0; i
< ETNA_NUM_LOD
; i
++)
511 FREE(rsc
->levels
[i
].patch_offsets
);
516 static struct pipe_resource
*
517 etna_resource_from_handle(struct pipe_screen
*pscreen
,
518 const struct pipe_resource
*tmpl
,
519 struct winsys_handle
*handle
, unsigned usage
)
521 struct etna_screen
*screen
= etna_screen(pscreen
);
522 struct etna_resource
*rsc
;
523 struct etna_resource_level
*level
;
524 struct pipe_resource
*prsc
;
525 struct pipe_resource
*ptiled
= NULL
;
527 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
528 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
529 tmpl
->target
, util_format_name(tmpl
->format
), tmpl
->width0
,
530 tmpl
->height0
, tmpl
->depth0
, tmpl
->array_size
, tmpl
->last_level
,
531 tmpl
->nr_samples
, tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
533 rsc
= CALLOC_STRUCT(etna_resource
);
537 level
= &rsc
->levels
[0];
542 pipe_reference_init(&prsc
->reference
, 1);
543 prsc
->screen
= pscreen
;
545 rsc
->bo
= etna_screen_bo_from_handle(pscreen
, handle
, &level
->stride
);
550 rsc
->layout
= modifier_to_layout(handle
->modifier
);
551 rsc
->halign
= TEXTURE_HALIGN_FOUR
;
552 rsc
->addressing_mode
= ETNA_ADDRESSING_MODE_TILED
;
555 level
->width
= tmpl
->width0
;
556 level
->height
= tmpl
->height0
;
558 /* Determine padding of the imported resource. */
559 unsigned paddingX
= 0, paddingY
= 0;
560 etna_layout_multiple(rsc
->layout
, screen
->specs
.pixel_pipes
,
561 is_rs_align(screen
, tmpl
),
562 &paddingX
, &paddingY
, &rsc
->halign
);
564 if (!screen
->specs
.use_blt
&& rsc
->layout
== ETNA_LAYOUT_LINEAR
)
565 paddingY
= align(paddingY
, ETNA_RS_HEIGHT_MASK
+ 1);
566 level
->padded_width
= align(level
->width
, paddingX
);
567 level
->padded_height
= align(level
->height
, paddingY
);
569 level
->layer_stride
= level
->stride
* util_format_get_nblocksy(prsc
->format
,
570 level
->padded_height
);
571 level
->size
= level
->layer_stride
;
573 /* The DDX must give us a BO which conforms to our padding size.
574 * The stride of the BO must be greater or equal to our padded
575 * stride. The size of the BO must accomodate the padded height. */
576 if (level
->stride
< util_format_get_stride(tmpl
->format
, level
->padded_width
)) {
577 BUG("BO stride %u is too small for RS engine width padding (%zu, format %s)",
578 level
->stride
, util_format_get_stride(tmpl
->format
, level
->padded_width
),
579 util_format_name(tmpl
->format
));
582 if (etna_bo_size(rsc
->bo
) < level
->stride
* level
->padded_height
) {
583 BUG("BO size %u is too small for RS engine height padding (%u, format %s)",
584 etna_bo_size(rsc
->bo
), level
->stride
* level
->padded_height
,
585 util_format_name(tmpl
->format
));
589 rsc
->pending_ctx
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
590 _mesa_key_pointer_equal
);
591 if (!rsc
->pending_ctx
)
594 if (rsc
->layout
== ETNA_LAYOUT_LINEAR
) {
596 * Both sampler and pixel pipes can't handle linear, create a compatible
597 * base resource, where we can attach the imported buffer as an external
600 struct pipe_resource tiled_templat
= *tmpl
;
603 * Remove BIND_SCANOUT to avoid recursion, as etna_resource_create uses
604 * this function to import the scanout buffer and get a tiled resource.
606 tiled_templat
.bind
&= ~PIPE_BIND_SCANOUT
;
608 ptiled
= etna_resource_create(pscreen
, &tiled_templat
);
612 etna_resource(ptiled
)->external
= prsc
;
620 etna_resource_destroy(pscreen
, prsc
);
622 etna_resource_destroy(pscreen
, ptiled
);
628 etna_resource_get_handle(struct pipe_screen
*pscreen
,
629 struct pipe_context
*pctx
,
630 struct pipe_resource
*prsc
,
631 struct winsys_handle
*handle
, unsigned usage
)
633 struct etna_resource
*rsc
= etna_resource(prsc
);
634 /* Scanout is always attached to the base resource */
635 struct renderonly_scanout
*scanout
= rsc
->scanout
;
638 * External resources are preferred, so a import->export chain of
639 * render/sampler incompatible buffers yield the same handle.
642 rsc
= etna_resource(rsc
->external
);
644 handle
->stride
= rsc
->levels
[0].stride
;
645 handle
->offset
= rsc
->levels
[0].offset
;
646 handle
->modifier
= layout_to_modifier(rsc
->layout
);
648 if (handle
->type
== WINSYS_HANDLE_TYPE_SHARED
) {
649 return etna_bo_get_name(rsc
->bo
, &handle
->handle
) == 0;
650 } else if (handle
->type
== WINSYS_HANDLE_TYPE_KMS
) {
651 if (renderonly_get_handle(scanout
, handle
)) {
654 handle
->handle
= etna_bo_handle(rsc
->bo
);
657 } else if (handle
->type
== WINSYS_HANDLE_TYPE_FD
) {
658 handle
->handle
= etna_bo_dmabuf(rsc
->bo
);
666 etna_resource_used(struct etna_context
*ctx
, struct pipe_resource
*prsc
,
667 enum etna_resource_status status
)
669 struct etna_screen
*screen
= ctx
->screen
;
670 struct etna_resource
*rsc
;
675 rsc
= etna_resource(prsc
);
677 mtx_lock(&screen
->lock
);
680 * if we are pending read or write by any other context or
681 * if reading a resource pending a write, then
682 * flush all the contexts to maintain coherency
684 if (((status
& ETNA_PENDING_WRITE
) && rsc
->status
) ||
685 ((status
& ETNA_PENDING_READ
) && (rsc
->status
& ETNA_PENDING_WRITE
))) {
686 set_foreach(rsc
->pending_ctx
, entry
) {
687 struct etna_context
*extctx
= (struct etna_context
*)entry
->key
;
688 struct pipe_context
*pctx
= &extctx
->base
;
693 pctx
->flush(pctx
, NULL
, 0);
694 /* It's safe to clear the status here. If we need to flush it means
695 * either another context had the resource in exclusive (write) use,
696 * or we transition the resource to exclusive use in our context.
697 * In both cases the new status accurately reflects the resource use
704 rsc
->status
|= status
;
706 _mesa_set_add(screen
->used_resources
, rsc
);
707 _mesa_set_add(rsc
->pending_ctx
, ctx
);
709 mtx_unlock(&screen
->lock
);
713 etna_resource_has_valid_ts(struct etna_resource
*rsc
)
718 for (int level
= 0; level
<= rsc
->base
.last_level
; level
++)
719 if (rsc
->levels
[level
].ts_valid
)
726 etna_resource_screen_init(struct pipe_screen
*pscreen
)
728 pscreen
->can_create_resource
= etna_screen_can_create_resource
;
729 pscreen
->resource_create
= etna_resource_create
;
730 pscreen
->resource_create_with_modifiers
= etna_resource_create_modifiers
;
731 pscreen
->resource_from_handle
= etna_resource_from_handle
;
732 pscreen
->resource_get_handle
= etna_resource_get_handle
;
733 pscreen
->resource_changed
= etna_resource_changed
;
734 pscreen
->resource_destroy
= etna_resource_destroy
;