• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2016 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 
17 #pragma once
18 
19 #include "aemu/base/containers/HybridComponentManager.h"
20 #include "aemu/base/synchronization/Lock.h"
21 #include "snapshot/common.h"
22 #include "GLcommon/GLBackgroundLoader.h"
23 #include "GLcommon/NamedObject.h"
24 #include "GLcommon/ObjectData.h"
25 #include "GLcommon/SaveableTexture.h"
26 #include "GLcommon/TranslatorIfaces.h"
27 
28 #include <GLES/gl.h>
29 #include <unordered_map>
30 #include <unordered_set>
31 
32 typedef android::base::HybridComponentManager<10000, ObjectLocalName, NamedObjectPtr> NamesMap;
33 typedef std::unordered_map<ObjectLocalName, ObjectDataPtr> ObjectDataMap;
34 typedef android::base::HybridComponentManager<10000, unsigned int, ObjectLocalName> GlobalToLocalNamesMap;
35 typedef android::base::HybridComponentManager<10000, ObjectLocalName, bool> BoundAtLeastOnceMap;
36 
37 class GlobalNameSpace;
38 
39 //
40 // Class NameSpace - this class manages allocations and deletions of objects
41 //                   from a single "local" namespace (private to a context share
42 //                   group). For each allocated object name, a "global" name is
43 //                   generated as well to be used in the space where all
44 //                   contexts are shared.
45 //
46 //   NOTE: this class does not used by the EGL/GLES layer directly,
47 //         the EGL/GLES layer creates objects using the ShareGroup class
48 //         interface (see below).
49 class NameSpace
50 {
51 public:
52 
53     NameSpace(NamedObjectType p_type, GlobalNameSpace *globalNameSpace,
54               android::base::Stream* stream,
55               const ObjectData::loadObject_t& loadObject);
56     ~NameSpace();
57 
58     //
59     // genName - creates new object in the namespace and  returns its name.
60     //           if genLocal is false then the specified p_localName will be used.
61     //           This function also generate a global name for the object,
62     //           the value of the global name can be retrieved using the
63     //           getGlobalName function.
64     //
65     ObjectLocalName genName(GenNameInfo genNameInfo, ObjectLocalName p_localName, bool genLocal);
66 
67     //
68     // getGlobalName - returns the global name of an object or 0 if the object
69     //                 does not exist.
70     //
71     unsigned int getGlobalName(ObjectLocalName p_localName, bool* found = nullptr);
72 
73     //
74     // getLocaalName - returns the local name of an object or 0 if the object
75     //                 does not exist.
76     //
77     ObjectLocalName getLocalName(unsigned int p_globalName);
78 
79     //
80     // getNamedObject - returns the shared pointer of an object or null if the
81     //                  object does not exist.
82     NamedObjectPtr getNamedObject(ObjectLocalName p_localName);
83 
84     //
85     // deleteName - deletes and object from the namespace as well as its
86     //              global name from the global name space.
87     //
88     void deleteName(ObjectLocalName p_localName);
89 
90     //
91     // isObject - returns true if the named object exist.
92     //
93     bool isObject(ObjectLocalName p_localName);
94 
95     //
96     // sets an object to map to an existing global object.
97     //
98     void setGlobalObject(ObjectLocalName p_localName,
99             NamedObjectPtr p_namedObject);
100     //
101     // replaces an object to map to an existing global object
102     //
103     void replaceGlobalObject(ObjectLocalName p_localName, NamedObjectPtr p_namedObject);
104 
105     // sets that the local name has been bound at least once, to save time later
106     void setBoundAtLeastOnce(ObjectLocalName p_localName);
107     bool everBound(ObjectLocalName p_localName) const;
108 
109     const ObjectDataPtr& getObjectDataPtr(ObjectLocalName p_localName);
110     void setObjectData(ObjectLocalName p_localName, ObjectDataPtr data);
111     // snapshot functions
112     void postLoad(const ObjectData::getObjDataPtr_t& getObjDataPtr);
113     void postLoadRestore(const ObjectData::getGlobalName_t& getGlobalName);
114     void preSave(GlobalNameSpace *globalNameSpace);
115     void onSave(android::base::Stream* stream);
116     ObjectDataMap::const_iterator objDataMapBegin() const;
117     ObjectDataMap::const_iterator objDataMapEnd() const;
118 private:
119     ObjectLocalName m_nextName = 0;
120     NamesMap m_localToGlobalMap;
121     ObjectDataMap m_objectDataMap;
122     BoundAtLeastOnceMap m_boundMap;
123     GlobalToLocalNamesMap m_globalToLocalMap;
124     const NamedObjectType m_type;
125     GlobalNameSpace *m_globalNameSpace = nullptr;
126     // touchTextures loads all textures onto GPU
127     // Please only call it if the NameSpace is for textures
128     void touchTextures();
129 };
130 
131 struct EglImage;
132 class TextureData;
133 
134 // Class GlobalNameSpace - this class maintain all global GL object names.
135 //                         It is contained in the EglDisplay. One emulator has
136 //                         only one GlobalNameSpace.
137 class GlobalNameSpace
138 {
139 public:
140     friend class NamedObject;
141 
142     // The following are used for snapshot
143     void preSaveAddEglImage(EglImage* eglImage);
144     void preSaveAddTex(TextureData* texture);
145     void onSave(android::base::Stream* stream,
146                 const android::snapshot::ITextureSaverPtr& textureSaver,
147                 SaveableTexture::saver_t saver);
148     void onLoad(android::base::Stream* stream,
149                 const android::snapshot::ITextureLoaderWPtr& textureLoaderWPtr,
150                 SaveableTexture::creator_t creator);
151     void postLoad(android::base::Stream* stream);
152     const SaveableTexturePtr& getSaveableTextureFromLoad(unsigned int oldGlobalName);
getSaveableTextureMap()153     SaveableTextureMap* getSaveableTextureMap() { return &m_textureMap; }
154 
155     void clearTextureMap();
156 
setIfaces(const EGLiface * eglIface,const GLESiface * glesIface)157     void setIfaces(const EGLiface* eglIface,
158                    const GLESiface* glesIface) {
159         m_eglIface = eglIface;
160         m_glesIface = glesIface;
161     }
162 
163 private:
164 
165     android::base::Lock m_lock;
166     // m_textureMap is only used when saving / loading a snapshot
167     // It is empty in all other situations
168     SaveableTextureMap m_textureMap;
169 
170     std::shared_ptr<GLBackgroundLoader>     m_backgroundLoader;
171 
172     const EGLiface* m_eglIface = nullptr;
173     const GLESiface* m_glesIface = nullptr;
174 };
175