Skip to content

Commit

Permalink
Merge pull request #4207 from nyalldawson/cancel_job
Browse files Browse the repository at this point in the history
Backport non-blocking render cancellation
  • Loading branch information
nyalldawson authored Mar 2, 2017
2 parents 3057738 + a4219b3 commit 0d17e85
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 11 deletions.
1 change: 1 addition & 0 deletions python/core/qgsmaprenderercustompainterjob.sip
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class QgsMapRendererCustomPainterJob : QgsMapRendererJob

virtual void start();
virtual void cancel();
virtual void cancelWithoutBlocking();
virtual void waitForFinished();
virtual bool isActive() const;
virtual QgsLabelingResults* takeLabelingResults() /Transfer/;
Expand Down
2 changes: 2 additions & 0 deletions python/core/qgsmaprendererjob.sip
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class QgsMapRendererJob : QObject
//! Does nothing if the rendering is not active.
virtual void cancel() = 0;

virtual void cancelWithoutBlocking() = 0;

//! Block until the job has finished.
virtual void waitForFinished() = 0;

Expand Down
1 change: 1 addition & 0 deletions python/core/qgsmaprendererparalleljob.sip
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class QgsMapRendererParallelJob : QgsMapRendererQImageJob

virtual void start();
virtual void cancel();
virtual void cancelWithoutBlocking();
virtual void waitForFinished();
virtual bool isActive() const;

Expand Down
1 change: 1 addition & 0 deletions python/core/qgsmaprenderersequentialjob.sip
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class QgsMapRendererSequentialJob : QgsMapRendererQImageJob

virtual void start();
virtual void cancel();
virtual void cancelWithoutBlocking();
virtual void waitForFinished();
virtual bool isActive() const;

Expand Down
28 changes: 19 additions & 9 deletions src/core/qgsmaprenderercustompainterjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,7 @@ void QgsMapRendererCustomPainterJob::cancel()

QgsDebugMsg( "QPAINTER cancelling" );
disconnect( &mFutureWatcher, SIGNAL( finished() ), this, SLOT( futureFinished() ) );

mLabelingRenderContext.setRenderingStopped( true );
for ( LayerRenderJobs::iterator it = mLayerJobs.begin(); it != mLayerJobs.end(); ++it )
{
it->context.setRenderingStopped( true );
if ( it->renderer && it->renderer->feedback() )
it->renderer->feedback()->cancel();
}
cancelWithoutBlocking();

QTime t;
t.start();
Expand All @@ -144,7 +137,24 @@ void QgsMapRendererCustomPainterJob::cancel()

futureFinished();

QgsDebugMsg( "QPAINTER cancelled" );
QgsDebugMsg( "QPAINTER canceled" );
}

void QgsMapRendererCustomPainterJob::cancelWithoutBlocking()
{
if ( !isActive() )
{
QgsDebugMsg( "QPAINTER not running!" );
return;
}

mLabelingRenderContext.setRenderingStopped( true );
for ( LayerRenderJobs::iterator it = mLayerJobs.begin(); it != mLayerJobs.end(); ++it )
{
it->context.setRenderingStopped( true );
if ( it->renderer && it->renderer->feedback() )
it->renderer->feedback()->cancel();
}
}

void QgsMapRendererCustomPainterJob::waitForFinished()
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsmaprenderercustompainterjob.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class CORE_EXPORT QgsMapRendererCustomPainterJob : public QgsMapRendererJob

virtual void start() override;
virtual void cancel() override;
virtual void cancelWithoutBlocking() override;
virtual void waitForFinished() override;
virtual bool isActive() const override;
virtual QgsLabelingResults* takeLabelingResults() override;
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsmaprendererjob.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ class CORE_EXPORT QgsMapRendererJob : public QObject
//! Does nothing if the rendering is not active.
virtual void cancel() = 0;

/**
* Triggers cancelation of the rendering job without blocking. The render job will continue
* to operate until it is able to cancel, at which stage the finished() signal will be emitted.
* Does nothing if the rendering is not active.
*/
virtual void cancelWithoutBlocking() = 0;

//! Block until the job has finished.
virtual void waitForFinished() = 0;

Expand Down Expand Up @@ -206,6 +213,7 @@ class CORE_EXPORT QgsMapRendererQImageJob : public QgsMapRendererJob

//! Get a preview/resulting image
virtual QImage renderedImage() = 0;

};


Expand Down
22 changes: 22 additions & 0 deletions src/core/qgsmaprendererparalleljob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,28 @@ void QgsMapRendererParallelJob::cancel()
Q_ASSERT( mStatus == Idle );
}

void QgsMapRendererParallelJob::cancelWithoutBlocking()
{
if ( !isActive() )
return;

QgsDebugMsg( QString( "PARALLEL cancel at status %1" ).arg( mStatus ) );

mLabelingRenderContext.setRenderingStopped( true );
for ( LayerRenderJobs::iterator it = mLayerJobs.begin(); it != mLayerJobs.end(); ++it )
{
it->context.setRenderingStopped( true );
if ( it->renderer && it->renderer->feedback() )
it->renderer->feedback()->cancel();
}

if ( mStatus == RenderingLayers )
{
disconnect( &mFutureWatcher, SIGNAL( finished() ), this, SLOT( renderLayersFinished() ) );
connect( &mFutureWatcher, SIGNAL( finished() ), this, SLOT( renderingFinished() ) );
}
}

