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 
NameSpace(NamedObjectType p_type,GlobalNameSpace * globalNameSpace)21 NameSpace::NameSpace(NamedObjectType p_type,
22                      GlobalNameSpace *globalNameSpace) :
23     m_nextName(0),
24     m_type(p_type),
25     m_globalNameSpace(globalNameSpace) {}
26 
~NameSpace()27 NameSpace::~NameSpace()
28 {
29     for (NamesMap::iterator n = m_localToGlobalMap.begin();
30          n != m_localToGlobalMap.end();
31          n++) {
32         m_globalNameSpace->deleteName(m_type, (*n).second);
33     }
34 }
35 
36 ObjectLocalName
genName(ObjectLocalName p_localName,bool genGlobal,bool genLocal)37 NameSpace::genName(ObjectLocalName p_localName,
38                    bool genGlobal, bool genLocal)
39 {
40     ObjectLocalName localName = p_localName;
41     if (genLocal) {
42         do {
43             localName = ++m_nextName;
44         } while(localName == 0 ||
45                 m_localToGlobalMap.find(localName) !=
46                         m_localToGlobalMap.end() );
47     }
48 
49     if (genGlobal) {
50         unsigned int globalName = m_globalNameSpace->genName(m_type);
51         m_localToGlobalMap[localName] = globalName;
52     }
53 
54     return localName;
55 }
56 
57 
58 unsigned int
genGlobalName(void)59 NameSpace::genGlobalName(void)
60 {
61     return m_globalNameSpace->genName(m_type);
62 }
63 
64 unsigned int
getGlobalName(ObjectLocalName p_localName)65 NameSpace::getGlobalName(ObjectLocalName p_localName)
66 {
67     NamesMap::iterator n( m_localToGlobalMap.find(p_localName) );
68     if (n != m_localToGlobalMap.end()) {
69         // object found - return its global name map
70         return (*n).second;
71     }
72 
73     // object does not exist;
74     return 0;
75 }
76 
77 ObjectLocalName
getLocalName(unsigned int p_globalName)78 NameSpace::getLocalName(unsigned int p_globalName)
79 {
80     for(NamesMap::iterator it = m_localToGlobalMap.begin(); it != m_localToGlobalMap.end();it++){
81         if((*it).second == p_globalName){
82             // object found - return its local name
83             return (*it).first;
84         }
85     }
86 
87     // object does not exist;
88     return 0;
89 }
90 
91 void
deleteName(ObjectLocalName p_localName)92 NameSpace::deleteName(ObjectLocalName p_localName)
93 {
94     NamesMap::iterator n( m_localToGlobalMap.find(p_localName) );
95     if (n != m_localToGlobalMap.end()) {
96         m_globalNameSpace->deleteName(m_type, (*n).second);
97         m_localToGlobalMap.erase(p_localName);
98     }
99 }
100 
101 bool
isObject(ObjectLocalName p_localName)102 NameSpace::isObject(ObjectLocalName p_localName)
103 {
104     return (m_localToGlobalMap.find(p_localName) != m_localToGlobalMap.end() );
105 }
106 
107 void
replaceGlobalName(ObjectLocalName p_localName,unsigned int p_globalName)108 NameSpace::replaceGlobalName(ObjectLocalName p_localName, unsigned int p_globalName)
109 {
110     NamesMap::iterator n( m_localToGlobalMap.find(p_localName) );
111     if (n != m_localToGlobalMap.end()) {
112         m_globalNameSpace->deleteName(m_type, (*n).second);
113         (*n).second = p_globalName;
114     }
115 }
116 
117 
GlobalNameSpace()118 GlobalNameSpace::GlobalNameSpace() : m_lock() {}
119 
~GlobalNameSpace()120 GlobalNameSpace::~GlobalNameSpace() {}
121 
122 unsigned int
genName(NamedObjectType p_type)123 GlobalNameSpace::genName(NamedObjectType p_type)
124 {
125     if ( p_type >= NUM_OBJECT_TYPES ) return 0;
126     unsigned int name = 0;
127 
128     emugl::Mutex::AutoLock _lock(m_lock);
129     switch (p_type) {
130     case VERTEXBUFFER:
131         GLEScontext::dispatcher().glGenBuffers(1,&name);
132         break;
133     case TEXTURE:
134         GLEScontext::dispatcher().glGenTextures(1,&name);
135         break;
136     case RENDERBUFFER:
137         GLEScontext::dispatcher().glGenRenderbuffersEXT(1,&name);
138         break;
139     case FRAMEBUFFER:
140         GLEScontext::dispatcher().glGenFramebuffersEXT(1,&name);
141         break;
142     case SHADER: //objects in shader namepace are not handled
143     default:
144         name = 0;
145     }
146     return name;
147 }
148 
149 void
deleteName(NamedObjectType p_type,unsigned int p_name)150 GlobalNameSpace::deleteName(NamedObjectType p_type, unsigned int p_name)
151 {
152 }
153 
154 typedef std::pair<NamedObjectType, ObjectLocalName> ObjectIDPair;
155 typedef std::map<ObjectIDPair, ObjectDataPtr> ObjectDataMap;
156 
ShareGroup(GlobalNameSpace * globalNameSpace)157 ShareGroup::ShareGroup(GlobalNameSpace *globalNameSpace) : m_lock() {
158     for (int i=0; i < NUM_OBJECT_TYPES; i++) {
159         m_nameSpace[i] = new NameSpace((NamedObjectType)i, globalNameSpace);
160     }
161 
162     m_objectsData = NULL;
163 }
164 
~ShareGroup()165 ShareGroup::~ShareGroup()
166 {
167     emugl::Mutex::AutoLock _lock(m_lock);
168     for (int t = 0; t < NUM_OBJECT_TYPES; t++) {
169         delete m_nameSpace[t];
170     }
171 
172     delete (ObjectDataMap *)m_objectsData;
173 }
174 
175 ObjectLocalName
genName(NamedObjectType p_type,ObjectLocalName p_localName,bool genLocal)176 ShareGroup::genName(NamedObjectType p_type,
177                     ObjectLocalName p_localName,
178                     bool genLocal)
179 {
180     if (p_type >= NUM_OBJECT_TYPES) return 0;
181 
182     emugl::Mutex::AutoLock _lock(m_lock);
183     ObjectLocalName localName =
184             m_nameSpace[p_type]->genName(p_localName, true, genLocal);
185     return localName;
186 }
187 
188 unsigned int
genGlobalName(NamedObjectType p_type)189 ShareGroup::genGlobalName(NamedObjectType p_type)
190 {
191     if (p_type >= NUM_OBJECT_TYPES) return 0;
192 
193     emugl::Mutex::AutoLock _lock(m_lock);
194     return m_nameSpace[p_type]->genGlobalName();
195 }
196 
197 unsigned int
getGlobalName(NamedObjectType p_type,ObjectLocalName p_localName)198 ShareGroup::getGlobalName(NamedObjectType p_type,
199                           ObjectLocalName p_localName)
200 {
201     if (p_type >= NUM_OBJECT_TYPES) return 0;
202 
203     emugl::Mutex::AutoLock _lock(m_lock);
204     return m_nameSpace[p_type]->getGlobalName(p_localName);
205 }
206 
207 ObjectLocalName
getLocalName(NamedObjectType p_type,unsigned int p_globalName)208 ShareGroup::getLocalName(NamedObjectType p_type,
209                          unsigned int p_globalName)
210 {
211     if (p_type >= NUM_OBJECT_TYPES) return 0;
212 
213     emugl::Mutex::AutoLock _lock(m_lock);
214     return m_nameSpace[p_type]->getLocalName(p_globalName);
215 }
216 
217 void
deleteName(NamedObjectType p_type,ObjectLocalName p_localName)218 ShareGroup::deleteName(NamedObjectType p_type, ObjectLocalName p_localName)
219 {
220     if (p_type >= NUM_OBJECT_TYPES) return;
221 
222     emugl::Mutex::AutoLock _lock(m_lock);
223     m_nameSpace[p_type]->deleteName(p_localName);
224     ObjectDataMap *map = (ObjectDataMap *)m_objectsData;
225     if (map) {
226         map->erase( ObjectIDPair(p_type, p_localName) );
227     }
228 }
229 
230 bool
isObject(NamedObjectType p_type,ObjectLocalName p_localName)231 ShareGroup::isObject(NamedObjectType p_type, ObjectLocalName p_localName)
232 {
233     if (p_type >= NUM_OBJECT_TYPES) return 0;
234 
235     emugl::Mutex::AutoLock _lock(m_lock);
236     return m_nameSpace[p_type]->isObject(p_localName);
237 }
238 
239 void
replaceGlobalName(NamedObjectType p_type,ObjectLocalName p_localName,unsigned int p_globalName)240 ShareGroup::replaceGlobalName(NamedObjectType p_type,
241                               ObjectLocalName p_localName,
242                               unsigned int p_globalName)
243 {
244     if (p_type >= NUM_OBJECT_TYPES) return;
245 
246     emugl::Mutex::AutoLock _lock(m_lock);
247     m_nameSpace[p_type]->replaceGlobalName(p_localName, p_globalName);
248 }
249 
250 void
setObjectData(NamedObjectType p_type,ObjectLocalName p_localName,ObjectDataPtr data)251 ShareGroup::setObjectData(NamedObjectType p_type,
252                           ObjectLocalName p_localName,
253                           ObjectDataPtr data)
254 {
255     if (p_type >= NUM_OBJECT_TYPES) return;
256 
257     emugl::Mutex::AutoLock _lock(m_lock);
258 
259     ObjectDataMap *map = (ObjectDataMap *)m_objectsData;
260     if (!map) {
261         map = new ObjectDataMap();
262         m_objectsData = map;
263     }
264 
265     ObjectIDPair id( p_type, p_localName );
266     map->insert( std::pair<ObjectIDPair, ObjectDataPtr>(id, data) );
267 }
268 
269 ObjectDataPtr
getObjectData(NamedObjectType p_type,ObjectLocalName p_localName)270 ShareGroup::getObjectData(NamedObjectType p_type,
271                           ObjectLocalName p_localName)
272 {
273     ObjectDataPtr ret;
274 
275     if (p_type >= NUM_OBJECT_TYPES) return ret;
276 
277     emugl::Mutex::AutoLock _lock(m_lock);
278 
279     ObjectDataMap *map = (ObjectDataMap *)m_objectsData;
280     if (map) {
281         ObjectDataMap::iterator i =
282                 map->find( ObjectIDPair(p_type, p_localName) );
283         if (i != map->end()) ret = (*i).second;
284     }
285     return ret;
286 }
287 
ObjectNameManager(GlobalNameSpace * globalNameSpace)288 ObjectNameManager::ObjectNameManager(GlobalNameSpace *globalNameSpace) :
289     m_lock(), m_globalNameSpace(globalNameSpace) {}
290 
~ObjectNameManager()291 ObjectNameManager::~ObjectNameManager() {}
292 
293 ShareGroupPtr
createShareGroup(void * p_groupName)294 ObjectNameManager::createShareGroup(void *p_groupName)
295 {
296     emugl::Mutex::AutoLock _lock(m_lock);
297 
298     ShareGroupPtr shareGroupReturn;
299 
300     ShareGroupsMap::iterator s( m_groups.find(p_groupName) );
301     if (s != m_groups.end()) {
302         shareGroupReturn = (*s).second;
303     }
304     else {
305         //
306         // Group does not exist, create new group
307         //
308         shareGroupReturn = ShareGroupPtr(new ShareGroup(m_globalNameSpace));
309         m_groups.insert(
310                 std::pair<void*, ShareGroupPtr>(
311                         p_groupName, shareGroupReturn));
312     }
313 
314     return shareGroupReturn;
315 }
316 
317 ShareGroupPtr
getShareGroup(void * p_groupName)318 ObjectNameManager::getShareGroup(void *p_groupName)
319 {
320     emugl::Mutex::AutoLock _lock(m_lock);
321 
322     ShareGroupPtr shareGroupReturn(NULL);
323 
324     ShareGroupsMap::iterator s( m_groups.find(p_groupName) );
325     if (s != m_groups.end()) {
326         shareGroupReturn = (*s).second;
327     }
328 
329     return shareGroupReturn;
330 }
331 
332 ShareGroupPtr
attachShareGroup(void * p_groupName,void * p_existingGroupName)333 ObjectNameManager::attachShareGroup(void *p_groupName,
334                                     void *p_existingGroupName)
335 {
336     emugl::Mutex::AutoLock _lock(m_lock);
337 
338     ShareGroupsMap::iterator s( m_groups.find(p_existingGroupName) );
339     if (s == m_groups.end()) {
340         // ShareGroup did not found !!!
341         return ShareGroupPtr(NULL);
342     }
343 
344     ShareGroupPtr shareGroupReturn((*s).second);
345     if (m_groups.find(p_groupName) == m_groups.end()) {
346         m_groups.insert(
347                 std::pair<void*, ShareGroupPtr>(
348                         p_groupName, shareGroupReturn));
349     }
350     return shareGroupReturn;
351 }
352 
353 void
deleteShareGroup(void * p_groupName)354 ObjectNameManager::deleteShareGroup(void *p_groupName)
355 {
356     emugl::Mutex::AutoLock _lock(m_lock);
357 
358     ShareGroupsMap::iterator s( m_groups.find(p_groupName) );
359     if (s != m_groups.end()) {
360         m_groups.erase(s);
361     }
362 }
363 
getGlobalContext()364 void *ObjectNameManager::getGlobalContext()
365 {
366     emugl::Mutex::AutoLock _lock(m_lock);
367     return (m_groups.size() > 0) ? (*m_groups.begin()).first : NULL;
368 }
369 
370