6#include "render/qohossurface.h"
7#include <native_buffer/native_buffer.h>
8#include <native_window/buffer_handle.h>
9#include <native_window/external_window.h>
10#include <qarkui/vsync.h>
11#include <qohosutils.h>
12#include <render/qohosview.h>
13#include <QtCore/private/qohoslogger_p.h>
14#include <QtCore/qendian.h>
30 std::int32_t bufferQueueSize;
31 auto getBufferQueueSizeResult = ::OH_NativeWindow_NativeWindowHandleOpt(
32 nativeWindow, ::NativeWindowOperation::GET_BUFFERQUEUE_SIZE, &bufferQueueSize);
34 qOhosReportFatalErrorAndAbort(
35 "%s: failed to get buffer queue size with error code: %d",
36 Q_FUNC_INFO, getBufferQueueSizeResult);
38 return bufferQueueSize;
43 const auto bitsPerPixel = image.depth();
44 const auto bytesPerPixel = bitsPerPixel / 8;
50 return QSpan(image.scanLine(y), image.width() * qImageBytesPerPixel(image));
55 const auto sizeToCopy =
std::min(srcRow.size(), dstRow.size());
56 std::memcpy(dstRow.data(), srcRow.data(), sizeToCopy);
60 QOhosPlatformBackingStore::QImageView srcImage, QImage &dstImage,
const QRegion ®ion,
const QPoint &dstOffset)
62 const auto intersectedRegion =
63 region.intersected(QRect({}, srcImage.size())).intersected(QRect({}, dstImage.size()));
65 for (
const auto &rect : intersectedRegion) {
66 const auto xSrc = rect.x() * srcImage.bytesPerPixel();
67 const auto widthSrc = rect.width() * srcImage.bytesPerPixel();
68 const auto xDst = (rect.x() + dstOffset.x()) * qImageBytesPerPixel(dstImage);
69 const auto widthDst = rect.width() * qImageBytesPerPixel(dstImage);
71 for (
int row = 0; row < rect.height(); ++row) {
72 const int srcY = rect.y() + row;
73 const int dstY = srcY + dstOffset.y();
74 const auto srcRow = srcImage.constScanLine(srcY).subspan(xSrc, widthSrc);
75 auto dstRow = qImageScanLine(dstImage, dstY).subspan(xDst, widthDst);
76 copyImageRow(srcRow, dstRow);
83 static const QColor debugBoxColor(
"#7F00FF00");
85 QPainter painter(&dstImage);
86 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
87 for (
const QRect &rect : region)
88 painter.fillRect(rect, debugBoxColor);
92 const QRegion ®ion,
const QPoint &rootWindowOffset,
const QSize &dstImageSize)
94 std::vector<::Region::Rect> rects;
95 if (!region.isEmpty()) {
97 region.begin(), region.end(),
std::back_inserter(rects),
98 [&](
const auto &qrect) {
99 return ::Region::Rect{
100 .x = qrect.x() + rootWindowOffset.x(),
101 .y = (dstImageSize.height() - qrect.y()) + rootWindowOffset.y() - qrect.height(),
102 .w =
static_cast<std::uint32_t>(qrect.width()),
103 .h =
static_cast<std::uint32_t>(qrect.height()),
111 QtOhos::QThreadSafeRef<QWindow> qWindowRef, ::OHNativeWindow *nativeWindow,
112 QOhosConsumer<QWindow *> qtThreadFlushFunc)
114 auto sharedQtThreadFlushFunc = QtOhos::moveToSharedPtr(std::move(qtThreadFlushFunc));
115 return QtOhos::evalInJsThread(
116 [&](QtOhos::JsState &) {
117 auto sharedFrameRequestFunc = QtOhos::makeProxyWithJsThreadDeleter(
118 QtOhos::moveToSharedPtr(
119 QArkUi::makeVSyncFrameRequester(
121 [qWindowRef, sharedQtThreadFlushFunc]() {
122 qWindowRef.visitInQtThreadIfAlive(
123 [sharedQtThreadFlushFunc](QWindow &qWindow) {
124 (*sharedQtThreadFlushFunc)(&qWindow);
128 return [sharedFrameRequestFunc]() {
129 (*sharedFrameRequestFunc)();
138 std::max(0, -offset.x()),
139 std::max(0, -offset.y()));
144QOhosPlatformBackingStore::QOhosPlatformBackingStore(QWindow *window,
const CreateInfo &createInfo)
145 : QRasterBackingStore(window)
146 , m_debugDrawFlushedRegion(createInfo.debugDrawFlushedRegion)
147 , m_vsyncEnabled(createInfo.enableVsync)
149 std::make_shared<std::function<
void(QWindow *)>>(
150 [
this](QWindow *qWindow) {
151 flushImmediate(qWindow);
153 , m_windowContextManager(createInfo.enableVsync, m_flushFunc)
157void QOhosPlatformBackingStore::flush(QWindow *window,
const QRegion ®ion,
const QPoint &offset)
159 if (m_reinitializeContextManager) {
160 m_windowContextManager = WindowContextManager(m_vsyncEnabled, m_flushFunc);
161 m_reinitializeContextManager =
false;
164 auto *platformWindow = QOhosPlatformWindow::fromQWindowOrNull(window);
165 auto *surface = platformWindow !=
nullptr ? platformWindow->ownedSurfaceOrNull() :
nullptr;
166 auto bounds = region.translated(offset).boundingRect() & m_image.rect();
168 if (bounds.isEmpty())
171 if (surface ==
nullptr) {
172 qOhosPrintfDebug(
"Window %p has no surface", window);
176 m_windowContextManager
177 .getOrCreateWindowContext(window, surface->nativeWindow())
178 .flushData().updateDirtyRegionAndScheduleFlush(region, offset);
181void QOhosPlatformBackingStore::resize(
const QSize &size,
const QRegion &staticContents)
183 m_reinitializeContextManager = (size != m_requestedSize);
184 QRasterBackingStore::resize(size, staticContents);
187QImage::Format QOhosPlatformBackingStore::format()
const
189 return QOhosSurface::mapNativeBufferFormatToQImageFormatOrFail(QOhosSurface::bufferFormat);
192QOhosPlatformBackingStore::QImageView::QImageView(
const QImage &srcImage,
const QRect &subRect)
193 : m_srcImage(srcImage)
196 auto imageRect = QRect(QPoint{}, m_srcImage.size());
197 if (!imageRect.contains(m_subRect)) {
198 qOhosReportFatalErrorAndAbort(
199 "image rect: (%d, %d, %d, %d) does not contain sub-rect: (%d, %d, %d, %d)",
200 imageRect.x(), imageRect.y(), imageRect.width(), imageRect.height(),
201 m_subRect.x(), m_subRect.y(), m_subRect.width(), m_subRect.height());
204 constexpr auto minAcceptedImageDepth = 8;
205 if (srcImage.depth() < minAcceptedImageDepth)
206 qOhosReportFatalErrorAndAbort(
"QImageView is not supported for <8bpp QImages");
209std::size_t QOhosPlatformBackingStore::QImageView::bytesPerPixel()
const
211 return qImageBytesPerPixel(m_srcImage);
214QSpan<
const uchar> QOhosPlatformBackingStore::QImageView::constScanLine(
int i)
const
216 Q_ASSERT(i >= 0 && i < m_subRect.height());
217 auto srcScanLine = QSpan(
218 m_srcImage.constScanLine(m_subRect.y() + i), m_srcImage.bytesPerLine());
219 return srcScanLine.subspan(
220 m_subRect.x() * bytesPerPixel(), bytesPerLine());
223std::size_t QOhosPlatformBackingStore::QImageView::bytesPerLine()
const
225 return bytesPerPixel() * m_subRect.width();
228QSize QOhosPlatformBackingStore::QImageView::size()
const
230 return m_subRect.size();
233QOhosPlatformBackingStore::BufferRegionHandler::BufferRegionHandler(::OHNativeWindow *nativeWindow)
234 : m_bufferQueueSize(getNativeWindowBufferQueueSize(nativeWindow))
238QOhosOptional<QRegion> QOhosPlatformBackingStore::BufferRegionHandler::mergeRegionForBufferHandle(
239 ::BufferHandle *bufferHandle,
240 QRegion region)
const
242 const auto it = m_buffersToFlushSequenceIds.find(bufferHandle);
243 if (it == m_buffersToFlushSequenceIds.end())
246 std::uint64_t numberOfRegions = m_flushSequenceId - it->second;
247 if (numberOfRegions > m_bufferQueueSize)
250 for (
auto it = m_lastFlushedRegions.cend() - numberOfRegions; it != m_lastFlushedRegions.cend(); ++it)
251 region = region.united(*it);
253 return makeQOhosOptional(region);
256void QOhosPlatformBackingStore::BufferRegionHandler::storeRegionForBufferHandle(
257 ::BufferHandle *bufferHandle,
258 const QRegion ®ion)
260 m_buffersToFlushSequenceIds[bufferHandle] = m_flushSequenceId;
262 if (m_buffersToFlushSequenceIds.size() > m_bufferQueueSize) {
263 m_buffersToFlushSequenceIds.clear();
264 m_lastFlushedRegions.clear();
265 m_flushSequenceId = 0;
269 m_lastFlushedRegions.push_back(region);
270 if (m_lastFlushedRegions.size() > m_bufferQueueSize)
271 m_lastFlushedRegions.pop_front();
276QOhosPlatformBackingStore::FlushData::FlushData(
std::function<
void()> flushRequestFunc)
277 : m_flushRequestFunc(std::move(flushRequestFunc))
281std::pair<QRegion, QPoint> QOhosPlatformBackingStore::FlushData::fetchAndReset()
283 return std::make_pair(
284 std::exchange(m_mergedRegionForFlush, QRegion {}),
285 std::exchange(m_lastWindowOffset, QPoint {}));
288void QOhosPlatformBackingStore::FlushData::updateDirtyRegionAndScheduleFlush(
289 const QRegion ®ion,
const QPoint &rootWindowOffset)
291 m_mergedRegionForFlush = m_mergedRegionForFlush.united(region);
292 m_lastWindowOffset = rootWindowOffset;
293 m_flushRequestFunc();
296QOhosPlatformBackingStore::WindowContext::WindowContext(
297 ::OHNativeWindow *nativeWindow,
std::function<
void()> flushRequestFunc)
298 : m_bufferRegionHandler(std::make_unique<BufferRegionHandler>(nativeWindow))
299 , m_flushData(std::move(flushRequestFunc))
303QOhosPlatformBackingStore::BufferRegionHandler &
304QOhosPlatformBackingStore::WindowContext::bufferRegionHandler()
306 return *m_bufferRegionHandler;
309QOhosPlatformBackingStore::FlushData &QOhosPlatformBackingStore::WindowContext::flushData()
314QOhosPlatformBackingStore::WindowContextManager::WindowContextManager(
316 std::shared_ptr<std::function<
void(QWindow *)>> flushImmediateFunc)
317 : m_flushFunc(flushImmediateFunc)
318 , m_vsyncEnabled(vsyncEnabled)
322QOhosPlatformBackingStore::WindowContext &
323QOhosPlatformBackingStore::WindowContextManager::getOrCreateWindowContext(
325 ::OHNativeWindow *nativeWindow)
327 auto handlerIter = m_windowContexts.find(window);
328 if (handlerIter == m_windowContexts.end()) {
330 std::function<
void()> flushFunc;
332 if (m_vsyncEnabled) {
333 auto weakQtFlushFunc = QtOhos::makeWeakPtr(m_flushFunc);
334 auto qWindowRef = QtOhos::makeQThreadSafeRef(window);
335 flushFunc = makeVSyncFrameRequestFunc(
336 qWindowRef, nativeWindow,
337 [weakQtFlushFunc](QWindow *qWindow) {
338 auto sharedQtThreadFlushFunc = weakQtFlushFunc.lock();
339 if (sharedQtThreadFlushFunc)
340 (*sharedQtThreadFlushFunc)(qWindow);
343 flushFunc = [window, flushFunc = m_flushFunc]() {
344 (*flushFunc)(window);
348 std::tie(handlerIter, std::ignore) = m_windowContexts.emplace(
349 window, std::make_unique<WindowContext>(
350 nativeWindow, std::move(flushFunc)));
352 return *(handlerIter->second);
355bool QOhosPlatformBackingStore::scroll(
const QRegion &area,
int dx,
int dy)
359 const qreal devicePixelRatio = m_image.devicePixelRatio();
361 static_cast<
int>(dx * devicePixelRatio),
362 static_cast<
int>(dy * devicePixelRatio));
364 for (
const QRect &rect : area) {
365 qt_scrollRectInImage(
367 QRect(rect.topLeft() * devicePixelRatio, rect.size() * devicePixelRatio), delta);
373void QOhosPlatformBackingStore::beginPaint(
const QRegion ®ion)
375 auto previousImageSize = m_image.size();
376 QRasterBackingStore::beginPaint(region);
377 bool imageWasResized = previousImageSize != m_image.size();
379 m_windowContextManager = WindowContextManager(m_vsyncEnabled, m_flushFunc);
382void QOhosPlatformBackingStore::flushImmediate(QWindow *window)
384 auto *platformWindow = QOhosPlatformWindow::fromQWindowOrNull(window);
385 auto *surface = platformWindow !=
nullptr ? platformWindow->ownedSurfaceOrNull() :
nullptr;
387 if (surface ==
nullptr) {
388 qOhosPrintfDebug(
"Window %p has no surface", window);
392 bool isRootWindow = window ==
this->window();
394 ::OHNativeWindow *nativeWindow = surface->nativeWindow();
396 auto &windowContext = m_windowContextManager.getOrCreateWindowContext(window, nativeWindow);
398 QPoint rootWindowOffset;
399 std::tie(region, rootWindowOffset) = windowContext.flushData().fetchAndReset();
401 if (region.isEmpty())
404 auto &bufferRegionHandler = m_windowContextManager
405 .getOrCreateWindowContext(window, nativeWindow).bufferRegionHandler();
407 auto srcImageRect = isRootWindow
408 ? QRect({}, m_image.size())
409 : QRect(rootWindowOffset, platformWindow->geometry().size()).intersected(QRect({}, m_image.size()));
410 if (srcImageRect.isEmpty()) {
411 qOhosPrintfDebug(
"Cannot get source image rect, ignore flush call");
415 QImageView srcImage = QImageView(m_image, srcImageRect);
417 const auto rootWindowRegionToFlush = region
418 .translated(rootWindowOffset)
419 .intersected(srcImageRect)
420 .translated(-rootWindowOffset);
422 surface->paintOnNativeWindowSurface(
423 [&](QImage &dstImage, ::BufferHandle *bufferHandle) {
424 if (srcImage.bytesPerPixel() != qImageBytesPerPixel(dstImage)) {
425 qOhosReportFatalErrorAndAbort(
426 "%s: bytes per pixel in src and dst image mismatch. Image formats are not the same.", Q_FUNC_INFO);
429 const auto& mergedRegionOpt = bufferRegionHandler.mergeRegionForBufferHandle(bufferHandle, rootWindowRegionToFlush);
430 const auto dstImageOffset = extractNegativeOffset(rootWindowOffset);
431 const auto windowVisibleRegion = QRegion(QRect(dstImageOffset, srcImage.size()));
432 const auto requestedRegion = mergedRegionOpt.value_or(windowVisibleRegion);
433 const auto sourceRegionToFlush = requestedRegion.translated(-dstImageOffset);
434 copyImage(srcImage, dstImage, sourceRegionToFlush, dstImageOffset);
436 if (m_debugDrawFlushedRegion)
437 debugDrawFlushedQRegion(dstImage, rootWindowRegionToFlush);
439 return makeOhosRegionRectsForFlush(
440 mergedRegionOpt.value_or(QRegion()), isRootWindow ? QPoint{} : rootWindowOffset, dstImage.size());
442 [&](::BufferHandle *bufferHandle) {
443 bufferRegionHandler.storeRegionForBufferHandle(bufferHandle, rootWindowRegionToFlush);
447 auto *view = platformWindow->ownedViewOrNull();
449 view->handleSurfaceContentsUpdated();
constexpr std::int32_t ohNativeWindowErrorCodeSuccess
std::function< void()> makeVSyncFrameRequestFunc(QtOhos::QThreadSafeRef< QWindow > qWindowRef, ::OHNativeWindow *nativeWindow, QOhosConsumer< QWindow * > qtThreadFlushFunc)
void copyImageRow(QSpan< const uchar > srcRow, QSpan< uchar > dstRow)
std::vector<::Region::Rect > makeOhosRegionRectsForFlush(const QRegion ®ion, const QPoint &rootWindowOffset, const QSize &dstImageSize)
QSpan< uchar > qImageScanLine(QImage &image, int y)
void debugDrawFlushedQRegion(QImage &dstImage, const QRegion ®ion)
std::size_t qImageBytesPerPixel(const QImage &image)
QPoint extractNegativeOffset(const QPoint &offset)
void copyImage(QOhosPlatformBackingStore::QImageView srcImage, QImage &dstImage, const QRegion ®ion, const QPoint &dstOffset)
std::uint64_t getNativeWindowBufferQueueSize(::OHNativeWindow *nativeWindow)
QT_BEGIN_NAMESPACE void qt_scrollRectInImage(QImage &, const QRect &, const QPoint &)