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
|
#include "qmapboxgl_map_renderer.hpp"
#include "qmapboxgl_scheduler.hpp"
#include <mbgl/gfx/backend_scope.hpp>
#include <QThreadStorage>
#include <QtGlobal>
static bool needsToForceScheduler() {
static QThreadStorage<bool> force;
if (!force.hasLocalData()) {
force.setLocalData(mbgl::Scheduler::GetCurrent() == nullptr);
}
return force.localData();
};
static auto *getScheduler() {
static QThreadStorage<std::shared_ptr<QMapboxGLScheduler>> scheduler;
if (!scheduler.hasLocalData()) {
scheduler.setLocalData(std::make_shared<QMapboxGLScheduler>());
}
return scheduler.localData().get();
};
QMapboxGLMapRenderer::QMapboxGLMapRenderer(qreal pixelRatio, QMapboxGLSettings::GLContextMode mode, const QString &localFontFamily)
: m_backend(static_cast<mbgl::gfx::ContextMode>(mode)),
m_renderer(std::make_unique<mbgl::Renderer>(m_backend, pixelRatio,
localFontFamily.isEmpty() ? mbgl::nullopt : mbgl::optional<std::string> { localFontFamily.toStdString() }))
, m_forceScheduler(needsToForceScheduler())
{
// If we don't have a Scheduler on this thread, which
// is usually the case for render threads, use a shared
// dummy scheduler that needs to be explicitly forced to
// process events.
if (m_forceScheduler) {
auto scheduler = getScheduler();
if (mbgl::Scheduler::GetCurrent() == nullptr) {
mbgl::Scheduler::SetCurrent(scheduler);
}
connect(scheduler, SIGNAL(needsProcessing()), this, SIGNAL(needsRendering()));
}
}
QMapboxGLMapRenderer::~QMapboxGLMapRenderer()
{
MBGL_VERIFY_THREAD(tid);
}
void QMapboxGLMapRenderer::updateParameters(std::shared_ptr<mbgl::UpdateParameters> newParameters)
{
std::lock_guard<std::mutex> lock(m_updateMutex);
m_updateParameters = std::move(newParameters);
}
void QMapboxGLMapRenderer::updateFramebuffer(quint32 fbo, const mbgl::Size &size)
{
MBGL_VERIFY_THREAD(tid);
m_backend.updateFramebuffer(fbo, size);
}
void QMapboxGLMapRenderer::render()
{
MBGL_VERIFY_THREAD(tid);
std::shared_ptr<mbgl::UpdateParameters> params;
{
// Lock on the parameters
std::lock_guard<std::mutex> lock(m_updateMutex);
// UpdateParameters should always be available when rendering.
assert(m_updateParameters);
// Hold on to the update parameters during render
params = m_updateParameters;
}
// The OpenGL implementation automatically enables the OpenGL context for us.
mbgl::gfx::BackendScope scope(m_backend, mbgl::gfx::BackendScope::ScopeType::Implicit);
m_renderer->render(params);
if (m_forceScheduler) {
getScheduler()->processEvents();
}
}
void QMapboxGLMapRenderer::setObserver(std::shared_ptr<mbgl::RendererObserver> observer)
{
m_renderer->setObserver(observer.get());
}
|