4#include <QtCore/private/qcore_ohos_p.h>
5#include <QtCore/qdebug.h>
6#include <QtCore/qdatetime.h>
8#include <qpa/qwindowsysteminterface.h>
10#include <qohosdisplayinfo.h>
17#include "render/qohosview.h"
19#include <multimedia/image_framework/image/pixelmap_native.h>
21#include <qarkui/qarkuiutils.h>
22#include <qohosapppermissions_p.h>
23#include <qohosjsutils.h>
24#include <QtGui/private/qohosimageconversions_p.h>
25#include <qohospixelmapconversions.h>
26#include <qohosutils.h>
27#include <render/qwindowproxyregistry.h>
28#include <window_manager/oh_display_capture.h>
29#include <window_manager/oh_display_manager.h>
31#include <QtGui/qguiapplication.h>
32#include <QtGui/qwindow.h>
33#include <QtCore/private/qnapi_p.h>
34#include <QtCore/private/qnumeric_p.h>
35#include <QtCore/private/qohoslogger_p.h>
36#include <QtGui/private/qwindow_p.h>
49 ::OH_PixelmapNative *pixelMapNativePtr;
51 QArkUi::callArkUiOrFailOnErrorResult(
52 Q_OHOS_NAMED_FUNC(::OH_NativeDisplayManager_CaptureScreenPixelmap),
53 static_cast<
std::uint32_t>(displayId.value()), &pixelMapNativePtr);
55 return wrapOhosNativePixelMapPtr(pixelMapNativePtr);
60 QOhosConsumer<std::shared_ptr<::OH_PixelmapNative>> pixelMapOrNullConsumer)
62 static constexpr const char *ohosCustomScreenCapturePermission =
63 "ohos.permission.CUSTOM_SCREEN_CAPTURE";
66 jsState, ohosCustomScreenCapturePermission,
67 [pixelMapOrNullConsumer = std::move(pixelMapOrNullConsumer), displayId](
68 auto &jsState,
bool permissionGranted) {
69 if (permissionGranted) {
70 pixelMapOrNullConsumer(captureScreenPixelmap(jsState, displayId));
73 "%s: %s hasn't been granted by user. Cannot grab window.", Q_FUNC_INFO,
74 ohosCustomScreenCapturePermission);
75 pixelMapOrNullConsumer(
nullptr);
82 auto allWindows = qApp->allWindows();
83 auto windowItByWId =
std::find_if(
84 std::begin(allWindows),
std::end(allWindows),
86 return w->winId() == wId;
89 if (windowItByWId == allWindows.end())
92 return *windowItByWId;
96 const QSize &windowSnapshotSize,
const QRect &windowSpaceCaptureRect)
99 windowSpaceCaptureRect.width() < 0
100 ? qMax(0, windowSnapshotSize.width() - windowSpaceCaptureRect.x())
101 : windowSpaceCaptureRect.width(),
102 windowSpaceCaptureRect.height() < 0
103 ? qMax(0, windowSnapshotSize.height() - windowSpaceCaptureRect.y())
104 : windowSpaceCaptureRect.height()
109 const QRect &windowSpaceCaptureRect,
const QRect &windowFragmentRect)
112 qAbs(windowSpaceCaptureRect.x() - windowFragmentRect.x()),
113 qAbs(windowSpaceCaptureRect.y() - windowFragmentRect.y()),
114 windowFragmentRect.width(),
115 windowFragmentRect.height()
120 const QPixmap &capturedWindowPixmap,
const QRect &windowSpaceCaptureRect)
122 auto resultSize = calculateResultWindowCapturePixmapSize(
123 capturedWindowPixmap.size(), windowSpaceCaptureRect);
125 if (resultSize.isEmpty())
128 auto windowFragmentRect = capturedWindowPixmap.rect().intersected(
129 QRect(windowSpaceCaptureRect.topLeft(), resultSize));
131 if (windowFragmentRect.isEmpty())
132 return QPixmap(resultSize);
134 auto resultSpaceWindowFragmentRect =
135 calculateResultPixmapWindowFragmentRect(windowSpaceCaptureRect, windowFragmentRect);
137 QPixmap result(resultSize);
139 p.drawPixmap(resultSpaceWindowFragmentRect, capturedWindowPixmap, windowFragmentRect);
148 switch (jsDisplayOrientation) {
149 case JsDisplayOrientation::PORTRAIT:
150 return Qt::ScreenOrientation::PortraitOrientation;
151 case JsDisplayOrientation::LANDSCAPE:
152 return Qt::ScreenOrientation::LandscapeOrientation;
153 case JsDisplayOrientation::PORTRAIT_INVERTED:
154 return Qt::ScreenOrientation::InvertedPortraitOrientation;
155 case JsDisplayOrientation::LANDSCAPE_INVERTED:
156 return Qt::ScreenOrientation::InvertedLandscapeOrientation;
165 switch (orientation) {
166 case Qt::ScreenOrientation::PortraitOrientation:
168 case Qt::ScreenOrientation::LandscapeOrientation:
170 case Qt::ScreenOrientation::InvertedPortraitOrientation:
172 case Qt::ScreenOrientation::InvertedLandscapeOrientation:
182 switch (nativeOrientation) {
183 case DISPLAY_MANAGER_PORTRAIT:
184 return Qt::ScreenOrientation::PortraitOrientation;
185 case DISPLAY_MANAGER_LANDSCAPE:
186 return Qt::ScreenOrientation::LandscapeOrientation;
187 case DISPLAY_MANAGER_PORTRAIT_INVERTED:
188 return Qt::ScreenOrientation::InvertedPortraitOrientation;
189 case DISPLAY_MANAGER_LANDSCAPE_INVERTED:
190 return Qt::ScreenOrientation::InvertedLandscapeOrientation;
191 case DISPLAY_MANAGER_UNKNOWN:
200 switch (((index % 4) + 4) % 4) {
202 return Qt::ScreenOrientation::PortraitOrientation;
204 return Qt::ScreenOrientation::LandscapeOrientation;
206 return Qt::ScreenOrientation::InvertedPortraitOrientation;
208 return Qt::ScreenOrientation::InvertedLandscapeOrientation;
217 , m_format(QImage::Format_ARGB32_Premultiplied)
219 , m_platformCursor(
new QOhosPlatformCursor())
220 , m_displayInfo(displayInfo)
221 , m_availableGeometry(getAvailableArea())
222 , m_platformScreenListSupplier(std::move(platformScreenListSupplier))
224 auto __dbg = make_QCScopedDebug(
"QOhosPlatformScreen::QOhosPlatformScreen");
231 auto *platformScreen = screen->handle();
232 if (Q_UNLIKELY(platformScreen ==
nullptr))
233 qOhosReportFatalErrorAndAbort(
"QScreen::handle() returned null");
239 for (
auto *platformScreen : virtualSiblings()) {
240 auto *ohosPlatformScreen =
static_cast<QOhosPlatformScreen *>(platformScreen);
242 auto windowIds = QOhosWindowProxy::queryWindowIdsByCoordinate(ohosPlatformScreen->m_displayInfo.id, p);
243 if (!windowIds.empty())
244 return QWindowProxyRegistry::instance().findQWindowByJsWindowIdOrNull(windowIds.front());
252 return m_platformCursor.data();
257 auto geometryChanged = displayInfo.displayGeometryPixels() != m_displayInfo.displayGeometryPixels();
258 auto logicalDpiChanged = displayInfo
.densityDPI != m_displayInfo.densityDPI;
259 const Qt::ScreenOrientation previousOrientation = orientation();
261 QVector<QPair<QWindow *, std::optional<QRect>>> windowRectPairs;
263 if (logicalDpiChanged) {
264 for (
auto *window : qGuiApp->allWindows()) {
265 auto *platformWindow = QOhosPlatformWindow::fromQWindowOrNull(window);
266 if (platformWindow ==
nullptr || platformWindow->screen() !=
this || !window->isVisible())
269 auto *ownedView = platformWindow->ownedViewOrNull();
270 if (ownedView ==
nullptr)
273 windowRectPairs.push_back({
275 ownedView->viewType() == QOhosView::ViewType::EmbeddedWindow
276 ? std::optional(window->geometry())
281 m_displayInfo = displayInfo;
284 QWindowSystemInterface::handleScreenGeometryChange(QPlatformScreen::screen(), geometry(), availableGeometry());
286 if (orientation() != previousOrientation)
287 QWindowSystemInterface::handleScreenOrientationChange(QPlatformScreen::screen(), orientation());
289 if (logicalDpiChanged) {
290 auto ldpi = logicalDpi();
291 QWindowSystemInterface::handleScreenLogicalDotsPerInchChange(
292 screen(), ldpi.first, ldpi.second);
294 QWindowSystemInterface::flushWindowSystemEvents();
296 for (
const auto &windowRectPair : windowRectPairs) {
297 auto *qWindow = windowRectPair.first;
298 auto *platformWindow = QOhosPlatformWindow::fromQWindow(qWindow);
299 if (windowRectPair.second.has_value())
300 qWindow->setGeometry(windowRectPair.second.value());
301 platformWindow->handleDpiChange();
308 qreal lDpi = m_displayInfo.densityPixels * ohosLogicalDpi;
309 return QDpi(lDpi, lDpi);
316 return m_displayInfo.densityPixels;
321 return m_displayInfo.refreshRate;
332 qAndThen(m_displayInfo.orientation, tryMapJsDisplayOrientationToQt)
333 .value_or(Qt::ScreenOrientation::PrimaryOrientation);
340 if (m_nativeOrientation)
341 return *m_nativeOrientation;
343 NativeDisplayManager_DisplayInfo *displayInfo =
nullptr;
344 const auto displayId =
static_cast<uint32_t>(m_displayInfo.id.value());
345 if (OH_NativeDisplayManager_CreateDisplayById(displayId, &displayInfo) != DISPLAY_MANAGER_OK
346 || displayInfo ==
nullptr) {
347 return Qt::ScreenOrientation::PrimaryOrientation;
350 const NativeDisplayManager_Orientation nativeOrientation = displayInfo->orientation;
351 const NativeDisplayManager_Rotation rotation = displayInfo->rotation;
352 OH_NativeDisplayManager_DestroyDisplay(displayInfo);
354 const auto current = tryMapNativeDisplayOrientationToQt(nativeOrientation);
356 return Qt::ScreenOrientation::PrimaryOrientation;
358 const int currentIndex = orientationToIndex(*current);
359 if (currentIndex < 0)
360 return Qt::ScreenOrientation::PrimaryOrientation;
362 m_nativeOrientation = orientationFromIndex(currentIndex -
static_cast<
int>(rotation));
363 return *m_nativeOrientation;
368 return QOhosJsThreadGateway::evalWithPromise<QRect>(
369 [&](QOhosJsState &jsState, QOhosTaskPromise<QRect> evalPromise) {
371 QNapi::Object display;
373 display = jsState.eval<QNapi::Object>(
374 "@ohos.display.getDisplayByIdSync(*)", {m_displayInfo.id.value()});
375 }
catch (
const Napi::Error &error) {
377 "%s: getDisplayByIdSync(%f) failed with error: %s",
378 Q_FUNC_INFO, m_displayInfo.id.value(), error.Message().c_str());
379 evalPromise(QRect());
383 auto thenCatchPromises = std::move(evalPromise).makeThenCatchBranches(Q_FUNC_INFO);
384 display.evalToPromiseOrRejectOnThrow(
"getAvailableArea()")
386 [thenPromise = std::move(thenCatchPromises.first)](
const QOhosCallbackInfo &cbInfo) {
387 auto availableArea = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
390 availableArea.get<QNapi::Number>(
"left"),
391 availableArea.get<QNapi::Number>(
"top"),
392 availableArea.get<QNapi::Number>(
"width"),
393 availableArea.get<QNapi::Number>(
"height")));
396 [catchPromise = std::move(thenCatchPromises.second)](
const QOhosCallbackInfo &cbInfo) {
397 QtOhos::logJsCallbackError(cbInfo,
"Error occurred in JS getAvailableArea()");
398 catchPromise(QRect());
410 return m_displayInfo;
415 const auto jsDisplayId = m_displayInfo.id.value();
416 qint64 displayId = 0;
417 if (!convertDoubleTo(jsDisplayId, &displayId))
418 qOhosReportFatalErrorAndAbort(
"%s: display ID %g is out of the qint64 range", Q_FUNC_INFO, jsDisplayId);
424 return m_displayInfo.displayGeometryPixels();
429 auto screenGeometry = geometry();
430 return !m_availableGeometry.isEmpty()
431 ? m_availableGeometry.translated(screenGeometry.topLeft())
447 return m_displayInfo.physicalSize();
452 if (rect != m_availableGeometry) {
453 m_availableGeometry = rect;
454 QWindowSystemInterface::handleScreenGeometryChange(QPlatformScreen::screen(), geometry(), availableGeometry());
460 if (!m_displayInfo.isDisplayMainOrExtended())
461 return QPlatformScreen::virtualSiblings();
463 QList<QPlatformScreen *> result;
464 auto allPlatformScreens = m_platformScreenListSupplier();
466 allPlatformScreens.begin(), allPlatformScreens.end(),
467 std::back_inserter(result),
477 auto captureRect = QRect(x, y, width, height);
480 auto *window = tryFindWindowByWIdOrNull(wId);
481 if (window ==
nullptr) {
483 "%s: Cannot find window with the given WId: %lld.", Q_FUNC_INFO, wId);
486 auto *platformWindow = QOhosPlatformWindow::fromQWindow(window);
487 auto *view = platformWindow->ownedViewOrNull();
490 const QRect content = view->nodeScreenGeometryPixels();
491 const QRect frame = view->viewGeometry().frameGeometry;
492 if (content.isEmpty() || !frame.contains(content))
494 const QRect contentRect(content.topLeft() - frame.topLeft(), content.size());
495 return grabWindowFromCapturedScreenPixmap(
496 platformWindow->makeSnapshot().copy(contentRect), captureRect);
499 auto capturedScreenPixmap = QOhosJsThreadGateway::evalWithPromise<QPixmap>(
500 [displayId = m_displayInfo.id](
501 QOhosJsState &jsState, QOhosTaskPromise<QPixmap> evalPromise) {
502 auto sharedEvalPromise = QtOhos::moveToSharedPtr(std::move(evalPromise).makeChained(Q_FUNC_INFO));
503 tryCaptureScreenPixelmapWithPermissionCheck(
506 std::shared_ptr<::OH_PixelmapNative> optPixelMap) {
507 (*sharedEvalPromise)(
509 ? QPixmap::fromImage(createQImageFromNativePixelMap(optPixelMap.get()))
515 return capturedScreenPixmap.copy(captureRect);
520 return m_displayInfo.name;
void checkAppPermissionGrantedWithConsumer(QOhosJsState &jsState, const std::string &permissionName, QOhosConsumer< QOhosJsState &, bool > resultConsumer)
Combined button and popup list for selecting options.
Qt::ScreenOrientation orientationFromIndex(int index)
QWindow * tryFindWindowByWIdOrNull(WId wId)
QPixmap grabWindowFromCapturedScreenPixmap(const QPixmap &capturedWindowPixmap, const QRect &windowSpaceCaptureRect)
QRect calculateResultPixmapWindowFragmentRect(const QRect &windowSpaceCaptureRect, const QRect &windowFragmentRect)
std::optional< Qt::ScreenOrientation > tryMapJsDisplayOrientationToQt(QOhosDisplayInfo::JsDisplayOrientation jsDisplayOrientation)
std::shared_ptr<::OH_PixelmapNative > captureScreenPixelmap(QOhosJsState &, QOhosDisplayInfo::JsDisplayId displayId)
static const int ohosLogicalDpi
void tryCaptureScreenPixelmapWithPermissionCheck(QOhosJsState &jsState, QOhosDisplayInfo::JsDisplayId displayId, QOhosConsumer< std::shared_ptr<::OH_PixelmapNative > > pixelMapOrNullConsumer)
std::optional< Qt::ScreenOrientation > tryMapNativeDisplayOrientationToQt(NativeDisplayManager_Orientation nativeOrientation)
int orientationToIndex(Qt::ScreenOrientation orientation)
QSize calculateResultWindowCapturePixmapSize(const QSize &windowSnapshotSize, const QRect &windowSpaceCaptureRect)
bool isDisplayMainOrExtended() const
QtOhosQpa::enums::ohos::display::Orientation JsDisplayOrientation