forked from carlonluca/pot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
omx_videosurfaceelement.cpp
202 lines (180 loc) · 7.51 KB
/
omx_videosurfaceelement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Project: PiOmxTextures
* Author: Luca Carlon
* Date: 12.16.2012
*
* Copyright (c) 2012 Luca Carlon. All rights reserved.
*
* This file is part of PiOmxTextures.
*
* PiOmxTextures is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PiOmxTextures is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PiOmxTextures. If not, see <http://www.gnu.org/licenses/>.
*/
/*------------------------------------------------------------------------------
| includes
+-----------------------------------------------------------------------------*/
#include <QSGOpaqueTextureMaterial>
#include <QSGNode>
#include <QSGTextureProvider>
#include <QtConcurrent/QtConcurrent>
#include <QQuickWindow>
#include <QSurface>
// Private headers.
#include <private/qguiapplication_p.h>
#include <qpa/qplatformintegration.h>
#include <qpa/qplatformnativeinterface.h>
#include "omx_videosurfaceelement.h"
#include "omx_texture.h"
#include "lc_logging.h"
/*------------------------------------------------------------------------------
| OMX_ImageElement::OMX_ImageElement
+-----------------------------------------------------------------------------*/
OMX_VideoSurfaceElement::OMX_VideoSurfaceElement(QQuickItem *parent)
: QQuickItem(parent),
m_source(NULL),
#ifdef ENABLE_VIDEO_PROCESSOR
m_videoProc(NULL) // Cannot be init yet. EGL is not ready.
#elif ENABLE_MEDIA_PROCESSOR
m_mediaProc(NULL), // Cannot be init yet. EGL is not ready.
#else
m_mediaProc(NULL),
#endif
m_sgtexture(new OMX_SGTexture(0, QSize(0, 0))),
m_textureSize(QSize(0, 0)),
m_textureId(0)
{
setFlag(ItemHasContents, true);
// TODO: Avoid updating when not needed.
m_timer = new QTimer(this);
m_timer->setSingleShot(false);
connect(m_timer, SIGNAL(timeout()), this, SLOT(update()));
m_timer->start(40);
}
/*------------------------------------------------------------------------------
| OMX_ImageElement::~OMX_ImageElement
+-----------------------------------------------------------------------------*/
OMX_VideoSurfaceElement::~OMX_VideoSurfaceElement()
{
delete m_sgtexture;
}
/*------------------------------------------------------------------------------
| OMX_ImageElement::updatePaintNode
+-----------------------------------------------------------------------------*/
QSGNode* OMX_VideoSurfaceElement::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData*)
{
QSGGeometryNode* node = 0;
QSGGeometry* geometry = 0;
#ifdef ENABLE_PROFILING
static QElapsedTimer timer;
LOG_DEBUG(LOG_TAG, "Elapsed: %lld.", timer.restart());
#endif // ENABLE_PROFILING
if (!oldNode) {
// Create the node.
node = new QSGGeometryNode;
geometry = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4);
geometry->setDrawingMode(GL_TRIANGLE_STRIP);
node->setGeometry(geometry);
node->setFlag(QSGNode::OwnsGeometry);
// TODO: Who is freeing the material?
QSGOpaqueTextureMaterial* material = new QSGOpaqueTextureMaterial;
material->setTexture(m_sgtexture);
node->setMaterial(material);
node->setFlag(QSGNode::OwnsMaterial);
#ifdef ENABLE_VIDEO_PROCESSOR
QPlatformNativeInterface* nativeInterface =
QGuiApplicationPrivate::platformIntegration()->nativeInterface();
Q_ASSERT(nativeInterface);
EGLDisplay eglDisplay = nativeInterface->nativeResourceForIntegration("egldisplay");
EGLContext eglContext = nativeInterface->nativeResourceForContext(
"eglcontext",
QOpenGLContext::currentContext()
);
#endif
// Provider MUST be built in this thread.
#ifdef ENABLE_VIDEO_PROCESSOR
m_videoProc = new OMX_VideoProcessor(eglDisplay, eglContext, m_provider);
connect(m_videoProc, SIGNAL(textureReady(uint)), this, SLOT(onTextureChanged(uint)));
if (!m_source.isNull())
m_videoProc->setVideoPath(m_source);
if (m_playScheduled) {
m_timer->start(30);
m_videoProc->play();
}
#elif ENABLE_MEDIA_PROCESSOR
#else
LOG_VERBOSE(LOG_TAG, "Starting video...");
QtConcurrent::run(&startVideo, m_provider, this);
m_timer->start(30);
#endif
}
else {
node = static_cast<QSGGeometryNode*>(oldNode);
geometry = node->geometry();
//geometry->allocate(4);
// Texture has changed. Change the texture item for the scene graph.
if ((GLuint)m_sgtexture->textureId() != m_textureId) {
QMutexLocker locker(&m_mutexTexture);
m_sgtexture->setTexture(m_textureId, m_textureSize);
}
// This is needed since Qt 5.1.1.
node->markDirty(QSGNode::DirtyMaterial);
}
// Create the vertices and map to texture.
QRectF bounds = boundingRect();
QSGGeometry::TexturedPoint2D* vertices = geometry->vertexDataAsTexturedPoint2D();
vertices[3].set(bounds.x(), bounds.y() + bounds.height(), 0.0f, 1.0f);
vertices[2].set(bounds.x() + bounds.width(), bounds.y() + bounds.height(), 1.0f, 1.0f);
vertices[1].set(bounds.x(), bounds.y(), 0.0f, 0.0f);
vertices[0].set(bounds.x() + bounds.width(), bounds.y(), 1.0f, 0.0f);
return node;
}
/*------------------------------------------------------------------------------
| OMX_VideoSurfaceElement::onTextureChanged
+-----------------------------------------------------------------------------*/
void OMX_VideoSurfaceElement::setSource(QObject* source)
{
LOG_VERBOSE(LOG_TAG, "Source was set: %x.", (unsigned int)source);
if (m_source)
disconnect(m_source);
// Reset the current texture.
//onTextureChanged(0, QSize(0, 0));
onTextureInvalidated();
// Get notifications from the OMX_MediaProcessorElement related to this
// OMX_VideoSurfaceElement of the texture I should use.
m_source = (OMX_MediaProcessorElement*)source;
connect(m_source, SIGNAL(textureReady(const OMX_TextureData*)),
this, SLOT(onTextureChanged(const OMX_TextureData*)), Qt::DirectConnection);
connect(m_source, SIGNAL(textureInvalidated()),
this, SLOT(onTextureInvalidated()), Qt::DirectConnection);
emit sourceChanged(source);
}
/*------------------------------------------------------------------------------
| OMX_VideoSurfaceElement::onTextureChanged
+-----------------------------------------------------------------------------*/
void OMX_VideoSurfaceElement::onTextureChanged(const OMX_TextureData* textureData)
{
LOG_INFORMATION(LOG_TAG, "Setting new texture %u!", textureData->m_textureId);
QMutexLocker locker(&m_mutexTexture);
m_textureId = textureData->m_textureId;
m_textureSize = textureData->m_textureSize;
}
/*------------------------------------------------------------------------------
| OMX_VideoSurfaceElement::onTextureInvalidated
+-----------------------------------------------------------------------------*/
void OMX_VideoSurfaceElement::onTextureInvalidated()
{
LOG_VERBOSE(LOG_TAG, "Texture was invalidated. Switching to 0.");
QMutexLocker locker(&m_mutexTexture);
m_textureId = 0;
m_textureSize = QSize(0, 0);
}