[Mesa-dev] [PATCH 02/25] mesa: add support for memory object creation/import/delete

Andres Rodriguez andresx7 at gmail.com
Fri Jul 7 04:24:34 UTC 2017


Used by EXT_external_objects and EXT_external_objects_fd

Signed-off-by: Andres Rodriguez <andresx7 at gmail.com>
---
 src/mesa/drivers/common/driverfuncs.c |   4 +
 src/mesa/main/dd.h                    |  36 +++++++++
 src/mesa/main/externalobjects.c       | 145 +++++++++++++++++++++++++++++++++-
 src/mesa/main/externalobjects.h       |  28 +++++++
 src/mesa/main/mtypes.h                |   9 +++
 src/mesa/main/shared.c                |  15 ++++
 6 files changed, 234 insertions(+), 3 deletions(-)

diff --git a/src/mesa/drivers/common/driverfuncs.c b/src/mesa/drivers/common/driverfuncs.c
index 5008ae8..ddb4bb6 100644
--- a/src/mesa/drivers/common/driverfuncs.c
+++ b/src/mesa/drivers/common/driverfuncs.c
@@ -49,6 +49,7 @@
 #include "main/syncobj.h"
 #include "main/barrier.h"
 #include "main/transformfeedback.h"
+#include "main/externalobjects.h"
 
 #include "program/program.h"
 #include "tnl/tnl.h"
@@ -166,6 +167,9 @@ _mesa_init_driver_functions(struct dd_function_table *driver)
 
    _mesa_init_sync_object_functions(driver);
 
+   /* memory objects */
+   _mesa_init_memory_object_functions(driver);
+
    driver->NewFramebuffer = _mesa_new_framebuffer;
    driver->NewRenderbuffer = _swrast_new_soft_renderbuffer;
    driver->MapRenderbuffer = _swrast_map_soft_renderbuffer;
diff --git a/src/mesa/main/dd.h b/src/mesa/main/dd.h
index 8e382e1..97ef5b8 100644
--- a/src/mesa/main/dd.h
+++ b/src/mesa/main/dd.h
@@ -1069,6 +1069,42 @@ struct dd_function_table {
    void (*MakeImageHandleResident)(struct gl_context *ctx, GLuint64 handle,
                                    GLenum access, bool resident);
    /*@}*/
+
+
+   /**
+    * \name GL_EXT_external_objects interface
+    */
+   /*@{*/
+  /**
+    * Called to allocate a new memory object.  Drivers will usually
+    * allocate/return a subclass of gl_memory_object.
+    */
+   struct gl_memory_object * (*NewMemoryObject)(struct gl_context *ctx,
+                                                GLuint name);
+   /**
+    * Called to delete/free a memory object.  Drivers should free the
+    * object and any image data it contains.
+    */
+   void (*DeleteMemoryObject)(struct gl_context *ctx,
+                              struct gl_memory_object *mem_obj);
+   /*@}*/
+
+   /**
+    * \name GL_EXT_external_objects_fd interface
+    */
+   /*@{*/
+   /**
+    * Called to import a memory object. The caller relinquishes ownership
+    * of fd after the call returns.
+    *
+    * Accessing fd after ImportMemoryObjectFd returns results in undefined
+    * behaviour. This is consistent with EXT_external_object_fd.
+    */
+   void (*ImportMemoryObjectFd)(struct gl_context *ctx,
+                                struct gl_memory_object *mem_obj,
+                                GLuint64 size,
+                                int fd);
+   /*@}*/
 };
 
 
diff --git a/src/mesa/main/externalobjects.c b/src/mesa/main/externalobjects.c
index d6c5109..2ae3f0b 100644
--- a/src/mesa/main/externalobjects.c
+++ b/src/mesa/main/externalobjects.c
@@ -21,24 +21,145 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
+#include "macros.h"
+#include "mtypes.h"
 #include "externalobjects.h"
 
