Skip to content

Commit

Permalink
move useQPainter() to base class VideoRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Feb 28, 2013
1 parent 11e04f5 commit 731d5e3
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 125 deletions.
80 changes: 34 additions & 46 deletions src/Direct2DRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ class Direct2DRendererPrivate : public VideoRendererPrivate
DPTR_DECLARE_PUBLIC(Direct2DRenderer)

Direct2DRendererPrivate():
use_qpainter(false)
, d2d_factory(0)
d2d_factory(0)
, render_target(0)
, bitmap(0)
, bitmap_width(0)
, bitmap_height(0)
{
use_qpainter = false;
HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &d2d_factory);
if (FAILED(hr)) {
qWarning("Create d2d factory failed");
Expand Down Expand Up @@ -135,6 +135,7 @@ Direct2DRenderer::Direct2DRenderer(QWidget *parent, Qt::WindowFlags f):
QWidget(parent, f),VideoRenderer(*new Direct2DRendererPrivate())
{
DPTR_INIT_PRIVATE(Direct2DRenderer);
d_func().widget_holder = this;
setAcceptDrops(true);
setFocusPolicy(Qt::StrongFocus);
//setAttribute(Qt::WA_OpaquePaintEvent);
Expand Down Expand Up @@ -171,12 +172,6 @@ void Direct2DRenderer::convertData(const QByteArray &data)
}
}

bool Direct2DRenderer::write()
{
update();
return true;
}

QPaintEngine* Direct2DRenderer::paintEngine() const
{
if (d_func().use_qpainter) {
Expand All @@ -186,44 +181,6 @@ QPaintEngine* Direct2DRenderer::paintEngine() const
}
}

void Direct2DRenderer::useQPainter(bool qp)
{
DPTR_D(Direct2DRenderer);
d.use_qpainter = qp;
setAttribute(Qt::WA_PaintOnScreen, !d.use_qpainter);
}

bool Direct2DRenderer::useQPainter() const
{
DPTR_D(const Direct2DRenderer);
return d.use_qpainter;
}

void Direct2DRenderer::resizeEvent(QResizeEvent *e)
{
resizeRenderer(e->size());

DPTR_D(Direct2DRenderer);
if (d.render_target) {
D2D1_SIZE_U size = {
e->size().width(),
e->size().height()
};
// Note: This method can fail, but it's okay to ignore the
// error here -- it will be repeated on the next call to
// EndDraw.
d.render_target->Resize(size);
}
update();
}

void Direct2DRenderer::showEvent(QShowEvent *)
{
DPTR_D(Direct2DRenderer);
useQPainter(d.use_qpainter);
d.createDeviceResource();
}

void Direct2DRenderer::paintEvent(QPaintEvent *)
{
DPTR_D(Direct2DRenderer);
Expand Down Expand Up @@ -267,4 +224,35 @@ void Direct2DRenderer::paintEvent(QPaintEvent *)
}
}

void Direct2DRenderer::resizeEvent(QResizeEvent *e)
{
resizeRenderer(e->size());

DPTR_D(Direct2DRenderer);
if (d.render_target) {
D2D1_SIZE_U size = {
e->size().width(),
e->size().height()
};
// Note: This method can fail, but it's okay to ignore the
// error here -- it will be repeated on the next call to
// EndDraw.
d.render_target->Resize(size);
}
update();
}

void Direct2DRenderer::showEvent(QShowEvent *)
{
DPTR_D(Direct2DRenderer);
useQPainter(d.use_qpainter);
d.createDeviceResource();
}

bool Direct2DRenderer::write()
{
update();
return true;
}

} //namespace QtAV
61 changes: 24 additions & 37 deletions src/GDIRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class GDIRendererPrivate : public VideoRendererPrivate
DPTR_DECLARE_PUBLIC(GDIRenderer)

GDIRendererPrivate():
use_qpainter(false)
, support_bitblt(true)
support_bitblt(true)
, gdiplus_token(0)
, device_context(0)
{
use_qpainter = false;
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&gdiplus_token, &gdiplusStartupInput, NULL);
}
Expand Down Expand Up @@ -84,7 +84,6 @@ class GDIRendererPrivate : public VideoRendererPrivate
off_dc = CreateCompatibleDC(device_context);
}

