forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickFBORenderer.cpp
273 lines (242 loc) · 7.38 KB
/
QuickFBORenderer.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2015-2016 Wang Bin <[email protected]>
* This file is part of QtAV
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/QuickFBORenderer.h"
#include "QmlAV/QmlAVPlayer.h"
#include "QtAV/AVPlayer.h"
#include "QtAV/OpenGLVideo.h"
#include "QtAV/private/VideoRenderer_p.h"
#include "QtAV/private/mkid.h"
#include "QtAV/private/factory.h"
#include <QtCore/QCoreApplication>
#include <QtGui/QOpenGLFramebufferObject>
#include <QtQuick/QQuickWindow>
// for dynamicgl. qglfunctions before qt5.3 does not have portable gl functions
#ifdef QT_OPENGL_DYNAMIC
#include <QtGui/QOpenGLFunctions>
#define DYGL(glFunc) QOpenGLContext::currentContext()->functions()->glFunc
#else
#define DYGL(glFunc) glFunc
#endif
namespace QtAV {
static const VideoRendererId VideoRendererId_QuickFBO = mkid::id32base36_4<'Q','F','B','O'>::value;
FACTORY_REGISTER(VideoRenderer, QuickFBO, "QuickFBO")
class FBORenderer : public QQuickFramebufferObject::Renderer
{
public:
FBORenderer(QuickFBORenderer* item) : m_item(item) {}
QOpenGLFramebufferObject* createFramebufferObject(const QSize &size) {
m_item->fboSizeChanged(size);
return QQuickFramebufferObject::Renderer::createFramebufferObject(size);
}
void render() {
Q_ASSERT(m_item);
m_item->renderToFbo();
}
void synchronize(QQuickFramebufferObject *item) {
m_item = static_cast<QuickFBORenderer*>(item);
}
private:
QuickFBORenderer *m_item;
};
class QuickFBORendererPrivate : public VideoRendererPrivate
{
public:
QuickFBORendererPrivate():
VideoRendererPrivate()
, frame_changed(false)
, opengl(true)
, fill_mode(QuickFBORenderer::PreserveAspectFit)
, node(0)
, source(0)
, glctx(0)
{}
void setupAspectRatio() { //TODO: call when out_rect, renderer_size, orientation changed
matrix.setToIdentity();
matrix.scale((GLfloat)out_rect.width()/(GLfloat)renderer_width, (GLfloat)out_rect.height()/(GLfloat)renderer_height, 1);
if (orientation)
matrix.rotate(orientation, 0, 0, 1); // Z axis
// FIXME: why x/y is mirrored?
if (orientation%180)
matrix.scale(-1, 1);
else
matrix.scale(1, -1);
}
bool frame_changed;
bool opengl;
QuickFBORenderer::FillMode fill_mode;
QSGNode *node;
QObject *source;
QOpenGLContext *glctx;
QMatrix4x4 matrix;
OpenGLVideo glv;
};
QuickFBORenderer::QuickFBORenderer(QQuickItem *parent)
: QQuickFramebufferObject(parent)
, VideoRenderer(*new QuickFBORendererPrivate())
{
setPreferredPixelFormat(VideoFormat::Format_YUV420P);
}
VideoRendererId QuickFBORenderer::id() const
{
return VideoRendererId_QuickFBO;
}
QQuickFramebufferObject::Renderer* QuickFBORenderer::createRenderer() const
{
return new FBORenderer((QuickFBORenderer*)this);
}
bool QuickFBORenderer::isSupported(VideoFormat::PixelFormat pixfmt) const
{
if (pixfmt == VideoFormat::Format_RGB48BE)
return false;
if (!isOpenGL())
return VideoFormat::isRGB(pixfmt);
return OpenGLVideo::isSupported(pixfmt);
}
bool QuickFBORenderer::receiveFrame(const VideoFrame &frame)
{
DPTR_D(QuickFBORenderer);
d.video_frame = frame;
d.frame_changed = true;
// update(); // why update slow? because of calling in a different thread?
//QMetaObject::invokeMethod(this, "update"); // slower than directly postEvent
QCoreApplication::postEvent(this, new QEvent(QEvent::User));
return true;
}
QObject* QuickFBORenderer::source() const
{
return d_func().source;
}
void QuickFBORenderer::setSource(QObject *source)
{
DPTR_D(QuickFBORenderer);
if (d.source == source)
return;
d.source = source;
Q_EMIT sourceChanged();
if (!source)
return;
((QmlAVPlayer*)source)->player()->addVideoRenderer(this);
}
QuickFBORenderer::FillMode QuickFBORenderer::fillMode() const
{
return d_func().fill_mode;
}
void QuickFBORenderer::setFillMode(FillMode mode)
{
DPTR_D(QuickFBORenderer);
if (d.fill_mode == mode)
return;
d_func().fill_mode = mode;
updateRenderRect();
Q_EMIT fillModeChanged(mode);
}
QRectF QuickFBORenderer::contentRect() const
{
return videoRect();
}
QRectF QuickFBORenderer::sourceRect() const
{
return QRectF(QPointF(), videoFrameSize());
}
bool QuickFBORenderer::isOpenGL() const
{
return d_func().opengl;
}
void QuickFBORenderer::setOpenGL(bool o)
{
DPTR_D(QuickFBORenderer);
if (d.opengl == o)
return;
d.opengl = o;
Q_EMIT openGLChanged();
if (o)
setPreferredPixelFormat(VideoFormat::Format_YUV420P);
else
setPreferredPixelFormat(VideoFormat::Format_RGB32);
}
void QuickFBORenderer::fboSizeChanged(const QSize &size)
{
DPTR_D(QuickFBORenderer);
d.update_background = true;
resizeRenderer(size);
d.glv.setProjectionMatrixToRect(QRectF(0, 0, size.width(), size.height()));
d.setupAspectRatio();
}
void QuickFBORenderer::renderToFbo()
{
handlePaintEvent();
}
void QuickFBORenderer::drawBackground()
{
if (backgroundRegion().isEmpty())
return;
d_func().glv.fill(backgroundColor());
}
void QuickFBORenderer::drawFrame()
{
DPTR_D(QuickFBORenderer);
if (d.glctx != QOpenGLContext::currentContext()) {
d.glctx = QOpenGLContext::currentContext();
d.glv.setOpenGLContext(d.glctx);
}
if (!d.video_frame.isValid()) {
d.glv.fill(QColor(0, 0, 0, 0));
return;
}
if (d.frame_changed) {
d.glv.setCurrentFrame(d.video_frame);
d.frame_changed = false;
}
d.glv.render(d.out_rect, normalizedROI(), d.matrix);
}
bool QuickFBORenderer::event(QEvent *e)
{
if (e->type() != QEvent::User)
return QQuickFramebufferObject::event(e);
update();
return true;
}
bool QuickFBORenderer::onSetOrientation(int value)
{
Q_UNUSED(value);
d_func().setupAspectRatio();
return true;
}
void QuickFBORenderer::onSetOutAspectRatio(qreal ratio)
{
Q_UNUSED(ratio);
DPTR_D(QuickFBORenderer);
d.setupAspectRatio();
}
void QuickFBORenderer::onSetOutAspectRatioMode(OutAspectRatioMode mode)
{
Q_UNUSED(mode);
DPTR_D(QuickFBORenderer);
d.setupAspectRatio();
}
void QuickFBORenderer::updateRenderRect()
{
DPTR_D(QuickFBORenderer);
if (d.fill_mode == Stretch) {
setOutAspectRatioMode(RendererAspectRatio);
} else {//compute out_rect fits video aspect ratio then compute again if crop
setOutAspectRatioMode(VideoAspectRatio);
}
//update();
d.setupAspectRatio();
}
} //namespace QtAV