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