1 /* 2 * Copyright (c) 2021 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 "EGL/egl.h" 20 #include "EGL/eglext.h" 21 #include "GLES3/gl32.h" 22 #include "include/core/SkCanvas.h" 23 #include "include/core/SkColorSpace.h" 24 #include "include/core/SkImageInfo.h" 25 #include "include/core/SkSurface.h" 26 #include "include/gpu/GrBackendSurface.h" 27 #include "include/gpu/GrContext.h" 28 #include "include/gpu/gl/GrGLInterface.h" 29 #include "surface_type.h" 30 31 #define GLES_VERSION 2 32 namespace OHOS { 33 namespace Rosen { 34 class RenderContext { 35 public: 36 RenderContext(); 37 virtual ~RenderContext(); 38 void CreateCanvas(int width, int height); 39 SkCanvas* AcquireCanvas(int width, int height); 40 41 void SetColorSpace(SurfaceColorGamut colorSpace); 42 void InitializeEglContext(); 43 GetGrContext()44 GrContext* GetGrContext() const 45 { 46 return grContext_.get(); 47 } 48 49 bool SetUpGrContext(); 50 51 EGLSurface CreateEGLSurface(EGLNativeWindowType eglNativeWindow); 52 void DestroyEGLSurface(EGLSurface surface); 53 void MakeCurrent(EGLSurface surface) const; 54 void SwapBuffers(EGLSurface surface) const; 55 void RenderFrame(); 56 void DamageFrame(int32_t left, int32_t top, int32_t width, int32_t height); 57 GetEGLSurface()58 EGLSurface GetEGLSurface() const 59 { 60 return eglSurface_; 61 } 62 GetEGLDisplay()63 EGLDisplay GetEGLDisplay() const 64 { 65 return eglDisplay_; 66 } 67 IsEglContextReady()68 bool IsEglContextReady() 69 { 70 return eglContext_ != EGL_NO_DISPLAY; 71 } 72 73 private: 74 sk_sp<GrContext> grContext_; 75 sk_sp<SkSurface> skSurface_; 76 77 EGLNativeWindowType nativeWindow_; 78 79 EGLDisplay eglDisplay_; 80 EGLContext eglContext_; 81 EGLSurface eglSurface_; 82 EGLConfig config_; 83 SurfaceColorGamut colorSpace_ = SurfaceColorGamut::COLOR_GAMUT_SRGB; 84 }; 85 86 class RenderContextFactory { 87 public: ~RenderContextFactory()88 ~RenderContextFactory() 89 { 90 if (context_ != nullptr) { 91 delete context_; 92 } 93 context_ = nullptr; 94 } 95 GetInstance()96 static RenderContextFactory& GetInstance() 97 { 98 static RenderContextFactory rf; 99 return rf; 100 } 101 CreateEngine()102 RenderContext* CreateEngine() 103 { 104 if (context_ == nullptr) { 105 context_ = new RenderContext(); 106 } 107 108 return context_; 109 } 110 111 private: RenderContextFactory()112 RenderContextFactory() : context_(nullptr) {} 113 RenderContextFactory(const RenderContextFactory&) = delete; 114 RenderContextFactory& operator=(const RenderContextFactory&) = delete; 115 116 RenderContext* context_; 117 }; 118 } // namespace Rosen 119 } // namespace OHOS 120 #endif 121