• 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 "base/Lock.h"
23 #include "base/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     bool removeContext(EGLContext ctx);
127     bool removeContext(ContextPtr ctx);
getManager(GLESVersion ver)128     ObjectNameManager* getManager(GLESVersion ver) const { return m_manager[ver];}
129 
130     ~EglDisplay();
131     void initialize(int renderableType);
132     void terminate();
133     bool isInitialize();
134 
135     ImagePtr getImage(EGLImageKHR img,
136         SaveableTexture::restorer_t restorer) const;
137     EGLImageKHR addImageKHR(ImagePtr);
138     bool destroyImageKHR(EGLImageKHR img);
139     EglOS::Context* getGlobalSharedContext() const;
getGlobalNameSpace()140     GlobalNameSpace* getGlobalNameSpace() { return &m_globalNameSpace; }
141 
142     void onSaveAllImages(android::base::Stream* stream,
143                          const android::snapshot::ITextureSaverPtr& textureSaver,
144                          SaveableTexture::saver_t saver,
145                          SaveableTexture::restorer_t restorer);
146     void onLoadAllImages(android::base::Stream* stream,
147                          const android::snapshot::ITextureLoaderPtr& textureLoader,
148                          SaveableTexture::creator_t creator);
149     void postLoadAllImages(android::base::Stream* stream);
150 
151 private:
152     static void addConfig(void* opaque, const EglOS::ConfigInfo* configInfo);
153 
154     int doChooseConfigs(const EglConfig& dummy,EGLConfig* configs,int config_size) const;
155     EglConfig* addSimplePixelFormat(int red_size, int green_size, int blue_size, int alpha_size, int sample_per_pixel);
156     void addReservedConfigs(void);
157     void initConfigurations(int renderableType);
158 
159     EGLNativeDisplayType    m_dpy = {};
160     EglOS::Display*         m_idpy = nullptr;
161     bool                    m_initialized = false;
162     bool                    m_configInitialized = false;
163     ConfigsList             m_configs;
164     ContextsHndlMap         m_contexts;
165     SurfacesHndlMap         m_surfaces;
166     GlobalNameSpace         m_globalNameSpace;
167     ObjectNameManager*      m_manager[MAX_GLES_VERSION];
168     mutable android::base::Lock    m_lock;
169     ImagesHndlMap           m_eglImages;
170     unsigned int            m_nextEglImageId = 0;
171     mutable std::shared_ptr<EglOS::Context> m_globalSharedContext;
172     ConfigSet               m_uniqueConfigs;
173 };
174 
175 #endif
176