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_display_render_node.h" 32 #include "pipeline/rs_paint_filter_canvas.h" 33 #include "pipeline/rs_render_node_map.h" 34 #include "pipeline/rs_surface_render_node.h" 35 #include "rs_base_render_engine.h" 36 #include "rs_base_render_util.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 void MergeDirtyRectAfterMergeHistory( 46 std::shared_ptr<RSDirtyRegionManager> dirtyManager, Occlusion::Region& dirtyRegion); 47 static std::vector<RectI> MergeDirtyHistory(DrawableV2::RSDisplayRenderNodeDrawable& displayDrawable, 48 int32_t bufferAge, ScreenInfo& screenInfo, RSDirtyRectsDfx& rsDirtyRectsDfx, RSDisplayRenderParams& params); 49 static std::vector<RectI> MergeDirtyHistoryInVirtual( 50 DrawableV2::RSDisplayRenderNodeDrawable& displayDrawable, int32_t bufferAge, ScreenInfo& screenInfo); 51 // merge history dirty region of current display node and its child surfacenode(app windows) 52 // for mirror display, call this function twice will introduce additional dirtyhistory in dirtymanager 53 static void MergeDirtyHistoryForDrawable(DrawableV2::RSDisplayRenderNodeDrawable& drawable, int32_t bufferAge, 54 RSDisplayRenderParams& params, bool useAlignedDirtyRegion = false); 55 static void SetAllSurfaceDrawableGlobalDirtyRegion( 56 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& allSurfaceDrawables, 57 const Occlusion::Region& globalDirtyRegion); 58 static void SetDrawRegionForQuickReject( 59 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& allSurfaceDrawables, 60 const Occlusion::Region mergedDirtyRects); 61 /* we want to set visible dirty region of each surfacenode into DamageRegionKHR interface, hence 62 * occlusion is calculated. 63 * make sure this function is called after merge dirty history 64 */ 65 static Occlusion::Region MergeVisibleDirtyRegion( 66 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& allSurfaceNodeDrawables, 67 std::vector<NodeId>& hasVisibleDirtyRegionSurfaceVec, bool useAlignedDirtyRegion = false); 68 static Occlusion::Region MergeVisibleAdvancedDirtyRegion( 69 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& allSurfaceNodeDrawables, 70 std::vector<NodeId>& hasVisibleDirtyRegionSurfaceVec); 71 static Occlusion::Region MergeDirtyRects(Occlusion::Region dirtyRegion); 72 static void MergeDirtyHistoryInVirtual( 73 DrawableV2::RSDisplayRenderNodeDrawable& displayDrawable, int32_t bufferAge, bool renderParallel = false); 74 static Occlusion::Region MergeVisibleDirtyRegionInVirtual( 75 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& allSurfaceNodeDrawables); 76 static std::vector<RectI> GetCurrentFrameVisibleDirty(DrawableV2::RSDisplayRenderNodeDrawable& displayDrawable, 77 ScreenInfo& screenInfo, RSDisplayRenderParams& params); 78 static std::vector<RectI> ScreenIntersectDirtyRects(const Occlusion::Region ®ion, ScreenInfo& screenInfo); 79 static std::vector<RectI> GetFilpDirtyRects(const std::vector<RectI>& srcRects, const ScreenInfo& screenInfo); 80 static std::vector<RectI> FilpRects(const std::vector<RectI>& srcRects, const ScreenInfo& screenInfo); 81 static GraphicIRect IntersectRect(const GraphicIRect& first, const GraphicIRect& second); 82 // This is used for calculate matrix from buffer coordinate to window's relative coordinate 83 static Drawing::Matrix GetMatrixOfBufferToRelRect(const RSSurfaceRenderNode& node); 84 static void SrcRectScaleDown(BufferDrawParam& params, const sptr<SurfaceBuffer>& buffer, 85 const sptr<IConsumerSurface>& surface, RectF& localBounds); 86 static void SrcRectScaleFit(BufferDrawParam& params, const sptr<SurfaceBuffer>& buffer, 87 const sptr<IConsumerSurface>& surface, RectF& localBounds); 88 static BufferDrawParam CreateBufferDrawParam(const RSSurfaceRenderNode& node, 89 bool forceCPU, uint32_t threadIndex = UNI_RENDER_THREAD_INDEX, bool useRenderParams = false); 90 static BufferDrawParam CreateBufferDrawParam(const RSDisplayRenderNode& node, bool forceCPU); 91 static BufferDrawParam CreateBufferDrawParam(const RSSurfaceHandler& surfaceHandler, bool forceCPU); 92 static BufferDrawParam CreateBufferDrawParam( 93 const DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable, bool forceCPU, uint32_t threadIndex); 94 static BufferDrawParam CreateBufferDrawParamForRotationFixed( 95 const DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable, RSSurfaceRenderParams& renderParams); 96 static BufferDrawParam CreateLayerBufferDrawParam(const LayerInfoPtr& layer, bool forceCPU); 97 static void DealWithRotationAndGravityForRotationFixed(GraphicTransformType transform, Gravity gravity, 98 RectF& localBounds, BufferDrawParam& params); 99 static bool IsNeedClient(RSSurfaceRenderNode& node, const ComposeInfo& info); 100 static void DrawRectForDfx(RSPaintFilterCanvas& canvas, const RectI& rect, Drawing::Color color, 101 float alpha, const std::string& extraInfo = ""); 102 static Occlusion::Region AlignedDirtyRegion(const Occlusion::Region& dirtyRegion, int32_t alignedBits = 32); 103 static int TransferToAntiClockwiseDegrees(int angle); 104 static int GetRotationFromMatrix(Drawing::Matrix matrix); 105 static int GetRotationDegreeFromMatrix(Drawing::Matrix matrix); 106 static float GetFloatRotationDegreeFromMatrix(Drawing::Matrix matrix); 107 static bool HasNonZRotationTransform(Drawing::Matrix matrix); 108 109 static void ClearCacheSurface(RSRenderNode& node, uint32_t threadIndex, bool isClearCompletedCacheSurface = true); 110 static void ClearNodeCacheSurface(std::shared_ptr<Drawing::Surface>&& cacheSurface, 111 std::shared_ptr<Drawing::Surface>&& cacheCompletedSurface, 112 uint32_t cacheSurfaceThreadIndex, uint32_t completedSurfaceThreadIndex); 113 static void CacheSubThreadNodes(std::list<std::shared_ptr<RSSurfaceRenderNode>>& oldSubThreadNodes, 114 std::list<std::shared_ptr<RSSurfaceRenderNode>>& subThreadNodes); 115 #ifdef RS_ENABLE_VK 116 static uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties); 117 static void SetVkImageInfo(std::shared_ptr<OHOS::Rosen::Drawing::VKTextureInfo> vkImageInfo, 118 const VkImageCreateInfo& imageInfo); 119 static Drawing::BackendTexture MakeBackendTexture(uint32_t width, uint32_t height, 120 VkFormat format = VK_FORMAT_R8G8B8A8_UNORM); 121 #endif 122 static void UpdateRealSrcRect(RSSurfaceRenderNode& node, const RectI& absRect); 123 static void DealWithNodeGravity(RSSurfaceRenderNode& node, const Drawing::Matrix& totalMatrix); 124 static void DealWithScalingMode(RSSurfaceRenderNode& node, const Drawing::Matrix& totalMatrix); 125 static void CheckForceHardwareAndUpdateDstRect(RSSurfaceRenderNode& node); 126 static void LayerRotate(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo); 127 static void LayerCrop(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo); 128 static GraphicTransformType GetLayerTransform(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo); 129 static void OptimizedFlushAndSubmit(std::shared_ptr<Drawing::Surface>& surface, 130 Drawing::GPUContext* const grContext, bool optFenceWait = true); 131 static void AccumulateMatrixAndAlpha(std::shared_ptr<RSSurfaceRenderNode>& hwcNode, 132 Drawing::Matrix& matrix, float& alpha); 133 static SecRectInfo GenerateSecRectInfoFromNode(RSRenderNode& node, RectI rect); 134 static SecSurfaceInfo GenerateSecSurfaceInfoFromNode( 135 NodeId uiExtensionId, NodeId hostId, SecRectInfo uiExtensionRectInfo); 136 static void UIExtensionFindAndTraverseAncestor( 137 const RSRenderNodeMap& nodeMap, UIExtensionCallbackData& callbackData, bool isUnobscured = false); 138 static void TraverseAndCollectUIExtensionInfo(std::shared_ptr<RSRenderNode> node, 139 Drawing::Matrix parentMatrix, NodeId hostId, UIExtensionCallbackData& callbackData, bool isUnobscured = false); 140 static void ProcessCacheImage(RSPaintFilterCanvas& canvas, Drawing::Image& cacheImageProcessed); 141 static void ProcessCacheImageRect(RSPaintFilterCanvas& canvas, Drawing::Image& cacheImageProcessed, 142 const Drawing::Rect& src, const Drawing::Rect& dst); 143 static void ProcessCacheImageForMultiScreenView(RSPaintFilterCanvas& canvas, Drawing::Image& cacheImageProcessed, 144 const RectF& rect); 145 static void FlushDmaSurfaceBuffer(Media::PixelMap* pixelMap); 146 template<typename... Callbacks> TraverseParentNodeAndReduce(std::shared_ptr<RSSurfaceRenderNode> hwcNode,Callbacks &&...callbacks)147 static void TraverseParentNodeAndReduce(std::shared_ptr<RSSurfaceRenderNode> hwcNode, Callbacks&&... callbacks) 148 { 149 static_assert((std::is_invocable<Callbacks, std::shared_ptr<RSRenderNode>>::value && ...), 150 "uninvocable callback"); 151 if (!hwcNode) { 152 return; 153 } 154 auto parent = std::static_pointer_cast<RSRenderNode>(hwcNode); 155 while (static_cast<bool>(parent = parent->GetParent().lock())) { 156 (std::invoke(callbacks, parent), ...); 157 if (parent->GetType() == RSRenderNodeType::DISPLAY_NODE) { 158 break; 159 } 160 } 161 } 162 static std::optional<Drawing::Matrix> GetMatrix(std::shared_ptr<RSRenderNode> hwcNode); 163 // RTthread needs to draw one more frame when screen is turned off. For other threads, use extraframe default value. 164 static bool CheckRenderSkipIfScreenOff(bool extraFrame = false, std::optional<ScreenId> screenId = std::nullopt); 165 static void UpdateHwcNodeProperty(std::shared_ptr<RSSurfaceRenderNode> hwcNode); 166 static void MultiLayersPerf(size_t layerNum); 167 static GraphicTransformType GetConsumerTransform(const RSSurfaceRenderNode& node, 168 const sptr<SurfaceBuffer> buffer, const sptr<IConsumerSurface> consumer); 169 static Drawing::Rect CalcSrcRectByBufferRotation(const SurfaceBuffer& buffer, 170 const GraphicTransformType consumerTransformType, Drawing::Rect newSrcRect); 171 static bool IsHwcEnabledByGravity(RSSurfaceRenderNode& node, const Gravity frameGravity); 172 static void DealWithNodeGravityOldVersion(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo); 173 static Drawing::Rect GetImageRegions(float screenWidth, float screenHeight, 174 float realImageWidth, float realImageHeight); 175 static RectI SrcRectRotateTransform(const SurfaceBuffer& buffer, 176 const GraphicTransformType bufferRotateTransformType, const RectI& newSrcRect); 177 static void CalcSrcRectByBufferFlip(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo); 178 static bool IsHwcEnabledByScalingMode(RSSurfaceRenderNode& node, const ScalingMode scalingMode); 179 static void UpdateHwcNodeByScalingMode(RSSurfaceRenderNode& node, const Drawing::Matrix& totalMatrix, 180 const Drawing::Matrix& gravityMatrix, const Drawing::Matrix& scalingModeMatrix); 181 182 private: 183 static void SetSrcRect(BufferDrawParam& params, const sptr<SurfaceBuffer>& buffer); 184 static void SortSubThreadNodes(std::list<std::shared_ptr<RSSurfaceRenderNode>>& subThreadNodes); 185 static void HandleHardwareNode(const std::shared_ptr<RSSurfaceRenderNode>& node); 186 static void PostReleaseSurfaceTask(std::shared_ptr<Drawing::Surface>&& surface, uint32_t threadIndex); 187 static GraphicTransformType GetRotateTransformForRotationFixed(RSSurfaceRenderNode& node, 188 sptr<IConsumerSurface> consumer); 189 static bool FrameAwareTraceBoost(size_t layerNum); 190 static void RequestPerf(uint32_t layerLevel, bool onOffTag); 191 static void GetSampledDamageAndDrawnRegion(const ScreenInfo& screenInfo, const Occlusion::Region& srcDamageRegion, 192 bool isDirtyAlignEnabled, Occlusion::Region& sampledDamageRegion, Occlusion::Region& sampledDrawnRegion); 193 194 static inline int currentUIExtensionIndex_ = -1; 195 static inline const std::string RELEASE_SURFACE_TASK = "releaseSurface"; 196 }; 197 } 198 } 199 #endif // RENDER_SERVICE_CORE_PIPELINE_RS_UNI_RENDER_UTIL_H 200