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 47 #define EGL_NO_CONFIG ((EGLConfig)0) 48 49 namespace gfxstream { 50 class FrameBuffer; 51 } // namespace gfxstream 52 53 namespace gfxstream { 54 namespace gl { 55 56 class EmulationGl { 57 public: 58 static std::unique_ptr<EmulationGl> create(uint32_t width, uint32_t height, 59 bool allowWindowSurface, bool egl2egl); 60 61 ~EmulationGl(); 62 63 const EGLDispatch* getEglDispatch(); 64 const GLESv2Dispatch* getGles2Dispatch(); 65 66 GLESDispatchMaxVersion getGlesMaxDispatchVersion() const; 67 68 static const GLint* getGlesMaxContextAttribs(); 69 70 bool hasEglExtension(const std::string& ext) const; 71 void getEglVersion(EGLint* major, EGLint* minor) const; 72 73 void getGlesVersion(GLint* major, GLint* minor) const; getGlesVendor()74 const std::string& getGlesVendor() const { return mGlesVendor; } getGlesRenderer()75 const std::string& getGlesRenderer() const { return mGlesRenderer; } getGlesVersionString()76 const std::string& getGlesVersionString() const { return mGlesVersion; } getGlesExtensionsString()77 const std::string& getGlesExtensionsString() const { return mGlesExtensions; } isGlesVulkanInteropSupported()78 bool isGlesVulkanInteropSupported() const { return mGlesVulkanInteropSupported; } 79 80 bool isMesa() const; 81 82 bool isFastBlitSupported() const; 83 void disableFastBlitForTesting(); 84 85 bool isAsyncReadbackSupported() const; 86 87 std::unique_ptr<DisplaySurface> createWindowSurface(uint32_t width, 88 uint32_t height, 89 EGLNativeWindowType window); 90 getEmulationEglConfigs()91 const EmulatedEglConfigList& getEmulationEglConfigs() const { return *mEmulatedEglConfigs; } 92 getCompositor()93 CompositorGl* getCompositor() { return mCompositorGl.get(); } 94 getDisplay()95 DisplayGl* getDisplay() { return mDisplayGl.get(); } 96 getReadbackWorker()97 ReadbackWorkerGl* getReadbackWorker() { return mReadbackWorkerGl.get(); } 98 99 using GlesUuid = std::array<uint8_t, GL_UUID_SIZE_EXT>; getGlesDeviceUuid()100 const std::optional<GlesUuid> getGlesDeviceUuid() const { return mGlesDeviceUuid; } 101 102 void setUseBoundSurfaceContextForDisplay(bool use); 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 std::unique_ptr<ColorBufferGl> createColorBuffer(uint32_t width, uint32_t height, 109 GLenum internalFormat, 110 FrameworkFormat frameworkFormat, 111 HandleType handle); 112 113 std::unique_ptr<ColorBufferGl> loadColorBuffer(android::base::Stream* stream); 114 115 std::unique_ptr<EmulatedEglContext> createEmulatedEglContext( 116 uint32_t emulatedEglConfigIndex, 117 const EmulatedEglContext* shareContext, 118 GLESApi api, 119 HandleType handle); 120 121 std::unique_ptr<EmulatedEglContext> loadEmulatedEglContext( 122 android::base::Stream* stream); 123 124 std::unique_ptr<EmulatedEglFenceSync> createEmulatedEglFenceSync( 125 EGLenum type, 126 int destroyWhenSignaled); 127 128 std::unique_ptr<EmulatedEglImage> createEmulatedEglImage( 129 EmulatedEglContext* context, 130 EGLenum target, 131 EGLClientBuffer buffer); 132 133 std::unique_ptr<EmulatedEglWindowSurface> createEmulatedEglWindowSurface( 134 uint32_t emulatedConfigIndex, 135 uint32_t width, 136 uint32_t height, 137 HandleType handle); 138 139 std::unique_ptr<EmulatedEglWindowSurface> loadEmulatedEglWindowSurface( 140 android::base::Stream* stream, 141 const ColorBufferMap& colorBuffers, 142 const EmulatedEglContextMap& contexts); 143 144 private: 145 // TODO(b/233939967): Remove this after fully transitioning to EmulationGl. 146 friend class gfxstream::FrameBuffer; 147 148 EmulationGl() = default; 149 150 ContextHelper* getColorBufferContextHelper(); 151 152 EGLDisplay mEglDisplay = EGL_NO_DISPLAY; 153 EGLint mEglVersionMajor = 0; 154 EGLint mEglVersionMinor = 0; 155 std::string mEglVendor; 156 std::unordered_set<std::string> mEglExtensions; 157 EGLConfig mEglConfig = EGL_NO_CONFIG; 158 159 // The "global" context that all other contexts are shared with. 160 EGLContext mEglContext = EGL_NO_CONTEXT; 161 162 // Used for ColorBuffer ops. 163 std::unique_ptr<gfxstream::DisplaySurface> mPbufferSurface; 164 165 // Used for Composition and Display ops. 166 std::unique_ptr<gfxstream::DisplaySurface> mWindowSurface; 167 std::unique_ptr<gfxstream::DisplaySurface> mFakeWindowSurface; 168 169 GLint mGlesVersionMajor = 0; 170 GLint mGlesVersionMinor = 0; 171 GLESDispatchMaxVersion mGlesDispatchMaxVersion = GLES_DISPATCH_MAX_VERSION_2; 172 std::string mGlesVendor; 173 std::string mGlesRenderer; 174 std::string mGlesVersion; 175 std::string mGlesExtensions; 176 std::optional<GlesUuid> mGlesDeviceUuid; 177 bool mGlesVulkanInteropSupported = false; 178 179 std::unique_ptr<EmulatedEglConfigList> mEmulatedEglConfigs; 180 181 bool mFastBlitSupported = false; 182 183 std::unique_ptr<CompositorGl> mCompositorGl; 184 std::unique_ptr<DisplayGl> mDisplayGl; 185 std::unique_ptr<ReadbackWorkerGl> mReadbackWorkerGl; 186 187 std::unique_ptr<TextureDraw> mTextureDraw; 188 }; 189 190 } // namespace gl 191 } // namespace gfxstream 192