This repository has been archived by the owner on Nov 11, 2023. It is now read-only.
forked from maplibre/maplibre-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLNRendererFrontend.h
73 lines (63 loc) · 2.35 KB
/
MLNRendererFrontend.h
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
#include <mbgl/gfx/backend_scope.hpp>
#include <mbgl/renderer/renderer.hpp>
#include <mbgl/renderer/renderer_frontend.hpp>
#include <mbgl/gfx/renderer_backend.hpp>
#include <mbgl/util/async_task.hpp>
/**
The RenderFrontend is passed to the Map to facilitate rendering in a platform
dependent way.
*/
class MLNRenderFrontend : public mbgl::RendererFrontend
{
public:
MLNRenderFrontend(std::unique_ptr<mbgl::Renderer> renderer_, MLNMapView* nativeView_, mbgl::gfx::RendererBackend& mbglBackend_, bool async = false)
: renderer(std::move(renderer_))
, nativeView(nativeView_)
, mbglBackend(mbglBackend_) {
if (async) {
asyncInvalidate.emplace([&]() {
[nativeView setNeedsRerender];
});
}
}
void reset() override {
if (renderer) {
renderer.reset();
}
}
void update(std::shared_ptr<mbgl::UpdateParameters> updateParameters_) override {
updateParameters = std::move(updateParameters_);
if (asyncInvalidate) {
asyncInvalidate->send();
} else {
[nativeView setNeedsRerender];
}
}
void setObserver(mbgl::RendererObserver& observer) override {
if (!renderer) return;
renderer->setObserver(&observer);
}
void render() {
if (!renderer || !updateParameters) return;
mbgl::gfx::BackendScope guard { mbglBackend, mbgl::gfx::BackendScope::ScopeType::Implicit };
// onStyleImageMissing might be called during a render. The user implemented method
// could trigger a call to MLNRenderFrontend#update which overwrites `updateParameters`.
// Copy the shared pointer here so that the parameters aren't destroyed while `render(...)` is
// still using them.
auto updateParameters_ = updateParameters;
renderer->render(updateParameters_);
}
mbgl::Renderer* getRenderer() {
return renderer.get();
}
void reduceMemoryUse() {
if (!renderer) return;
renderer->reduceMemoryUse();
}
private:
std::unique_ptr<mbgl::Renderer> renderer;
__weak MLNMapView *nativeView = nullptr;
mbgl::gfx::RendererBackend& mbglBackend;
std::shared_ptr<mbgl::UpdateParameters> updateParameters;
std::optional<mbgl::util::AsyncTask> asyncInvalidate;
};