1 #pragma once
2 
3 #include <mbgl/style/layer.hpp>
4 
5 #include <array>
6 
7 namespace mbgl {
8 namespace style {
9 
10 /**
11  * Parameters that define the current camera position for a `CustomLayerHost::render()` function.
12  */
13 struct CustomLayerRenderParameters {
14     double width;
15     double height;
16     double latitude;
17     double longitude;
18     double zoom;
19     double bearing;
20     double pitch;
21     double fieldOfView;
22     std::array<double, 16> projectionMatrix;
23 };
24 
25 class CustomLayerHost {
26 public:
27     virtual ~CustomLayerHost() = default;
28     /**
29      * Initialize any GL state needed by the custom layer. This method is called once, from the
30      * main thread, at a point when the GL context is active but before rendering for the first
31      * time.
32      *
33      * Resources that are acquired in this method must be released in the `deinitialize` function.
34      */
35     virtual void initialize() = 0;
36 
37     /**
38      * Render the layer. This method is called once per frame. The implementation should not make
39      * any assumptions about the GL state (other than that the correct context is active). It may
40      * make changes to the state, and is not required to reset values such as the depth mask, stencil
41      * mask, and corresponding test flags to their original values.
42      * Make sure that you are drawing your fragments with a z value of 1 to take advantage of the
43      * opaque fragment culling in case there are opaque layers above your custom layer.
44      */
45     virtual void render(const CustomLayerRenderParameters&) = 0;
46 
47     /**
48      * Called when the system has destroyed the underlying GL context. The
49      * `deinitialize` function will not be called in this case, however
50      * `initialize` will be called instead to prepare for a new render.
51      *
52      */
53     virtual void contextLost() = 0;
54 
55     /**
56      * Destroy any GL state needed by the custom layer, and deallocate context, if necessary. This
57      * method is called once, from the main thread, at a point when the GL context is active.
58      *
59      * Note that it may be called even when the `initialize` function has not been called.
60      */
61     virtual void deinitialize() = 0;
62 };
63 
64 class CustomLayer : public Layer {
65 public:
66     CustomLayer(const std::string& id,
67                 std::unique_ptr<CustomLayerHost> host);
68 
69     ~CustomLayer() final;
70 
71     // Visibility
72     void setVisibility(VisibilityType) final;
73 
74     // Zoom range
75     void setMinZoom(float) final;
76     void setMaxZoom(float) final;
77 
78     // Private implementation
79 
80     class Impl;
81     const Impl& impl() const;
82 
83     Mutable<Impl> mutableImpl() const;
84     std::unique_ptr<Layer> cloneRef(const std::string& id) const final;
85 
86     CustomLayer(const CustomLayer&) = delete;
87 };
88 
89 template <>
90 bool Layer::is<CustomLayer>() const;
91 
92 } // namespace style
93 } // namespace mbgl
94