• 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 RENDER_SERVICE_CORE_PIPELINE_RS_UNI_RENDER_UTIL_H
17 #define RENDER_SERVICE_CORE_PIPELINE_RS_UNI_RENDER_UTIL_H
18 
19 #include <list>
20 #include <mutex>
21 #include <set>
22 #include <unordered_map>
23 
24 #include "surface.h"
25 #include "surface_type.h"
26 #include "sync_fence.h"
27 
28 #include "common/rs_obj_abs_geometry.h"
29 #include "drawable/rs_surface_render_node_drawable.h"
30 #include "params/rs_display_render_params.h"
31 #include "pipeline/rs_base_render_engine.h"
32 #include "pipeline/rs_base_render_util.h"
33 #include "pipeline/rs_display_render_node.h"
34 #include "pipeline/rs_paint_filter_canvas.h"
35 #include "pipeline/rs_render_node_map.h"
36 #include "pipeline/rs_surface_render_node.h"
37 #include "transaction/rs_uiextension_data.h"
38 #include "utils/matrix.h"
39 
40 namespace OHOS {
41 namespace Rosen {
42 class RSDirtyRectsDfx;
43 class RSUniRenderUtil {
44 public:
45     static std::vector<RectI> MergeDirtyHistory(DrawableV2::RSDisplayRenderNodeDrawable& displayDrawable,
46         int32_t bufferAge, ScreenInfo& screenInfo, RSDirtyRectsDfx& rsDirtyRectsDfx, RSDisplayRenderParams& params);
47     static std::vector<RectI> MergeDirtyHistoryInVirtual(
48         DrawableV2::RSDisplayRenderNodeDrawable& displayDrawable, int32_t bufferAge, ScreenInfo& screenInfo);
49     // merge history dirty region of current display node and its child surfacenode(app windows)
50     // for mirror display, call this function twice will introduce additional dirtyhistory in dirtymanager
51     static void MergeDirtyHistoryForDrawable(DrawableV2::RSDisplayRenderNodeDrawable& drawable, int32_t bufferAge,
52         RSDisplayRenderParams& params, bool useAlignedDirtyRegion = false);
53     static void SetAllSurfaceDrawableGlobalDityRegion(
54         std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& allSurfaceDrawables,
55         const Occlusion::Region& globalDirtyRegion);
56     /* we want to set visible dirty region of each surfacenode into DamageRegionKHR interface, hence
57      * occlusion is calculated.
58      * make sure this function is called after merge dirty history
59      */
60     static Occlusion::Region MergeVisibleDirtyRegion(
61         std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& allSurfaceNodeDrawables,
62         std::vector<NodeId>& hasVisibleDirtyRegionSurfaceVec, bool useAlignedDirtyRegion = false);
63     static void MergeDirtyHistoryInVirtual(
64         DrawableV2::RSDisplayRenderNodeDrawable& displayDrawable, int32_t bufferAge, bool renderParallel = false);
65     static Occlusion::Region MergeVisibleDirtyRegionInVirtual(
66         std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& allSurfaceNodeDrawables);
67     static std::vector<RectI> ScreenIntersectDirtyRects(const Occlusion::Region &region, ScreenInfo& screenInfo);
68     static std::vector<RectI> GetFilpDirtyRects(const std::vector<RectI>& srcRects, const ScreenInfo& screenInfo);
69     static std::vector<RectI> FilpRects(const std::vector<RectI>& srcRects, const ScreenInfo& screenInfo);
70     static bool HandleSubThreadNode(RSSurfaceRenderNode& node, RSPaintFilterCanvas& canvas);
71     static bool HandleCaptureNode(RSRenderNode& node, RSPaintFilterCanvas& canvas);
72     // This is used for calculate matrix from buffer coordinate to window's relative coordinate
73     static Drawing::Matrix GetMatrixOfBufferToRelRect(const RSSurfaceRenderNode& node);
74     static void SrcRectScaleDown(BufferDrawParam& params, const sptr<SurfaceBuffer>& buffer,
75         const sptr<IConsumerSurface>& surface, RectF& localBounds);
76     static void SrcRectScaleFit(BufferDrawParam& params, const sptr<SurfaceBuffer>& buffer,
77         const sptr<IConsumerSurface>& surface, RectF& localBounds);
78     static BufferDrawParam CreateBufferDrawParam(const RSSurfaceRenderNode& node,
79         bool forceCPU, uint32_t threadIndex = UNI_RENDER_THREAD_INDEX, bool useRenderParams = false);
80     static BufferDrawParam CreateBufferDrawParam(const RSDisplayRenderNode& node, bool forceCPU);
81     static BufferDrawParam CreateBufferDrawParam(const RSSurfaceHandler& surfaceHandler, bool forceCPU);
82     static BufferDrawParam CreateBufferDrawParam(
83         const DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable, bool forceCPU, uint32_t threadIndex);
84     static BufferDrawParam CreateBufferDrawParamForRotationFixed(
85         const DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable, RSSurfaceRenderParams& renderParams);
86     static BufferDrawParam CreateLayerBufferDrawParam(const LayerInfoPtr& layer, bool forceCPU);
87     static void DealWithRotationAndGravityForRotationFixed(GraphicTransformType transform, Gravity gravity,
88         RectF& localBounds, BufferDrawParam& params);
89     static bool IsNeedClient(RSSurfaceRenderNode& node, const ComposeInfo& info);
90     static void DrawRectForDfx(RSPaintFilterCanvas& canvas, const RectI& rect, Drawing::Color color,
91         float alpha, const std::string& extraInfo = "");
92     static Occlusion::Region AlignedDirtyRegion(const Occlusion::Region& dirtyRegion, int32_t alignedBits = 32);
93     static int TransferToAntiClockwiseDegrees(int angle);
94     static int GetRotationFromMatrix(Drawing::Matrix matrix);
95     static float GetFloatRotationDegreeFromMatrix(Drawing::Matrix matrix);
96     static int GetRotationDegreeFromMatrix(Drawing::Matrix matrix);
97     static bool Is3DRotation(Drawing::Matrix matrix);
98 
99     static void AssignWindowNodes(const std::shared_ptr<RSDisplayRenderNode>& displayNode,
100         std::list<std::shared_ptr<RSSurfaceRenderNode>>& mainThreadNodes,
101         std::list<std::shared_ptr<RSSurfaceRenderNode>>& subThreadNodes);
102     static void ClearSurfaceIfNeed(const RSRenderNodeMap& map, const std::shared_ptr<RSDisplayRenderNode>& displayNode,
103         std::set<std::shared_ptr<RSBaseRenderNode>>& oldChildren, DeviceType deviceType = DeviceType::PHONE);
104     static void ClearCacheSurface(RSRenderNode& node, uint32_t threadIndex, bool isClearCompletedCacheSurface = true);
105     static void ClearNodeCacheSurface(std::shared_ptr<Drawing::Surface>&& cacheSurface,
106         std::shared_ptr<Drawing::Surface>&& cacheCompletedSurface,
107         uint32_t cacheSurfaceThreadIndex, uint32_t completedSurfaceThreadIndex);
108     static void CacheSubThreadNodes(std::list<std::shared_ptr<RSSurfaceRenderNode>>& oldSubThreadNodes,
109         std::list<std::shared_ptr<RSSurfaceRenderNode>>& subThreadNodes);
110     static bool IsNodeAssignSubThread(std::shared_ptr<RSSurfaceRenderNode> node, bool isDisplayRotation);
111 #ifdef RS_ENABLE_VK
112     static uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
113     static void SetVkImageInfo(std::shared_ptr<OHOS::Rosen::Drawing::VKTextureInfo> vkImageInfo,
114         const VkImageCreateInfo& imageInfo);
115     static Drawing::BackendTexture MakeBackendTexture(uint32_t width, uint32_t height,
116         VkFormat format = VK_FORMAT_R8G8B8A8_UNORM);
117 #endif
118     static void UpdateRealSrcRect(RSSurfaceRenderNode& node, const RectI& absRect);
119     static void DealWithNodeGravity(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo);
120     static void DealWithScalingMode(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo);
121     static void CheckForceHardwareAndUpdateDstRect(RSSurfaceRenderNode& node);
122     static void LayerRotate(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo);
123     static void LayerCrop(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo);
124     static void LayerScaleDown(RSSurfaceRenderNode& node);
125     static void LayerScaleFit(RSSurfaceRenderNode& node);
126     static GraphicTransformType GetLayerTransform(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo);
127     static void OptimizedFlushAndSubmit(std::shared_ptr<Drawing::Surface>& surface,
128         Drawing::GPUContext* const grContext, bool optFenceWait = true);
129     static void AccumulateMatrixAndAlpha(std::shared_ptr<RSSurfaceRenderNode>& hwcNode,
130         Drawing::Matrix& matrix, float& alpha);
131     static SecRectInfo GenerateSecRectInfoFromNode(RSRenderNode& node, RectI rect);
132     static SecSurfaceInfo GenerateSecSurfaceInfoFromNode(
133         NodeId uiExtensionId, NodeId hostId, SecRectInfo uiExtensionRectInfo);
134     static void UIExtensionFindAndTraverseAncestor(
135         const RSRenderNodeMap& nodeMap, UIExtensionCallbackData& callbackData, bool isUnobscured = false);
136     static void TraverseAndCollectUIExtensionInfo(std::shared_ptr<RSRenderNode> node,
137         Drawing::Matrix parentMatrix, NodeId hostId, UIExtensionCallbackData& callbackData, bool isUnobscured = false);
138     static void ProcessCacheImage(RSPaintFilterCanvas& canvas, Drawing::Image& cacheImageProcessed);
139     template<typename... Callbacks>
TraverseParentNodeAndReduce(std::shared_ptr<RSSurfaceRenderNode> hwcNode,Callbacks &&...callbacks)140     static void TraverseParentNodeAndReduce(std::shared_ptr<RSSurfaceRenderNode> hwcNode, Callbacks&&... callbacks)
141     {
142         static_assert((std::is_invocable<Callbacks, std::shared_ptr<RSRenderNode>>::value && ...),
143                     "uninvocable callback");
144         if (!hwcNode) {
145             return;
146         }
147         auto parent = std::static_pointer_cast<RSRenderNode>(hwcNode);
148         while (static_cast<bool>(parent = parent->GetParent().lock())) {
149             (std::invoke(callbacks, parent), ...);
150             if (parent->GetType() == RSRenderNodeType::DISPLAY_NODE) {
151                 break;
152             }
153         }
154     }
155     static std::optional<Drawing::Matrix> GetMatrix(std::shared_ptr<RSRenderNode> hwcNode);
156     // RTthread needs to draw one more frame when screen is turned off. For other threads, use extraframe default value.
157     static bool CheckRenderSkipIfScreenOff(bool extraFrame = false, std::optional<ScreenId> screenId = std::nullopt);
158 
159 private:
160     static void SetSrcRect(BufferDrawParam& params, const sptr<SurfaceBuffer>& buffer);
161     static RectI SrcRectRotateTransform(RSSurfaceRenderNode& node, GraphicTransformType transformType);
162     static void AssignMainThreadNode(std::list<std::shared_ptr<RSSurfaceRenderNode>>& mainThreadNodes,
163         const std::shared_ptr<RSSurfaceRenderNode>& node);
164     static void AssignSubThreadNode(std::list<std::shared_ptr<RSSurfaceRenderNode>>& subThreadNodes,
165         const std::shared_ptr<RSSurfaceRenderNode>& node);
166     static void SortSubThreadNodes(std::list<std::shared_ptr<RSSurfaceRenderNode>>& subThreadNodes);
167     static void HandleHardwareNode(const std::shared_ptr<RSSurfaceRenderNode>& node);
168     static void PostReleaseSurfaceTask(std::shared_ptr<Drawing::Surface>&& surface, uint32_t threadIndex);
169     static GraphicTransformType GetRotateTransformForRotationFixed(RSSurfaceRenderNode& node,
170         sptr<IConsumerSurface> consumer);
171     static inline int currentUIExtensionIndex_ = -1;
172     static inline const std::string RELEASE_SURFACE_TASK = "releaseSurface";
173 };
174 }
175 }
176 #endif // RENDER_SERVICE_CORE_PIPELINE_RS_UNI_RENDER_UTIL_H
177