Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qohossurface.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <render/qohossurface.h>
5
6#include <QtCore/qscopeguard.h>
7#include <QtGui/qimage.h>
8#include <native_window/external_window.h>
9#include <native_buffer/native_buffer.h>
10#include <native_window/buffer_handle.h>
11#include <poll.h>
12#include <qohosplugincore.h>
13#include <qohosutils.h>
14#include <sys/mman.h>
15#include <unistd.h>
16
17#if QT_CONFIG(vulkan)
18#include <QtGui/qvulkaninstance.h>
19#endif
20
21namespace ch = std::chrono;
22
23QT_BEGIN_NAMESPACE
24
25namespace
26{
27
28constexpr std::int32_t ohNativeWindowErrorCodeSuccess = 0;
29
30void setupNativeWindowBufferUsage(::OHNativeWindow *nativeWindow, std::uint64_t usageSetBits)
31{
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);
37
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);
44 }
45}
46
47void setupNativeWindowBufferFormat(::OHNativeWindow *nativeWindow)
48{
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);
55 }
56}
57
58std::shared_ptr<int> makeFdAutoClosingWrapper()
59{
60 auto fileDescriptor = std::make_shared<int>(-1);
61 return std::shared_ptr<int>(
62 fileDescriptor.get(),
63 [fileDescriptor](auto) {
64 if (*fileDescriptor != -1)
65 std::ignore = ::close(*fileDescriptor);
66 });
67}
68
69bool tryMapBufferHandleMemory(::BufferHandle *bufferHandle, void *&outMemory)
70{
71 void *bufferMemory = ::mmap(
72 bufferHandle->virAddr, bufferHandle->size, PROT_WRITE, MAP_SHARED, bufferHandle->fd, 0);
73
74 if (bufferMemory == MAP_FAILED) {
75 int mmapErrno = errno;
76 qCWarning(QtForOhos, "%s: mmap failed with error: %d", Q_FUNC_INFO, mmapErrno);
77 return false;
78 }
79
80 outMemory = bufferMemory;
81 return true;
82}
83
84bool waitForFdReadyForReadOrTimeout(int fd, const char *fdName, ch::milliseconds timeoutMs)
85{
86 std::array<::pollfd, 1> pollFileDescriptors = {{
87 {
88 .fd = fd,
89 .events = POLLIN,
90 },
91 }};
92
93 int pollRetCode;
94 do {
95 pollRetCode = ::poll(pollFileDescriptors.data(), pollFileDescriptors.size(), timeoutMs.count());
96 } while (pollRetCode == -1 && (errno == EINTR || errno == EAGAIN));
97
98 auto pollErrno = errno;
99
100 if (pollRetCode == -1) {
101 qCWarning(
102 QtForOhos, "%s: polling '%s' file descriptor failed with error: %d",
103 Q_FUNC_INFO, fdName, pollErrno);
104 } else if (pollRetCode == 0) {
105 qCWarning(
106 QtForOhos, "%s: polling '%s' file descriptor timed out",
107 Q_FUNC_INFO, fdName);
108 }
109
110 return pollRetCode > 0;
111}
112
113}
114
115QImage::Format
116QOhosSurface::mapNativeBufferFormatToQImageFormatOrFail(std::int32_t format)
117{
118 static_assert(
119 Q_BYTE_ORDER == Q_LITTLE_ENDIAN,
120 "Pixel format mapping is currently supported for little-endian targets only");
121
122 QImage::Format result;
123
124 switch (format) {
125 case ::NATIVEBUFFER_PIXEL_FMT_RGB_888:
126 result = QImage::Format_RGB888;
127 break;
128 case ::NATIVEBUFFER_PIXEL_FMT_RGBA_8888:
129 result = QImage::Format_RGBA8888;
130 break;
131 case ::NATIVEBUFFER_PIXEL_FMT_BGRA_8888:
132 result = QImage::Format_ARGB32_Premultiplied;
133 break;
134 default:
135 qOhosReportFatalErrorAndAbort("%s: unsupported format %d", Q_FUNC_INFO, format);
136 }
137
138 return result;
139}
140
141QOhosOptional<QSize> QOhosSurface::tryGetBufferGeometryForWindow(::OHNativeWindow *nativeWindow)
142{
143 std::int32_t surfaceWidth = 0;
144 std::int32_t surfaceHeight = 0;
145
146 // NOTE - The parameters height and width are reversed than what is usual order
147 // on purpose as required by the documentation
148 std::int32_t getWindowHandleErrorCode = ::OH_NativeWindow_NativeWindowHandleOpt(
149 nativeWindow, ::GET_BUFFER_GEOMETRY, &surfaceHeight, &surfaceWidth);
150
151 return getWindowHandleErrorCode == ohNativeWindowErrorCodeSuccess
152 ? QOhosOptional<QSize>({surfaceWidth, surfaceHeight})
153 : makeEmptyQOhosOptional();
154}
155
156QOhosSurface::QOhosSurface(::OHNativeWindow *nativeWindow)
158{
159 if (nativeWindow == nullptr)
160 qOhosReportFatalErrorAndAbort("NativeWindow cannot be null");
161
162 setupNativeWindowBufferUsage(
163 nativeWindow,
164 ::OH_NativeBuffer_Usage::NATIVEBUFFER_USAGE_CPU_READ);
165}
166
167::OHNativeWindow *QOhosSurface::nativeWindow() const
168{
169 return m_nativeWindow;
170}
171
172void QOhosSurface::setNativeWindowSurface(::OHNativeWindow *nativeWindow)
173{
174 if (nativeWindow == nullptr)
175 qOhosReportFatalErrorAndAbort("NativeWindow cannot be null");
176
177 m_nativeWindow = nativeWindow;
178
179#if QT_CONFIG(vulkan)
180 if (m_vulkanSurface)
181 m_vulkanSurface->setNativeWindowSurface(nativeWindow);
182#endif
183 setupNativeWindowBufferUsage(
184 nativeWindow,
185 ::OH_NativeBuffer_Usage::NATIVEBUFFER_USAGE_CPU_READ);
186
187 if (m_eglSurface) {
188 m_eglSurface->setNativeWindowSurface(
189 reinterpret_cast<::EGLNativeWindowType>(m_nativeWindow));
190 }
191}
192
193EGLSurface QOhosSurface::tryGetOrCreateEGLWindowSurface(EGLDisplay display, EGLConfig config)
194{
195 if (!m_eglSurface) {
196 m_eglSurface = std::make_unique<QOhosEGLSurface>();
197 m_eglSurface->setNativeWindowSurface(
198 reinterpret_cast<::EGLNativeWindowType>(m_nativeWindow));
199 }
200 return m_eglSurface->tryGetOrCreateEGLWindowSurface(display, config, {});
201}
202
204{
205 return tryGetBufferGeometryForWindow(m_nativeWindow);
206}
207
208void QOhosSurface::paintOnNativeWindowSurface(
209 std::function<std::vector<::Region::Rect>(QImage &, ::BufferHandle *)> paintFunc,
210 std::function<void(::BufferHandle *)> onFlushSuccessFunc)
211{
212 ::OHNativeWindowBuffer *nativeWindowBuffer = nullptr;
213 auto fenceFileDescriptor = makeFdAutoClosingWrapper();
214
215 setupNativeWindowBufferFormat(m_nativeWindow);
216
217 auto requestBufferEc = ::OH_NativeWindow_NativeWindowRequestBuffer(
218 m_nativeWindow, &nativeWindowBuffer, fenceFileDescriptor.get());
219 if (requestBufferEc != ohNativeWindowErrorCodeSuccess) {
220 qCWarning(QtForOhos, "%s: NativeWindowRequestBuffer failed with error code: %d", Q_FUNC_INFO, requestBufferEc);
221 return;
222 }
223
224 auto nativeWindowAbortBufferGuard = qScopeGuard([nativeWindow = m_nativeWindow, nativeWindowBuffer](){
225 auto abortBufferEc = ::OH_NativeWindow_NativeWindowAbortBuffer(nativeWindow, nativeWindowBuffer);
226 if (abortBufferEc != ohNativeWindowErrorCodeSuccess)
227 qCWarning(QtForOhos, "%s: NativeWindowAbortBuffer failed with error code: %d", Q_FUNC_INFO, abortBufferEc);
228 });
229
230 ::BufferHandle *bufferHandle = ::OH_NativeWindow_GetBufferHandleFromNative(nativeWindowBuffer);
231 if (bufferHandle == nullptr) {
232 qCWarning(QtForOhos, "%s: GetBufferHandleFromNative returned null", Q_FUNC_INFO);
233 return;
234 }
235
236 QImage::Format dstImageFormat = QOhosSurface::mapNativeBufferFormatToQImageFormatOrFail(bufferHandle->format);
237 QSize dstImageSize{bufferHandle->width, bufferHandle->height};
238
239 void *bufferMemory = nullptr;
240 if (!tryMapBufferHandleMemory(bufferHandle, bufferMemory))
241 return;
242
243 auto unmapMemoryGuard = qScopeGuard([bufferMemory, bufferMemorySize = bufferHandle->size]() {
244 int result = ::munmap(bufferMemory, bufferMemorySize);
245 if (result != 0) {
246 int munmapErrno = errno;
247 qCWarning(QtForOhos, "%s: munmap failed with error: %d", Q_FUNC_INFO, munmapErrno);
248 }
249 });
250
251 if (*fenceFileDescriptor != -1) {
252 if (!waitForFdReadyForReadOrTimeout(*fenceFileDescriptor, "window buffer fence", ch::seconds(3)))
253 return;
254 }
255
256 QImage dstImage(
257 reinterpret_cast<uchar *>(bufferMemory),
258 dstImageSize.width(), dstImageSize.height(),
259 bufferHandle->stride,
260 dstImageFormat);
261
262 auto rects = paintFunc(dstImage, bufferHandle);
263
264 const std::int32_t acquireFenceFileDescriptor = -1;
265 auto flushBufferEc = ::OH_NativeWindow_NativeWindowFlushBuffer(
266 m_nativeWindow, nativeWindowBuffer, acquireFenceFileDescriptor,
267 ::Region{
268 .rects = rects.empty() ? nullptr : rects.data(),
269 .rectNumber = static_cast<std::int32_t>(rects.size()),
270 });
271 if (flushBufferEc != ohNativeWindowErrorCodeSuccess) {
272 qCWarning(QtForOhos, "%s: failed to flush buffer with error code: %d", Q_FUNC_INFO, flushBufferEc);
273 return;
274 }
275 onFlushSuccessFunc(bufferHandle);
276 nativeWindowAbortBufferGuard.dismiss();
277}
278
279void QOhosSurface::clearNativeWindowSurface()
280{
281 paintOnNativeWindowSurface(
282 [](QImage &dstImage, ::BufferHandle *) {
283 dstImage.fill(Qt::transparent);
284 return std::vector<::Region::Rect>();
285 },
286 [](::BufferHandle *) {});
287}
288
289void QOhosSurface::setExtraUsageBitsForNativeWindowBuffer(std::uint64_t usageSetBits)
290{
291 setupNativeWindowBufferUsage(m_nativeWindow, usageSetBits);
292}
293
294#if QT_CONFIG(vulkan)
295
296VkSurfaceKHR *QOhosSurface::tryGetOrCreateVulkanWindowSurface(QVulkanInstance *instance)
297{
298 if (!m_vulkanSurface) {
299 m_vulkanSurface = std::make_unique<QOhosVulkanSurface>();
300 m_vulkanSurface->setNativeWindowSurface(m_nativeWindow);
301 setExtraUsageBitsForNativeWindowBuffer(NATIVEBUFFER_USAGE_HW_RENDER | NATIVEBUFFER_USAGE_HW_TEXTURE);
302 }
303 auto createFn = reinterpret_cast<PFN_vkCreateSurfaceOHOS>(
304 instance->getInstanceProcAddr("vkCreateSurfaceOHOS"));
305 auto destroyFn = reinterpret_cast<PFN_vkDestroySurfaceKHR>(
306 instance->getInstanceProcAddr("vkDestroySurfaceKHR"));
307 return m_vulkanSurface->tryGetOrCreateVulkanWindowSurface(instance->vkInstance(), createFn, destroyFn);
308}
309
310#endif // QT_CONFIG(vulkan)
311
312QT_END_NAMESPACE
void setExtraUsageBitsForNativeWindowBuffer(std::uint64_t usageSetBits)
QOhosOptional< QSize > surfaceResolution() const
::OHNativeWindow * nativeWindow() const
void setNativeWindowSurface(::OHNativeWindow *nativeWindow)
void paintOnNativeWindowSurface(std::function< std::vector<::Region::Rect >(QImage &, ::BufferHandle *)> paintFunc, std::function< void(::BufferHandle *)> onFlushSuccessFunc)
QOhosSurface(::OHNativeWindow *nativeWindow)
void clearNativeWindowSurface()
void setupNativeWindowBufferFormat(::OHNativeWindow *nativeWindow)
std::shared_ptr< int > makeFdAutoClosingWrapper()
bool waitForFdReadyForReadOrTimeout(int fd, const char *fdName, ch::milliseconds timeoutMs)
void setupNativeWindowBufferUsage(::OHNativeWindow *nativeWindow, std::uint64_t usageSetBits)
bool tryMapBufferHandleMemory(::BufferHandle *bufferHandle, void *&outMemory)
#define MAP_FAILED