1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 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 16 #ifndef RENDER_CONTEXT_H 17 #define RENDER_CONTEXT_H 18 19 #include <memory> 20 #include <mutex> 21 #include "common/rs_rect.h" 22 23 #ifdef ROSEN_IOS 24 #include "render_context_egl_defines.h" 25 #else 26 #include "EGL/egl.h" 27 #include "EGL/eglext.h" 28 #include "GLES3/gl32.h" 29 #endif 30 31 #include "draw/surface.h" 32 #include "image/gpu_context.h" 33 #include "memory_handler.h" 34 #include "surface_type.h" 35 #include "effect/color_space.h" 36 37 #define GLES_VERSION 2 38 namespace OHOS { 39 namespace Rosen { 40 class RenderContext { 41 public: 42 RenderContext(); 43 virtual ~RenderContext(); 44 void CreateCanvas(int width, int height); 45 std::shared_ptr<Drawing::Surface> AcquireSurface(int width, int height); 46 47 void InitializeEglContext(); GetDrGPUContext()48 Drawing::GPUContext* GetDrGPUContext() const 49 { 50 return drGPUContext_.get(); 51 } 52 GetSharedDrGPUContext()53 std::shared_ptr<Drawing::GPUContext> GetSharedDrGPUContext() const 54 { 55 return drGPUContext_; 56 } 57 GetSurface()58 std::shared_ptr<Drawing::Surface> GetSurface() const 59 { 60 return surface_; 61 } 62 virtual bool SetUpGpuContext(std::shared_ptr<Drawing::GPUContext> drawingContext = nullptr); 63 64 #ifdef RS_ENABLE_VK 65 void AbandonContext(); 66 #endif 67 68 EGLSurface CreateEGLSurface(EGLNativeWindowType eglNativeWindow); 69 void DestroyEGLSurface(EGLSurface surface); 70 void MakeCurrent(EGLSurface surface, EGLContext context = EGL_NO_CONTEXT); 71 void SwapBuffers(EGLSurface surface) const; 72 void RenderFrame(); 73 EGLint QueryEglBufferAge(); 74 void DamageFrame(int32_t left, int32_t top, int32_t width, int32_t height); 75 void DamageFrame(const std::vector<RectI> &rects); 76 void ClearRedundantResources(); 77 void CreatePbufferSurface(); 78 void ShareMakeCurrent(EGLContext shareContext); 79 void ShareMakeCurrentNoSurface(EGLContext shareContext); 80 void SetAndMakeCurrentShareContex(EGLContext shareContext); 81 void MakeSelfCurrent(); GetEGLSurface()82 EGLSurface GetEGLSurface() const 83 { 84 return eglSurface_; 85 } 86 GetEGLContext()87 EGLContext GetEGLContext() const 88 { 89 return eglContext_; 90 } 91 GetEGLDisplay()92 EGLDisplay GetEGLDisplay() const 93 { 94 return eglDisplay_; 95 } 96 SetColorSpace(GraphicColorGamut colorSpace)97 void SetColorSpace(GraphicColorGamut colorSpace) 98 { 99 colorSpace_ = colorSpace; 100 } 101 GetColorSpace()102 GraphicColorGamut GetColorSpace() const 103 { 104 return colorSpace_; 105 } 106 107 #ifndef ROSEN_CROSS_PLATFORM SetPixelFormat(int32_t pixelFormat)108 void SetPixelFormat(int32_t pixelFormat) 109 { 110 pixelFormat_ = pixelFormat; 111 } 112 GetPixelFormat()113 int32_t GetPixelFormat() const 114 { 115 return pixelFormat_; 116 } 117 #endif 118 IsEglContextReady()119 bool IsEglContextReady() const 120 { 121 return eglContext_ != EGL_NO_DISPLAY; 122 } 123 SetCacheDir(const std::string & filePath)124 void SetCacheDir(const std::string& filePath) 125 { 126 cacheDir_ = filePath; 127 } 128 SetUniRenderMode(bool isUni)129 void SetUniRenderMode(bool isUni) 130 { 131 isUniRenderMode_ = isUni; 132 } 133 #if defined(RS_ENABLE_VK) 134 bool CheckShaderCacheOverSoftLimit() const; 135 #endif 136 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK) 137 virtual std::string GetShaderCacheSize() const; 138 139 virtual std::string CleanAllShaderCache() const; 140 #endif 141 EGLContext CreateShareContext(); 142 #ifdef ROSEN_IOS ColorSpace()143 std::shared_ptr<Drawing::ColorSpace> ColorSpace() const { return color_space_; } 144 bool UpdateStorageSizeIfNecessary(); 145 bool ResourceMakeCurrent(); 146 static const EGLContext GetResourceContext(); 147 #endif 148 static std::shared_ptr<Drawing::ColorSpace> ConvertColorGamutToColorSpace(GraphicColorGamut colorGamut); 149 150 protected: 151 std::shared_ptr<Drawing::GPUContext> drGPUContext_ = nullptr; 152 std::shared_ptr<Drawing::Surface> surface_ = nullptr; 153 154 EGLNativeWindowType nativeWindow_; 155 156 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY; 157 EGLContext eglContext_ = EGL_NO_CONTEXT; 158 EGLSurface eglSurface_ = EGL_NO_SURFACE; 159 EGLSurface pbufferSurface_= EGL_NO_SURFACE; 160 #ifdef ROSEN_IOS 161 std::shared_ptr<Drawing::ColorSpace> color_space_ = nullptr; 162 void *layer_ = nullptr; 163 static EGLContext resourceContext; 164 static std::mutex resourceContextMutex; 165 166 uint32_t framebuffer_ = 0; 167 uint32_t colorbuffer_ = 0; 168 int32_t storage_width_ = 0; 169 int32_t storage_height_ = 0; 170 bool valid_ = false; 171 #endif 172 EGLConfig config_; 173 GraphicColorGamut colorSpace_ = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB; 174 int32_t pixelFormat_ = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_RGBA_8888; 175 176 bool isUniRenderMode_ = false; 177 const std::string UNIRENDER_CACHE_DIR = "/data/service/el0/render_service"; 178 std::string cacheDir_; 179 std::shared_ptr<MemoryHandler> mHandler_; 180 std::mutex shareContextMutex_; 181 182 #ifdef RS_ENABLE_GL 183 void InitGrContextOptions(Drawing::GPUContextOptions &options); 184 #endif 185 }; 186 187 class RenderContextFactory { 188 public: 189 static RenderContextFactory& GetInstance(); 190 ~RenderContextFactory()191 ~RenderContextFactory() 192 { 193 if (context_ != nullptr) { 194 delete context_; 195 } 196 context_ = nullptr; 197 } 198 CreateEngine()199 RenderContext* CreateEngine() 200 { 201 if (context_ == nullptr) { 202 context_ = new RenderContext(); 203 } 204 205 return context_; 206 } 207 CreateNewEngine()208 RenderContext* CreateNewEngine() 209 { 210 return context_; 211 } 212 213 private: RenderContextFactory()214 RenderContextFactory() : context_(nullptr) {} 215 RenderContextFactory(const RenderContextFactory&) = delete; 216 RenderContextFactory& operator=(const RenderContextFactory&) = delete; 217 218 RenderContext* context_; 219 }; 220 } // namespace Rosen 221 } // namespace OHOS 222 #endif 223