bool use_qpainter;
bool support_bitblt;
ULONG_PTR gdiplus_token;
/*
Expand All @@ -102,6 +101,7 @@ GDIRenderer::GDIRenderer(QWidget *parent, Qt::WindowFlags f):
QWidget(parent, f),VideoRenderer(*new GDIRendererPrivate())
{
DPTR_INIT_PRIVATE(GDIRenderer);
d_func().widget_holder = this;
setAcceptDrops(true);
setFocusPolicy(Qt::StrongFocus);
//setAttribute(Qt::WA_OpaquePaintEvent);
Expand All @@ -114,12 +114,6 @@ GDIRenderer::~GDIRenderer()
{
}

bool GDIRenderer::write()
{
update();
return true;
}

QPaintEngine* GDIRenderer::paintEngine() const
{
if (d_func().use_qpainter) {
Expand All @@ -129,19 +123,6 @@ QPaintEngine* GDIRenderer::paintEngine() const
}
}

void GDIRenderer::useQPainter(bool qp)
{
DPTR_D(GDIRenderer);
d.use_qpainter = qp;
setAttribute(Qt::WA_PaintOnScreen, !d.use_qpainter);
}

bool GDIRenderer::useQPainter() const
{
DPTR_D(const GDIRenderer);
return d.use_qpainter;
}

void GDIRenderer::convertData(const QByteArray &data)
{
DPTR_D(GDIRenderer);
Expand All @@ -150,21 +131,6 @@ void GDIRenderer::convertData(const QByteArray &data)
d.data = data;
}

void GDIRenderer::showEvent(QShowEvent *)
{
DPTR_D(const GDIRenderer);
useQPainter(d.use_qpainter);
if (!d.use_qpainter) {
d_func().prepare();
}
}

void GDIRenderer::resizeEvent(QResizeEvent *e)
{
resizeRenderer(e->size());
update();
}

void GDIRenderer::paintEvent(QPaintEvent *)
{
DPTR_D(GDIRenderer);
Expand Down Expand Up @@ -205,4 +171,25 @@ void GDIRenderer::paintEvent(QPaintEvent *)
//end paint
}

void GDIRenderer::resizeEvent(QResizeEvent *e)
{
resizeRenderer(e->size());
update();
}

void GDIRenderer::showEvent(QShowEvent *)
{
DPTR_D(const GDIRenderer);
useQPainter(d.use_qpainter);
if (!d.use_qpainter) {
d_func().prepare();
}
}

bool GDIRenderer::write()
{
update();
return true;
}

} //namespace QtAV
6 changes: 2 additions & 4 deletions src/QtAV/Direct2DRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ class Q_EXPORT Direct2DRenderer : public QWidget, public VideoRenderer
* return 0 and set this flag
*/
virtual QPaintEngine* paintEngine() const;
bool useQPainter() const;
void useQPainter(bool qp);
protected:
virtual void convertData(const QByteArray &data);
//stay on top will change parent, hide then show(windows). we need GetDC() again
virtual void paintEvent(QPaintEvent *);
virtual void resizeEvent(QResizeEvent *);
//stay on top will change parent, hide then show(windows). we need GetDC() again
virtual void showEvent(QShowEvent *);
virtual void paintEvent(QPaintEvent *);
virtual bool write();
};

Expand Down
7 changes: 2 additions & 5 deletions src/QtAV/GDIRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ class Q_EXPORT GDIRenderer : public QWidget, public VideoRenderer
* true: paintEngine.getDC(), double buffer is enabled by defalut.
* false: GetDC(winId()), no double buffer, should reimplement paintEngine()
*/
//TODO: move to base class
bool useQPainter() const;
void useQPainter(bool qp);
protected:
virtual void convertData(const QByteArray &data);
virtual void paintEvent(QPaintEvent *);
virtual void resizeEvent(QResizeEvent *);
//stay on top will change parent, hide then show(windows). we need GetDC() again
virtual void showEvent(QShowEvent *);
virtual void resizeEvent(QResizeEvent *);
virtual void paintEvent(QPaintEvent *);
virtual bool write();
};

