From a432b9378a6125d3614ea2a5f43f4210f77655e2 Mon Sep 17 00:00:00 2001 From: fanby01 Date: Mon, 10 Jan 2022 10:48:44 +0800 Subject: [PATCH] enable DisplayManager->GetScreenshot() Change-Id: Id958e75c3d338ab89d14436baba16bd5c0ed5003 Signed-off-by: fanby01 --- dm/include/display_manager_adapter.h | 3 +- dm/src/display_manager.cpp | 105 +++++++++++++------ dm/src/display_manager_adapter.cpp | 21 ++-- dmserver/include/abstract_display_manager.h | 4 +- dmserver/include/display_manager_interface.h | 10 +- dmserver/include/display_manager_proxy.h | 3 +- dmserver/include/display_manager_service.h | 3 +- dmserver/src/abstract_display_manager.cpp | 62 ++--------- dmserver/src/display_manager_proxy.cpp | 67 ++++++------ dmserver/src/display_manager_service.cpp | 13 ++- dmserver/src/display_manager_stub.cpp | 17 +-- interfaces/innerkits/BUILD.gn | 2 +- interfaces/innerkits/dm/display_manager.h | 16 ++- wm/BUILD.gn | 5 +- 14 files changed, 160 insertions(+), 171 deletions(-) diff --git a/dm/include/display_manager_adapter.h b/dm/include/display_manager_adapter.h index 53e1c8d30d..7037c76bc0 100644 --- a/dm/include/display_manager_adapter.h +++ b/dm/include/display_manager_adapter.h @@ -38,8 +38,7 @@ public: DisplayId CreateVirtualDisplay(const VirtualDisplayInfo &virtualDisplayInfo, sptr surface); bool DestroyVirtualDisplay(DisplayId displayId); - // TODO: fix me - // sptr GetDisplaySnapshot(DisplayId displayId); + sptr GetDisplaySnapshot(DisplayId displayId); void Clear(); private: DisplayManagerAdapter() = default; diff --git a/dm/src/display_manager.cpp b/dm/src/display_manager.cpp index 6ec0eb6910..a391e69742 100644 --- a/dm/src/display_manager.cpp +++ b/dm/src/display_manager.cpp @@ -40,40 +40,77 @@ const sptr DisplayManager::GetDisplayById(DisplayId displayId) return display; } -// TODO: fix me -// sptr DisplayManager::GetScreenshot(DisplayId displayId) -// { -// sptr screenShot = SingletonContainer::Get().GetDisplaySnapshot(displayId); -// if (screenShot == nullptr) { -// WLOGFE("DisplayManager::GetScreenshot failed!\n"); -// return nullptr; -// } - -// return screenShot; -// } - -// sptr DisplayManager::GetScreenshot(DisplayId displayId, const Media::Rect &rect, -// const Media::Size &size, int rotation) -// { -// sptr screenShot = SingletonContainer::Get().GetDisplaySnapshot(displayId); -// if (screenShot == nullptr) { -// WLOGFE("DisplayManager::GetScreenshot failed!\n"); -// return nullptr; -// } - -// // create crop dest pixelmap -// Media::InitializationOptions opt; -// opt.size.width = size.width; -// opt.size.height = size.height; -// opt.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE; -// opt.editable = false; -// opt.useSourceIfMatch = true; - -// auto dstScreenshot = Media::PixelMap::Create(*screenShot, rect, opt); -// sptr dstScreenshot_ = dstScreenshot.release(); - -// return dstScreenshot_; -// } +sptr DisplayManager::GetScreenshot(DisplayId displayId) +{ + if (displayId == DISPLAY_ID_INVALD) { + WLOGFE("displayId invalid!"); + return nullptr; + } + sptr screenShot = SingletonContainer::Get().GetDisplaySnapshot(displayId); + if (screenShot == nullptr) { + WLOGFE("DisplayManager::GetScreenshot failed!"); + return nullptr; + } + + return screenShot; +} + +bool DisplayManager::CheckRectOffsetValid(int32_t param) const +{ + if (param < 0 || param > MAX_RESOLUTION_VALUE) { + return false; + } + return true; +} + +bool DisplayManager::CheckRectSizeValid(int32_t param) const +{ + if (param < MIN_RESOLUTION_VALUE || param > MAX_RESOLUTION_VALUE) { + return false; + } + return true; +} + +sptr DisplayManager::GetScreenshot(DisplayId displayId, const Media::Rect &rect, + const Media::Size &size, int rotation) +{ + if (displayId == DISPLAY_ID_INVALD) { + WLOGFE("displayId invalid!"); + return nullptr; + } + if (!CheckRectOffsetValid(rect.left) || !CheckRectOffsetValid(rect.top) || + !CheckRectSizeValid(rect.width) || !CheckRectSizeValid(rect.height)) { + WLOGFE("rect invalid! left %{public}d, top %{public}d, w %{public}d, h %{public}d", + rect.left, rect.top, rect.width, rect.height); + return nullptr; + } + if (!CheckRectSizeValid(size.width) || !CheckRectSizeValid(size.height)) { + WLOGFE("size invalid! w %{public}d, h %{public}d", rect.width, rect.height); + return nullptr; + } + sptr screenShot = SingletonContainer::Get().GetDisplaySnapshot(displayId); + if (screenShot == nullptr) { + WLOGFE("DisplayManager::GetScreenshot failed!"); + return nullptr; + } + + // create crop dest pixelmap + Media::InitializationOptions opt; + opt.size.width = size.width; + opt.size.height = size.height; + opt.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE; + opt.editable = false; + opt.useSourceIfMatch = true; + + auto pixelMap = Media::PixelMap::Create(*screenShot, rect, opt); + if (pixelMap == nullptr) { + WLOGFE("Media::PixelMap::Create failed!"); + return nullptr; + } + sptr dstScreenshot = pixelMap.release(); + + return dstScreenshot; +} const sptr DisplayManager::GetDefaultDisplay() { diff --git a/dm/src/display_manager_adapter.cpp b/dm/src/display_manager_adapter.cpp index e9d4949d1a..1aa9a4c1ae 100644 --- a/dm/src/display_manager_adapter.cpp +++ b/dm/src/display_manager_adapter.cpp @@ -64,20 +64,19 @@ sptr DisplayManagerAdapter::GetDisplayById(DisplayId displayId) return display; } -// TODO: fix me -// sptr DisplayManagerAdapter::GetDisplaySnapshot(DisplayId displayId) -// { -// std::lock_guard lock(mutex_); +sptr DisplayManagerAdapter::GetDisplaySnapshot(DisplayId displayId) +{ + std::lock_guard lock(mutex_); -// if (!InitDMSProxyLocked()) { -// WLOGFE("displayManagerAdapter::GetDisplaySnapshot: InitDMSProxyLocked failed!"); -// return nullptr; -// } + if (!InitDMSProxyLocked()) { + WLOGFE("displayManagerAdapter::GetDisplaySnapshot: InitDMSProxyLocked failed!"); + return nullptr; + } -// sptr dispalySnapshot = displayManagerServiceProxy_->GetDispalySnapshot(displayId); + sptr dispalySnapshot = displayManagerServiceProxy_->GetDispalySnapshot(displayId); -// return dispalySnapshot; -// } + return dispalySnapshot; +} DisplayId DisplayManagerAdapter::CreateVirtualDisplay(const VirtualDisplayInfo &virtualDisplayInfo, sptr surface) diff --git a/dmserver/include/abstract_display_manager.h b/dmserver/include/abstract_display_manager.h index e18dbbaa17..d2a7852dc2 100644 --- a/dmserver/include/abstract_display_manager.h +++ b/dmserver/include/abstract_display_manager.h @@ -17,6 +17,7 @@ #define FOUNDATION_DMSERVER_ABSTRACT_DISPLAY_MANAGER_H #include +#include #include #include "abstract_display.h" @@ -34,8 +35,7 @@ public: RSScreenModeInfo GetScreenActiveMode(ScreenId id); ScreenId CreateVirtualScreen(const VirtualDisplayInfo &virtualDisplayInfo, sptr surface); bool DestroyVirtualScreen(ScreenId screenId); - // TODO: fix me - // sptr GetScreenSnapshot(ScreenId screenId); + sptr GetScreenSnapshot(ScreenId screenId); private: AbstractDisplayManager(); diff --git a/dmserver/include/display_manager_interface.h b/dmserver/include/display_manager_interface.h index aff799ad33..c7a73f6abb 100644 --- a/dmserver/include/display_manager_interface.h +++ b/dmserver/include/display_manager_interface.h @@ -17,13 +17,11 @@ #define FOUNDATION_DMSERVER_DISPLAY_MANAGER_INTERFACE_H #include - +#include #include #include "display_info.h" - #include "virtual_display_info.h" -// #include "pixel_map.h" namespace OHOS::Rosen { class IDisplayManager : public IRemoteBroker { @@ -35,8 +33,7 @@ public: TRANS_ID_GET_DISPLAY_BY_ID, TRANS_ID_CREATE_VIRTUAL_DISPLAY, TRANS_ID_DESTROY_VIRTUAL_DISPLAY, - // TODO: fix me - // TRANS_ID_GET_DISPLAY_SNAPSHOT, + TRANS_ID_GET_DISPLAY_SNAPSHOT, }; virtual DisplayId GetDefaultDisplayId() = 0; @@ -46,8 +43,7 @@ public: sptr surface) = 0; virtual bool DestroyVirtualDisplay(DisplayId displayId) = 0; - // TODO: fix me - // virtual sptr GetDispalySnapshot(DisplayId displayId) = 0; + virtual sptr GetDispalySnapshot(DisplayId displayId) = 0; }; } // namespace OHOS::Rosen diff --git a/dmserver/include/display_manager_proxy.h b/dmserver/include/display_manager_proxy.h index 141bb41156..ab8d6b24ab 100644 --- a/dmserver/include/display_manager_proxy.h +++ b/dmserver/include/display_manager_proxy.h @@ -33,8 +33,7 @@ public: DisplayId CreateVirtualDisplay(const VirtualDisplayInfo &virtualDisplayInfo, sptr surface) override; bool DestroyVirtualDisplay(DisplayId displayId) override; - // TODO: fix me - // sptr GetDispalySnapshot(DisplayId displayId) override; + sptr GetDispalySnapshot(DisplayId displayId) override; private: static inline BrokerDelegator delegator_; }; diff --git a/dmserver/include/display_manager_service.h b/dmserver/include/display_manager_service.h index 2a07d16bd5..0684e31432 100644 --- a/dmserver/include/display_manager_service.h +++ b/dmserver/include/display_manager_service.h @@ -43,8 +43,7 @@ public: DisplayId GetDefaultDisplayId() override; DisplayInfo GetDisplayInfoById(DisplayId displayId) override; - // TODO: fix me - // sptr GetDispalySnapshot(DisplayId displayId) override; + sptr GetDispalySnapshot(DisplayId displayId) override; private: DisplayManagerService(); ~DisplayManagerService() = default; diff --git a/dmserver/src/abstract_display_manager.cpp b/dmserver/src/abstract_display_manager.cpp index 06cadffaea..2ed3b24e26 100644 --- a/dmserver/src/abstract_display_manager.cpp +++ b/dmserver/src/abstract_display_manager.cpp @@ -24,8 +24,6 @@ namespace { constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0, "AbstractDisplayManager"}; } -#define SCREENSHOT_GENERATOR - AbstractDisplayManager::AbstractDisplayManager() : rsInterface_(&(RSInterfaces::GetInstance())) { parepareRSScreenManger(); @@ -79,58 +77,14 @@ bool AbstractDisplayManager::DestroyVirtualScreen(ScreenId screenId) return true; } -// TODO: fix me -// #ifdef SCREENSHOT_GENERATOR -// sptr TempCreatePixelMap() -// { -// // pixel_map testing code -// Media::InitializationOptions opt; -// opt.size.width = 1920; -// opt.size.height = 1080; -// opt.pixelFormat = Media::PixelFormat::RGBA_8888; -// opt.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE; -// opt.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE; -// opt.editable = false; -// opt.useSourceIfMatch = false; - -// const int bitmapDepth = 8; // color depth -// const int bpp = 4; // bytes per pixel -// const int maxByteNum = 256; - -// auto data = (uint32_t *)malloc(opt.size.width * opt.size.height * bpp); -// uint8_t *pic = (uint8_t *)data; -// for (uint32_t i = 0; i < opt.size.width; i++) { -// for (uint32_t j = 0; j < opt.size.height; j++) { -// for (uint32_t k = 0; k < bpp; k++) { -// pic[0] = rand() % maxByteNum; -// pic++; -// } -// } -// } -// uint32_t colorLen = opt.size.width * opt.size.height * bpp * bitmapDepth; -// auto newPixelMap = Media::PixelMap::Create(data, colorLen, opt); -// sptr pixelMap_ = newPixelMap.release(); -// if (pixelMap_ == nullptr) { -// WLOGFE("Failed to get pixelMap"); -// return nullptr; -// } - -// return pixelMap_; -// } -// #endif - -// sptr AbstractDisplayManager::GetScreenSnapshot(ScreenId screenId) -// { -// if (rsInterface_ == nullptr) { -// return nullptr; -// } +sptr AbstractDisplayManager::GetScreenSnapshot(ScreenId screenId) +{ + if (rsInterface_ == nullptr) { + return nullptr; + } -// #ifdef SCREENSHOT_GENERATOR -// sptr screenshot = TempCreatePixelMap(); -// #else -// sptr screenshot = rsInterface_->GetScreenSnapshot(screenId); -// #endif + sptr screenshot = nullptr; -// return screenshot; -// } + return screenshot; +} } // namespace OHOS::Rosen \ No newline at end of file diff --git a/dmserver/src/display_manager_proxy.cpp b/dmserver/src/display_manager_proxy.cpp index 53c3099428..5bc29b9384 100644 --- a/dmserver/src/display_manager_proxy.cpp +++ b/dmserver/src/display_manager_proxy.cpp @@ -135,38 +135,37 @@ bool DisplayManagerProxy::DestroyVirtualDisplay(DisplayId displayId) return reply.ReadBool(); } -// TODO: fix me -// sptr DisplayManagerProxy::GetDispalySnapshot(DisplayId displayId) -// { -// sptr remote = Remote(); -// if (remote == nullptr) { -// WLOGFW("GetDispalySnapshot: remote is nullptr"); -// return nullptr; -// } - -// MessageParcel data; -// MessageParcel reply; -// MessageOption option; -// if (!data.WriteInterfaceToken(GetDescriptor())) { -// WLOGFE("GetDispalySnapshot: WriteInterfaceToken failed"); -// return nullptr; -// } - -// if (!data.WriteUint64(displayId)) { -// WLOGFE("Write dispalyId failed"); -// return nullptr; -// } - -// if (remote->SendRequest(TRANS_ID_GET_DISPLAY_SNAPSHOT, data, reply, option) != ERR_NONE) { -// WLOGFW("GetDispalySnapshot: SendRequest failed"); -// return nullptr; -// } - -// sptr pixelMap = reply.ReadParcelable(); -// if (pixelMap == nullptr) { -// WLOGFW("DisplayManagerProxy::GetDispalySnapshot SendRequest nullptr."); -// return nullptr; -// } -// return pixelMap; -// } +sptr DisplayManagerProxy::GetDispalySnapshot(DisplayId displayId) +{ + sptr remote = Remote(); + if (remote == nullptr) { + WLOGFW("GetDispalySnapshot: remote is nullptr"); + return nullptr; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + WLOGFE("GetDispalySnapshot: WriteInterfaceToken failed"); + return nullptr; + } + + if (!data.WriteUint64(displayId)) { + WLOGFE("Write dispalyId failed"); + return nullptr; + } + + if (remote->SendRequest(TRANS_ID_GET_DISPLAY_SNAPSHOT, data, reply, option) != ERR_NONE) { + WLOGFW("GetDispalySnapshot: SendRequest failed"); + return nullptr; + } + + sptr pixelMap = reply.ReadParcelable(); + if (pixelMap == nullptr) { + WLOGFW("DisplayManagerProxy::GetDispalySnapshot SendRequest nullptr."); + return nullptr; + } + return pixelMap; +} } // namespace OHOS::Rosen \ No newline at end of file diff --git a/dmserver/src/display_manager_service.cpp b/dmserver/src/display_manager_service.cpp index ef9aff6428..d95ee09530 100644 --- a/dmserver/src/display_manager_service.cpp +++ b/dmserver/src/display_manager_service.cpp @@ -101,13 +101,12 @@ bool DisplayManagerService::DestroyVirtualDisplay(DisplayId displayId) return AbstractDisplayManager::GetInstance().DestroyVirtualScreen(screenId); } -// TODO: fix me -// sptr DisplayManagerService::GetDispalySnapshot(DisplayId displayId) -// { -// ScreenId screenId = GetScreenIdFromDisplayId(displayId); -// sptr screenSnapshot = AbstractDisplayManager::GetInstance().GetScreenSnapshot(screenId); -// return screenSnapshot; -// } +sptr DisplayManagerService::GetDispalySnapshot(DisplayId displayId) +{ + ScreenId screenId = GetScreenIdFromDisplayId(displayId); + sptr screenSnapshot = AbstractDisplayManager::GetInstance().GetScreenSnapshot(screenId); + return screenSnapshot; +} void DisplayManagerService::OnStop() { diff --git a/dmserver/src/display_manager_stub.cpp b/dmserver/src/display_manager_stub.cpp index 260a1416bd..90c35cdd42 100644 --- a/dmserver/src/display_manager_stub.cpp +++ b/dmserver/src/display_manager_stub.cpp @@ -62,14 +62,17 @@ int32_t DisplayManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, reply.WriteBool(result); break; } - // TODO: fix me - // case TRANS_ID_GET_DISPLAY_SNAPSHOT: { - // DisplayId displayId = data.ReadUint64(); + case TRANS_ID_GET_DISPLAY_SNAPSHOT: { + DisplayId displayId = data.ReadUint64(); - // sptr dispalySnapshot = GetDispalySnapshot(displayId); - // reply.WriteParcelable(dispalySnapshot.GetRefPtr()); - // break; - // } + sptr dispalySnapshot = GetDispalySnapshot(displayId); + if (dispalySnapshot == nullptr) { + reply.WriteParcelable(nullptr); + break; + } + reply.WriteParcelable(dispalySnapshot.GetRefPtr()); + break; + } default: WLOGFW("unknown transaction code"); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index 6c913281dd..e4ac6224b7 100644 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -9,7 +9,7 @@ # 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. +# limitations under the License. import("//build/ohos.gni") diff --git a/interfaces/innerkits/dm/display_manager.h b/interfaces/innerkits/dm/display_manager.h index 7db69d16d6..92bb9dc4b1 100644 --- a/interfaces/innerkits/dm/display_manager.h +++ b/interfaces/innerkits/dm/display_manager.h @@ -17,12 +17,12 @@ #define FOUNDATION_DM_DISPLAY_MANAGER_H #include +#include #include #include "display.h" #include "single_instance.h" #include "virtual_display_info.h" -// #include "pixel_map.h" // #include "wm_common.h" @@ -45,10 +45,16 @@ public: sptr surface, DisplayId displayIdToMirror, int32_t flags); bool DestroyVirtualDisplay(DisplayId displayId); - // TODO: fix me - // sptr GetScreenshot(DisplayId displayId); - // sptr GetScreenshot(DisplayId displayId, const Media::Rect &rect, - // const Media::Size &size, int rotation); + sptr GetScreenshot(DisplayId displayId); + sptr GetScreenshot(DisplayId displayId, const Media::Rect &rect, + const Media::Size &size, int rotation); + +private: + bool CheckRectOffsetValid(int32_t param) const; + bool CheckRectSizeValid(int32_t param) const; + + const int32_t MAX_RESOLUTION_VALUE = 15260; // max resolution, 16K + const int32_t MIN_RESOLUTION_VALUE = 16; // min resolution }; } // namespace OHOS::Rosen diff --git a/wm/BUILD.gn b/wm/BUILD.gn index 6d7293a9b2..3b813c46b4 100644 --- a/wm/BUILD.gn +++ b/wm/BUILD.gn @@ -86,6 +86,7 @@ ohos_shared_library("libwmutil") { "//foundation/graphic/standard:libsurface", "//foundation/graphic/standard/rosen/modules/render_service_base:librender_service_base", "//foundation/graphic/standard/rosen/modules/render_service_client:librender_service_client", + "//foundation/multimedia/image_standard/interfaces/innerkits:image_native", ] external_deps = [ @@ -128,11 +129,9 @@ ohos_shared_library("libwm") { "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//foundation/multimedia/image_standard/interfaces/innerkits:image_native", "//utils/native/base:utils", - # TODO: fix me - # "//foundation/multimedia/image_standard/interfaces/innerkits:image_native", - # weston adapter "//foundation/windowmanager/adapter:libwmadapter", -- Gitee