diff options
author | Thiago Marcos P. Santos <tmpsantos@gmail.com> | 2018-01-25 02:20:09 +0200 |
---|---|---|
committer | Thiago Marcos P. Santos <tmpsantos@gmail.com> | 2018-02-09 20:28:54 +0200 |
commit | 4f8a23cd8fe920ca7009e0ca6689d0c5bcb8c7eb (patch) | |
tree | 7e5784f10471f378fb3dae685af2b6e65e4732ee | |
parent | bb00f558fb31f000eefa1bb825bb580445cedc10 (diff) | |
download | qtlocation-mapboxgl-4f8a23cd8fe920ca7009e0ca6689d0c5bcb8c7eb.tar.gz |
[qt] Expose an interface to create a renderer on a separated thread
If not called, it will render on the main thread as usual.
-rw-r--r-- | platform/qt/include/qmapboxgl.hpp | 5 | ||||
-rw-r--r-- | platform/qt/src/qmapboxgl.cpp | 38 |
2 files changed, 37 insertions, 6 deletions
diff --git a/platform/qt/include/qmapboxgl.hpp b/platform/qt/include/qmapboxgl.hpp index 8b319b0453..a1758ba6e6 100644 --- a/platform/qt/include/qmapboxgl.hpp +++ b/platform/qt/include/qmapboxgl.hpp @@ -226,6 +226,11 @@ public: void setFilter(const QString &layer, const QVariant &filter); + // When rendering on a different thread, + // should be called on this thread + void createRenderer(); + void destroyRenderer(); + public slots: void render(); void connectionEstablished(); diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index cf6be2c9a7..89904ddf88 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -1468,6 +1468,23 @@ void QMapboxGL::setFilter(const QString& layer, const QVariant& filter) qWarning() << "Layer doesn't support filters"; } +void QMapboxGL::createRenderer() +{ + d_ptr->mapRenderer = std::make_unique<QMapboxGLMapRenderer>( + d_ptr->pixelRatio, + *d_ptr->fileSourceObj, + *d_ptr->threadPool, + d_ptr->mode + ); + + d_ptr->mapRenderer->setObserver(d_ptr->rendererObserver); +} + +void QMapboxGL::destroyRenderer() +{ + d_ptr->mapRenderer.reset(); +} + /*! Renders the map using OpenGL draw calls. It will make sure to bind the framebuffer object before drawing; otherwise a valid OpenGL context is @@ -1479,6 +1496,10 @@ void QMapboxGL::setFilter(const QString& layer, const QVariant& filter) */ void QMapboxGL::render() { + if (!d_ptr->mapRenderer) { + createRenderer(); + } + #if defined(__APPLE__) && QT_VERSION < 0x050000 // FIXME Qt 4.x provides an incomplete FBO at start. // See https://bugreports.qt.io/browse/QTBUG-36802 for details. @@ -1527,7 +1548,7 @@ void QMapboxGL::connectionEstablished() \a copyrightsHtml is a string with a HTML snippet. */ -QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settings, const QSize &size, qreal pixelRatio) +QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settings, const QSize &size, qreal pixelRatio_) : QObject(q) , q_ptr(q) , fileSourceObj(sharedDefaultFileSource( @@ -1535,6 +1556,8 @@ QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settin settings.assetPath().toStdString(), settings.cacheDatabaseMaximumSize())) , threadPool(mbgl::sharedThreadPool()) + , mode(settings.contextMode()) + , pixelRatio(pixelRatio_) { // Setup the FileSource fileSourceObj->setAccessToken(settings.accessToken().toStdString()); @@ -1556,9 +1579,6 @@ QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settin connect(mapObserver.get(), SIGNAL(mapChanged(QMapboxGL::MapChange)), q_ptr, SIGNAL(mapChanged(QMapboxGL::MapChange))); connect(mapObserver.get(), SIGNAL(copyrightsChanged(QString)), q_ptr, SIGNAL(copyrightsChanged(QString))); - // Setup RendererBackend - mapRenderer = std::make_unique<QMapboxGLMapRenderer>(pixelRatio, *fileSourceObj, *threadPool, settings.contextMode()); - // Setup the Map object mapObj = std::make_unique<mbgl::Map>( *this, // RendererFrontend @@ -1579,6 +1599,10 @@ QMapboxGLPrivate::~QMapboxGLPrivate() void QMapboxGLPrivate::update(std::shared_ptr<mbgl::UpdateParameters> parameters) { + if (!mapRenderer) { + return; + } + mapRenderer->updateParameters(std::move(parameters)); if (!renderQueued.test_and_set()) { @@ -1588,8 +1612,10 @@ void QMapboxGLPrivate::update(std::shared_ptr<mbgl::UpdateParameters> parameters void QMapboxGLPrivate::setObserver(mbgl::RendererObserver &observer) { - m_rendererObserver = std::make_shared<QMapboxGLRendererObserver>( + rendererObserver = std::make_shared<QMapboxGLRendererObserver>( *mbgl::util::RunLoop::Get(), observer); - mapRenderer->setObserver(m_rendererObserver); + if (mapRenderer) { + mapRenderer->setObserver(rendererObserver); + } } |