added Window-isms previously in gl.h
[mesa.git] / src / mesa / main / texobj.c
index a7e6e34b8b478c9dbcb4e4a7a71a51172e7b022f..330943b5794494b24869eb5410792880f05bb5f0 100644 (file)
@@ -1,4 +1,3 @@
-/* $Id: texobj.c,v 1.11 2000/01/24 16:19:55 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
@@ -29,6 +28,7 @@
 #include "all.h"
 #else
 #include "glheader.h"
+#include "colortab.h"
 #include "context.h"
 #include "enums.h"
 #include "hash.h"
@@ -74,18 +74,14 @@ gl_alloc_texture_object( struct gl_shared_state *shared, GLuint name,
       obj->BaseLevel = 0;
       obj->MaxLevel = 1000;
       obj->MinMagThresh = 0.0F;
-      obj->Palette.Table[0] = 255;
-      obj->Palette.Table[1] = 255;
-      obj->Palette.Table[2] = 255;
-      obj->Palette.Table[3] = 255;
-      obj->Palette.Size = 1;
-      obj->Palette.IntFormat = GL_RGBA;
-      obj->Palette.Format = GL_RGBA;
+      _mesa_init_colortable(&obj->Palette);
 
       /* insert into linked list */
       if (shared) {
+         _glthread_LOCK_MUTEX(shared->Mutex);
          obj->Next = shared->TexObjectList;
          shared->TexObjectList = obj;
+         _glthread_UNLOCK_MUTEX(shared->Mutex);
       }
 
       if (name > 0) {
@@ -118,6 +114,7 @@ void gl_free_texture_object( struct gl_shared_state *shared,
 
    /* unlink t from the linked list */
    if (shared) {
+      _glthread_LOCK_MUTEX(shared->Mutex);
       tprev = NULL;
       tcurr = shared->TexObjectList;
       while (tcurr) {
@@ -133,6 +130,7 @@ void gl_free_texture_object( struct gl_shared_state *shared,
          tprev = tcurr;
          tcurr = tcurr->Next;
       }
+      _glthread_UNLOCK_MUTEX(shared->Mutex);
    }
 
    if (t->Name) {
@@ -140,12 +138,14 @@ void gl_free_texture_object( struct gl_shared_state *shared,
       _mesa_HashRemove(shared->TexObjects, t->Name);
    }
 
-   /* free texture image */
+   _mesa_free_colortable_data(&t->Palette);
+
+   /* free texture images */
    {
       GLuint i;
       for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
          if (t->Image[i]) {
-            gl_free_texture_image( t->Image[i] );
+            _mesa_free_texture_image( t->Image[i] );
          }
       }
    }
@@ -164,7 +164,7 @@ void gl_test_texture_object_completeness( const GLcontext *ctx, struct gl_textur
    t->Complete = GL_TRUE;  /* be optimistic */
 
    /* Always need level zero image */
-   if (!t->Image[0] || !t->Image[0]->Data) {
+   if (!t->Image[0]) {
       t->Complete = GL_FALSE;
       return;
    }
@@ -203,10 +203,6 @@ void gl_test_texture_object_completeness( const GLcontext *ctx, struct gl_textur
       /* Test dimension-independent attributes */
       for (i = minLevel; i <= maxLevel; i++) {
          if (t->Image[i]) {
-            if (!t->Image[i]->Data) {
-               t->Complete = GL_FALSE;
-               return;
-            }
             if (t->Image[i]->Format != t->Image[0]->Format) {
                t->Complete = GL_FALSE;
                return;
@@ -231,10 +227,6 @@ void gl_test_texture_object_completeness( const GLcontext *ctx, struct gl_textur
                   t->Complete = GL_FALSE;
                   return;
                }
-               if (!t->Image[i]->Data) {
-                  t->Complete = GL_FALSE;
-                  return;
-               }
                if (t->Image[i]->Width2 != width ) {
                   t->Complete = GL_FALSE;
                   return;
@@ -321,6 +313,8 @@ void gl_test_texture_object_completeness( const GLcontext *ctx, struct gl_textur
 }
 
 
+_glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
+
 
 /*
  * Execute glGenTextures
@@ -338,6 +332,12 @@ _mesa_GenTextures( GLsizei n, GLuint *texName )
       return;
    }
 
+
+   /*
+    * This must be atomic (generation and allocation of texture IDs)
+    */
+   _glthread_LOCK_MUTEX(GenTexturesLock);
+
    first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
 
    /* Return the texture names */
@@ -351,6 +351,8 @@ _mesa_GenTextures( GLsizei n, GLuint *texName )
       GLuint dims = 0;
       (void) gl_alloc_texture_object(ctx->Shared, name, dims);
    }
+
+   _glthread_UNLOCK_MUTEX(GenTexturesLock);
 }
 
 
@@ -372,26 +374,29 @@ _mesa_DeleteTextures( GLsizei n, const GLuint *texName)
          t = (struct gl_texture_object *)
             _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
          if (t) {
+            /* First check if this texture is currently bound.
+             * If so, unbind it and decrement the reference count.
+             */
             GLuint u;
-            for (u=0; u<MAX_TEXTURE_UNITS; u++) {
+            for (u = 0; u < MAX_TEXTURE_UNITS; u++) {
                struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
               GLuint d;
               for (d = 1 ; d <= 3 ; d++) {
-                 if (unit->CurrentD[d]==t) {
+                 if (unit->CurrentD[d] == t) {
                     unit->CurrentD[d] = ctx->Shared->DefaultD[d];
                     ctx->Shared->DefaultD[d]->RefCount++;
                     t->RefCount--;
-                    assert( t->RefCount >= 0 );
+                    ASSERT( t->RefCount >= 0 );
                  }
               }
             }
 
-            /* tell device driver to delete texture */
-            if (ctx->Driver.DeleteTexture) {
-               (*ctx->Driver.DeleteTexture)( ctx, t );
-            }
-
-            if (t->RefCount==0) {
+            /* Decrement reference count and delete if zero */
+            t->RefCount--;
+            ASSERT( t->RefCount >= 0 );
+            if (t->RefCount == 0) {
+               if (ctx->Driver.DeleteTexture)
+                  (*ctx->Driver.DeleteTexture)( ctx, t );
                gl_free_texture_object(ctx->Shared, t);
             }
          }
@@ -444,7 +449,7 @@ _mesa_BindTexture( GLenum target, GLuint texName )
       newTexObj = ctx->Shared->DefaultD[dim];
    else {
       struct _mesa_HashTable *hash = ctx->Shared->TexObjects;
-      newTexObj = (struct gl_texture_object *) HashLookup(hash, texName);
+      newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName);
 
       if (!newTexObj)
         newTexObj = gl_alloc_texture_object(ctx->Shared, texName, dim);