forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SGVideoNode.cpp
204 lines (172 loc) · 6.88 KB
/
SGVideoNode.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
203
204
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV (from 2014)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "QmlAV/SGVideoNode.h"
#include "QtAV/VideoShader.h"
#include "QtAV/VideoFrame.h"
#include <QtCore/QScopedPointer>
#include <QtGui/QOpenGLFunctions>
#include <QtQuick/QSGMaterial>
#include <QtQuick/QSGMaterialShader>
// all in QSGRenderThread
namespace QtAV {
class SGVideoMaterialShader : public QSGMaterialShader
{
public:
SGVideoMaterialShader(VideoShader* s) :
QSGMaterialShader()
, m_shader(s)
{}
virtual void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial);
virtual char const *const *attributeNames() const { return m_shader->attributeNames();}
protected:
virtual const char *vertexShader() const { return m_shader->vertexShader();}
virtual const char *fragmentShader() const { return m_shader->fragmentShader();}
virtual void initialize() { m_shader->initialize(program());}
int textureLocationCount() const { return m_shader->textureLocationCount();}
int textureLocation(int index) const { return m_shader->textureLocation(index);}
int matrixLocation() const { return m_shader->matrixLocation();}
int colorMatrixLocation() const { return m_shader->colorMatrixLocation();}
int opacityLocation() const { return m_shader->opacityLocation();}
private:
QScopedPointer<VideoShader> m_shader;
};
class SGVideoMaterial : public QSGMaterial
{
public:
SGVideoMaterial() : QSGMaterial(), m_opacity(1.0) {}
virtual QSGMaterialType *type() const {
return reinterpret_cast<QSGMaterialType*>((quintptr)m_material.type());
}
virtual QSGMaterialShader *createShader() const {
return new SGVideoMaterialShader(m_material.createShader());
}
virtual int compare(const QSGMaterial *other) const {
Q_ASSERT(other && type() == other->type());
const SGVideoMaterial *m = static_cast<const SGVideoMaterial*>(other);
return m_material.compare(&m->m_material);
}
/*
void updateBlending() {
setFlag(Blending, m_material.hasAlpha() || !qFuzzyCompare(m_opacity, qreal(1.0)));
}
*/
void setCurrentFrame(const VideoFrame &frame) {
m_material.setCurrentFrame(frame);
setFlag(Blending, frame.format().hasAlpha());
}
VideoMaterial* videoMaterial() { return &m_material;}
qreal m_opacity;
VideoMaterial m_material;
};
void SGVideoMaterialShader::updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
{
Q_UNUSED(oldMaterial);
SGVideoMaterial *mat = static_cast<SGVideoMaterial *>(newMaterial);
if (!m_shader->update(&mat->m_material)) //material not ready. e.g. video item have not got a frame
return;
//mat->updateBlending();
if (state.isOpacityDirty()) {
mat->m_opacity = state.opacity();
program()->setUniformValue(opacityLocation(), GLfloat(mat->m_opacity));
}
if (state.isMatrixDirty())
program()->setUniformValue(matrixLocation(), state.combinedMatrix());
}
SGVideoNode::SGVideoNode()
: QSGGeometryNode()
, m_material(new SGVideoMaterial())
, m_validWidth(1.0)
{
setFlag(QSGNode::OwnsGeometry);
setFlag(QSGNode::OwnsMaterial);
setMaterial(m_material);
}
SGVideoNode::~SGVideoNode() {}
void SGVideoNode::setCurrentFrame(const VideoFrame &frame)
{
m_material->setCurrentFrame(frame);
markDirty(DirtyMaterial);
}
/* Helpers */
static inline void qSetGeom(QSGGeometry::TexturedPoint2D *v, const QPointF &p)
{
v->x = p.x();
v->y = p.y();
}
static inline void qSetTex(QSGGeometry::TexturedPoint2D *v, const QPointF &p)
{
v->tx = p.x();
v->ty = p.y();
}
void SGVideoNode::setTexturedRectGeometry(const QRectF &rect, const QRectF &textureRect, int orientation)
{
if (m_validWidth == m_material->videoMaterial()->validTextureWidth()
&& rect == m_rect && textureRect == m_textureRect && orientation == m_orientation)
return;
QRectF validTexRect = m_material->videoMaterial()->normalizedROI(textureRect);
if (!validTexRect.isEmpty()) {
m_validWidth = m_material->videoMaterial()->validTextureWidth();
m_rect = rect;
m_textureRect = textureRect;
m_orientation = orientation;
}
//qDebug() << ">>>>>>>valid: " << m_validWidth << " roi: " << validTexRect;
QSGGeometry *g = geometry();
if (g == 0)
g = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4);
QSGGeometry::TexturedPoint2D *v = g->vertexDataAsTexturedPoint2D();
// Set geometry first
qSetGeom(v 0, rect.topLeft());
qSetGeom(v 1, rect.bottomLeft());
qSetGeom(v 2, rect.topRight());
qSetGeom(v 3, rect.bottomRight());
// and then texture coordinates
switch (orientation) {
default:
// tl, bl, tr, br
qSetTex(v 0, validTexRect.topLeft());
qSetTex(v 1, validTexRect.bottomLeft());
qSetTex(v 2, validTexRect.topRight());
qSetTex(v 3, validTexRect.bottomRight());
break;
case 90:
// tr, tl, br, bl
qSetTex(v 0, validTexRect.topRight());
qSetTex(v 1, validTexRect.topLeft());
qSetTex(v 2, validTexRect.bottomRight());
qSetTex(v 3, validTexRect.bottomLeft());
break;
case 180:
// br, tr, bl, tl
qSetTex(v 0, validTexRect.bottomRight());
qSetTex(v 1, validTexRect.topRight());
qSetTex(v 2, validTexRect.bottomLeft());
qSetTex(v 3, validTexRect.topLeft());
break;
case 270:
// bl, br, tl, tr
qSetTex(v 0, validTexRect.bottomLeft());
qSetTex(v 1, validTexRect.bottomRight());
qSetTex(v 2, validTexRect.topLeft());
qSetTex(v 3, validTexRect.topRight());
break;
}
if (!geometry())
setGeometry(g);
markDirty(DirtyGeometry);
}
} //namespace QtAV