From 154e572c84ebe248df6bdb200530f3a2065829be Mon Sep 17 00:00:00 2001 From: Woochan Lee Date: Mon, 3 Apr 2023 16:56:07 +0900 Subject: [PATCH] libaurum: Add option to receive pixel data of screenshot image To provide past performance in case that pixel data is needed without stroing image Remove unnecessary params such as scale, quality. Change-Id: Ibc6db0b33375a8597b576ba410eb520126335ef5 --- libaurum/inc/Impl/MockDeviceImpl.h | 4 +- libaurum/inc/Impl/TizenDeviceImpl.h | 2 +- libaurum/inc/Interface/IDevice.h | 6 +- libaurum/inc/UiDevice.h | 8 +-- libaurum/src/Impl/MockDeviceImpl.cc | 2 +- libaurum/src/Impl/TizenDeviceImpl.cc | 29 +++++++-- libaurum/src/UiDevice.cc | 6 +- .../src/Commands/TakeScreenshotCommand.cc | 63 ++++++++++++------- protocol/aurum.proto | 1 + 9 files changed, 79 insertions(+), 42 deletions(-) diff --git a/libaurum/inc/Impl/MockDeviceImpl.h b/libaurum/inc/Impl/MockDeviceImpl.h index e7ef2ce..4631912 100644 --- a/libaurum/inc/Impl/MockDeviceImpl.h +++ b/libaurum/inc/Impl/MockDeviceImpl.h @@ -157,9 +157,9 @@ public: /** * @brief TBD - * @since_tizen 6.5 + * @since_tizen 7.0 */ - bool takeScreenshot(std::string path, float scale, int quality) override; + bool takeScreenshot(std::string path, bool asPixels, void **pixels) override; /** * @brief TBD diff --git a/libaurum/inc/Impl/TizenDeviceImpl.h b/libaurum/inc/Impl/TizenDeviceImpl.h index 1dd946c..774841c 100644 --- a/libaurum/inc/Impl/TizenDeviceImpl.h +++ b/libaurum/inc/Impl/TizenDeviceImpl.h @@ -119,7 +119,7 @@ public: /** * @copydoc IDevice::takeScreenshot() */ - bool takeScreenshot(std::string path, float scale, int quality) override; + bool takeScreenshot(std::string path, bool asPixels, void **pixels) override; /** * @copydoc IDevice::getSystemTime() diff --git a/libaurum/inc/Interface/IDevice.h b/libaurum/inc/Interface/IDevice.h index 23058c6..e501b7e 100644 --- a/libaurum/inc/Interface/IDevice.h +++ b/libaurum/inc/Interface/IDevice.h @@ -262,14 +262,14 @@ public: * @brief Take a screenshot of current window and store it as image file. * * @param[in] path where the image file should be written to - * @param[in] scale scale the screenshot down if needed; 1.0f for original size - * @param[in] quality quality of the PNG compression; range: 0-100 + * @param[in] asPixels asPixels wheter to receive screen pixel data + * @param[in] pixels pixel pixel buffer data pointer * * @return true if screen shot is created successfully, false otherwise * * @since_tizen 6.5 */ - virtual bool takeScreenshot(std::string path, float scale, int quality) = 0; + virtual bool takeScreenshot(std::string path, bool asPixels, void **pixels) = 0; /** * @brief Gets device system time. diff --git a/libaurum/inc/UiDevice.h b/libaurum/inc/UiDevice.h index 46a43f2..1c6a49b 100644 --- a/libaurum/inc/UiDevice.h +++ b/libaurum/inc/UiDevice.h @@ -253,14 +253,14 @@ public: * @brief Take a screenshot of current window and store it as image file. * * @param[in] path where the image file should be written to - * @param[in] scale scale the screenshot down if needed; 1.0f for original size - * @param[in] quality quality of the PNG compression; range: 0-100 + * @param[in] asPixels asPixels wheter to receive screen pixel data + * @param[in] pixels pixel pixel buffer data pointer * * @return true if screen shot is created successfully, false otherwise * - * @since_tizen 6.5 + * @since_tizen 7.0 */ - bool takeScreenshot(std::string path, float scale, int quality) override; + bool takeScreenshot(std::string path, bool asPixels, void **pixels) override; /** * @brief Gets device system time. diff --git a/libaurum/src/Impl/MockDeviceImpl.cc b/libaurum/src/Impl/MockDeviceImpl.cc index 4820f11..3d3ea8f 100644 --- a/libaurum/src/Impl/MockDeviceImpl.cc +++ b/libaurum/src/Impl/MockDeviceImpl.cc @@ -195,7 +195,7 @@ bool MockDeviceImpl::repeatKeyCode(std::string keycode, int intervalMs, int dura return false; } -bool MockDeviceImpl::takeScreenshot(std::string path, float scale, int quality) +bool MockDeviceImpl::takeScreenshot(std::string path, bool asPixels, void **pixels) { return true; } diff --git a/libaurum/src/Impl/TizenDeviceImpl.cc b/libaurum/src/Impl/TizenDeviceImpl.cc index 819ec1b..273bd9c 100644 --- a/libaurum/src/Impl/TizenDeviceImpl.cc +++ b/libaurum/src/Impl/TizenDeviceImpl.cc @@ -299,19 +299,40 @@ bool TizenDeviceImpl::releaseKeyCode(std::string keycode) return result == EFL_UTIL_ERROR_NONE; } -bool TizenDeviceImpl::takeScreenshot(std::string path, float scale, int quality) +bool TizenDeviceImpl::takeScreenshot(std::string path, bool asPixels, void **pixels) { efl_util_screenshot_h screenshot = NULL; tbm_surface_h tbm_surface = NULL; + void *ptr = NULL; + const int WIDTH = mScreenSize.width; + const int HEIGHT = mScreenSize.height; + CaptureMutex.lock(); - screenshot = efl_util_screenshot_initialize(mScreenSize.width, mScreenSize.height); + screenshot = efl_util_screenshot_initialize(WIDTH, HEIGHT); if (screenshot) { tbm_surface = efl_util_screenshot_take_tbm_surface(screenshot); if (tbm_surface) { - tdm_helper_dump_buffer(tbm_surface, path.c_str()); - sync(); + if (asPixels) { + tbm_surface_info_s info; + + unsigned char *src = NULL; + unsigned char *dst = NULL; + tbm_surface_map(tbm_surface, TBM_SURF_OPTION_READ, &info); + + ptr = malloc( WIDTH * HEIGHT * 4 ); + src = (unsigned char *)info.planes[0].ptr; + dst = (unsigned char *)ptr; + + memcpy(dst, src, WIDTH * HEIGHT * 4); + *pixels = ptr; + + tbm_surface_unmap(tbm_surface); + } else { + tdm_helper_dump_buffer(tbm_surface, path.c_str()); + sync(); + } tbm_surface_destroy(tbm_surface); } else { efl_util_screenshot_deinitialize(screenshot); diff --git a/libaurum/src/UiDevice.cc b/libaurum/src/UiDevice.cc index e6e24fe..1cac2bb 100644 --- a/libaurum/src/UiDevice.cc +++ b/libaurum/src/UiDevice.cc @@ -416,9 +416,9 @@ bool UiDevice::repeatKeyCode(std::string keycode, int intervalMs, int durationMs return result; } -bool UiDevice::takeScreenshot(std::string path, float scale, int quality) +bool UiDevice::takeScreenshot(std::string path, bool asPixels, void **pixels) { - return mDeviceImpl->takeScreenshot(path, scale, quality); + return mDeviceImpl->takeScreenshot(path, asPixels, pixels); } long long UiDevice::getSystemTime(TimeRequestType type) @@ -469,4 +469,4 @@ bool UiDevice::getWithScreenAnalyzer() bool UiDevice::registerCallback(const A11yEvent type, EventHandler cb, void *data) const { return AccessibleWatcher::getInstance()->registerCallback(type, cb, data); -} \ No newline at end of file +} diff --git a/org.tizen.aurum-bootstrap/src/Commands/TakeScreenshotCommand.cc b/org.tizen.aurum-bootstrap/src/Commands/TakeScreenshotCommand.cc index 5825eef..a536684 100644 --- a/org.tizen.aurum-bootstrap/src/Commands/TakeScreenshotCommand.cc +++ b/org.tizen.aurum-bootstrap/src/Commands/TakeScreenshotCommand.cc @@ -30,36 +30,51 @@ TakeScreenshotCommand::TakeScreenshotCommand( ::grpc::Status TakeScreenshotCommand::execute() { - LOGI("TakeScreenshot --------------- "); + LOGI("TakeScreenshot (getPixels : %d)--------------- ", mRequest->getpixels()); - struct tm timeinfo; - time_t now = time(0); - if (!localtime_r(&now, &timeinfo)) { - LOGE("fail to get localtime. Screenshot cancelled"); - return grpc::Status::CANCELLED; - } + if (!mRequest->getpixels()) { + struct tm timeinfo; + time_t now = time(0); + if (!localtime_r(&now, &timeinfo)) { + LOGE("fail to get localtime. Screenshot cancelled"); + return grpc::Status::CANCELLED; + } + + char name[128]; + std::snprintf(name, 128, "/tmp/screenshot-%d-%d-%d-%d:%d:%d.png", + (timeinfo.tm_year + 1900), (timeinfo.tm_mon + 1), timeinfo.tm_mday, + timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); + std::string path(name); + std::shared_ptr mDevice = UiDevice::getInstance(); + mDevice->takeScreenshot(path, false, NULL); - char name[128]; - std::snprintf(name, 128, "/tmp/screenshot-%d-%d-%d-%d:%d:%d.png", - (timeinfo.tm_year + 1900), (timeinfo.tm_mon + 1), timeinfo.tm_mday, - timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); - std::string path(name); - std::shared_ptr mDevice = UiDevice::getInstance(); - mDevice->takeScreenshot(path, 1.0, 1); + std::ifstream ifs(path, std::ifstream::binary); + ::aurum::RspTakeScreenshot rsp; + const Size2D screenSize = mDevice->getScreenSize(); + int size = screenSize.width * screenSize.height; + char *buf = new char[size]; - std::ifstream ifs(path, std::ifstream::binary); - ::aurum::RspTakeScreenshot rsp; - const Size2D screenSize = mDevice->getScreenSize(); - int size = screenSize.width * screenSize.height; - char *buf = new char[size]; + while (!ifs.eof()) { + ifs.read(buf, size); + rsp.set_image(buf, ifs.gcount()); + mWriter->Write(rsp); + } + ifs.close(); + delete[] buf; + } + else { + void *pixels = NULL; + std::shared_ptr mDevice = UiDevice::getInstance(); + mDevice->takeScreenshot("", true, &pixels); + ::aurum::RspTakeScreenshot rsp; + const Size2D screenSize = mDevice->getScreenSize(); + int size = screenSize.width * screenSize.height; - while (!ifs.eof()) { - ifs.read(buf, size); - rsp.set_image(buf, ifs.gcount()); + rsp.set_image(pixels, size * 4); mWriter->Write(rsp); + + free(pixels); } - ifs.close(); - delete[] buf; return grpc::Status::OK; } diff --git a/protocol/aurum.proto b/protocol/aurum.proto index 2f087d5..da7bee5 100644 --- a/protocol/aurum.proto +++ b/protocol/aurum.proto @@ -549,6 +549,7 @@ message RspKey{ } message ReqTakeScreenshot{ + bool getPixels = 1; } message RspTakeScreenshot{ bytes image = 1; -- 2.34.1