32 std::uint64_t windowBufferUsage = 0;
33 auto getUsageRes = ::OH_NativeWindow_NativeWindowHandleOpt(
34 nativeWindow, ::NativeWindowOperation::GET_USAGE, &windowBufferUsage);
35 if (Q_UNLIKELY(getUsageRes != ohNativeWindowErrorCodeSuccess))
36 qOhosReportFatalErrorAndAbort(
"QOhosNativeXComponent: error reading window buffer usage: %d", getUsageRes);
38 std::uint64_t requestedWindowBufferUsage = windowBufferUsage | usageSetBits;
39 if (requestedWindowBufferUsage != windowBufferUsage) {
40 auto setUsageRes = ::OH_NativeWindow_NativeWindowHandleOpt(
41 nativeWindow, ::NativeWindowOperation::SET_USAGE, requestedWindowBufferUsage);
42 if (Q_UNLIKELY(setUsageRes != ohNativeWindowErrorCodeSuccess))
43 qOhosReportFatalErrorAndAbort(
"QOhosNativeXComponent: error setting window buffer usage: %d", setUsageRes);
49 auto setFormatRes = ::OH_NativeWindow_NativeWindowHandleOpt(
50 nativeWindow, ::NativeWindowOperation::SET_FORMAT, QOhosSurface::bufferFormat);
51 if (Q_UNLIKELY(setFormatRes != ohNativeWindowErrorCodeSuccess)) {
52 qOhosReportFatalErrorAndAbort(
53 "%s: QOhosNativeXComponent: error setting window buffer format: %d",
54 Q_FUNC_INFO, setFormatRes);
71 void *bufferMemory = ::mmap(
72 bufferHandle->virAddr, bufferHandle->size, PROT_WRITE, MAP_SHARED, bufferHandle->fd, 0);
75 int mmapErrno = errno;
76 qCWarning(QtForOhos,
"%s: mmap failed with error: %d", Q_FUNC_INFO, mmapErrno);
80 outMemory = bufferMemory;
86 std::array<::pollfd, 1> pollFileDescriptors = {{
96 pollRetCode = ::poll(pollFileDescriptors.data(), pollFileDescriptors.size(), timeoutMs.count());
97 }
while (pollRetCode == -1 && (errno == EINTR || errno == EAGAIN));
99 auto pollErrno = errno;
101 if (pollRetCode == -1) {
103 QtForOhos,
"%s: polling '%s' file descriptor failed with error: %d",
104 Q_FUNC_INFO, fdName, pollErrno);
105 }
else if (pollRetCode == 0) {
107 QtForOhos,
"%s: polling '%s' file descriptor timed out",
108 Q_FUNC_INFO, fdName);
111 return pollRetCode > 0;
117QOhosSurface::mapNativeBufferFormatToQImageFormatOrFail(
std::int32_t format)
120 Q_BYTE_ORDER == Q_LITTLE_ENDIAN,
121 "Pixel format mapping is currently supported for little-endian targets only");
123 QImage::Format result;
126 case ::NATIVEBUFFER_PIXEL_FMT_RGB_888:
127 result = QImage::Format_RGB888;
129 case ::NATIVEBUFFER_PIXEL_FMT_RGBA_8888:
130 result = QImage::Format_RGBA8888;
132 case ::NATIVEBUFFER_PIXEL_FMT_BGRA_8888:
133 result = QImage::Format_ARGB32_Premultiplied;
136 qOhosReportFatalErrorAndAbort(
"%s: unsupported format %d", Q_FUNC_INFO, format);
142std::optional<QSize> QOhosSurface::tryGetBufferGeometryForWindow(::OHNativeWindow *nativeWindow)
144 std::int32_t surfaceWidth = 0;
145 std::int32_t surfaceHeight = 0;
149 std::int32_t getWindowHandleErrorCode = ::OH_NativeWindow_NativeWindowHandleOpt(
150 nativeWindow, ::GET_BUFFER_GEOMETRY, &surfaceHeight, &surfaceWidth);
152 return getWindowHandleErrorCode == ohNativeWindowErrorCodeSuccess
153 ? std::optional<QSize>({surfaceWidth, surfaceHeight})
174 ::OHNativeWindow *nativeWindow,
const std::optional<QSize> &optSurfaceSize)
176 if (nativeWindow ==
nullptr)
177 qOhosReportFatalErrorAndAbort(
"NativeWindow cannot be null");
179 m_nativeWindow = nativeWindow;
183 m_vulkanSurface->setNativeWindowSurface(nativeWindow);
185 setupNativeWindowBufferUsage(
187 ::OH_NativeBuffer_Usage::NATIVEBUFFER_USAGE_CPU_READ);
190 m_eglSurface->setNativeWindowSurface(
191 reinterpret_cast<::EGLNativeWindowType>(m_nativeWindow), optSurfaceSize);
195EGLSurface QOhosSurface::tryGetOrCreateEGLWindowSurface(EGLDisplay display, EGLConfig config,
bool swappingBuffers)
198 m_eglSurface = std::make_unique<QOhosEGLSurface>();
199 m_eglSurface->setNativeWindowSurface(
200 reinterpret_cast<::EGLNativeWindowType>(m_nativeWindow), {});
202 return m_eglSurface->tryGetOrCreateEGLWindowSurface(display, config, swappingBuffers, {});
211 std::function<std::vector<::Region::Rect>(QImage &, ::BufferHandle *)> paintFunc,
212 std::function<
void(::BufferHandle *)> onFlushSuccessFunc)
214 ::OHNativeWindowBuffer *nativeWindowBuffer =
nullptr;
215 auto fenceFileDescriptor = makeFdAutoClosingWrapper();
217 setupNativeWindowBufferFormat(m_nativeWindow);
219 auto requestBufferEc = ::OH_NativeWindow_NativeWindowRequestBuffer(
220 m_nativeWindow, &nativeWindowBuffer, fenceFileDescriptor.get());
221 if (requestBufferEc != ohNativeWindowErrorCodeSuccess) {
222 qCWarning(QtForOhos,
"%s: NativeWindowRequestBuffer failed with error code: %d", Q_FUNC_INFO, requestBufferEc);
226 auto nativeWindowAbortBufferGuard = qScopeGuard([nativeWindow = m_nativeWindow, nativeWindowBuffer](){
227 auto abortBufferEc = ::OH_NativeWindow_NativeWindowAbortBuffer(nativeWindow, nativeWindowBuffer);
228 if (abortBufferEc != ohNativeWindowErrorCodeSuccess)
229 qCWarning(QtForOhos,
"%s: NativeWindowAbortBuffer failed with error code: %d", Q_FUNC_INFO, abortBufferEc);
232 ::BufferHandle *bufferHandle = ::OH_NativeWindow_GetBufferHandleFromNative(nativeWindowBuffer);
233 if (bufferHandle ==
nullptr) {
234 qCWarning(QtForOhos,
"%s: GetBufferHandleFromNative returned null", Q_FUNC_INFO);
238 QImage::Format dstImageFormat = QOhosSurface::mapNativeBufferFormatToQImageFormatOrFail(bufferHandle->format);
239 QSize dstImageSize{bufferHandle->width, bufferHandle->height};
241 void *bufferMemory =
nullptr;
242 if (!tryMapBufferHandleMemory(bufferHandle, bufferMemory))
245 auto unmapMemoryGuard = qScopeGuard([bufferMemory, bufferMemorySize = bufferHandle->size]() {
246 int result = ::munmap(bufferMemory, bufferMemorySize);
248 int munmapErrno = errno;
249 qCWarning(QtForOhos,
"%s: munmap failed with error: %d", Q_FUNC_INFO, munmapErrno);
253 if (*fenceFileDescriptor != -1) {
254 if (!waitForFdReadyForReadOrTimeout(*fenceFileDescriptor,
"window buffer fence", ch::seconds(3)))
259 reinterpret_cast<uchar *>(bufferMemory),
260 dstImageSize.width(), dstImageSize.height(),
261 bufferHandle->stride,
264 auto rects = paintFunc(dstImage, bufferHandle);
266 const std::int32_t acquireFenceFileDescriptor = -1;
267 auto flushBufferEc = ::OH_NativeWindow_NativeWindowFlushBuffer(
268 m_nativeWindow, nativeWindowBuffer, acquireFenceFileDescriptor,
270 .rects = rects.empty() ?
nullptr : rects.data(),
271 .rectNumber =
static_cast<std::int32_t>(rects.size()),
273 if (flushBufferEc != ohNativeWindowErrorCodeSuccess) {
274 qCWarning(QtForOhos,
"%s: failed to flush buffer with error code: %d", Q_FUNC_INFO, flushBufferEc);
277 onFlushSuccessFunc(bufferHandle);
278 nativeWindowAbortBufferGuard.dismiss();