• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 EGL_DISPLAY_H
17 #define EGL_DISPLAY_H
18 
19 #include <EGL/egl.h>
20 #include <EGL/eglext.h>
21 
22 #include "aemu/base/synchronization/Lock.h"
23 #include "aemu/base/files/Stream.h"
24 #include "EglConfig.h"
25 #include "EglContext.h"
26 #include "EglOsApi.h"
27 #include "EglSurface.h"
28 #include "EglWindowSurface.h"
29 #include "GLcommon/ObjectNameSpace.h"
30 #include <memory>
31 #include <unordered_map>
32 #include <unordered_set>
33 #include <vector>
34 
35 typedef std::vector<std::unique_ptr<EglConfig>> ConfigsList;
36 typedef std::unordered_map<unsigned int, ContextPtr>  ContextsHndlMap;
37 typedef std::unordered_map<unsigned int, SurfacePtr>  SurfacesHndlMap;
38 
39 
40 typedef std::unordered_set<EglConfig> ConfigSet;
41 
42 class EglDisplay {
43 public:
44     // Create new EglDisplay instance from a given native display |dpy|,
45     // with matching internal display |idpy|. If |isDefault| is true,
46     // this will be considered the default diplay.
47     EglDisplay(EGLNativeDisplayType dpy, EglOS::Display* idpy);
48 
49     // Return the EglOs Engine display handle for this EglDisplay.
getEglOsEngineDisplay()50     EGLNativeDisplayType getEglOsEngineDisplay() const { return m_dpy; }
51 
52     // Return the native internal display handle for this EglDisplay.
nativeType()53     EglOS::Display* nativeType() const { return m_idpy; }
54 
55     // Return the number of known configurations for this EglDisplay.
nConfigs()56     int nConfigs() const { return m_configs.size(); }
57 
58     // Returns the max supported GLES version
getMaxGlesVersion()59     EglOS::GlesVersion getMaxGlesVersion() const {
60         return nativeType()->getMaxGlesVersion();
61     }
62 
getExtensionString()63     const char* getExtensionString() {
64         return nativeType()->getExtensionString();
65     }
66 
getVendorString()67     const char* getVendorString() {
68         return nativeType()->getVendorString();
69     }
70 
createNativeImage(EGLDisplay dpy,EGLContext ctx,EGLenum target,EGLClientBuffer buffer,const EGLint * attrib_list)71     EGLImage createNativeImage(
72             EGLDisplay dpy,
73             EGLContext ctx,
74             EGLenum target,
75             EGLClientBuffer buffer,
76             const EGLint *attrib_list) {
77         return nativeType()->createImageKHR(dpy, ctx, target, buffer, attrib_list);
78     }
79 
destroyNativeImage(EGLDisplay dpy,EGLImage image)80     EGLBoolean destroyNativeImage(
81             EGLDisplay dpy,
82             EGLImage image) {
83         return nativeType()->destroyImageKHR(dpy, image);
84     }
85 
getHostDriverDisplay()86     EGLDisplay getHostDriverDisplay() {
87         return nativeType()->getNative();
88     }
89 
getNativeContext(EGLContext ctx)90     void* getNativeContext(EGLContext ctx) const {
91         auto dispContext = getContext(ctx);
92         if (!dispContext) return nullptr;
93         return dispContext->nativeType()->getNative();
94     }
95 
releaseThread()96     EGLBoolean releaseThread() {
97         return nativeType()->releaseThread();
98     }
99 
100     // Write up to |config_size| EGLConfig values into the |configs| array.
101     // Return the number if values written.
102     int getConfigs(EGLConfig* configs,int config_size) const;
103 
104     // Select all display configurations that match at least the values
105     // in |dummy|. If |configs| is NULL, this returns the number of all
106     // matching configs. Otherwise, this writes into |configs| up to
107     // |config_size| matching EGLConfig values, and returns their number.
108     int chooseConfigs(const EglConfig& dummy,
109                       EGLConfig* configs,
110                       int config_size) const;
111 
112     // Return the EglConfig value that matches a given EGLConfig |conf|.
113     EglConfig* getConfig(EGLConfig conf) const;
114 
115     // Return the EglConfig value that matches a given EGLConfig with
116     // EGL_CONFIG_ID value |id|.
117     EglConfig* getConfig(EGLint id) const;
118 
119     EglConfig* getDefaultConfig() const;
120 
121     EGLSurface addSurface(SurfacePtr s );
122     SurfacePtr getSurface(EGLSurface surface) const;
123     bool removeSurface(EGLSurface s);
124     EGLContext addContext(ContextPtr ctx );
125     ContextPtr getContext(EGLContext ctx) const;
126     void* getLowLevelContext(EGLContext ctx) const;
127     bool removeContext(EGLContext ctx);
128     bool removeContext(ContextPtr ctx);
getManager(GLESVersion ver)129     ObjectNameManager* getManager(GLESVersion ver) const { return m_manager[ver];}
130 
131     ~EglDisplay();
132     void initialize(int renderableType);
133     void terminate();
134     bool isInitialize();
135 
136     ImagePtr getImage(EGLImageKHR img,
137         SaveableTexture::restorer_t restorer) const;
138     EGLImageKHR addImageKHR(ImagePtr);
139     bool destroyImageKHR(EGLImageKHR img);
140     EglOS::Context* getGlobalSharedContext() const;
getGlobalNameSpace()141     GlobalNameSpace* getGlobalNameSpace() { return &m_globalNameSpace; }
142 
143     void onSaveAllImages(android::base::Stream* stream,
144                          const android::snapshot::ITextureSaverPtr& textureSaver,
145                          SaveableTexture::saver_t saver,
146                          SaveableTexture::restorer_t restorer);
147     void onLoadAllImages(android::base::Stream* stream,
148                          const android::snapshot::ITextureLoaderPtr& textureLoader,
149                          SaveableTexture::creator_t creator);
150     void postLoadAllImages(android::base::Stream* stream);
151 
152     bool nativeTextureDecompressionEnabled() const;
153     void setNativeTextureDecompressionEnabled(bool enabled);
154 
155 private:
156     static void addConfig(void* opaque, const EglOS::ConfigInfo* configInfo);
157 
158     int doChooseConfigs(const EglConfig& dummy,EGLConfig* configs,int config_size) const;
159     EglConfig* addSimplePixelFormat(int red_size, int green_size, int blue_size, int alpha_size, int sample_per_pixel);
160     void addReservedConfigs(void);
161     void initConfigurations(int renderableType);
162 
163     EGLNativeDisplayType    m_dpy = {};
164     EglOS::Display*         m_idpy = nullptr;
165     bool                    m_initialized = false;
166     bool                    m_configInitialized = false;
167     ConfigsList             m_configs;
168     ContextsHndlMap         m_contexts;
169     SurfacesHndlMap         m_surfaces;
170     GlobalNameSpace         m_globalNameSpace;
171     ObjectNameManager*      m_manager[MAX_GLES_VERSION];
172     mutable android::base::Lock    m_lock;
173     ImagesHndlMap           m_eglImages;
174     unsigned int            m_nextEglImageId = 0;
175     mutable std::shared_ptr<EglOS::Context> m_globalSharedContext;
176     ConfigSet               m_uniqueConfigs;
177     bool                    m_nativeTextureDecompressionEnabled = false;
178 };
179 
180 #endif
181