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 "hardware_cursor_pointer_manager.h"
17
18 #include <thread>
19
20 #include "mmi_log.h"
21
22 #undef MMI_LOG_DOMAIN
23 #define MMI_LOG_DOMAIN MMI_LOG_CURSOR
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "HardwareCursorPointerManager"
26
27 namespace OHOS {
28 namespace MMI {
SetTargetDevice(uint32_t devId)29 void HardwareCursorPointerManager::SetTargetDevice(uint32_t devId)
30 {
31 if (static_cast<int32_t>(devId) < 0) {
32 MMI_HILOGE("SetTargetDevice devId %{public}d is invalid", static_cast<int32_t>(devId));
33 return;
34 }
35
36 if (devId != devId_) {
37 devId_ = devId;
38 MMI_HILOGI("SetTargetDevice devId_ changed");
39 isEnableState_ = false;
40 isDeviceChange_ = false;
41 }
42 }
43
SetHdiServiceState(bool hdiServiceState)44 void HardwareCursorPointerManager::SetHdiServiceState(bool hdiServiceState)
45 {
46 isEnable_ = hdiServiceState;
47 }
48
IsSupported()49 bool HardwareCursorPointerManager::IsSupported()
50 {
51 if (isEnable_ && isEnableState_) {
52 return true;
53 }
54 if (isEnable_ && isDeviceChange_ && (isEnableState_ == false)) {
55 return false;
56 }
57 if (!isEnable_) {
58 auto DisplayComposer = OHOS::HDI::Display::Composer::V1_2::IDisplayComposerInterface::Get(false);
59 std::lock_guard<std::mutex> guard(mtx_);
60 powerInterface_ = DisplayComposer;
61 CHKPF(powerInterface_);
62 isEnable_ = true;
63 }
64 auto powerInterface = GetPowerInterface();
65 CHKPF(powerInterface);
66 uint64_t value = 0;
67 if (powerInterface->GetDisplayProperty(devId_,
68 HDI::Display::Composer::V1_2::DISPLAY_CAPBILITY_HARDWARE_CURSOR, value)
69 != HDI::Display::Composer::V1_2::DISPLAY_SUCCESS) {
70 MMI_HILOGE("Get display property is error");
71 isDeviceChange_ = true;
72 return false;
73 }
74 if (value) {
75 MMI_HILOGI("Get display property is support");
76 isEnableState_ = true;
77 }
78 isDeviceChange_ = true;
79 return isEnableState_;
80 }
81
SetPosition(uint32_t devId,int32_t x,int32_t y,BufferHandle * buffer)82 int32_t HardwareCursorPointerManager::SetPosition(uint32_t devId, int32_t x, int32_t y, BufferHandle* buffer)
83 {
84 CHKPR(buffer, RET_ERR);
85
86 auto powerInterface = GetPowerInterface();
87 CHKPR(powerInterface, RET_ERR);
88 if (powerInterface->UpdateHardwareCursor(devId, x, y, buffer) != HDI::Display::Composer::V1_2::DISPLAY_SUCCESS) {
89 MMI_HILOGE("UpdateHardwareCursor failed, attempting to reinitialize interface");
90 {
91 auto DisplayComposer = OHOS::HDI::Display::Composer::V1_2::IDisplayComposerInterface::Get(false);
92 std::lock_guard<std::mutex> guard(mtx_);
93 powerInterface_ = DisplayComposer;
94 }
95 powerInterface = GetPowerInterface();
96 CHKPR(powerInterface, RET_ERR);
97 if (powerInterface->UpdateHardwareCursor(devId, x, y, buffer) !=
98 HDI::Display::Composer::V1_2::DISPLAY_SUCCESS) {
99 MMI_HILOGE("UpdateHardwareCursor is error");
100 return RET_ERR;
101 }
102 }
103 MMI_HILOGD("SetPosition, devId:%{public}u, x:%{private}d, y:%{private}d", devId, x, y);
104 return RET_OK;
105 }
106
EnableStats(bool enable)107 int32_t HardwareCursorPointerManager::EnableStats(bool enable)
108 {
109 CALL_DEBUG_ENTER;
110 auto powerInterface = GetPowerInterface();
111 CHKPR(powerInterface, RET_ERR);
112 if (powerInterface->EnableHardwareCursorStats(devId_, enable) != HDI::Display::Composer::V1_2::DISPLAY_SUCCESS) {
113 MMI_HILOGE("Enable hardware cursor stats is error");
114 return RET_ERR;
115 }
116 MMI_HILOGD("EnableStats, enable:%{public}d", enable);
117 return RET_OK;
118 }
119
GetCursorStats(uint32_t & frameCount,uint32_t & vsyncCount)120 int32_t HardwareCursorPointerManager::GetCursorStats(uint32_t &frameCount, uint32_t &vsyncCount)
121 {
122 CALL_DEBUG_ENTER;
123 auto powerInterface = GetPowerInterface();
124 CHKPR(powerInterface, RET_ERR);
125 if (powerInterface->GetHardwareCursorStats(devId_, frameCount, vsyncCount) !=
126 HDI::Display::Composer::V1_2::DISPLAY_SUCCESS) {
127 MMI_HILOGE("Get hardware cursor stats is error");
128 return RET_ERR;
129 }
130 MMI_HILOGD("Get hardware cursor stats, frameCount:%{private}d, vsyncCount:%{private}d", frameCount, vsyncCount);
131 return RET_OK;
132 }
133
GetPowerInterface()134 sptr<OHOS::HDI::Display::Composer::V1_2::IDisplayComposerInterface> HardwareCursorPointerManager::GetPowerInterface()
135 {
136 std::lock_guard<std::mutex> guard(mtx_);
137 return powerInterface_;
138 }
139 } // namespace MMI
140 } // namespace OHOS