summaryrefslogtreecommitdiff
path: root/platform/android/src/android_renderer_frontend.cpp
blob: 11bb9ab3fc89f3bf70d59924018715940fcd1b0b (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
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
#include "android_renderer_frontend.hpp"

#include <mbgl/actor/scheduler.hpp>
#include <mbgl/renderer/backend_scope.hpp>
#include <mbgl/renderer/renderer.hpp>
#include <mbgl/renderer/renderer_observer.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/run_loop.hpp>

#include "android_renderer_backend.hpp"

namespace mbgl {
namespace android {

// Forwards RendererObserver signals to the given
// Delegate RendererObserver on the given RunLoop
class ForwardingRendererObserver : public RendererObserver {
public:
    ForwardingRendererObserver(util::RunLoop& mapRunLoop_, RendererObserver& delegate_)
            : mapRunLoop(mapRunLoop_)
            , delegate(delegate_) {}

    void onInvalidate() override {
        mapRunLoop.invoke([&]() {
            delegate.onInvalidate();
        });
    }

    void onResourceError(std::exception_ptr err) override {
        mapRunLoop.invoke([&, err]() {
            delegate.onResourceError(err);
        });
    }

    void onWillStartRenderingMap() override {
        mapRunLoop.invoke([&]() {
            delegate.onWillStartRenderingMap();
        });
    }

    void onWillStartRenderingFrame() override {
        mapRunLoop.invoke([&]() {
            delegate.onWillStartRenderingFrame();
        });
    }

    void onDidFinishRenderingFrame(RenderMode mode, bool repaintNeeded) override {
        mapRunLoop.invoke([&, mode, repaintNeeded]() {
            delegate.onDidFinishRenderingFrame(mode, repaintNeeded);
        });
    }

    void onDidFinishRenderingMap() override {
        mapRunLoop.invoke([&]() {
            delegate.onDidFinishRenderingMap();
        });
    }

private:
    util::RunLoop& mapRunLoop;
    RendererObserver& delegate;
};

AndroidRendererFrontend::AndroidRendererFrontend(float pixelRatio_,
                                                 FileSource& fileSource_,
                                                 Scheduler& scheduler_,
                                                 std::string programCacheDir_,
                                                 InvalidateCallback invalidate_)
        : pixelRatio(pixelRatio_)
        , fileSource(fileSource_)
        , scheduler(scheduler_)
        , programCacheDir(programCacheDir_)
        , asyncInvalidate([=, invalidate=std::move(invalidate_)]() {
            invalidate();
        })
        , mapRunLoop(util::RunLoop::Get()) {
}

AndroidRendererFrontend::~AndroidRendererFrontend() = default;

void AndroidRendererFrontend::reset() {
    assert(renderer);
    renderer.reset();
}

void AndroidRendererFrontend::initialise() {
    // Lock as the initialization can come from the main thread or the GL thread first
    std::lock_guard<std::mutex> lock(intitialisationMutex);

    if (backend) {
        // The android system will have already destroyed the underlying
        // GL resources and an attempt to clean them up will fail
        backend->abandonContext();
    }

    // Destroy in reverse order
    renderer.reset();
    backend.reset();

    // Re-create
    backend = std::make_unique<AndroidRendererBackend>();
    renderer = std::make_unique<util::Thread<Renderer>>(
            "Orchestration Thread",
            *backend,
            pixelRatio,
            fileSource,
            scheduler,
            GLContextMode::Unique,
            programCacheDir
    );

    // Set the observer on the new Renderer implementation
    if (rendererObserver) {
        renderer->actor().invoke(&Renderer::setObserver, rendererObserver.get());
    }
}

void AndroidRendererFrontend::setObserver(RendererObserver& observer) {
    assert (util::RunLoop::Get());

    // Lock as the initialization can come from the main thread or the GL thread first
    std::lock_guard<std::mutex> lock(intitialisationMutex);
    
    rendererObserver = std::make_unique<ForwardingRendererObserver>(*mapRunLoop, observer);

    // Set the new observer on the Renderer implementation
    if (renderer) {
        renderer->actor().invoke(&Renderer::setObserver, rendererObserver.get());
    }
}

void AndroidRendererFrontend::update(std::shared_ptr<UpdateParameters> params) {
    {
        // Lock on the parameters
        std::lock_guard<std::mutex> lock(updateMutex);
        updateParameters = std::move(params);
    }
    asyncInvalidate.send();
}

// Called on OpenGL thread
void AndroidRendererFrontend::render() {
    assert (renderer);

    std::shared_ptr<UpdateParameters> params;
    {
        // Lock on the parameters
        std::unique_lock<std::mutex> lock(updateMutex);
        if (!updateParameters) return;

        // Hold on to the update parameters during render
        params = updateParameters;
    }

    // Activate the backend
    BackendScope backendGuard { *backend };

    // Block the orchestration thread during render
    util::BlockingThreadGuard<Renderer> rendererGuard { *renderer };

    // Ensure that the "current" scheduler on the render thread is
    // actually the scheduler from the orchestration  thread
    Scheduler::SetCurrent(renderer.get());

    if (framebufferSizeChanged) {
        backend->updateViewPort();
        framebufferSizeChanged = false;
    }

    rendererGuard.object().render(*params);
}

void AndroidRendererFrontend::onLowMemory() {
    assert (renderer);
    renderer->actor().invoke(&Renderer::onLowMemory);
}

std::vector<Feature> AndroidRendererFrontend::querySourceFeatures(const std::string& sourceID,
                                                                  const SourceQueryOptions& options) const {
    assert (renderer);
    // Waits for the result from the orchestration thread and returns
    return renderer->actor().ask(&Renderer::querySourceFeatures, sourceID, options).get();
}

std::vector<Feature> AndroidRendererFrontend::queryRenderedFeatures(const ScreenBox& box,
                                                                    const RenderedQueryOptions& options) const {
    assert (renderer);

    // Select the right overloaded method
    std::vector<Feature> (Renderer::*fn)(const ScreenBox&, const RenderedQueryOptions&) const = &Renderer::queryRenderedFeatures;

    // Waits for the result from the orchestration thread and returns
    return renderer->actor().ask(fn, box, options).get();
}

std::vector<Feature> AndroidRendererFrontend::queryRenderedFeatures(const ScreenCoordinate& point,
                                                                    const RenderedQueryOptions& options) const {
    assert (renderer);

    // Select the right overloaded method
    std::vector<Feature> (Renderer::*fn)(const ScreenCoordinate&, const RenderedQueryOptions&) const = &Renderer::queryRenderedFeatures;

    // Waits for the result from the orchestration thread and returns
    return renderer->actor().ask(fn, point, options).get();
}

AnnotationIDs AndroidRendererFrontend::queryPointAnnotations(const ScreenBox& box) const {
    assert (renderer);

    // Waits for the result from the orchestration thread and returns
    return renderer->actor().ask(&Renderer::queryPointAnnotations, box).get();
}

void AndroidRendererFrontend::requestSnapshot(SnapshotCallback callback_) {
    snapshotCallback = std::make_unique<SnapshotCallback>([callback=std::move(callback_), runloop=util::RunLoop::Get()](PremultipliedImage image){
        runloop->invoke([&]() {
            callback(std::move(image));
        });
    });
}

void AndroidRendererFrontend::resizeFramebuffer(int width, int height) {
    util::BlockingThreadGuard<Renderer> guard { *renderer };
    backend->resizeFramebuffer(width, height);
    framebufferSizeChanged = true;
    // TODO: thread safe?
    asyncInvalidate.send();
}

} // namespace android
} // namespace mbgl