1 #pragma once
2 
3 #include <mbgl/util/noncopyable.hpp>
4 #include <mbgl/util/chrono.hpp>
5 #include <mbgl/util/optional.hpp>
6 #include <mbgl/util/feature.hpp>
7 #include <mbgl/util/tile_coordinate.hpp>
8 #include <mbgl/tile/tile_id.hpp>
9 #include <mbgl/tile/tile_necessity.hpp>
10 #include <mbgl/renderer/tile_mask.hpp>
11 #include <mbgl/renderer/bucket.hpp>
12 #include <mbgl/tile/geometry_tile_data.hpp>
13 #include <mbgl/storage/resource.hpp>
14 #include <mbgl/style/layer_impl.hpp>
15 
16 #include <string>
17 #include <memory>
18 #include <functional>
19 #include <unordered_map>
20 
21 namespace mbgl {
22 
23 class DebugBucket;
24 class TransformState;
25 class TileObserver;
26 class RenderLayer;
27 class RenderedQueryOptions;
28 class SourceQueryOptions;
29 
30 class CollisionIndex;
31 
32 namespace gl {
33 class Context;
34 } // namespace gl
35 
36 class Tile : private util::noncopyable {
37 public:
38     Tile(OverscaledTileID);
39     virtual ~Tile();
40 
41     void setObserver(TileObserver* observer);
42 
setNecessity(TileNecessity)43     virtual void setNecessity(TileNecessity) {}
44 
45     // Mark this tile as no longer needed and cancel any pending work.
46     virtual void cancel();
47 
48     virtual void upload(gl::Context&) = 0;
49     virtual Bucket* getBucket(const style::Layer::Impl&) const = 0;
50 
51     template <class T>
getBucket(const style::Layer::Impl & layer) const52     T* getBucket(const style::Layer::Impl& layer) const {
53         Bucket* bucket = getBucket(layer);
54         return bucket ? bucket->as<T>() : nullptr;
55     }
56 
setShowCollisionBoxes(const bool)57     virtual void setShowCollisionBoxes(const bool) {}
setLayers(const std::vector<Immutable<style::Layer::Impl>> &)58     virtual void setLayers(const std::vector<Immutable<style::Layer::Impl>>&) {}
setMask(TileMask &&)59     virtual void setMask(TileMask&&) {}
60 
61     virtual void queryRenderedFeatures(
62             std::unordered_map<std::string, std::vector<Feature>>& result,
63             const GeometryCoordinates& queryGeometry,
64             const TransformState&,
65             const std::vector<const RenderLayer*>&,
66             const RenderedQueryOptions& options,
67             const mat4& projMatrix);
68 
69     virtual void querySourceFeatures(
70             std::vector<Feature>& result,
71             const SourceQueryOptions&);
72 
73     virtual float getQueryPadding(const std::vector<const RenderLayer*>&);
74 
75     void setTriedCache();
76 
77     // Returns true when the tile source has received a first response, regardless of whether a load
78     // error occurred or actual data was loaded.
hasTriedCache() const79     bool hasTriedCache() const {
80         return triedOptional;
81     }
82 
83     // Tile data considered "Renderable" can be used for rendering. Data in
84     // partial state is still waiting for network resources but can also
85     // be rendered, although layers will be missing.
isRenderable() const86     bool isRenderable() const {
87         return renderable;
88     }
89 
90     // A tile is "Loaded" when we have received a response from a FileSource, and have attempted to
91     // parse the tile (if applicable). Tile implementations should set this to true when a load
92     // error occurred, or after the tile was parsed successfully.
isLoaded() const93     bool isLoaded() const {
94         return loaded;
95     }
96 
97     // "Completion" of a tile means that we have attempted to load it, and parsed it completely,
98     // i.e. no parsing or placement operations are pending for that tile.
99     // Completeness doesn't mean that the tile can be rendered, but merely that we have exhausted
100     // all options to get this tile to a renderable state. Some tiles may not be renderable, but
101     // complete, e.g. when a raster tile couldn't be loaded, or parsing failed.
isComplete() const102     bool isComplete() const {
103         return loaded && !pending;
104     }
105 
106     // "holdForFade" is used to keep tiles in the render tree after they're no longer
107     // ideal tiles in order to allow symbols to fade out
holdForFade() const108     virtual bool holdForFade() const {
109         return false;
110     }
111     // Set whenever this tile is used as an ideal tile
markRenderedIdeal()112     virtual void markRenderedIdeal() {}
113     // Set when the tile is removed from the ideal render set but may still be held for fading
markRenderedPreviously()114     virtual void markRenderedPreviously() {}
115     // Placement operation performed while this tile is fading
116     // We hold onto a tile for two placements: fading starts with the first placement
117     // and will have time to finish by the second placement.
performedFadePlacement()118     virtual void performedFadePlacement() {}
119 
120     void dumpDebugLogs() const;
121 
122     OverscaledTileID id;
123     optional<Timestamp> modified;
124     optional<Timestamp> expires;
125 
126     // Contains the tile ID string for painting debug information.
127     std::unique_ptr<DebugBucket> debugBucket;
128 
129 protected:
130     bool triedOptional = false;
131     bool renderable = false;
132     bool pending = false;
133     bool loaded = false;
134 
135     TileObserver* observer = nullptr;
136 };
137 
138 } // namespace mbgl
139