freedreno: Refactor fd_resource_create_with_modifiers() into a helper
authorEduardo Lima Mitev <elima@igalia.com>
Tue, 18 Aug 2020 08:04:02 +0000 (10:04 +0200)
committerMarge Bot <eric+marge@anholt.net>
Tue, 18 Aug 2020 20:40:40 +0000 (20:40 +0000)
The helper just allocates and resolves layout, but does not deal with
scanout buffers nor allocation of the actual bo.

The resolved bo size is returned as an output argument.

Reviewed-by: Rob Clark <robdclark@chromium.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4565>

src/gallium/drivers/freedreno/freedreno_resource.c

index 1696cb8cc9ca75604866e6423f62eee0c5afa4fb..624714718de407dae17e79e3850bd339bac8d3f6 100644 (file)
@@ -833,12 +833,16 @@ fd_resource_layout_init(struct pipe_resource *prsc)
 }
 
 /**
 }
 
 /**
- * Create a new texture object, using the given template info.
+ * Helper that allocates a resource and resolves its layout (but doesn't
+ * allocate its bo).
+ *
+ * It returns a pipe_resource (as fd_resource_create_with_modifiers()
+ * would do), and also bo's minimum required size as an output argument.
  */
 static struct pipe_resource *
  */
 static struct pipe_resource *
-fd_resource_create_with_modifiers(struct pipe_screen *pscreen,
+fd_resource_allocate_and_resolve(struct pipe_screen *pscreen,
                const struct pipe_resource *tmpl,
                const struct pipe_resource *tmpl,
-               const uint64_t *modifiers, int count)
+               const uint64_t *modifiers, int count, uint32_t *psize)
 {
        struct fd_screen *screen = fd_screen(pscreen);
        struct fd_resource *rsc;
 {
        struct fd_screen *screen = fd_screen(pscreen);
        struct fd_resource *rsc;
@@ -846,38 +850,6 @@ fd_resource_create_with_modifiers(struct pipe_screen *pscreen,
        enum pipe_format format = tmpl->format;
        uint32_t size;
 
        enum pipe_format format = tmpl->format;
        uint32_t size;
 
-       /* when using kmsro, scanout buffers are allocated on the display device
-        * create_with_modifiers() doesn't give us usage flags, so we have to
-        * assume that all calls with modifiers are scanout-possible
-        */
-       if (screen->ro &&
-               ((tmpl->bind & PIPE_BIND_SCANOUT) ||
-                !(count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID))) {
-               struct pipe_resource scanout_templat = *tmpl;
-               struct renderonly_scanout *scanout;
-               struct winsys_handle handle;
-
-               /* note: alignment is wrong for a6xx */
-               scanout_templat.width0 = align(tmpl->width0, screen->gmem_alignw);
-
-               scanout = renderonly_scanout_for_resource(&scanout_templat,
-                                                                                                 screen->ro, &handle);
-               if (!scanout)
-                       return NULL;
-
-               renderonly_scanout_destroy(scanout, screen->ro);
-
-               assert(handle.type == WINSYS_HANDLE_TYPE_FD);
-               rsc = fd_resource(pscreen->resource_from_handle(pscreen, tmpl,
-                                                                                                               &handle,
-                                                                                                               PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
-               close(handle.handle);
-               if (!rsc)
-                       return NULL;
-
-               return &rsc->base;
-       }
-
        rsc = CALLOC_STRUCT(fd_resource);
        prsc = &rsc->base;
 
        rsc = CALLOC_STRUCT(fd_resource);
        prsc = &rsc->base;
 
@@ -964,6 +936,63 @@ fd_resource_create_with_modifiers(struct pipe_screen *pscreen,
        if (fd_mesa_debug & FD_DBG_LAYOUT)
                fdl_dump_layout(&rsc->layout);
 
        if (fd_mesa_debug & FD_DBG_LAYOUT)
                fdl_dump_layout(&rsc->layout);
 
+       /* Hand out the resolved size. */
+       if (psize)
+               *psize = size;
+
+       return prsc;
+}
+
+/**
+ * Create a new texture object, using the given template info.
+ */
+static struct pipe_resource *
+fd_resource_create_with_modifiers(struct pipe_screen *pscreen,
+               const struct pipe_resource *tmpl,
+               const uint64_t *modifiers, int count)
+{
+       struct fd_screen *screen = fd_screen(pscreen);
+       struct fd_resource *rsc;
+       struct pipe_resource *prsc;
+       uint32_t size;
+
+       /* when using kmsro, scanout buffers are allocated on the display device
+        * create_with_modifiers() doesn't give us usage flags, so we have to
+        * assume that all calls with modifiers are scanout-possible
+        */
+       if (screen->ro &&
+               ((tmpl->bind & PIPE_BIND_SCANOUT) ||
+                !(count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID))) {
+               struct pipe_resource scanout_templat = *tmpl;
+               struct renderonly_scanout *scanout;
+               struct winsys_handle handle;
+
+               /* note: alignment is wrong for a6xx */
+               scanout_templat.width0 = align(tmpl->width0, screen->gmem_alignw);
+
+               scanout = renderonly_scanout_for_resource(&scanout_templat,
+                                                                                                 screen->ro, &handle);
+               if (!scanout)
+                       return NULL;
+
+               renderonly_scanout_destroy(scanout, screen->ro);
+
+               assert(handle.type == WINSYS_HANDLE_TYPE_FD);
+               rsc = fd_resource(pscreen->resource_from_handle(pscreen, tmpl,
+                                                                                                               &handle,
+                                                                                                               PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
+               close(handle.handle);
+               if (!rsc)
+                       return NULL;
+
+               return &rsc->base;
+       }
+
+       prsc = fd_resource_allocate_and_resolve(pscreen, tmpl, modifiers, count, &size);
+       if (!prsc)
+               return NULL;
+       rsc = fd_resource(prsc);
+
        realloc_bo(rsc, size);
        if (!rsc->bo)
                goto fail;
        realloc_bo(rsc, size);
        if (!rsc->bo)
                goto fail;