[Mesa-dev] [PATCH 04/24] gallium: introduce memory object

Timothy Arceri tarceri at itsqueeze.com
Thu Jul 27 13:08:29 UTC 2017


From: Dave Airlie <airlied at redhat.com>

v2: fix comment regarding fd ownership, define pipe_memory_object
v3: remove stray return
v4 (Timothy Arceri): update trace

Reviewed-by: Marek Olšák <marek.olsak at amd.com> (v3)
---
 src/gallium/drivers/ddebug/dd_screen.c | 40 +++++++++++++++++++++++++++++++
 src/gallium/drivers/trace/tr_screen.c  | 43 ++++++++++++++++++++++++++++++++++
 src/gallium/include/pipe/p_screen.h    | 36 ++++++++++++++++++++++++++++
 src/gallium/include/pipe/p_state.h     |  8 +++++++
 4 files changed, 127 insertions(+)

diff --git a/src/gallium/drivers/ddebug/dd_screen.c b/src/gallium/drivers/ddebug/dd_screen.c
index 14e6f6b011..51382da1d7 100644
--- a/src/gallium/drivers/ddebug/dd_screen.c
+++ b/src/gallium/drivers/ddebug/dd_screen.c
@@ -238,20 +238,36 @@ dd_screen_resource_from_user_memory(struct pipe_screen *_screen,
    struct pipe_screen *screen = dd_screen(_screen)->screen;
    struct pipe_resource *res =
       screen->resource_from_user_memory(screen, templ, user_memory);
 
    if (!res)
       return NULL;
    res->screen = _screen;
    return res;
 }
 
+static struct pipe_resource *
+dd_screen_resource_from_memobj(struct pipe_screen *_screen,
+                               const struct pipe_resource *templ,
+                               struct pipe_memory_object *memobj,
+                               uint64_t offset)
+{
+   struct pipe_screen *screen = dd_screen(_screen)->screen;
+   struct pipe_resource *res =
+      screen->resource_from_memobj(screen, templ, memobj, offset);
+
+   if (!res)
+      return NULL;
+   res->screen = _screen;
+   return res;
+}
+
 static void
 dd_screen_resource_changed(struct pipe_screen *_screen,
                            struct pipe_resource *res)
 {
    struct pipe_screen *screen = dd_screen(_screen)->screen;
 
    screen->resource_changed(screen, res);
 }
 
 static void
@@ -296,21 +312,42 @@ dd_screen_fence_finish(struct pipe_screen *_screen,
                        struct pipe_context *_ctx,
                        struct pipe_fence_handle *fence,
                        uint64_t timeout)
 {
    struct pipe_screen *screen = dd_screen(_screen)->screen;
    struct pipe_context *ctx = _ctx ? dd_context(_ctx)->pipe : NULL;
 
    return screen->fence_finish(screen, ctx, fence, timeout);
 }
 
