From ebdca72f4608fdcae891f4bf25090de8a6db4a81 Mon Sep 17 00:00:00 2001 From: xxxx Date: Mon, 1 Aug 2022 16:15:08 +0800 Subject: [PATCH] Description:context Match-id-da88c5265094958c557bce93c4a42bd4aea02b2d --- common/include/dinput_errcode.h | 2 + .../distributed_input_source_transport.h | 4 +- utils/BUILD.gn | 1 + utils/include/dinput_context.h | 131 ++++++++++++++++ utils/src/dinput_context.cpp | 146 ++++++++++++++++++ 5 files changed, 282 insertions(+), 2 deletions(-) create mode 100644 utils/include/dinput_context.h create mode 100644 utils/src/dinput_context.cpp diff --git a/common/include/dinput_errcode.h b/common/include/dinput_errcode.h index 0ca3062..f86b079 100644 --- a/common/include/dinput_errcode.h +++ b/common/include/dinput_errcode.h @@ -35,6 +35,8 @@ namespace DistributedInput { constexpr int32_t ERR_DH_INPUT_DLOPEN_FAIL = -60009; constexpr int32_t ERR_DH_INPUT_DLSYM_FAIL = -60010; constexpr int32_t ERR_DH_INPUT_LOW_LATENCY_LIB_NULL = -60011; + constexpr int32_t ERR_DH_INPUT_CONTEXT_KEY_NOT_EXIST = -60012; + constexpr int32_t ERR_DH_INPUT_CONTEXT_CALCULATE_FAIL = -60013; // whilte list error code constexpr int32_t ERR_DH_INPUT_WHILTELIST_INIT_FAIL = -61001; diff --git a/services/source/transport/include/distributed_input_source_transport.h b/services/source/transport/include/distributed_input_source_transport.h index 689905f..34d36c2 100644 --- a/services/source/transport/include/distributed_input_source_transport.h +++ b/services/source/transport/include/distributed_input_source_transport.h @@ -82,8 +82,8 @@ private: uint64_t deltaTime_ = 0; uint64_t deltaTimeAll_ = 0; uint64_t sendTime_ = 0; - int32_t sendNum_ = 0; - int32_t recvNum_ = 0; + uint32_t sendNum_ = 0; + uint32_t recvNum_ = 0; std::atomic isLatencyThreadRunning_ = false; std::thread latencyThread_; std::string eachLatencyDetails_ = ""; diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 5b274e3..8a4246c 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -26,6 +26,7 @@ ohos_shared_library("libdinput_utils") { sources = [ "src/dinput_utils_tool.cpp", + "src/dinput_context.cpp", ] defines = [ diff --git a/utils/include/dinput_context.h b/utils/include/dinput_context.h new file mode 100644 index 0000000..01445ef --- /dev/null +++ b/utils/include/dinput_context.h @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_INPUT_CONTEXT_H +#define OHOS_DISTRIBUTED_INPUT_CONTEXT_H + +#include +#include +#include +#include +#include + +#include "single_instance.h" + +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +struct LocalAbsInfo { + int32_t abs_x_min; + int32_t abs_x_max; + int32_t abs_y_min; + int32_t abs_y_max; + int32_t abs_pressure_min; + int32_t abs_pressure_max; + int32_t abs_mt_touch_major_min; + int32_t abs_mt_touch_major_max; + int32_t abs_mt_touch_minor_min; + int32_t abs_mt_touch_minor_max; + int32_t abs_mt_orientation_min; + int32_t abs_mt_orientation_max; + int32_t abs_mt_position_x_min; + int32_t abs_mt_position_x_max; + int32_t abs_mt_position_y_min; + int32_t abs_mt_position_y_max; + int32_t abs_mt_blob_id_min; + int32_t abs_mt_blob_id_max; + int32_t abs_mt_tracking_id_min; + int32_t abs_mt_tracking_id_max; + int32_t abs_mt_pressure_min; + int32_t abs_mt_pressure_max; +}; + +struct LocalTouchScreenInfo { + uint32_t sinkShowWidth; + uint32_t sinkShowHeight; + uint32_t sinkPhyWidth; + uint32_t sinkPhyHeight; + LocalAbsInfo localAbsInfo; +}; + +struct SrcScreenInfo { + std::string devId; // source device id + std::string sourceWinId; // source projection window id + uint32_t sourceWinWidth; // source projection window width + uint32_t sourceWinHeight; // source projection window height + std::string sourcePhyId; // source virtual screen driver id + uint32_t sourcePhyFd; // source virtual screen driver fd + uint32_t sourcePhyWidth; // source virtual screen driver width + uint32_t sourcePhyHeight; // source virtual screen driver height +}; + +struct TransformInfo { + uint32_t sinkWinPhyX; // projection area X coordinate in touch coordinate + uint32_t sinkWinPhyY; // projection area Y coordinate in touch coordinate + uint32_t sinkProjPhyWidth; // projection area width in touch coordinate + uint32_t sinkProjPhyHeight; // projection area height in touch coordinate + float coeffWidth; // sink width transform source coefficient + float coeffHeight; // sink height transform source coefficient +}; + +struct SinkScreenInfo { + uint32_t sinkShowWidth; // sink show width + uint32_t sinkShowHeight; // sink show height + uint32_t sinkPhyWidth; // sink touch screen width + uint32_t sinkPhyHeight; // sink touch screen height + std::string sinkShowWinId; // sink show window id + uint32_t sinkWinShowX; // sink show window x coordinate + uint32_t sinkWinShowY; // sink show window y coordinate + uint32_t sinkProjShowWidth; // sink show window width + uint32_t sinkProjShowHeight; // sink show window height + SrcScreenInfo srcScreenInfo; + TransformInfo transformInfo; +}; + +class DInputContext { +DECLARE_SINGLE_INSTANCE_BASE(DInputContext); +public: + std::string GetSourceWindId(const std::string &devId, const std::string &sourceWinId); + int32_t RemoveSinkScreenInfo(const std::string &sourceWinId); + int32_t UpdateSinkScreenInfo(const std::string &sourceWinId, const SinkScreenInfo &sinkScreenInfo); + SinkScreenInfo GetSinkScreenInfo(const std::string &sourceWinId); + + int32_t RemoveSrcScreenInfo(const std::string &sourceWinId); + int32_t UpdateSrcScreenInfo(const std::string &sourceWinId, const SrcScreenInfo &srcScreenInfo); + SrcScreenInfo GetSrcScreenInfo(const std::string &sourceWinId); + + void SetLocalTouchScrennInfo(const LocalTouchScreenInfo &localTouchScreenInfo); + LocalTouchScreenInfo GetLocalTouchScreenInfo(); +private: + int32_t CalculateTransformInfo(SinkScreenInfo &sinkScreenInfo); + +private: + DInputContext() = default; + ~DInputContext(); + + std::unordered_map sinkScreenInfoMap_; + std::unordered_map srcScreenInfoMap_; + LocalTouchScreenInfo localTouchScreenInfo_; + std::mutex srcMapMutex_; + std::mutex sinkMapMutex_; + std::mutex localTouchScreenInfoMutex_; +}; +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_DISTRIBUTED_INPUT_CONTEXT_H \ No newline at end of file diff --git a/utils/src/dinput_context.cpp b/utils/src/dinput_context.cpp new file mode 100644 index 0000000..1c2c47c --- /dev/null +++ b/utils/src/dinput_context.cpp @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dinput_context.h" + +#include "constants.h" + +#include "dinput_errcode.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +IMPLEMENT_SINGLE_INSTANCE(DInputContext); + +DInputContext::~DInputContext() +{ + sinkScreenInfoMap_.clear(); + srcScreenInfoMap_.clear(); +} + +std::string GetSourceWindId(const std::string &devId, const std::string &sourceWinId) +{ + return devId + RESOURCE_SEPARATOR + sourceWinId; +} + +int32_t DInputContext::RemoveSinkScreenInfo(const std::string &sourceWinId) +{ + std::lock_guard lock(sinkMapMutex_); + sinkScreenInfoMap_.erase(sourceWinId); + return DH_SUCCESS; +} + +int32_t DInputContext::UpdateSinkScreenInfo(const std::string &sourceWinId, const SinkScreenInfo &sinkScreenInfo) +{ + std::lock_guard lock(sinkMapMutex_); + if (sinkScreenInfoMap_.count(sourceWinId) <= 0) { + DHLOGE("source window id not exist"); + return ERR_DH_INPUT_CONTEXT_KEY_NOT_EXIST; + } + + SinkScreenInfo tmp = sinkScreenInfo; + if (CalculateTransformInfo(tmp) != DH_SUCCESS) { + DHLOGE("calculate transform infomation failed"); + } + + sinkScreenInfoMap_[sourceWinId] = tmp; + return DH_SUCCESS; +} + +SinkScreenInfo DInputContext::GetSinkScreenInfo(const std::string &sourceWinId) +{ + std::lock_guard lock(sinkMapMutex_); + if (sinkScreenInfoMap_.count(sourceWinId) <= 0) { + DHLOGE("source window id not exist"); + SinkScreenInfo sinkScreenInfo; + sinkScreenInfoMap_[sourceWinId] = sinkScreenInfo; + } + + return sinkScreenInfoMap_[sourceWinId]; +} + +int32_t DInputContext::RemoveSrcScreenInfo(const std::string &sourceWinId) +{ + std::lock_guard lock(srcMapMutex_); + srcScreenInfoMap_.erase(sourceWinId); + return DH_SUCCESS; +} + +int32_t DInputContext::UpdateSrcScreenInfo(const std::string &sourceWinId, const SrcScreenInfo &srcScreenInfo) +{ + std::lock_guard lock(srcMapMutex_); + if (srcScreenInfoMap_.count(sourceWinId) <= 0) { + DHLOGE("source window id not exist"); + return ERR_DH_INPUT_CONTEXT_KEY_NOT_EXIST; + } + + srcScreenInfoMap_[sourceWinId] = srcScreenInfo; + return DH_SUCCESS; +} + +SrcScreenInfo DInputContext::GetSrcScreenInfo(const std::string &sourceWinId) +{ + std::lock_guard lock(srcMapMutex_); + if (srcScreenInfoMap_.count(sourceWinId) <= 0) { + DHLOGE("source window id not exist"); + SrcScreenInfo srcScreenInfo; + srcScreenInfoMap_[sourceWinId] = srcScreenInfo; + } + + return srcScreenInfoMap_[sourceWinId]; +} + +void DInputContext::SetLocalTouchScrennInfo(const LocalTouchScreenInfo &localTouchScreenInfo) +{ + std::lock_guard lock(localTouchScreenInfoMutex_); + localTouchScreenInfo_ = localTouchScreenInfo; +} + +LocalTouchScreenInfo DInputContext::GetLocalTouchScreenInfo() +{ + std::lock_guard lock(localTouchScreenInfoMutex_); + return localTouchScreenInfo_; +} + +int32_t DInputContext::CalculateTransformInfo(SinkScreenInfo &sinkScreenInfo) +{ + if (sinkScreenInfo.sinkShowHeight == 0 || sinkScreenInfo.sinkShowWidth == 0) { + DHLOGE("can not calculate transform infomation"); + return ERR_DH_INPUT_CONTEXT_CALCULATE_FAIL; + } + TransformInfo transformInfo; + transformInfo.sinkWinPhyX = (sinkScreenInfo.sinkWinShowX / sinkScreenInfo.sinkShowWidth) * + sinkScreenInfo.sinkPhyWidth; + transformInfo.sinkWinPhyY = (sinkScreenInfo.sinkWinShowY / sinkScreenInfo.sinkShowHeight) * + sinkScreenInfo.sinkPhyHeight; + transformInfo.sinkProjPhyWidth = (sinkScreenInfo.sinkProjShowWidth / sinkScreenInfo.sinkShowWidth) * + sinkScreenInfo.sinkPhyWidth; + transformInfo.sinkProjPhyHeight = (sinkScreenInfo.sinkProjShowHeight / sinkScreenInfo.sinkShowHeight) * + sinkScreenInfo.sinkPhyHeight; + if (transformInfo.sinkProjPhyWidth == 0 || transformInfo.sinkProjPhyHeight == 0) { + DHLOGE("can not calculate transform infomation"); + return ERR_DH_INPUT_CONTEXT_CALCULATE_FAIL; + } + + // coefficient of the sink projection area in the source touch driver + transformInfo.coeffWidth = sinkScreenInfo.srcScreenInfo.sourcePhyWidth / transformInfo.sinkProjPhyWidth; + transformInfo.coeffHeight = sinkScreenInfo.srcScreenInfo.sourcePhyHeight / transformInfo.sinkProjPhyHeight; + + sinkScreenInfo.transformInfo = transformInfo; + return DH_SUCCESS; +} +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file