Home | History | Annotate | Download | only in main
      1 /*
      2  * Mesa 3-D graphics library
      3  *
      4  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included
     14  * in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 
     25 /**
     26  * \file shared.c
     27  * Shared-context state
     28  */
     29 
     30 #include "imports.h"
     31 #include "mtypes.h"
     32 #include "hash.h"
     33 #include "atifragshader.h"
     34 #include "bufferobj.h"
     35 #include "shared.h"
     36 #include "program/program.h"
     37 #include "dlist.h"
     38 #include "samplerobj.h"
     39 #include "shaderapi.h"
     40 #include "shaderobj.h"
     41 #include "syncobj.h"
     42 
     43 #include "util/hash_table.h"
     44 #include "util/set.h"
     45 
     46 /**
     47  * Allocate and initialize a shared context state structure.
     48  * Initializes the display list, texture objects and vertex programs hash
     49  * tables, allocates the texture objects. If it runs out of memory, frees
     50  * everything already allocated before returning NULL.
     51  *
     52  * \return pointer to a gl_shared_state structure on success, or NULL on
     53  * failure.
     54  */
     55 struct gl_shared_state *
     56 _mesa_alloc_shared_state(struct gl_context *ctx)
     57 {
     58    struct gl_shared_state *shared;
     59    GLuint i;
     60 
     61    shared = CALLOC_STRUCT(gl_shared_state);
     62    if (!shared)
     63       return NULL;
     64 
     65    mtx_init(&shared->Mutex, mtx_plain);
     66 
     67    shared->DisplayList = _mesa_NewHashTable();
     68    shared->BitmapAtlas = _mesa_NewHashTable();
     69    shared->TexObjects = _mesa_NewHashTable();
     70    shared->Programs = _mesa_NewHashTable();
     71 
     72    shared->DefaultVertexProgram =
     73       ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0, true);
     74    shared->DefaultFragmentProgram =
     75       ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0, true);
     76 
     77    shared->ATIShaders = _mesa_NewHashTable();
     78    shared->DefaultFragmentShader = _mesa_new_ati_fragment_shader(ctx, 0);
     79 
     80    shared->ShaderObjects = _mesa_NewHashTable();
     81 
     82    shared->BufferObjects = _mesa_NewHashTable();
     83 
     84    /* GL_ARB_sampler_objects */
     85    shared->SamplerObjects = _mesa_NewHashTable();
     86 
     87    /* Allocate the default buffer object */
     88    shared->NullBufferObj = ctx->Driver.NewBufferObject(ctx, 0);
     89 
     90    /* Create default texture objects */
     91    for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
     92       /* NOTE: the order of these enums matches the TEXTURE_x_INDEX values */
     93       static const GLenum targets[] = {
     94          GL_TEXTURE_2D_MULTISAMPLE,
     95          GL_TEXTURE_2D_MULTISAMPLE_ARRAY,
     96          GL_TEXTURE_CUBE_MAP_ARRAY,
     97          GL_TEXTURE_BUFFER,
     98          GL_TEXTURE_2D_ARRAY_EXT,
     99          GL_TEXTURE_1D_ARRAY_EXT,
    100          GL_TEXTURE_EXTERNAL_OES,
    101          GL_TEXTURE_CUBE_MAP,
    102          GL_TEXTURE_3D,
    103          GL_TEXTURE_RECTANGLE_NV,
    104          GL_TEXTURE_2D,
    105          GL_TEXTURE_1D
    106       };
    107       STATIC_ASSERT(ARRAY_SIZE(targets) == NUM_TEXTURE_TARGETS);
    108       shared->DefaultTex[i] = ctx->Driver.NewTextureObject(ctx, 0, targets[i]);
    109       /* Need to explicitly set/overwrite the TargetIndex field here since
    110        * the call to _mesa_tex_target_to_index() in NewTextureObject() may
    111        * fail if the texture target is not supported.
    112        */
    113       shared->DefaultTex[i]->TargetIndex = i;
    114    }
    115 
    116    /* sanity check */
    117    assert(shared->DefaultTex[TEXTURE_1D_INDEX]->RefCount == 1);
    118 
    119    /* Mutex and timestamp for texobj state validation */
    120    mtx_init(&shared->TexMutex, mtx_recursive);
    121    shared->TextureStateStamp = 0;
    122 
    123    shared->FrameBuffers = _mesa_NewHashTable();
    124    shared->RenderBuffers = _mesa_NewHashTable();
    125 
    126    shared->SyncObjects = _mesa_set_create(NULL, _mesa_hash_pointer,
    127                                           _mesa_key_pointer_equal);
    128 
    129    return shared;
    130 }
    131 
    132 
    133 /**
    134  * Callback for deleting a display list.  Called by _mesa_HashDeleteAll().
    135  */
    136 static void
    137 delete_displaylist_cb(GLuint id, void *data, void *userData)
    138 {
    139    struct gl_display_list *list = (struct gl_display_list *) data;
    140    struct gl_context *ctx = (struct gl_context *) userData;
    141    _mesa_delete_list(ctx, list);
    142 }
    143 
    144 
    145 /**
    146  * Callback for deleting a bitmap atlas.  Called by _mesa_HashDeleteAll().
    147  */
    148 static void
    149 delete_bitmap_atlas_cb(GLuint id, void *data, void *userData)
    150 {
    151    struct gl_bitmap_atlas *atlas = (struct gl_bitmap_atlas *) data;
    152    struct gl_context *ctx = (struct gl_context *) userData;
    153    _mesa_delete_bitmap_atlas(ctx, atlas);
    154 }
    155 
    156 
    157 /**
    158  * Callback for deleting a texture object.  Called by _mesa_HashDeleteAll().
    159  */
    160 static void
    161 delete_texture_cb(GLuint id, void *data, void *userData)
    162 {
    163    struct gl_texture_object *texObj = (struct gl_texture_object *) data;
    164    struct gl_context *ctx = (struct gl_context *) userData;
    165    ctx->Driver.DeleteTexture(ctx, texObj);
    166 }
    167 
    168 
    169 /**
    170  * Callback for deleting a program object.  Called by _mesa_HashDeleteAll().
    171  */
    172 static void
    173 delete_program_cb(GLuint id, void *data, void *userData)
    174 {
    175    struct gl_program *prog = (struct gl_program *) data;
    176    struct gl_context *ctx = (struct gl_context *) userData;
    177    if(prog != &_mesa_DummyProgram) {
    178       assert(prog->RefCount == 1); /* should only be referenced by hash table */
    179       prog->RefCount = 0;  /* now going away */
    180       ctx->Driver.DeleteProgram(ctx, prog);
    181    }
    182 }
    183 
    184 
    185 /**
    186  * Callback for deleting an ATI fragment shader object.
    187  * Called by _mesa_HashDeleteAll().
    188  */
    189 static void
    190 delete_fragshader_cb(GLuint id, void *data, void *userData)
    191 {
    192    struct ati_fragment_shader *shader = (struct ati_fragment_shader *) data;
    193    struct gl_context *ctx = (struct gl_context *) userData;
    194    _mesa_delete_ati_fragment_shader(ctx, shader);
    195 }
    196 
    197 
    198 /**
    199  * Callback for deleting a buffer object.  Called by _mesa_HashDeleteAll().
    200  */
    201 static void
    202 delete_bufferobj_cb(GLuint id, void *data, void *userData)
    203 {
    204    struct gl_buffer_object *bufObj = (struct gl_buffer_object *) data;
    205    struct gl_context *ctx = (struct gl_context *) userData;
    206 
    207    _mesa_buffer_unmap_all_mappings(ctx, bufObj);
    208    _mesa_reference_buffer_object(ctx, &bufObj, NULL);
    209 }
    210 
    211 
    212 /**
    213  * Callback for freeing shader program data. Call it before delete_shader_cb
    214  * to avoid memory access error.
    215  */
    216 static void
    217 free_shader_program_data_cb(GLuint id, void *data, void *userData)
    218 {
    219    struct gl_context *ctx = (struct gl_context *) userData;
    220    struct gl_shader_program *shProg = (struct gl_shader_program *) data;
    221 
    222    if (shProg->Type == GL_SHADER_PROGRAM_MESA) {
    223        _mesa_free_shader_program_data(ctx, shProg);
    224    }
    225 }
    226 
    227 
    228 /**
    229  * Callback for deleting shader and shader programs objects.
    230  * Called by _mesa_HashDeleteAll().
    231  */
    232 static void
    233 delete_shader_cb(GLuint id, void *data, void *userData)
    234 {
    235    struct gl_context *ctx = (struct gl_context *) userData;
    236    struct gl_shader *sh = (struct gl_shader *) data;
    237    if (_mesa_validate_shader_target(ctx, sh->Type)) {
    238       _mesa_delete_shader(ctx, sh);
    239    }
    240    else {
    241       struct gl_shader_program *shProg = (struct gl_shader_program *) data;
    242       assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
    243       _mesa_delete_shader_program(ctx, shProg);
    244    }
    245 }
    246 
    247 
    248 /**
    249  * Callback for deleting a framebuffer object.  Called by _mesa_HashDeleteAll()
    250  */
    251 static void
    252 delete_framebuffer_cb(GLuint id, void *data, void *userData)
    253 {
    254    struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
    255    /* The fact that the framebuffer is in the hashtable means its refcount
    256     * is one, but we're removing from the hashtable now.  So clear refcount.
    257     */
    258    /*assert(fb->RefCount == 1);*/
    259    fb->RefCount = 0;
    260 
    261    /* NOTE: Delete should always be defined but there are two reports
    262     * of it being NULL (bugs 13507, 14293).  Work-around for now.
    263     */
    264    if (fb->Delete)
    265       fb->Delete(fb);
    266 }
    267 
    268 
    269 /**
    270  * Callback for deleting a renderbuffer object. Called by _mesa_HashDeleteAll()
    271  */
    272 static void
    273 delete_renderbuffer_cb(GLuint id, void *data, void *userData)
    274 {
    275    struct gl_context *ctx = (struct gl_context *) userData;
    276    struct gl_renderbuffer *rb = (struct gl_renderbuffer *) data;
    277    rb->RefCount = 0;  /* see comment for FBOs above */
    278    if (rb->Delete)
    279       rb->Delete(ctx, rb);
    280 }
    281 
    282 
    283 /**
    284  * Callback for deleting a sampler object. Called by _mesa_HashDeleteAll()
    285  */
    286 static void
    287 delete_sampler_object_cb(GLuint id, void *data, void *userData)
    288 {
    289    struct gl_context *ctx = (struct gl_context *) userData;
    290    struct gl_sampler_object *sampObj = (struct gl_sampler_object *) data;
    291    _mesa_reference_sampler_object(ctx, &sampObj, NULL);
    292 }
    293 
    294 
    295 /**
    296  * Deallocate a shared state object and all children structures.
    297  *
    298  * \param ctx GL context.
    299  * \param shared shared state pointer.
    300  *
    301  * Frees the display lists, the texture objects (calling the driver texture
    302  * deletion callback to free its private data) and the vertex programs, as well
    303  * as their hash tables.
    304  *
    305  * \sa alloc_shared_state().
    306  */
    307 static void
    308 free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
    309 {
    310    GLuint i;
    311 
    312    /* Free the dummy/fallback texture objects */
    313    for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
    314       if (shared->FallbackTex[i])
    315          ctx->Driver.DeleteTexture(ctx, shared->FallbackTex[i]);
    316    }
    317 
    318    /*
    319     * Free display lists
    320     */
    321    _mesa_HashDeleteAll(shared->DisplayList, delete_displaylist_cb, ctx);
    322    _mesa_DeleteHashTable(shared->DisplayList);
    323    _mesa_HashDeleteAll(shared->BitmapAtlas, delete_bitmap_atlas_cb, ctx);
    324    _mesa_DeleteHashTable(shared->BitmapAtlas);
    325 
    326    _mesa_HashWalk(shared->ShaderObjects, free_shader_program_data_cb, ctx);
    327    _mesa_HashDeleteAll(shared->ShaderObjects, delete_shader_cb, ctx);
    328    _mesa_DeleteHashTable(shared->ShaderObjects);
    329 
    330    _mesa_HashDeleteAll(shared->Programs, delete_program_cb, ctx);
    331    _mesa_DeleteHashTable(shared->Programs);
    332 
    333    _mesa_reference_program(ctx, &shared->DefaultVertexProgram, NULL);
    334    _mesa_reference_program(ctx, &shared->DefaultFragmentProgram, NULL);
    335 
    336    _mesa_HashDeleteAll(shared->ATIShaders, delete_fragshader_cb, ctx);
    337    _mesa_DeleteHashTable(shared->ATIShaders);
    338    _mesa_delete_ati_fragment_shader(ctx, shared->DefaultFragmentShader);
    339 
    340    _mesa_HashDeleteAll(shared->BufferObjects, delete_bufferobj_cb, ctx);
    341    _mesa_DeleteHashTable(shared->BufferObjects);
    342 
    343    _mesa_HashDeleteAll(shared->FrameBuffers, delete_framebuffer_cb, ctx);
    344    _mesa_DeleteHashTable(shared->FrameBuffers);
    345    _mesa_HashDeleteAll(shared->RenderBuffers, delete_renderbuffer_cb, ctx);
    346    _mesa_DeleteHashTable(shared->RenderBuffers);
    347 
    348    _mesa_reference_buffer_object(ctx, &shared->NullBufferObj, NULL);
    349 
    350    {
    351       struct set_entry *entry;
    352 
    353       set_foreach(shared->SyncObjects, entry) {
    354          _mesa_unref_sync_object(ctx, (struct gl_sync_object *) entry->key, 1);
    355       }
    356    }
    357    _mesa_set_destroy(shared->SyncObjects, NULL);
    358 
    359    _mesa_HashDeleteAll(shared->SamplerObjects, delete_sampler_object_cb, ctx);
    360    _mesa_DeleteHashTable(shared->SamplerObjects);
    361 
    362    /*
    363     * Free texture objects (after FBOs since some textures might have
    364     * been bound to FBOs).
    365     */
    366    assert(ctx->Driver.DeleteTexture);
    367    /* the default textures */
    368    for (i = 0; i < NUM_TEXTURE_TARGETS; i++) {
    369       ctx->Driver.DeleteTexture(ctx, shared->DefaultTex[i]);
    370    }
    371 
    372    /* all other textures */
    373    _mesa_HashDeleteAll(shared->TexObjects, delete_texture_cb, ctx);
    374    _mesa_DeleteHashTable(shared->TexObjects);
    375 
    376    mtx_destroy(&shared->Mutex);
    377    mtx_destroy(&shared->TexMutex);
    378 
    379    free(shared);
    380 }
    381 
    382 
    383 /**
    384  * gl_shared_state objects are ref counted.
    385  * If ptr's refcount goes to zero, free the shared state.
    386  */
    387 void
    388 _mesa_reference_shared_state(struct gl_context *ctx,
    389                              struct gl_shared_state **ptr,
    390                              struct gl_shared_state *state)
    391 {
    392    if (*ptr == state)
    393       return;
    394 
    395    if (*ptr) {
    396       /* unref old state */
    397       struct gl_shared_state *old = *ptr;
    398       GLboolean delete;
    399 
    400       mtx_lock(&old->Mutex);
    401       assert(old->RefCount >= 1);
    402       old->RefCount--;
    403       delete = (old->RefCount == 0);
    404       mtx_unlock(&old->Mutex);
    405 
    406       if (delete) {
    407          free_shared_state(ctx, old);
    408       }
    409 
    410       *ptr = NULL;
    411    }
    412 
    413    if (state) {
    414       /* reference new state */
    415       mtx_lock(&state->Mutex);
    416       state->RefCount++;
    417       *ptr = state;
    418       mtx_unlock(&state->Mutex);
    419    }
    420 }
    421