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 #ifndef USE_ROSEN_DRAWING 32 #include "include/core/SkCanvas.h" 33 #include "include/core/SkColorSpace.h" 34 #include "include/core/SkImageInfo.h" 35 #include "include/core/SkSurface.h" 36 #include "include/gpu/GrBackendSurface.h" 37 #if defined(NEW_SKIA) 38 #include "include/gpu/GrDirectContext.h" 39 #else 40 #include "include/gpu/GrContext.h" 41 #endif 42 #include "include/gpu/gl/GrGLInterface.h" 43 #else 44 #include "draw/surface.h" 45 #include "image/gpu_context.h" 46 #endif 47 #include "memory_handler.h" 48 #include "surface_type.h" 49 50 #define GLES_VERSION 2 51 namespace OHOS { 52 namespace Rosen { 53 class RenderContext { 54 public: 55 RenderContext(); 56 virtual ~RenderContext(); 57 void CreateCanvas(int width, int height); 58 #ifndef USE_ROSEN_DRAWING 59 sk_sp<SkSurface> AcquireSurface(int width, int height); 60 #else 61 std::shared_ptr<Drawing::Surface> AcquireSurface(int width, int height); 62 #endif 63 64 void InitializeEglContext(); 65 #ifndef USE_ROSEN_DRAWING 66 #if defined(NEW_SKIA) GetGrContext()67 GrDirectContext* GetGrContext() const 68 { 69 return grContext_.get(); 70 } 71 #else GetGrContext()72 GrContext* GetGrContext() const 73 { 74 return grContext_.get(); 75 } 76 #endif GetSurface()77 sk_sp<SkSurface> GetSurface() const 78 { 79 return skSurface_; 80 } 81 bool SetUpGrContext(sk_sp<GrDirectContext> skContext = nullptr); 82 #else GetDrGPUContext()83 Drawing::GPUContext* GetDrGPUContext() const 84 { 85 return drGPUContext_.get(); 86 } 87 GetSharedDrGPUContext()88 std::shared_ptr<Drawing::GPUContext> GetSharedDrGPUContext() const 89 { 90 return drGPUContext_; 91 } 92 GetSurface()93 std::shared_ptr<Drawing::Surface> GetSurface() const 94 { 95 return surface_; 96 } 97 bool SetUpGpuContext(std::shared_ptr<Drawing::GPUContext> drawingContext = nullptr); 98 #endif 99 100 #ifdef RS_ENABLE_VK 101 void AbandonContext(); 102 #endif 103 104 EGLSurface CreateEGLSurface(EGLNativeWindowType eglNativeWindow); 105 void DestroyEGLSurface(EGLSurface surface); 106 void MakeCurrent(EGLSurface surface, EGLContext context = EGL_NO_CONTEXT); 107 void SwapBuffers(EGLSurface surface) const; 108 void RenderFrame(); 109 EGLint QueryEglBufferAge(); 110 void DamageFrame(int32_t left, int32_t top, int32_t width, int32_t height); 111 void DamageFrame(const std::vector<RectI> &rects); 112 void ClearRedundantResources(); 113 void CreatePbufferSurface(); 114 void ShareMakeCurrent(EGLContext shareContext); 115 void ShareMakeCurrentNoSurface(EGLContext shareContext); 116 void SetAndMakeCurrentShareContex(EGLContext shareContext); 117 void MakeSelfCurrent(); GetEGLSurface()118 EGLSurface GetEGLSurface() const 119 { 120 return eglSurface_; 121 } 122 GetEGLContext()123 EGLContext GetEGLContext() const 124 { 125 return eglContext_; 126 } 127 GetEGLDisplay()128 EGLDisplay GetEGLDisplay() const 129 { 130 return eglDisplay_; 131 } 132 SetColorSpace(GraphicColorGamut colorSpace)133 void SetColorSpace(GraphicColorGamut colorSpace) 134 { 135 colorSpace_ = colorSpace; 136 } 137 GetColorSpace()138 GraphicColorGamut GetColorSpace() const 139 { 140 return colorSpace_; 141 } 142 143 #ifndef ROSEN_CROSS_PLATFORM SetPixelFormat(int32_t pixelFormat)144 void SetPixelFormat(int32_t pixelFormat) 145 { 146 pixelFormat_ = pixelFormat; 147 } 148 GetPixelFormat()149 int32_t GetPixelFormat() const 150 { 151 return pixelFormat_; 152 } 153 #endif 154 IsEglContextReady()155 bool IsEglContextReady() const 156 { 157 return eglContext_ != EGL_NO_DISPLAY; 158 } 159 SetCacheDir(const std::string & filePath)160 void SetCacheDir(const std::string& filePath) 161 { 162 cacheDir_ = filePath; 163 } 164 SetUniRenderMode(bool isUni)165 void SetUniRenderMode(bool isUni) 166 { 167 isUniRenderMode_ = isUni; 168 } 169 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK) 170 std::string GetShaderCacheSize() const; 171 172 std::string CleanAllShaderCache() const; 173 #endif 174 EGLContext CreateShareContext(); 175 #ifdef ROSEN_IOS ColorSpace()176 sk_sp<SkColorSpace> ColorSpace() const { return color_space_; } 177 bool UpdateStorageSizeIfNecessary(); 178 bool ResourceMakeCurrent(); 179 #endif 180 static sk_sp<SkColorSpace> ConvertColorGamutToSkColorSpace(GraphicColorGamut colorGamut); 181 182 private: 183 #ifndef USE_ROSEN_DRAWING 184 #if defined(NEW_SKIA) 185 sk_sp<GrDirectContext> grContext_; 186 #else 187 sk_sp<GrContext> grContext_; 188 #endif 189 sk_sp<SkSurface> skSurface_; 190 #else 191 std::shared_ptr<Drawing::GPUContext> drGPUContext_ = nullptr; 192 std::shared_ptr<Drawing::Surface> surface_ = nullptr; 193 #endif 194 195 EGLNativeWindowType nativeWindow_; 196 197 EGLDisplay eglDisplay_ = EGL_NO_DISPLAY; 198 EGLContext eglContext_ = EGL_NO_CONTEXT; 199 EGLSurface eglSurface_ = EGL_NO_SURFACE; 200 EGLSurface pbufferSurface_= EGL_NO_SURFACE; 201 #ifdef ROSEN_IOS 202 sk_sp<SkColorSpace> color_space_ = nullptr; 203 void *layer_ = nullptr; 204 EGLContext resource_context_ = EGL_NO_CONTEXT; 205 uint32_t framebuffer_ = 0; 206 uint32_t colorbuffer_ = 0; 207 int32_t storage_width_ = 0; 208 int32_t storage_height_ = 0; 209 bool valid_ = false; 210 #endif 211 EGLConfig config_; 212 GraphicColorGamut colorSpace_ = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB; 213 int32_t pixelFormat_ = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_RGBA_8888; 214 bool isUniRenderMode_ = false; 215 const std::string UNIRENDER_CACHE_DIR = "/data/service/el0/render_service"; 216 std::string cacheDir_; 217 std::shared_ptr<MemoryHandler> mHandler_; 218 std::mutex shareContextMutex_; 219 220 #ifndef USE_ROSEN_DRAWING 221 #ifdef RS_ENABLE_GL 222 void InitGrContextOptions(GrContextOptions &options); 223 #endif 224 #else 225 #ifdef RS_ENABLE_GL 226 void InitGrContextOptions(Drawing::GPUContextOptions &options); 227 #endif 228 #endif 229 }; 230 231 class RenderContextFactory { 232 public: 233 static RenderContextFactory& GetInstance(); 234 ~RenderContextFactory()235 ~RenderContextFactory() 236 { 237 if (context_ != nullptr) { 238 delete context_; 239 } 240 context_ = nullptr; 241 } 242 CreateEngine()243 RenderContext* CreateEngine() 244 { 245 if (context_ == nullptr) { 246 context_ = new RenderContext(); 247 } 248 249 return context_; 250 } 251 CreateNewEngine()252 RenderContext* CreateNewEngine() 253 { 254 return context_; 255 } 256 257 private: RenderContextFactory()258 RenderContextFactory() : context_(nullptr) {} 259 RenderContextFactory(const RenderContextFactory&) = delete; 260 RenderContextFactory& operator=(const RenderContextFactory&) = delete; 261 262 RenderContext* context_; 263 }; 264 } // namespace Rosen 265 } // namespace OHOS 266 #endif 267