1 /*
2  * Copyright (c) 2021-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_PROCESSOR_H
17 #define RS_CORE_PIPELINE_PROCESSOR_H
18 
19 #include <memory>
20 
21 #include "params/rs_surface_render_params.h"
22 #include "utils/matrix.h"
23 
24 #include "drawable/rs_surface_render_node_drawable.h"
25 #include "rs_base_render_engine.h"
26 #include "pipeline/rs_display_render_node.h"
27 #include "pipeline/rs_surface_render_node.h"
28 
29 namespace OHOS {
30 namespace Rosen {
31 class RSRcdSurfaceRenderNode;
32 class RSDisplayRenderParams;
33 class RSSurfaceRenderParams;
34 class RSProcessor : public std::enable_shared_from_this<RSProcessor> {
35 public:
36     static inline constexpr RSProcessorType Type = RSProcessorType::RS_PROCESSOR;
GetType()37     virtual RSProcessorType GetType() const
38     {
39         return Type;
40     }
41 
42     RSProcessor() = default;
43     virtual ~RSProcessor() noexcept = default;
44 
45     RSProcessor(const RSProcessor&) = delete;
46     void operator=(const RSProcessor&) = delete;
47 
48     virtual bool Init(RSDisplayRenderNode& node, int32_t offsetX, int32_t offsetY, ScreenId mirroredId,
49         std::shared_ptr<RSBaseRenderEngine> renderEngine);
CreateLayer(RSSurfaceRenderNode & node,RSSurfaceRenderParams & params)50     virtual void CreateLayer(RSSurfaceRenderNode& node, RSSurfaceRenderParams& params) {}
CreateUIFirstLayer(DrawableV2::RSSurfaceRenderNodeDrawable & drawable,RSSurfaceRenderParams & params)51     virtual void CreateUIFirstLayer(DrawableV2::RSSurfaceRenderNodeDrawable& drawable,
52         RSSurfaceRenderParams& params) {}
53     virtual void ProcessSurface(RSSurfaceRenderNode& node) = 0;
54     virtual void ProcessDisplaySurface(RSDisplayRenderNode& node) = 0;
55     virtual void PostProcess() = 0;
56     virtual void ProcessRcdSurface(RSRcdSurfaceRenderNode& node) = 0;
57 
58     virtual bool InitForRenderThread(DrawableV2::RSDisplayRenderNodeDrawable& displayDrawable, ScreenId mirroredId,
59         std::shared_ptr<RSBaseRenderEngine> renderEngine);
CreateLayerForRenderThread(DrawableV2::RSSurfaceRenderNodeDrawable & surfaceDrawable)60     virtual void CreateLayerForRenderThread(DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable) {}
ProcessDisplaySurfaceForRenderThread(DrawableV2::RSDisplayRenderNodeDrawable & displayDrawable)61     virtual void ProcessDisplaySurfaceForRenderThread(DrawableV2::RSDisplayRenderNodeDrawable& displayDrawable) {}
ProcessSurfaceForRenderThread(DrawableV2::RSSurfaceRenderNodeDrawable & surfaceDrawable)62     virtual void ProcessSurfaceForRenderThread(DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable) {}
63 
64     void SetSecurityDisplay(bool isSecurityDisplay);
65     void SetDisplayHasSecSurface(bool displayHasSecSurface);
66     void MirrorScenePerf();
67 
GetScreenTransformMatrix()68     const Drawing::Matrix& GetScreenTransformMatrix() const
69     {
70         return screenTransformMatrix_;
71     }
72 
73     // type-safe reinterpret_cast
74     template<typename T>
IsInstanceOf()75     bool IsInstanceOf() const
76     {
77         constexpr auto targetType = static_cast<uint32_t>(T::Type);
78         return (static_cast<uint32_t>(GetType()) & targetType) == targetType;
79     }
80     template<typename T>
ReinterpretCast(std::shared_ptr<RSProcessor> processer)81     static std::shared_ptr<T> ReinterpretCast(std::shared_ptr<RSProcessor> processer)
82     {
83         return processer ? processer->ReinterpretCastTo<T>() : nullptr;
84     }
85     template<typename T>
ReinterpretCastTo()86     std::shared_ptr<T> ReinterpretCastTo()
87     {
88         return (IsInstanceOf<T>()) ? std::static_pointer_cast<T>(shared_from_this()) : nullptr;
89     }
90     template<typename T>
ReinterpretCastTo()91     std::shared_ptr<const T> ReinterpretCastTo() const
92     {
93         return (IsInstanceOf<T>()) ? std::static_pointer_cast<const T>(shared_from_this()) : nullptr;
94     }
95 
96 protected:
97     void CalculateMirrorAdaptiveCoefficient(float curWidth, float curHeight,
98         float mirroredWidth, float mirroredHeight);
99     void CalculateScreenTransformMatrix(const RSDisplayRenderNode& node);
100     void SetMirrorScreenSwap(const RSDisplayRenderNode& node);
101 
102     void MultiLayersPerf(size_t layerNum);
103     void RequestPerf(uint32_t layerLevel, bool onOffTag);
104 #ifdef FRAME_AWARE_TRACE
105     bool FrameAwareTraceBoost(size_t layerNum);
106 #endif
107 
108     ScreenInfo screenInfo_;
109     int32_t offsetX_ = 0;
110     int32_t offsetY_ = 0;
111     ScreenId mirroredId_ = INVALID_SCREEN_ID;
112     float mirrorAdaptiveCoefficient_ = 1.0f;
113     std::shared_ptr<RSBaseRenderEngine> renderEngine_;
114     Drawing::Matrix screenTransformMatrix_;
115     BufferRequestConfig renderFrameConfig_ {};
116     bool isSecurityDisplay_ = false;
117     bool displayHasSecSurface_ = false;
118     static bool needDisableMultiLayersPerf_;
119 };
120 } // namespace Rosen
121 } // namespace OHOS
122 #endif // RS_CORE_PIPELINE_PROCESSOR_H
123