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 if (QAndroidEventDispatcherStopper::stopped() ||
55 QGuiApplication::applicationState() == Qt::ApplicationSuspended) {
56 qCDebug(lcQpaWindow) << "Application not active, return existing surface.";
57 return m_eglSurface;
58 }
59 // If we haven't called createSurface() yet, call it and wait until Android has created
60 // the Surface
61 if (!m_androidSurfaceCreated) {
62 static constexpr char funcName[] = "QAndroidPlatformOpenGLWindow::eglSurface()";
63 QtAndroidPrivate::AndroidDeadlockProtector protector(funcName);
64 if (!protector.acquire()) {
65 qFatal("Failed to acquire deadlock protector for %s.", funcName);
66 return m_eglSurface;
67 }
68
70 qCDebug(lcQpaWindow) << "called createSurface(), waiting for Surface to be ready...";
71 m_surfaceWaitCondition.wait(&m_surfaceMutex);
72 }
73
74 if (m_eglSurface == EGL_NO_SURFACE)
75 ensureEglSurfaceCreated(config);
76
77 return m_eglSurface;
78}
79
80void QAndroidPlatformOpenGLWindow::applicationStateChanged(Qt::ApplicationState state)
81{
82 QAndroidPlatformWindow::applicationStateChanged(state);
83 if (state <= Qt::ApplicationHidden) {
86 clearSurface();
88 }
89}
90
91// m_surfaceMutex already locked, called only by eglSurface()
92// and QAndroidPlatformOpenGLContext::swapBuffers().
93bool QAndroidPlatformOpenGLWindow::ensureEglSurfaceCreated(EGLConfig config)
94{
95 // Either no surface created, or the m_eglSurface already wraps the active Surface,
96 // so makeCurrent is NOT needed, and we should not create a new EGL surface.
97 if (!m_androidSurfaceCreated || !m_androidSurfaceObject.isValid()) {
98 qCDebug(lcQpaWindow) << "Skipping create egl on invalid or not yet created surface";
99 return false;
100 }
101
102 clearSurface();
103 m_nativeWindow = ANativeWindow_fromSurface(
104 QJniEnvironment::getJniEnv(), m_androidSurfaceObject.object());
105 m_androidSurfaceObject = QJniObject();
106 m_eglSurface = eglCreateWindowSurface(m_eglDisplay, config, m_nativeWindow, NULL);
107 m_format = q_glFormatFromConfig(m_eglDisplay, config, window()->requestedFormat());
108 if (Q_UNLIKELY(m_eglSurface == EGL_NO_SURFACE)) {
109 EGLint error = eglGetError();
110 eglTerminate(m_eglDisplay);
111 qFatal("EGL Error : Could not create the egl surface: error = 0x%x\n", error);
112 }
113
114 // we've created another Surface, the window should be repainted
115 sendExpose();
116
118
119 return true;
120}
121
122QSurfaceFormat QAndroidPlatformOpenGLWindow::format() const
123{
124 if (m_nativeWindow == 0)
125 return window()->requestedFormat();
126
127 return m_format;
128}
129
130void QAndroidPlatformOpenGLWindow::clearSurface()
131{
132 if (m_eglSurface != EGL_NO_SURFACE) {
133 eglMakeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
134 eglDestroySurface(m_eglDisplay, m_eglSurface);
135 m_eglSurface = EGL_NO_SURFACE;
136 }
137
138 if (m_nativeWindow) {
139 ANativeWindow_release(m_nativeWindow);
140 m_nativeWindow = nullptr;
141 }
142
144}
145
146QT_END_NAMESPACE
\inmodule QtGui
Definition qwindow.h:63
Combined button and popup list for selecting options.