From a4d259c33f9bb890bba97fd89552720e3e0ec09b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Mon, 10 Oct 2016 17:16:37 +0200 Subject: [core] move gl::Context to Backend and refactor View --- platform/qt/src/qmapboxgl.cpp | 84 ++++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 33 deletions(-) (limited to 'platform/qt/src/qmapboxgl.cpp') diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index 73a1771908..edda1f9599 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -23,6 +24,7 @@ #if QT_VERSION >= 0x050000 #include #include +#include #else #include #endif @@ -271,7 +273,7 @@ void QMapboxGLSettings::setAccessToken(const QString &token) Constructs a QMapboxGL object with \a settings and sets \a parent as the parent object. The \a settings cannot be changed after the object is constructed. */ -QMapboxGL::QMapboxGL(QObject *parent, const QMapboxGLSettings &settings) +QMapboxGL::QMapboxGL(QObject *parent, const QMapboxGLSettings &settings, const QSize& size, qreal pixelRatio) : QObject(parent) { // Multiple QMapboxGL running on the same thread @@ -280,7 +282,7 @@ QMapboxGL::QMapboxGL(QObject *parent, const QMapboxGLSettings &settings) loop.setLocalData(std::make_shared()); } - d_ptr = new QMapboxGLPrivate(this, settings); + d_ptr = new QMapboxGLPrivate(this, settings, size, pixelRatio); } QMapboxGL::~QMapboxGL() @@ -602,15 +604,14 @@ void QMapboxGL::rotateBy(const QPointF &first, const QPointF &second) mbgl::ScreenCoordinate { second.x(), second.y() }); } -void QMapboxGL::resize(const QSize& size) +void QMapboxGL::resize(const QSize& size, const QSize& framebufferSize) { - QSize converted = size / d_ptr->getPixelRatio(); - if (d_ptr->size == converted) return; + if (d_ptr->size == size && d_ptr->fbSize == framebufferSize) return; - glViewport(0, 0, converted.width(), converted.height()); + d_ptr->size = size; + d_ptr->fbSize = framebufferSize; - d_ptr->size = converted; - d_ptr->mapObj->update(mbgl::Update::Dimensions); + d_ptr->mapObj->setSize({{ static_cast(size.width()), static_cast(size.height()) }}); } void QMapboxGL::addAnnotationIcon(const QString &name, const QImage &sprite) @@ -793,19 +794,29 @@ void QMapboxGL::setFilter(const QString& layer_, const QVariant& filter_) qWarning() << "Layer doesn't support filters"; } +#if QT_VERSION >= 0x050000 +void QMapboxGL::render(QOpenGLFramebufferObject *fbo) +{ + d_ptr->dirty = false; + d_ptr->updateFramebufferBinding(fbo); + d_ptr->mapObj->render(*d_ptr); +} +#else void QMapboxGL::render() { d_ptr->dirty = false; - d_ptr->mapObj->render(); + d_ptr->mapObj->render(*d_ptr); } +#endif void QMapboxGL::connectionEstablished() { d_ptr->connectionEstablished(); } -QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settings) +QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settings, const QSize &size_, qreal pixelRatio) : QObject(q) + , size(size_) , q_ptr(q) , fileSourceObj(std::make_unique( settings.cacheDatabasePath().toStdString(), @@ -813,7 +824,8 @@ QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settin settings.cacheDatabaseMaximumSize())) , threadPool(4) , mapObj(std::make_unique( - *this, *this, getPixelRatio(), *fileSourceObj, threadPool, + *this, std::array{{ static_cast(size.width()), static_cast(size.height()) }}, + pixelRatio, *fileSourceObj, threadPool, static_cast(settings.mapMode()), static_cast(settings.contextMode()), static_cast(settings.constrainMode()), @@ -830,36 +842,42 @@ QMapboxGLPrivate::~QMapboxGLPrivate() { } -float QMapboxGLPrivate::getPixelRatio() const -{ #if QT_VERSION >= 0x050000 - // QWindow is the most reliable pixel ratio because QGuiApplication returns - // the maximum pixel ratio of all available QScreen objects - this is not - // valid for cases e.g. where two or more QScreen objects with different - // pixel ratios are present and the window shows on the screen with lower - // pixel ratio. - static const float pixelRatio = QGuiApplication::allWindows().first()->devicePixelRatio(); -#else - static const float pixelRatio = 1.0; -#endif - return pixelRatio; +void QMapboxGLPrivate::updateFramebufferBinding(QOpenGLFramebufferObject *fbo_) +{ + fbo = fbo_; + if (fbo) { + getContext().bindFramebuffer.setDirty(); + getContext().viewport.setCurrentValue( + { 0, 0, static_cast(fbo->width()), static_cast(fbo->height()) }); + } else { + getContext().bindFramebuffer.setCurrentValue(0); + getContext().viewport.setCurrentValue({ 0, 0, static_cast(fbSize.width()), + static_cast(fbSize.height()) }); + } } void QMapboxGLPrivate::bind() { - MBGL_CHECK_ERROR(glBindFramebuffer(GL_FRAMEBUFFER, 0)); -} - -std::array QMapboxGLPrivate::getSize() const -{ - return {{ static_cast(size.width()), static_cast(size.height()) }}; + if (fbo) { + fbo->bind(); + getContext().bindFramebuffer.setDirty(); + getContext().viewport = { 0, 0, static_cast(fbo->width()), + static_cast(fbo->height()) }; + } else { + getContext().bindFramebuffer = 0; + getContext().viewport = { 0, 0, static_cast(fbSize.width()), + static_cast(fbSize.height()) }; + } } - -std::array QMapboxGLPrivate::getFramebufferSize() const +#else +void QMapboxGLPrivate::bind() { - return {{ static_cast(size.width() * getPixelRatio()), - static_cast(size.height() * getPixelRatio()) }}; + getContext().bindFramebuffer = 0; + getContext().viewport = { 0, 0, static_cast(fbSize.width()), + static_cast(fbSize.height()) }; } +#endif void QMapboxGLPrivate::invalidate() { -- cgit v1.2.1