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 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 std::unique_ptr<gfxstream::DisplaySurface> createFakeWindowSurface(); 145 146 private: 147 // TODO(b/233939967): Remove this after fully transitioning to EmulationGl. 148 friend class gfxstream::FrameBuffer; 149 150 EmulationGl() = default; 151 152 ContextHelper* getColorBufferContextHelper(); 153 154 gfxstream::host::FeatureSet mFeatures; 155 156 EGLDisplay mEglDisplay = EGL_NO_DISPLAY; 157 EGLint mEglVersionMajor = 0; 158 EGLint mEglVersionMinor = 0; 159 std::string mEglVendor; 160 std::unordered_set<std::string> mEglExtensions; 161 EGLConfig mEglConfig = EGL_NO_CONFIG; 162 163 // The "global" context that all other contexts are shared with. 164 EGLContext mEglContext = EGL_NO_CONTEXT; 165 166 // Used for ColorBuffer ops. 167 std::unique_ptr<gfxstream::DisplaySurface> mPbufferSurface; 168 169 // Used for Composition and Display ops. 170 std::unique_ptr<gfxstream::DisplaySurface> mWindowSurface; 171 172 GLint mGlesVersionMajor = 0; 173 GLint mGlesVersionMinor = 0; 174 GLESDispatchMaxVersion mGlesDispatchMaxVersion = GLES_DISPATCH_MAX_VERSION_2; 175 std::string mGlesVendor; 176 std::string mGlesRenderer; 177 std::string mGlesVersion; 178 std::string mGlesExtensions; 179 std::optional<GlesUuid> mGlesDeviceUuid; 180 bool mGlesVulkanInteropSupported = false; 181 182 std::unique_ptr<EmulatedEglConfigList> mEmulatedEglConfigs; 183 184 bool mFastBlitSupported = false; 185 186 std::unique_ptr<CompositorGl> mCompositorGl; 187 std::unique_ptr<DisplayGl> mDisplayGl; 188 std::unique_ptr<ReadbackWorkerGl> mReadbackWorkerGl; 189 190 std::unique_ptr<TextureDraw> mTextureDraw; 191 192 uint32_t mWidth = 0; 193 uint32_t mHeight = 0; 194 }; 195 196 } // namespace gl 197 } // namespace gfxstream 198