Merge remote branch 'origin/master' into nv50-compiler
[mesa.git] / src / mesa / main / shared.c
index ad6e6ce7cd3cef671397e4a662166cec0d5fe1da..ea7e503cf3bad94947b07dc8386888f66de3ffc1 100644 (file)
 #include "imports.h"
 #include "mtypes.h"
 #include "hash.h"
-#include "arrayobj.h"
+#if FEATURE_ATI_fragment_shader
+#include "atifragshader.h"
+#endif
 #include "bufferobj.h"
 #include "shared.h"
-#include "shader/program.h"
-#include "shader/shader_api.h"
-#if FEATURE_dlist
+#include "program/program.h"
 #include "dlist.h"
-#endif
-#if FEATURE_ATI_fragment_shader
-#include "shader/atifragshader.h"
+#include "shaderobj.h"
+#if FEATURE_ARB_sync
+#include "syncobj.h"
 #endif
 
-
 /**
  * Allocate and initialize a shared context state structure.
  * Initializes the display list, texture objects and vertex programs hash
@@ -93,12 +92,8 @@ _mesa_alloc_shared_state(GLcontext *ctx)
    shared->BufferObjects = _mesa_NewHashTable();
 #endif
 
-   /* Allocate the default buffer object and set refcount so high that
-    * it never gets deleted.
-    * XXX with recent/improved refcounting this may not longer be needed.
-    */
+   /* Allocate the default buffer object */
    shared->NullBufferObj = ctx->Driver.NewBufferObject(ctx, 0, 0);
-   shared->NullBufferObj->RefCount = 1000 * 1000 * 1000;
 
    /* Create default texture objects */
    for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
@@ -127,6 +122,10 @@ _mesa_alloc_shared_state(GLcontext *ctx)
    shared->RenderBuffers = _mesa_NewHashTable();
 #endif
 
+#if FEATURE_ARB_sync
+   make_empty_list(& shared->SyncObjects);
+#endif
+
    return shared;
 }
 
@@ -137,11 +136,9 @@ _mesa_alloc_shared_state(GLcontext *ctx)
 static void
 delete_displaylist_cb(GLuint id, void *data, void *userData)
 {
-#if FEATURE_dlist
    struct gl_display_list *list = (struct gl_display_list *) data;
    GLcontext *ctx = (GLcontext *) userData;
    _mesa_delete_list(ctx, list);
-#endif
 }
 
 
@@ -196,11 +193,11 @@ delete_bufferobj_cb(GLuint id, void *data, void *userData)
 {
    struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
    GLcontext *ctx = (GLcontext *) userData;
-   if (bufObj->Pointer) {
+   if (_mesa_bufferobj_mapped(bufObj)) {
       ctx->Driver.UnmapBuffer(ctx, 0, bufObj);
       bufObj->Pointer = NULL;
    }
-   ctx->Driver.DeleteBuffer(ctx, bufObj);
+   _mesa_reference_buffer_object(ctx, &bufObj, NULL);
 }
 
 
@@ -230,12 +227,12 @@ delete_shader_cb(GLuint id, void *data, void *userData)
    GLcontext *ctx = (GLcontext *) userData;
    struct gl_shader *sh = (struct gl_shader *) data;
    if (sh->Type == GL_FRAGMENT_SHADER || sh->Type == GL_VERTEX_SHADER) {
-      _mesa_free_shader(ctx, sh);
+      ctx->Driver.DeleteShader(ctx, sh);
    }
    else {
       struct gl_shader_program *shProg = (struct gl_shader_program *) data;
       ASSERT(shProg->Type == GL_SHADER_PROGRAM_MESA);
-      _mesa_free_shader_program(ctx, shProg);
+      ctx->Driver.DeleteShaderProgram(ctx, shProg);
    }
 }
 
@@ -286,11 +283,15 @@ delete_renderbuffer_cb(GLuint id, void *data, void *userData)
  *
  * \sa alloc_shared_state().
  */
-void
-_mesa_free_shared_state(GLcontext *ctx, struct gl_shared_state *shared)
+static void
+free_shared_state(GLcontext *ctx, struct gl_shared_state *shared)
 {
    GLuint i;
 
+   /* Free the dummy/fallback texture object */
+   if (shared->FallbackTex)
+      ctx->Driver.DeleteTexture(ctx, shared->FallbackTex);
+
    /*
     * Free display lists
     */
@@ -333,7 +334,18 @@ _mesa_free_shared_state(GLcontext *ctx, struct gl_shared_state *shared)
 #endif
 
 #if FEATURE_ARB_vertex_buffer_object
-   ctx->Driver.DeleteBuffer(ctx, shared->NullBufferObj);
+   _mesa_reference_buffer_object(ctx, &shared->NullBufferObj, NULL);
+#endif
+
+#if FEATURE_ARB_sync
+   {
+      struct simple_node *node;
+      struct simple_node *temp;
+
+      foreach_s(node, temp, & shared->SyncObjects) {
+        _mesa_unref_sync_object(ctx, (struct gl_sync_object *) node);
+      }
+   }
 #endif
 
    /*
@@ -353,5 +365,32 @@ _mesa_free_shared_state(GLcontext *ctx, struct gl_shared_state *shared)
    _glthread_DESTROY_MUTEX(shared->Mutex);
    _glthread_DESTROY_MUTEX(shared->TexMutex);
 
-   _mesa_free(shared);
+   free(shared);
+}
+
+
+/**
+ * Decrement shared state object reference count and potentially free it
+ * and all children structures.
+ *
+ * \param ctx GL context.
+ * \param shared shared state pointer.
+ *
+ * \sa free_shared_state().
+ */
+void
+_mesa_release_shared_state(GLcontext *ctx, struct gl_shared_state *shared)
+{
+   GLint RefCount;
+
+   _glthread_LOCK_MUTEX(shared->Mutex);
+   RefCount = --shared->RefCount;
+   _glthread_UNLOCK_MUTEX(shared->Mutex);
+
+   assert(RefCount >= 0);
+
+   if (RefCount == 0) {
+      /* free shared state */
+      free_shared_state( ctx, shared );
+   }
 }