• 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 #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 #endif
143     static sk_sp<SkColorSpace> ConvertColorGamutToSkColorSpace(GraphicColorGamut colorGamut);
144 
145 protected:
146     std::shared_ptr<Drawing::GPUContext> drGPUContext_ = nullptr;
147     std::shared_ptr<Drawing::Surface> surface_ = nullptr;
148 
149     EGLNativeWindowType nativeWindow_;
150 
151     EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
152     EGLContext eglContext_ = EGL_NO_CONTEXT;
153     EGLSurface eglSurface_ = EGL_NO_SURFACE;
154     EGLSurface pbufferSurface_= EGL_NO_SURFACE;
155 #ifdef ROSEN_IOS
156     sk_sp<SkColorSpace> color_space_ = nullptr;
157     void *layer_ = nullptr;
158     EGLContext resource_context_ = EGL_NO_CONTEXT;
159     uint32_t framebuffer_ = 0;
160     uint32_t colorbuffer_ = 0;
161     int32_t storage_width_ = 0;
162     int32_t storage_height_ = 0;
163     bool valid_ = false;
164 #endif
165     EGLConfig config_;
166     GraphicColorGamut colorSpace_ = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
167     int32_t pixelFormat_ = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_RGBA_8888;
168 
169     bool isUniRenderMode_ = false;
170     const std::string UNIRENDER_CACHE_DIR = "/data/service/el0/render_service";
171     std::string cacheDir_;
172     std::shared_ptr<MemoryHandler> mHandler_;
173     std::mutex shareContextMutex_;
174 
175 #ifdef RS_ENABLE_GL
176     void InitGrContextOptions(Drawing::GPUContextOptions &options);
177 #endif
178 };
179 
180 class RenderContextFactory {
181 public:
182     static RenderContextFactory& GetInstance();
183 
~RenderContextFactory()184     ~RenderContextFactory()
185     {
186         if (context_ != nullptr) {
187             delete context_;
188         }
189         context_ = nullptr;
190     }
191 
CreateEngine()192     RenderContext* CreateEngine()
193     {
194         if (context_ == nullptr) {
195             context_ = new RenderContext();
196         }
197 
198         return context_;
199     }
200 
CreateNewEngine()201     RenderContext* CreateNewEngine()
202     {
203         return context_;
204     }
205 
206 private:
RenderContextFactory()207     RenderContextFactory() : context_(nullptr) {}
208     RenderContextFactory(const RenderContextFactory&) = delete;
209     RenderContextFactory& operator=(const RenderContextFactory&) = delete;
210 
211     RenderContext* context_;
212 };
213 } // namespace Rosen
214 } // namespace OHOS
215 #endif
216