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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
#include <mbgl/map/map_context.hpp>
#include <mbgl/map/map_data.hpp>
#include <mbgl/map/view.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/renderer/painter.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/sprite/sprite_atlas.hpp>
#include <mbgl/sprite/sprite_store.hpp>
#include <mbgl/util/gl_object_store.hpp>
#include <mbgl/util/worker.hpp>
#include <mbgl/util/texture_pool.hpp>
#include <mbgl/util/exception.hpp>
#include <mbgl/util/string.hpp>
#include <algorithm>
namespace mbgl {
MapContext::MapContext(View& view_, FileSource& fileSource, MapMode mode_, GLContextMode contextMode_, const float pixelRatio_)
: view(view_),
dataPtr(std::make_unique<MapData>(mode_, contextMode_, pixelRatio_)),
data(*dataPtr),
asyncUpdate([this] { update(); }),
asyncInvalidate([&view_] { view_.invalidate(); }),
texturePool(std::make_unique<TexturePool>()) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
util::ThreadContext::setFileSource(&fileSource);
util::ThreadContext::setGLObjectStore(&glObjectStore);
asyncUpdate.unref();
asyncInvalidate.unref();
view.activate();
}
MapContext::~MapContext() {
// Make sure we call cleanup() before deleting this object.
assert(!style);
}
void MapContext::cleanup() {
view.notify();
styleRequest = nullptr;
// Explicit resets currently necessary because these abandon resources that need to be
// cleaned up by glObjectStore.performCleanup();
style.reset();
painter.reset();
texturePool.reset();
dataPtr.reset();
glObjectStore.performCleanup();
view.deactivate();
}
void MapContext::pause() {
MBGL_CHECK_ERROR(glFinish());
view.deactivate();
std::unique_lock<std::mutex> lockPause(data.mutexPause);
data.paused = true;
data.condPause.notify_all();
data.condPause.wait(lockPause, [&]{ return !data.paused; });
view.activate();
asyncInvalidate.send();
}
void MapContext::triggerUpdate(const TransformState& state, const Update flags) {
transformState = state;
updateFlags |= flags;
asyncUpdate.send();
}
void MapContext::setStyleURL(const std::string& url) {
if (styleURL == url) {
return;
}
styleRequest = nullptr;
styleURL = url;
styleJSON.clear();
style = std::make_unique<Style>(data);
const size_t pos = styleURL.rfind('/');
std::string base = "";
if (pos != std::string::npos) {
base = styleURL.substr(0, pos + 1);
}
FileSource* fs = util::ThreadContext::getFileSource();
styleRequest = fs->request({ Resource::Kind::Style, styleURL }, [this, base](Response res) {
if (res.stale) {
// Only handle fresh responses.
return;
}
styleRequest = nullptr;
if (res.error) {
if (res.error->reason == Response::Error::Reason::NotFound && styleURL.find("mapbox://") == 0) {
Log::Error(Event::Setup, "style %s could not be found or is an incompatible legacy map or style", styleURL.c_str());
} else {
Log::Error(Event::Setup, "loading style failed: %s", res.error->message.c_str());
data.loading = false;
}
} else {
loadStyleJSON(*res.data, base);
}
});
}
void MapContext::setStyleJSON(const std::string& json, const std::string& base) {
if (styleJSON == json) {
return;
}
styleURL.clear();
styleJSON = json;
style = std::make_unique<Style>(data);
loadStyleJSON(json, base);
}
void MapContext::loadStyleJSON(const std::string& json, const std::string& base) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
style->setJSON(json, base);
style->setObserver(this);
// force style cascade, causing all pending transitions to complete.
style->cascade();
// set loading here so we don't get a false loaded event as soon as map is
// created but before a style is loaded
data.loading = true;
updateFlags |= Update::DefaultTransition | Update::Classes | Update::Zoom | Update::Annotations;
asyncUpdate.send();
}
void MapContext::update() {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
if (!style) {
updateFlags = Update::Nothing;
}
if (updateFlags == Update::Nothing || (data.mode == MapMode::Still && !callback)) {
return;
}
data.setAnimationTime(Clock::now());
if (style->loaded && updateFlags & Update::Annotations) {
data.getAnnotationManager()->updateStyle(*style);
updateFlags |= Update::Classes;
}
if (updateFlags & Update::Classes) {
style->cascade();
}
if (updateFlags & Update::Classes || updateFlags & Update::Zoom) {
style->recalculate(transformState.getNormalizedZoom());
}
style->update(transformState, *texturePool);
if (data.mode == MapMode::Continuous) {
asyncInvalidate.send();
} else if (callback && style->isLoaded()) {
renderSync(transformState, frameData);
}
updateFlags = Update::Nothing;
}
void MapContext::renderStill(const TransformState& state, const FrameData& frame, Map::StillImageCallback fn) {
if (!fn) {
Log::Error(Event::General, "StillImageCallback not set");
return;
}
if (data.mode != MapMode::Still) {
fn(std::make_exception_ptr(util::MisuseException("Map is not in still image render mode")), {});
return;
}
if (callback) {
fn(std::make_exception_ptr(util::MisuseException("Map is currently rendering an image")), {});
return;
}
if (!style) {
fn(std::make_exception_ptr(util::MisuseException("Map doesn't have a style")), {});
return;
}
if (style->getLastError()) {
fn(style->getLastError(), {});
return;
}
callback = fn;
transformState = state;
frameData = frame;
updateFlags |= Update::RenderStill;
asyncUpdate.send();
}
bool MapContext::renderSync(const TransformState& state, const FrameData& frame) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
// Style was not loaded yet.
if (!style) {
return false;
}
view.beforeRender();
transformState = state;
// Cleanup OpenGL objects that we abandoned since the last render call.
glObjectStore.performCleanup();
if (!painter) painter = std::make_unique<Painter>(data, transformState);
painter->render(*style, frame, data.getAnnotationManager()->getSpriteAtlas());
if (data.mode == MapMode::Still) {
callback(nullptr, std::move(view.readStillImage()));
callback = nullptr;
}
view.afterRender();
if (style->hasTransitions()) {
updateFlags |= Update::Classes;
asyncUpdate.send();
} else if (painter->needsAnimation()) {
updateFlags |= Update::Repaint;
asyncUpdate.send();
}
return isLoaded();
}
bool MapContext::isLoaded() const {
return style->isLoaded();
}
double MapContext::getTopOffsetPixelsForAnnotationSymbol(const std::string& symbol) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
return data.getAnnotationManager()->getTopOffsetPixelsForAnnotationSymbol(symbol);
}
void MapContext::setSourceTileCacheSize(size_t size) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
if (size != sourceCacheSize) {
sourceCacheSize = size;
if (!style) return;
style->setSourceTileCacheSize(size);
asyncInvalidate.send();
}
}
void MapContext::onLowMemory() {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
if (!style) return;
style->onLowMemory();
asyncInvalidate.send();
}
void MapContext::setSprite(const std::string& name, std::shared_ptr<const SpriteImage> sprite) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
data.getAnnotationManager()->setSprite(name, sprite);
}
void MapContext::onTileDataChanged() {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
updateFlags |= Update::Repaint;
asyncUpdate.send();
}
void MapContext::onResourceLoadingFailed(std::exception_ptr error) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
if (data.mode == MapMode::Still && callback) {
callback(error, {});
callback = nullptr;
}
}
void MapContext::dumpDebugLogs() const {
Log::Info(Event::General, "--------------------------------------------------------------------------------");
Log::Info(Event::General, "MapContext::styleURL: %s", styleURL.c_str());
if (style) {
style->dumpDebugLogs();
} else {
Log::Info(Event::General, "no style loaded");
}
Log::Info(Event::General, "--------------------------------------------------------------------------------");
}
}
|