From 3397398c1f5f05e22219557e0e81cfe61d693b33 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Sun, 14 Feb 2016 18:45:10 +0200 Subject: [gl] Added mbgl::gl::BufferHolder --- src/mbgl/gl/gl_object_store.cpp | 25 +++++++++++++++++-------- src/mbgl/gl/gl_object_store.hpp | 18 ++++++++++++++++-- 2 files changed, 33 insertions(+), 10 deletions(-) (limited to 'src/mbgl/gl') diff --git a/src/mbgl/gl/gl_object_store.cpp b/src/mbgl/gl/gl_object_store.cpp index 7a6037d505..68d743cd95 100644 --- a/src/mbgl/gl/gl_object_store.cpp +++ b/src/mbgl/gl/gl_object_store.cpp @@ -35,15 +35,28 @@ void ShaderHolder::reset() { 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 GLObjectStore::abandonVAO(GLuint vao) { assert(util::ThreadContext::currentlyOn(util::ThreadType::Map)); abandonedVAOs.emplace_back(vao); } -void GLObjectStore::abandonBuffer(GLuint buffer) { +void GLObjectStore::abandon(BufferHolder&& buffer) { assert(util::ThreadContext::currentlyOn(util::ThreadType::Map)); - abandonedBuffers.emplace_back(buffer); + abandonedBuffers.push_back(std::move(buffer)); } void GLObjectStore::abandonTexture(GLuint texture) { @@ -60,17 +73,13 @@ void GLObjectStore::performCleanup() { abandonedVAOs.clear(); } + abandonedBuffers.clear(); + if (!abandonedTextures.empty()) { MBGL_CHECK_ERROR(glDeleteTextures(static_cast(abandonedTextures.size()), abandonedTextures.data())); abandonedTextures.clear(); } - - if (!abandonedBuffers.empty()) { - MBGL_CHECK_ERROR(glDeleteBuffers(static_cast(abandonedBuffers.size()), - abandonedBuffers.data())); - abandonedBuffers.clear(); - } } } // namespace gl diff --git a/src/mbgl/gl/gl_object_store.hpp b/src/mbgl/gl/gl_object_store.hpp index 6c7f3c92f2..7b9409f329 100644 --- a/src/mbgl/gl/gl_object_store.hpp +++ b/src/mbgl/gl/gl_object_store.hpp @@ -51,11 +51,23 @@ private: GLenum type = 0; }; +class BufferHolder : public GLHolder { +public: + BufferHolder() = default; + ~BufferHolder() { reset(); } + + BufferHolder(BufferHolder&& o) noexcept : GLHolder(std::move(o)) {} + BufferHolder& operator=(BufferHolder&& o) noexcept { GLHolder::operator=(std::move(o)); return *this; } + + void create(); + void reset(); +}; + class GLObjectStore : private util::noncopyable { public: // Mark OpenGL objects for deletion void abandonVAO(GLuint vao); - void abandonBuffer(GLuint buffer); + void abandon(BufferHolder&&); void abandonTexture(GLuint texture); // Actually remove the objects we marked as abandoned with the above methods. @@ -63,8 +75,10 @@ public: void performCleanup(); private: + // We split the holder objects in separate containers because each + // GLHolder-derived object can vary in size. std::vector abandonedVAOs; - std::vector abandonedBuffers; + std::vector abandonedBuffers; std::vector abandonedTextures; }; -- cgit v1.2.1