forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphicsItemRenderer.cpp
345 lines (313 loc) · 8.86 KB
/
GraphicsItemRenderer.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2017 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 "QtAVWidgets/GraphicsItemRenderer.h"
#include "QtAV/private/QPainterRenderer_p.h"
#include "QtAV/FilterContext.h"
#if !defined QT_NO_OPENGL && (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) || defined(QT_OPENGL_LIB))
#define QTAV_HAVE_OPENGL 1
#else
#define QTAV_HAVE_OPENGL 0
#endif
#if QTAV_HAVE(OPENGL)
#include "QtAV/OpenGLVideo.h"
#else
typedef float GLfloat;
#endif
#include <QMatrix4x4>
#include <QGraphicsScene>
#include <QtGui/QPainter>
#include <QEvent>
#include <QKeyEvent>
#include <QGraphicsSceneEvent>
#include <QtCore/QCoreApplication>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QtGui/QSurface>
#endif
namespace QtAV {
class GraphicsItemRendererPrivate : public QPainterRendererPrivate
{
public:
GraphicsItemRendererPrivate()
: frame_changed(false)
, opengl(false)
{}
virtual ~GraphicsItemRendererPrivate(){}
void setupAspectRatio() {
matrix.setToIdentity();
matrix.scale((GLfloat)out_rect.width()/(GLfloat)renderer_width, (GLfloat)out_rect.height()/(GLfloat)renderer_height, 1);
if (rotation())
matrix.rotate(rotation(), 0, 0, 1); // Z axis
}
// return true if opengl is enabled and context is ready. may called by non-rendering thread
bool checkGL() {
#if QTAV_HAVE(OPENGL)
if (!opengl) {
glv.setOpenGLContext(0); // it's for Qt4. may not in rendering thread
return false;
}
if (!glv.openGLContext()) {
//qWarning("no opengl context! set current");
// null if not called from renderering thread;
QOpenGLContext *ctx = const_cast<QOpenGLContext*>(QOpenGLContext::currentContext());
if (!ctx)
return false;
glv.setOpenGLContext(ctx);
}
return true;
#endif
return false;
}
bool frame_changed;
bool opengl;
#if QTAV_HAVE(OPENGL)
OpenGLVideo glv;
#endif
QMatrix4x4 matrix;
};
VideoRendererId GraphicsItemRenderer::id() const
{
return VideoRendererId_GraphicsItem;
}
GraphicsItemRenderer::GraphicsItemRenderer(QGraphicsItem * parent)
:GraphicsWidget(parent),QPainterRenderer(*new GraphicsItemRendererPrivate())
{
setFlag(ItemIsFocusable); //receive key events
//setAcceptHoverEvents(true);
#if CONFIG_GRAPHICSWIDGET
setFocusPolicy(Qt::ClickFocus); //for widget
#endif //CONFIG_GRAPHICSWIDGET
}
GraphicsItemRenderer::GraphicsItemRenderer(GraphicsItemRendererPrivate &d, QGraphicsItem *parent)
:GraphicsWidget(parent),QPainterRenderer(d)
{
setFlag(ItemIsFocusable); //receive key events
//setAcceptHoverEvents(true);
#if CONFIG_GRAPHICSWIDGET
setFocusPolicy(Qt::ClickFocus); //for widget
#endif //CONFIG_GRAPHICSWIDGET
}
bool GraphicsItemRenderer::isSupported(VideoFormat::PixelFormat pixfmt) const
{
if (isOpenGL())
return true;
return QPainterRenderer::isSupported(pixfmt);
}
bool GraphicsItemRenderer::receiveFrame(const VideoFrame& frame)
{
#if QTAV_HAVE(OPENGL)
DPTR_D(GraphicsItemRenderer);
if (isOpenGL()) {
d.video_frame = frame;
d.frame_changed = true;
} else
#endif
{
preparePixmap(frame);
}
// moved to event
// scene()->update(sceneBoundingRect()); //TODO: thread?
QCoreApplication::postEvent(this, new QEvent(QEvent::User));
//update(); //does not cause an immediate paint. my not redraw.
return true;
}
QRectF GraphicsItemRenderer::boundingRect() const
{
return QRectF(0, 0, rendererWidth(), rendererHeight());
}
bool GraphicsItemRenderer::isOpenGL() const
{
#if QTAV_HAVE(OPENGL)
return d_func().opengl;
#endif
return false;
}
void GraphicsItemRenderer::setOpenGL(bool o)
{
DPTR_D(GraphicsItemRenderer);
if (d.opengl == o)
return;
d.opengl = o;
Q_EMIT openGLChanged();
}
OpenGLVideo* GraphicsItemRenderer::opengl() const
{
#if QTAV_HAVE(OPENGL)
return const_cast<OpenGLVideo*>(&d_func().glv);
#endif
return NULL;
}
void GraphicsItemRenderer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
DPTR_D(GraphicsItemRenderer);
d.painter = painter;
QPainterFilterContext *ctx = static_cast<QPainterFilterContext*>(d.filter_context);
if (ctx) {
ctx->painter = d.painter;
} else {
qWarning("FilterContext not available!");
}
// save painter state, switch to native opengl painting
painter->save();
painter->beginNativePainting();
handlePaintEvent();
// end native painting, restore state
painter->endNativePainting();
painter->restore();
d.painter = 0; //painter may be not available outside this function
if (ctx)
ctx->painter = 0;
}
void GraphicsItemRenderer::drawBackground()
{
DPTR_D(GraphicsItemRenderer);
#if QTAV_HAVE(OPENGL)
if (d.checkGL()) {
// d.glv.fill(QColor(0, 0, 0)); //FIXME: fill boundingRect
return;
} else
#endif
{
QPainterRenderer::drawBackground();
}
}
void GraphicsItemRenderer::drawFrame()
{
DPTR_D(GraphicsItemRenderer);
if (!d.painter)
return;
#if QTAV_HAVE(OPENGL)
if (d.checkGL()) {
if (d.frame_changed) {
d.glv.setCurrentFrame(d.video_frame);
d.frame_changed = false;
}
d.glv.render(boundingRect(), realROI(), d.matrix*sceneTransform());
return;
}
#endif
QPainterRenderer::drawFrame();
}
void GraphicsItemRenderer::onSetOutAspectRatio(qreal ratio)
{
Q_UNUSED(ratio);
DPTR_D(GraphicsItemRenderer);
d.setupAspectRatio();
}
bool GraphicsItemRenderer::onSetOrientation(int value)
{
Q_UNUSED(value);
d_func().setupAspectRatio();
update(); //TODO: thread?
return true;
}
void GraphicsItemRenderer::onSetOutAspectRatioMode(OutAspectRatioMode mode)
{
Q_UNUSED(mode);
DPTR_D(GraphicsItemRenderer);
d.setupAspectRatio();
}
bool GraphicsItemRenderer::onSetBrightness(qreal b)
{
if (!isOpenGL())
return false;
Q_UNUSED(b);
#if QTAV_HAVE(OPENGL)
d_func().glv.setBrightness(b);
update();
return true;
#endif
return false;
}
bool GraphicsItemRenderer::onSetContrast(qreal c)
{
if (!isOpenGL())
return false;
Q_UNUSED(c);
#if QTAV_HAVE(OPENGL)
d_func().glv.setContrast(c);
update();
return true;
#endif
return false;
}
bool GraphicsItemRenderer::onSetHue(qreal h)
{
if (!isOpenGL())
return false;
Q_UNUSED(h);
#if QTAV_HAVE(OPENGL)
d_func().glv.setHue(h);
update();
return true;
#endif
return false;
}
bool GraphicsItemRenderer::onSetSaturation(qreal s)
{
if (!isOpenGL())
return false;
Q_UNUSED(s);
#if QTAV_HAVE(OPENGL)
d_func().glv.setSaturation(s);
update();
return true;
#endif
return false;
}
//GraphicsWidget will lose focus forever if focus out. Why?
#if CONFIG_GRAPHICSWIDGET
bool GraphicsItemRenderer::event(QEvent *event)
{
if (e->type() == QEvent::User) {
scene()->update(sceneBoundingRect());
}
else {
setFocus(); //WHY: Force focus
QEvent::Type type = event->type();
qDebug("GraphicsItemRenderer event type = %d", type);
if (type == QEvent::KeyPress) {
qDebug("KeyPress Event. key=%d", static_cast<QKeyEvent*>(event)->key());
}
}
return true;
}
#else
bool GraphicsItemRenderer::event(QEvent *event)
{
if (event->type() != QEvent::User)
return GraphicsWidget::event(event);
scene()->update(sceneBoundingRect());
return true;
}
/*simply passes event to QGraphicsWidget::event(). you should not have to
*reimplement sceneEvent() in a subclass of QGraphicsWidget.
*/
/*
bool GraphicsItemRenderer::sceneEvent(QEvent *event)
{
QEvent::Type type = event->type();
qDebug("sceneEvent type = %d", type);
if (type == QEvent::KeyPress) {
qDebug("KeyPress Event. key=%d", static_cast<QKeyEvent*>(event)->key());
}
return true;
}
*/
#endif //!CONFIG_GRAPHICSWIDGET
} //namespace QtAV