From c4890c4d9d00485bd044a7b4914903ebaff43568 Mon Sep 17 00:00:00 2001 From: cloud_nine Date: Wed, 13 Mar 2024 10:50:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81DUMP=E8=BE=93=E5=87=BAScreen?= =?UTF-8?q?=E5=9F=BA=E7=A1=80=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyunjiu --- window_scene/session_manager/BUILD.gn | 1 + .../include/screen_session_dumper.h | 53 +++++ .../src/screen_session_dumper.cpp | 195 ++++++++++++++++++ .../src/screen_session_manager.cpp | 15 +- window_scene/test/unittest/BUILD.gn | 13 ++ .../unittest/screen_session_dumper_test.cpp | 123 +++++++++++ 6 files changed, 391 insertions(+), 9 deletions(-) create mode 100644 window_scene/session_manager/include/screen_session_dumper.h create mode 100644 window_scene/session_manager/src/screen_session_dumper.cpp create mode 100644 window_scene/test/unittest/screen_session_dumper_test.cpp diff --git a/window_scene/session_manager/BUILD.gn b/window_scene/session_manager/BUILD.gn index d453bf7aa4..349858a50e 100644 --- a/window_scene/session_manager/BUILD.gn +++ b/window_scene/session_manager/BUILD.gn @@ -202,6 +202,7 @@ ohos_shared_library("screen_session_manager") { "src/screen_rotation_property.cpp", "src/screen_scene_config.cpp", "src/screen_sensor_connector.cpp", + "src/screen_session_dumper.cpp", "src/screen_session_manager.cpp", "src/screen_setting_helper.cpp", "src/session_display_power_controller.cpp", diff --git a/window_scene/session_manager/include/screen_session_dumper.h b/window_scene/session_manager/include/screen_session_dumper.h new file mode 100644 index 0000000000..c60ac784e7 --- /dev/null +++ b/window_scene/session_manager/include/screen_session_dumper.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2024 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_ROSEN_SCREEN_SESSION_DUMPER_H +#define OHOS_ROSEN_SCREEN_SESSION_DUMPER_H + +#include +#include +#include +#include +#include + +#include "dm_common.h" +#include "window_manager_hilog.h" +#include "screen_session_manager.h" + +namespace OHOS { +namespace Rosen { + +class ScreenSessionDumper : public RefBase { +public: + ScreenSessionDumper(int fd, const std::vector& args); + ~ScreenSessionDumper() = default; + + void ExcuteDumpCmd(); + +private: + void ShowHelpInfo(); + void ShowAllScreenInfo(); + void OutputDumpInfo(); + void DumpScreenInfoById(ScreenId id); + void DumpScreenPropertyById(ScreenId id); + +private: + int fd_; + std::vector params_; + std::string dumpInfo_; +}; +} // Rosen +} // OHOS +#endif // OHOS_ROSEN_SCREEN_SESSION_DUMPER_H \ No newline at end of file diff --git a/window_scene/session_manager/src/screen_session_dumper.cpp b/window_scene/session_manager/src/screen_session_dumper.cpp new file mode 100644 index 0000000000..bbe2086913 --- /dev/null +++ b/window_scene/session_manager/src/screen_session_dumper.cpp @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2024 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 "screen_session_dumper.h" + +#include + +#include "unique_fd.h" +#include "session_permission.h" + +namespace OHOS { +namespace Rosen { +namespace { +constexpr int LINE_WIDTH = 25; +constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_DISPLAY, "ScreenSessionDumper" }; +} + +ScreenSessionDumper::ScreenSessionDumper(int fd, const std::vector& args) + : fd_(fd) +{ + std::wstring_convert, char16_t> cv; + std::string info; + for (auto& u16str: args) { + std::string arg = cv.to_bytes(u16str); + params_.emplace_back(arg); + info += arg; + } + WLOGFI("input args: [%{public}s]", info.c_str()); +} + +void ScreenSessionDumper::OutputDumpInfo() +{ + if (fd_ < 0) { + WLOGFE("invalid fd: %{public}d", fd_); + return; + } + + static_cast(signal(SIGPIPE, SIG_IGN)); // ignore SIGPIPE crash + UniqueFd ufd = UniqueFd(fd_); // auto free + int ret = dprintf(ufd.Get(), "%s\n", dumpInfo_.c_str()); + if (ret < 0) { + WLOGFE("dprintf error. ret: %{public}d", ret); + return; + } + dumpInfo_.clear(); +} + + +void ScreenSessionDumper::ExcuteDumpCmd() +{ + const std::string ARG_DUMP_HELP = "-h"; + const std::string ARG_DUMP_ALL = "-a"; + if (params_.empty() || params_[0] == ARG_DUMP_HELP) { //help command + ShowHelpInfo(); + } + + if (!(SessionPermission::IsSACalling() || SessionPermission::IsStartByHdcd())) { + WLOGFE("dump permission denied!"); + return; + } + if (params_[0] == ARG_DUMP_ALL) { // dump all info command + ShowAllScreenInfo(); + } + OutputDumpInfo(); +} + +void ScreenSessionDumper::ShowHelpInfo() +{ + dumpInfo_.append("Usage:\n") + .append(" -h ") + .append("|help text for the tool\n") + .append(" -a ") + .append("|dump all screen information in the system\n"); +} + +void ScreenSessionDumper::ShowAllScreenInfo() +{ + std::vector screenIds = ScreenSessionManager::GetInstance().GetAllScreenIds(); + for (auto screenId : screenIds) { + std::ostringstream oss; + oss << "---------------- Screen ID: " << screenId << " ----------------" << std::endl; + dumpInfo_.append(oss.str()); + DumpScreenInfoById(screenId); + DumpScreenPropertyById(screenId); + } +} + +void ScreenSessionDumper::DumpScreenInfoById(ScreenId id) +{ + std::ostringstream oss; + oss << "[SCREEN INFO]" << std::endl; + auto screenInfo = ScreenSessionManager::GetInstance().GetScreenInfoById(id); + if (screenInfo == nullptr) { + WLOGFE("screenInfo nullptr. screen id: %{public}" PRIu64"", id); + return; + } + auto modes = screenInfo->GetModes(); + auto modeId = screenInfo->GetModeId(); + oss << std::left << std::setw(LINE_WIDTH) << "Name: " + << screenInfo->GetName() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "VirtualWidth: " + << screenInfo->GetVirtualWidth() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "VirtualHeight: " + << screenInfo->GetVirtualHeight() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "LastParentId: " + << screenInfo->GetLastParentId() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "ParentId: " + << screenInfo->GetParentId() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "IsScreenGroup: " + << screenInfo->GetIsScreenGroup() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "VirtualPixelRatio: " + << screenInfo->GetVirtualPixelRatio() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "Rotation: " + << static_cast(screenInfo->GetRotation()) << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "Orientation: " + << static_cast(screenInfo->GetOrientation()) << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "SourceMode: " + << static_cast(screenInfo->GetSourceMode()) << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "ScreenType: " + << static_cast(screenInfo->GetType()) << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "ModeId: " << modeId << std::endl; + if (modes.size() > modeId) { + oss << std::left << std::setw(LINE_WIDTH) << "modes: " + << modes[modeId]->id_ << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "modes: " + << modes[modeId]->width_ << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "modes: " + << modes[modeId]->height_ << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "modes: " + << modes[modeId]->refreshRate_ << std::endl; + } else { + WLOGFW("invalid modes size: %{public}u ", static_cast(modes.size())); + } + dumpInfo_.append(oss.str()); +} + +void ScreenSessionDumper::DumpScreenPropertyById(ScreenId id) +{ + std::ostringstream oss; + oss << "[SCREEN PROPERTY]" << std::endl; + ScreenProperty screenProperty = ScreenSessionManager::GetInstance().GetScreenProperty(id); + + oss << std::left << std::setw(LINE_WIDTH) << "Rotation: " << screenProperty.GetRotation() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "Density: " << screenProperty.GetDensity() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "DensityInCurResolution: " + << screenProperty.GetDensityInCurResolution() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "PhyWidth: " << screenProperty.GetPhyWidth() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "PhyHeight: " << screenProperty.GetPhyHeight() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "RefreshRate: " << screenProperty.GetRefreshRate() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "VirtualPixelRatio: " + << screenProperty.GetVirtualPixelRatio() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "ScreenRotation: " + << static_cast(screenProperty.GetRotation()) << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "Orientation: " + << static_cast(screenProperty.GetOrientation()) << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "DisplayOrientation: " + << static_cast(screenProperty.GetDisplayOrientation()) << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "GetScreenType: " + << static_cast(screenProperty.GetScreenType()) << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "ReqOrientation: " + << static_cast(screenProperty.GetScreenRequestedOrientation()) << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "DPI: " << screenProperty.GetXDpi() + << ", " << screenProperty.GetYDpi() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "Offset: " << screenProperty.GetOffsetX() + << ", " << screenProperty.GetOffsetY() << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "Bounds: " + << screenProperty.GetBounds().rect_.GetLeft() << ", " + << screenProperty.GetBounds().rect_.GetTop() << ", " + << screenProperty.GetBounds().rect_.GetWidth() << ", " + << screenProperty.GetBounds().rect_.GetHeight() << ", " << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "PhyBounds: " + << screenProperty.GetPhyBounds().rect_.GetLeft() << ", " + << screenProperty.GetPhyBounds().rect_.GetTop() << ", " + << screenProperty.GetPhyBounds().rect_.GetWidth() << ", " + << screenProperty.GetPhyBounds().rect_.GetHeight() << ", " << std::endl; + oss << std::left << std::setw(LINE_WIDTH) << "AvailableArea " + << screenProperty.GetAvailableArea().posX_ << ", " + << screenProperty.GetAvailableArea().posY_ << ", " + << screenProperty.GetAvailableArea().width_ << ", " + << screenProperty.GetAvailableArea().height_ << ", " << std::endl; + dumpInfo_.append(oss.str()); +} +} // Rosen +} // OHOS \ No newline at end of file diff --git a/window_scene/session_manager/src/screen_session_manager.cpp b/window_scene/session_manager/src/screen_session_manager.cpp index b92bff948e..d5769088f0 100644 --- a/window_scene/session_manager/src/screen_session_manager.cpp +++ b/window_scene/session_manager/src/screen_session_manager.cpp @@ -42,6 +42,7 @@ #include "screen_rotation_property.h" #include "screen_sensor_connector.h" #include "screen_setting_helper.h" +#include "screen_session_dumper.h" #include "mock_session_manager_service.h" namespace OHOS::Rosen { @@ -3289,12 +3290,13 @@ static std::string Str16ToStr8(const std::u16string& str) int ScreenSessionManager::Dump(int fd, const std::vector& args) { WLOGFI("Dump begin"); - if (fd < 0) { + sptr dumper = new ScreenSessionDumper(fd, args); + if (dumper == nullptr) { + WLOGFE("dumper is nullptr"); return -1; } - (void) signal(SIGPIPE, SIG_IGN); // ignore SIGPIPE crash - UniqueFd ufd = UniqueFd(fd); // auto close - fd = ufd.Get(); + dumper->ExcuteDumpCmd(); + std::vector params; for (auto& arg : args) { params.emplace_back(Str16ToStr8(arg)); @@ -3325,11 +3327,6 @@ int ScreenSessionManager::Dump(int fd, const std::vector& args) ShowIllegalArgsInfo(dumpInfo); } } - int ret = dprintf(fd, "%s\n", dumpInfo.c_str()); - if (ret < 0) { - WLOGFE("dprintf error"); - return -1; // WMError::WM_ERROR_INVALID_OPERATION; - } WLOGI("dump end"); return 0; } diff --git a/window_scene/test/unittest/BUILD.gn b/window_scene/test/unittest/BUILD.gn index 326a754ecd..e5864917ea 100644 --- a/window_scene/test/unittest/BUILD.gn +++ b/window_scene/test/unittest/BUILD.gn @@ -500,6 +500,19 @@ ohos_unittest("ws_screen_rotation_property_test") { } } +ohos_unittest("ws_screen_session_dumper_test") { + module_out_path = module_out_path + + sources = [ "ws_screen_session_dumper_test.cpp" ] + + deps = [ ":ws_unittest_common" ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] +} + ohos_unittest("ws_move_drag_controller_test") { module_out_path = module_out_path diff --git a/window_scene/test/unittest/screen_session_dumper_test.cpp b/window_scene/test/unittest/screen_session_dumper_test.cpp new file mode 100644 index 0000000000..c6daf4c2d3 --- /dev/null +++ b/window_scene/test/unittest/screen_session_dumper_test.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2024 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 + +#include "screen_session_dumper.h" +#include "screen_session_manager.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace Rosen { +class ScreenSessionDumperTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; +}; + +void ScreenSessionDumperTest::SetUpTestCase() +{ +} + +void ScreenSessionDumperTest::TearDownTestCase() +{ +} + +void ScreenSessionDumperTest::SetUp() +{ +} + +void ScreenSessionDumperTest::TearDown() +{ +} + +namespace { +/** + * @tc.name: Dump01 + * @tc.desc: Dump + * @tc.type: FUNC + */ +HWTEST_F(ScreenSessionDumperTest, Dump01, Function | SmallTest | Level1) +{ + int fd = 1; + std::vector args; + sptr dumper = new ScreenSessionDumper(fd, args); + ASSERT_NE(nullptr, dumper); +} + +/** + * @tc.name: Dump02 + * @tc.desc: Dump input for -h + * @tc.type: FUNC + */ +HWTEST_F(ScreenSessionDumperTest, Dump02, Function | SmallTest | Level1) +{ + int fd = 1; + std::vector args = {u"-h"}; + sptr dumper = new ScreenSessionDumper(fd, args); + dumper->ExcuteDumpCmd(); + ASSERT_TRUE(true); +} + +/** + * @tc.name: Dump03 + * @tc.desc: Dump input for -a + * @tc.type: FUNC + */ +HWTEST_F(ScreenSessionDumperTest, Dump03, Function | SmallTest | Level1) +{ + int fd = 1; + std::vector args = {u"-a"}; + sptr dumper = new ScreenSessionDumper(fd, args); + dumper->ExcuteDumpCmd(); + ASSERT_TRUE(true); +} + +/** + * @tc.name: Dump04 + * @tc.desc: Dump input for abnormal + * @tc.type: FUNC + */ +HWTEST_F(ScreenSessionDumperTest, Dump04, Function | SmallTest | Level1) +{ + int fd = 1; + std::vector args = {u"-abnormal"}; + sptr dumper = new ScreenSessionDumper(fd, args); + dumper->ExcuteDumpCmd(); + ASSERT_TRUE(true); +} + +/** + * @tc.name: Dump05 + * @tc.desc: Dump fd less 0 + * @tc.type: FUNC + */ +HWTEST_F(ScreenSessionDumperTest, Dump05, Function | SmallTest | Level1) +{ + int fd = -1; + std::vector args = {u"-h"}; + sptr dumper = new ScreenSessionDumper(fd, args); + dumper->ExcuteDumpCmd(); + ASSERT_TRUE(true); +} + +} + +} +} \ No newline at end of file -- Gitee