diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-02-14 22:26:30 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-02-18 13:27:35 +0200 |
commit | 124173080f35d4a53e24790c0ad2cf2687d35836 (patch) | |
tree | 415f17836fd92d53fa212b55c29536d71407df1c /src/mbgl/gl | |
parent | 3397398c1f5f05e22219557e0e81cfe61d693b33 (diff) | |
download | qtlocation-mapboxgl-124173080f35d4a53e24790c0ad2cf2687d35836.tar.gz |
[gl] Added mbgl::gl::Texture{,Pool}Holder
Diffstat (limited to 'src/mbgl/gl')
-rw-r--r-- | src/mbgl/gl/gl_object_store.cpp | 43 | ||||
-rw-r--r-- | src/mbgl/gl/gl_object_store.hpp | 41 | ||||
-rw-r--r-- | src/mbgl/gl/texture_pool.cpp | 18 | ||||
-rw-r--r-- | src/mbgl/gl/texture_pool.hpp | 1 |
4 files changed, 74 insertions, 29 deletions
diff --git a/src/mbgl/gl/gl_object_store.cpp b/src/mbgl/gl/gl_object_store.cpp index 68d743cd95..addf1cc972 100644 --- a/src/mbgl/gl/gl_object_store.cpp +++ b/src/mbgl/gl/gl_object_store.cpp @@ -48,6 +48,32 @@ void BufferHolder::reset() { 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 GLObjectStore::abandonVAO(GLuint vao) { assert(util::ThreadContext::currentlyOn(util::ThreadType::Map)); @@ -59,9 +85,14 @@ void GLObjectStore::abandon(BufferHolder&& buffer) { abandonedBuffers.push_back(std::move(buffer)); } -void GLObjectStore::abandonTexture(GLuint texture) { +void GLObjectStore::abandon(TextureHolder&& texture) { assert(util::ThreadContext::currentlyOn(util::ThreadType::Map)); - abandonedTextures.emplace_back(texture); + 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::performCleanup() { @@ -74,12 +105,8 @@ void GLObjectStore::performCleanup() { } abandonedBuffers.clear(); - - if (!abandonedTextures.empty()) { - MBGL_CHECK_ERROR(glDeleteTextures(static_cast<GLsizei>(abandonedTextures.size()), - abandonedTextures.data())); - abandonedTextures.clear(); - } + abandonedTextures.clear(); + abandonedTexturePools.clear(); } } // namespace gl diff --git a/src/mbgl/gl/gl_object_store.hpp b/src/mbgl/gl/gl_object_store.hpp index 7b9409f329..6a115df0cb 100644 --- a/src/mbgl/gl/gl_object_store.hpp +++ b/src/mbgl/gl/gl_object_store.hpp @@ -4,6 +4,8 @@ #include <mbgl/gl/gl.hpp> #include <mbgl/util/noncopyable.hpp> +#include <array> +#include <algorithm> #include <memory> #include <vector> @@ -63,12 +65,46 @@ public: void reset(); }; +class TextureHolder : public GLHolder { +public: + TextureHolder() = default; + ~TextureHolder() { reset(); } + + TextureHolder(TextureHolder&& o) noexcept : GLHolder(std::move(o)) {} + TextureHolder& operator=(TextureHolder&& o) noexcept { GLHolder::operator=(std::move(o)); return *this; } + + void create(); + void reset(); +}; + +class TexturePoolHolder : private util::noncopyable { +public: + static const GLsizei TextureMax = 64; + + TexturePoolHolder() { ids.fill(0); } + ~TexturePoolHolder() { reset(); } + + TexturePoolHolder(TexturePoolHolder&& o) noexcept : ids(std::move(o.ids)) {} + TexturePoolHolder& operator=(TexturePoolHolder&& o) noexcept { ids = std::move(o.ids); return *this; } + + explicit operator bool() { return std::none_of(ids.begin(), ids.end(), [](int id) { return id == 0; }); } + const std::array<GLuint, TextureMax>& getIDs() const { return ids; } + const GLuint& operator[](size_t pos) { return ids[pos]; } + + void create(); + void reset(); + +private: + std::array<GLuint, TextureMax> ids; +}; + class GLObjectStore : private util::noncopyable { public: // Mark OpenGL objects for deletion void abandonVAO(GLuint vao); void abandon(BufferHolder&&); - void abandonTexture(GLuint texture); + void abandon(TextureHolder&&); + void abandon(TexturePoolHolder&&); // Actually remove the objects we marked as abandoned with the above methods. // Only call this while the OpenGL context is exclusive to this thread. @@ -79,7 +115,8 @@ private: // GLHolder-derived object can vary in size. std::vector<GLuint> abandonedVAOs; std::vector<BufferHolder> abandonedBuffers; - std::vector<GLuint> abandonedTextures; + std::vector<TextureHolder> abandonedTextures; + std::vector<TexturePoolHolder> abandonedTexturePools; }; } // namespace gl diff --git a/src/mbgl/gl/texture_pool.cpp b/src/mbgl/gl/texture_pool.cpp index 9d0ff79b11..334111dd52 100644 --- a/src/mbgl/gl/texture_pool.cpp +++ b/src/mbgl/gl/texture_pool.cpp @@ -31,26 +31,8 @@ GLuint TexturePool::getTextureID() { } void TexturePool::removeTextureID(GLuint texture_id) { - bool needs_clear = false; - texture_ids.insert(texture_id); - - if (texture_ids.size() > TextureMax) { - needs_clear = true; - } - - if (needs_clear) { // TODO: We need to find a better way to deal with texture pool cleanup -// clearTextureIDs(); - } -} - -void TexturePool::clearTextureIDs() { - auto getGLObjectStore = util::ThreadContext::getGLObjectStore(); - for (auto texture : texture_ids) { - getGLObjectStore->abandonTexture(texture); - } - texture_ids.clear(); } } // namespace gl diff --git a/src/mbgl/gl/texture_pool.hpp b/src/mbgl/gl/texture_pool.hpp index 3981d98f08..f68ca2eeaa 100644 --- a/src/mbgl/gl/texture_pool.hpp +++ b/src/mbgl/gl/texture_pool.hpp @@ -14,7 +14,6 @@ class TexturePool : private util::noncopyable { public: GLuint getTextureID(); void removeTextureID(GLuint texture_id); - void clearTextureIDs(); private: std::set<GLuint> texture_ids; |