1 /*
2  * Copyright (c) 2024 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 #include "pipeline/rs_pointer_window_manager.h"
17 #include "common/rs_optional_trace.h"
18 #include "pipeline/rs_main_thread.h"
19 
20 namespace OHOS {
21 namespace Rosen {
Instance()22 RSPointerWindowManager& RSPointerWindowManager::Instance()
23 {
24     static RSPointerWindowManager instance;
25     return instance;
26 }
27 
UpdatePointerDirtyToGlobalDirty(std::shared_ptr<RSSurfaceRenderNode> & pointWindow,std::shared_ptr<RSDisplayRenderNode> & curDisplayNode)28 void RSPointerWindowManager::UpdatePointerDirtyToGlobalDirty(std::shared_ptr<RSSurfaceRenderNode>& pointWindow,
29     std::shared_ptr<RSDisplayRenderNode>& curDisplayNode)
30 {
31     if (pointWindow == nullptr || curDisplayNode == nullptr) {
32         return;
33     }
34     auto dirtyManager = pointWindow->GetDirtyManager();
35     if (dirtyManager && pointWindow->GetHardCursorStatus()) {
36         if (!pointWindow->GetHardCursorLastStatus()) {
37             RectI lastFrameSurfacePos = curDisplayNode->GetLastFrameSurfacePos(pointWindow->GetId());
38             curDisplayNode->GetDirtyManager()->MergeDirtyRect(lastFrameSurfacePos);
39         }
40         auto pointerWindowDirtyRegion = dirtyManager->GetCurrentFrameDirtyRegion();
41         if (!pointerWindowDirtyRegion.IsEmpty()) {
42             curDisplayNode->GetDirtyManager()->MergeHwcDirtyRect(pointerWindowDirtyRegion);
43             dirtyManager->SetCurrentFrameDirtyRect(RectI());
44             isNeedForceCommitByPointer_ = true;
45         } else {
46             isNeedForceCommitByPointer_ = false;
47         }
48     }
49 }
50 
SetHardCursorNodeInfo(std::shared_ptr<RSSurfaceRenderNode> hardCursorNode)51 void RSPointerWindowManager::SetHardCursorNodeInfo(std::shared_ptr<RSSurfaceRenderNode> hardCursorNode)
52 {
53     if (!hardCursorNode) {
54         return;
55     }
56     if (!hardCursorNode->IsHardwareEnabledTopSurface() || !hardCursorNode->ShouldPaint()) {
57         return;
58     }
59     hardCursorNodes_ = hardCursorNode;
60 }
61 
GetHardCursorNode() const62 const std::shared_ptr<RSSurfaceRenderNode>& RSPointerWindowManager::GetHardCursorNode() const
63 {
64     return hardCursorNodes_;
65 }
66 
HardCursorCreateLayerForDirect(std::shared_ptr<RSProcessor> processor)67 void RSPointerWindowManager::HardCursorCreateLayerForDirect(std::shared_ptr<RSProcessor> processor)
68 {
69     auto hardCursorNode = GetHardCursorNode();
70     if (hardCursorNode && hardCursorNode->IsHardwareEnabledTopSurface()) {
71         auto surfaceHandler = hardCursorNode->GetRSSurfaceHandler();
72         auto params = static_cast<RSSurfaceRenderParams*>(hardCursorNode->GetStagingRenderParams().get());
73         if (!surfaceHandler->IsCurrentFrameBufferConsumed() && params->GetPreBuffer() != nullptr) {
74             params->SetPreBuffer(nullptr);
75             hardCursorNode->AddToPendingSyncList();
76         }
77         RS_OPTIONAL_TRACE_NAME("HardCursorCreateLayerForDirect create layer");
78         processor->CreateLayer(*hardCursorNode, *params);
79     }
80 }
81 
CheckHardCursorSupport(std::shared_ptr<RSDisplayRenderNode> & curDisplayNode)82 bool RSPointerWindowManager::CheckHardCursorSupport(std::shared_ptr<RSDisplayRenderNode>& curDisplayNode)
83 {
84     if (curDisplayNode == nullptr) {
85         return false;
86     }
87     auto screenManager = CreateOrGetScreenManager();
88     if (!screenManager) {
89         return false;
90     }
91     return screenManager->GetDisplayPropertyForHardCursor(curDisplayNode->GetScreenId());
92 }
93 
HasMirrorDisplay() const94 bool RSPointerWindowManager::HasMirrorDisplay() const
95 {
96     const std::shared_ptr<RSBaseRenderNode> rootNode =
97         RSMainThread::Instance()->GetContext().GetGlobalRootRenderNode();
98     if (rootNode == nullptr || rootNode->GetChildrenCount() <= 1) {
99         return false;
100     }
101     for (auto& child : *rootNode->GetSortedChildren()) {
102         if (!child || !child->IsInstanceOf<RSDisplayRenderNode>()) {
103             continue;
104         }
105         auto displayNode = child->ReinterpretCastTo<RSDisplayRenderNode>();
106         if (!displayNode) {
107             continue;
108         }
109         if (displayNode->IsMirrorDisplay()) {
110             return true;
111         }
112     }
113     return false;
114 }
115 } // namespace Rosen
116 } // namespace OHOS