+/**
+ * Allocate and initialize a new memory object.  But don't put it into the
+ * memory object hash table.
+ *
+ * Called via ctx->Driver.NewMemoryObject, unless overridden by a device
+ * driver.
+ *
+ * \return pointer to new memory object.
+ */
+static struct gl_memory_object *
+_mesa_new_memory_object(struct gl_context *ctx, GLuint name)
+{
+   struct gl_memory_object *obj;
+   (void) ctx;
+   obj = MALLOC_STRUCT(gl_memory_object);
+
+   _mesa_initialize_memory_object(ctx, obj, name);
+   return obj;
+}
+
+/**
+ * Delete a memory object.  Called via ctx->Driver.DeleteMemory().
+ * Not removed from hash table here.
+ */
+void
+_mesa_delete_memory_object(struct gl_context *ctx, struct gl_memory_object *mo)
+{
+   free(mo);
+}
+
+void
+_mesa_init_memory_object_functions(struct dd_function_table *driver)
+{
+   driver->NewMemoryObject = _mesa_new_memory_object;
+   driver->DeleteMemoryObject = _mesa_delete_memory_object;
+}
+
+/**
+ * Initialize a buffer object to default values.
+ */
+void
+_mesa_initialize_memory_object(struct gl_context *ctx,
+                               struct gl_memory_object *obj,
+                               GLuint name)
+{
+   memset(obj, 0, sizeof(struct gl_memory_object));
+   obj->Name = name;
+}
+
 void GLAPIENTRY
 _mesa_DeleteMemoryObjectsEXT(GLsizei n, const GLuint *memoryObjects)
 {
-
+   GET_CURRENT_CONTEXT(ctx);
+   GLint i;
+
+   if (MESA_VERBOSE & (VERBOSE_API))
+      _mesa_debug(ctx, "glDeleteMemoryObjectsEXT(%d, %p)\n", n, memoryObjects);
+
+   if (n < 0) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteMemoryObjects(n < 0)");
+      return;
+   }
+
+   if (!memoryObjects)
+      return;
+
+   _mesa_HashLockMutex(ctx->Shared->MemoryObjects);
+   for (i = 0; i < n; i++) {
+      if (memoryObjects[i] > 0) {
+         struct gl_memory_object *delObj
+            = _mesa_lookup_memory_object_locked(ctx, memoryObjects[i]);
+
+         if (delObj) {
+            _mesa_HashRemoveLocked(ctx->Shared->MemoryObjects, memoryObjects[i]);
+            ctx->Driver.DeleteMemoryObject(ctx, delObj);
+         }
+      }
+   }
+   _mesa_HashUnlockMutex(ctx->Shared->MemoryObjects);
 }
 
 GLboolean GLAPIENTRY
 _mesa_IsMemoryObjectEXT(GLuint memoryObject)
 {
-   return GL_FALSE;
+   GET_CURRENT_CONTEXT(ctx);
+   struct gl_memory_object *obj;
+
+   if (memoryObject == 0)
+      return GL_FALSE;
+
+   obj = _mesa_lookup_memory_object(ctx, memoryObject);
+
+   return obj ? GL_TRUE : GL_FALSE;
 }
 
 void GLAPIENTRY
 _mesa_CreateMemoryObjectsEXT(GLsizei n, GLuint *memoryObjects)
 {
-
+   GET_CURRENT_CONTEXT(ctx);
+   GLuint first;
+
+   if (MESA_VERBOSE & (VERBOSE_API))
+      _mesa_debug(ctx, "glCreateMemoryObjectsEXT(%d, %p)", n, memoryObjects);
+
+   if (n < 0) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glCreateMemoryObjects(n < 0)");
+      return;
+   }
+
+   if (!memoryObjects)
+      return;
+
+   _mesa_HashLockMutex(ctx->Shared->MemoryObjects);
+   first = _mesa_HashFindFreeKeyBlock(ctx->Shared->MemoryObjects, n);
+   if (first) {
+      GLsizei i;
+      for (i = 0; i < n; i++) {
+         struct gl_memory_object *memObj;
+
+         memoryObjects[i] = first + i;
+
+         /* allocate memory object */
+         memObj = ctx->Driver.NewMemoryObject(ctx, memoryObjects[i]);
+         if (!memObj)
+            goto out_unlock;
+
+         /* insert into hash table */
+         _mesa_HashInsertLocked(ctx->Shared->MemoryObjects,
+                                memoryObjects[i],
+                                memObj);
+      }
+   }
+
+out_unlock:
+   _mesa_HashUnlockMutex(ctx->Shared->MemoryObjects);
 }
 
 void GLAPIENTRY
