QGIS API Documentation 3.39.0-Master (3aed037ce22)
Loading...
Searching...
No Matches
qgsmaprenderercustompainterjob.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaprenderercustompainterjob.cpp
3 --------------------------------------
4 Date : December 2013
5 Copyright : (C) 2013 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgsfeedback.h"
19#include "qgslabelingengine.h"
20#include "qgslogger.h"
21#include "qgsmaplayerrenderer.h"
23#include "qgselevationmap.h"
24#include "qgspainting.h"
25
26#include <QtConcurrentRun>
27
28//
29// QgsMapRendererAbstractCustomPainterJob
30//
31
37
38void QgsMapRendererAbstractCustomPainterJob::preparePainter( QPainter *painter, const QColor &backgroundColor )
39{
40 // clear the background
41 painter->fillRect( 0, 0, mSettings.deviceOutputSize().width(), mSettings.deviceOutputSize().height(), backgroundColor );
42
43 painter->setRenderHint( QPainter::Antialiasing, mSettings.testFlag( Qgis::MapSettingsFlag::Antialiasing ) );
44 painter->setRenderHint( QPainter::SmoothPixmapTransform, mSettings.testFlag( Qgis::MapSettingsFlag::HighQualityImageTransforms ) );
45 painter->setRenderHint( QPainter::LosslessImageRendering, mSettings.testFlag( Qgis::MapSettingsFlag::LosslessImageRendering ) );
46
47#ifndef QT_NO_DEBUG
48 QPaintDevice *paintDevice = painter->device();
49 const QString errMsg = QStringLiteral( "pre-set DPI not equal to painter's DPI (%1 vs %2)" )
50 .arg( paintDevice->logicalDpiX() )
51 .arg( mSettings.outputDpi() );
52 Q_ASSERT_X( qgsDoubleNear( paintDevice->logicalDpiX(), mSettings.outputDpi(), 1.0 ),
53 "Job::startRender()", errMsg.toLatin1().data() );
54#endif
55}
56
57
58//
59// QgsMapRendererCustomPainterJob
60//
61
64 , mPainter( painter )
65 , mActive( false )
66 , mRenderSynchronously( false )
67{
68 QgsDebugMsgLevel( QStringLiteral( "QPAINTER construct" ), 5 );
69}
70
72{
73 QgsDebugMsgLevel( QStringLiteral( "QPAINTER destruct" ), 5 );
74 Q_ASSERT( !mFutureWatcher.isRunning() );
75 //cancel();
76}
77
78void QgsMapRendererCustomPainterJob::startPrivate()
79{
80 if ( isActive() )
81 return;
82
83 if ( !mPrepareOnly )
84 mRenderingStart.start();
85
86 mActive = true;
87
88 mErrors.clear();
89
90 QgsDebugMsgLevel( QStringLiteral( "QPAINTER run!" ), 5 );
91
92 QgsDebugMsgLevel( QStringLiteral( "Preparing list of layer jobs for rendering" ), 5 );
93 QElapsedTimer prepareTime;
94 prepareTime.start();
95
97
98 mLabelingEngineV2.reset();
99
101 {
102 mLabelingEngineV2.reset( new QgsDefaultLabelingEngine() );
103 mLabelingEngineV2->setMapSettings( mSettings );
104 }
105
106 const bool canUseLabelCache = prepareLabelCache();
107 mLayerJobs = prepareJobs( mPainter, mLabelingEngineV2.get() );
108 mLabelJob = prepareLabelingJob( mPainter, mLabelingEngineV2.get(), canUseLabelCache );
109 mSecondPassLayerJobs = prepareSecondPassJobs( mLayerJobs, mLabelJob );
110
111 QgsDebugMsgLevel( QStringLiteral( "Rendering prepared in (seconds): %1" ).arg( prepareTime.elapsed() / 1000.0 ), 4 );
112
113 if ( mRenderSynchronously )
114 {
115 if ( !mPrepareOnly )
116 {
117 // do the rendering right now!
118 doRender();
119 }
120 return;
121 }
122
123 // now we are ready to start rendering!
124 connect( &mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsMapRendererCustomPainterJob::futureFinished );
125
126 mFuture = QtConcurrent::run( staticRender, this );
127 mFutureWatcher.setFuture( mFuture );
128}
129
130
132{
133 if ( !isActive() )
134 {
135 QgsDebugMsgLevel( QStringLiteral( "QPAINTER not running!" ), 4 );
136 return;
137 }
138
139 QgsDebugMsgLevel( QStringLiteral( "QPAINTER canceling" ), 5 );
140 disconnect( &mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsMapRendererCustomPainterJob::futureFinished );
142
143 QElapsedTimer t;
144 t.start();
145
146 mFutureWatcher.waitForFinished();
147
148 QgsDebugMsgLevel( QStringLiteral( "QPAINER cancel waited %1 ms" ).arg( t.elapsed() / 1000.0 ), 5 );
149
150 futureFinished();
151
152 QgsDebugMsgLevel( QStringLiteral( "QPAINTER canceled" ), 5 );
153}
154
156{
157 if ( !isActive() )
158 {
159 QgsDebugError( QStringLiteral( "QPAINTER not running!" ) );
160 return;
161 }
162
163 mLabelJob.context.setRenderingStopped( true );
164 for ( LayerRenderJob &job : mLayerJobs )
165 {
166 job.context()->setRenderingStopped( true );
167 if ( job.renderer && job.renderer->feedback() )
168 job.renderer->feedback()->cancel();
169 }
170}
171
173{
174 if ( !isActive() )
175 return;
176
177 disconnect( &mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsMapRendererCustomPainterJob::futureFinished );
178
179 QElapsedTimer t;
180 t.start();
181
182 mFutureWatcher.waitForFinished();
183
184 QgsDebugMsgLevel( QStringLiteral( "waitForFinished: %1 ms" ).arg( t.elapsed() / 1000.0 ), 4 );
185
186 futureFinished();
187}
188
190{
191 return mActive;
192}
193
195{
196 return mLabelJob.cached;
197}
198
200{
201 if ( mLabelingEngineV2 )
202 return mLabelingEngineV2->takeResults();
203 else
204 return nullptr;
205}
206
207
208void QgsMapRendererCustomPainterJob::waitForFinishedWithEventLoop( QEventLoop::ProcessEventsFlags flags )
209{
210 QEventLoop loop;
211 connect( &mFutureWatcher, &QFutureWatcher<void>::finished, &loop, &QEventLoop::quit );
212 loop.exec( flags );
213}
214
215
217{
218 mRenderSynchronously = true;
219 start();
220 futureFinished();
221 mRenderSynchronously = false;
222}
223
225{
226 mRenderSynchronously = true;
227 mPrepareOnly = true;
228 start();
229 mPrepared = true;
230}
231
233{
234 if ( !mPrepared )
235 return;
236
237 doRender();
238 futureFinished();
239 mRenderSynchronously = false;
240 mPrepareOnly = false;
241 mPrepared = false;
242}
243
244void QgsMapRendererCustomPainterJob::futureFinished()
245{
246 mActive = false;
247 if ( !mPrepared ) // can't access from other thread
249 QgsDebugMsgLevel( QStringLiteral( "QPAINTER futureFinished" ), 5 );
250
251 if ( !mPrepared )
252 logRenderingTime( mLayerJobs, {}, mLabelJob );
253
254 // final cleanup
255 cleanupJobs( mLayerJobs );
256 cleanupSecondPassJobs( mSecondPassLayerJobs );
257 cleanupLabelJob( mLabelJob );
258
259 emit finished();
260}
261
262
263void QgsMapRendererCustomPainterJob::staticRender( QgsMapRendererCustomPainterJob *self )
264{
265 try
266 {
267 self->doRender();
268 }
269 catch ( QgsException &e )
270 {
271 Q_UNUSED( e )
272 QgsDebugError( "Caught unhandled QgsException: " + e.what() );
273 }
274 catch ( std::exception &e )
275 {
276 Q_UNUSED( e )
277 QgsDebugError( "Caught unhandled std::exception: " + QString::fromLatin1( e.what() ) );
278 }
279 catch ( ... )
280 {
281 QgsDebugError( QStringLiteral( "Caught unhandled unknown exception" ) );
282 }
283}
284
285void QgsMapRendererCustomPainterJob::doRender()
286{
287 const bool hasSecondPass = ! mSecondPassLayerJobs.empty();
288 QgsDebugMsgLevel( QStringLiteral( "Starting to render layer stack." ), 5 );
289 QElapsedTimer renderTime;
290 renderTime.start();
291
293 std::unique_ptr<QgsElevationMap> mainElevationMap;
294 if ( mapShadingRenderer.isActive() )
295 mainElevationMap.reset( new QgsElevationMap( mSettings.deviceOutputSize(), mSettings.devicePixelRatio() ) );
296
297 for ( LayerRenderJob &job : mLayerJobs )
298 {
299 if ( job.context()->renderingStopped() )
300 break;
301
302 emit layerRenderingStarted( job.layerId );
303
304 if ( ! hasSecondPass && job.context()->useAdvancedEffects() )
305 {
306 // Set the QPainter composition mode so that this layer is rendered using
307 // the desired blending mode
308 mPainter->setCompositionMode( job.blendMode );
309 }
310
311 if ( !job.cached )
312 {
313 QElapsedTimer layerTime;
314 layerTime.start();
315
316 if ( job.previewRenderImage && !job.previewRenderImageInitialized )
317 {
318 job.previewRenderImage->fill( 0 );
319 job.previewRenderImageInitialized = true;
320 }
321
322 if ( job.img )
323 {
324 job.img->fill( 0 );
325 job.imageInitialized = true;
326 }
327
328 job.completed = job.renderer->render();
329
330 if ( job.picture )
331 {
332 job.renderer->renderContext()->painter()->end();
333 }
334
335 job.renderingTime += layerTime.elapsed();
336 }
337
338 if ( ! hasSecondPass && job.img )
339 {
340 // If we flattened this layer for alternate blend modes, composite it now
341 mPainter->setOpacity( job.opacity );
342 mPainter->drawImage( 0, 0, *job.img );
343 mPainter->setOpacity( 1.0 );
344 }
345
346 if ( mainElevationMap && job.context()->elevationMap() )
347 {
348 const QgsElevationMap &layerElevationMap = *job.context()->elevationMap();
349 if ( layerElevationMap.isValid() )
350 mainElevationMap->combine( layerElevationMap, mapShadingRenderer.combinedElevationMethod() );
351 }
352
353 emit layerRendered( job.layerId );
354 }
355
357 QgsDebugMsgLevel( QStringLiteral( "Done rendering map layers" ), 5 );
358
359 if ( mapShadingRenderer.isActive() && mainElevationMap )
360 {
361 QImage image( mainElevationMap->rawElevationImage().size(), QImage::Format_RGB32 );
362 image.setDevicePixelRatio( mSettings.devicePixelRatio() );
363 image.fill( Qt::white );
364 mapShadingRenderer.renderShading( *mainElevationMap.get(), image, QgsRenderContext::fromMapSettings( mSettings ) );
365 mPainter->save();
366 mPainter->setCompositionMode( QPainter::CompositionMode_Multiply );
367 mPainter->drawImage( 0, 0, image );
368 mPainter->restore();
369 }
370
371 if ( mSettings.testFlag( Qgis::MapSettingsFlag::DrawLabeling ) && !mLabelJob.context.renderingStopped() )
372 {
373 if ( !mLabelJob.cached )
374 {
375 QElapsedTimer labelTime;
376 labelTime.start();
377
378 if ( mLabelJob.img )
379 {
380 QPainter painter;
381 mLabelJob.img->fill( 0 );
382 painter.begin( mLabelJob.img );
383 mLabelJob.context.setPainter( &painter );
384 drawLabeling( mLabelJob.context, mLabelingEngineV2.get(), &painter );
385 painter.end();
386 }
387 else if ( mLabelJob.picture )
388 {
389 QPainter painter;
390 painter.begin( mLabelJob.picture.get() );
391 mLabelJob.context.setPainter( &painter );
392 drawLabeling( mLabelJob.context, mLabelingEngineV2.get(), &painter );
393 painter.end();
394 }
395 else
396 {
397 drawLabeling( mLabelJob.context, mLabelingEngineV2.get(), mPainter );
398 }
399
400 mLabelJob.complete = true;
401 mLabelJob.renderingTime = labelTime.elapsed();
402 mLabelJob.participatingLayers = _qgis_listRawToQPointer( mLabelingEngineV2->participatingLayers() );
403 }
404 }
405
406 if ( ! hasSecondPass )
407 {
408 if ( mLabelJob.img && mLabelJob.complete )
409 {
410 mPainter->setCompositionMode( QPainter::CompositionMode_SourceOver );
411 mPainter->setOpacity( 1.0 );
412 mPainter->drawImage( 0, 0, *mLabelJob.img );
413 }
414 }
415 else
416 {
417 initSecondPassJobs( mSecondPassLayerJobs, mLabelJob );
418
419 for ( LayerRenderJob &job : mSecondPassLayerJobs )
420 {
421 if ( job.context()->renderingStopped() )
422 break;
423
424 if ( !job.cached )
425 {
426 QElapsedTimer layerTime;
427 layerTime.start();
428
429 if ( job.previewRenderImage && !job.previewRenderImageInitialized )
430 {
431 job.previewRenderImage->fill( 0 );
432 job.previewRenderImageInitialized = true;
433 }
434
435 if ( job.img )
436 {
437 job.img->fill( 0 );
438 job.imageInitialized = true;
439 }
440
441 job.completed = job.renderer->render();
442
443 if ( job.picture )
444 {
445 job.renderer->renderContext()->painter()->end();
446 }
447
448 job.renderingTime += layerTime.elapsed();
449 }
450 }
451
453 composeSecondPass( mSecondPassLayerJobs, mLabelJob, forceVector );
454
455 if ( !forceVector )
456 {
457 const QImage finalImage = composeImage( mSettings, mLayerJobs, mLabelJob );
458
459 mPainter->setCompositionMode( QPainter::CompositionMode_SourceOver );
460 mPainter->setOpacity( 1.0 );
461 mPainter->drawImage( 0, 0, finalImage );
462 }
463 else
464 {
465 //Vector composition is simply draw the saved picture on the painter
466 for ( LayerRenderJob &job : mLayerJobs )
467 {
468 // if there is vector rendering we use it, else we use the raster rendering
469 if ( job.picture )
470 {
471 QgsPainting::drawPicture( mPainter, QPointF( 0, 0 ), *job.picture );
472 }
473 else
474 mPainter->drawImage( 0, 0, *job.img );
475 }
476
477 if ( mLabelJob.picture )
478 {
479 QgsPainting::drawPicture( mPainter, QPointF( 0, 0 ), *mLabelJob.picture );
480 }
481 }
482 }
483
484 QgsDebugMsgLevel( QStringLiteral( "Rendering completed in (seconds): %1" ).arg( renderTime.elapsed() / 1000.0 ), 2 );
485}
@ ForceVectorOutput
Vector graphics should not be cached and drawn as raster images.
@ ForceRasterMasks
Force symbol masking to be applied using a raster method. This is considerably faster when compared t...
@ LosslessImageRendering
Render images losslessly whenever possible, instead of the default lossy jpeg rendering used for some...
@ Antialiasing
Enable anti-aliasing for map rendering.
@ DrawLabeling
Enable drawing of labels on top of the map.
@ HighQualityImageTransforms
Enable high quality image transformations, which results in better appearance of scaled or rotated ra...
Default QgsLabelingEngine implementation, which completes the whole labeling operation (including lab...
Stores digital elevation model in a raster image which may get updated as a part of map layer renderi...
void combine(const QgsElevationMap &otherElevationMap, Qgis::ElevationMapCombineMethod method)
Combines this elevation map with otherElevationMap.
bool isValid() const
Returns whether the elevation map is valid.
This class can render elevation shading on an image with different methods (eye dome lighting,...
Qgis::ElevationMapCombineMethod combinedElevationMethod() const
Returns the method used when conbining different elevation sources.
bool isActive() const
Returns whether this shading renderer is active.
void renderShading(const QgsElevationMap &elevation, QImage &image, const QgsRenderContext &context) const
Render shading on image condidering the elevation map elevation and the renderer context context If e...
Defines a QGIS exception class.
QString what() const
Class that stores computed placement from labeling engine.
Abstract base class for map renderer jobs which use custom painters.
void preparePainter(QPainter *painter, const QColor &backgroundColor=Qt::transparent)
Prepares the given painter ready for a map render.
QgsMapRendererAbstractCustomPainterJob(const QgsMapSettings &settings)
Constructor for QgsMapRendererAbstractCustomPainterJob, using the given map settings.
Job implementation that renders everything sequentially using a custom painter.
QgsMapRendererCustomPainterJob(const QgsMapSettings &settings, QPainter *painter)
void renderSynchronously()
Render the map synchronously in this thread.
void cancel() override
Stop the rendering job - does not return until the job has terminated.
void waitForFinishedWithEventLoop(QEventLoop::ProcessEventsFlags flags=QEventLoop::AllEvents)
Wait for the job to be finished - and keep the thread's event loop running while waiting.
void renderPrepared()
Render a pre-prepared job.
QgsLabelingResults * takeLabelingResults() override
Gets pointer to internal labeling engine (in order to get access to the results).
void cancelWithoutBlocking() override
Triggers cancellation of the rendering job without blocking.
void prepare()
Prepares the job for rendering synchronously in a background thread.
void waitForFinished() override
Block until the job has finished.
bool isActive() const override
Tell whether the rendering job is currently running in background.
bool usedCachedLabels() const override
Returns true if the render job was able to use a cached labeling solution.
Abstract base class for map rendering implementations.
void logRenderingTime(const std::vector< LayerRenderJob > &jobs, const std::vector< LayerRenderJob > &secondPassJobs, const LabelRenderJob &labelJob)
static QImage composeImage(const QgsMapSettings &settings, const std::vector< LayerRenderJob > &jobs, const LabelRenderJob &labelJob, const QgsMapRendererCache *cache=nullptr)
void cleanupSecondPassJobs(std::vector< LayerRenderJob > &jobs)
void initSecondPassJobs(std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob) const
Initialize secondPassJobs according to what have been rendered (mask clipping path e....
static Q_DECL_DEPRECATED void drawLabeling(const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter)
void layerRendered(const QString &layerId)
Emitted when a layer has completed rendering.
std::vector< LayerRenderJob > prepareJobs(QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet=false)
Creates a list of layer rendering jobs and prepares them for later render.
void renderingLayersFinished()
Emitted when the layers are rendered.
void cleanupJobs(std::vector< LayerRenderJob > &jobs)
QElapsedTimer mRenderingStart
void finished()
emitted when asynchronous rendering is finished (or canceled).
QgsMapSettings mSettings
void start()
Start the rendering job and immediately return.
void layerRenderingStarted(const QString &layerId)
Emitted just before rendering starts for a particular layer.
static void composeSecondPass(std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob, bool forceVector=false)
Compose second pass images into first pass images.
std::vector< LayerRenderJob > prepareSecondPassJobs(std::vector< LayerRenderJob > &firstPassJobs, LabelRenderJob &labelJob)
Prepares jobs for a second pass, if selective masks exist (from labels or symbol layers).
LabelRenderJob prepareLabelingJob(QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache=true)
Prepares a labeling job.
void cleanupLabelJob(LabelRenderJob &job)
Handles clean up tasks for a label job, including deletion of images and storing cached label results...
bool prepareLabelCache() const
Prepares the cache for storing the result of labeling.
The QgsMapSettings class contains configuration for rendering of the map.
QSize deviceOutputSize() const
Returns the device output size of the map render.
QColor backgroundColor() const
Returns the background color of the map.
float devicePixelRatio() const
Returns the device pixel ratio.
const QgsElevationShadingRenderer & elevationShadingRenderer() const
Returns the shading renderer used to render shading on the entire map.
double outputDpi() const
Returns the DPI (dots per inch) used for conversion between real world units (e.g.
bool testFlag(Qgis::MapSettingsFlag flag) const
Check whether a particular flag is enabled.
static void drawPicture(QPainter *painter, const QPointF &point, const QPicture &picture)
Draws a picture onto a painter, correctly applying workarounds to avoid issues with incorrect scaling...
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5652
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38