etnaviv: Check that resource has a valid TS in etna_resource_needs_flush
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_resource.c
1 /*
2 * Copyright (c) 2012-2015 Etnaviv Project
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 * 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:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 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.
22 *
23 * Authors:
24 * Wladimir J. van der Laan <laanwj@gmail.com>
25 */
26
27 #include "etnaviv_resource.h"
28
29 #include "hw/common.xml.h"
30
31 #include "etnaviv_context.h"
32 #include "etnaviv_debug.h"
33 #include "etnaviv_screen.h"
34 #include "etnaviv_translate.h"
35
36 #include "util/u_inlines.h"
37 #include "util/u_memory.h"
38
39 #include <drm_fourcc.h>
40
41 #ifndef DRM_FORMAT_MOD_INVALID
42 #define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1)
43 #endif
44
45 static enum etna_surface_layout modifier_to_layout(uint64_t modifier)
46 {
47 switch (modifier) {
48 case DRM_FORMAT_MOD_VIVANTE_TILED:
49 return ETNA_LAYOUT_TILED;
50 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
51 return ETNA_LAYOUT_SUPER_TILED;
52 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
53 return ETNA_LAYOUT_MULTI_TILED;
54 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
55 return ETNA_LAYOUT_MULTI_SUPERTILED;
56 case DRM_FORMAT_MOD_LINEAR:
57 default:
58 return ETNA_LAYOUT_LINEAR;
59 }
60 }
61
62 static uint64_t layout_to_modifier(enum etna_surface_layout layout)
63 {
64 switch (layout) {
65 case ETNA_LAYOUT_TILED:
66 return DRM_FORMAT_MOD_VIVANTE_TILED;
67 case ETNA_LAYOUT_SUPER_TILED:
68 return DRM_FORMAT_MOD_VIVANTE_SUPER_TILED;
69 case ETNA_LAYOUT_MULTI_TILED:
70 return DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED;
71 case ETNA_LAYOUT_MULTI_SUPERTILED:
72 return DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED;
73 case ETNA_LAYOUT_LINEAR:
74 return DRM_FORMAT_MOD_LINEAR;
75 default:
76 return DRM_FORMAT_MOD_INVALID;
77 }
78 }
79
80 /* A tile is 4x4 pixels, having 'screen->specs.bits_per_tile' of tile status.
81 * So, in a buffer of N pixels, there are N / (4 * 4) tiles.
82 * We need N * screen->specs.bits_per_tile / (4 * 4) bits of tile status, or
83 * N * screen->specs.bits_per_tile / (4 * 4 * 8) bytes.
84 */
85 bool
86 etna_screen_resource_alloc_ts(struct pipe_screen *pscreen,
87 struct etna_resource *rsc)
88 {
89 struct etna_screen *screen = etna_screen(pscreen);
90 size_t rt_ts_size, ts_layer_stride, pixels;
91
92 assert(!rsc->ts_bo);
93
94 /* TS only for level 0 -- XXX is this formula correct? */
95 pixels = rsc->levels[0].layer_stride / util_format_get_blocksize(rsc->base.format);
96 ts_layer_stride = align(pixels * screen->specs.bits_per_tile / 0x80,
97 0x100 * screen->specs.pixel_pipes);
98 rt_ts_size = ts_layer_stride * rsc->base.array_size;
99 if (rt_ts_size == 0)
100 return true;
101
102 DBG_F(ETNA_DBG_RESOURCE_MSGS, "%p: Allocating tile status of size %zu",
103 rsc, rt_ts_size);
104
105 struct etna_bo *rt_ts;
106 rt_ts = etna_bo_new(screen->dev, rt_ts_size, DRM_ETNA_GEM_CACHE_WC);
107
108 if (unlikely(!rt_ts)) {
109 BUG("Problem allocating tile status for resource");
110 return false;
111 }
112
113 rsc->ts_bo = rt_ts;
114 rsc->levels[0].ts_offset = 0;
115 rsc->levels[0].ts_layer_stride = ts_layer_stride;
116 rsc->levels[0].ts_size = rt_ts_size;
117
118 /* It is important to initialize the TS, as random pattern
119 * can result in crashes. Do this on the CPU as this only happens once
120 * per surface anyway and it's a small area, so it may not be worth
121 * queuing this to the GPU. */
122 void *ts_map = etna_bo_map(rt_ts);
123 memset(ts_map, screen->specs.ts_clear_value, rt_ts_size);
124
125 return true;
126 }
127
128 static boolean
129 etna_screen_can_create_resource(struct pipe_screen *pscreen,
130 const struct pipe_resource *templat)
131 {
132 struct etna_screen *screen = etna_screen(pscreen);
133 if (!translate_samples_to_xyscale(templat->nr_samples, NULL, NULL, NULL))
134 return false;
135
136 /* templat->bind is not set here, so we must use the minimum sizes */
137 uint max_size =
138 MIN2(screen->specs.max_rendertarget_size, screen->specs.max_texture_size);
139
140 if (templat->width0 > max_size || templat->height0 > max_size)
141 return false;
142
143 return true;
144 }
145
146 static unsigned
147 setup_miptree(struct etna_resource *rsc, unsigned paddingX, unsigned paddingY,
148 unsigned msaa_xscale, unsigned msaa_yscale)
149 {
150 struct pipe_resource *prsc = &rsc->base;
151 unsigned level, size = 0;
152 unsigned width = prsc->width0;
153 unsigned height = prsc->height0;
154 unsigned depth = prsc->depth0;
155
156 for (level = 0; level <= prsc->last_level; level++) {
157 struct etna_resource_level *mip = &rsc->levels[level];
158
159 mip->width = width;
160 mip->height = height;
161 mip->padded_width = align(width * msaa_xscale, paddingX);
162 mip->padded_height = align(height * msaa_yscale, paddingY);
163 mip->stride = util_format_get_stride(prsc->format, mip->padded_width);
164 mip->offset = size;
165 mip->layer_stride = mip->stride * util_format_get_nblocksy(prsc->format, mip->padded_height);
166 mip->size = prsc->array_size * mip->layer_stride;
167
168 /* align levels to 64 bytes to be able to render to them */
169 size += align(mip->size, ETNA_PE_ALIGNMENT) * depth;
170
171 width = u_minify(width, 1);
172 height = u_minify(height, 1);
173 depth = u_minify(depth, 1);
174 }
175
176 return size;
177 }
178
179 /* Create a new resource object, using the given template info */
180 struct pipe_resource *
181 etna_resource_alloc(struct pipe_screen *pscreen, unsigned layout,
182 uint64_t modifier, const struct pipe_resource *templat)
183 {
184 struct etna_screen *screen = etna_screen(pscreen);
185 struct etna_resource *rsc;
186 unsigned size;
187
188 DBG_F(ETNA_DBG_RESOURCE_MSGS,
189 "target=%d, format=%s, %ux%ux%u, array_size=%u, "
190 "last_level=%u, nr_samples=%u, usage=%u, bind=%x, flags=%x",
191 templat->target, util_format_name(templat->format), templat->width0,
192 templat->height0, templat->depth0, templat->array_size,
193 templat->last_level, templat->nr_samples, templat->usage,
194 templat->bind, templat->flags);
195
196 /* Determine scaling for antialiasing, allow override using debug flag */
197 int nr_samples = templat->nr_samples;
198 if ((templat->bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL)) &&
199 !(templat->bind & PIPE_BIND_SAMPLER_VIEW)) {
200 if (DBG_ENABLED(ETNA_DBG_MSAA_2X))
201 nr_samples = 2;
202 if (DBG_ENABLED(ETNA_DBG_MSAA_4X))
203 nr_samples = 4;
204 }
205
206 int msaa_xscale = 1, msaa_yscale = 1;
207 if (!translate_samples_to_xyscale(nr_samples, &msaa_xscale, &msaa_yscale, NULL)) {
208 /* Number of samples not supported */
209 return NULL;
210 }
211
212 /* Determine needed padding (alignment of height/width) */
213 unsigned paddingX = 0, paddingY = 0;
214 unsigned halign = TEXTURE_HALIGN_FOUR;
215 if (!util_format_is_compressed(templat->format)) {
216 /* If we have the TEXTURE_HALIGN feature, we can always align to the
217 * resolve engine's width. If not, we must not align resources used
218 * only for textures. */
219 bool rs_align = VIV_FEATURE(screen, chipMinorFeatures1, TEXTURE_HALIGN) ||
220 !etna_resource_sampler_only(templat);
221 etna_layout_multiple(layout, screen->specs.pixel_pipes, rs_align, &paddingX,
222 &paddingY, &halign);
223 assert(paddingX && paddingY);
224 } else {
225 /* Compressed textures are padded to their block size, but we don't have
226 * to do anything special for that. */
227 paddingX = 1;
228 paddingY = 1;
229 }
230
231 if (templat->target != PIPE_BUFFER)
232 etna_adjust_rs_align(screen->specs.pixel_pipes, NULL, &paddingY);
233
234 if (templat->bind & PIPE_BIND_SCANOUT) {
235 struct pipe_resource scanout_templat = *templat;
236 struct renderonly_scanout *scanout;
237 struct winsys_handle handle;
238
239 /* pad scanout buffer size to be compatible with the RS */
240 if (modifier == DRM_FORMAT_MOD_LINEAR)
241 etna_adjust_rs_align(screen->specs.pixel_pipes, &paddingX, &paddingY);
242
243 scanout_templat.width0 = align(scanout_templat.width0, paddingX);
244 scanout_templat.height0 = align(scanout_templat.height0, paddingY);
245
246 scanout = renderonly_scanout_for_resource(&scanout_templat,
247 screen->ro, &handle);
248 if (!scanout)
249 return NULL;
250
251 assert(handle.type == DRM_API_HANDLE_TYPE_FD);
252 handle.modifier = modifier;
253 rsc = etna_resource(pscreen->resource_from_handle(pscreen, templat,
254 &handle,
255 PIPE_HANDLE_USAGE_WRITE));
256 close(handle.handle);
257 if (!rsc)
258 return NULL;
259
260 rsc->scanout = scanout;
261
262 return &rsc->base;
263 }
264
265 rsc = CALLOC_STRUCT(etna_resource);
266 if (!rsc)
267 return NULL;
268
269 rsc->base = *templat;
270 rsc->base.screen = pscreen;
271 rsc->base.nr_samples = nr_samples;
272 rsc->layout = layout;
273 rsc->halign = halign;
274
275 pipe_reference_init(&rsc->base.reference, 1);
276 list_inithead(&rsc->list);
277
278 size = setup_miptree(rsc, paddingX, paddingY, msaa_xscale, msaa_yscale);
279
280 uint32_t flags = DRM_ETNA_GEM_CACHE_WC;
281 if (templat->bind & PIPE_BIND_VERTEX_BUFFER)
282 flags |= DRM_ETNA_GEM_FORCE_MMU;
283 struct etna_bo *bo = etna_bo_new(screen->dev, size, flags);
284 if (unlikely(bo == NULL)) {
285 BUG("Problem allocating video memory for resource");
286 goto free_rsc;
287 }
288
289 rsc->bo = bo;
290 rsc->ts_bo = 0; /* TS is only created when first bound to surface */
291
292 if (DBG_ENABLED(ETNA_DBG_ZERO)) {
293 void *map = etna_bo_map(bo);
294 memset(map, 0, size);
295 }
296
297 return &rsc->base;
298
299 free_rsc:
300 FREE(rsc);
301 return NULL;
302 }
303
304 static struct pipe_resource *
305 etna_resource_create(struct pipe_screen *pscreen,
306 const struct pipe_resource *templat)
307 {
308 struct etna_screen *screen = etna_screen(pscreen);
309
310 /* Figure out what tiling to use -- for now, assume that texture cannot be linear.
311 * there is a capability LINEAR_TEXTURE_SUPPORT (supported on gc880 and
312 * gc2000 at least), but not sure how it works.
313 * Buffers always have LINEAR layout.
314 */
315 unsigned layout = ETNA_LAYOUT_LINEAR;
316 if (etna_resource_sampler_only(templat)) {
317 /* The buffer is only used for texturing, so create something
318 * directly compatible with the sampler. Such a buffer can
319 * never be rendered to. */
320 layout = ETNA_LAYOUT_TILED;
321
322 if (util_format_is_compressed(templat->format))
323 layout = ETNA_LAYOUT_LINEAR;
324 } else if (templat->target != PIPE_BUFFER) {
325 bool want_multitiled = false;
326 bool want_supertiled = screen->specs.can_supertile;
327
328 /* When this GPU supports single-buffer rendering, don't ever enable
329 * multi-tiling. This replicates the blob behavior on GC3000.
330 */
331 if (!screen->specs.single_buffer)
332 want_multitiled = screen->specs.pixel_pipes > 1;
333
334 /* Keep single byte blocksized resources as tiled, since we
335 * are unable to use the RS blit to de-tile them. However,
336 * if they're used as a render target or depth/stencil, they
337 * must be multi-tiled for GPUs with multiple pixel pipes.
338 * Ignore depth/stencil here, but it is an error for a render
339 * target.
340 */
341 if (util_format_get_blocksize(templat->format) == 1 &&
342 !(templat->bind & PIPE_BIND_DEPTH_STENCIL)) {
343 assert(!(templat->bind & PIPE_BIND_RENDER_TARGET && want_multitiled));
344 want_multitiled = want_supertiled = false;
345 }
346
347 layout = ETNA_LAYOUT_BIT_TILE;
348 if (want_multitiled)
349 layout |= ETNA_LAYOUT_BIT_MULTI;
350 if (want_supertiled)
351 layout |= ETNA_LAYOUT_BIT_SUPER;
352 }
353
354 if (templat->target == PIPE_TEXTURE_3D)
355 layout = ETNA_LAYOUT_LINEAR;
356
357 /* modifier is only used for scanout surfaces, so safe to use LINEAR here */
358 return etna_resource_alloc(pscreen, layout, DRM_FORMAT_MOD_LINEAR, templat);
359 }
360
361 enum modifier_priority {
362 MODIFIER_PRIORITY_INVALID = 0,
363 MODIFIER_PRIORITY_LINEAR,
364 MODIFIER_PRIORITY_SPLIT_TILED,
365 MODIFIER_PRIORITY_SPLIT_SUPER_TILED,
366 MODIFIER_PRIORITY_TILED,
367 MODIFIER_PRIORITY_SUPER_TILED,
368 };
369
370 const uint64_t priority_to_modifier[] = {
371 [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,
372 [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,
373 [MODIFIER_PRIORITY_SPLIT_TILED] = DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED,
374 [MODIFIER_PRIORITY_SPLIT_SUPER_TILED] = DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED,
375 [MODIFIER_PRIORITY_TILED] = DRM_FORMAT_MOD_VIVANTE_TILED,
376 [MODIFIER_PRIORITY_SUPER_TILED] = DRM_FORMAT_MOD_VIVANTE_SUPER_TILED,
377 };
378
379 static uint64_t
380 select_best_modifier(const struct etna_screen * screen,
381 const uint64_t *modifiers, const unsigned count)
382 {
383 enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;
384
385 for (int i = 0; i < count; i++) {
386 switch (modifiers[i]) {
387 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
388 if ((screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer) ||
389 !screen->specs.can_supertile)
390 break;
391 prio = MAX2(prio, MODIFIER_PRIORITY_SUPER_TILED);
392 break;
393 case DRM_FORMAT_MOD_VIVANTE_TILED:
394 if (screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer)
395 break;
396 prio = MAX2(prio, MODIFIER_PRIORITY_TILED);
397 break;
398 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
399 if ((screen->specs.pixel_pipes < 2) || !screen->specs.can_supertile)
400 break;
401 prio = MAX2(prio, MODIFIER_PRIORITY_SPLIT_SUPER_TILED);
402 break;
403 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
404 if (screen->specs.pixel_pipes < 2)
405 break;
406 prio = MAX2(prio, MODIFIER_PRIORITY_SPLIT_TILED);
407 break;
408 case DRM_FORMAT_MOD_LINEAR:
409 prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);
410 break;
411 case DRM_FORMAT_MOD_INVALID:
412 default:
413 break;
414 }
415 }
416
417 return priority_to_modifier[prio];
418 }
419
420 static struct pipe_resource *
421 etna_resource_create_modifiers(struct pipe_screen *pscreen,
422 const struct pipe_resource *templat,
423 const uint64_t *modifiers, int count)
424 {
425 struct etna_screen *screen = etna_screen(pscreen);
426 struct pipe_resource tmpl = *templat;
427 uint64_t modifier = select_best_modifier(screen, modifiers, count);
428
429 if (modifier == DRM_FORMAT_MOD_INVALID)
430 return NULL;
431
432 /*
433 * We currently assume that all buffers allocated through this interface
434 * should be scanout enabled.
435 */
436 tmpl.bind |= PIPE_BIND_SCANOUT;
437
438 return etna_resource_alloc(pscreen, modifier_to_layout(modifier),
439 modifier, &tmpl);
440 }
441
442 static void
443 etna_resource_changed(struct pipe_screen *pscreen, struct pipe_resource *prsc)
444 {
445 struct etna_resource *res = etna_resource(prsc);
446
447 if (res->external)
448 etna_resource(res->external)->seqno++;
449 else
450 res->seqno++;
451 }
452
453 static void
454 etna_resource_destroy(struct pipe_screen *pscreen, struct pipe_resource *prsc)
455 {
456 struct etna_resource *rsc = etna_resource(prsc);
457
458 if (rsc->bo)
459 etna_bo_del(rsc->bo);
460
461 if (rsc->ts_bo)
462 etna_bo_del(rsc->ts_bo);
463
464 if (rsc->scanout)
465 renderonly_scanout_destroy(rsc->scanout, etna_screen(pscreen)->ro);
466
467 list_delinit(&rsc->list);
468
469 pipe_resource_reference(&rsc->texture, NULL);
470 pipe_resource_reference(&rsc->external, NULL);
471
472 FREE(rsc);
473 }
474
475 static struct pipe_resource *
476 etna_resource_from_handle(struct pipe_screen *pscreen,
477 const struct pipe_resource *tmpl,
478 struct winsys_handle *handle, unsigned usage)
479 {
480 struct etna_screen *screen = etna_screen(pscreen);
481 struct etna_resource *rsc;
482 struct etna_resource_level *level;
483 struct pipe_resource *prsc;
484 struct pipe_resource *ptiled = NULL;
485
486 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
487 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
488 tmpl->target, util_format_name(tmpl->format), tmpl->width0,
489 tmpl->height0, tmpl->depth0, tmpl->array_size, tmpl->last_level,
490 tmpl->nr_samples, tmpl->usage, tmpl->bind, tmpl->flags);
491
492 rsc = CALLOC_STRUCT(etna_resource);
493 if (!rsc)
494 return NULL;
495
496 level = &rsc->levels[0];
497 prsc = &rsc->base;
498
499 *prsc = *tmpl;
500
501 pipe_reference_init(&prsc->reference, 1);
502 list_inithead(&rsc->list);
503 prsc->screen = pscreen;
504
505 rsc->bo = etna_screen_bo_from_handle(pscreen, handle, &level->stride);
506 if (!rsc->bo)
507 goto fail;
508
509 rsc->seqno = 1;
510 rsc->layout = modifier_to_layout(handle->modifier);
511 rsc->halign = TEXTURE_HALIGN_FOUR;
512
513
514 level->width = tmpl->width0;
515 level->height = tmpl->height0;
516
517 /* Determine padding of the imported resource. */
518 unsigned paddingX = 0, paddingY = 0;
519 etna_layout_multiple(rsc->layout, screen->specs.pixel_pipes,
520 VIV_FEATURE(screen, chipMinorFeatures1, TEXTURE_HALIGN),
521 &paddingX, &paddingY, &rsc->halign);
522
523 etna_adjust_rs_align(screen->specs.pixel_pipes, NULL, &paddingY);
524 level->padded_width = align(level->width, paddingX);
525 level->padded_height = align(level->height, paddingY);
526
527 level->layer_stride = level->stride * util_format_get_nblocksy(prsc->format,
528 level->padded_height);
529
530 /* The DDX must give us a BO which conforms to our padding size.
531 * The stride of the BO must be greater or equal to our padded
532 * stride. The size of the BO must accomodate the padded height. */
533 if (level->stride < util_format_get_stride(tmpl->format, level->padded_width)) {
534 BUG("BO stride is too small for RS engine width padding");
535 goto fail;
536 }
537 if (etna_bo_size(rsc->bo) < level->stride * level->padded_height) {
538 BUG("BO size is too small for RS engine height padding");
539 goto fail;
540 }
541
542 if (rsc->layout == ETNA_LAYOUT_LINEAR) {
543 /*
544 * Both sampler and pixel pipes can't handle linear, create a compatible
545 * base resource, where we can attach the imported buffer as an external
546 * resource.
547 */
548 struct pipe_resource tiled_templat = *tmpl;
549
550 /*
551 * Remove BIND_SCANOUT to avoid recursion, as etna_resource_create uses
552 * this function to import the scanout buffer and get a tiled resource.
553 */
554 tiled_templat.bind &= ~PIPE_BIND_SCANOUT;
555
556 ptiled = etna_resource_create(pscreen, &tiled_templat);
557 if (!ptiled)
558 goto fail;
559
560 etna_resource(ptiled)->external = prsc;
561
562 return ptiled;
563 }
564
565 return prsc;
566
567 fail:
568 etna_resource_destroy(pscreen, prsc);
569 if (ptiled)
570 etna_resource_destroy(pscreen, ptiled);
571
572 return NULL;
573 }
574
575 static boolean
576 etna_resource_get_handle(struct pipe_screen *pscreen,
577 struct pipe_context *pctx,
578 struct pipe_resource *prsc,
579 struct winsys_handle *handle, unsigned usage)
580 {
581 struct etna_resource *rsc = etna_resource(prsc);
582 /* Scanout is always attached to the base resource */
583 struct renderonly_scanout *scanout = rsc->scanout;
584
585 /*
586 * External resources are preferred, so a import->export chain of
587 * render/sampler incompatible buffers yield the same handle.
588 */
589 if (rsc->external)
590 rsc = etna_resource(rsc->external);
591
592 handle->stride = rsc->levels[0].stride;
593 handle->modifier = layout_to_modifier(rsc->layout);
594
595 if (handle->type == DRM_API_HANDLE_TYPE_SHARED) {
596 return etna_bo_get_name(rsc->bo, &handle->handle) == 0;
597 } else if (handle->type == DRM_API_HANDLE_TYPE_KMS) {
598 if (renderonly_get_handle(scanout, handle)) {
599 return TRUE;
600 } else {
601 handle->handle = etna_bo_handle(rsc->bo);
602 return TRUE;
603 }
604 } else if (handle->type == DRM_API_HANDLE_TYPE_FD) {
605 handle->handle = etna_bo_dmabuf(rsc->bo);
606 return TRUE;
607 } else {
608 return FALSE;
609 }
610 }
611
612 void
613 etna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc,
614 enum etna_resource_status status)
615 {
616 struct etna_resource *rsc;
617
618 if (!prsc)
619 return;
620
621 rsc = etna_resource(prsc);
622 rsc->status |= status;
623
624 /* TODO resources can actually be shared across contexts,
625 * so I'm not sure a single list-head will do the trick? */
626 debug_assert((rsc->pending_ctx == ctx) || !rsc->pending_ctx);
627 list_delinit(&rsc->list);
628 list_addtail(&rsc->list, &ctx->used_resources);
629 rsc->pending_ctx = ctx;
630 }
631
632 bool
633 etna_resource_has_valid_ts(struct etna_resource *rsc)
634 {
635 if (!rsc->ts_bo)
636 return false;
637
638 for (int level = 0; level <= rsc->base.last_level; level++)
639 if (rsc->levels[level].ts_valid)
640 return true;
641
642 return false;
643 }
644
645 void
646 etna_resource_screen_init(struct pipe_screen *pscreen)
647 {
648 pscreen->can_create_resource = etna_screen_can_create_resource;
649 pscreen->resource_create = etna_resource_create;
650 pscreen->resource_create_with_modifiers = etna_resource_create_modifiers;
651 pscreen->resource_from_handle = etna_resource_from_handle;
652 pscreen->resource_get_handle = etna_resource_get_handle;
653 pscreen->resource_changed = etna_resource_changed;
654 pscreen->resource_destroy = etna_resource_destroy;
655 }