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
qandroidplatformopenglwindow.cpp
Go to the documentation of this file.
1// Copyright (C) 2014 BogDan Vatra <bogdan@kde.org>
2// Copyright (C) 2016 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
7
11
12#include <QSurfaceFormat>
13#include <QtGui/private/qwindow_p.h>
14#include <QtGui/qguiapplication.h>
15
16#include <QtGui/private/qeglconvenience_p.h>
17#include <android/native_window.h>
18#include <android/native_window_jni.h>
19#include <qpa/qplatformscreen.h>
20#include <qpa/qwindowsysteminterface.h>
21
23
24QAndroidPlatformOpenGLWindow::QAndroidPlatformOpenGLWindow(QWindow *window, EGLDisplay display)
25 :QAndroidPlatformWindow(window), m_eglDisplay(display)
26{ }
27
28QAndroidPlatformOpenGLWindow::~QAndroidPlatformOpenGLWindow()
29{
30 m_surfaceWaitCondition.wakeOne();
31 lockSurface();
32 destroySurface();
33 clearSurface();
34 unlockSurface();
35}
36
37void QAndroidPlatformOpenGLWindow::setGeometry(const QRect &rect)
38{
39 QAndroidPlatformWindow::setGeometry(rect);
40
41 QRect availableGeometry = screen()->availableGeometry();
42 if (rect.width() > 0
43 && rect.height() > 0
44 && availableGeometry.width() > 0
45 && availableGeometry.height() > 0) {
46 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(0, 0), rect.size()));
47 }
48}
49
50// Called by QAndroidPlatformOpenGLContext::eglSurfaceForPlatformSurface(),
51// surface is already locked when calling this
52EGLSurface QAndroidPlatformOpenGLWindow::eglSurface(EGLConfig config)
53{
54 // Avoid early return on initial suspension unless a surface was already created.
55 // This avoids the multi‑window startup race.
56 if (m_androidSurfaceCreated &&
57 (QAndroidEventDispatcherStopper::stopped() ||
58 QGuiApplication::applicationState() == Qt::ApplicationSuspended)) {
59 qCDebug(lcQpaWindow) << "Application not active, return existing surface.";
60 return m_eglSurface;
61 }
62 // If we haven't called createSurface() yet, call it and wait until Android has created
63 // the Surface
64 if (!m_androidSurfaceCreated) {
65 static constexpr char funcName[] = "QAndroidPlatformOpenGLWindow::eglSurface()";
66 QtAndroidPrivate::AndroidDeadlockProtector protector(funcName);
67 if (!protector.acquire()) {
68 qFatal("Failed to acquire deadlock protector for %s.", funcName);
69 return m_eglSurface;
70 }
71
73 qCDebug(lcQpaWindow) << "called createSurface(), waiting for Surface to be ready...";
74 m_surfaceWaitCondition.wait(&m_surfaceMutex);
75 }
76
77 if (m_eglSurface == EGL_NO_SURFACE)
78 ensureEglSurfaceCreated(config);
79
80 return m_eglSurface;
81}
82
83void QAndroidPlatformOpenGLWindow::applicationStateChanged(Qt::ApplicationState state)
84{
85 if (state <= Qt::ApplicationHidden) {
88 clearSurface();
90 }
91 QAndroidPlatformWindow::applicationStateChanged(state);
92}
93
94// m_surfaceMutex already locked, called only by eglSurface()
95// and QAndroidPlatformOpenGLContext::swapBuffers().
96bool QAndroidPlatformOpenGLWindow::ensureEglSurfaceCreated(EGLConfig config)
97{
98 // Either no surface created, or the m_eglSurface already wraps the active Surface,
99 // so makeCurrent is NOT needed, and we should not create a new EGL surface.
100 if (!m_androidSurfaceCreated || !m_androidSurfaceObject.isValid()) {
101 qCDebug(lcQpaWindow) << "Skipping create egl on invalid or not yet created surface";
102 return false;
103 }
104
105 clearSurface();
106 m_nativeWindow = ANativeWindow_fromSurface(
107 QJniEnvironment::getJniEnv(), m_androidSurfaceObject.object());
108 m_androidSurfaceObject = QJniObject();
109 m_eglSurface = eglCreateWindowSurface(m_eglDisplay, config, m_nativeWindow, NULL);
110 m_format = q_glFormatFromConfig(m_eglDisplay, config, window()->requestedFormat());
111 if (Q_UNLIKELY(m_eglSurface == EGL_NO_SURFACE)) {
112 EGLint error = eglGetError();
113 eglTerminate(m_eglDisplay);
114 qFatal("EGL Error : Could not create the egl surface: error = 0x%x\n", error);
115 }
116
117 // we've created another Surface, the window should be repainted
118 sendExpose();
119
121
122 return true;
123}
124
125QSurfaceFormat QAndroidPlatformOpenGLWindow::format() const
126{
127 if (m_nativeWindow == 0)
128 return window()->requestedFormat();
129
130 return m_format;
131}
132
133void QAndroidPlatformOpenGLWindow::clearSurface()
134{
135 if (m_eglSurface != EGL_NO_SURFACE) {
136 eglMakeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
137 eglDestroySurface(m_eglDisplay, m_eglSurface);
138 m_eglSurface = EGL_NO_SURFACE;
139 }
140
141 if (m_nativeWindow) {
142 ANativeWindow_release(m_nativeWindow);
143 m_nativeWindow = nullptr;
144 }
145
147}
148
149QT_END_NAMESPACE
\inmodule QtGui
Definition qwindow.h:64
Combined button and popup list for selecting options.