• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 RS_CORE_PIPELINE_BASE_RENDER_ENGINE_H
17 #define RS_CORE_PIPELINE_BASE_RENDER_ENGINE_H
18 
19 #include <memory>
20 
21 #include "hdi_layer_info.h"
22 #include "pipeline/rs_paint_filter_canvas.h"
23 #include "pipeline/rs_display_render_node.h"
24 #include "pipeline/rs_surface_render_node.h"
25 #ifdef RS_ENABLE_VK
26 #include "rs_vk_image_manager.h"
27 #endif
28 #include "include/gpu/GrDirectContext.h"
29 #include "rs_base_render_util.h"
30 
31 #ifdef NEW_RENDER_CONTEXT
32 #include "render_backend/rs_render_surface_frame.h"
33 #include "ohos/rs_render_surface_ohos.h"
34 #include "render_context_base.h"
35 #else
36 #include "platform/drawing/rs_surface_frame.h"
37 #include "platform/ohos/rs_surface_ohos.h"
38 #if (defined RS_ENABLE_GL) || (defined RS_ENABLE_VK)
39 #include "render_context/render_context.h"
40 #endif // RS_ENABLE_GL || RS_ENABLE_VK
41 #endif
42 #ifdef RS_ENABLE_EGLIMAGE
43 #include "rs_egl_image_manager.h"
44 #endif // RS_ENABLE_EGLIMAGE
45 #ifdef USE_VIDEO_PROCESSING_ENGINE
46 #include "colorspace_converter_display.h"
47 #endif
48 
49 namespace OHOS {
50 namespace Rosen {
51 namespace DrawableV2 {
52 class RSDisplayRenderNodeDrawable;
53 class RSSurfaceRenderNodeDrawable;
54 }
55 struct FrameContextConfig {
56 public:
FrameContextConfigFrameContextConfig57     FrameContextConfig(bool isProtected, bool independentContext)
58     {
59         this->isProtected = isProtected;
60         this->independentContext = independentContext;
61     }
62     bool isProtected = false;
63     bool independentContext = false;
64     bool isVirtual = false;
65     int32_t timeOut = 3000; // ms
66 };
67 // The RenderFrame can do auto flush
68 class RSRenderFrame {
69 public:
70     // we guarantee when constructing this object, all parameters are valid.
71 #ifdef NEW_RENDER_CONTEXT
RSRenderFrame(const std::shared_ptr<RSRenderSurfaceOhos> & target)72     explicit RSRenderFrame(const std::shared_ptr<RSRenderSurfaceOhos>& target)
73         : targetSurface_(target)
74     {
75     }
76 #else
77     RSRenderFrame(const std::shared_ptr<RSSurfaceOhos>& target, std::unique_ptr<RSSurfaceFrame>&& frame)
78         : targetSurface_(target), surfaceFrame_(std::move(frame))
79     {
80     }
81 #endif
~RSRenderFrame()82     ~RSRenderFrame() noexcept
83     {
84         Flush();
85     }
86 
87     // noncopyable
88     RSRenderFrame(const RSRenderFrame&) = delete;
89     void operator=(const RSRenderFrame&) = delete;
90 #ifdef NEW_RENDER_CONTEXT
Flush()91     void Flush() noexcept
92     {
93         if (targetSurface_ != nullptr) {
94             targetSurface_->FlushFrame();
95             targetSurface_ = nullptr;
96         }
97     }
98 #else
Flush()99     void Flush() noexcept
100     {
101         if (targetSurface_ != nullptr && surfaceFrame_ != nullptr) {
102             targetSurface_->FlushFrame(surfaceFrame_);
103             targetSurface_ = nullptr;
104             surfaceFrame_ = nullptr;
105         }
106     }
107 #endif
108 #ifdef NEW_RENDER_CONTEXT
GetSurface()109     const std::shared_ptr<RSRenderSurfaceOhos>& GetSurface() const
110 #else
111     const std::shared_ptr<RSSurfaceOhos>& GetSurface() const
112 #endif
113     {
114         return targetSurface_;
115     }
116 #ifndef NEW_RENDER_CONTEXT
GetFrame()117     const std::unique_ptr<RSSurfaceFrame>& GetFrame() const
118     {
119         return surfaceFrame_;
120     }
121 #endif
GetCanvas()122     std::unique_ptr<RSPaintFilterCanvas> GetCanvas()
123     {
124 #ifdef NEW_RENDER_CONTEXT
125         return std::make_unique<RSPaintFilterCanvas>(targetSurface_->GetSurface().get());
126 #else
127         return std::make_unique<RSPaintFilterCanvas>(surfaceFrame_->GetSurface().get());
128 #endif
129     }
130 
GetBufferAge()131     int32_t GetBufferAge()
132     {
133 #ifdef NEW_RENDER_CONTEXT
134         return targetSurface_ != nullptr ? targetSurface_->GetBufferAge() : 0;
135 #else
136         return surfaceFrame_ != nullptr ? surfaceFrame_->GetBufferAge() : 0;
137 #endif
138     }
139 
SetDamageRegion(const std::vector<RectI> & rects)140     void SetDamageRegion(const std::vector<RectI> &rects)
141     {
142 #ifdef NEW_RENDER_CONTEXT
143         if (targetSurface_ != nullptr) {
144             targetSurface_ ->SetDamageRegion(rects);
145         }
146 #else
147         if (surfaceFrame_ != nullptr) {
148             surfaceFrame_->SetDamageRegion(rects);
149         }
150 #endif
151     }
152 private:
153 #ifdef NEW_RENDER_CONTEXT
154     std::shared_ptr<RSRenderSurfaceOhos> targetSurface_;
155 #else
156     std::shared_ptr<RSSurfaceOhos> targetSurface_;
157     std::unique_ptr<RSSurfaceFrame> surfaceFrame_;
158 #endif
159 };
160 
161 // function that will be called before drawing Buffer / Image.
162 using PreProcessFunc = std::function<void(RSPaintFilterCanvas&, BufferDrawParam&)>;
163 // function that will be called after drawing Buffer / Image.
164 using PostProcessFunc = std::function<void(RSPaintFilterCanvas&, BufferDrawParam&)>;
165 
166 struct VideoInfo {
167     std::shared_ptr<Drawing::ColorSpace> drawingColorSpace_ = nullptr;
168 #ifdef USE_VIDEO_PROCESSING_ENGINE
169     GSError retGetColorSpaceInfo_ = GSERROR_OK;
170 	Media::VideoProcessingEngine::ColorSpaceConverterDisplayParameter parameter_ = {};
171 #endif
172 };
173 
174 // This render engine aims to do the client composition for all surfaces that hardware can't handle.
175 class RSBaseRenderEngine {
176 public:
177     RSBaseRenderEngine();
178     virtual ~RSBaseRenderEngine() noexcept;
179     void Init(bool independentContext = false);
180     void InitCapture(bool independentContext = false);
181     RSBaseRenderEngine(const RSBaseRenderEngine&) = delete;
182     void operator=(const RSBaseRenderEngine&) = delete;
183 
184     // [PLANNING]: this is a work-around for the lack of colorgamut conversion and yuv support in GPU.
185     // We should remove this function in someday.
186     static bool NeedForceCPU(const std::vector<LayerInfoPtr>& layers);
187 
188     // There would only one user(thread) to renderFrame(request frame) at one time.
189     // for framebuffer surface
190     std::unique_ptr<RSRenderFrame> RequestFrame(const sptr<Surface>& targetSurface,
191         const BufferRequestConfig& config, bool forceCPU = false, bool useAFBC = true,
192         const FrameContextConfig& frameContextConfig = {false, false});
193 
194     // There would only one user(thread) to renderFrame(request frame) at one time.
195 #ifdef NEW_RENDER_CONTEXT
196     std::unique_ptr<RSRenderFrame> RequestFrame(const std::shared_ptr<RSRenderSurfaceOhos>& rsSurface,
197         const BufferRequestConfig& config, bool forceCPU = false, bool useAFBC = true,
198         const FrameContextConfig& frameContextConfig = {false, false});
199     std::shared_ptr<RSRenderSurfaceOhos> MakeRSSurface(const sptr<Surface>& targetSurface, bool forceCPU);
200     void SetUiTimeStamp(const std::unique_ptr<RSRenderFrame>& renderFrame,
201         std::shared_ptr<RSRenderSurfaceOhos> surfaceOhos);
202 #else
203     std::unique_ptr<RSRenderFrame> RequestFrame(const std::shared_ptr<RSSurfaceOhos>& rsSurface,
204         const BufferRequestConfig& config, bool forceCPU = false, bool useAFBC = true,
205         const FrameContextConfig& frameContextConfig = {false, false});
206     std::shared_ptr<RSSurfaceOhos> MakeRSSurface(const sptr<Surface>& targetSurface, bool forceCPU);
207     void SetUiTimeStamp(const std::unique_ptr<RSRenderFrame>& renderFrame,
208         std::shared_ptr<RSSurfaceOhos> surfaceOhos);
209 #endif
210 
211     virtual void DrawSurfaceNodeWithParams(RSPaintFilterCanvas& canvas, RSSurfaceRenderNode& node,
212         BufferDrawParam& params, PreProcessFunc preProcess = nullptr, PostProcessFunc postProcess = nullptr) = 0;
213     virtual void DrawSurfaceNodeWithParams(RSPaintFilterCanvas& canvas,
214         DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable, BufferDrawParam& params,
215         PreProcessFunc preProcess = nullptr, PostProcessFunc postProcess = nullptr) {}
216 
217     virtual void DrawUIFirstCacheWithParams(RSPaintFilterCanvas& canvas, BufferDrawParam& params) = 0;
218 
219     void DrawDisplayNodeWithParams(RSPaintFilterCanvas& canvas, RSDisplayRenderNode& node,
220         BufferDrawParam& params);
221     void DrawDisplayNodeWithParams(RSPaintFilterCanvas& canvas, RSSurfaceHandler& surfaceHandler,
222         BufferDrawParam& drawParam);
223     void RegisterDeleteBufferListener(const sptr<IConsumerSurface>& consumer, bool isForUniRedraw = false);
224     void RegisterDeleteBufferListener(RSSurfaceHandler& handler);
225     std::shared_ptr<Drawing::Image> CreateImageFromBuffer(RSPaintFilterCanvas& canvas,
226         BufferDrawParam& params, VideoInfo& videoInfo);
227 #ifdef USE_VIDEO_PROCESSING_ENGINE
228     virtual void DrawLayers(RSPaintFilterCanvas& canvas, const std::vector<LayerInfoPtr>& layers, bool forceCPU = false,
229         const ScreenInfo& screenInfo = {}, GraphicColorGamut colorGamut = GRAPHIC_COLOR_GAMUT_SRGB) = 0;
230 #else
231     virtual void DrawLayers(RSPaintFilterCanvas& canvas, const std::vector<LayerInfoPtr>& layers, bool forceCPU = false,
232         const ScreenInfo& screenInfo = {}) = 0;
233 #endif
234 
235     static void DrawBuffer(RSPaintFilterCanvas& canvas, BufferDrawParam& params);
236 
237     void ShrinkCachesIfNeeded(bool isForUniRedraw = false);
238     void ClearCacheSet(const std::set<int32_t> unmappedCache);
239     static void SetColorFilterMode(ColorFilterMode mode);
240     static ColorFilterMode GetColorFilterMode();
241     static void SetHighContrast(bool enabled);
242     static bool IsHighContrastEnabled();
243 
244 #if defined(NEW_RENDER_CONTEXT)
GetRenderContext()245     const std::shared_ptr<RenderContextBase>& GetRenderContext() const
246     {
247         return renderContext_;
248     }
GetDrawingContext()249     const std::shared_ptr<DrawingContext>& GetDrawingContext() const
250     {
251         return drawingContext_;
252     }
253 #else
254 #if (defined RS_ENABLE_GL) || (defined RS_ENABLE_VK)
GetRenderContext()255     const std::shared_ptr<RenderContext>& GetRenderContext()
256     {
257         return renderContext_;
258     }
GetCaptureRenderContext()259     const std::shared_ptr<RenderContext>& GetCaptureRenderContext()
260     {
261         return captureRenderContext_;
262     }
263 #endif // RS_ENABLE_GL || RS_ENABLE_VK
264 #endif
265     void ResetCurrentContext();
266 
267 #ifdef RS_ENABLE_EGLIMAGE
GetEglImageManager()268     const std::shared_ptr<RSEglImageManager>& GetEglImageManager()
269     {
270         return eglImageManager_;
271     }
272 #endif // RS_ENABLE_EGLIMAGE
273 #ifdef USE_VIDEO_PROCESSING_ENGINE
274     void ColorSpaceConvertor(std::shared_ptr<Drawing::ShaderEffect> &inputShader, BufferDrawParam& params,
275         Media::VideoProcessingEngine::ColorSpaceConverterDisplayParameter& parameter);
276 #endif
277     static std::shared_ptr<Drawing::ColorSpace> ConvertColorGamutToDrawingColorSpace(GraphicColorGamut colorGamut);
278 #ifdef RS_ENABLE_VK
GetVkImageManager()279     const std::shared_ptr<RSVkImageManager>& GetVkImageManager() const
280     {
281         return vkImageManager_;
282     }
GetSkContext()283     const std::shared_ptr<Drawing::GPUContext> GetSkContext() const
284     {
285         return skContext_;
286     }
GetCaptureSkContext()287     const std::shared_ptr<Drawing::GPUContext> GetCaptureSkContext() const
288     {
289         return captureSkContext_;
290     }
291 #endif
292 protected:
293     void DrawImage(RSPaintFilterCanvas& canvas, BufferDrawParam& params);
294     static bool CheckIsHdrSurfaceBuffer(const sptr<SurfaceBuffer> surfaceNode);
295 
296     static inline std::mutex colorFilterMutex_;
297     static inline ColorFilterMode colorFilterMode_ = ColorFilterMode::COLOR_FILTER_END;
298     static inline std::atomic_bool isHighContrastEnabled_ = false;
299 
300 private:
301     std::shared_ptr<Drawing::Image> CreateEglImageFromBuffer(RSPaintFilterCanvas& canvas,
302         const sptr<SurfaceBuffer>& buffer, const sptr<SyncFence>& acquireFence,
303         const uint32_t threadIndex = UNI_MAIN_THREAD_INDEX,
304         const std::shared_ptr<Drawing::ColorSpace>& drawingColorSpace = nullptr);
305 
306     static void DrawImageRect(RSPaintFilterCanvas& canvas, std::shared_ptr<Drawing::Image> image,
307         BufferDrawParam& params, Drawing::SamplingOptions& samplingOptions);
308 
309 #if defined(NEW_RENDER_CONTEXT)
310     std::shared_ptr<RenderContextBase> renderContext_ = nullptr;
311     std::shared_ptr<DrawingContext> drawingContext_ = nullptr;
312 #else
313 #if (defined RS_ENABLE_GL) || (defined RS_ENABLE_VK)
314     std::shared_ptr<RenderContext> renderContext_ = nullptr;
315     std::shared_ptr<RenderContext> captureRenderContext_ = nullptr;
316 #endif // RS_ENABLE_GL || RS_ENABLE_VK
317 #endif
318 #ifdef RS_ENABLE_EGLIMAGE
319     std::shared_ptr<RSEglImageManager> eglImageManager_ = nullptr;
320 #endif // RS_ENABLE_EGLIMAGE
321 #ifdef RS_ENABLE_VK
322     std::shared_ptr<Drawing::GPUContext> skContext_ = nullptr;
323     std::shared_ptr<Drawing::GPUContext> captureSkContext_ = nullptr;
324     std::shared_ptr<RSVkImageManager> vkImageManager_ = nullptr;
325 #endif
326     using SurfaceId = uint64_t;
327 #ifdef USE_VIDEO_PROCESSING_ENGINE
328     static bool SetColorSpaceConverterDisplayParameter(
329         const BufferDrawParam& params, Media::VideoProcessingEngine::ColorSpaceConverterDisplayParameter& parameter);
330     static std::shared_ptr<Drawing::ColorSpace> GetCanvasColorSpace(const RSPaintFilterCanvas& canvas);
331     static bool ConvertDrawingColorSpaceToSpaceInfo(const std::shared_ptr<Drawing::ColorSpace>& colorSpace,
332         HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceInfo& colorSpaceInfo);
333     std::shared_ptr<Media::VideoProcessingEngine::ColorSpaceConverterDisplay> colorSpaceConverterDisplay_ = nullptr;
334 #endif
335 };
336 } // namespace Rosen
337 } // namespace OHOS
338 #endif // RS_CORE_PIPELINE_BASE_RENDER_ENGINE_H
339