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
8
9#include <QSurfaceFormat>
10#include <qpa/qplatformscreen.h>
11#include <qpa/qwindowsysteminterface.h>
12
13#include <android/native_window.h>
14#include <android/native_window_jni.h>
15
17
18using namespace Qt::StringLiterals;
19
28
30{
31 m_surfaceWaitCondition.wakeOne();
32 destroyAndClearSurface();
33}
34
36{
38
39 QRect availableGeometry = screen()->availableGeometry();
40 if (rect.width() > 0
41 && rect.height() > 0
42 && availableGeometry.width() > 0
43 && availableGeometry.height() > 0) {
44 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(0, 0), rect.size()));
45 }
46}
47
49{
50 QAndroidPlatformWindow::applicationStateChanged(state);
51 if (state <= Qt::ApplicationHidden) {
52 destroyAndClearSurface();
53 }
54}
55
57{
58 return window()->requestedFormat();
59}
60
62{
63 if (m_vkSurface && m_destroyVkSurface) {
64 m_destroyVkSurface(window()->vulkanInstance()->vkInstance(), m_vkSurface, nullptr);
65 m_vkSurface = 0;
66 }
67
68 if (m_nativeWindow) {
69 ANativeWindow_release(m_nativeWindow);
70 m_nativeWindow = nullptr;
71 }
72}
73
74void QAndroidPlatformVulkanWindow::destroyAndClearSurface()
75{
80}
81
83{
84 if (QAndroidEventDispatcherStopper::stopped() ||
85 QGuiApplication::applicationState() == Qt::ApplicationSuspended) {
86 qDebug(lcQpaWindow) << "Application not active, return existing surface.";
87 return &m_vkSurface;
88 }
89
90 bool needsExpose = false;
91 if (!m_vkSurface) {
93
94 QMutexLocker lock(&m_surfaceMutex);
96 QtAndroidPrivate::AndroidDeadlockProtector protector(
97 u"QAndroidPlatformVulkanWindow::vkSurface()"_s);
98 if (!protector.acquire())
99 return &m_vkSurface;
101 m_surfaceWaitCondition.wait(&m_surfaceMutex);
102 }
103
104 if (!m_androidSurfaceCreated || !m_androidSurfaceObject.isValid())
105 return &m_vkSurface;
106
107 QJniEnvironment env;
108 m_nativeWindow = ANativeWindow_fromSurface(env.jniEnv(), m_androidSurfaceObject.object());
109
110 VkAndroidSurfaceCreateInfoKHR surfaceInfo;
111 memset(&surfaceInfo, 0, sizeof(surfaceInfo));
112 surfaceInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
113 surfaceInfo.window = m_nativeWindow;
114 QVulkanInstance *inst = window()->vulkanInstance();
115 if (!inst) {
116 qWarning("Attempted to create Vulkan surface without an instance; was QWindow::setVulkanInstance() called?");
117 return &m_vkSurface;
118 }
119 if (!m_createVkSurface) {
120 m_createVkSurface = reinterpret_cast<PFN_vkCreateAndroidSurfaceKHR>(
121 inst->getInstanceProcAddr("vkCreateAndroidSurfaceKHR"));
122 }
123 if (!m_destroyVkSurface) {
124 m_destroyVkSurface = reinterpret_cast<PFN_vkDestroySurfaceKHR>(
125 inst->getInstanceProcAddr("vkDestroySurfaceKHR"));
126 }
127 VkResult err = m_createVkSurface(inst->vkInstance(), &surfaceInfo, nullptr, &m_vkSurface);
128 if (err != VK_SUCCESS)
129 qWarning("Failed to create Android VkSurface: %d", err);
130
131 needsExpose = true;
132 }
133
134 if (needsExpose)
135 sendExpose();
136
137 return &m_vkSurface;
138}
139
140QT_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.