From d0aec08ff784618ae2f8fa15ea161cdfe83a80c2 Mon Sep 17 00:00:00 2001 From: wang-bin Date: Mon, 30 Sep 2013 14:59:25 +0800 Subject: [PATCH] avoid calling QPainter::end() and delete painter multiple times the whole context is shared before. now only some common data shared, e.g. painter. we can delete them in each context TODO: use shared ptr for those common ptr --- src/FilterContext.cpp | 30 ++++++++++++++++++++---------- src/QtAV/FilterContext.h | 3 +++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/FilterContext.cpp b/src/FilterContext.cpp index 33a3de43..e353c533 100644 --- a/src/FilterContext.cpp +++ b/src/FilterContext.cpp @@ -59,14 +59,17 @@ void FilterContext::initializeOnData(QByteArray *data) void FilterContext::shareFrom(FilterContext *ctx) { Q_UNUSED(ctx); + video_width = ctx->video_width; + video_height = ctx->video_height; } VideoFilterContext::VideoFilterContext(): FilterContext() - , painter(0) - , paint_device(0) - , own_paint_device(false) - , opacity(1) + , painter(0) + , paint_device(0) + , own_painter(false) + , own_paint_device(false) + , opacity(1) { font.setBold(true); font.setPixelSize(26); @@ -76,15 +79,19 @@ VideoFilterContext::VideoFilterContext(): VideoFilterContext::~VideoFilterContext() { - if (paint_device) { - if (painter) { //painter may assigned by vo - if (painter->isActive()) - painter->end(); - qDebug("delete painter"); + if (painter) { + // painter is shared, so may be end() multiple times. + // TODO: use shared ptr + //if (painter->isActive()) + // painter->end(); + if (own_painter) { + qDebug("VideoFilterContext %p delete painter %p", this, painter); delete painter; painter = 0; } - qDebug("delete paint device %p in %p", paint_device, this); + } + if (paint_device) { + qDebug("VideoFilterContext %p delete paint device in %p", this, paint_device); if (own_paint_device) delete paint_device; //delete recursively for widget paint_device = 0; @@ -120,6 +127,8 @@ void VideoFilterContext::shareFrom(FilterContext *ctx) VideoFilterContext *vctx = static_cast(ctx); painter = vctx->painter; paint_device = vctx->paint_device; + own_painter = false; + own_paint_device = false; video_width = vctx->video_width; video_height = vctx->video_height; } @@ -211,6 +220,7 @@ void QPainterFilterContext::initializeOnData(QByteArray *data) paint_device = new QImage((uchar*)data->data(), video_width, video_height, QImage::Format_RGB32); if (!painter) painter = new QPainter(); + own_painter = true; own_paint_device = true; //TODO: what about renderer is not a widget? painter->begin((QImage*)paint_device); } diff --git a/src/QtAV/FilterContext.h b/src/QtAV/FilterContext.h index 5941e177..7c144f40 100644 --- a/src/QtAV/FilterContext.h +++ b/src/QtAV/FilterContext.h @@ -93,6 +93,9 @@ public: * can we allocate memory on stack? */ QPaintDevice *paint_device; + +protected: + bool own_painter; bool own_paint_device; protected: virtual void shareFrom(FilterContext *ctx);