summaryrefslogtreecommitdiff
path: root/src/mbgl/gl/gl_object_store.cpp
blob: 62d92f310099d0de71db3e88ab98db1d031f7d28 (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
#include <mbgl/gl/gl_object_store.hpp>
#include <mbgl/util/thread_context.hpp>

#include <cassert>

namespace mbgl {
namespace gl {

void ProgramHolder::create() {
    if (id) return;
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    id = MBGL_CHECK_ERROR(glCreateProgram());
}

void ProgramHolder::reset() {
    if (!id) return;
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    MBGL_CHECK_ERROR(glDeleteProgram(id));
    id = 0;
}

void ShaderHolder::create() {
    if (id) return;
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    id = MBGL_CHECK_ERROR(glCreateShader(type));
}

void ShaderHolder::reset() {
    if (!id) return;
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    MBGL_CHECK_ERROR(glDeleteShader(id));
    id = 0;
}

void BufferHolder::create() {
    if (id) return;
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    MBGL_CHECK_ERROR(glGenBuffers(1, &id));
}

void BufferHolder::reset() {
    if (!id) return;
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    MBGL_CHECK_ERROR(glDeleteBuffers(1, &id));
    id = 0;
}

void TextureHolder::create() {
    if (id) return;
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    MBGL_CHECK_ERROR(glGenTextures(1, &id));
}

void TextureHolder::reset() {
    if (!id) return;
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    MBGL_CHECK_ERROR(glDeleteTextures(1, &id));
    id = 0;
}

void TexturePoolHolder::create() {
    if (bool()) return;
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    MBGL_CHECK_ERROR(glGenTextures(TextureMax, ids.data()));
}

void TexturePoolHolder::reset() {
    if (!bool()) return;
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    MBGL_CHECK_ERROR(glDeleteTextures(TextureMax, ids.data()));
    ids.fill(0);
}

void VAOHolder::create() {
    if (id) return;
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    MBGL_CHECK_ERROR(gl::GenVertexArrays(1, &id));
}

void VAOHolder::reset() {
    if (!id) return;
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    MBGL_CHECK_ERROR(gl::DeleteVertexArrays(1, &id));
    id = 0;
}

void GLObjectStore::abandon(BufferHolder&& buffer) {
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    abandonedBuffers.push_back(std::move(buffer));
}

void GLObjectStore::abandon(TextureHolder&& texture) {
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    abandonedTextures.push_back(std::move(texture));
}

void GLObjectStore::abandon(TexturePoolHolder&& texture) {
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    abandonedTexturePools.push_back(std::move(texture));
}

void GLObjectStore::abandon(VAOHolder&& vao) {
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    abandonedVAOs.push_back(std::move(vao));
}

void GLObjectStore::performCleanup() {
    assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
    abandonedBuffers.clear();
    abandonedTextures.clear();
    abandonedTexturePools.clear();
    abandonedVAOs.clear();
}

} // namespace gl
} // namespace mbgl