@@ -263,7 +384,25 @@ _mesa_ImportMemoryFdEXT(GLuint memory,
                         GLenum handleType,
                         GLint fd)
 {
+   GET_CURRENT_CONTEXT(ctx);
+   struct gl_memory_object *memObj;
+
+   if (handleType != GL_HANDLE_TYPE_OPAQUE_FD_EXT) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glImportMemoryFdEXT(handleType=%u)", handleType);
+      return;
+   }
+
+   if (memory == 0)
+      return;
+
+   memObj = _mesa_lookup_memory_object(ctx, memory);
+   if (!memObj) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glImportMemoryFdEXT(non-gen name)");
+      return;
+   }
 
+   ctx->Driver.ImportMemoryObjectFd(ctx, memObj, size, fd);
 }
 
 void GLAPIENTRY
diff --git a/src/mesa/main/externalobjects.h b/src/mesa/main/externalobjects.h
index 65548f0..7c33bc1 100644
--- a/src/mesa/main/externalobjects.h
+++ b/src/mesa/main/externalobjects.h
@@ -35,6 +35,34 @@
 #define EXTERNALOBJECTS_H
 
 #include "glheader.h"
+#include "hash.h"
+
+static inline struct gl_memory_object *
+_mesa_lookup_memory_object(struct gl_context *ctx, GLuint id)
+{
+   return (struct gl_memory_object *)
+      _mesa_HashLookup(ctx->Shared->MemoryObjects, id);
+}
+
+static inline struct gl_memory_object *
+_mesa_lookup_memory_object_locked(struct gl_context *ctx, GLuint memory)
+{
+   if (!memory)
+      return NULL;
+
+   return (struct gl_memory_object *)
+      _mesa_HashLookupLocked(ctx->Shared->MemoryObjects, memory);
+}
+
+extern void
+_mesa_init_memory_object_functions(struct dd_function_table *driver);
+
+extern void
+_mesa_initialize_memory_object(struct gl_context *ctx,
+                               struct gl_memory_object *obj,
+                               GLuint name);
+extern void
+_mesa_delete_memory_object(struct gl_context *ctx, struct gl_memory_object *mo);
 
 extern void GLAPIENTRY
 _mesa_DeleteMemoryObjectsEXT(GLsizei n, const GLuint *memoryObjects);
diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h
index 0cb0024..a720b07 100644
--- a/src/mesa/main/mtypes.h
+++ b/src/mesa/main/mtypes.h
@@ -3284,6 +3284,10 @@ struct gl_shared_state
     * Once this field becomes true, it is never reset to false.
     */
    bool ShareGroupReset;
+
+   /** EXT_external_objects */
+   struct _mesa_HashTable *MemoryObjects;
+
 };
 
 
@@ -4650,6 +4654,11 @@ struct gl_image_handle_object
    GLuint64 handle;
 };
 
+struct gl_memory_object
+{
+   GLuint Name;          /**< hash table ID/name */
+};
+
 /**
  * Mesa rendering context.
  *
diff --git a/src/mesa/main/shared.c b/src/mesa/main/shared.c
index 6926d40..ddd5db8 100644
--- a/src/mesa/main/shared.c
+++ b/src/mesa/main/shared.c
@@ -130,6 +130,7 @@ _mesa_alloc_shared_state(struct gl_context *ctx)
    shared->SyncObjects = _mesa_set_create(NULL, _mesa_hash_pointer,
                                           _mesa_key_pointer_equal);
 
+   shared->MemoryObjects = _mesa_NewHashTable();
    return shared;
 }
 
@@ -295,6 +296,17 @@ delete_sampler_object_cb(GLuint id, void *data, void *userData)
    _mesa_reference_sampler_object(ctx, &sampObj, NULL);
 }
 
+/**
+ * Callback for deleting a memory object.  Called by _mesa_HashDeleteAll().
+ */
+static void
+delete_memory_object_cb(GLuint id, void *data, void *userData)
+{
+   struct gl_memory_object *memObj = (struct gl_memory_object *) data;
+   struct gl_context *ctx = (struct gl_context *) userData;
+   ctx->Driver.DeleteMemoryObject(ctx, memObj);
+}
+
 
 /**
  * Deallocate a shared state object and all children structures.
@@ -379,6 +391,9 @@ free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
 
    _mesa_free_shared_handles(shared);
 
+   _mesa_HashDeleteAll(shared->MemoryObjects, delete_memory_object_cb, ctx);
+   _mesa_DeleteHashTable(shared->MemoryObjects);
+
    mtx_destroy(&shared->Mutex);
    mtx_destroy(&shared->TexMutex);
 
-- 
2.9.3



More information about the mesa-dev mailing list