Skip to content

Commit

Permalink
vo: use UpdateLater event for widgets instead of a signal
Browse files Browse the repository at this point in the history
fix wang-bin#430 wang-bin#532
Less code and should be faster. Calling update() in User event should be
a solution too but more code is required.
WidgetRenderer::imageReady() is not removed because some people is using
it
  • Loading branch information
wang-bin committed Oct 25, 2015
1 parent 5294c21 commit 9bb56c0
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/QtAV/VideoRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class Q_AV_EXPORT VideoRenderer : public AVOutput
// TODO: parameter VideoFrame
virtual void drawFrame() = 0; //You MUST reimplement this to display a frame. Other draw functions are not essential
virtual void handlePaintEvent(); //has default. User don't have to implement it
virtual void updateUi(); // by default schedual an UpdateRequest event on ui thread
virtual void updateUi(); // by default post an UpdateRequest event for window and UpdateLater event for widget to ensure ui update

private: //used by VideoOutput class
// property change
Expand Down
6 changes: 0 additions & 6 deletions src/output/video/OpenGLWidgetRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ OpenGLWidgetRenderer::OpenGLWidgetRenderer(QWidget *parent, Qt::WindowFlags f):
{
setAcceptDrops(true);
setFocusPolicy(Qt::StrongFocus);
connect(this, SIGNAL(frameReady()), SLOT(update()));
}

void OpenGLWidgetRenderer::updateUi()
{
Q_EMIT frameReady();
}

void OpenGLWidgetRenderer::initializeGL()
Expand Down
25 changes: 20 additions & 5 deletions src/output/video/VideoRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "QtAV/private/VideoRenderer_p.h"
#include "QtAV/Filter.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QEvent>
#include "QtAV/private/factory.h"
#include "QtAV/private/mkid.h"
#include "utils/Logger.h"
Expand Down Expand Up @@ -610,12 +611,26 @@ void VideoRenderer::onFrameSizeChanged(const QSize &size)

void VideoRenderer::updateUi()
{
QObject *obj = (QObject*)qwindow();
if (!obj)
obj = (QObject*)widget();
QObject *obj = (QObject*)widget();
if (obj) {
QCoreApplication::instance()->postEvent(obj, new QEvent(QEvent::UpdateRequest));
// UpdateRequest only sync backing store but do not shedule repainting. UpdateLater does
// Copy from qwidget_p.h. QWidget::event() will convert UpdateLater to QUpdateLaterEvent and get it's region()
class QUpdateLaterEvent : public QEvent
{
public:
explicit QUpdateLaterEvent(const QRegion& paintRegion)
: QEvent(UpdateLater), m_region(paintRegion)
{}
~QUpdateLaterEvent() {}
inline const QRegion &region() const { return m_region; }
protected:
QRegion m_region;
};
QCoreApplication::instance()->postEvent(obj, new QUpdateLaterEvent(QRegion(0, 0, rendererWidth(), rendererHeight())));
} else {
obj = (QObject*)qwindow();
if (obj)
QCoreApplication::instance()->postEvent(obj, new QEvent(QEvent::UpdateRequest));
}
}

} //namespace QtAV
6 changes: 2 additions & 4 deletions src/output/video/WidgetRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ WidgetRenderer::WidgetRenderer(QWidget *parent, Qt::WindowFlags f) :
} else {
qWarning("FilterContext not available!");
}
connect(this, SIGNAL(imageReady()), SLOT(update()));
}

WidgetRenderer::WidgetRenderer(WidgetRendererPrivate &d, QWidget *parent, Qt::WindowFlags f)
Expand All @@ -76,18 +75,17 @@ WidgetRenderer::WidgetRenderer(WidgetRendererPrivate &d, QWidget *parent, Qt::Wi
} else {
qWarning("FilterContext not available!");
}
connect(this, SIGNAL(frameReady()), SLOT(update()));
}

bool WidgetRenderer::receiveFrame(const VideoFrame &frame)
{
prepareFrame(frame);
//update();
updateUi();
/*
* workaround for the widget not updated if has parent. don't know why it works and why update() can't
* Thanks to Vito Covito and Carlo Scarpato
* Now it's fixed by posting a QUpdateLaterEvent
*/
Q_EMIT frameReady();
Q_EMIT imageReady();
return true;
}
Expand Down
4 changes: 0 additions & 4 deletions widgets/QtAVWidgets/OpenGLWidgetRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,12 @@ namespace QtAV {
class OpenGLWidgetRendererPrivate;
class Q_AVWIDGETS_EXPORT OpenGLWidgetRenderer : public QOpenGLWidget, public OpenGLRendererBase
{
Q_OBJECT
DPTR_DECLARE_PRIVATE(OpenGLWidgetRenderer)
public:
explicit OpenGLWidgetRenderer(QWidget* parent = 0, Qt::WindowFlags f = 0);
virtual VideoRendererId id() const Q_DECL_OVERRIDE;
virtual QWidget* widget() Q_DECL_OVERRIDE { return this; }
Q_SIGNALS:
void frameReady();
protected:
virtual void updateUi() Q_DECL_OVERRIDE;
virtual void initializeGL() Q_DECL_OVERRIDE;
virtual void paintGL() Q_DECL_OVERRIDE;
virtual void resizeGL(int w, int h) Q_DECL_OVERRIDE;
Expand Down
3 changes: 1 addition & 2 deletions widgets/QtAVWidgets/WidgetRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class Q_AVWIDGETS_EXPORT WidgetRenderer : public QWidget, public QPainterRendere
virtual VideoRendererId id() const;
virtual QWidget* widget() { return this; }
Q_SIGNALS:
void frameReady();
QTAVWIDGETS_DEPRECATED void imageReady(); // use frameReady() instead
QTAVWIDGETS_DEPRECATED void imageReady(); // add frameReady() in the future?
protected:
virtual bool receiveFrame(const VideoFrame& frame);
virtual bool needUpdateBackground() const;
Expand Down

0 comments on commit 9bb56c0

Please sign in to comment.