• 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 #include "rs_base_render_util.h"
26 
27 #ifdef NEW_RENDER_CONTEXT
28 #include "render_backend/rs_render_surface_frame.h"
29 #include "ohos/rs_render_surface_ohos.h"
30 #include "render_context_base.h"
31 #else
32 #include "platform/drawing/rs_surface_frame.h"
33 #include "platform/ohos/rs_surface_ohos.h"
34 #if (defined RS_ENABLE_GL) || (defined RS_ENABLE_VK)
35 #include "render_context/render_context.h"
36 #endif // RS_ENABLE_GL || RS_ENABLE_VK
37 #endif
38 #ifdef RS_ENABLE_EGLIMAGE
39 #include "rs_egl_image_manager.h"
40 #endif // RS_ENABLE_EGLIMAGE
41 
42 namespace OHOS {
43 namespace Rosen {
44 // The RenderFrame can do auto flush
45 class RSRenderFrame {
46 public:
47     // we guarantee when constructing this object, all parameters are valid.
48 #ifdef NEW_RENDER_CONTEXT
RSRenderFrame(const std::shared_ptr<RSRenderSurfaceOhos> & target)49     explicit RSRenderFrame(const std::shared_ptr<RSRenderSurfaceOhos>& target)
50         : targetSurface_(target)
51     {
52     }
53 #else
54     RSRenderFrame(const std::shared_ptr<RSSurfaceOhos>& target, std::unique_ptr<RSSurfaceFrame>&& frame)
55         : targetSurface_(target), surfaceFrame_(std::move(frame))
56     {
57     }
58 #endif
~RSRenderFrame()59     ~RSRenderFrame() noexcept
60     {
61         Flush();
62     }
63 
64     // noncopyable
65     RSRenderFrame(const RSRenderFrame&) = delete;
66     void operator=(const RSRenderFrame&) = delete;
67 #ifdef NEW_RENDER_CONTEXT
Flush()68     void Flush() noexcept
69     {
70         if (targetSurface_ != nullptr) {
71             targetSurface_->FlushFrame();
72             targetSurface_ = nullptr;
73         }
74     }
75 #else
Flush()76     void Flush() noexcept
77     {
78         if (targetSurface_ != nullptr && surfaceFrame_ != nullptr) {
79             targetSurface_->FlushFrame(surfaceFrame_);
80             targetSurface_ = nullptr;
81             surfaceFrame_ = nullptr;
82         }
83     }
84 #endif
85 #ifdef NEW_RENDER_CONTEXT
GetSurface()86     const std::shared_ptr<RSRenderSurfaceOhos>& GetSurface() const
87 #else
88     const std::shared_ptr<RSSurfaceOhos>& GetSurface() const
89 #endif
90     {
91         return targetSurface_;
92     }
93 #ifndef NEW_RENDER_CONTEXT
GetFrame()94     const std::unique_ptr<RSSurfaceFrame>& GetFrame() const
95     {
96         return surfaceFrame_;
97     }
98 #endif
GetCanvas()99     std::unique_ptr<RSPaintFilterCanvas> GetCanvas()
100     {
101 #ifdef NEW_RENDER_CONTEXT
102         return std::make_unique<RSPaintFilterCanvas>(targetSurface_->GetSurface().get());
103 #else
104         return std::make_unique<RSPaintFilterCanvas>(surfaceFrame_->GetSurface().get());
105 #endif
106     }
107 
GetBufferAge()108     int32_t GetBufferAge()
109     {
110 #ifdef NEW_RENDER_CONTEXT
111         return targetSurface_ != nullptr ? targetSurface_->GetBufferAge() : 0;
112 #else
113         return surfaceFrame_ != nullptr ? surfaceFrame_->GetBufferAge() : 0;
114 #endif
115     }
116 
SetDamageRegion(const std::vector<RectI> & rects)117     void SetDamageRegion(const std::vector<RectI> &rects)
118     {
119 #ifdef NEW_RENDER_CONTEXT
120         if (targetSurface_ != nullptr) {
121             targetSurface_ ->SetDamageRegion(rects);
122         }
123 #else
124         if (surfaceFrame_ != nullptr) {
125             surfaceFrame_->SetDamageRegion(rects);
126         }
127 #endif
128     }
129 private:
130 #ifdef NEW_RENDER_CONTEXT
131     std::shared_ptr<RSRenderSurfaceOhos> targetSurface_;
132 #else
133     std::shared_ptr<RSSurfaceOhos> targetSurface_;
134     std::unique_ptr<RSSurfaceFrame> surfaceFrame_;
135 #endif
136 };
137 
138 // function that will be called before drawing Buffer / Image.
139 using PreProcessFunc = std::function<void(RSPaintFilterCanvas&, BufferDrawParam&)>;
140 // function that will be called after drawing Buffer / Image.
141 using PostProcessFunc = std::function<void(RSPaintFilterCanvas&, BufferDrawParam&)>;
142 
143 // This render engine aims to do the client composition for all surfaces that hardware can't handle.
144 class RSBaseRenderEngine {
145 public:
146     RSBaseRenderEngine();
147     virtual ~RSBaseRenderEngine() noexcept;
148     void Init();
149     RSBaseRenderEngine(const RSBaseRenderEngine&) = delete;
150     void operator=(const RSBaseRenderEngine&) = delete;
151 
152     // [PLANNING]: this is a work-around for the lack of colorgamut conversion and yuv support in GPU.
153     // We should remove this function in someday.
154     static bool NeedForceCPU(const std::vector<LayerInfoPtr>& layers);
155 
156     // There would only one user(thread) to renderFrame(request frame) at one time.
157     // for framebuffer surface
158     std::unique_ptr<RSRenderFrame> RequestFrame(const sptr<Surface>& targetSurface,
159         const BufferRequestConfig& config, bool forceCPU = false, bool useAFBC = true);
160 
161     // There would only one user(thread) to renderFrame(request frame) at one time.
162 #ifdef NEW_RENDER_CONTEXT
163     std::unique_ptr<RSRenderFrame> RequestFrame(const std::shared_ptr<RSRenderSurfaceOhos>& rsSurface,
164         const BufferRequestConfig& config, bool forceCPU = false, bool useAFBC = true);
165     void SetUiTimeStamp(const std::unique_ptr<RSRenderFrame>& renderFrame,
166         std::shared_ptr<RSRenderSurfaceOhos> surfaceOhos);
167 #else
168     std::unique_ptr<RSRenderFrame> RequestFrame(const std::shared_ptr<RSSurfaceOhos>& rsSurface,
169         const BufferRequestConfig& config, bool forceCPU = false, bool useAFBC = true);
170     void SetUiTimeStamp(const std::unique_ptr<RSRenderFrame>& renderFrame,
171         std::shared_ptr<RSSurfaceOhos> surfaceOhos);
172 #endif
173 
174     virtual void DrawSurfaceNodeWithParams(RSPaintFilterCanvas& canvas, RSSurfaceRenderNode& node,
175         BufferDrawParam& params, PreProcessFunc preProcess = nullptr, PostProcessFunc postProcess = nullptr) = 0;
176 
177     void DrawDisplayNodeWithParams(RSPaintFilterCanvas& canvas, RSDisplayRenderNode& node,
178         BufferDrawParam& params);
179 
180     virtual void DrawLayers(RSPaintFilterCanvas& canvas, const std::vector<LayerInfoPtr>& layers, bool forceCPU = false,
181         float mirrorAdaptiveCoefficient = 1.0f) = 0;
182 
183     void DrawBuffer(RSPaintFilterCanvas& canvas, BufferDrawParam& params);
184 
185     void ShrinkCachesIfNeeded(bool isForUniRedraw = false);
186     static void SetColorFilterMode(ColorFilterMode mode);
GetColorFilterMode()187     static ColorFilterMode GetColorFilterMode()
188     {
189         return colorFilterMode_;
190     }
SetHighContrast(bool enabled)191     static void SetHighContrast(bool enabled)
192     {
193         isHighContrastEnabled_  = enabled;
194     }
IsHighContrastEnabled()195     static bool IsHighContrastEnabled()
196     {
197         return isHighContrastEnabled_;
198     }
199 #if defined(NEW_RENDER_CONTEXT)
GetRenderContext()200     const std::shared_ptr<RenderContextBase>& GetRenderContext() const
201     {
202         return renderContext_;
203     }
GetDrawingContext()204     const std::shared_ptr<DrawingContext>& GetDrawingContext() const
205     {
206         return drawingContext_;
207     }
208 #else
209 #if (defined RS_ENABLE_GL) || (defined RS_ENABLE_VK)
GetRenderContext()210     const std::shared_ptr<RenderContext>& GetRenderContext()
211     {
212         return renderContext_;
213     }
214 #endif // RS_ENABLE_GL || RS_ENABLE_VK
215 #endif
216 #ifdef RS_ENABLE_EGLIMAGE
GetEglImageManager()217     const std::shared_ptr<RSEglImageManager>& GetEglImageManager()
218     {
219         return eglImageManager_;
220     }
221 #endif // RS_ENABLE_EGLIMAGE
222 protected:
223     void RegisterDeleteBufferListener(const sptr<IConsumerSurface>& consumer, bool isForUniRedraw = false);
224     void RegisterDeleteBufferListener(RSSurfaceHandler& handler);
225     void DrawImage(RSPaintFilterCanvas& canvas, BufferDrawParam& params);
226 
227     static inline ColorFilterMode colorFilterMode_ = ColorFilterMode::COLOR_FILTER_END;
228 
229 private:
230 #ifndef USE_ROSEN_DRAWING
231     sk_sp<SkImage> CreateEglImageFromBuffer(RSPaintFilterCanvas& canvas,
232         const sptr<SurfaceBuffer>& buffer, const sptr<SyncFence>& acquireFence,
233         const uint32_t threadIndex = UNI_MAIN_THREAD_INDEX);
234 #else
235     std::shared_ptr<Drawing::Image> CreateEglImageFromBuffer(RSPaintFilterCanvas& canvas,
236         const sptr<SurfaceBuffer>& buffer, const sptr<SyncFence>& acquireFence,
237         const uint32_t threadIndex = UNI_MAIN_THREAD_INDEX);
238 #endif
239 
240     static inline std::atomic_bool isHighContrastEnabled_ = false;
241 #if defined(NEW_RENDER_CONTEXT)
242     std::shared_ptr<RenderContextBase> renderContext_ = nullptr;
243     std::shared_ptr<DrawingContext> drawingContext_ = nullptr;
244 #else
245 #if (defined RS_ENABLE_GL) || (defined RS_ENABLE_VK)
246     std::shared_ptr<RenderContext> renderContext_ = nullptr;
247 #endif // RS_ENABLE_GL || RS_ENABLE_VK
248 #endif
249 #ifdef RS_ENABLE_EGLIMAGE
250     std::shared_ptr<RSEglImageManager> eglImageManager_ = nullptr;
251 #endif // RS_ENABLE_EGLIMAGE
252     using SurfaceId = uint64_t;
253 };
254 } // namespace Rosen
255 } // namespace OHOS
256 #endif // RS_CORE_PIPELINE_BASE_RENDER_ENGINE_H
257