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 #ifndef _OBJECT_NAME_MANAGER_H 17 #define _OBJECT_NAME_MANAGER_H 18 19 #include <cutils/threads.h> 20 #include <map> 21 #include "SmartPtr.h" 22 23 enum NamedObjectType { 24 VERTEXBUFFER = 0, 25 TEXTURE = 1, 26 RENDERBUFFER = 2, 27 FRAMEBUFFER = 3, 28 SHADER = 4, 29 NUM_OBJECT_TYPES = 5 // Must be last 30 }; 31 32 enum ObjectDataType { 33 SHADER_DATA, 34 PROGRAM_DATA, 35 TEXTURE_DATA, 36 BUFFER_DATA, 37 UNDEFINED_DATA 38 }; 39 40 class ObjectData 41 { 42 public: 43 ObjectData() : m_dataType(UNDEFINED_DATA) {}; 44 ObjectData(ObjectDataType type): m_dataType(type) {}; 45 ObjectDataType getDataType() { return m_dataType; }; 46 virtual ~ObjectData() {}; 47 private: 48 ObjectDataType m_dataType; 49 }; 50 typedef SmartPtr<ObjectData> ObjectDataPtr; 51 typedef unsigned long long ObjectLocalName; 52 typedef std::map<ObjectLocalName, unsigned int> NamesMap; 53 54 // 55 // Class NameSpace - this class manages allocations and deletions of objects 56 // from a single "local" namespace (private to context group). 57 // For each allocated object name, a "global" name is 58 // generated as well to be used in the space where all 59 // contexts are shared. 60 // 61 // NOTE: this class does not used by the EGL/GLES layer directly, 62 // the EGL/GLES layer creates objects using the ShareGroup class 63 // interface (see below). 64 class GlobalNameSpace; 65 class NameSpace 66 { 67 friend class ShareGroup; 68 friend class GlobalNameSpace; 69 70 private: 71 NameSpace(NamedObjectType p_type, GlobalNameSpace *globalNameSpace); 72 ~NameSpace(); 73 74 // 75 // genName - creates new object in the namespace and returns its name. 76 // if genLocal is false then the specified p_localName will be used. 77 // This function also generate a global name for the object, 78 // the value of the global name can be retrieved using the 79 // getGlobalName function. 80 // 81 ObjectLocalName genName(ObjectLocalName p_localName, bool genGlobal, bool genLocal); 82 83 // genGlobalName() - This function creates a global name 84 // with no associated local name, for the 85 // translator internal use. 86 unsigned int genGlobalName(void); 87 88 // 89 // getGlobalName - returns the global name of an object or 0 if the object 90 // does not exist. 91 // 92 unsigned int getGlobalName(ObjectLocalName p_localName); 93 94 // 95 // getLocaalName - returns the local name of an object or 0 if the object 96 // does not exist. 97 // 98 ObjectLocalName getLocalName(unsigned int p_globalName); 99 100 // 101 // deleteName - deletes and object from the namespace as well as its 102 // global name from the global name space. 103 // 104 void deleteName(ObjectLocalName p_localName); 105 106 // 107 // isObject - returns true if the named object exist. 108 // 109 bool isObject(ObjectLocalName p_localName); 110 111 // 112 // replaces an object to map to an existing global object 113 // 114 void replaceGlobalName(ObjectLocalName p_localName, unsigned int p_globalName); 115 116 private: 117 ObjectLocalName m_nextName; 118 NamesMap m_localToGlobalMap; 119 const NamedObjectType m_type; 120 GlobalNameSpace *m_globalNameSpace; 121 }; 122 123 class GlobalNameSpace 124 { 125 public: 126 GlobalNameSpace(); 127 ~GlobalNameSpace(); 128 unsigned int genName(NamedObjectType p_type); 129 void deleteName(NamedObjectType p_type, unsigned int p_name); 130 131 private: 132 mutex_t m_lock; 133 }; 134 135 // 136 // class ShareGroup - 137 // That class manages objects of one "local" context share group, typically 138 // there will be one inctance of ShareGroup for each user OpenGL context 139 // unless the user context share with another user context. In that case they 140 // both will share the same ShareGroup instance. 141 // calls into that class gets serialized through a lock so it is thread safe. 142 // 143 class ShareGroup 144 { 145 friend class ObjectNameManager; 146 friend class SmartPtr<ShareGroup>; // to allow destructing when ShareGroupPtr refcount reaches zero 147 148 public: 149 150 // 151 // genName - generates new object name and returns its name value. 152 // if genLocal is false, p_localName will be used as the name. 153 // This function also generates a "global" name for the object 154 // which can be queried using the getGlobalName function. 155 // 156 ObjectLocalName genName(NamedObjectType p_type, ObjectLocalName p_localName = 0, bool genLocal= false); 157 158 // genGlobalName() - This function creates a global name 159 // with no associated local name, for the 160 // translator internal use. 161 unsigned int genGlobalName(NamedObjectType p_type); 162 163 // 164 // getGlobalName - retrieves the "global" name of an object or 0 if the 165 // object does not exist. 166 // 167 unsigned int getGlobalName(NamedObjectType p_type, ObjectLocalName p_localName); 168 169 // 170 // getLocalName - retrieves the "local" name of an object or 0 if the 171 // object does not exist. 172 // 173 ObjectLocalName getLocalName(NamedObjectType p_type, unsigned int p_globalName); 174 175 // 176 // deleteName - deletes and object from the namespace as well as its 177 // global name from the global name space. 178 // 179 void deleteName(NamedObjectType p_type, ObjectLocalName p_localName); 180 181 // 182 // replaceGlobalName - replaces an object to map to an existing global 183 // named object. (used when creating EGLImage siblings) 184 // 185 void replaceGlobalName(NamedObjectType p_type, ObjectLocalName p_localName, unsigned int p_globalName); 186 187 // 188 // isObject - returns true if the named object exist. 189 // 190 bool isObject(NamedObjectType p_type, ObjectLocalName p_localName); 191 192 // 193 // Assign object global data to a names object 194 // 195 void setObjectData(NamedObjectType p_type, ObjectLocalName p_localName, ObjectDataPtr data); 196 197 // 198 // Retrieve object global data 199 // 200 ObjectDataPtr getObjectData(NamedObjectType p_type, ObjectLocalName p_localName); 201 202 private: 203 explicit ShareGroup(GlobalNameSpace *globalNameSpace); 204 ~ShareGroup(); 205 206 private: 207 mutex_t m_lock; 208 NameSpace *m_nameSpace[NUM_OBJECT_TYPES]; 209 void *m_objectsData; 210 }; 211 212 typedef SmartPtr<ShareGroup> ShareGroupPtr; 213 typedef std::multimap<void *, ShareGroupPtr> ShareGroupsMap; 214 215 // 216 // ObjectNameManager - 217 // This class manages the set of all ShareGroups instances, 218 // each ShareGroup instance can be accessed through one or more 'groupName' 219 // values. the type of 'groupName' is void *, the intent is that the EGL 220 // layer will use the user context handle as the name for its ShareGroup 221 // object. Multiple names can be attached to a ShareGroup object to support 222 // user context sharing. 223 // 224 class ObjectNameManager 225 { 226 public: 227 explicit ObjectNameManager(GlobalNameSpace *globalNameSpace); 228 ~ObjectNameManager(); 229 230 // 231 // createShareGroup - create a new ShareGroup object and attach it with 232 // the "name" specified by p_groupName. 233 // 234 ShareGroupPtr createShareGroup(void *p_groupName); 235 236 // 237 // attachShareGroup - find the ShareGroup object attached to the name 238 // specified in p_existingGroupName and attach p_groupName to the same 239 // ShareGroup instance. 240 // 241 ShareGroupPtr attachShareGroup(void *p_groupName, void *p_existingGroupName); 242 243 // 244 // getShareGroup - retreive a ShareGroup object based on its "name" 245 // 246 ShareGroupPtr getShareGroup(void *p_groupName); 247 248 // 249 // deleteShareGroup - deletes the attachment of the p_groupName to its 250 // attached ShareGroup. When the last name of ShareGroup is 251 // deleted the ShareGroup object is destroyed. 252 // 253 void deleteShareGroup(void *p_groupName); 254 255 // 256 // getGlobalContext() - this function returns a name of an existing 257 // ShareGroup. The intent is that the EGL layer will 258 // use that function to get the GL context which each 259 // new context needs to share with. 260 // 261 void *getGlobalContext(); 262 263 private: 264 ShareGroupsMap m_groups; 265 mutex_t m_lock; 266 GlobalNameSpace *m_globalNameSpace; 267 }; 268 269 #endif 270