void QgsMapRendererParallelJob::waitForFinished()
{
if ( !isActive() )
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsmaprendererparalleljob.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class CORE_EXPORT QgsMapRendererParallelJob : public QgsMapRendererQImageJob

virtual void start() override;
virtual void cancel() override;
virtual void cancelWithoutBlocking() override;
virtual void waitForFinished() override;
virtual bool isActive() const override;

Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsmaprenderersequentialjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ void QgsMapRendererSequentialJob::cancel()
Q_ASSERT( !mInternalJob && !mPainter );
}

void QgsMapRendererSequentialJob::cancelWithoutBlocking()
{
if ( !isActive() )
return;

QgsDebugMsg( "sequential - cancel internal" );
mInternalJob->cancelWithoutBlocking();
}

void QgsMapRendererSequentialJob::waitForFinished()
{
if ( !isActive() )
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsmaprenderersequentialjob.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CORE_EXPORT QgsMapRendererSequentialJob : public QgsMapRendererQImageJob

virtual void start() override;
virtual void cancel() override;
virtual void cancelWithoutBlocking() override;
virtual void waitForFinished() override;
virtual bool isActive() const override;

Expand Down
6 changes: 4 additions & 2 deletions src/gui/qgsmapcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,10 @@ void QgsMapCanvas::stopRendering()
{
QgsDebugMsg( "CANVAS stop rendering!" );
mJobCancelled = true;
mJob->cancel();
Q_ASSERT( !mJob ); // no need to delete here: already deleted in finished()
disconnect( mJob, SIGNAL( finished() ), this, SLOT( rendererJobFinished() ) );
connect( mJob, SIGNAL( finished() ), mJob, SLOT( deleteLater() ) );
mJob->cancelWithoutBlocking();
mJob = nullptr;
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ ADD_PYTHON_TEST(PyQgsGeometryValidator test_qgsgeometryvalidator.py)
ADD_PYTHON_TEST(PyQgsGraduatedSymbolRendererV2 test_qgsgraduatedsymbolrendererv2.py)
ADD_PYTHON_TEST(PyQgsInterval test_qgsinterval.py)
ADD_PYTHON_TEST(PyQgsJSONUtils test_qgsjsonutils.py)
ADD_PYTHON_TEST(PyQgsMapRenderer test_qgsmaprenderer.py)
ADD_PYTHON_TEST(PyQgsMapUnitScale test_qgsmapunitscale.py)
ADD_PYTHON_TEST(PyQgsMemoryProvider test_provider_memory.py)
ADD_PYTHON_TEST(PyQgsMultiEditToolButton test_qgsmultiedittoolbutton.py)
Expand Down
112 changes: 112 additions & 0 deletions tests/src/python/test_qgsmaprenderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsMapRenderer.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '1/02/2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA

from qgis.core import (QgsMapRendererCache,
QgsMapRendererParallelJob,
QgsMapRendererSequentialJob,
QgsMapRendererCustomPainterJob,
QgsRectangle,
QgsVectorLayer,
QgsProject,
QgsFeature,
QgsGeometry,
QgsMapSettings,
QgsPoint)
from qgis.testing import start_app, unittest
from qgis.PyQt.QtCore import QSize, QThreadPool
from qgis.PyQt.QtGui import QPainter, QImage
from random import uniform


app = start_app()


class TestQgsMapRenderer(unittest.TestCase):

def setUp(self):
pass

def tearDown(self):
# avoid crash on finish, probably related to https://bugreports.qt.io/browse/QTBUG-35760
QThreadPool.globalInstance().waitForDone()

def checkCancel(self, job_type):
"""test canceling a render job"""
layer = QgsVectorLayer("Point?field=fldtxt:string",
"layer1", "memory")

# add a ton of random points
for i in range(2000):
x = uniform(5, 25)
y = uniform(25, 45)
g = QgsGeometry.fromPoint(QgsPoint(x, y))
f = QgsFeature()
f.setGeometry(g)
f.initAttributes(1)
layer.dataProvider().addFeatures([f])

settings = QgsMapSettings()
settings.setExtent(QgsRectangle(5, 25, 25, 45))
settings.setOutputSize(QSize(600, 400))
settings.setLayers([layer.id()])

# first try non-blocking cancelWithoutBlocking() call
job = job_type(settings)
job.start()

# insta cancel!
job.cancelWithoutBlocking()
# should still be active immediately after
self.assertTrue(job.isActive())

while job.isActive():
app.processEvents()

# try blocking cancel() call
job = job_type(settings)
job.start()

# insta cancel!
job.cancel()
# should not be active anymore
self.assertFalse(job.isActive())

def runRendererChecks(self, renderer):
""" runs all checks on the specified renderer """
self.checkCancel(renderer)

def testParallelRenderer(self):
""" run test suite on QgsMapRendererParallelJob"""
self.runRendererChecks(QgsMapRendererParallelJob)

def testSequentialRenderer(self):
""" run test suite on QgsMapRendererSequentialJob"""
self.runRendererChecks(QgsMapRendererSequentialJob)

def testCustomPainterRenderer(self):
""" run test suite on QgsMapRendererCustomPainterJob"""
im = QImage(200, 200, QImage.Format_RGB32)
p = QPainter(im)

def create_job(settings):
return QgsMapRendererCustomPainterJob(settings, p)

self.runRendererChecks(create_job)
p.end()


if __name__ == '__main__':
unittest.main()

0 comments on commit 0d17e85

Please sign in to comment.