Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[test] Added GL objects tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Jun 1, 2016
1 parent 1b0683b commit 3175358
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/mbgl/gl/gl_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,32 @@ namespace gl {
template <typename T>
class Value {
public:
inline void operator=(const typename T::Type& value) {
void operator=(const typename T::Type& value) {
if (dirty || current != value) {
dirty = false;
current = value;
T::Set(current);
}
}

inline void reset() {
void reset() {
dirty = true;
current = T::Default;
T::Set(current);
}

inline void setDirty() {
void setDirty() {
dirty = true;
}

typename T::Type getCurrent() {
return current;
}

bool getDirty() {
return dirty;
}

private:
typename T::Type current = T::Default;
bool dirty = false;
Expand Down
8 changes: 8 additions & 0 deletions src/mbgl/gl/object_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ class ObjectStore : private util::noncopyable {
// Only call this while the OpenGL context is exclusive to this thread.
void performCleanup();

bool empty() const {
return abandonedPrograms.empty()
&& abandonedShaders.empty()
&& abandonedBuffers.empty()
&& abandonedTextures.empty()
&& abandonedVAOs.empty();
}

private:
friend ProgramDeleter;
friend ShaderDeleter;
Expand Down
120 changes: 120 additions & 0 deletions test/gl/object.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#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 <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_TRUE(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_TRUE(shader.get() != 0);
shader.reset();
EXPECT_FALSE(store.empty());
store.performCleanup();
EXPECT_TRUE(store.empty());

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

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

mbgl::gl::UniqueVAO vao = store.createVAO();
EXPECT_TRUE(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_TRUE(id != 0);
}
texturePool.reset();
EXPECT_FALSE(store.empty());
store.performCleanup();
EXPECT_TRUE(store.empty());

view.deactivate();
}
2 changes: 2 additions & 0 deletions test/test.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

'geometry/binpack.cpp',

'gl/object.cpp',

'map/map.cpp',
'map/transform.cpp',

Expand Down

0 comments on commit 3175358

Please sign in to comment.