Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
androidsurfacetexture.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
4
#
include
"androidsurfacetexture_p.h"
5
#
include
<
QtCore
/
qmutex
.
h
>
6
#
include
<
QtCore
/
qcoreapplication
.
h
>
7
8
QT_BEGIN_NAMESPACE
9
10
static
const
char
QtSurfaceTextureListenerClassName
[] =
"org/qtproject/qt/android/multimedia/QtSurfaceTextureListener"
;
11
typedef
QList
<
jlong
>
SurfaceTextures
;
12
Q_GLOBAL_STATIC
(
SurfaceTextures
,
g_surfaceTextures
);
13
Q_GLOBAL_STATIC
(
QMutex
,
g_textureMutex
);
14
15
static
QAtomicInteger
<
quint64
>
indexCounter
= 0u;
16
17
// native method for QtSurfaceTexture.java
18
static
void
notifyFrameAvailable
(JNIEnv* ,
jobject
, jlong id)
19
{
20
const
QMutexLocker lock(g_textureMutex());
21
const
int
idx = g_surfaceTextures->indexOf(id);
22
if
(idx == -1)
23
return
;
24
25
AndroidSurfaceTexture
*obj =
reinterpret_cast
<AndroidSurfaceTexture *>(g_surfaceTextures->at(idx));
26
if
(obj)
27
Q_EMIT obj->frameAvailable();
28
}
29
30
AndroidSurfaceTexture
::AndroidSurfaceTexture(quint32 texName)
31
: QObject()
32
, m_index(indexCounter.fetchAndAddRelaxed(1))
33
{
34
Q_STATIC_ASSERT(
sizeof
(jlong) >=
sizeof
(
void
*));
35
m_surfaceTexture = QJniObject(
"android/graphics/SurfaceTexture"
,
"(I)V"
, jint(texName));
36
37
if
(!m_surfaceTexture.isValid())
38
return
;
39
40
const
QMutexLocker lock(g_textureMutex());
41
g_surfaceTextures->append(jlong(
this
));
42
QJniObject listener(
QtSurfaceTextureListenerClassName
,
"(J)V"
, jlong(
this
));
43
setOnFrameAvailableListener(listener);
44
}
45
46
AndroidSurfaceTexture
::~
AndroidSurfaceTexture
()
47
{
48
if
(m_surface.isValid())
49
m_surface.callMethod<
void
>(
"release"
);
50
51
if
(m_surfaceTexture.isValid()) {
52
release
(
)
;
53
const
QMutexLocker lock(g_textureMutex());
54
const
int
idx = g_surfaceTextures->indexOf(jlong(
this
));
55
if
(idx != -1)
56
g_surfaceTextures->remove(idx);
57
}
58
}
59
60
QMatrix4x4
AndroidSurfaceTexture
::
getTransformMatrix
()
61
{
62
QMatrix4x4 matrix;
63
if
(!m_surfaceTexture.isValid())
64
return
matrix;
65
66
QJniEnvironment env;
67
jfloatArray array = env->NewFloatArray(16);
68
m_surfaceTexture.callMethod<
void
>(
"getTransformMatrix"
,
"([F)V"
, array);
69
env->GetFloatArrayRegion(array, 0, 16, matrix.data());
70
env->DeleteLocalRef(array);
71
72
return
matrix;
73
}
74
75
void
AndroidSurfaceTexture
::
release
()
76
{
77
m_surfaceTexture.callMethod<
void
>(
"release"
);
78
}
79
80
void
AndroidSurfaceTexture
::
updateTexImage
()
81
{
82
if
(!m_surfaceTexture.isValid())
83
return
;
84
85
m_surfaceTexture.callMethod<
void
>(
"updateTexImage"
);
86
}
87
88
jobject
AndroidSurfaceTexture
::
surfaceTexture
()
89
{
90
return
m_surfaceTexture.object();
91
}
92
93
jobject
AndroidSurfaceTexture
::
surface
()
94
{
95
if
(!m_surface.isValid()) {
96
m_surface = QJniObject(
"android/view/Surface"
,
97
"(Landroid/graphics/SurfaceTexture;)V"
,
98
m_surfaceTexture.object());
99
}
100
101
return
m_surface.object();
102
}
103
104
jobject
AndroidSurfaceTexture
::
surfaceHolder
()
105
{
106
if
(!m_surfaceHolder.isValid()) {
107
m_surfaceHolder = QJniObject(
"org/qtproject/qt/android/multimedia/QtSurfaceTextureHolder"
,
108
"(Landroid/view/Surface;)V"
,
109
surface());
110
}
111
112
return
m_surfaceHolder.object();
113
}
114
115
void
AndroidSurfaceTexture
::
attachToGLContext
(quint32 texName)
116
{
117
if
(!m_surfaceTexture.isValid())
118
return
;
119
120
m_surfaceTexture.callMethod<
void
>(
"attachToGLContext"
,
"(I)V"
, texName);
121
}
122
123
void
AndroidSurfaceTexture
::
detachFromGLContext
()
124
{
125
if
(!m_surfaceTexture.isValid())
126
return
;
127
128
m_surfaceTexture.callMethod<
void
>(
"detachFromGLContext"
);
129
}
130
131
bool
AndroidSurfaceTexture
::
registerNativeMethods
()
132
{
133
static
const
JNINativeMethod methods[] = {
134
{
"notifyFrameAvailable"
,
"(J)V"
, (
void
*)notifyFrameAvailable}
135
};
136
const
int
size = std::size(methods);
137
return
QJniEnvironment().registerNativeMethods(
QtSurfaceTextureListenerClassName
, methods, size);
138
}
139
140
void
AndroidSurfaceTexture
::setOnFrameAvailableListener(
const
QJniObject &listener)
141
{
142
m_surfaceTexture.callMethod<
void
>(
"setOnFrameAvailableListener"
,
143
"(Landroid/graphics/SurfaceTexture$OnFrameAvailableListener;)V"
,
144
listener.object());
145
}
146
147
QT_END_NAMESPACE
148
149
#
include
"moc_androidsurfacetexture_p.cpp"
indexCounter
static QAtomicInteger< quint64 > indexCounter
Definition
androidsurfacetexture.cpp:15
notifyFrameAvailable
static void notifyFrameAvailable(JNIEnv *, jobject, jlong id)
Definition
androidsurfacetexture.cpp:18
SurfaceTextures
QList< jlong > SurfaceTextures
Definition
androidsurfacetexture.cpp:11
QtSurfaceTextureListenerClassName
static QT_BEGIN_NAMESPACE const char QtSurfaceTextureListenerClassName[]
Definition
androidsurfacetexture.cpp:10
AndroidSurfaceTexture
Definition
androidsurfacetexture_p.h:26
AndroidSurfaceTexture::surface
jobject surface()
Definition
androidsurfacetexture.cpp:93
AndroidSurfaceTexture::surfaceHolder
jobject surfaceHolder()
Definition
androidsurfacetexture.cpp:104
AndroidSurfaceTexture::~AndroidSurfaceTexture
~AndroidSurfaceTexture()
Definition
androidsurfacetexture.cpp:46
AndroidSurfaceTexture::updateTexImage
void updateTexImage()
Definition
androidsurfacetexture.cpp:80
AndroidSurfaceTexture::attachToGLContext
void attachToGLContext(quint32 texName)
Definition
androidsurfacetexture.cpp:115
AndroidSurfaceTexture::release
void release()
Definition
androidsurfacetexture.cpp:75
AndroidSurfaceTexture::registerNativeMethods
static bool registerNativeMethods()
Definition
androidsurfacetexture.cpp:131
AndroidSurfaceTexture::surfaceTexture
jobject surfaceTexture()
Definition
androidsurfacetexture.cpp:88
AndroidSurfaceTexture::getTransformMatrix
QMatrix4x4 getTransformMatrix()
Definition
androidsurfacetexture.cpp:60
AndroidSurfaceTexture::detachFromGLContext
void detachFromGLContext()
Definition
androidsurfacetexture.cpp:123
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
Q_GLOBAL_STATIC
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
qtmultimedia
src
plugins
multimedia
android
wrappers
jni
androidsurfacetexture.cpp
Generated on
for Qt by
1.18.0