1 // Copyright (C) 2022 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma once 16 17 #include <EGL/egl.h> 18 #include <EGL/eglext.h> 19 #include <GLES/gl.h> 20 #include <GLES3/gl3.h> 21 22 #include <array> 23 #include <memory> 24 #include <optional> 25 #include <string> 26 #include <unordered_set> 27 28 #include "BufferGl.h" 29 #include "ColorBufferGl.h" 30 #include "Compositor.h" 31 #include "CompositorGl.h" 32 #include "ContextHelper.h" 33 #include "Display.h" 34 #include "DisplayGl.h" 35 #include "DisplaySurface.h" 36 #include "EmulatedEglConfig.h" 37 #include "EmulatedEglContext.h" 38 #include "EmulatedEglFenceSync.h" 39 #include "EmulatedEglImage.h" 40 #include "EmulatedEglWindowSurface.h" 41 #include "OpenGLESDispatch/EGLDispatch.h" 42 #include "OpenGLESDispatch/GLESv2Dispatch.h" 43 #include "ReadbackWorkerGl.h" 44 #include "TextureDraw.h" 45 #include "aemu/base/files/Stream.h" 46 #include "gfxstream/host/Features.h" 47 48 #define EGL_NO_CONFIG ((EGLConfig)0) 49 50 namespace gfxstream { 51 class FrameBuffer; 52 } // namespace gfxstream 53 54 namespace gfxstream { 55 namespace gl { 56 57 class EmulationGl { 58 public: 59 static std::unique_ptr<EmulationGl> create(uint32_t width, uint32_t height, 60 gfxstream::host::FeatureSet features, 61 bool allowWindowSurface, bool egl2egl); 62 63 ~EmulationGl(); 64 65 const EGLDispatch* getEglDispatch(); 66 const GLESv2Dispatch* getGles2Dispatch(); 67 68 GLESDispatchMaxVersion getGlesMaxDispatchVersion() const; 69 70 static const GLint* getGlesMaxContextAttribs(); 71 72 bool hasEglExtension(const std::string& ext) const; 73 void getEglVersion(EGLint* major, EGLint* minor) const; 74 75 void getGlesVersion(GLint* major, GLint* minor) const; getGlesVendor()76 const std::string& getGlesVendor() const { return mGlesVendor; } getGlesRenderer()77 const std::string& getGlesRenderer() const { return mGlesRenderer; } getGlesVersionString()78 const std::string& getGlesVersionString() const { return mGlesVersion; } getGlesExtensionsString()79 const std::string& getGlesExtensionsString() const { return mGlesExtensions; } isGlesVulkanInteropSupported()80 bool isGlesVulkanInteropSupported() const { return mGlesVulkanInteropSupported; } 81 82 bool isMesa() const; 83 84 bool isFastBlitSupported() const; 85 void disableFastBlitForTesting(); 86 87 bool isAsyncReadbackSupported() const; 88 89 std::unique_ptr<DisplaySurface> createWindowSurface(uint32_t width, 90 uint32_t height, 91 EGLNativeWindowType window); 92 getEmulationEglConfigs()93 const EmulatedEglConfigList& getEmulationEglConfigs() const { return *mEmulatedEglConfigs; } 94 getCompositor()95 CompositorGl* getCompositor() { return mCompositorGl.get(); } 96 getDisplay()97 DisplayGl* getDisplay() { return mDisplayGl.get(); } 98 getReadbackWorker()99 ReadbackWorkerGl* getReadbackWorker() { return mReadbackWorkerGl.get(); } 100 101 using GlesUuid = std::array<uint8_t, GL_UUID_SIZE_EXT>; getGlesDeviceUuid()102 const std::optional<GlesUuid> getGlesDeviceUuid() const { return mGlesDeviceUuid; } 103 104 std::unique_ptr<BufferGl> createBuffer(uint64_t size, HandleType handle); 105 106 std::unique_ptr<BufferGl> loadBuffer(android::base::Stream* stream); 107 108 bool isFormatSupported(GLenum format); 109 110 std::unique_ptr<ColorBufferGl> createColorBuffer(uint32_t width, uint32_t height, 111 GLenum internalFormat, 112 FrameworkFormat frameworkFormat, 113 HandleType handle); 114 115 std::unique_ptr<ColorBufferGl> loadColorBuffer(android::base::Stream* stream); 116 117 std::unique_ptr<EmulatedEglContext> createEmulatedEglContext( 118 uint32_t emulatedEglConfigIndex, 119 const EmulatedEglContext* shareContext, 120 GLESApi api, 121 HandleType handle); 122 123 std::unique_ptr<EmulatedEglContext> loadEmulatedEglContext( 124 android::base::Stream* stream); 125 126 std::unique_ptr<EmulatedEglFenceSync> createEmulatedEglFenceSync( 127 EGLenum type, 128 int destroyWhenSignaled); 129 130 std::unique_ptr<EmulatedEglImage> createEmulatedEglImage( 131 EmulatedEglContext* context, 132 EGLenum target, 133 EGLClientBuffer buffer); 134 135 std::unique_ptr<EmulatedEglWindowSurface> createEmulatedEglWindowSurface( 136 uint32_t emulatedConfigIndex, 137 uint32_t width, 138 uint32_t height, 139 HandleType handle); 140 141 std::unique_ptr<EmulatedEglWindowSurface> loadEmulatedEglWindowSurface( 142 android::base::Stream* stream, 143 const ColorBufferMap& colorBuffers, 144 const EmulatedEglContextMap& contexts); 145 146 std::unique_ptr<gfxstream::DisplaySurface> createFakeWindowSurface(); 147 148 private: 149 // TODO(b/233939967): Remove this after fully transitioning to EmulationGl. 150 friend class gfxstream::FrameBuffer; 151 152 EmulationGl() = default; 153 154 ContextHelper* getColorBufferContextHelper(); 155 156 gfxstream::host::FeatureSet mFeatures; 157 158 EGLDisplay mEglDisplay = EGL_NO_DISPLAY; 159 EGLint mEglVersionMajor = 0; 160 EGLint mEglVersionMinor = 0; 161 std::string mEglVendor; 162 std::unordered_set<std::string> mEglExtensions; 163 EGLConfig mEglConfig = EGL_NO_CONFIG; 164 165 // The "global" context that all other contexts are shared with. 166 EGLContext mEglContext = EGL_NO_CONTEXT; 167 168 // Used for ColorBuffer ops. 169 std::unique_ptr<gfxstream::DisplaySurface> mPbufferSurface; 170 171 // Used for Composition and Display ops. 172 std::unique_ptr<gfxstream::DisplaySurface> mWindowSurface; 173 174 GLint mGlesVersionMajor = 0; 175 GLint mGlesVersionMinor = 0; 176 GLESDispatchMaxVersion mGlesDispatchMaxVersion = GLES_DISPATCH_MAX_VERSION_2; 177 std::string mGlesVendor; 178 std::string mGlesRenderer; 179 std::string mGlesVersion; 180 std::string mGlesExtensions; 181 std::optional<GlesUuid> mGlesDeviceUuid; 182 bool mGlesVulkanInteropSupported = false; 183 184 std::unique_ptr<EmulatedEglConfigList> mEmulatedEglConfigs; 185 186 bool mFastBlitSupported = false; 187 188 std::unique_ptr<CompositorGl> mCompositorGl; 189 std::unique_ptr<DisplayGl> mDisplayGl; 190 std::unique_ptr<ReadbackWorkerGl> mReadbackWorkerGl; 191 192 std::unique_ptr<TextureDraw> mTextureDraw; 193 194 uint32_t mWidth = 0; 195 uint32_t mHeight = 0; 196 }; 197 198 } // namespace gl 199 } // namespace gfxstream 200