31 std::uint64_t windowBufferUsage = 0;
32 auto getUsageRes = ::OH_NativeWindow_NativeWindowHandleOpt(
33 nativeWindow, ::NativeWindowOperation::GET_USAGE, &windowBufferUsage);
34 if (Q_UNLIKELY(getUsageRes != ohNativeWindowErrorCodeSuccess))
35 qOhosReportFatalErrorAndAbort(
"QOhosNativeXComponent: error reading window buffer usage: %d", getUsageRes);
37 std::uint64_t requestedWindowBufferUsage = windowBufferUsage | usageSetBits;
38 if (requestedWindowBufferUsage != windowBufferUsage) {
39 auto setUsageRes = ::OH_NativeWindow_NativeWindowHandleOpt(
40 nativeWindow, ::NativeWindowOperation::SET_USAGE, requestedWindowBufferUsage);
41 if (Q_UNLIKELY(setUsageRes != ohNativeWindowErrorCodeSuccess))
42 qOhosReportFatalErrorAndAbort(
"QOhosNativeXComponent: error setting window buffer usage: %d", setUsageRes);
48 auto setFormatRes = ::OH_NativeWindow_NativeWindowHandleOpt(
49 nativeWindow, ::NativeWindowOperation::SET_FORMAT, QOhosSurface::bufferFormat);
50 if (Q_UNLIKELY(setFormatRes != ohNativeWindowErrorCodeSuccess)) {
51 qOhosReportFatalErrorAndAbort(
52 "%s: QOhosNativeXComponent: error setting window buffer format: %d",
53 Q_FUNC_INFO, setFormatRes);
70 void *bufferMemory = ::mmap(
71 bufferHandle->virAddr, bufferHandle->size, PROT_WRITE, MAP_SHARED, bufferHandle->fd, 0);
74 int mmapErrno = errno;
75 qCWarning(QtForOhos,
"%s: mmap failed with error: %d", Q_FUNC_INFO, mmapErrno);
79 outMemory = bufferMemory;
85 std::array<::pollfd, 1> pollFileDescriptors = {{
95 pollRetCode = ::poll(pollFileDescriptors.data(), pollFileDescriptors.size(), timeoutMs.count());
96 }
while (pollRetCode == -1 && (errno == EINTR || errno == EAGAIN));
98 auto pollErrno = errno;
100 if (pollRetCode == -1) {
102 QtForOhos,
"%s: polling '%s' file descriptor failed with error: %d",
103 Q_FUNC_INFO, fdName, pollErrno);
104 }
else if (pollRetCode == 0) {
106 QtForOhos,
"%s: polling '%s' file descriptor timed out",
107 Q_FUNC_INFO, fdName);
110 return pollRetCode > 0;
116QOhosSurface::mapNativeBufferFormatToQImageFormatOrFail(
std::int32_t format)
119 Q_BYTE_ORDER == Q_LITTLE_ENDIAN,
120 "Pixel format mapping is currently supported for little-endian targets only");
122 QImage::Format result;
125 case ::NATIVEBUFFER_PIXEL_FMT_RGB_888:
126 result = QImage::Format_RGB888;
128 case ::NATIVEBUFFER_PIXEL_FMT_RGBA_8888:
129 result = QImage::Format_RGBA8888;
131 case ::NATIVEBUFFER_PIXEL_FMT_BGRA_8888:
132 result = QImage::Format_ARGB32_Premultiplied;
135 qOhosReportFatalErrorAndAbort(
"%s: unsupported format %d", Q_FUNC_INFO, format);
141std::optional<QSize> QOhosSurface::tryGetBufferGeometryForWindow(::OHNativeWindow *nativeWindow)
143 std::int32_t surfaceWidth = 0;
144 std::int32_t surfaceHeight = 0;
148 std::int32_t getWindowHandleErrorCode = ::OH_NativeWindow_NativeWindowHandleOpt(
149 nativeWindow, ::GET_BUFFER_GEOMETRY, &surfaceHeight, &surfaceWidth);
151 return getWindowHandleErrorCode == ohNativeWindowErrorCodeSuccess
152 ? std::optional<QSize>({surfaceWidth, surfaceHeight})
173 ::OHNativeWindow *nativeWindow,
const std::optional<QSize> &optSurfaceSize)
175 if (nativeWindow ==
nullptr)
176 qOhosReportFatalErrorAndAbort(
"NativeWindow cannot be null");
178 const bool nativeWindowChanged = nativeWindow != m_nativeWindow;
179 m_nativeWindow = nativeWindow;
183 m_vulkanSurface->setNativeWindowSurface(nativeWindow);
185 if (nativeWindowChanged)
186 setupNativeWindowBufferUsage(
188 ::OH_NativeBuffer_Usage::NATIVEBUFFER_USAGE_CPU_READ);
191 m_eglSurface->setNativeWindowSurface(
192 reinterpret_cast<::EGLNativeWindowType>(m_nativeWindow), optSurfaceSize);
196EGLSurface QOhosSurface::tryGetOrCreateEGLWindowSurface(EGLDisplay display, EGLConfig config,
bool swappingBuffers)
199 m_eglSurface = std::make_unique<QOhosEGLSurface>();
200 m_eglSurface->setNativeWindowSurface(
201 reinterpret_cast<::EGLNativeWindowType>(m_nativeWindow), {});
203 return m_eglSurface->tryGetOrCreateEGLWindowSurface(display, config, swappingBuffers, {});
212 std::function<std::vector<::Region::Rect>(QImage &, ::BufferHandle *)> paintFunc,
213 std::function<
void(::BufferHandle *)> onFlushSuccessFunc)
215 ::OHNativeWindowBuffer *nativeWindowBuffer =
nullptr;
216 auto fenceFileDescriptor = makeFdAutoClosingWrapper();
218 setupNativeWindowBufferFormat(m_nativeWindow);
220 auto requestBufferEc = ::OH_NativeWindow_NativeWindowRequestBuffer(
221 m_nativeWindow, &nativeWindowBuffer, fenceFileDescriptor.get());
222 if (requestBufferEc != ohNativeWindowErrorCodeSuccess) {
223 qCWarning(QtForOhos,
"%s: NativeWindowRequestBuffer failed with error code: %d", Q_FUNC_INFO, requestBufferEc);
227 auto nativeWindowAbortBufferGuard = qScopeGuard([nativeWindow = m_nativeWindow, nativeWindowBuffer](){
228 auto abortBufferEc = ::OH_NativeWindow_NativeWindowAbortBuffer(nativeWindow, nativeWindowBuffer);
229 if (abortBufferEc != ohNativeWindowErrorCodeSuccess)
230 qCWarning(QtForOhos,
"%s: NativeWindowAbortBuffer failed with error code: %d", Q_FUNC_INFO, abortBufferEc);
233 ::BufferHandle *bufferHandle = ::OH_NativeWindow_GetBufferHandleFromNative(nativeWindowBuffer);
234 if (bufferHandle ==
nullptr) {
235 qCWarning(QtForOhos,
"%s: GetBufferHandleFromNative returned null", Q_FUNC_INFO);
239 QImage::Format dstImageFormat = QOhosSurface::mapNativeBufferFormatToQImageFormatOrFail(bufferHandle->format);
240 QSize dstImageSize{bufferHandle->width, bufferHandle->height};
242 void *bufferMemory =
nullptr;
243 if (!tryMapBufferHandleMemory(bufferHandle, bufferMemory))
246 auto unmapMemoryGuard = qScopeGuard([bufferMemory, bufferMemorySize = bufferHandle->size]() {
247 int result = ::munmap(bufferMemory, bufferMemorySize);
249 int munmapErrno = errno;
250 qCWarning(QtForOhos,
"%s: munmap failed with error: %d", Q_FUNC_INFO, munmapErrno);
254 if (*fenceFileDescriptor != -1) {
255 if (!waitForFdReadyForReadOrTimeout(*fenceFileDescriptor,
"window buffer fence", ch::seconds(3)))
260 reinterpret_cast<uchar *>(bufferMemory),
261 dstImageSize.width(), dstImageSize.height(),
262 bufferHandle->stride,
265 auto rects = paintFunc(dstImage, bufferHandle);
267 const std::int32_t acquireFenceFileDescriptor = -1;
268 auto flushBufferEc = ::OH_NativeWindow_NativeWindowFlushBuffer(
269 m_nativeWindow, nativeWindowBuffer, acquireFenceFileDescriptor,
271 .rects = rects.empty() ?
nullptr : rects.data(),
272 .rectNumber =
static_cast<std::int32_t>(rects.size()),
274 if (flushBufferEc != ohNativeWindowErrorCodeSuccess) {
275 qCWarning(QtForOhos,
"%s: failed to flush buffer with error code: %d", Q_FUNC_INFO, flushBufferEc);
278 onFlushSuccessFunc(bufferHandle);
279 nativeWindowAbortBufferGuard.dismiss();