Qt 6.x
The Qt SDK
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
6
8#include "androidjnimain.h"
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
26{
27}
28
30{
31 m_surfaceWaitCondition.wakeOne();
33 if (m_nativeSurfaceId != -1)
34 QtAndroid::destroySurface(m_nativeSurfaceId);
35 clearEgl();
37}
38
40{
41 // This is only for real raster top-level windows. Stop in all other cases.
42 if ((window()->surfaceType() == QSurface::RasterGLSurface && qt_window_private(window())->compositing)
43 || window()->surfaceType() == QSurface::OpenGLSurface
45 return;
46
47 QRect currentGeometry = geometry();
48
49 QRect dirtyClient = region.boundingRect();
50 QRect dirtyRegion(currentGeometry.left() + dirtyClient.left(),
51 currentGeometry.top() + dirtyClient.top(),
52 dirtyClient.width(),
53 dirtyClient.height());
54 QRect mOldGeometryLocal = m_oldGeometry;
55 m_oldGeometry = currentGeometry;
56 // If this is a move, redraw the previous location
57 if (mOldGeometryLocal != currentGeometry)
58 platformScreen()->setDirty(mOldGeometryLocal);
59 platformScreen()->setDirty(dirtyRegion);
60}
61
63{
64 if (rect == geometry())
65 return;
66
67 m_oldGeometry = geometry();
68
70 if (m_nativeSurfaceId != -1)
71 QtAndroid::setSurfaceGeometry(m_nativeSurfaceId, rect);
72
73 QRect availableGeometry = screen()->availableGeometry();
74 if (rect.width() > 0
75 && rect.height() > 0
76 && availableGeometry.width() > 0
77 && availableGeometry.height() > 0) {
79 }
80
81 if (rect.topLeft() != m_oldGeometry.topLeft())
83}
84
86{
88 return m_eglSurface;
89
91
92 if (m_nativeSurfaceId == -1) {
94 if (!protector.acquire())
95 return m_eglSurface;
96
97 const bool windowStaysOnTop = bool(window()->flags() & Qt::WindowStaysOnTopHint);
98 m_nativeSurfaceId = QtAndroid::createSurface(this, geometry(), windowStaysOnTop, 32);
99 m_surfaceWaitCondition.wait(&m_surfaceMutex);
100 }
101
102 if (m_eglSurface == EGL_NO_SURFACE) {
106 }
107 return m_eglSurface;
108}
109
111{
113 if (m_nativeSurfaceId == -1 || !m_androidSurfaceObject.isValid())
114 return false; // makeCurrent is NOT needed.
115
117
118 // we've create another surface, the window should be repainted
119 QRect availableGeometry = screen()->availableGeometry();
120 if (geometry().width() > 0 && geometry().height() > 0 && availableGeometry.width() > 0 && availableGeometry.height() > 0)
122 return true; // makeCurrent is needed!
123}
124
126{
129 lockSurface();
130 if (m_nativeSurfaceId != -1) {
131 QtAndroid::destroySurface(m_nativeSurfaceId);
132 m_nativeSurfaceId = -1;
133 }
134 clearEgl();
136 }
137}
138
140{
141 clearEgl();
142 QJniEnvironment env;
143 m_nativeWindow = ANativeWindow_fromSurface(env.jniEnv(), m_androidSurfaceObject.object());
144 m_androidSurfaceObject = QJniObject();
145 m_eglSurface = eglCreateWindowSurface(m_eglDisplay, config, m_nativeWindow, NULL);
146 m_format = q_glFormatFromConfig(m_eglDisplay, config, window()->requestedFormat());
147 if (Q_UNLIKELY(m_eglSurface == EGL_NO_SURFACE)) {
148 EGLint error = eglGetError();
149 eglTerminate(m_eglDisplay);
150 qFatal("EGL Error : Could not create the egl surface: error = 0x%x\n", error);
151 }
152}
153
155{
156 if (m_nativeWindow == 0)
157 return window()->requestedFormat();
158
159 return m_format;
160}
161
163{
164 if (m_eglSurface != EGL_NO_SURFACE) {
165 eglMakeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
166 eglDestroySurface(m_eglDisplay, m_eglSurface);
167 m_eglSurface = EGL_NO_SURFACE;
168 }
169
170 if (m_nativeWindow) {
171 ANativeWindow_release(m_nativeWindow);
172 m_nativeWindow = 0;
173 }
174}
175
176void QAndroidPlatformOpenGLWindow::surfaceChanged(JNIEnv *jniEnv, jobject surface, int w, int h)
177{
178 Q_UNUSED(jniEnv);
179 Q_UNUSED(w);
180 Q_UNUSED(h);
181
182 lockSurface();
183 m_androidSurfaceObject = surface;
184 if (surface) // wait until we have a valid surface to draw into
185 m_surfaceWaitCondition.wakeOne();
187
188 if (surface) {
189 // repaint the window, when we have a valid surface
190 QRect availableGeometry = screen()->availableGeometry();
191 if (geometry().width() > 0 && geometry().height() > 0 && availableGeometry.width() > 0 && availableGeometry.height() > 0)
193 }
194}
195
void repaint(const QRegion &region) override
void applicationStateChanged(Qt::ApplicationState) override
EGLSurface eglSurface(EGLConfig config)
void surfaceChanged(JNIEnv *jniEnv, jobject surface, int w, int h) override
void setGeometry(const QRect &rect) override
This function is called by Qt whenever a window is moved or resized using the QWindow API.
QAndroidPlatformOpenGLWindow(QWindow *window, EGLDisplay display)
QSurfaceFormat format() const override
Returns the actual surface format of the window.
void setDirty(const QRect &rect)
virtual void applicationStateChanged(Qt::ApplicationState)
QAndroidPlatformScreen * platformScreen() const
void setGeometry(const QRect &rect) override
This function is called by Qt whenever a window is moved or resized using the QWindow API.
static Qt::ApplicationState applicationState()
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qmutex.h:317
void unlock() noexcept
Unlocks the mutex.
Definition qmutex.h:293
void lock() noexcept
Locks the mutex.
Definition qmutex.h:290
virtual QRect availableGeometry() const
Reimplement in subclass to return the pixel geometry of the available space This normally is the desk...
QSurface * surface() const
QWindow * window() const
Returns the window which belongs to the QPlatformWindow.
QPlatformScreen * screen() const override
Returns the platform screen handle corresponding to this platform window, or null if the window is no...
QPlatformWindow * parent() const
Returns the parent platform window (or \nullptr if orphan).
virtual QRect geometry() const
Returns the current geometry of a window.
\inmodule QtCore\reentrant
Definition qpoint.h:23
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr int height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:238
constexpr QPoint topLeft() const noexcept
Returns the position of the rectangle's top-left corner.
Definition qrect.h:220
constexpr int top() const noexcept
Returns the y-coordinate of the rectangle's top edge.
Definition qrect.h:175
constexpr int left() const noexcept
Returns the x-coordinate of the rectangle's left edge.
Definition qrect.h:172
constexpr int width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:235
The QRegion class specifies a clip region for a painter.
Definition qregion.h:27
QRect boundingRect() const noexcept
Returns the bounding rectangle of this region.
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
@ RasterGLSurface
Definition qsurface.h:33
@ OpenGLSurface
Definition qsurface.h:32
bool wait(QMutex *, QDeadlineTimer=QDeadlineTimer(QDeadlineTimer::Forever))
static bool handleExposeEvent(QWindow *window, const QRegion &region)
\inmodule QtGui
Definition qwindow.h:63
rect
[4]
else opt state
[0]
struct wl_display * display
Definition linuxdmabuf.h:41
Combined button and popup list for selecting options.
void destroySurface(int surfaceId)
void setSurfaceGeometry(int surfaceId, const QRect &geometry)
int createSurface(AndroidSurfaceClient *client, const QRect &geometry, bool onTop, int imageDepth)
ApplicationState
Definition qnamespace.h:261
@ ApplicationHidden
Definition qnamespace.h:263
@ ApplicationSuspended
Definition qnamespace.h:262
@ WindowStaysOnTopHint
Definition qnamespace.h:232
#define Q_UNLIKELY(x)
DBusConnection const char DBusError * error
QSurfaceFormat q_glFormatFromConfig(EGLDisplay display, const EGLConfig config, const QSurfaceFormat &referenceFormat)
EGLConfig config
typedef EGLSurface(EGLAPIENTRYP PFNEGLCREATESTREAMPRODUCERSURFACEKHRPROC)(EGLDisplay dpy
typedef EGLDisplay(EGLAPIENTRYP PFNEGLGETPLATFORMDISPLAYEXTPROC)(EGLenum platform
#define qFatal
Definition qlogging.h:164
GLfloat GLfloat GLfloat w
[0]
GLint GLsizei GLsizei height
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei width
GLbitfield flags
GLfloat GLfloat GLfloat GLfloat h
#define Q_UNUSED(x)
Q_GUI_EXPORT QWindowPrivate * qt_window_private(QWindow *window)
Definition qwindow.cpp:2864
QReadWriteLock lock
[0]
aWidget window() -> setWindowTitle("New Window Title")
[2]