summaryrefslogtreecommitdiff
path: root/src/mbgl/util/worker.cpp
blob: ec9d02573d0e30403af4d94af991dd8c302bb03e (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
#include <mbgl/util/worker.hpp>
#include <mbgl/util/work_task.hpp>
#include <mbgl/util/work_request.hpp>
#include <mbgl/platform/platform.hpp>
#include <mbgl/renderer/raster_bucket.hpp>
#include <mbgl/tile/geometry_tile.hpp>
#include <mbgl/style/style_layer.hpp>

#include <cassert>
#include <future>

namespace mbgl {

class Worker::Impl {
public:
    Impl() = default;

    void parseRasterTile(std::unique_ptr<RasterBucket> bucket,
                         std::shared_ptr<const std::string> data,
                         std::function<void(RasterTileParseResult)> callback) {
        try {
            bucket->setImage(decodeImage(*data));
            // Destruct the shared pointer before calling the callback.
            data.reset();
            callback(RasterTileParseResult(std::move(bucket)));
        } catch (...) {
            callback(std::current_exception());
        }
    }

    void parseGeometryTile(TileWorker* worker,
                           std::vector<std::unique_ptr<StyleLayer>> layers,
                           std::unique_ptr<GeometryTile> tile,
                           PlacementConfig config,
                           std::function<void(TileParseResult)> callback) {
        try {
            callback(worker->parseAllLayers(std::move(layers), std::move(tile), config));
        } catch (...) {
            callback(std::current_exception());
        }
    }

    void parsePendingGeometryTileLayers(TileWorker* worker,
                                        PlacementConfig config,
                                        std::function<void(TileParseResult)> callback) {
        try {
            callback(worker->parsePendingLayers(config));
        } catch (...) {
            callback(std::current_exception());
        }
    }

    void redoPlacement(TileWorker* worker,
                       const std::unordered_map<std::string, std::unique_ptr<Bucket>>* buckets,
                       PlacementConfig config,
                       std::function<void()> callback) {
        worker->redoPlacement(buckets, config);
        callback();
    }
};

Worker::Worker(std::size_t count) {
    util::ThreadContext context = { "Worker", util::ThreadType::Worker, util::ThreadPriority::Low };
    for (std::size_t i = 0; i < count; i++) {
        threads.emplace_back(std::make_unique<util::Thread<Impl>>(context));
    }
}

Worker::~Worker() = default;

std::unique_ptr<AsyncRequest>
Worker::parseRasterTile(std::unique_ptr<RasterBucket> bucket,
                        const std::shared_ptr<const std::string> data,
                        std::function<void(RasterTileParseResult)> callback) {
    current = (current + 1) % threads.size();
    return threads[current]->invokeWithCallback(&Worker::Impl::parseRasterTile, callback, bucket,
                                                data);
}

std::unique_ptr<AsyncRequest>
Worker::parseGeometryTile(TileWorker& worker,
                          std::vector<std::unique_ptr<StyleLayer>> layers,
                          std::unique_ptr<GeometryTile> tile,
                          PlacementConfig config,
                          std::function<void(TileParseResult)> callback) {
    current = (current + 1) % threads.size();
    return threads[current]->invokeWithCallback(&Worker::Impl::parseGeometryTile, callback, &worker,
                                                std::move(layers), std::move(tile), config);
}

std::unique_ptr<AsyncRequest>
Worker::parsePendingGeometryTileLayers(TileWorker& worker,
                                       PlacementConfig config,
                                       std::function<void(TileParseResult)> callback) {
    current = (current + 1) % threads.size();
    return threads[current]->invokeWithCallback(&Worker::Impl::parsePendingGeometryTileLayers,
                                                callback, &worker, config);
}

std::unique_ptr<AsyncRequest>
Worker::redoPlacement(TileWorker& worker,
                      const std::unordered_map<std::string, std::unique_ptr<Bucket>>& buckets,
                      PlacementConfig config,
                      std::function<void()> callback) {
    current = (current + 1) % threads.size();
    return threads[current]->invokeWithCallback(&Worker::Impl::redoPlacement, callback, &worker,
                                                &buckets, config);
}

} // end namespace mbgl