Home | History | Annotate | Download | only in GLcommon
      1 /*
      2 * Copyright (C) 2011 The Android Open Source Project
      3 *
      4 * Licensed under the Apache License, Version 2.0 (the "License");
      5 * you may not use this file except in compliance with the License.
      6 * You may obtain a copy of the License at
      7 *
      8 * http://www.apache.org/licenses/LICENSE-2.0
      9 *
     10 * Unless required by applicable law or agreed to in writing, software
     11 * distributed under the License is distributed on an "AS IS" BASIS,
     12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 * See the License for the specific language governing permissions and
     14 * limitations under the License.
     15 */
     16 #include <map>
     17 #include <GLcommon/objectNameManager.h>
     18 #include <GLcommon/GLEScontext.h>
     19 
     20 
     21 NameSpace::NameSpace(NamedObjectType p_type, GlobalNameSpace *globalNameSpace) :
     22     m_nextName(0),
     23     m_type(p_type),
     24     m_globalNameSpace(globalNameSpace)
     25 {
     26 }
     27 
     28 NameSpace::~NameSpace()
     29 {
     30     for (NamesMap::iterator n = m_localToGlobalMap.begin();
     31          n != m_localToGlobalMap.end();
     32          n++) {
     33         m_globalNameSpace->deleteName(m_type, (*n).second);
     34     }
     35 }
     36 
     37 ObjectLocalName
     38 NameSpace::genName(ObjectLocalName p_localName, bool genGlobal, bool genLocal)
     39 {
     40 
     41     ObjectLocalName localName = p_localName;
     42     if (genLocal) {
     43         do {
     44             localName = ++m_nextName;
     45         } while( localName == 0 || m_localToGlobalMap.find(localName) != m_localToGlobalMap.end() );
     46     }
     47 
     48     if (genGlobal) {
     49         unsigned int globalName = m_globalNameSpace->genName(m_type);
     50         m_localToGlobalMap[localName] = globalName;
     51     }
     52 
     53     return localName;
     54 }
     55 
     56 
     57 unsigned int
     58 NameSpace::genGlobalName(void)
     59 {
     60     return m_globalNameSpace->genName(m_type);
     61 }
     62 
     63 unsigned int
     64 NameSpace::getGlobalName(ObjectLocalName p_localName)
     65 {
     66     NamesMap::iterator n( m_localToGlobalMap.find(p_localName) );
     67     if (n != m_localToGlobalMap.end()) {
     68         // object found - return its global name map
     69         return (*n).second;
     70     }
     71 
     72     // object does not exist;
     73     return 0;
     74 }
     75 
     76 ObjectLocalName
     77 NameSpace::getLocalName(unsigned int p_globalName)
     78 {
     79     for(NamesMap::iterator it = m_localToGlobalMap.begin(); it != m_localToGlobalMap.end();it++){
     80         if((*it).second == p_globalName){
     81             // object found - return its local name
     82             return (*it).first;
     83         }
     84     }
     85 
     86     // object does not exist;
     87     return 0;
     88 }
     89 
     90 void
     91 NameSpace::deleteName(ObjectLocalName p_localName)
     92 {
     93     NamesMap::iterator n( m_localToGlobalMap.find(p_localName) );
     94     if (n != m_localToGlobalMap.end()) {
     95         m_globalNameSpace->deleteName(m_type, (*n).second);
     96         m_localToGlobalMap.erase(p_localName);
     97     }
     98 }
     99 
    100 bool
    101 NameSpace::isObject(ObjectLocalName p_localName)
    102 {
    103     return (m_localToGlobalMap.find(p_localName) != m_localToGlobalMap.end() );
    104 }
    105 
    106 void
    107 NameSpace::replaceGlobalName(ObjectLocalName p_localName, unsigned int p_globalName)
    108 {
    109     NamesMap::iterator n( m_localToGlobalMap.find(p_localName) );
    110     if (n != m_localToGlobalMap.end()) {
    111         m_globalNameSpace->deleteName(m_type, (*n).second);
    112         (*n).second = p_globalName;
    113     }
    114 }
    115 
    116 
    117 GlobalNameSpace::GlobalNameSpace()
    118 {
    119     mutex_init(&m_lock);
    120 }
    121 
    122 GlobalNameSpace::~GlobalNameSpace()
    123 {
    124     mutex_destroy(&m_lock);
    125 }
    126 
    127 unsigned int
    128 GlobalNameSpace::genName(NamedObjectType p_type)
    129 {
    130     if ( p_type >= NUM_OBJECT_TYPES ) return 0;
    131     unsigned int name = 0;
    132 
    133     mutex_lock(&m_lock);
    134     switch (p_type) {
    135     case VERTEXBUFFER:
    136         GLEScontext::dispatcher().glGenBuffers(1,&name);
    137         break;
    138     case TEXTURE:
    139         GLEScontext::dispatcher().glGenTextures(1,&name);
    140         break;
    141     case RENDERBUFFER:
    142         GLEScontext::dispatcher().glGenRenderbuffersEXT(1,&name);
    143         break;
    144     case FRAMEBUFFER:
    145         GLEScontext::dispatcher().glGenFramebuffersEXT(1,&name);
    146         break;
    147     case SHADER: //objects in shader namepace are not handled
    148     default:
    149         name = 0;
    150     }
    151     mutex_unlock(&m_lock);
    152     return name;
    153 }
    154 
    155 void
    156 GlobalNameSpace::deleteName(NamedObjectType p_type, unsigned int p_name)
    157 {
    158 }
    159 
    160 typedef std::pair<NamedObjectType, ObjectLocalName> ObjectIDPair;
    161 typedef std::map<ObjectIDPair, ObjectDataPtr> ObjectDataMap;
    162 
    163 ShareGroup::ShareGroup(GlobalNameSpace *globalNameSpace)
    164 {
    165     mutex_init(&m_lock);
    166 
    167     for (int i=0; i<NUM_OBJECT_TYPES; i++) {
    168         m_nameSpace[i] = new NameSpace((NamedObjectType)i, globalNameSpace);
    169     }
    170 
    171     m_objectsData = NULL;
    172 }
    173 
    174 ShareGroup::~ShareGroup()
    175 {
    176     mutex_lock(&m_lock);
    177     for (int t = 0; t < NUM_OBJECT_TYPES; t++) {
    178         delete m_nameSpace[t];
    179     }
    180 
    181     ObjectDataMap *map = (ObjectDataMap *)m_objectsData;
    182     if (map) delete map;
    183 
    184     mutex_unlock(&m_lock);
    185     mutex_destroy(&m_lock);
    186 }
    187 
    188 ObjectLocalName
    189 ShareGroup::genName(NamedObjectType p_type, ObjectLocalName p_localName, bool genLocal)
    190 {
    191     if (p_type >= NUM_OBJECT_TYPES) return 0;
    192 
    193     mutex_lock(&m_lock);
    194     ObjectLocalName localName = m_nameSpace[p_type]->genName(p_localName,true,genLocal);
    195     mutex_unlock(&m_lock);
    196 
    197     return localName;
    198 }
    199 
    200 unsigned int
    201 ShareGroup::genGlobalName(NamedObjectType p_type)
    202 {
    203     if (p_type >= NUM_OBJECT_TYPES) return 0;
    204 
    205     mutex_lock(&m_lock);
    206     unsigned int name = m_nameSpace[p_type]->genGlobalName();
    207     mutex_unlock(&m_lock);
    208 
    209     return name;
    210 }
    211 
    212 unsigned int
    213 ShareGroup::getGlobalName(NamedObjectType p_type, ObjectLocalName p_localName)
    214 {
    215     if (p_type >= NUM_OBJECT_TYPES) return 0;
    216 
    217     mutex_lock(&m_lock);
    218     unsigned int globalName = m_nameSpace[p_type]->getGlobalName(p_localName);
    219     mutex_unlock(&m_lock);
    220 
    221     return globalName;
    222 }
    223 
    224 ObjectLocalName
    225 ShareGroup::getLocalName(NamedObjectType p_type, unsigned int p_globalName)
    226 {
    227     if (p_type >= NUM_OBJECT_TYPES) return 0;
    228 
    229     mutex_lock(&m_lock);
    230     ObjectLocalName localName = m_nameSpace[p_type]->getLocalName(p_globalName);
    231     mutex_unlock(&m_lock);
    232 
    233     return localName;
    234 }
    235 
    236 void
    237 ShareGroup::deleteName(NamedObjectType p_type, ObjectLocalName p_localName)
    238 {
    239     if (p_type >= NUM_OBJECT_TYPES) return;
    240 
    241     mutex_lock(&m_lock);
    242     m_nameSpace[p_type]->deleteName(p_localName);
    243     ObjectDataMap *map = (ObjectDataMap *)m_objectsData;
    244     if (map) {
    245         map->erase( ObjectIDPair(p_type, p_localName) );
    246     }
    247     mutex_unlock(&m_lock);
    248 }
    249 
    250 bool
    251 ShareGroup::isObject(NamedObjectType p_type, ObjectLocalName p_localName)
    252 {
    253     if (p_type >= NUM_OBJECT_TYPES) return 0;
    254 
    255     mutex_lock(&m_lock);
    256     bool exist = m_nameSpace[p_type]->isObject(p_localName);
    257     mutex_unlock(&m_lock);
    258 
    259     return exist;
    260 }
    261 
    262 void
    263 ShareGroup::replaceGlobalName(NamedObjectType p_type, ObjectLocalName p_localName, unsigned int p_globalName)
    264 {
    265     if (p_type >= NUM_OBJECT_TYPES) return;
    266 
    267     mutex_lock(&m_lock);
    268     m_nameSpace[p_type]->replaceGlobalName(p_localName, p_globalName);
    269     mutex_unlock(&m_lock);
    270 }
    271 
    272 void
    273 ShareGroup::setObjectData(NamedObjectType p_type, ObjectLocalName p_localName, ObjectDataPtr data)
    274 {
    275     if (p_type >= NUM_OBJECT_TYPES) return;
    276 
    277     mutex_lock(&m_lock);
    278 
    279     ObjectDataMap *map = (ObjectDataMap *)m_objectsData;
    280     if (!map) {
    281         map = new ObjectDataMap();
    282         m_objectsData = map;
    283     }
    284 
    285     ObjectIDPair id( p_type, p_localName );
    286     map->insert( std::pair<ObjectIDPair, ObjectDataPtr>(id, data) );
    287 
    288     mutex_unlock(&m_lock);
    289 }
    290 
    291 ObjectDataPtr
    292 ShareGroup::getObjectData(NamedObjectType p_type, ObjectLocalName p_localName)
    293 {
    294     ObjectDataPtr ret;
    295 
    296     if (p_type >= NUM_OBJECT_TYPES) return ret;
    297 
    298     mutex_lock(&m_lock);
    299 
    300     ObjectDataMap *map = (ObjectDataMap *)m_objectsData;
    301     if (map) {
    302         ObjectDataMap::iterator i = map->find( ObjectIDPair(p_type, p_localName) );
    303         if (i != map->end()) ret = (*i).second;
    304     }
    305 
    306     mutex_unlock(&m_lock);
    307 
    308     return ret;
    309 }
    310 
    311 ObjectNameManager::ObjectNameManager(GlobalNameSpace *globalNameSpace) :
    312     m_globalNameSpace(globalNameSpace)
    313 {
    314     mutex_init(&m_lock);
    315 }
    316 
    317 ObjectNameManager::~ObjectNameManager()
    318 {
    319     mutex_destroy(&m_lock);
    320 }
    321 
    322 ShareGroupPtr
    323 ObjectNameManager::createShareGroup(void *p_groupName)
    324 {
    325     mutex_lock(&m_lock);
    326 
    327     ShareGroupPtr shareGroupReturn;
    328 
    329     ShareGroupsMap::iterator s( m_groups.find(p_groupName) );
    330     if (s != m_groups.end()) {
    331         shareGroupReturn = (*s).second;
    332     }
    333     else {
    334         //
    335         // Group does not exist, create new group
    336         //
    337         shareGroupReturn = ShareGroupPtr( new ShareGroup(m_globalNameSpace) );
    338         m_groups.insert( std::pair<void *, ShareGroupPtr>(p_groupName, shareGroupReturn) );
    339     }
    340 
    341     mutex_unlock(&m_lock);
    342 
    343     return shareGroupReturn;
    344 }
    345 
    346 ShareGroupPtr
    347 ObjectNameManager::getShareGroup(void *p_groupName)
    348 {
    349     mutex_lock(&m_lock);
    350 
    351     ShareGroupPtr shareGroupReturn(NULL);
    352 
    353     ShareGroupsMap::iterator s( m_groups.find(p_groupName) );
    354     if (s != m_groups.end()) {
    355         shareGroupReturn = (*s).second;
    356     }
    357     mutex_unlock(&m_lock);
    358 
    359     return shareGroupReturn;
    360 }
    361 
    362 ShareGroupPtr
    363 ObjectNameManager::attachShareGroup(void *p_groupName, void *p_existingGroupName)
    364 {
    365     mutex_lock(&m_lock);
    366 
    367     ShareGroupPtr shareGroupReturn;
    368 
    369     ShareGroupsMap::iterator s( m_groups.find(p_existingGroupName) );
    370     if (s == m_groups.end()) {
    371         // ShareGroup did not found !!!
    372         mutex_unlock(&m_lock);
    373         return ShareGroupPtr(NULL);
    374     }
    375 
    376     shareGroupReturn = (*s).second;
    377 
    378     if (m_groups.find(p_groupName) == m_groups.end())
    379     {
    380         m_groups.insert( std::pair<void *, ShareGroupPtr>(p_groupName, shareGroupReturn) );
    381     }
    382 
    383     mutex_unlock(&m_lock);
    384 
    385     return shareGroupReturn;
    386 }
    387 
    388 void
    389 ObjectNameManager::deleteShareGroup(void *p_groupName)
    390 {
    391     mutex_lock(&m_lock);
    392 
    393     ShareGroupsMap::iterator s( m_groups.find(p_groupName) );
    394     if (s != m_groups.end()) {
    395         m_groups.erase(s);
    396     }
    397 
    398     mutex_unlock(&m_lock);
    399 }
    400 
    401 void *ObjectNameManager::getGlobalContext()
    402 {
    403     void *ret = NULL;
    404 
    405     mutex_lock(&m_lock);
    406     if (m_groups.size() > 0) ret = (*m_groups.begin()).first;
    407     mutex_unlock(&m_lock);
    408 
    409     return ret;
    410 }
    411 
    412