Expand Down
2 changes: 2 additions & 0 deletions src/QtAV/VideoRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class Q_EXPORT VideoRenderer : public AVOutput
//The video frame rect in renderer you shoud paint to. e.g. in RendererAspectRatio mode, the rect equals to renderer's
QRect videoRect() const;

bool useQPainter() const;
void useQPainter(bool qp);
protected:
VideoRenderer(VideoRendererPrivate &d);
/*!
Expand Down
41 changes: 26 additions & 15 deletions src/QtAV/private/VideoRenderer_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,29 @@
* Region of Interest(ROI)
*/
class QObject;
class QWidget;
namespace QtAV {
class Q_EXPORT VideoRendererPrivate : public AVOutputPrivate
{
public:
VideoRendererPrivate():
scale_in_qt(true)
use_qpainter(true)
, scale_in_qt(true)
, renderer_width(480)
, renderer_height(320)
, source_aspect_ratio(0)
, src_width(0)
, src_height(0)
, aspect_ratio_mode(VideoRenderer::VideoAspectRatio)
, out_aspect_ratio(0)
, widget_holder(0)
{
//conv.setInFormat(PIX_FMT_YUV420P);
//conv.setOutFormat(PIX_FMT_BGR32); //TODO: why not RGB32?
}
virtual ~VideoRendererPrivate(){}
bool scale_in_qt;
// width, height: the renderer's size. i.e. size of video frame with the value with borders
//TODO: rename to renderer_width/height
int renderer_width, renderer_height;
qreal source_aspect_ratio;
int src_width, src_height;
//ImageConverter conv;
QMutex img_mutex;
VideoRenderer::OutAspectRatioMode aspect_ratio_mode;
qreal out_aspect_ratio;
//out_rect: the displayed video frame out_rect in the renderer
QRect out_rect; //TODO: out_out_rect

virtual ~VideoRendererPrivate(){
widget_holder = 0;
}
void computeOutParameters(qreal rendererAspectRatio, qreal outAspectRatio) {
if (rendererAspectRatio > outAspectRatio) { //equals to original video aspect ratio here, also equals to out ratio
//renderer is too wide, use renderer's height, horizonal align center
Expand All @@ -78,6 +70,25 @@ class Q_EXPORT VideoRendererPrivate : public AVOutputPrivate
}
out_aspect_ratio = outAspectRatio;
}

bool use_qpainter;
bool scale_in_qt;
// width, height: the renderer's size. i.e. size of video frame with the value with borders
//TODO: rename to renderer_width/height
int renderer_width, renderer_height;
qreal source_aspect_ratio;
int src_width, src_height;
//ImageConverter conv;
QMutex img_mutex;
VideoRenderer::OutAspectRatioMode aspect_ratio_mode;
qreal out_aspect_ratio;
//out_rect: the displayed video frame out_rect in the renderer
QRect out_rect; //TODO: out_out_rect

/* Stores but not own the ptr if renderer is a subclass of QWidget.
* Some operations are based on QWidget
*/
QWidget *widget_holder;
};

} //namespace QtAV
Expand Down
16 changes: 16 additions & 0 deletions src/VideoRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QtAV/VideoDecoder.h>
#include <private/VideoRenderer_p.h>
#include <QtCore/QCoreApplication>
#include <QWidget>

namespace QtAV {

Expand Down Expand Up @@ -183,6 +184,21 @@ QRect VideoRenderer::videoRect() const
return d_func().out_rect;
}

void VideoRenderer::useQPainter(bool qp)
{
DPTR_D(VideoRenderer);
d.use_qpainter = qp;
if (d.widget_holder) {
d.widget_holder->setAttribute(Qt::WA_PaintOnScreen, !d.use_qpainter);
}
}

bool VideoRenderer::useQPainter() const
{
DPTR_D(const VideoRenderer);
return d.use_qpainter;
}

void VideoRenderer::resizeFrame(int width, int height)
{
Q_UNUSED(width);
Expand Down
Loading

0 comments on commit 731d5e3

Please sign in to comment.