Skip to content
Permalink
Browse files

Don't block when canceling canvas render jobs

In some cases canceling render jobs can take a long time. Eg when
using database layers over a sloooooow connection, canceling a job
can be blocked by minutes while waiting for the first batch of feature
fetching to finish. (Since eg postgres features are fetched in batches
of 2000 with no opportunity to abort mid-way through this).

This meant that while the first render allows the GUI to remain
responsive, any subsequent render operations which occured before
the first render completes locks up the whole ui until the first
render can finish cancellation.

With this change, the render cancelation happens with blocking.
It means that you can pan and zoom around a map over of slow
connection without any ui locks.
  • Loading branch information
nyalldawson committed Mar 2, 2017
1 parent e9dee40 commit 3b56b79f6d3f002dbea1bc2879b424a1a92c8ec5
@@ -19,6 +19,7 @@ class QgsMapRendererCustomPainterJob : QgsMapRendererJob

virtual void start();
virtual void cancel();
virtual void cancelWithoutBlocking();
virtual void waitForFinished();
virtual bool isActive() const;
virtual bool usedCachedLabels() const;
@@ -18,6 +18,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;

@@ -18,6 +18,7 @@ class QgsMapRendererParallelJob : QgsMapRendererQImageJob

virtual void start();
virtual void cancel();
virtual void cancelWithoutBlocking();
virtual void waitForFinished();
virtual bool isActive() const;
virtual bool usedCachedLabels() const;
@@ -19,6 +19,7 @@ class QgsMapRendererSequentialJob : QgsMapRendererQImageJob

virtual void start();
virtual void cancel();
virtual void cancelWithoutBlocking();
virtual void waitForFinished();
virtual bool isActive() const;
virtual bool usedCachedLabels() const;
@@ -109,14 +109,7 @@ void QgsMapRendererCustomPainterJob::cancel()

QgsDebugMsg( "QPAINTER canceling" );
disconnect( &mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsMapRendererCustomPainterJob::futureFinished );

mLabelJob.context.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();
@@ -130,6 +123,23 @@ void QgsMapRendererCustomPainterJob::cancel()
QgsDebugMsg( "QPAINTER canceled" );
}

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

mLabelJob.context.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()
{
if ( !isActive() )
@@ -39,6 +39,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 bool usedCachedLabels() const override;
@@ -121,6 +121,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;

@@ -296,6 +303,7 @@ class CORE_EXPORT QgsMapRendererQImageJob : public QgsMapRendererJob

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

};


@@ -108,6 +108,28 @@ void QgsMapRendererParallelJob::cancel()
Q_ASSERT( mStatus == Idle );
}

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

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

mLabelJob.context.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, &QFutureWatcher<void>::finished, this, &QgsMapRendererParallelJob::renderLayersFinished );
connect( &mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsMapRendererParallelJob::renderingFinished );
}
}

void QgsMapRendererParallelJob::waitForFinished()
{
if ( !isActive() )
@@ -36,6 +36,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;

@@ -83,6 +83,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() )
@@ -38,6 +38,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;

@@ -604,8 +604,10 @@ void QgsMapCanvas::stopRendering()
{
QgsDebugMsg( "CANVAS stop rendering!" );
mJobCanceled = true;
mJob->cancel();
Q_ASSERT( !mJob ); // no need to delete here: already deleted in finished()
disconnect( mJob, &QgsMapRendererJob::finished, this, &QgsMapCanvas::rendererJobFinished );
connect( mJob, &QgsMapRendererQImageJob::finished, mJob, &QgsMapRendererQImageJob::deleteLater );
mJob->cancelWithoutBlocking();
mJob = nullptr;
}
}

0 comments on commit 3b56b79

Please sign in to comment.
You can’t perform that action at this time.