summaryrefslogtreecommitdiff
path: root/include/mbgl/map/environment.hpp
blob: d8068187e7d7666abb368761f77a33325beb84c6 (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
#ifndef MBGL_MAP_MAP_ENVIRONMENT
#define MBGL_MAP_MAP_ENVIRONMENT

#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/util.hpp>

#include <thread>
#include <functional>
#include <vector>

typedef struct uv_loop_s uv_loop_t;

namespace mbgl {

class FileSource;
class Request;
class Response;
struct Resource;

enum class ThreadType : uint8_t {
    Unknown    = 0,
    Main       = 1 << 0,
    Map        = 1 << 1,
    TileWorker = 1 << 2,
};

class Environment final : private util::noncopyable {
public:
    Environment(FileSource&);
    ~Environment();

    static Environment& Get();
    static bool inScope();
    static bool currentlyOn(ThreadType);
    static std::string threadName();

    unsigned getID() const;

    // #############################################################################################

    // File request APIs
    Request* request(const Resource&, std::function<void(const Response&)>);
    void cancelRequest(Request*);

    // #############################################################################################

    // Mark OpenGL objects for deletion
    void abandonVAO(uint32_t vao);
    void abandonBuffer(uint32_t buffer);
    void abandonTexture(uint32_t texture);

    // Actually remove the objects we marked as abandoned with the above methods.
    // Only call this while the OpenGL context is exclusive to this thread.
    void performCleanup();

private:
    unsigned id;
    FileSource& fileSource;

    // Stores OpenGL objects that we marked for deletion
    std::vector<uint32_t> abandonedVAOs;
    std::vector<uint32_t> abandonedBuffers;
    std::vector<uint32_t> abandonedTextures;
};

class EnvironmentScope final {
public:
    EnvironmentScope(Environment&, ThreadType, const std::string& name);
    ~EnvironmentScope();

private:
    std::thread::id id;
};

}

#endif