• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef RS_UNI_HWC_COMPUTE_UTIL_H
16 #define RS_UNI_HWC_COMPUTE_UTIL_H
17 
18 #include "pipeline/render_thread/rs_uni_render_util.h"
19 
20 #include <utility>
21 
22 namespace OHOS {
23 namespace Rosen {
24 class RSUniHwcComputeUtil {
25 struct HwcPropertyContext {
26     float alpha = 0.f;
27     bool isNodeRenderByDrawingCache = false;
28     bool isNodeRenderByChildNode = false;
29     Drawing::Matrix totalMatrix;
30     float absRotation = 0.f;
31 };
32 
33 public:
34     static void CalcSrcRectByBufferFlip(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo);
35     static Drawing::Rect CalcSrcRectByBufferRotation(const SurfaceBuffer& buffer,
36         const GraphicTransformType consumerTransformType, Drawing::Rect newSrcRect);
37     static void DealWithNodeGravity(RSSurfaceRenderNode& node, const Drawing::Matrix& totalMatrix);
38     static void DealWithNewSrcRect(Drawing::Rect& newSrcRect, Drawing::Rect newDstRect,
39         Drawing::Matrix inverseTotalMatrix, Drawing::Matrix inverseGravityMatrix,
40         const GraphicTransformType consumerTransformType);
41     static void DealWithNodeGravityOldVersion(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo);
42     static void DealWithScalingMode(RSSurfaceRenderNode& node, const Drawing::Matrix& totalMatrix);
43     static void LayerCrop(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo);
44     static void LayerRotate(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo);
45     static RectI SrcRectRotateTransform(const SurfaceBuffer& buffer,
46         const GraphicTransformType bufferRotateTransformType, const RectI& newSrcRect);
47     static void UpdateHwcNodeByScalingMode(RSSurfaceRenderNode& node, const Drawing::Matrix& totalMatrix,
48         const Drawing::Matrix& gravityMatrix, const Drawing::Matrix& scalingModeMatrix);
49     static void UpdateRealSrcRect(RSSurfaceRenderNode& node, const RectI& absRect);
50     static GraphicTransformType GetConsumerTransform(const RSSurfaceRenderNode& node,
51         const sptr<SurfaceBuffer>& buffer, const sptr<IConsumerSurface>& consumer);
52     static GraphicTransformType GetRotateTransformForRotationFixed(RSSurfaceRenderNode& node,
53         sptr<IConsumerSurface> consumer);
54     static void UpdateHwcNodeProperty(const std::shared_ptr<RSSurfaceRenderNode>& hwcNode);
55     static bool HasNonZRotationTransform(const Drawing::Matrix& matrix);
56     static GraphicTransformType GetLayerTransform(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo);
57     static std::optional<Drawing::Matrix> GetMatrix(const std::shared_ptr<RSRenderNode>& hwcNode);
58     static bool IntersectRect(Drawing::Rect& result, const Drawing::Rect& other);
59     static bool IsBlendNeedFilter(RSRenderNode& node);
60     static bool IsBlendNeedBackground(RSRenderNode& node);
61     static bool IsBlendNeedChildNode(RSRenderNode& node);
62     static bool IsDangerousBlendMode(int32_t blendMode, int32_t blendApplyType);
63     static bool IsForegroundColorStrategyValid(RSRenderNode& node);
64     static float GetFloatRotationDegreeFromMatrix(const Drawing::Matrix& matrix);
65 
66 private:
67     template <typename T>
IS_NULLPTR(const T & ptr)68     constexpr static bool IS_NULLPTR(const T& ptr)
69     {
70         if constexpr (std::is_pointer_v<T>) {
71             return ptr == nullptr;
72         } else {
73             return ptr.get() == nullptr;
74         }
75     }
76     template <typename... Args>
IS_ANY_NULLPTR(Args...args)77     constexpr static bool IS_ANY_NULLPTR(Args... args)
78     {
79         const auto results = std::make_tuple((IS_NULLPTR(args))...);
80         const bool anyNull = std::apply([](const auto&... result) {
81             return (result || ...);
82         }, results);
83         return anyNull;
84     }
85     template<typename... Callbacks>
TraverseParentNodeAndReduce(std::shared_ptr<RSSurfaceRenderNode> hwcNode,Callbacks &&...callbacks)86     static void TraverseParentNodeAndReduce(std::shared_ptr<RSSurfaceRenderNode> hwcNode, Callbacks&&... callbacks)
87     {
88         static_assert((std::is_invocable<Callbacks, std::shared_ptr<RSRenderNode>>::value && ...),
89                       "uninvocable callback");
90         if (!hwcNode) {
91             return;
92         }
93         auto parent = std::static_pointer_cast<RSRenderNode>(hwcNode);
94         while ((parent = parent->GetParent().lock())) {
95             (std::invoke(callbacks, parent), ...);
96             if (parent->GetType() == RSRenderNodeType::SCREEN_NODE) {
97                 break;
98             }
99         }
100     }
101     static inline void UpdateHwcNodeDrawingCache(const std::shared_ptr<RSRenderNode>& parent, HwcPropertyContext& ctx);
102     static inline void UpdateHwcNodeBlendNeedChildNode(const std::shared_ptr<RSRenderNode>& parent,
103         HwcPropertyContext& ctx);
104     static inline void UpdateHwcNodeAlpha(const std::shared_ptr<RSRenderNode>& parent, HwcPropertyContext& ctx);
105     static inline void UpdateHwcNodeTotalMatrix(const std::shared_ptr<RSRenderNode>& parent, HwcPropertyContext& ctx);
106     static void UpdateHwcNodeAbsRotation(const std::shared_ptr<RSRenderNode>& parent, HwcPropertyContext& ctx);
107     template<typename T>
108     static std::shared_ptr<RSRenderProperty<T>> GetPropertyFromModifier(
109         const RSRenderNode& node, ModifierNG::RSModifierType modifierType, ModifierNG::RSPropertyType propertyType);
110     static void CheckForceHardwareAndUpdateDstRect(RSSurfaceRenderNode& node);
111     static bool IsHwcEnabledByGravity(RSSurfaceRenderNode& node, const Gravity frameGravity);
112     static bool IsHwcEnabledByScalingMode(RSSurfaceRenderNode& node, const ScalingMode scalingMode);
113 };
114 } // namespace Rosen
115 } // namespace OHOS
116 #endif // RS_UNI_HWC_COMPUTE_UTIL_H