• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2014 Broadcom
3  * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include "pipe/p_defines.h"
26 #include "util/u_memory.h"
27 #include "util/format/u_format.h"
28 #include "util/perf/cpu_trace.h"
29 #include "util/u_inlines.h"
30 #include "util/u_resource.h"
31 #include "util/u_surface.h"
32 #include "util/u_transfer_helper.h"
33 #include "util/u_upload_mgr.h"
34 #include "util/u_drm.h"
35 
36 #include "drm-uapi/drm_fourcc.h"
37 #include "drm-uapi/vc4_drm.h"
38 #include "vc4_screen.h"
39 #include "vc4_context.h"
40 #include "vc4_resource.h"
41 #include "vc4_tiling.h"
42 
43 static bool
vc4_resource_bo_alloc(struct vc4_resource * rsc)44 vc4_resource_bo_alloc(struct vc4_resource *rsc)
45 {
46         struct pipe_resource *prsc = &rsc->base;
47         struct pipe_screen *pscreen = prsc->screen;
48         struct vc4_bo *bo;
49 
50         if (VC4_DBG(SURFACE)) {
51                 fprintf(stderr, "alloc %p: size %d + offset %d -> %d\n",
52                         rsc,
53                         rsc->slices[0].size,
54                         rsc->slices[0].offset,
55                         rsc->slices[0].offset +
56                         rsc->slices[0].size +
57                         rsc->cube_map_stride * (prsc->array_size - 1));
58         }
59 
60         bo = vc4_bo_alloc(vc4_screen(pscreen),
61                           rsc->slices[0].offset +
62                           rsc->slices[0].size +
63                           rsc->cube_map_stride * (prsc->array_size - 1),
64                           "resource");
65         if (bo) {
66                 vc4_bo_unreference(&rsc->bo);
67                 rsc->bo = bo;
68                 return true;
69         } else {
70                 return false;
71         }
72 }
73 
74 static void
vc4_resource_transfer_unmap(struct pipe_context * pctx,struct pipe_transfer * ptrans)75 vc4_resource_transfer_unmap(struct pipe_context *pctx,
76                             struct pipe_transfer *ptrans)
77 {
78         struct vc4_context *vc4 = vc4_context(pctx);
79         struct vc4_transfer *trans = vc4_transfer(ptrans);
80 
81         if (trans->map) {
82                 struct vc4_resource *rsc = vc4_resource(ptrans->resource);
83                 struct vc4_resource_slice *slice = &rsc->slices[ptrans->level];
84 
85                 if (ptrans->usage & PIPE_MAP_WRITE) {
86                         vc4_store_tiled_image(rsc->bo->map + slice->offset +
87                                               ptrans->box.z * rsc->cube_map_stride,
88                                               slice->stride,
89                                               trans->map, ptrans->stride,
90                                               slice->tiling, rsc->cpp,
91                                               &ptrans->box);
92                 }
93                 free(trans->map);
94         }
95 
96         pipe_resource_reference(&ptrans->resource, NULL);
97         slab_free(&vc4->transfer_pool, ptrans);
98 }
99 
100 static void
vc4_map_usage_prep(struct pipe_context * pctx,struct pipe_resource * prsc,unsigned usage)101 vc4_map_usage_prep(struct pipe_context *pctx,
102                    struct pipe_resource *prsc,
103                    unsigned usage)
104 {
105         struct vc4_context *vc4 = vc4_context(pctx);
106         struct vc4_resource *rsc = vc4_resource(prsc);
107 
108         MESA_TRACE_FUNC();
109 
110         if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE) {
111                 if (vc4_resource_bo_alloc(rsc)) {
112                         /* If it might be bound as one of our vertex buffers,
113                          * make sure we re-emit vertex buffer state.
114                          */
115                         if (prsc->bind & PIPE_BIND_VERTEX_BUFFER)
116                                 vc4->dirty |= VC4_DIRTY_VTXBUF;
117                         if (prsc->bind & PIPE_BIND_CONSTANT_BUFFER)
118                                 vc4->dirty |= VC4_DIRTY_CONSTBUF;
119                 } else {
120                         /* If we failed to reallocate, flush users so that we
121                          * don't violate any syncing requirements.
122                          */
123                         vc4_flush_jobs_reading_resource(vc4, prsc);
124                 }
125         } else if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {
126                 /* If we're writing and the buffer is being used by the CL, we
127                  * have to flush the CL first. If we're only reading, we need
128                  * to flush if the CL has written our buffer.
129                  */
130                 if (usage & PIPE_MAP_WRITE)
131                         vc4_flush_jobs_reading_resource(vc4, prsc);
132                 else
133                         vc4_flush_jobs_writing_resource(vc4, prsc);
134         }
135 
136         if (usage & PIPE_MAP_WRITE) {
137                 rsc->writes++;
138                 rsc->initialized_buffers = ~0;
139         }
140 }
141 
142 static void *
vc4_resource_transfer_map(struct pipe_context * pctx,struct pipe_resource * prsc,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** pptrans)143 vc4_resource_transfer_map(struct pipe_context *pctx,
144                           struct pipe_resource *prsc,
145                           unsigned level, unsigned usage,
146                           const struct pipe_box *box,
147                           struct pipe_transfer **pptrans)
148 {
149         struct vc4_context *vc4 = vc4_context(pctx);
150         struct vc4_resource *rsc = vc4_resource(prsc);
151         struct vc4_transfer *trans;
152         struct pipe_transfer *ptrans;
153         enum pipe_format format = prsc->format;
154         char *buf;
155 
156         /* Upgrade DISCARD_RANGE to WHOLE_RESOURCE if the whole resource is
157          * being mapped.
158          */
159         if ((usage & PIPE_MAP_DISCARD_RANGE) &&
160             !(usage & PIPE_MAP_UNSYNCHRONIZED) &&
161             !(prsc->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) &&
162             prsc->last_level == 0 &&
163             prsc->width0 == box->width &&
164             prsc->height0 == box->height &&
165             prsc->depth0 == box->depth &&
166             prsc->array_size == 1 &&
167             rsc->bo->private) {
168                 usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
169         }
170 
171         vc4_map_usage_prep(pctx, prsc, usage);
172 
173         trans = slab_zalloc(&vc4->transfer_pool);
174         if (!trans)
175                 return NULL;
176 
177         /* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */
178 
179         ptrans = &trans->base;
180 
181         pipe_resource_reference(&ptrans->resource, prsc);
182         ptrans->level = level;
183         ptrans->usage = usage;
184         ptrans->box = *box;
185 
186         if (usage & PIPE_MAP_UNSYNCHRONIZED)
187                 buf = vc4_bo_map_unsynchronized(rsc->bo);
188         else
189                 buf = vc4_bo_map(rsc->bo);
190         if (!buf) {
191                 fprintf(stderr, "Failed to map bo\n");
192                 goto fail;
193         }
194 
195         *pptrans = ptrans;
196 
197         struct vc4_resource_slice *slice = &rsc->slices[level];
198         if (rsc->tiled) {
199                 /* No direct mappings of tiled, since we need to manually
200                  * tile/untile.
201                  */
202                 if (usage & PIPE_MAP_DIRECTLY)
203                         return NULL;
204 
205                 /* Our load/store routines work on entire compressed blocks. */
206                 u_box_pixels_to_blocks(&ptrans->box, &ptrans->box, format);
207 
208                 ptrans->stride = ptrans->box.width * rsc->cpp;
209                 ptrans->layer_stride = ptrans->stride * ptrans->box.height;
210 
211                 trans->map = malloc(ptrans->layer_stride * ptrans->box.depth);
212 
213                 if (usage & PIPE_MAP_READ) {
214                         vc4_load_tiled_image(trans->map, ptrans->stride,
215                                              buf + slice->offset +
216                                              ptrans->box.z * rsc->cube_map_stride,
217                                              slice->stride,
218                                              slice->tiling, rsc->cpp,
219                                              &ptrans->box);
220                 }
221                 return trans->map;
222         } else {
223                 ptrans->stride = slice->stride;
224                 ptrans->layer_stride = ptrans->stride;
225 
226                 return buf + slice->offset +
227                         ptrans->box.y / util_format_get_blockheight(format) * ptrans->stride +
228                         ptrans->box.x / util_format_get_blockwidth(format) * rsc->cpp +
229                         ptrans->box.z * rsc->cube_map_stride;
230         }
231 
232 
233 fail:
234         vc4_resource_transfer_unmap(pctx, ptrans);
235         return NULL;
236 }
237 
238 static void
vc4_texture_subdata(struct pipe_context * pctx,struct pipe_resource * prsc,unsigned level,unsigned usage,const struct pipe_box * box,const void * data,unsigned stride,uintptr_t layer_stride)239 vc4_texture_subdata(struct pipe_context *pctx,
240                     struct pipe_resource *prsc,
241                     unsigned level,
242                     unsigned usage,
243                     const struct pipe_box *box,
244                     const void *data,
245                     unsigned stride,
246                     uintptr_t layer_stride)
247 {
248         struct vc4_resource *rsc = vc4_resource(prsc);
249         struct vc4_resource_slice *slice = &rsc->slices[level];
250 
251         /* For a direct mapping, we can just take the u_transfer path. */
252         if (!rsc->tiled ||
253             box->depth != 1 ||
254             (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE)) {
255                 return u_default_texture_subdata(pctx, prsc, level, usage, box,
256                                                  data, stride, layer_stride);
257         }
258 
259         /* Otherwise, map and store the texture data directly into the tiled
260          * texture.  Note that gallium's texture_subdata may be called with
261          * obvious usage flags missing!
262          */
263         vc4_map_usage_prep(pctx, prsc, usage | (PIPE_MAP_WRITE |
264                                                 PIPE_MAP_DISCARD_RANGE));
265 
266         void *buf;
267         if (usage & PIPE_MAP_UNSYNCHRONIZED)
268                 buf = vc4_bo_map_unsynchronized(rsc->bo);
269         else
270                 buf = vc4_bo_map(rsc->bo);
271 
272         vc4_store_tiled_image(buf + slice->offset +
273                               box->z * rsc->cube_map_stride,
274                               slice->stride,
275                               (void *)data, stride,
276                               slice->tiling, rsc->cpp,
277                               box);
278 }
279 
280 static void
vc4_resource_destroy(struct pipe_screen * pscreen,struct pipe_resource * prsc)281 vc4_resource_destroy(struct pipe_screen *pscreen,
282                      struct pipe_resource *prsc)
283 {
284         struct vc4_screen *screen = vc4_screen(pscreen);
285         struct vc4_resource *rsc = vc4_resource(prsc);
286         vc4_bo_unreference(&rsc->bo);
287 
288         if (rsc->scanout)
289                 renderonly_scanout_destroy(rsc->scanout, screen->ro);
290 
291         free(rsc);
292 }
293 
294 static uint64_t
vc4_resource_modifier(struct vc4_resource * rsc)295 vc4_resource_modifier(struct vc4_resource *rsc)
296 {
297         if (rsc->tiled) {
298                 assert(rsc->slices[0].tiling == VC4_TILING_FORMAT_T);
299                 return DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
300         } else {
301                 return DRM_FORMAT_MOD_LINEAR;
302         }
303 }
304 
305 static bool
vc4_resource_get_handle(struct pipe_screen * pscreen,struct pipe_context * pctx,struct pipe_resource * prsc,struct winsys_handle * whandle,unsigned usage)306 vc4_resource_get_handle(struct pipe_screen *pscreen,
307                         struct pipe_context *pctx,
308                         struct pipe_resource *prsc,
309                         struct winsys_handle *whandle,
310                         unsigned usage)
311 {
312         struct vc4_screen *screen = vc4_screen(pscreen);
313         struct vc4_resource *rsc = vc4_resource(prsc);
314 
315         whandle->stride = rsc->slices[0].stride;
316         whandle->offset = 0;
317         whandle->modifier = vc4_resource_modifier(rsc);
318 
319         /* If we're passing some reference to our BO out to some other part of
320          * the system, then we can't do any optimizations about only us being
321          * the ones seeing it (like BO caching or shadow update avoidance).
322          */
323         rsc->bo->private = false;
324 
325         switch (whandle->type) {
326         case WINSYS_HANDLE_TYPE_SHARED:
327                 if (screen->ro) {
328                         /* This could probably be supported, assuming that a
329                          * control node was used for pl111.
330                          */
331                         fprintf(stderr, "flink unsupported with pl111\n");
332                         return false;
333                 }
334 
335                 return vc4_bo_flink(rsc->bo, &whandle->handle);
336         case WINSYS_HANDLE_TYPE_KMS:
337                 if (screen->ro) {
338                         return renderonly_get_handle(rsc->scanout, whandle);
339                 }
340                 whandle->handle = rsc->bo->handle;
341                 return true;
342         case WINSYS_HANDLE_TYPE_FD:
343                 /* FDs are cross-device, so we can export directly from vc4.
344                  */
345                 whandle->handle = vc4_bo_get_dmabuf(rsc->bo);
346                 return whandle->handle != -1;
347         }
348 
349         return false;
350 }
351 
352 static bool
vc4_resource_get_param(struct pipe_screen * pscreen,struct pipe_context * pctx,struct pipe_resource * prsc,unsigned plane,unsigned layer,unsigned level,enum pipe_resource_param param,unsigned usage,uint64_t * value)353 vc4_resource_get_param(struct pipe_screen *pscreen,
354                        struct pipe_context *pctx, struct pipe_resource *prsc,
355                        unsigned plane, unsigned layer, unsigned level,
356                        enum pipe_resource_param param,
357                        unsigned usage, uint64_t *value)
358 {
359         struct vc4_resource *rsc =
360                 (struct vc4_resource *)util_resource_at_index(prsc, plane);
361 
362         switch (param) {
363         case PIPE_RESOURCE_PARAM_STRIDE:
364                 *value = rsc->slices[level].stride;
365                 return true;
366         case PIPE_RESOURCE_PARAM_OFFSET:
367                 *value = rsc->slices[level].offset;
368                 return true;
369         case PIPE_RESOURCE_PARAM_MODIFIER:
370                 *value = vc4_resource_modifier(rsc);
371                 return true;
372         case PIPE_RESOURCE_PARAM_NPLANES:
373                 *value = util_resource_num(prsc);
374                 return true;
375         default:
376                 return false;
377         }
378 }
379 
380 static void
vc4_setup_slices(struct vc4_resource * rsc,const char * caller,bool force_format_t)381 vc4_setup_slices(struct vc4_resource *rsc, const char *caller,
382                  bool force_format_t)
383 {
384         struct pipe_resource *prsc = &rsc->base;
385         uint32_t width = prsc->width0;
386         uint32_t height = prsc->height0;
387         if (prsc->format == PIPE_FORMAT_ETC1_RGB8) {
388                 width = (width + 3) >> 2;
389                 height = (height + 3) >> 2;
390         }
391 
392         uint32_t pot_width = util_next_power_of_two(width);
393         uint32_t pot_height = util_next_power_of_two(height);
394         uint32_t offset = 0;
395         uint32_t utile_w = vc4_utile_width(rsc->cpp);
396         uint32_t utile_h = vc4_utile_height(rsc->cpp);
397 
398         for (int i = prsc->last_level; i >= 0; i--) {
399                 struct vc4_resource_slice *slice = &rsc->slices[i];
400 
401                 uint32_t level_width, level_height;
402                 if (i == 0) {
403                         level_width = width;
404                         level_height = height;
405                 } else {
406                         level_width = u_minify(pot_width, i);
407                         level_height = u_minify(pot_height, i);
408                 }
409 
410                 if (!rsc->tiled) {
411                         slice->tiling = VC4_TILING_FORMAT_LINEAR;
412                         if (prsc->nr_samples > 1) {
413                                 /* MSAA (4x) surfaces are stored as raw tile buffer contents. */
414                                 level_width = align(level_width, 32);
415                                 level_height = align(level_height, 32);
416                         } else {
417                                 level_width = align(level_width, utile_w);
418                         }
419                 } else {
420                         if (!force_format_t &&
421                             vc4_size_is_lt(level_width, level_height,
422                                            rsc->cpp)) {
423                                 slice->tiling = VC4_TILING_FORMAT_LT;
424                                 level_width = align(level_width, utile_w);
425                                 level_height = align(level_height, utile_h);
426                         } else {
427                                 slice->tiling = VC4_TILING_FORMAT_T;
428                                 level_width = align(level_width,
429                                                     4 * 2 * utile_w);
430                                 level_height = align(level_height,
431                                                      4 * 2 * utile_h);
432                         }
433                 }
434 
435                 slice->offset = offset;
436                 slice->stride = (level_width * rsc->cpp *
437                                  MAX2(prsc->nr_samples, 1));
438                 slice->size = level_height * slice->stride;
439 
440                 offset += slice->size;
441 
442                 if (VC4_DBG(SURFACE)) {
443                         static const char tiling_chars[] = {
444                                 [VC4_TILING_FORMAT_LINEAR] = 'R',
445                                 [VC4_TILING_FORMAT_LT] = 'L',
446                                 [VC4_TILING_FORMAT_T] = 'T'
447                         };
448                         fprintf(stderr,
449                                 "rsc %s %p (format %s: vc4 %d), %dx%d: "
450                                 "level %d (%c) -> %dx%d, stride %d@0x%08x\n",
451                                 caller, rsc,
452                                 util_format_short_name(prsc->format),
453                                 rsc->vc4_format,
454                                 prsc->width0, prsc->height0,
455                                 i, tiling_chars[slice->tiling],
456                                 level_width, level_height,
457                                 slice->stride, slice->offset);
458                 }
459         }
460 
461         /* The texture base pointer that has to point to level 0 doesn't have
462          * intra-page bits, so we have to align it, and thus shift up all the
463          * smaller slices.
464          */
465         uint32_t page_align_offset = (align(rsc->slices[0].offset, 4096) -
466                                       rsc->slices[0].offset);
467         if (page_align_offset) {
468                 for (int i = 0; i <= prsc->last_level; i++)
469                         rsc->slices[i].offset += page_align_offset;
470         }
471 
472         /* Cube map faces appear as whole miptrees at a page-aligned offset
473          * from the first face's miptree.
474          */
475         if (prsc->target == PIPE_TEXTURE_CUBE) {
476                 rsc->cube_map_stride = align(rsc->slices[0].offset +
477                                              rsc->slices[0].size, 4096);
478         }
479 }
480 
481 static struct vc4_resource *
vc4_resource_setup(struct pipe_screen * pscreen,const struct pipe_resource * tmpl)482 vc4_resource_setup(struct pipe_screen *pscreen,
483                    const struct pipe_resource *tmpl)
484 {
485         struct vc4_resource *rsc = CALLOC_STRUCT(vc4_resource);
486         if (!rsc)
487                 return NULL;
488         struct pipe_resource *prsc = &rsc->base;
489 
490         *prsc = *tmpl;
491 
492         pipe_reference_init(&prsc->reference, 1);
493         prsc->screen = pscreen;
494 
495         if (prsc->nr_samples <= 1)
496                 rsc->cpp = util_format_get_blocksize(tmpl->format);
497         else
498                 rsc->cpp = sizeof(uint32_t);
499 
500         assert(rsc->cpp);
501 
502         return rsc;
503 }
504 
505 static enum vc4_texture_data_type
get_resource_texture_format(struct pipe_resource * prsc)506 get_resource_texture_format(struct pipe_resource *prsc)
507 {
508         struct vc4_resource *rsc = vc4_resource(prsc);
509         uint8_t format = vc4_get_tex_format(prsc->format);
510 
511         if (!rsc->tiled) {
512                 if (prsc->nr_samples > 1) {
513                         return ~0;
514                 } else {
515                         if (format == VC4_TEXTURE_TYPE_RGBA8888)
516                                 return VC4_TEXTURE_TYPE_RGBA32R;
517                         else
518                                 return ~0;
519                 }
520         }
521 
522         return format;
523 }
524 
525 static struct pipe_resource *
vc4_resource_create_with_modifiers(struct pipe_screen * pscreen,const struct pipe_resource * tmpl,const uint64_t * modifiers,int count)526 vc4_resource_create_with_modifiers(struct pipe_screen *pscreen,
527                                    const struct pipe_resource *tmpl,
528                                    const uint64_t *modifiers,
529                                    int count)
530 {
531         struct vc4_screen *screen = vc4_screen(pscreen);
532         struct vc4_resource *rsc = vc4_resource_setup(pscreen, tmpl);
533         struct pipe_resource *prsc = &rsc->base;
534         /* Use a tiled layout if we can, for better 3D performance. */
535         bool should_tile = true;
536 
537         /* VBOs/PBOs are untiled (and 1 height). */
538         if (tmpl->target == PIPE_BUFFER)
539                 should_tile = false;
540 
541         /* MSAA buffers are linear. */
542         if (tmpl->nr_samples > 1)
543                 should_tile = false;
544 
545         /* No tiling when we're sharing with another device (pl111). */
546         if (screen->ro && (tmpl->bind & PIPE_BIND_SCANOUT))
547                 should_tile = false;
548 
549         /* Cursors are always linear, and the user can request linear as well.
550          */
551         if (tmpl->bind & (PIPE_BIND_LINEAR | PIPE_BIND_CURSOR))
552                 should_tile = false;
553 
554         /* No shared objects with LT format -- the kernel only has T-format
555          * metadata.  LT objects are small enough it's not worth the trouble to
556          * give them metadata to tile.
557          */
558         if ((tmpl->bind & (PIPE_BIND_SHARED | PIPE_BIND_SCANOUT)) &&
559             vc4_size_is_lt(prsc->width0, prsc->height0, rsc->cpp))
560                 should_tile = false;
561 
562         /* If we're sharing or scanning out, we need the ioctl present to
563          * inform the kernel or the other side.
564          */
565         if ((tmpl->bind & (PIPE_BIND_SHARED |
566                            PIPE_BIND_SCANOUT)) && !screen->has_tiling_ioctl)
567                 should_tile = false;
568 
569         /* No user-specified modifier; determine our own. */
570         if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
571                 rsc->tiled = should_tile;
572         } else if (should_tile &&
573                    drm_find_modifier(DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED,
574                                  modifiers, count)) {
575                 rsc->tiled = true;
576         } else if (drm_find_modifier(DRM_FORMAT_MOD_LINEAR, modifiers, count)) {
577                 rsc->tiled = false;
578         } else {
579                 fprintf(stderr, "Unsupported modifier requested\n");
580                 return NULL;
581         }
582 
583         if (tmpl->target != PIPE_BUFFER)
584                 rsc->vc4_format = get_resource_texture_format(prsc);
585 
586         vc4_setup_slices(rsc, "create", tmpl->bind & PIPE_BIND_SHARED);
587         if (!vc4_resource_bo_alloc(rsc))
588                 goto fail;
589 
590         if (screen->has_tiling_ioctl) {
591                 uint64_t modifier;
592                 if (rsc->tiled)
593                         modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
594                 else
595                         modifier = DRM_FORMAT_MOD_LINEAR;
596                 struct drm_vc4_set_tiling set_tiling = {
597                         .handle = rsc->bo->handle,
598                         .modifier = modifier,
599                 };
600                 int ret = vc4_ioctl(screen->fd, DRM_IOCTL_VC4_SET_TILING,
601                                     &set_tiling);
602                 if (ret != 0)
603                         goto fail;
604         }
605 
606         /* Set up the "scanout resource" (the dmabuf export of our buffer to
607          * the KMS handle) if the buffer might ever have
608          * resource_get_handle(WINSYS_HANDLE_TYPE_KMS) called on it.
609          * create_with_modifiers() doesn't give us usage flags, so we have to
610          * assume that all calls with modifiers are scanout-possible.
611          */
612         if (screen->ro &&
613             ((tmpl->bind & PIPE_BIND_SCANOUT) ||
614              !(count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID))) {
615                 rsc->scanout =
616                         renderonly_scanout_for_resource(prsc, screen->ro, NULL);
617                 if (!rsc->scanout)
618                         goto fail;
619         }
620 
621         vc4_bo_label(screen, rsc->bo, "%sresource %dx%d@%d/%d",
622                      (tmpl->bind & PIPE_BIND_SCANOUT) ? "scanout " : "",
623                      tmpl->width0, tmpl->height0,
624                      rsc->cpp * 8, prsc->last_level);
625 
626         return prsc;
627 fail:
628         vc4_resource_destroy(pscreen, prsc);
629         return NULL;
630 }
631 
632 struct pipe_resource *
vc4_resource_create(struct pipe_screen * pscreen,const struct pipe_resource * tmpl)633 vc4_resource_create(struct pipe_screen *pscreen,
634                     const struct pipe_resource *tmpl)
635 {
636         const uint64_t mod = DRM_FORMAT_MOD_INVALID;
637         return vc4_resource_create_with_modifiers(pscreen, tmpl, &mod, 1);
638 }
639 
640 static struct pipe_resource *
vc4_resource_from_handle(struct pipe_screen * pscreen,const struct pipe_resource * tmpl,struct winsys_handle * whandle,unsigned usage)641 vc4_resource_from_handle(struct pipe_screen *pscreen,
642                          const struct pipe_resource *tmpl,
643                          struct winsys_handle *whandle,
644                          unsigned usage)
645 {
646         struct vc4_screen *screen = vc4_screen(pscreen);
647         struct vc4_resource *rsc = vc4_resource_setup(pscreen, tmpl);
648         struct pipe_resource *prsc = &rsc->base;
649         struct vc4_resource_slice *slice = &rsc->slices[0];
650 
651         if (!rsc)
652                 return NULL;
653 
654         switch (whandle->type) {
655         case WINSYS_HANDLE_TYPE_SHARED:
656                 rsc->bo = vc4_bo_open_name(screen, whandle->handle);
657                 break;
658         case WINSYS_HANDLE_TYPE_FD:
659                 rsc->bo = vc4_bo_open_dmabuf(screen, whandle->handle);
660                 break;
661         default:
662                 fprintf(stderr,
663                         "Attempt to import unsupported handle type %d\n",
664                         whandle->type);
665         }
666 
667         if (!rsc->bo)
668                 goto fail;
669 
670         struct drm_vc4_get_tiling get_tiling = {
671                 .handle = rsc->bo->handle,
672         };
673         int ret = vc4_ioctl(screen->fd, DRM_IOCTL_VC4_GET_TILING, &get_tiling);
674 
675         if (ret != 0) {
676                 whandle->modifier = DRM_FORMAT_MOD_LINEAR;
677         } else if (whandle->modifier == DRM_FORMAT_MOD_INVALID) {
678                 whandle->modifier = get_tiling.modifier;
679         } else if (whandle->modifier != get_tiling.modifier) {
680                 fprintf(stderr,
681                         "Modifier 0x%llx vs. tiling (0x%llx) mismatch\n",
682                         (long long)whandle->modifier, get_tiling.modifier);
683                 goto fail;
684         }
685 
686         switch (whandle->modifier) {
687         case DRM_FORMAT_MOD_LINEAR:
688                 rsc->tiled = false;
689                 break;
690         case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
691                 rsc->tiled = true;
692                 break;
693         default:
694                 fprintf(stderr,
695                         "Attempt to import unsupported modifier 0x%llx\n",
696                         (long long)whandle->modifier);
697                 goto fail;
698         }
699 
700         rsc->vc4_format = get_resource_texture_format(prsc);
701         vc4_setup_slices(rsc, "import", true);
702 
703         if (whandle->offset != 0) {
704                 if (rsc->tiled) {
705                         fprintf(stderr,
706                                 "Attempt to import unsupported "
707                                 "winsys offset %u\n",
708                                 whandle->offset);
709                         goto fail;
710                 }
711 
712                 rsc->slices[0].offset += whandle->offset;
713 
714                 if (rsc->slices[0].offset + rsc->slices[0].size >
715                     rsc->bo->size) {
716                         fprintf(stderr, "Attempt to import "
717                                 "with overflowing offset (%d + %d > %d)\n",
718                                 whandle->offset,
719                                 rsc->slices[0].size,
720                                 rsc->bo->size);
721                         goto fail;
722                 }
723         }
724 
725         if (screen->ro) {
726                 /* Make sure that renderonly has a handle to our buffer in the
727                  * display's fd, so that a later renderonly_get_handle()
728                  * returns correct handles or GEM names.
729                  */
730                 rsc->scanout =
731                         renderonly_create_gpu_import_for_resource(prsc,
732                                                                   screen->ro,
733                                                                   NULL);
734         }
735 
736         if (rsc->tiled && whandle->stride != slice->stride) {
737                 static bool warned = false;
738                 if (!warned) {
739                         warned = true;
740                         fprintf(stderr,
741                                 "Attempting to import %dx%d %s with "
742                                 "unsupported stride %d instead of %d\n",
743                                 prsc->width0, prsc->height0,
744                                 util_format_short_name(prsc->format),
745                                 whandle->stride,
746                                 slice->stride);
747                 }
748                 goto fail;
749         } else if (!rsc->tiled) {
750                 slice->stride = whandle->stride;
751         }
752 
753         return prsc;
754 
755 fail:
756         vc4_resource_destroy(pscreen, prsc);
757         return NULL;
758 }
759 
760 static struct pipe_surface *
vc4_create_surface(struct pipe_context * pctx,struct pipe_resource * ptex,const struct pipe_surface * surf_tmpl)761 vc4_create_surface(struct pipe_context *pctx,
762                    struct pipe_resource *ptex,
763                    const struct pipe_surface *surf_tmpl)
764 {
765         struct vc4_surface *surface = CALLOC_STRUCT(vc4_surface);
766         struct vc4_resource *rsc = vc4_resource(ptex);
767 
768         if (!surface)
769                 return NULL;
770 
771         assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
772 
773         struct pipe_surface *psurf = &surface->base;
774         unsigned level = surf_tmpl->u.tex.level;
775 
776         pipe_reference_init(&psurf->reference, 1);
777         pipe_resource_reference(&psurf->texture, ptex);
778 
779         psurf->context = pctx;
780         psurf->format = surf_tmpl->format;
781         psurf->width = u_minify(ptex->width0, level);
782         psurf->height = u_minify(ptex->height0, level);
783         psurf->u.tex.level = level;
784         psurf->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
785         psurf->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
786         surface->offset = (rsc->slices[level].offset +
787                            psurf->u.tex.first_layer * rsc->cube_map_stride);
788         surface->tiling = rsc->slices[level].tiling;
789 
790         return &surface->base;
791 }
792 
793 static void
vc4_surface_destroy(struct pipe_context * pctx,struct pipe_surface * psurf)794 vc4_surface_destroy(struct pipe_context *pctx, struct pipe_surface *psurf)
795 {
796         pipe_resource_reference(&psurf->texture, NULL);
797         FREE(psurf);
798 }
799 
800 static void
vc4_dump_surface_non_msaa(struct pipe_surface * psurf)801 vc4_dump_surface_non_msaa(struct pipe_surface *psurf)
802 {
803         struct pipe_resource *prsc = psurf->texture;
804         struct vc4_resource *rsc = vc4_resource(prsc);
805         uint32_t *map = vc4_bo_map(rsc->bo);
806         uint32_t stride = rsc->slices[0].stride / 4;
807         uint32_t width = psurf->width;
808         uint32_t height = psurf->height;
809         uint32_t chunk_w = width / 79;
810         uint32_t chunk_h = height / 40;
811         uint32_t found_colors[10] = { 0 };
812         uint32_t num_found_colors = 0;
813 
814         if (rsc->vc4_format != VC4_TEXTURE_TYPE_RGBA32R) {
815                 fprintf(stderr, "%s: Unsupported format %s\n",
816                         __func__, util_format_short_name(psurf->format));
817                 return;
818         }
819 
820         for (int by = 0; by < height; by += chunk_h) {
821                 for (int bx = 0; bx < width; bx += chunk_w) {
822                         int all_found_color = -1; /* nothing found */
823 
824                         for (int y = by; y < MIN2(height, by + chunk_h); y++) {
825                                 for (int x = bx; x < MIN2(width, bx + chunk_w); x++) {
826                                         uint32_t pix = map[y * stride + x];
827 
828                                         int i;
829                                         for (i = 0; i < num_found_colors; i++) {
830                                                 if (pix == found_colors[i])
831                                                         break;
832                                         }
833                                         if (i == num_found_colors &&
834                                             num_found_colors <
835                                             ARRAY_SIZE(found_colors)) {
836                                                 found_colors[num_found_colors++] = pix;
837                                         }
838 
839                                         if (i < num_found_colors) {
840                                                 if (all_found_color == -1)
841                                                         all_found_color = i;
842                                                 else if (i != all_found_color)
843                                                         all_found_color = ARRAY_SIZE(found_colors);
844                                         }
845                                 }
846                         }
847                         /* If all pixels for this chunk have a consistent
848                          * value, then print a character for it.  Either a
849                          * fixed name (particularly common for piglit tests),
850                          * or a runtime-generated number.
851                          */
852                         if (all_found_color >= 0 &&
853                             all_found_color < ARRAY_SIZE(found_colors)) {
854                                 static const struct {
855                                         uint32_t val;
856                                         const char *c;
857                                 } named_colors[] = {
858                                         { 0xff000000, "█" },
859                                         { 0x00000000, "█" },
860                                         { 0xffff0000, "r" },
861                                         { 0xff00ff00, "g" },
862                                         { 0xff0000ff, "b" },
863                                         { 0xffffffff, "w" },
864                                 };
865                                 int i;
866                                 for (i = 0; i < ARRAY_SIZE(named_colors); i++) {
867                                         if (named_colors[i].val ==
868                                             found_colors[all_found_color]) {
869                                                 fprintf(stderr, "%s",
870                                                         named_colors[i].c);
871                                                 break;
872                                         }
873                                 }
874                                 /* For unnamed colors, print a number and the
875                                  * numbers will have values printed at the
876                                  * end.
877                                  */
878                                 if (i == ARRAY_SIZE(named_colors)) {
879                                         fprintf(stderr, "%c",
880                                                 '0' + all_found_color);
881                                 }
882                         } else {
883                                 /* If there's no consistent color, print this.
884                                  */
885                                 fprintf(stderr, ".");
886                         }
887                 }
888                 fprintf(stderr, "\n");
889         }
890 
891         for (int i = 0; i < num_found_colors; i++) {
892                 fprintf(stderr, "color %d: 0x%08x\n", i, found_colors[i]);
893         }
894 }
895 
896 static uint32_t
vc4_surface_msaa_get_sample(struct pipe_surface * psurf,uint32_t x,uint32_t y,uint32_t sample)897 vc4_surface_msaa_get_sample(struct pipe_surface *psurf,
898                             uint32_t x, uint32_t y, uint32_t sample)
899 {
900         struct pipe_resource *prsc = psurf->texture;
901         struct vc4_resource *rsc = vc4_resource(prsc);
902         uint32_t tile_w = 32, tile_h = 32;
903         uint32_t tiles_w = DIV_ROUND_UP(psurf->width, 32);
904 
905         uint32_t tile_x = x / tile_w;
906         uint32_t tile_y = y / tile_h;
907         uint32_t *tile = (vc4_bo_map(rsc->bo) +
908                           VC4_TILE_BUFFER_SIZE * (tile_y * tiles_w + tile_x));
909         uint32_t subtile_x = x % tile_w;
910         uint32_t subtile_y = y % tile_h;
911 
912         uint32_t quad_samples = VC4_MAX_SAMPLES * 4;
913         uint32_t tile_stride = quad_samples * tile_w / 2;
914 
915         return *((uint32_t *)tile +
916                  (subtile_y >> 1) * tile_stride +
917                  (subtile_x >> 1) * quad_samples +
918                  ((subtile_y & 1) << 1) +
919                  (subtile_x & 1) +
920                  sample);
921 }
922 
923 static void
vc4_dump_surface_msaa_char(struct pipe_surface * psurf,uint32_t start_x,uint32_t start_y,uint32_t w,uint32_t h)924 vc4_dump_surface_msaa_char(struct pipe_surface *psurf,
925                            uint32_t start_x, uint32_t start_y,
926                            uint32_t w, uint32_t h)
927 {
928         bool all_same_color = true;
929         uint32_t all_pix = 0;
930 
931         for (int y = start_y; y < start_y + h; y++) {
932                 for (int x = start_x; x < start_x + w; x++) {
933                         for (int s = 0; s < VC4_MAX_SAMPLES; s++) {
934                                 uint32_t pix = vc4_surface_msaa_get_sample(psurf,
935                                                                            x, y,
936                                                                            s);
937                                 if (x == start_x && y == start_y)
938                                         all_pix = pix;
939                                 else if (all_pix != pix)
940                                         all_same_color = false;
941                         }
942                 }
943         }
944         if (all_same_color) {
945                 static const struct {
946                         uint32_t val;
947                         const char *c;
948                 } named_colors[] = {
949                         { 0xff000000, "█" },
950                         { 0x00000000, "█" },
951                         { 0xffff0000, "r" },
952                         { 0xff00ff00, "g" },
953                         { 0xff0000ff, "b" },
954                         { 0xffffffff, "w" },
955                 };
956                 int i;
957                 for (i = 0; i < ARRAY_SIZE(named_colors); i++) {
958                         if (named_colors[i].val == all_pix) {
959                                 fprintf(stderr, "%s",
960                                         named_colors[i].c);
961                                 return;
962                         }
963                 }
964                 fprintf(stderr, "x");
965         } else {
966                 fprintf(stderr, ".");
967         }
968 }
969 
970 static void
vc4_dump_surface_msaa(struct pipe_surface * psurf)971 vc4_dump_surface_msaa(struct pipe_surface *psurf)
972 {
973         uint32_t tile_w = 32, tile_h = 32;
974         uint32_t tiles_w = DIV_ROUND_UP(psurf->width, tile_w);
975         uint32_t tiles_h = DIV_ROUND_UP(psurf->height, tile_h);
976         uint32_t char_w = 140, char_h = 60;
977         uint32_t char_w_per_tile = char_w / tiles_w - 1;
978         uint32_t char_h_per_tile = char_h / tiles_h - 1;
979 
980         fprintf(stderr, "Surface: %dx%d (%dx MSAA)\n",
981                 psurf->width, psurf->height, psurf->texture->nr_samples);
982 
983         for (int x = 0; x < (char_w_per_tile + 1) * tiles_w; x++)
984                 fprintf(stderr, "-");
985         fprintf(stderr, "\n");
986 
987         for (int ty = 0; ty < psurf->height; ty += tile_h) {
988                 for (int y = 0; y < char_h_per_tile; y++) {
989 
990                         for (int tx = 0; tx < psurf->width; tx += tile_w) {
991                                 for (int x = 0; x < char_w_per_tile; x++) {
992                                         uint32_t bx1 = (x * tile_w /
993                                                         char_w_per_tile);
994                                         uint32_t bx2 = ((x + 1) * tile_w /
995                                                         char_w_per_tile);
996                                         uint32_t by1 = (y * tile_h /
997                                                         char_h_per_tile);
998                                         uint32_t by2 = ((y + 1) * tile_h /
999                                                         char_h_per_tile);
1000 
1001                                         vc4_dump_surface_msaa_char(psurf,
1002                                                                    tx + bx1,
1003                                                                    ty + by1,
1004                                                                    bx2 - bx1,
1005                                                                    by2 - by1);
1006                                 }
1007                                 fprintf(stderr, "|");
1008                         }
1009                         fprintf(stderr, "\n");
1010                 }
1011 
1012                 for (int x = 0; x < (char_w_per_tile + 1) * tiles_w; x++)
1013                         fprintf(stderr, "-");
1014                 fprintf(stderr, "\n");
1015         }
1016 }
1017 
1018 /** Debug routine to dump the contents of an 8888 surface to the console */
1019 void
vc4_dump_surface(struct pipe_surface * psurf)1020 vc4_dump_surface(struct pipe_surface *psurf)
1021 {
1022         if (!psurf)
1023                 return;
1024 
1025         if (psurf->texture->nr_samples > 1)
1026                 vc4_dump_surface_msaa(psurf);
1027         else
1028                 vc4_dump_surface_non_msaa(psurf);
1029 }
1030 
1031 static void
vc4_flush_resource(struct pipe_context * pctx,struct pipe_resource * prsc)1032 vc4_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
1033 {
1034         /* All calls to flush_resource are followed by a flush of the context,
1035          * so there's nothing to do. Still, if the resource is going to be
1036          * shared and it is tiled, only T format is valid, so we need to
1037          * convert it.
1038          */
1039         struct vc4_resource *rsc = vc4_resource(prsc);
1040         struct vc4_screen *screen = vc4_screen(prsc->screen);
1041         if (rsc->tiled &&
1042             (rsc->slices[0].tiling != VC4_TILING_FORMAT_T ||
1043             !screen->has_tiling_ioctl)) {
1044                 /* Shared resources must be not mipmapped */
1045                 assert(prsc->last_level == 0);
1046                 /* Shared resources must not be multisampled */
1047                 assert(prsc->nr_samples <= 1);
1048 
1049                 struct pipe_resource ptmpl = *prsc;
1050                 ptmpl.bind |= PIPE_BIND_SHARED;
1051                 struct vc4_resource *new_rsc =
1052                         vc4_resource(pctx->screen->resource_create(pctx->screen,
1053                                                                    &ptmpl));
1054                 assert(new_rsc);
1055 
1056                 struct pipe_blit_info blit = { 0 };
1057                 u_box_3d(0, 0, 0,
1058                          prsc->width0, prsc->height0, prsc->depth0,
1059                          &blit.dst.box);
1060                 blit.src.box = blit.dst.box;
1061                 blit.dst.resource = &new_rsc->base;
1062                 blit.dst.format = new_rsc->base.format;
1063                 blit.dst.level = 0;
1064                 blit.src.resource = prsc;
1065                 blit.src.format = prsc->format;
1066                 blit.src.level = 0;
1067                 blit.mask = util_format_get_mask(blit.src.format);
1068                 blit.filter = PIPE_TEX_FILTER_NEAREST;
1069 
1070                 vc4_blit(pctx, &blit);
1071 
1072                 rsc->base.bind = new_rsc->base.bind;
1073                 /* Swap the BOs */
1074                 struct vc4_bo *old_bo = rsc->bo;
1075                 rsc->bo = new_rsc->bo;
1076                 new_rsc->bo = old_bo;
1077 
1078                 /* Copy the affected fields */
1079                 rsc->slices[0] = new_rsc->slices[0];
1080                 rsc->cube_map_stride = new_rsc->cube_map_stride;
1081                 rsc->tiled = new_rsc->tiled;
1082 
1083                 struct pipe_resource *new_prsc = (struct pipe_resource *)&new_rsc;
1084                 pipe_resource_reference(&new_prsc, NULL);
1085         }
1086 }
1087 
1088 void
vc4_update_shadow_baselevel_texture(struct pipe_context * pctx,struct pipe_sampler_view * pview)1089 vc4_update_shadow_baselevel_texture(struct pipe_context *pctx,
1090                                     struct pipe_sampler_view *pview)
1091 {
1092         struct vc4_context *vc4 = vc4_context(pctx);
1093         struct vc4_sampler_view *view = vc4_sampler_view(pview);
1094         struct vc4_resource *shadow = vc4_resource(view->texture);
1095         struct vc4_resource *orig = vc4_resource(pview->texture);
1096 
1097         assert(view->texture != pview->texture);
1098 
1099         if (shadow->writes == orig->writes && orig->bo->private)
1100                 return;
1101 
1102         perf_debug("Updating %dx%d@%d shadow texture due to %s\n",
1103                    orig->base.width0, orig->base.height0,
1104                    pview->u.tex.first_level,
1105                    pview->u.tex.first_level ? "base level" : "raster layout");
1106 
1107         for (int i = 0; i <= shadow->base.last_level; i++) {
1108                 unsigned width = u_minify(shadow->base.width0, i);
1109                 unsigned height = u_minify(shadow->base.height0, i);
1110                 struct pipe_blit_info info = {
1111                         .dst = {
1112                                 .resource = &shadow->base,
1113                                 .level = i,
1114                                 .box = {
1115                                         .x = 0,
1116                                         .y = 0,
1117                                         .z = 0,
1118                                         .width = width,
1119                                         .height = height,
1120                                         .depth = 1,
1121                                 },
1122                                 .format = shadow->base.format,
1123                         },
1124                         .src = {
1125                                 .resource = &orig->base,
1126                                 .level = pview->u.tex.first_level + i,
1127                                 .box = {
1128                                         .x = 0,
1129                                         .y = 0,
1130                                         .z = 0,
1131                                         .width = width,
1132                                         .height = height,
1133                                         .depth = 1,
1134                                 },
1135                                 .format = orig->base.format,
1136                         },
1137                         .mask = util_format_get_mask(orig->base.format),
1138                 };
1139                 pctx->blit(pctx, &info);
1140         }
1141 
1142         shadow->writes = orig->writes;
1143 }
1144 
1145 /**
1146  * Converts a 4-byte index buffer to 2 bytes.
1147  *
1148  * Since GLES2 only has support for 1 and 2-byte indices, the hardware doesn't
1149  * include 4-byte index support, and we have to shrink it down.
1150  *
1151  * There's no fallback support for when indices end up being larger than 2^16,
1152  * though it will at least assertion fail.  Also, if the original index data
1153  * was in user memory, it would be nice to not have uploaded it to a VBO
1154  * before translating.
1155  */
1156 struct pipe_resource *
vc4_get_shadow_index_buffer(struct pipe_context * pctx,const struct pipe_draw_info * info,uint32_t offset,uint32_t count,uint32_t * shadow_offset)1157 vc4_get_shadow_index_buffer(struct pipe_context *pctx,
1158                             const struct pipe_draw_info *info,
1159                             uint32_t offset,
1160                             uint32_t count,
1161                             uint32_t *shadow_offset)
1162 {
1163         struct vc4_context *vc4 = vc4_context(pctx);
1164         struct vc4_resource *orig = vc4_resource(info->index.resource);
1165         perf_debug("Fallback conversion for %d uint indices\n", count);
1166 
1167         void *data;
1168         struct pipe_resource *shadow_rsc = NULL;
1169         u_upload_alloc(vc4->uploader, 0, count * 2, 4,
1170                        shadow_offset, &shadow_rsc, &data);
1171         uint16_t *dst = data;
1172 
1173         struct pipe_transfer *src_transfer = NULL;
1174         const uint32_t *src;
1175         if (info->has_user_indices) {
1176                 src = (uint32_t*)((char*)info->index.user + offset);
1177         } else {
1178                 src = pipe_buffer_map_range(pctx, &orig->base,
1179                                             offset,
1180                                             count * 4,
1181                                             PIPE_MAP_READ, &src_transfer);
1182         }
1183 
1184         for (int i = 0; i < count; i++) {
1185                 uint32_t src_index = src[i];
1186                 assert(src_index <= 0xffff);
1187                 dst[i] = src_index;
1188         }
1189 
1190         if (src_transfer)
1191                 pctx->buffer_unmap(pctx, src_transfer);
1192 
1193         return shadow_rsc;
1194 }
1195 
1196 static const struct u_transfer_vtbl transfer_vtbl = {
1197         .resource_create          = vc4_resource_create,
1198         .resource_destroy         = vc4_resource_destroy,
1199         .transfer_map             = vc4_resource_transfer_map,
1200         .transfer_unmap           = vc4_resource_transfer_unmap,
1201         .transfer_flush_region    = u_default_transfer_flush_region,
1202 };
1203 
1204 void
vc4_resource_screen_init(struct pipe_screen * pscreen)1205 vc4_resource_screen_init(struct pipe_screen *pscreen)
1206 {
1207         struct vc4_screen *screen = vc4_screen(pscreen);
1208 
1209         pscreen->resource_create = vc4_resource_create;
1210         pscreen->resource_create_with_modifiers =
1211                 vc4_resource_create_with_modifiers;
1212         pscreen->resource_from_handle = vc4_resource_from_handle;
1213         pscreen->resource_get_handle = vc4_resource_get_handle;
1214         pscreen->resource_get_param = vc4_resource_get_param;
1215         pscreen->resource_destroy = vc4_resource_destroy;
1216         pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
1217                                                             U_TRANSFER_HELPER_MSAA_MAP);
1218 
1219         /* Test if the kernel has GET_TILING; it will return -EINVAL if the
1220          * ioctl does not exist, but -ENOENT if we pass an impossible handle.
1221          * 0 cannot be a valid GEM object, so use that.
1222          */
1223         struct drm_vc4_get_tiling get_tiling = {
1224                 .handle = 0x0,
1225         };
1226         int ret = vc4_ioctl(screen->fd, DRM_IOCTL_VC4_GET_TILING, &get_tiling);
1227         if (ret == -1 && errno == ENOENT)
1228                 screen->has_tiling_ioctl = true;
1229 }
1230 
1231 void
vc4_resource_context_init(struct pipe_context * pctx)1232 vc4_resource_context_init(struct pipe_context *pctx)
1233 {
1234         pctx->buffer_map = u_transfer_helper_transfer_map;
1235         pctx->texture_map = u_transfer_helper_transfer_map;
1236         pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
1237         pctx->buffer_unmap = u_transfer_helper_transfer_unmap;
1238         pctx->texture_unmap = u_transfer_helper_transfer_unmap;
1239         pctx->buffer_subdata = u_default_buffer_subdata;
1240         pctx->texture_subdata = vc4_texture_subdata;
1241         pctx->create_surface = vc4_create_surface;
1242         pctx->surface_destroy = vc4_surface_destroy;
1243         pctx->resource_copy_region = util_resource_copy_region;
1244         pctx->blit = vc4_blit;
1245         pctx->flush_resource = vc4_flush_resource;
1246 }
1247