Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
androidsurfaceview.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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
5
6#include <QtCore/qcoreapplication.h>
7#include <QtCore/qdebug.h>
8#include <QtCore/qlist.h>
9#include <QtCore/qmutex.h>
10#include <QtGui/qwindow.h>
11
13
14static const char QtSurfaceHolderCallbackClassName[] = "org/qtproject/qt/android/multimedia/QtSurfaceHolderCallback";
15typedef QList<AndroidSurfaceHolder *> SurfaceHolders;
16Q_GLOBAL_STATIC(SurfaceHolders, surfaceHolders)
18
19AndroidSurfaceHolder::AndroidSurfaceHolder(QJniObject object)
20 : m_surfaceHolder(object)
21 , m_surfaceCreated(false)
22{
23 if (!m_surfaceHolder.isValid())
24 return;
25
26 {
27 QMutexLocker locker(shLock());
28 surfaceHolders->append(this);
29 }
30
31 QJniObject callback(QtSurfaceHolderCallbackClassName, "(J)V", reinterpret_cast<jlong>(this));
32 m_surfaceHolder.callMethod<void>("addCallback",
33 "(Landroid/view/SurfaceHolder$Callback;)V",
34 callback.object());
35}
36
38{
39 QMutexLocker locker(shLock());
40 const int i = surfaceHolders->indexOf(this);
41 if (Q_UNLIKELY(i == -1))
42 return;
43
44 surfaceHolders->remove(i);
45}
46
48{
49 return m_surfaceHolder.object();
50}
51
53{
54 QMutexLocker locker(shLock());
55 return m_surfaceCreated;
56}
57
58void AndroidSurfaceHolder::handleSurfaceCreated(JNIEnv*, jobject, jlong id)
59{
60 QMutexLocker locker(shLock());
61 const int i = surfaceHolders->indexOf(reinterpret_cast<AndroidSurfaceHolder *>(id));
62 if (Q_UNLIKELY(i == -1))
63 return;
64
65 (*surfaceHolders)[i]->m_surfaceCreated = true;
66 Q_EMIT (*surfaceHolders)[i]->surfaceCreated();
67}
68
69void AndroidSurfaceHolder::handleSurfaceDestroyed(JNIEnv*, jobject, jlong id)
70{
71 QMutexLocker locker(shLock());
72 const int i = surfaceHolders->indexOf(reinterpret_cast<AndroidSurfaceHolder *>(id));
73 if (Q_UNLIKELY(i == -1))
74 return;
75
76 (*surfaceHolders)[i]->m_surfaceCreated = false;
77}
78
80{
81 static const JNINativeMethod methods[] = {
82 {"notifySurfaceCreated", "(J)V", (void *)AndroidSurfaceHolder::handleSurfaceCreated},
83 {"notifySurfaceDestroyed", "(J)V", (void *)AndroidSurfaceHolder::handleSurfaceDestroyed}
84 };
85
86 const int size = std::size(methods);
87 return QJniEnvironment().registerNativeMethods(QtSurfaceHolderCallbackClassName, methods, size);
88}
89
91 : m_window(0)
92 , m_surfaceHolder(0)
93 , m_pendingVisible(-1)
94{
95 QNativeInterface::QAndroidApplication::runOnAndroidMainThread([this] {
96 m_surfaceView = QJniObject("android/view/SurfaceView",
97 "(Landroid/content/Context;)V",
98 QNativeInterface::QAndroidApplication::context().object());
99 }).waitForFinished();
100
101 Q_ASSERT(m_surfaceView.isValid());
102
103 QJniObject holder = m_surfaceView.callObjectMethod("getHolder",
104 "()Landroid/view/SurfaceHolder;");
105 if (!holder.isValid()) {
106 m_surfaceView = QJniObject();
107 } else {
108 m_surfaceHolder = new AndroidSurfaceHolder(holder);
111 { // Lock now to avoid a race with handleSurfaceCreated()
112 QMutexLocker locker(shLock());
113 m_window = QWindow::fromWinId(WId(m_surfaceView.object()));
114
115 if (m_pendingVisible != -1)
116 m_window->setVisible(m_pendingVisible);
117 if (m_pendingGeometry.isValid())
118 m_window->setGeometry(m_pendingGeometry);
119 }
120 }
121}
122
124{
125 delete m_surfaceHolder;
126 delete m_window;
127}
128
130{
131 return m_surfaceHolder;
132}
133
135{
136 if (m_window)
137 m_window->setVisible(v);
138 else
139 m_pendingVisible = int(v);
140}
141
143{
144 if (m_window)
145 m_window->setGeometry(x, y, width, height);
146 else
147 m_pendingGeometry = QRect(x, y, width, height);
148}
149
151
152#include "moc_androidsurfaceview_p.cpp"
static JNINativeMethod methods[]
static QT_BEGIN_NAMESPACE const char QtSurfaceHolderCallbackClassName[]
QList< AndroidSurfaceHolder * > SurfaceHolders
jobject surfaceHolder() const
static bool registerNativeMethods()
AndroidSurfaceHolder * holder() const
void setGeometry(int x, int y, int width, int height)
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qmutex.h:313
\inmodule QtCore
Definition qmutex.h:281
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr bool isValid() const noexcept
Returns true if the rectangle is valid, otherwise returns false.
Definition qrect.h:170
void setGeometry(int posx, int posy, int w, int h)
Sets the geometry of the window, excluding its window frame, to a rectangle constructed from posx,...
Definition qwindow.cpp:1802
void setVisible(bool visible)
Definition qwindow.cpp:689
Combined button and popup list for selecting options.
#define Q_UNLIKELY(x)
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
GLsizei const GLfloat * v
[13]
GLint GLint GLint GLint GLint x
[0]
GLint GLsizei GLsizei height
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint object
[3]
GLint GLsizei width
GLint y
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_EMIT
future waitForFinished()