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 native display handle for this EglDisplay. getNativeDisplay()50 EGLNativeDisplayType getNativeDisplay() 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 63 // Write up to |config_size| EGLConfig values into the |configs| array. 64 // Return the number if values written. 65 int getConfigs(EGLConfig* configs,int config_size) const; 66 67 // Select all display configurations that match at least the values 68 // in |dummy|. If |configs| is NULL, this returns the number of all 69 // matching configs. Otherwise, this writes into |configs| up to 70 // |config_size| matching EGLConfig values, and returns their number. 71 int chooseConfigs(const EglConfig& dummy, 72 EGLConfig* configs, 73 int config_size) const; 74 75 // Return the EglConfig value that matches a given EGLConfig |conf|. 76 EglConfig* getConfig(EGLConfig conf) const; 77 78 // Return the EglConfig value that matches a given EGLConfig with 79 // EGL_CONFIG_ID value |id|. 80 EglConfig* getConfig(EGLint id) const; 81 82 EglConfig* getDefaultConfig() const; 83 84 EGLSurface addSurface(SurfacePtr s ); 85 SurfacePtr getSurface(EGLSurface surface) const; 86 bool removeSurface(EGLSurface s); 87 EGLContext addContext(ContextPtr ctx ); 88 ContextPtr getContext(EGLContext ctx) const; 89 bool removeContext(EGLContext ctx); 90 bool removeContext(ContextPtr ctx); getManager(GLESVersion ver)91 ObjectNameManager* getManager(GLESVersion ver) const { return m_manager[ver];} 92 93 ~EglDisplay(); 94 void initialize(int renderableType); 95 void terminate(); 96 bool isInitialize(); 97 98 ImagePtr getImage(EGLImageKHR img, 99 SaveableTexture::restorer_t restorer) const; 100 EGLImageKHR addImageKHR(ImagePtr); 101 bool destroyImageKHR(EGLImageKHR img); 102 EglOS::Context* getGlobalSharedContext() const; getGlobalNameSpace()103 GlobalNameSpace* getGlobalNameSpace() { return &m_globalNameSpace; } 104 105 void onSaveAllImages(android::base::Stream* stream, 106 const android::snapshot::ITextureSaverPtr& textureSaver, 107 SaveableTexture::saver_t saver, 108 SaveableTexture::restorer_t restorer); 109 void onLoadAllImages(android::base::Stream* stream, 110 const android::snapshot::ITextureLoaderPtr& textureLoader, 111 SaveableTexture::creator_t creator); 112 void postLoadAllImages(android::base::Stream* stream); 113 114 private: 115 static void addConfig(void* opaque, const EglOS::ConfigInfo* configInfo); 116 117 int doChooseConfigs(const EglConfig& dummy,EGLConfig* configs,int config_size) const; 118 EglConfig* addSimplePixelFormat(int red_size, int green_size, int blue_size, int alpha_size, int sample_per_pixel); 119 void addReservedConfigs(void); 120 void initConfigurations(int renderableType); 121 122 EGLNativeDisplayType m_dpy = {}; 123 EglOS::Display* m_idpy = nullptr; 124 bool m_initialized = false; 125 bool m_configInitialized = false; 126 ConfigsList m_configs; 127 ContextsHndlMap m_contexts; 128 SurfacesHndlMap m_surfaces; 129 GlobalNameSpace m_globalNameSpace; 130 ObjectNameManager* m_manager[MAX_GLES_VERSION]; 131 mutable android::base::Lock m_lock; 132 ImagesHndlMap m_eglImages; 133 unsigned int m_nextEglImageId = 0; 134 mutable std::shared_ptr<EglOS::Context> m_globalSharedContext; 135 ConfigSet m_uniqueConfigs; 136 }; 137 138 #endif 139