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