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