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 RSUniRenderUtil { 43 public: 44 // merge history dirty region of current display node and its child surfacenode(app windows) 45 // for mirror display, call this function twice will introduce additional dirtyhistory in dirtymanager 46 static void MergeDirtyHistory(std::shared_ptr<RSDisplayRenderNode>& node, int32_t bufferAge, 47 bool useAlignedDirtyRegion = false); 48 49 static void MergeDirtyHistoryForNode(std::shared_ptr<RSDisplayRenderNode>& node, int32_t bufferAge, 50 RSDisplayRenderParams* params = nullptr, bool useAlignedDirtyRegion = false); 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 ®ion, 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 GetRotationFromMatrix(Drawing::Matrix matrix); 94 static int GetRotationDegreeFromMatrix(Drawing::Matrix matrix); 95 static bool Is3DRotation(Drawing::Matrix matrix); 96 97 static void AssignWindowNodes(const std::shared_ptr<RSDisplayRenderNode>& displayNode, 98 std::list<std::shared_ptr<RSSurfaceRenderNode>>& mainThreadNodes, 99 std::list<std::shared_ptr<RSSurfaceRenderNode>>& subThreadNodes); 100 static void ClearSurfaceIfNeed(const RSRenderNodeMap& map, const std::shared_ptr<RSDisplayRenderNode>& displayNode, 101 std::set<std::shared_ptr<RSBaseRenderNode>>& oldChildren, DeviceType deviceType = DeviceType::PHONE); 102 static void ClearCacheSurface(RSRenderNode& node, uint32_t threadIndex, bool isClearCompletedCacheSurface = true); 103 static void ClearNodeCacheSurface(std::shared_ptr<Drawing::Surface>&& cacheSurface, 104 std::shared_ptr<Drawing::Surface>&& cacheCompletedSurface, 105 uint32_t cacheSurfaceThreadIndex, uint32_t completedSurfaceThreadIndex); 106 static void CacheSubThreadNodes(std::list<std::shared_ptr<RSSurfaceRenderNode>>& oldSubThreadNodes, 107 std::list<std::shared_ptr<RSSurfaceRenderNode>>& subThreadNodes); 108 static bool IsNodeAssignSubThread(std::shared_ptr<RSSurfaceRenderNode> node, bool isDisplayRotation); 109 #ifdef RS_ENABLE_VK 110 static uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties); 111 static void SetVkImageInfo(std::shared_ptr<OHOS::Rosen::Drawing::VKTextureInfo> vkImageInfo, 112 const VkImageCreateInfo& imageInfo); 113 static Drawing::BackendTexture MakeBackendTexture(uint32_t width, uint32_t height, 114 VkFormat format = VK_FORMAT_R8G8B8A8_UNORM); 115 #endif 116 static void UpdateRealSrcRect(RSSurfaceRenderNode& node, const RectI& absRect); 117 static void DealWithNodeGravity(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo); 118 static void DealWithScalingMode(RSSurfaceRenderNode& node); 119 static void CheckForceHardwareAndUpdateDstRect(RSSurfaceRenderNode& node); 120 static void LayerRotate(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo); 121 static void LayerCrop(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo); 122 static void LayerScaleDown(RSSurfaceRenderNode& node); 123 static void LayerScaleFit(RSSurfaceRenderNode& node); 124 static GraphicTransformType GetLayerTransform(RSSurfaceRenderNode& node, const ScreenInfo& screenInfo); 125 static void OptimizedFlushAndSubmit(std::shared_ptr<Drawing::Surface>& surface, 126 Drawing::GPUContext* const grContext, bool optFenceWait = true); 127 static void AccumulateMatrixAndAlpha(std::shared_ptr<RSSurfaceRenderNode>& hwcNode, 128 Drawing::Matrix& matrix, float& alpha); 129 static SecRectInfo GenerateSecRectInfoFromNode(RSRenderNode& node, RectI rect); 130 static SecSurfaceInfo GenerateSecSurfaceInfoFromNode( 131 NodeId uiExtensionId, NodeId hostId, SecRectInfo uiExtensionRectInfo); 132 static void UIExtensionFindAndTraverseAncestor( 133 const RSRenderNodeMap& nodeMap, UIExtensionCallbackData& callbackData); 134 static void TraverseAndCollectUIExtensionInfo(std::shared_ptr<RSRenderNode> node, 135 Drawing::Matrix parentMatrix, NodeId hostId, UIExtensionCallbackData& callbackData); 136 static void ProcessCacheImage(RSPaintFilterCanvas& canvas, Drawing::Image& cacheImageProcessed); 137 template<typename... Callbacks> TraverseParentNodeAndReduce(std::shared_ptr<RSSurfaceRenderNode> hwcNode,Callbacks &&...callbacks)138 static void TraverseParentNodeAndReduce(std::shared_ptr<RSSurfaceRenderNode> hwcNode, Callbacks&&... callbacks) 139 { 140 static_assert((std::is_invocable<Callbacks, std::shared_ptr<RSRenderNode>>::value && ...), 141 "uninvocable callback"); 142 if (!hwcNode) { 143 return; 144 } 145 auto parent = std::static_pointer_cast<RSRenderNode>(hwcNode); 146 while (static_cast<bool>(parent = parent->GetParent().lock())) { 147 (std::invoke(callbacks, parent), ...); 148 if (parent->GetType() == RSRenderNodeType::DISPLAY_NODE) { 149 break; 150 } 151 } 152 } 153 static std::optional<Drawing::Matrix> GetMatrix(std::shared_ptr<RSRenderNode> hwcNode); 154 // RTthread needs to draw one more frame when screen is turned off. For other threads, use extraframe default value. 155 static bool CheckRenderSkipIfScreenOff(bool extraFrame = false, std::optional<ScreenId> screenId = std::nullopt); 156 157 private: 158 static RectI SrcRectRotateTransform(RSSurfaceRenderNode& node, GraphicTransformType transformType); 159 static void AssignMainThreadNode(std::list<std::shared_ptr<RSSurfaceRenderNode>>& mainThreadNodes, 160 const std::shared_ptr<RSSurfaceRenderNode>& node); 161 static void AssignSubThreadNode(std::list<std::shared_ptr<RSSurfaceRenderNode>>& subThreadNodes, 162 const std::shared_ptr<RSSurfaceRenderNode>& node); 163 static void SortSubThreadNodes(std::list<std::shared_ptr<RSSurfaceRenderNode>>& subThreadNodes); 164 static void HandleHardwareNode(const std::shared_ptr<RSSurfaceRenderNode>& node); 165 static void PostReleaseSurfaceTask(std::shared_ptr<Drawing::Surface>&& surface, uint32_t threadIndex); 166 static GraphicTransformType GetRotateTransformForRotationFixed(RSSurfaceRenderNode& node, 167 sptr<IConsumerSurface> consumer); 168 static inline int currentUIExtensionIndex_ = -1; 169 static inline std::string RELEASE_SURFACE_TASK = "releaseSurface"; 170 }; 171 } 172 } 173 #endif // RENDER_SERVICE_CORE_PIPELINE_RS_UNI_RENDER_UTIL_H 174