summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/group_by_layout.cpp
blob: 41a895902c2e83aec8236f21215c137092475fd7 (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
#include <mbgl/renderer/group_by_layout.hpp>
#include <mbgl/renderer/render_layer.hpp>
#include <mbgl/style/layer_impl.hpp>
#include <mbgl/style/conversion/stringify.hpp>
#include <mbgl/util/rapidjson.hpp>

#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>

#include <unordered_map>

namespace mbgl {

std::string layoutKey(const RenderLayer& layer) {
    using namespace style::conversion;

    rapidjson::StringBuffer s;
    rapidjson::Writer<rapidjson::StringBuffer> writer(s);

    writer.StartArray();
    writer.Uint64(reinterpret_cast<uint64_t>(layer.baseImpl->getTypeInfo()));
    writer.String(layer.baseImpl->source);
    writer.String(layer.baseImpl->sourceLayer);
    writer.Double(layer.baseImpl->minZoom);
    writer.Double(layer.baseImpl->maxZoom);
    writer.Uint(static_cast<uint32_t>(layer.baseImpl->visibility));
    stringify(writer, layer.baseImpl->filter);
    layer.baseImpl->stringifyLayout(writer);
    writer.EndArray();

    return s.GetString();
}

std::vector<std::vector<const RenderLayer*>> groupByLayout(const std::vector<std::unique_ptr<RenderLayer>>& layers) {
    std::unordered_map<std::string, std::vector<const RenderLayer*>> map;
    for (auto& layer : layers) {
        map[layoutKey(*layer)].push_back(layer.get());
    }

    std::vector<std::vector<const RenderLayer*>> result;
    for (auto& pair : map) {
        result.push_back(pair.second);
    }

    return result;
}

} // namespace mbgl