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_screen_render_params.h" 31 #include "pipeline/rs_screen_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 RSScreenRenderParams; 43 class RSDirtyRectsDfx; 44 class RSUniRenderUtil { 45 public: 46 // planning: dirty region related functions should be moved to feature_utils 47 static void ExpandDamageRegionToSingleRect(Occlusion::Region& damageRegion); 48 static std::vector<RectI> MergeDirtyHistory(DrawableV2::RSScreenRenderNodeDrawable& screenDrawable, 49 int32_t bufferAge, ScreenInfo& screenInfo, RSDirtyRectsDfx& rsDirtyRectsDfx, RSScreenRenderParams& params); 50 // isSecScreen means that special layers can be displayed on the screen, for example wired mirror screen. 51 static std::vector<RectI> MergeDirtyHistoryInVirtual(DrawableV2::RSScreenRenderNodeDrawable& screenDrawable, 52 int32_t bufferAge, const ScreenInfo& screenInfo, bool isSecScreen = false); 53 // merge history dirty region of current display node and its child surfacenode(app windows) 54 // for mirror display, call this function twice will introduce additional dirtyhistory in dirtymanager 55 static void MergeDirtyHistoryForDrawable(DrawableV2::RSScreenRenderNodeDrawable& drawable, int32_t bufferAge, 56 RSScreenRenderParams& params, bool useAlignedDirtyRegion = false); 57 static void SetDrawRegionForQuickReject( 58 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& allSurfaceDrawables, 59 const Occlusion::Region mergedDirtyRects); 60 /* we want to set visible dirty region of each surfacenode into DamageRegionKHR interface, hence 61 * occlusion is calculated. 62 * make sure this function is called after merge dirty history 63 */ 64 static Occlusion::Region MergeVisibleDirtyRegion( 65 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& allSurfaceNodeDrawables, 66 std::vector<NodeId>& hasVisibleDirtyRegionSurfaceVec, bool useAlignedDirtyRegion = false); 67 static Occlusion::Region MergeVisibleAdvancedDirtyRegion( 68 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& allSurfaceNodeDrawables, 69 std::vector<NodeId>& hasVisibleDirtyRegionSurfaceVec); 70 static Occlusion::Region MergeDirtyRects(Occlusion::Region dirtyRegion); 71 static void MergeDirtyHistoryInVirtual( 72 DrawableV2::RSScreenRenderNodeDrawable& screenDrawable, int32_t bufferAge, bool renderParallel = false); 73 // isSecScreen means that special layers can be displayed on the screen, for example wired mirror screen. 74 static Occlusion::Region MergeVisibleDirtyRegionInVirtual( 75 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& allSurfaceNodeDrawables, 76 RSScreenRenderParams& screenParams, bool isSecScreen = false); 77 // This is used for calculate matrix from buffer coordinate to window's relative coordinate 78 static Drawing::Matrix GetMatrixOfBufferToRelRect(const RSSurfaceRenderNode& node); 79 static void SrcRectScaleDown(BufferDrawParam& params, const sptr<SurfaceBuffer>& buffer, 80 const sptr<IConsumerSurface>& surface, RectF& localBounds); 81 static void SrcRectScaleFit(BufferDrawParam& params, const sptr<SurfaceBuffer>& buffer, 82 const sptr<IConsumerSurface>& surface, RectF& localBounds); 83 static BufferDrawParam CreateBufferDrawParam(const RSSurfaceRenderNode& node, 84 bool forceCPU, uint32_t threadIndex = UNI_RENDER_THREAD_INDEX, bool useRenderParams = false); 85 static BufferDrawParam CreateBufferDrawParam(const RSScreenRenderNode& node, bool forceCPU); 86 static BufferDrawParam CreateBufferDrawParam(const RSSurfaceHandler& surfaceHandler, bool forceCPU); 87 static BufferDrawParam CreateBufferDrawParam( 88 const DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable, bool forceCPU, uint32_t threadIndex); 89 static BufferDrawParam CreateBufferDrawParamForRotationFixed( 90 const DrawableV2::RSSurfaceRenderNodeDrawable& surfaceDrawable, RSSurfaceRenderParams& renderParams, 91 uint32_t threadIndex); 92 static BufferDrawParam CreateLayerBufferDrawParam(const LayerInfoPtr& layer, bool forceCPU); 93 static void DealWithRotationAndGravityForRotationFixed(GraphicTransformType transform, Gravity gravity, 94 RectF& localBounds, BufferDrawParam& params); 95 static bool IsNeedClient(RSSurfaceRenderNode& node, const ComposeInfo& info); 96 static void DrawRectForDfx(RSPaintFilterCanvas& canvas, const RectI& rect, Drawing::Color color, 97 float alpha, const std::string& extraInfo = ""); 98 static Occlusion::Region AlignedDirtyRegion(const Occlusion::Region& dirtyRegion, int32_t alignedBits = 32); 99 static int TransferToAntiClockwiseDegrees(int angle); 100 static int GetRotationFromMatrix(Drawing::Matrix matrix); 101 static int GetRotationDegreeFromMatrix(Drawing::Matrix matrix); 102 103 static void ClearCacheSurface(RSRenderNode& node, uint32_t threadIndex, bool isClearCompletedCacheSurface = true); 104 static void ClearNodeCacheSurface(std::shared_ptr<Drawing::Surface>&& cacheSurface, 105 std::shared_ptr<Drawing::Surface>&& cacheCompletedSurface, 106 uint32_t cacheSurfaceThreadIndex, uint32_t completedSurfaceThreadIndex); 107 static void OptimizedFlushAndSubmit(std::shared_ptr<Drawing::Surface>& surface, 108 Drawing::GPUContext* const grContext, bool optFenceWait = true); 109 static std::vector<GrBackendSemaphore> PrepareHdrSemaphoreVector(GrBackendSemaphore& backendSemaphore, 110 std::shared_ptr<Drawing::Surface>& surface); 111 static SecRectInfo GenerateSecRectInfoFromNode(RSRenderNode& node, RectI rect); 112 static SecSurfaceInfo GenerateSecSurfaceInfoFromNode( 113 NodeId uiExtensionId, NodeId hostId, SecRectInfo uiExtensionRectInfo); 114 static void UIExtensionFindAndTraverseAncestor( 115 const RSRenderNodeMap& nodeMap, UIExtensionCallbackData& callbackData, bool isUnobscured = false); 116 static void TraverseAndCollectUIExtensionInfo(std::shared_ptr<RSRenderNode> node, 117 Drawing::Matrix parentMatrix, NodeId hostId, UIExtensionCallbackData& callbackData, bool isUnobscured = false); 118 static void ProcessCacheImage(RSPaintFilterCanvas& canvas, Drawing::Image& cacheImageProcessed); 119 static void ProcessCacheImageRect(RSPaintFilterCanvas& canvas, Drawing::Image& cacheImageProcessed, 120 const Drawing::Rect& src, const Drawing::Rect& dst); 121 static void ProcessCacheImageForMultiScreenView(RSPaintFilterCanvas& canvas, Drawing::Image& cacheImageProcessed, 122 const RectF& rect); 123 static void FlushDmaSurfaceBuffer(Media::PixelMap* pixelMap); 124 // RTthread needs to draw one more frame when screen is turned off. For other threads, use extraframe default value. 125 static bool CheckRenderSkipIfScreenOff(bool extraFrame = false, std::optional<ScreenId> screenId = std::nullopt); 126 static void MultiLayersPerf(size_t layerNum); 127 static Drawing::Rect GetImageRegions(float screenWidth, float screenHeight, 128 float realImageWidth, float realImageHeight); 129 // Calculate the rotation angle around the z-axis from quaternion 130 static float GetYawFromQuaternion(const Quaternion& q); 131 static void CollectHardwareEnabledNodeByScreenNodeId( 132 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& enableNodes, 133 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& topEnabledNodes, NodeId id); 134 static void CollectHardwareEnabledNodesByDisplayNodeId( 135 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& enableNodes, 136 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& topEnabledNodes, NodeId id); 137 static void AdjustZOrderAndDrawSurfaceNode( 138 std::vector<DrawableV2::RSRenderNodeDrawableAdapter::SharedPtr>& drawables, 139 Drawing::Canvas& canvas, RSScreenRenderParams& params); 140 static void SwitchColorFilter(RSPaintFilterCanvas& canvas, float hdrBrightnessRatio = 1.f, 141 bool displayP3Enable = false); 142 143 private: 144 static void SetSrcRect(BufferDrawParam& params, const sptr<SurfaceBuffer>& buffer); 145 static void PostReleaseSurfaceTask(std::shared_ptr<Drawing::Surface>&& surface, uint32_t threadIndex); 146 static bool FrameAwareTraceBoost(size_t layerNum); 147 static void RequestPerf(uint32_t layerLevel, bool onOffTag); 148 static void GetSampledDamageAndDrawnRegion(const ScreenInfo& screenInfo, const Occlusion::Region& srcDamageRegion, 149 bool isDirtyAlignEnabled, Occlusion::Region& sampledDamageRegion, Occlusion::Region& sampledDrawnRegion); 150 static void SwitchColorFilterWithP3(RSPaintFilterCanvas& canvas, ColorFilterMode colorFilterMode, 151 float hdrBrightnessRatio = 1.f); 152 153 static inline int currentUIExtensionIndex_ = -1; 154 static inline const std::string RELEASE_SURFACE_TASK = "releaseSurface"; 155 }; 156 } 157 } 158 #endif // RENDER_SERVICE_CORE_PIPELINE_RS_UNI_RENDER_UTIL_H 159