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/qcore_ohos_p.h>
14#include <QtCore/private/qohoslogger_p.h>
15#include <QtCore/qendian.h>
31 std::int32_t bufferQueueSize;
32 auto getBufferQueueSizeResult = ::OH_NativeWindow_NativeWindowHandleOpt(
33 nativeWindow, ::NativeWindowOperation::GET_BUFFERQUEUE_SIZE, &bufferQueueSize);
35 qOhosReportFatalErrorAndAbort(
36 "%s: failed to get buffer queue size with error code: %d",
37 Q_FUNC_INFO, getBufferQueueSizeResult);
39 return bufferQueueSize;
44 const auto bitsPerPixel = image.depth();
45 const auto bytesPerPixel = bitsPerPixel / 8;
51 return QSpan(image.scanLine(y), image.width() * qImageBytesPerPixel(image));
56 const auto sizeToCopy =
std::min(srcRow.size(), dstRow.size());
57 std::memcpy(dstRow.data(), srcRow.data(), sizeToCopy);
61 QOhosPlatformBackingStore::QImageView srcImage, QImage &dstImage,
const QRegion ®ion,
const QPoint &dstOffset)
63 const auto intersectedRegion =
64 region.intersected(QRect({}, srcImage.size()))
65 .intersected(QRect({}, dstImage.size()).translated(-dstOffset));
67 for (
const auto &rect : intersectedRegion) {
68 const auto xSrc = rect.x() * srcImage.bytesPerPixel();
69 const auto widthSrc = rect.width() * srcImage.bytesPerPixel();
70 const auto xDst = (rect.x() + dstOffset.x()) * qImageBytesPerPixel(dstImage);
71 const auto widthDst = rect.width() * qImageBytesPerPixel(dstImage);
73 for (
int row = 0; row < rect.height(); ++row) {
74 const int srcY = rect.y() + row;
75 const int dstY = srcY + dstOffset.y();
76 const auto srcRow = srcImage.constScanLine(srcY).subspan(xSrc, widthSrc);
77 auto dstRow = qImageScanLine(dstImage, dstY).subspan(xDst, widthDst);
78 copyImageRow(srcRow, dstRow);
85 static const QColor debugBoxColor(
"#7F00FF00");
87 QPainter painter(&dstImage);
88 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
89 for (
const QRect &rect : region)
90 painter.fillRect(rect, debugBoxColor);
94 const QRegion ®ion,
const QPoint &rootWindowOffset,
const QSize &dstImageSize)
96 std::vector<::Region::Rect> rects;
97 if (!region.isEmpty()) {
99 region.begin(), region.end(),
std::back_inserter(rects),
100 [&](
const auto &qrect) {
101 return ::Region::Rect{
102 .x = qrect.x() + rootWindowOffset.x(),
103 .y = (dstImageSize.height() - qrect.y()) + rootWindowOffset.y() - qrect.height(),
104 .w =
static_cast<std::uint32_t>(qrect.width()),
105 .h =
static_cast<std::uint32_t>(qrect.height()),
113 QtOhos::QThreadSafeRef<QWindow> qWindowRef, ::OHNativeWindow *nativeWindow,
114 QOhosConsumer<QWindow *> qtThreadFlushFunc)
116 auto sharedQtThreadFlushFunc = QtOhos::moveToSharedPtr(std::move(qtThreadFlushFunc));
117 return QOhosJsThreadGateway::eval(
118 [&](QOhosJsState &) {
119 auto sharedFrameRequestFunc = QtOhos::makeProxyWithJsThreadDeleter(
120 QtOhos::moveToSharedPtr(
121 QArkUi::makeVSyncFrameRequester(
123 [qWindowRef, sharedQtThreadFlushFunc]() {
124 qWindowRef.visitInQtThreadIfAlive(
125 [sharedQtThreadFlushFunc](QWindow &qWindow) {
126 (*sharedQtThreadFlushFunc)(&qWindow);
130 return [sharedFrameRequestFunc]() {
131 (*sharedFrameRequestFunc)();
140 std::max(0, -offset.x()),
141 std::max(0, -offset.y()));
146QOhosPlatformBackingStore::QOhosPlatformBackingStore(QWindow *window,
const CreateInfo &createInfo)
147 : QRasterBackingStore(window)
148 , m_debugDrawFlushedRegion(createInfo.debugDrawFlushedRegion)
149 , m_vsyncEnabled(createInfo.enableVsync)
151 std::make_shared<std::function<
void(QWindow *)>>(
152 [
this](QWindow *qWindow) {
153 flushImmediate(qWindow);
155 , m_windowContextManager(createInfo.enableVsync, m_flushFunc)
159void QOhosPlatformBackingStore::flush(QWindow *window,
const QRegion ®ion,
const QPoint &offset)
161 if (m_reinitializeContextManager) {
162 m_windowContextManager = WindowContextManager(m_vsyncEnabled, m_flushFunc);
163 m_reinitializeContextManager =
false;
166 auto *platformWindow = QOhosPlatformWindow::fromQWindowOrNull(window);
167 auto *surface = platformWindow !=
nullptr ? platformWindow->ownedSurfaceOrNull() :
nullptr;
168 auto bounds = region.translated(offset).boundingRect() & m_image.rect();
170 if (bounds.isEmpty())
173 if (surface ==
nullptr) {
174 qOhosPrintfDebug(
"Window %p has no surface", window);
178 m_windowContextManager
179 .getOrCreateWindowContext(window, surface->nativeWindow())
180 .flushData().updateDirtyRegionAndScheduleFlush(region, offset);
183void QOhosPlatformBackingStore::resize(
const QSize &size,
const QRegion &staticContents)
185 m_reinitializeContextManager = (size != m_requestedSize);
186 QRasterBackingStore::resize(size, staticContents);
189QImage::Format QOhosPlatformBackingStore::format()
const
191 return QOhosSurface::mapNativeBufferFormatToQImageFormatOrFail(QOhosSurface::bufferFormat);
194QOhosPlatformBackingStore::QImageView::QImageView(
const QImage &srcImage,
const QRect &subRect)
195 : m_srcImage(srcImage)
198 auto imageRect = QRect(QPoint{}, m_srcImage.size());
199 if (!imageRect.contains(m_subRect)) {
200 qOhosReportFatalErrorAndAbort(
201 "image rect: (%d, %d, %d, %d) does not contain sub-rect: (%d, %d, %d, %d)",
202 imageRect.x(), imageRect.y(), imageRect.width(), imageRect.height(),
203 m_subRect.x(), m_subRect.y(), m_subRect.width(), m_subRect.height());
206 constexpr auto minAcceptedImageDepth = 8;
207 if (srcImage.depth() < minAcceptedImageDepth)
208 qOhosReportFatalErrorAndAbort(
"QImageView is not supported for <8bpp QImages");
211std::size_t QOhosPlatformBackingStore::QImageView::bytesPerPixel()
const
213 return qImageBytesPerPixel(m_srcImage);
216QSpan<
const uchar> QOhosPlatformBackingStore::QImageView::constScanLine(
int i)
const
218 Q_ASSERT(i >= 0 && i < m_subRect.height());
219 auto srcScanLine = QSpan(
220 m_srcImage.constScanLine(m_subRect.y() + i), m_srcImage.bytesPerLine());
221 return srcScanLine.subspan(
222 m_subRect.x() * bytesPerPixel(), bytesPerLine());
225std::size_t QOhosPlatformBackingStore::QImageView::bytesPerLine()
const
227 return bytesPerPixel() * m_subRect.width();
230QSize QOhosPlatformBackingStore::QImageView::size()
const
232 return m_subRect.size();
235QOhosPlatformBackingStore::BufferRegionHandler::BufferRegionHandler(::OHNativeWindow *nativeWindow)
236 : m_bufferQueueSize(getNativeWindowBufferQueueSize(nativeWindow))
240std::optional<QRegion> QOhosPlatformBackingStore::BufferRegionHandler::mergeRegionForBufferHandle(
241 ::BufferHandle *bufferHandle,
242 QRegion region)
const
244 const auto it = m_buffersToFlushSequenceIds.find(bufferHandle);
245 if (it == m_buffersToFlushSequenceIds.end())
248 std::uint64_t numberOfRegions = m_flushSequenceId - it->second;
249 if (numberOfRegions > m_bufferQueueSize)
252 for (
auto it = m_lastFlushedRegions.cend() - numberOfRegions; it != m_lastFlushedRegions.cend(); ++it)
253 region = region.united(*it);
258void QOhosPlatformBackingStore::BufferRegionHandler::storeRegionForBufferHandle(
259 ::BufferHandle *bufferHandle,
260 const QRegion ®ion)
262 m_buffersToFlushSequenceIds[bufferHandle] = m_flushSequenceId;
264 if (m_buffersToFlushSequenceIds.size() > m_bufferQueueSize) {
265 m_buffersToFlushSequenceIds.clear();
266 m_lastFlushedRegions.clear();
267 m_flushSequenceId = 0;
271 m_lastFlushedRegions.push_back(region);
272 if (m_lastFlushedRegions.size() > m_bufferQueueSize)
273 m_lastFlushedRegions.pop_front();
278QOhosPlatformBackingStore::FlushData::FlushData(
std::function<
void()> flushRequestFunc)
279 : m_flushRequestFunc(std::move(flushRequestFunc))
283std::pair<QRegion, QPoint> QOhosPlatformBackingStore::FlushData::fetchAndReset()
285 return std::make_pair(
286 std::exchange(m_mergedRegionForFlush, QRegion {}),
287 std::exchange(m_lastWindowOffset, QPoint {}));
290void QOhosPlatformBackingStore::FlushData::updateDirtyRegionAndScheduleFlush(
291 const QRegion ®ion,
const QPoint &rootWindowOffset)
293 m_mergedRegionForFlush = m_mergedRegionForFlush.united(region);
294 m_lastWindowOffset = rootWindowOffset;
295 m_flushRequestFunc();
298QOhosPlatformBackingStore::WindowContext::WindowContext(
299 ::OHNativeWindow *nativeWindow,
std::function<
void()> flushRequestFunc)
300 : m_bufferRegionHandler(std::make_unique<BufferRegionHandler>(nativeWindow))
301 , m_flushData(std::move(flushRequestFunc))
305QOhosPlatformBackingStore::BufferRegionHandler &
306QOhosPlatformBackingStore::WindowContext::bufferRegionHandler()
308 return *m_bufferRegionHandler;
311QOhosPlatformBackingStore::FlushData &QOhosPlatformBackingStore::WindowContext::flushData()
316QOhosPlatformBackingStore::WindowContextManager::WindowContextManager(
318 std::shared_ptr<std::function<
void(QWindow *)>> flushImmediateFunc)
319 : m_flushFunc(flushImmediateFunc)
320 , m_vsyncEnabled(vsyncEnabled)
324QOhosPlatformBackingStore::WindowContext &
325QOhosPlatformBackingStore::WindowContextManager::getOrCreateWindowContext(
327 ::OHNativeWindow *nativeWindow)
329 auto handlerIter = m_windowContexts.find(window);
330 if (handlerIter == m_windowContexts.end()) {
332 std::function<
void()> flushFunc;
334 if (m_vsyncEnabled) {
335 auto weakQtFlushFunc = QtOhos::makeWeakPtr(m_flushFunc);
336 auto qWindowRef = QtOhos::makeQThreadSafeRef(window);
337 flushFunc = makeVSyncFrameRequestFunc(
338 qWindowRef, nativeWindow,
339 [weakQtFlushFunc](QWindow *qWindow) {
340 auto sharedQtThreadFlushFunc = weakQtFlushFunc.lock();
341 if (sharedQtThreadFlushFunc)
342 (*sharedQtThreadFlushFunc)(qWindow);
345 flushFunc = [window, flushFunc = m_flushFunc]() {
346 (*flushFunc)(window);
350 std::tie(handlerIter, std::ignore) = m_windowContexts.emplace(
351 window, std::make_unique<WindowContext>(
352 nativeWindow, std::move(flushFunc)));
354 return *(handlerIter->second);
357bool QOhosPlatformBackingStore::scroll(
const QRegion &area,
int dx,
int dy)
361 const qreal devicePixelRatio = m_image.devicePixelRatio();
363 static_cast<
int>(dx * devicePixelRatio),
364 static_cast<
int>(dy * devicePixelRatio));
366 for (
const QRect &rect : area) {
367 qt_scrollRectInImage(
369 QRect(rect.topLeft() * devicePixelRatio, rect.size() * devicePixelRatio), delta);
375void QOhosPlatformBackingStore::beginPaint(
const QRegion ®ion)
377 auto previousImageSize = m_image.size();
378 QRasterBackingStore::beginPaint(region);
379 bool imageWasResized = previousImageSize != m_image.size();
381 m_windowContextManager = WindowContextManager(m_vsyncEnabled, m_flushFunc);
384void QOhosPlatformBackingStore::flushImmediate(QWindow *window)
386 auto *platformWindow = QOhosPlatformWindow::fromQWindowOrNull(window);
387 auto *surface = platformWindow !=
nullptr ? platformWindow->ownedSurfaceOrNull() :
nullptr;
389 if (surface ==
nullptr) {
390 qOhosPrintfDebug(
"Window %p has no surface", window);
394 bool isRootWindow = window ==
this->window();
396 ::OHNativeWindow *nativeWindow = surface->nativeWindow();
398 auto &windowContext = m_windowContextManager.getOrCreateWindowContext(window, nativeWindow);
400 QPoint rootWindowOffset;
401 std::tie(region, rootWindowOffset) = windowContext.flushData().fetchAndReset();
403 if (region.isEmpty())
406 auto &bufferRegionHandler = m_windowContextManager
407 .getOrCreateWindowContext(window, nativeWindow).bufferRegionHandler();
409 auto srcImageRect = isRootWindow
410 ? QRect({}, m_image.size())
411 : QRect(rootWindowOffset, platformWindow->geometry().size()).intersected(QRect({}, m_image.size()));
412 if (srcImageRect.isEmpty()) {
413 qOhosPrintfDebug(
"Cannot get source image rect, ignore flush call");
417 QImageView srcImage = QImageView(m_image, srcImageRect);
419 const auto rootWindowRegionToFlush = region
420 .translated(rootWindowOffset)
421 .intersected(srcImageRect)
422 .translated(-rootWindowOffset);
424 surface->paintOnNativeWindowSurface(
425 [&](QImage &dstImage, ::BufferHandle *bufferHandle) {
426 if (srcImage.bytesPerPixel() != qImageBytesPerPixel(dstImage)) {
427 qOhosReportFatalErrorAndAbort(
428 "%s: bytes per pixel in src and dst image mismatch. Image formats are not the same.", Q_FUNC_INFO);
431 const auto& mergedRegionOpt = bufferRegionHandler.mergeRegionForBufferHandle(bufferHandle, rootWindowRegionToFlush);
432 const auto dstImageOffset = extractNegativeOffset(rootWindowOffset);
433 const auto windowVisibleRegion = QRegion(QRect(dstImageOffset, srcImage.size()));
434 const auto requestedRegion = mergedRegionOpt.value_or(windowVisibleRegion);
435 const auto sourceRegionToFlush = requestedRegion.translated(-dstImageOffset);
436 copyImage(srcImage, dstImage, sourceRegionToFlush, dstImageOffset);
438 if (m_debugDrawFlushedRegion)
439 debugDrawFlushedQRegion(dstImage, rootWindowRegionToFlush);
441 return makeOhosRegionRectsForFlush(
442 mergedRegionOpt.value_or(QRegion()), isRootWindow ? QPoint{} : rootWindowOffset, dstImage.size());
444 [&](::BufferHandle *bufferHandle) {
445 bufferRegionHandler.storeRegionForBufferHandle(bufferHandle, rootWindowRegionToFlush);
449 auto *view = platformWindow->ownedViewOrNull();
451 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 &)