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
qandroidplatformvulkanwindow.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 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// Qt-Security score:significant reason:default
4
9
10#include <QSurfaceFormat>
11#include <qpa/qplatformscreen.h>
12#include <qpa/qwindowsysteminterface.h>
13
14#include <android/native_window.h>
15#include <android/native_window_jni.h>
16
18
19using namespace Qt::StringLiterals;
20
29
31{
32 m_surfaceWaitCondition.wakeOne();
33 destroyAndClearSurface();
34}
35
37{
39
40 QRect availableGeometry = screen()->availableGeometry();
41 if (rect.width() > 0
42 && rect.height() > 0
43 && availableGeometry.width() > 0
44 && availableGeometry.height() > 0) {
45 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(0, 0), rect.size()));
46 }
47}
48
50{
51 QAndroidPlatformWindow::applicationStateChanged(state);
52 if (state <= Qt::ApplicationHidden) {
53 destroyAndClearSurface();
54 }
55}
56
58{
59 return window()->requestedFormat();
60}
61
63{
64 if (m_vkSurface && m_destroyVkSurface) {
65 m_destroyVkSurface(window()->vulkanInstance()->vkInstance(), m_vkSurface, nullptr);
66 m_vkSurface = 0;
67 }
68
69 if (m_nativeWindow) {
70 ANativeWindow_release(m_nativeWindow);
71 m_nativeWindow = nullptr;
72 }
73
75}
76
77void QAndroidPlatformVulkanWindow::destroyAndClearSurface()
78{
83}
84
86{
87 if (QAndroidEventDispatcherStopper::stopped() ||
88 QGuiApplication::applicationState() == Qt::ApplicationSuspended) {
89 qDebug(lcQpaWindow) << "Application not active, return existing surface.";
90 return &m_vkSurface;
91 }
92
93 bool needsExpose = false;
94 if (!m_vkSurface) {
96
97 QMutexLocker lock(&m_surfaceMutex);
98 if (!m_androidSurfaceCreated) {
99 QtAndroidPrivate::AndroidDeadlockProtector protector(
100 u"QAndroidPlatformVulkanWindow::vkSurface()"_s);
101 if (!protector.acquire())
102 return &m_vkSurface;
104 m_surfaceWaitCondition.wait(&m_surfaceMutex);
105 }
106
107 if (!m_androidSurfaceCreated || !m_androidSurfaceObject.isValid())
108 return &m_vkSurface;
109
110 QJniEnvironment env;
111 m_nativeWindow = ANativeWindow_fromSurface(env.jniEnv(), m_androidSurfaceObject.object());
112
113 VkAndroidSurfaceCreateInfoKHR surfaceInfo;
114 memset(&surfaceInfo, 0, sizeof(surfaceInfo));
115 surfaceInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
116 surfaceInfo.window = m_nativeWindow;
117 QVulkanInstance *inst = window()->vulkanInstance();
118 if (!inst) {
119 qWarning("Attempted to create Vulkan surface without an instance; was QWindow::setVulkanInstance() called?");
120 return &m_vkSurface;
121 }
122 if (!m_createVkSurface) {
123 m_createVkSurface = reinterpret_cast<PFN_vkCreateAndroidSurfaceKHR>(
124 inst->getInstanceProcAddr("vkCreateAndroidSurfaceKHR"));
125 }
126 if (!m_destroyVkSurface) {
127 m_destroyVkSurface = reinterpret_cast<PFN_vkDestroySurfaceKHR>(
128 inst->getInstanceProcAddr("vkDestroySurfaceKHR"));
129 }
130 VkResult err = m_createVkSurface(inst->vkInstance(), &surfaceInfo, nullptr, &m_vkSurface);
131 if (err != VK_SUCCESS)
132 qWarning("Failed to create Android VkSurface: %d", err);
133
134 needsExpose = true;
135 }
136
137 if (needsExpose)
138 sendExpose();
139
141
142 return &m_vkSurface;
143}
144
145QT_END_NAMESPACE
void applicationStateChanged(Qt::ApplicationState) override
void setGeometry(const QRect &rect) override
This function is called by Qt whenever a window is moved or resized using the QWindow API.
QSurfaceFormat format() const override
Returns the actual surface format of the window.
QAndroidPlatformWindow(QWindow *window)
void setGeometry(const QRect &rect) override
This function is called by Qt whenever a window is moved or resized using the QWindow API.
Combined button and popup list for selecting options.