• 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 "platform/drawing/rs_surface_frame.h"
26 #include "platform/ohos/rs_surface_ohos.h"
27 #include "rs_base_render_util.h"
28 
29 #ifdef RS_ENABLE_GL
30 #include "render_context/render_context.h"
31 #endif // RS_ENABLE_GL
32 
33 #ifdef RS_ENABLE_EGLIMAGE
34 #include "rs_egl_image_manager.h"
35 #endif // RS_ENABLE_EGLIMAGE
36 
37 namespace OHOS {
38 namespace Rosen {
39 // The RenderFrame can do auto flush
40 class RSRenderFrame {
41 public:
42     // we guarantee when constructing this object, all parameters are valid.
RSRenderFrame(const std::shared_ptr<RSSurfaceOhos> & target,std::unique_ptr<RSSurfaceFrame> && frame)43     RSRenderFrame(const std::shared_ptr<RSSurfaceOhos>& target, std::unique_ptr<RSSurfaceFrame>&& frame)
44         : targetSurface_(target), surfaceFrame_(std::move(frame))
45     {
46     }
~RSRenderFrame()47     ~RSRenderFrame() noexcept
48     {
49         Flush();
50     }
51 
52     // noncopyable
53     RSRenderFrame(const RSRenderFrame&) = delete;
54     void operator=(const RSRenderFrame&) = delete;
55 
Flush()56     void Flush() noexcept
57     {
58         if (targetSurface_ != nullptr && surfaceFrame_ != nullptr) {
59             targetSurface_->FlushFrame(surfaceFrame_);
60             targetSurface_ = nullptr;
61             surfaceFrame_ = nullptr;
62         }
63     }
64 
GetSurface()65     const std::shared_ptr<RSSurfaceOhos>& GetSurface() const
66     {
67         return targetSurface_;
68     }
69 
GetFrame()70     const std::unique_ptr<RSSurfaceFrame>& GetFrame() const
71     {
72         return surfaceFrame_;
73     }
74 
GetCanvas()75     std::unique_ptr<RSPaintFilterCanvas> GetCanvas()
76     {
77         return std::make_unique<RSPaintFilterCanvas>(surfaceFrame_->GetSurface().get());
78     }
79 
GetBufferAge()80     int32_t GetBufferAge()
81     {
82         return surfaceFrame_ != nullptr ? surfaceFrame_->GetBufferAge() : 0;
83     }
84 
SetDamageRegion(const std::vector<RectI> & rects)85     void SetDamageRegion(const std::vector<RectI> &rects)
86     {
87         if (surfaceFrame_ != nullptr) {
88             surfaceFrame_->SetDamageRegion(rects);
89         }
90     }
91 private:
92     std::shared_ptr<RSSurfaceOhos> targetSurface_;
93     std::unique_ptr<RSSurfaceFrame> surfaceFrame_;
94 };
95 
96 // function that will be called before drawing Buffer / Image.
97 using PreProcessFunc = std::function<void(RSPaintFilterCanvas&, BufferDrawParam&)>;
98 // function that will be called after drawing Buffer / Image.
99 using PostProcessFunc = std::function<void(RSPaintFilterCanvas&, BufferDrawParam&)>;
100 
101 // This render engine aims to do the client composition for all surfaces that hardware can't handle.
102 class RSBaseRenderEngine {
103 public:
104     RSBaseRenderEngine();
105     virtual ~RSBaseRenderEngine() noexcept;
106     static void Init();
107     RSBaseRenderEngine(const RSBaseRenderEngine&) = delete;
108     void operator=(const RSBaseRenderEngine&) = delete;
109 
110     // [PLANNING]: this is a work-around for the lack of colorgamut conversion and yuv support in GPU.
111     // We should remove this function in someday.
112     static bool NeedForceCPU(const std::vector<LayerInfoPtr>& layers);
113 
114     // There would only one user(thread) to renderFrame(request frame) at one time.
115     // for framebuffer surface
116     static std::unique_ptr<RSRenderFrame> RequestFrame(const sptr<Surface>& rsSurface,
117         const BufferRequestConfig& config, bool forceCPU = false, bool useAFBC = true);
118     // There would only one user(thread) to renderFrame(request frame) at one time.
119     static std::unique_ptr<RSRenderFrame> RequestFrame(const std::shared_ptr<RSSurfaceOhos>& rsSurface,
120         const BufferRequestConfig& config, bool forceCPU = false, bool useAFBC = true);
121 
122     static void SetUiTimeStamp(const std::unique_ptr<RSRenderFrame>& renderFrame, const uint64_t surfaceId);
123 
124     virtual void DrawSurfaceNodeWithParams(RSPaintFilterCanvas& canvas, RSSurfaceRenderNode& node,
125         BufferDrawParam& params, PreProcessFunc preProcess = nullptr, PostProcessFunc postProcess = nullptr) = 0;
126 
127     static void DrawDisplayNodeWithParams(RSPaintFilterCanvas& canvas, RSDisplayRenderNode& node,
128         BufferDrawParam& params);
129 
130     virtual void DrawLayers(RSPaintFilterCanvas& canvas, const std::vector<LayerInfoPtr>& layers, bool forceCPU = false,
131         float mirrorAdaptiveCoefficient = 1.0f) = 0;
132 
133     static void ShrinkCachesIfNeeded();
134     static void SetColorFilterMode(ColorFilterMode mode);
GetColorFilterMode()135     static ColorFilterMode GetColorFilterMode()
136     {
137         return colorFilterMode_;
138     }
SetHighContrast(bool enabled)139     static void SetHighContrast(bool enabled)
140     {
141         isHighContrastEnabled_  = enabled;
142     }
IsHighContrastEnabled()143     static bool IsHighContrastEnabled()
144     {
145         return isHighContrastEnabled_;
146     }
147 #ifdef RS_ENABLE_GL
GetRenderContext()148     static const std::shared_ptr<RenderContext>& GetRenderContext()
149     {
150         return renderContext_;
151     }
152 #endif // RS_ENABLE_GL
153 protected:
154     static void RegisterDeleteBufferListener(const sptr<Surface>& consumer);
155     static void DrawBuffer(RSPaintFilterCanvas& canvas, BufferDrawParam& params);
156     static void DrawImage(RSPaintFilterCanvas& canvas, BufferDrawParam& params);
157 
158     static inline ColorFilterMode colorFilterMode_ = ColorFilterMode::COLOR_FILTER_END;
159 
160 private:
161     static sk_sp<SkImage> CreateEglImageFromBuffer(const sptr<SurfaceBuffer>& buffer,
162         const sptr<SyncFence>& acquireFence);
163 
164     static inline std::atomic_bool isHighContrastEnabled_ = false;
165 
166 #ifdef RS_ENABLE_GL
167     static inline std::shared_ptr<RenderContext> renderContext_ = nullptr;
168 #endif // RS_ENABLE_GL
169 
170 #ifdef RS_ENABLE_EGLIMAGE
171     static inline std::shared_ptr<RSEglImageManager> eglImageManager_ = nullptr;
172 #endif // RS_ENABLE_EGLIMAGE
173 
174     // RSSurfaces for framebuffer surfaces.
175     static constexpr size_t MAX_RS_SURFACE_SIZE = 32; // used for rsSurfaces_.
176     using SurfaceId = uint64_t;
177     static inline std::unordered_map<SurfaceId, std::shared_ptr<RSSurfaceOhos>> rsSurfaces_;
178 };
179 } // namespace Rosen
180 } // namespace OHOS
181 #endif // RS_CORE_PIPELINE_BASE_RENDER_ENGINE_H
182