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>
16Q_GLOBAL_STATIC(SurfaceHolders, surfaceHolders)
17Q_GLOBAL_STATIC(QMutex, shLock)
19AndroidSurfaceHolder::AndroidSurfaceHolder(QJniObject object)
20 : m_surfaceHolder(object)
21 , m_surfaceCreated(
false)
23 if (!m_surfaceHolder.isValid())
27 QMutexLocker locker(shLock());
28 surfaceHolders->append(
this);
31 QJniObject callback(QtSurfaceHolderCallbackClassName,
"(J)V",
reinterpret_cast<jlong>(
this));
32 m_surfaceHolder.callMethod<
void>(
"addCallback",
33 "(Landroid/view/SurfaceHolder$Callback;)V",
37AndroidSurfaceHolder::~AndroidSurfaceHolder()
39 QMutexLocker locker(shLock());
40 const int i = surfaceHolders->indexOf(
this);
41 if (Q_UNLIKELY(i == -1))
44 surfaceHolders->remove(i);
47jobject AndroidSurfaceHolder::surfaceHolder()
const
49 return m_surfaceHolder.object();
52bool AndroidSurfaceHolder::isSurfaceCreated()
const
54 QMutexLocker locker(shLock());
55 return m_surfaceCreated;
58void AndroidSurfaceHolder::handleSurfaceCreated(JNIEnv*, jobject, jlong id)
60 QMutexLocker locker(shLock());
61 const int i = surfaceHolders->indexOf(
reinterpret_cast<AndroidSurfaceHolder *>(id));
62 if (Q_UNLIKELY(i == -1))
65 (*surfaceHolders)[i]->m_surfaceCreated =
true;
66 Q_EMIT (*surfaceHolders)[i]->surfaceCreated();
69void AndroidSurfaceHolder::handleSurfaceDestroyed(JNIEnv*, jobject, jlong id)
71 QMutexLocker locker(shLock());
72 const int i = surfaceHolders->indexOf(
reinterpret_cast<AndroidSurfaceHolder *>(id));
73 if (Q_UNLIKELY(i == -1))
76 (*surfaceHolders)[i]->m_surfaceCreated =
false;
79bool AndroidSurfaceHolder::registerNativeMethods()
81 static const JNINativeMethod methods[] = {
82 {
"notifySurfaceCreated",
"(J)V", (
void *)AndroidSurfaceHolder::handleSurfaceCreated},
83 {
"notifySurfaceDestroyed",
"(J)V", (
void *)AndroidSurfaceHolder::handleSurfaceDestroyed}
86 const int size = std::size(methods);
87 return QJniEnvironment().registerNativeMethods(QtSurfaceHolderCallbackClassName, methods, size);
90AndroidSurfaceView::AndroidSurfaceView()
93 , m_pendingVisible(-1)
95 QNativeInterface::QAndroidApplication::runOnAndroidMainThread([
this] {
96 m_surfaceView = QJniObject(
"android/view/SurfaceView",
97 "(Landroid/content/Context;)V",
98 QNativeInterface::QAndroidApplication::context().object());
101 Q_ASSERT(m_surfaceView.isValid());
103 QJniObject holder = m_surfaceView.callObjectMethod(
"getHolder",
104 "()Landroid/view/SurfaceHolder;");
105 if (!holder.isValid()) {
106 m_surfaceView = QJniObject();
108 m_surfaceHolder =
new AndroidSurfaceHolder(holder);
109 connect(m_surfaceHolder, &AndroidSurfaceHolder::surfaceCreated,
110 this, &AndroidSurfaceView::surfaceCreated);
112 QMutexLocker locker(shLock());
113 m_window = QWindow::fromWinId(WId(m_surfaceView.object()));
115 if (m_pendingVisible != -1)
116 m_window->setVisible(m_pendingVisible);
117 if (m_pendingGeometry.isValid())
118 m_window->setGeometry(m_pendingGeometry);
123AndroidSurfaceView::~AndroidSurfaceView()
125 delete m_surfaceHolder;
129AndroidSurfaceHolder *AndroidSurfaceView::holder()
const
131 return m_surfaceHolder;
134void AndroidSurfaceView::setVisible(
bool v)
137 m_window->setVisible(v);
139 m_pendingVisible =
int(v);
142void AndroidSurfaceView::setGeometry(
int x,
int y,
int width,
int height)
145 m_window->setGeometry(x, y, width, height);
147 m_pendingGeometry = QRect(x, y, width, height);
152#include "moc_androidsurfaceview_p.cpp"
QList< AndroidSurfaceHolder * > SurfaceHolders
static QT_BEGIN_NAMESPACE const char QtSurfaceHolderCallbackClassName[]