summaryrefslogtreecommitdiff
path: root/platform/qt/src/qquickmapboxglrenderer.cpp
blob: 880d7b2e40ffebb5fc6652728db2857da0247672 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "qquickmapboxglrenderer.hpp"

#include "qmapboxgl.hpp"
#include "qquickmapboxgl.hpp"

#include <QSize>
#include <QOpenGLFramebufferObject>
#include <QOpenGLFramebufferObjectFormat>
#include <QQuickWindow>
#include <QThread>

QQuickMapboxGLRenderer::QQuickMapboxGLRenderer()
{
    QMapbox::initializeGLExtensions();

    QMapboxGLSettings settings;
    settings.setAccessToken(qgetenv("MAPBOX_ACCESS_TOKEN"));
    settings.setCacheDatabasePath("/tmp/mbgl-cache.db");
    settings.setCacheDatabaseMaximumSize(20 * 1024 * 1024);
    settings.setViewportMode(QMapboxGLSettings::FlippedYViewport);

    m_map.reset(new QMapboxGL(nullptr, settings, QSize(256, 256), 1));
}

QQuickMapboxGLRenderer::~QQuickMapboxGLRenderer()
{
}

QOpenGLFramebufferObject* QQuickMapboxGLRenderer::createFramebufferObject(const QSize &size)
{
    m_map->resize(size / m_pixelRatio, size);

    QOpenGLFramebufferObjectFormat format;
    format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);

    return new QOpenGLFramebufferObject(size, format);
}

void QQuickMapboxGLRenderer::render()
{
    m_map->render(framebufferObject());
}

void QQuickMapboxGLRenderer::synchronize(QQuickFramebufferObject *item)
{
    auto quickMap = qobject_cast<QQuickMapboxGL*>(item);
    if (!m_initialized) {
        QObject::connect(m_map.data(), &QMapboxGL::needsRendering, quickMap, &QQuickMapboxGL::update);
        QObject::connect(m_map.data(), SIGNAL(mapChanged(QMapbox::MapChange)), quickMap, SLOT(onMapChanged(QMapbox::MapChange)));
        QObject::connect(this, &QQuickMapboxGLRenderer::centerChanged, quickMap, &QQuickMapboxGL::setCenter);
        m_initialized = true;
    }

    if (auto window = quickMap->window()) {
        m_pixelRatio = window->devicePixelRatio();
    } else {
        m_pixelRatio = 1;
    }

    auto syncStatus = quickMap->m_syncState;
    quickMap->m_syncState = QQuickMapboxGL::NothingNeedsSync;

    if (syncStatus & QQuickMapboxGL::CenterNeedsSync || syncStatus & QQuickMapboxGL::ZoomNeedsSync) {
        const auto& center = quickMap->center();
        m_map->setCoordinateZoom({ center.latitude(), center.longitude() }, quickMap->zoomLevel());
    }

    if (syncStatus & QQuickMapboxGL::StyleNeedsSync && !quickMap->m_styleUrl.isEmpty()) {
        m_map->setStyleUrl(quickMap->m_styleUrl);
    }

    if (syncStatus & QQuickMapboxGL::PanNeedsSync) {
        m_map->moveBy(quickMap->m_pan);
        quickMap->m_pan = QPointF();
        emit centerChanged(QGeoCoordinate(m_map->latitude(), m_map->longitude()));
    }

    if (syncStatus & QQuickMapboxGL::BearingNeedsSync) {
        m_map->setBearing(quickMap->m_bearing);
    }

    if (syncStatus & QQuickMapboxGL::PitchNeedsSync) {
        m_map->setPitch(quickMap->m_pitch);
    }

    if (!quickMap->m_styleLoaded) {
        return;
    }

    for (const auto& change : quickMap->m_sourceChanges) {
        m_map->updateSource(change.value("id").toString(), change);
    }
    quickMap->m_sourceChanges.clear();

    for (const auto& change : quickMap->m_layerChanges) {
        m_map->addLayer(change);
    }
    quickMap->m_layerChanges.clear();

    for (const auto& change : quickMap->m_filterChanges) {
        m_map->setFilter(change.value("layer").toString(), change.value("filter"));
    }
    quickMap->m_filterChanges.clear();

    for (const auto& change : quickMap->m_imageChanges) {
        m_map->addImage(change.name, change.sprite);
    }
    quickMap->m_imageChanges.clear();

    for (const auto& change : quickMap->m_stylePropertyChanges) {
        if (change.type == QQuickMapboxGL::StyleProperty::Paint) {
            m_map->setPaintProperty(change.layer, change.property, change.value, change.klass);
        } else {
            m_map->setLayoutProperty(change.layer, change.property, change.value);
        }
    }
    quickMap->m_stylePropertyChanges.clear();
}