summaryrefslogtreecommitdiff
path: root/test/gl/object.cpp
blob: 1ed2bc91d61a499e35739f98cddcf1fe9692b787 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <mbgl/test/util.hpp>

#include <mbgl/platform/default/headless_display.hpp>
#include <mbgl/platform/default/headless_view.hpp>

#include <mbgl/gl/gl_helper.hpp>
#include <mbgl/gl/gl_config.hpp>
#include <mbgl/gl/object_store.hpp>
#include <mbgl/gl/texture_pool.hpp>

#include <memory>

namespace {

static bool getFlag = false;
static bool setFlag = false;

}; // namespace

struct MockGLObject {
    using Type = bool;
    static const Type Default = false;
    static Type Get() { getFlag = true; return true; }
    static void Set(const Type&) { setFlag = true; }
};

TEST(GLObject, Preserve) {
    getFlag = false;
    setFlag = false;

    auto object = std::make_unique<mbgl::gl::Preserve<MockGLObject>>();
    EXPECT_TRUE(getFlag);
    EXPECT_FALSE(setFlag);

    getFlag = false;
    object.reset();
    EXPECT_FALSE(getFlag);
    EXPECT_TRUE(setFlag);
}

TEST(GLObject, Value) {
    setFlag = false;

    auto object = std::make_unique<mbgl::gl::Value<MockGLObject>>();
    EXPECT_EQ(object->getCurrent(), false);
    EXPECT_FALSE(object->getDirty());
    EXPECT_FALSE(setFlag);

    object->setDirty();
    EXPECT_TRUE(object->getDirty());

    *object = false;
    EXPECT_EQ(object->getCurrent(), false);
    EXPECT_FALSE(object->getDirty());
    EXPECT_TRUE(setFlag);

    setFlag = false;
    *object = true;
    EXPECT_EQ(object->getCurrent(), true);
    EXPECT_FALSE(object->getDirty());
    EXPECT_TRUE(setFlag);

    object->reset();
    EXPECT_EQ(object->getCurrent(), false);
    EXPECT_TRUE(object->getDirty());
    EXPECT_TRUE(setFlag);
}

TEST(GLObject, Store) {
    mbgl::HeadlessView view(std::make_shared<mbgl::HeadlessDisplay>(), 1);
    view.activate();

    mbgl::gl::ObjectStore store;
    EXPECT_TRUE(store.empty());

    mbgl::gl::UniqueProgram program = store.createProgram();
    EXPECT_NE(program.get(), 0);
    program.reset();
    EXPECT_FALSE(store.empty());
    store.performCleanup();
    EXPECT_TRUE(store.empty());

    mbgl::gl::UniqueShader shader = store.createShader(GL_VERTEX_SHADER);
    EXPECT_NE(shader.get(), 0);
    shader.reset();
    EXPECT_FALSE(store.empty());
    store.performCleanup();
    EXPECT_TRUE(store.empty());

    mbgl::gl::UniqueBuffer buffer = store.createBuffer();
    EXPECT_NE(buffer.get(), 0);
    buffer.reset();
    EXPECT_FALSE(store.empty());
    store.performCleanup();
    EXPECT_TRUE(store.empty());

    mbgl::gl::UniqueTexture texture = store.createTexture();
    EXPECT_NE(texture.get(), 0);
    texture.reset();
    EXPECT_FALSE(store.empty());
    store.performCleanup();
    EXPECT_TRUE(store.empty());

    mbgl::gl::UniqueVAO vao = store.createVAO();
    EXPECT_NE(vao.get(), 0);
    vao.reset();
    EXPECT_FALSE(store.empty());
    store.performCleanup();
    EXPECT_TRUE(store.empty());

    mbgl::gl::UniqueTexturePool texturePool = store.createTexturePool();
    for (auto& id : texturePool.get()) {
        EXPECT_NE(id, 0);
    }
    EXPECT_TRUE(texturePool.get().size() == size_t(mbgl::gl::TextureMax));
    texturePool.reset();
    EXPECT_FALSE(store.empty());
    store.performCleanup();
    EXPECT_TRUE(store.empty());

    view.deactivate();
}

TEST(GLObject, TexturePool) {
    mbgl::HeadlessView view(std::make_shared<mbgl::HeadlessDisplay>(), 1);
    view.activate();

    mbgl::gl::ObjectStore store;
    EXPECT_TRUE(store.empty());

    mbgl::gl::TexturePool pool;

    std::vector<mbgl::gl::SharedTexture> ids;

    // Fill an entire texture pool.
    for (auto i = 0; i != mbgl::gl::TextureMax; ++i) {
        ids.push_back(pool.acquireTexture(store));
        EXPECT_EQ(ids.back().get(), GLuint(i + 1));
        EXPECT_TRUE(store.empty());
    }

    // Reuse texture ids from the same pool.
    for (auto i = 0; i != mbgl::gl::TextureMax; ++i) {
        ids[i].reset();
        ids.push_back(pool.acquireTexture(store));
        EXPECT_EQ(ids.back().get(), GLuint(i + 1));
        EXPECT_TRUE(store.empty());
    }

    // Trigger a new texture pool creation.
    {
        mbgl::gl::SharedTexture id = pool.acquireTexture(store);
        EXPECT_EQ(id, mbgl::gl::TextureMax + 1);
        EXPECT_TRUE(store.empty());

        id.reset();

        // Last used texture from pool triggers pool recycling.
        EXPECT_FALSE(store.empty());

        store.performCleanup();
        EXPECT_TRUE(store.empty());
    }

    // First pool is still full, thus creating a new pool.
    mbgl::gl::SharedTexture id1 = pool.acquireTexture(store);
    EXPECT_GT(id1.get(), mbgl::gl::TextureMax);
    EXPECT_TRUE(store.empty());

    // Release all textures from the first pool.
    ids.clear();
    EXPECT_FALSE(store.empty());

    store.performCleanup();
    EXPECT_TRUE(store.empty());

    // The first pool is now gone, the next pool is now in use.
    mbgl::gl::SharedTexture id2 = pool.acquireTexture(store);
    EXPECT_GT(id2.get(), id1.get());

    id2.reset();
    EXPECT_TRUE(store.empty());

    // Last used texture from the pool triggers pool recycling.
    id1.reset();
    EXPECT_FALSE(store.empty());

    store.performCleanup();
    EXPECT_TRUE(store.empty());

    view.deactivate();
}