+/********************************************************************
+ * memobj
+ */
+
+static struct pipe_memory_object *
+dd_screen_memobj_create_from_handle(struct pipe_screen *_screen,
+                                    struct winsys_handle *handle,
+                                    bool dedicated)
+{
+   struct pipe_screen *screen = dd_screen(_screen)->screen;
+
+   return screen->memobj_create_from_handle(screen, handle, dedicated);
+}
+
+static void
+dd_screen_memobj_destroy(struct pipe_screen *_screen,
+                         struct pipe_memory_object *memobj)
+{
+   struct pipe_screen *screen = dd_screen(_screen)->screen;
 
+   screen->memobj_destroy(screen, memobj);
+}
 /********************************************************************
  * screen
  */
 
 static void
 dd_screen_destroy(struct pipe_screen *_screen)
 {
    struct dd_screen *dscreen = dd_screen(_screen);
    struct pipe_screen *screen = dscreen->screen;
 
@@ -405,27 +442,30 @@ ddebug_screen_create(struct pipe_screen *screen)
    dscreen->base.query_memory_info = dd_screen_query_memory_info;
    /* get_video_param */
    /* get_compute_param */
    SCR_INIT(get_timestamp);
    dscreen->base.context_create = dd_screen_context_create;
    dscreen->base.is_format_supported = dd_screen_is_format_supported;
    /* is_video_format_supported */
    SCR_INIT(can_create_resource);
    dscreen->base.resource_create = dd_screen_resource_create;
    dscreen->base.resource_from_handle = dd_screen_resource_from_handle;
+   SCR_INIT(resource_from_memobj);
    SCR_INIT(resource_from_user_memory);
    dscreen->base.resource_get_handle = dd_screen_resource_get_handle;
    SCR_INIT(resource_changed);
    dscreen->base.resource_destroy = dd_screen_resource_destroy;
    SCR_INIT(flush_frontbuffer);
    SCR_INIT(fence_reference);
    SCR_INIT(fence_finish);
+   SCR_INIT(memobj_create_from_handle);
+   SCR_INIT(memobj_destroy);
    SCR_INIT(get_driver_query_info);
    SCR_INIT(get_driver_query_group_info);
    SCR_INIT(get_compiler_options);
 
 #undef SCR_INIT
 
    dscreen->screen = screen;
    dscreen->timeout_ms = timeout;
    dscreen->mode = mode;
    dscreen->no_flush = no_flush;
diff --git a/src/gallium/drivers/trace/tr_screen.c b/src/gallium/drivers/trace/tr_screen.c
index e56434c5bd..572d455105 100644
--- a/src/gallium/drivers/trace/tr_screen.c
+++ b/src/gallium/drivers/trace/tr_screen.c
@@ -360,20 +360,36 @@ trace_screen_resource_get_handle(struct pipe_screen *_screen,
    struct trace_screen *tr_screen = trace_screen(_screen);
    struct trace_context *tr_pipe = _pipe ? trace_context(_pipe) : NULL;
    struct pipe_screen *screen = tr_screen->screen;
 
    /* TODO trace call */
 
    return screen->resource_get_handle(screen, tr_pipe ? tr_pipe->pipe : NULL,
                                       resource, handle, usage);
 }
 
+static struct pipe_resource *
+trace_screen_resource_from_memobj(struct pipe_screen *_screen,
+                                  const struct pipe_resource *templ,
+                                  struct pipe_memory_object *memobj,
+                                  uint64_t offset)
+{
+   struct pipe_screen *screen = trace_screen(_screen)->screen;
+   struct pipe_resource *res =
+      screen->resource_from_memobj(screen, templ, memobj, offset);
+
+   if (!res)
+      return NULL;
+   res->screen = _screen;
+   return res;
+}
+
 static void
 trace_screen_resource_changed(struct pipe_screen *_screen,
                               struct pipe_resource *resource)
 {
    struct trace_screen *tr_scr = trace_screen(_screen);
    struct pipe_screen *screen = tr_scr->screen;
 
    trace_dump_call_begin("pipe_screen", "resource_changed");
 
    trace_dump_arg(ptr, screen);
@@ -450,20 +466,44 @@ trace_screen_fence_finish(struct pipe_screen *_screen,
 
    trace_dump_ret(bool, result);
 
    trace_dump_call_end();
 
    return result;
 }
 
 
 /********************************************************************
+ * memobj
+ */
+
+static struct pipe_memory_object *
+trace_screen_memobj_create_from_handle(struct pipe_screen *_screen,
+                                       struct winsys_handle *handle,
+                                       bool dedicated)
+{
+   struct pipe_screen *screen = trace_screen(_screen)->screen;
+
+   return screen->memobj_create_from_handle(screen, handle, dedicated);
+}
+
+static void
+trace_screen_memobj_destroy(struct pipe_screen *_screen,
+                            struct pipe_memory_object *memobj)
+{
+   struct pipe_screen *screen = trace_screen(_screen)->screen;
+
+   screen->memobj_destroy(screen, memobj);
+}
+
+
+/********************************************************************
  * screen
  */
 
 static uint64_t
 trace_screen_get_timestamp(struct pipe_screen *_screen)
 {
    struct trace_screen *tr_scr = trace_screen(_screen);
    struct pipe_screen *screen = tr_scr->screen;
    uint64_t result;
 
@@ -535,24 +575,27 @@ trace_screen_create(struct pipe_screen *screen)
    tr_scr->base.get_param = trace_screen_get_param;
    tr_scr->base.get_shader_param = trace_screen_get_shader_param;
    tr_scr->base.get_paramf = trace_screen_get_paramf;
    tr_scr->base.get_compute_param = trace_screen_get_compute_param;
    tr_scr->base.is_format_supported = trace_screen_is_format_supported;
    assert(screen->context_create);
    tr_scr->base.context_create = trace_screen_context_create;
    tr_scr->base.resource_create = trace_screen_resource_create;
    tr_scr->base.resource_from_handle = trace_screen_resource_from_handle;
    tr_scr->base.resource_get_handle = trace_screen_resource_get_handle;
+   SCR_INIT(resource_from_memobj);
    SCR_INIT(resource_changed);
    tr_scr->base.resource_destroy = trace_screen_resource_destroy;
    tr_scr->base.fence_reference = trace_screen_fence_reference;
    tr_scr->base.fence_finish = trace_screen_fence_finish;
+   SCR_INIT(memobj_create_from_handle);
+   SCR_INIT(memobj_destroy);
    tr_scr->base.flush_frontbuffer = trace_screen_flush_frontbuffer;
    tr_scr->base.get_timestamp = trace_screen_get_timestamp;
 
    tr_scr->screen = screen;
 
    trace_dump_ret(ptr, screen);
    trace_dump_call_end();
 
    return &tr_scr->base;
 
diff --git a/src/gallium/include/pipe/p_screen.h b/src/gallium/include/pipe/p_screen.h
index 65e954aa87..d81625c91f 100644
--- a/src/gallium/include/pipe/p_screen.h
+++ b/src/gallium/include/pipe/p_screen.h
@@ -350,18 +350,54 @@ struct pipe_screen {
     * format is returned in \p count, with no modification to \p modifiers.
     * Otherwise, \p modifiers is filled with upto \p max supported modifier
     * codes, and \p count with the number of modifiers copied.
     * The \p external_only array is used to return whether the format and
     * modifier combination can only be used with an external texture target.
     */
    void (*query_dmabuf_modifiers)(struct pipe_screen *screen,
                                   enum pipe_format format, int max,
                                   uint64_t *modifiers,
                                   unsigned int *external_only, int *count);
+
+   /**
+    * Create a memory object from a winsys handle
+    *
+    * The underlying memory is most often allocated in by a foregin API.
+    * Then the underlying memory object is then exported through interfaces
+    * compatible with EXT_external_resources.
+    *
+    * Note: For DRM_API_HANDLE_TYPE_FD handles, the caller retains ownership
+    * of the fd.
+    *
+    * \param handle  A handle representing the memory object to import
+    */
+   struct pipe_memory_object *(*memobj_create_from_handle)(struct pipe_screen *screen,
+                                                           struct winsys_handle *handle,
+                                                           bool dedicated);
+
+   /**
+    * Destroy a memory object
+    *
+    * \param memobj  The memory object to destroy
+    */
+   void (*memobj_destroy)(struct pipe_screen *screen,
+                          struct pipe_memory_object *memobj);
+
+   /**
+    * Create a texture from a memory object
+    *
+    * \param t       texture template
+    * \param memobj  The memory object used to back the texture
+    */
+   struct pipe_resource * (*resource_from_memobj)(struct pipe_screen *screen,
+                                                  const struct pipe_resource *t,
+                                                  struct pipe_memory_object *memobj,
+                                                  uint64_t offset);
+
 };
 
 
 #ifdef __cplusplus
 }
 #endif
 
 #endif /* P_SCREEN_H */
diff --git a/src/gallium/include/pipe/p_state.h b/src/gallium/include/pipe/p_state.h
index 15be8cb5d0..86c7751eb1 100644
--- a/src/gallium/include/pipe/p_state.h
+++ b/src/gallium/include/pipe/p_state.h
@@ -881,15 +881,23 @@ struct pipe_device_reset_callback
 struct pipe_memory_info
 {
    unsigned total_device_memory; /**< size of device memory, e.g. VRAM */
    unsigned avail_device_memory; /**< free device memory at the moment */
    unsigned total_staging_memory; /**< size of staging memory, e.g. GART */
    unsigned avail_staging_memory; /**< free staging memory at the moment */
    unsigned device_memory_evicted; /**< size of memory evicted (monotonic counter) */
    unsigned nr_device_memory_evictions; /**< # of evictions (monotonic counter) */
 };
 
+/**
+ * Structure that contains information about external memory
+ */
+struct pipe_memory_object
+{
+   bool dedicated;
+};
+
 #ifdef __cplusplus
 }
 #endif
 
 #endif
-- 
2.13.3



More information about the mesa-dev mailing list