mirror of
https://github.com/wang-bin/QtAV.git
synced 2026-10-28 02:56:45 +08:00
use filter context drawing api
TODO: implement QPaintEngine for different backend then remove context?
This commit is contained in:
@@ -76,17 +76,6 @@ bool Filter::isEnabled() const
|
||||
return d.enabled;
|
||||
}
|
||||
|
||||
void Filter::setOpacity(qreal o)
|
||||
{
|
||||
DPTR_D(Filter);
|
||||
d.opacity = o;
|
||||
}
|
||||
|
||||
qreal Filter::opacity() const
|
||||
{
|
||||
return d_func().opacity;
|
||||
}
|
||||
|
||||
FilterContext::Type Filter::contextType() const
|
||||
{
|
||||
return FilterContext::None;
|
||||
|
||||
@@ -61,7 +61,11 @@ VideoFilterContext::VideoFilterContext()
|
||||
, painter(0)
|
||||
, paint_device(0)
|
||||
, own_paint_device(false)
|
||||
, opacity(1)
|
||||
{
|
||||
font.setBold(true);
|
||||
font.setPixelSize(26);
|
||||
pen.setColor(Qt::white);
|
||||
}
|
||||
|
||||
VideoFilterContext::~VideoFilterContext()
|
||||
@@ -81,11 +85,78 @@ VideoFilterContext::~VideoFilterContext()
|
||||
}
|
||||
}
|
||||
|
||||
void VideoFilterContext::drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags)
|
||||
{
|
||||
Q_UNUSED(target);
|
||||
Q_UNUSED(image);
|
||||
Q_UNUSED(source);
|
||||
Q_UNUSED(flags);
|
||||
}
|
||||
|
||||
void VideoFilterContext::drawPlainText(const QRectF &rect, const QString &text)
|
||||
{
|
||||
Q_UNUSED(rect);
|
||||
Q_UNUSED(text);
|
||||
}
|
||||
|
||||
void VideoFilterContext::drawRichText(const QRectF &rect, const QString &text)
|
||||
{
|
||||
Q_UNUSED(rect);
|
||||
Q_UNUSED(text);
|
||||
}
|
||||
|
||||
|
||||
|
||||
FilterContext::Type QPainterFilterContext::type() const
|
||||
{
|
||||
return FilterContext::QtPainter;
|
||||
}
|
||||
|
||||
void QPainterFilterContext::drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags)
|
||||
{
|
||||
if (!prepare())
|
||||
return;
|
||||
painter->drawImage(target, image, source, flags);
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
void QPainterFilterContext::drawPlainText(const QRectF &rect, int flags, const QString &text)
|
||||
{
|
||||
if (!prepare())
|
||||
return;
|
||||
if (rect.isNull())
|
||||
painter->drawText(rect.topLeft(), text);
|
||||
else
|
||||
painter->drawText(rect, flags, text);
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
void QPainterFilterContext::drawRichText(const QRectF &rect, int flags, const QString &text)
|
||||
{
|
||||
if (!prepare())
|
||||
return;
|
||||
//QTextDocument
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
bool QPainterFilterContext::isReady() const
|
||||
{
|
||||
return !!painter && painter->isActive();
|
||||
}
|
||||
|
||||
bool QPainterFilterContext::prepare()
|
||||
{
|
||||
if (!isReady())
|
||||
return false;
|
||||
painter->save(); //is it necessary?
|
||||
painter->setBrush(brush);
|
||||
painter->setPen(pen);
|
||||
painter->setFont(font);
|
||||
painter->setOpacity(opacity);
|
||||
painter->setTransform(transform);
|
||||
return true;
|
||||
}
|
||||
|
||||
void QPainterFilterContext::initializeOnData(QByteArray *data)
|
||||
{
|
||||
if (!data) {
|
||||
@@ -120,4 +191,16 @@ FilterContext::Type GLFilterContext::type() const
|
||||
return FilterContext::OpenGL;
|
||||
}
|
||||
|
||||
bool GLFilterContext::isReady() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GLFilterContext::prepare()
|
||||
{
|
||||
if (!isReady())
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
} //namespace QtAV
|
||||
|
||||
-12
@@ -28,8 +28,6 @@ OSD::OSD():
|
||||
mShowType(ShowCurrentAndTotalTime)
|
||||
, mSecsTotal(-1)
|
||||
{
|
||||
mFont.setBold(true);
|
||||
mFont.setPixelSize(26);
|
||||
}
|
||||
|
||||
OSD::~OSD()
|
||||
@@ -64,16 +62,6 @@ bool OSD::hasShowType(ShowType t) const
|
||||
return (t&mShowType) == t;
|
||||
}
|
||||
|
||||
void OSD::setFont(const QFont &font)
|
||||
{
|
||||
mFont = font;
|
||||
}
|
||||
|
||||
QFont OSD::font() const
|
||||
{
|
||||
return mFont;
|
||||
}
|
||||
|
||||
QString OSD::text(Statistics *statistics)
|
||||
{
|
||||
QString text;
|
||||
|
||||
+8
-33
@@ -38,19 +38,11 @@ OSDFilter::OSDFilter(OSDFilterPrivate &d):
|
||||
{
|
||||
}
|
||||
|
||||
template<>
|
||||
OSDFilterImpl<QPainterFilterContext>::OSDFilterImpl():
|
||||
OSDFilterQPainter::OSDFilterQPainter():
|
||||
OSDFilter(*new OSDFilterPrivate())
|
||||
{
|
||||
}
|
||||
|
||||
template<>
|
||||
OSDFilterImpl<GLFilterContext>::OSDFilterImpl():
|
||||
OSDFilter(*new OSDFilterPrivate())
|
||||
{
|
||||
}
|
||||
|
||||
template<>
|
||||
void OSDFilterQPainter::process()
|
||||
{
|
||||
if (mShowType == ShowNone)
|
||||
@@ -58,31 +50,14 @@ void OSDFilterQPainter::process()
|
||||
DPTR_D(Filter);
|
||||
QPainterFilterContext* ctx = static_cast<QPainterFilterContext*>(d.context);
|
||||
//qDebug("ctx=%p tid=%p main tid=%p", ctx, QThread::currentThread(), qApp->thread());
|
||||
if (!ctx->painter) {
|
||||
qWarning("null QPainter in OSDFilterQPainter!");
|
||||
return;
|
||||
}
|
||||
if (!ctx->painter->isActive()) {
|
||||
qWarning("QPainter in OSDFilterQPainter is not active");
|
||||
return;
|
||||
}
|
||||
QPainter *p = ctx->painter;
|
||||
p->save(); //TODO: move outside?
|
||||
p->setFont(mFont);
|
||||
p->setPen(Qt::white);
|
||||
p->setOpacity(d.opacity);
|
||||
p->drawText(ctx->rect.topLeft(), text(d.statistics));
|
||||
p->restore(); //TODO: move outside?
|
||||
ctx->drawPlainText(ctx->rect, Qt::AlignCenter, text(d.statistics));
|
||||
}
|
||||
|
||||
OSDFilterGL::OSDFilterGL():
|
||||
OSDFilter(*new OSDFilterPrivate())
|
||||
{
|
||||
}
|
||||
/*
|
||||
template<>
|
||||
FilterContext::Type OSDFilterQPainter::contextType() const
|
||||
{
|
||||
return FilterContext::QtPainter;
|
||||
}
|
||||
*/
|
||||
|
||||
template<>
|
||||
void OSDFilterGL::process()
|
||||
{
|
||||
if (mShowType == ShowNone)
|
||||
@@ -94,7 +69,7 @@ void OSDFilterGL::process()
|
||||
QGLWidget *glw = static_cast<QGLWidget*>(ctx->paint_device);
|
||||
if (!glw)
|
||||
return;
|
||||
glw->renderText(ctx->rect.x(), ctx->rect.y(), text(d.statistics), font());
|
||||
glw->renderText(ctx->rect.x(), ctx->rect.y(), text(d.statistics), ctx->font);
|
||||
#endif //QTAV_HAVE(GL)
|
||||
}
|
||||
} //namespace QtAV
|
||||
|
||||
+1
-4
@@ -46,10 +46,7 @@ public:
|
||||
void setEnabled(bool enabled); //AVComponent.enabled
|
||||
bool isEnabled() const;
|
||||
|
||||
qreal opacity() const;
|
||||
void setOpacity(qreal o);
|
||||
|
||||
virtual FilterContext::Type contextType() const;
|
||||
virtual FilterContext::Type contextType() const = 0;
|
||||
|
||||
/*!
|
||||
* check context and apply the filter
|
||||
|
||||
+27
-12
@@ -25,6 +25,7 @@
|
||||
#include <QtAV/QtAV_Global.h>
|
||||
#include <QtCore/QByteArray>
|
||||
#include <QtCore/QRect>
|
||||
#include <QtGui/QPainter>
|
||||
/*
|
||||
* QPainterFilterContext, D2DFilterContext, ...
|
||||
*/
|
||||
@@ -59,21 +60,37 @@ protected:
|
||||
friend class Filter;
|
||||
};
|
||||
|
||||
|
||||
//TODO: DrawTarget? New backend for QPaintEngine?
|
||||
class Q_EXPORT VideoFilterContext : public FilterContext
|
||||
{
|
||||
public:
|
||||
VideoFilterContext();
|
||||
virtual ~VideoFilterContext();
|
||||
QRect rect;
|
||||
|
||||
// map to Qt types
|
||||
//drawSurface?
|
||||
virtual void drawImage(const QRectF& target, const QImage& image, const QRectF& source, Qt::ImageConversionFlags flags = Qt::AutoColor);
|
||||
virtual void drawPlainText(const QRectF& rect, const QString& text);
|
||||
virtual void drawRichText(const QRectF& rect, const QString& text);
|
||||
|
||||
QRect rect;
|
||||
// Fallback to QPainter if no other paint engine implemented
|
||||
QPainter *painter;
|
||||
qreal opacity;
|
||||
QTransform transform;
|
||||
QFont font;
|
||||
QPen pen;
|
||||
QBrush brush;
|
||||
/*
|
||||
* for the filters apply on decoded data, paint_device must be initialized once the data changes
|
||||
* can we allocate memory on stack?
|
||||
*/
|
||||
QPaintDevice *paint_device;
|
||||
bool own_paint_device;
|
||||
protected:
|
||||
virtual bool isReady() const = 0;
|
||||
virtual bool prepare() = 0;
|
||||
};
|
||||
|
||||
//TODO: font, pen, brush etc?
|
||||
@@ -81,7 +98,13 @@ class Q_EXPORT QPainterFilterContext : public VideoFilterContext
|
||||
{
|
||||
public:
|
||||
virtual Type type() const; //QtPainter
|
||||
virtual void drawImage(const QRectF& target, const QImage& image, const QRectF& source, Qt::ImageConversionFlags flags = Qt::AutoColor);
|
||||
virtual void drawPlainText(const QRectF& rect, int flags, const QString& text);
|
||||
virtual void drawRichText(const QRectF& rect, int flags, const QString& text);
|
||||
|
||||
protected:
|
||||
virtual bool isReady() const;
|
||||
virtual bool prepare();
|
||||
virtual void initializeOnData(QByteArray* data);
|
||||
};
|
||||
|
||||
@@ -89,19 +112,11 @@ class Q_EXPORT GLFilterContext : public VideoFilterContext
|
||||
{
|
||||
public:
|
||||
virtual Type type() const; //OpenGL
|
||||
protected:
|
||||
virtual bool isReady() const;
|
||||
virtual bool prepare();
|
||||
};
|
||||
|
||||
template<class T> struct TypeTrait {};
|
||||
template<> struct TypeTrait<QPainterFilterContext> {
|
||||
enum {
|
||||
type = FilterContext::QtPainter
|
||||
};
|
||||
};
|
||||
template<> struct TypeTrait<GLFilterContext> {
|
||||
enum {
|
||||
type = FilterContext::OpenGL
|
||||
};
|
||||
};
|
||||
|
||||
} //namespace QtAV
|
||||
|
||||
|
||||
@@ -46,13 +46,10 @@ public:
|
||||
ShowType showType() const;
|
||||
void useNextShowType();
|
||||
bool hasShowType(ShowType t) const;
|
||||
void setFont(const QFont& font);
|
||||
QFont font() const;
|
||||
QString text(Statistics* statistics);
|
||||
protected:
|
||||
ShowType mShowType;
|
||||
int mSecsTotal;
|
||||
QFont mFont;
|
||||
};
|
||||
|
||||
}//namespace QtAV
|
||||
|
||||
+14
-9
@@ -36,22 +36,27 @@ protected:
|
||||
OSDFilter(OSDFilterPrivate& d);
|
||||
};
|
||||
|
||||
template<class Context>
|
||||
class OSDFilterImpl : public OSDFilter
|
||||
class OSDFilterQPainter : public OSDFilter
|
||||
{
|
||||
//DPTR_DECLARE_PRIVATE(OSDFilterImpl<Context>)
|
||||
public:
|
||||
OSDFilterImpl();
|
||||
~OSDFilterImpl() {}
|
||||
OSDFilterQPainter();
|
||||
FilterContext::Type contextType() const {
|
||||
return (FilterContext::Type)TypeTrait<Context>::type;
|
||||
return FilterContext::QtPainter;
|
||||
}
|
||||
protected:
|
||||
virtual void process();
|
||||
void process();
|
||||
};
|
||||
|
||||
typedef OSDFilterImpl<QPainterFilterContext> OSDFilterQPainter;
|
||||
typedef OSDFilterImpl<GLFilterContext> OSDFilterGL;
|
||||
class OSDFilterGL : public OSDFilter
|
||||
{
|
||||
public:
|
||||
OSDFilterGL();
|
||||
FilterContext::Type contextType() const {
|
||||
return FilterContext::OpenGL;
|
||||
}
|
||||
protected:
|
||||
void process();
|
||||
};
|
||||
|
||||
} //namespace QtAV
|
||||
|
||||
|
||||
@@ -36,14 +36,12 @@ public:
|
||||
enabled(true)
|
||||
, context(0)
|
||||
, statistics(0)
|
||||
, opacity(1)
|
||||
{}
|
||||
virtual ~FilterPrivate() {}
|
||||
|
||||
bool enabled;
|
||||
FilterContext *context; //used only when is necessary
|
||||
Statistics *statistics;
|
||||
qreal opacity;
|
||||
};
|
||||
|
||||
} //namespace QtAV
|
||||
|
||||
Reference in New Issue
Block a user