summaryrefslogtreecommitdiff
path: root/src/mbgl/algorithm/update_renderables.hpp
blob: d691a7def09a9fbeadae3c0af4902e8687f1a8b2 (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
#pragma once

#include <mbgl/tile/tile.hpp>
#include <mbgl/tile/tile_id.hpp>
#include <mbgl/tile/tile_necessity.hpp>
#include <mbgl/util/range.hpp>

#include <unordered_set>

#include <iostream>

namespace mbgl {
namespace algorithm {


template <typename GetTileFn,
          typename CreateTileFn,
          typename RetainTileFn,
          typename RenderTileFn>
bool findBestTile(GetTileFn getTile,
                       CreateTileFn createTile,
                       RetainTileFn retainTile,
                       RenderTileFn renderTile,
                       const Range<uint8_t>& zoomRange,
                       const uint8_t dataTileZoom,
                       const UnwrappedTileID& idealRenderTileID,
                       const OverscaledTileID& idealDataTileID,
                       const bool requireFullyRenderable,
                       std::unordered_set<UnwrappedTileID> checked) {

    auto tile = getTile(idealDataTileID);
    if (!tile) {
       // std::cout << "Create tile: " << idealDataTileID << std::endl;
        tile = createTile(idealDataTileID);
        assert(tile);
    }

    bool covered = true;
    std::vector<std::pair<UnwrappedTileID,decltype(tile)>> renderTiles;
    // if (source has the tile and bucket is loaded) {
    if (requireFullyRenderable ? tile->isFullyRenderable() : tile->isRenderable()) {
        retainTile(*tile, TileNecessity::Required);
        renderTiles.emplace_back(std::make_pair(idealRenderTileID, tile));
    } else {
        // We are now attempting to load child and parent tiles.
        bool parentHasTriedOptional = tile->hasTriedCache();
        bool parentIsLoaded = tile->isLoaded();

        // The tile isn't loaded yet, but retain it anyway because it's an ideal tile.
        retainTile(*tile, TileNecessity::Required); // TODO: Is it fine to retain this twice for both full and partial render?

        
        int32_t overscaledZ = dataTileZoom + 1;
        if (overscaledZ > zoomRange.max) {
            // We're looking for an overzoomed child tile.
            const auto childDataTileID = idealDataTileID.scaledTo(overscaledZ);
            tile = getTile(childDataTileID);
            if (tile && (requireFullyRenderable ? tile->isFullyRenderable() : tile->isRenderable())) {
                retainTile(*tile, TileNecessity::Optional);
                renderTiles.emplace_back(std::make_pair(idealRenderTileID, tile));
            } else {
                covered = false;
            }
        } else {
            // Check all four actual child tiles.
            for (const auto& childTileID : idealDataTileID.canonical.children()) {
                const OverscaledTileID childDataTileID(overscaledZ, idealRenderTileID.wrap, childTileID);
                tile = getTile(childDataTileID);
                if (tile && (requireFullyRenderable ? tile->isFullyRenderable() : tile->isRenderable())) {
                    retainTile(*tile, TileNecessity::Optional);
                    renderTiles.emplace_back(std::make_pair(childDataTileID.toUnwrapped(), tile));
                } else {
                    // At least one child tile doesn't exist, so we are going to look for
                    // parents as well.
                    covered = false;
                }
            }
        }

        if (!covered) {
            // We couldn't find child tiles that entirely cover the ideal tile.
            for (overscaledZ = dataTileZoom - 1; overscaledZ >= zoomRange.min; --overscaledZ) {
                const auto parentDataTileID = idealDataTileID.scaledTo(overscaledZ);
                const auto parentRenderTileID = parentDataTileID.toUnwrapped();

                if (checked.find(parentRenderTileID) != checked.end()) {
                    // Break parent tile ascent, this route has been checked by another child
                    // tile before.
                    break;
                } else {
                    checked.emplace(parentRenderTileID);
                }

                tile = getTile(parentDataTileID);
                // Don't do cache lookups for parents while we're in the "fully renderable" pass:
                // Since the cache tiles aren't currently displaying, we don't have to worry about symbol flicker
                // And we'd rather get a partially rendered tile closer to our ideal zoom level
                if (!tile && !requireFullyRenderable && (parentHasTriedOptional || parentIsLoaded)) {
                    tile = createTile(parentDataTileID);
                }

                if (tile) {
                    if (!parentIsLoaded) {
                        // We haven't completed loading the child, so we only do an optional
                        // (cache) request in an attempt to quickly load data that we can show.
                        retainTile(*tile, TileNecessity::Optional);
                    } else {
                        // Now that we've checked the child and know for sure that we can't load
                        // it, we attempt to load the parent from the network.
                        retainTile(*tile, TileNecessity::Required);
                    }

                    // Save the current values, since they're the parent of the next iteration
                    // of the parent tile ascent loop.
                    parentHasTriedOptional = tile->hasTriedCache();
                    parentIsLoaded = tile->isLoaded();

                    if ((requireFullyRenderable ? tile->isFullyRenderable() : tile->isRenderable())) {
                        //std::cout << "Rendering parent zoom change: " << parentRenderTileID.canonical.z - idealRenderTileID.canonical.z << std::endl;
                        renderTiles.emplace_back(std::make_pair(parentRenderTileID, tile));
                        covered = true;
                        // Break parent tile ascent, since we found one.
                        break;
                    }
                }
            }
        }
    }
    if (covered || !requireFullyRenderable) {
        // Only add tiles to render tree if we've covered the ideal tile OR we're on our second pass
        for (auto tilePair : renderTiles) {
          //  std::cout << "Rendering tile: " << tilePair.first << std::endl;
            renderTile(tilePair.first, *(tilePair.second));
        }
    } else {
       // std::cout << "No tiles added to render tree" << std::endl;
    }
    return covered;
}

template <typename GetTileFn,
          typename CreateTileFn,
          typename RetainTileFn,
          typename RenderTileFn,
          typename IdealTileIDs>
void updateRenderables(GetTileFn getTile,
                       CreateTileFn createTile,
                       RetainTileFn retainTile,
                       RenderTileFn renderTile,
                       const IdealTileIDs& idealTileIDs,
                       const Range<uint8_t>& zoomRange,
                       const uint8_t dataTileZoom) {
    std::unordered_set<UnwrappedTileID> checkedPartial;
    std::unordered_set<UnwrappedTileID> checkedFull;

    //std::cout << "Begin updateRenderables" << std::endl;

    // for (all in the set of ideal tiles of the source) {
    for (const auto& idealRenderTileID : idealTileIDs) {
        assert(idealRenderTileID.canonical.z >= zoomRange.min);
        assert(idealRenderTileID.canonical.z <= zoomRange.max);
        assert(dataTileZoom >= idealRenderTileID.canonical.z);

        const OverscaledTileID idealDataTileID(dataTileZoom, idealRenderTileID.wrap, idealRenderTileID.canonical);
        
        //std::cout << "Ideal tile ID: " << idealDataTileID << std::endl;

        // TODO: Two-pass algorithm isn't necessary for tiles that don't have two stage loading (e.g. raster tiles)
        // TODO: More efficient to write algorithm as single pass?
       if (!findBestTile(getTile, createTile, retainTile, renderTile, zoomRange, dataTileZoom, idealRenderTileID, idealDataTileID, true, checkedFull)) {
            findBestTile(getTile, createTile, retainTile, renderTile, zoomRange, dataTileZoom, idealRenderTileID, idealDataTileID, false, checkedPartial);
       }
    }
}

} // namespace algorithm
} // namespace mbgl