diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-06-06 20:17:12 +0300 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-06-07 13:01:58 +0300 |
commit | 83d349ee16f1918d7e0275d47b9c22abc1a4f50c (patch) | |
tree | 3db5a34ec11598cc2e82df4534200f9a8d7f308c /src/mbgl/gl | |
parent | 858311d19b9879cf6ac1fea2d8746e136be3c4a3 (diff) | |
download | qtlocation-mapboxgl-83d349ee16f1918d7e0275d47b9c22abc1a4f50c.tar.gz |
[core] Use RAII for TexturePool textures
TexturePool now disposes acquirable ids via SharedTexture, which
guarantees that these are going back to TexturePool once released.
Diffstat (limited to 'src/mbgl/gl')
-rw-r--r-- | src/mbgl/gl/texture_pool.cpp | 30 | ||||
-rw-r--r-- | src/mbgl/gl/texture_pool.hpp | 16 |
2 files changed, 29 insertions, 17 deletions
diff --git a/src/mbgl/gl/texture_pool.cpp b/src/mbgl/gl/texture_pool.cpp index 87e5aed256..53e29b907b 100644 --- a/src/mbgl/gl/texture_pool.cpp +++ b/src/mbgl/gl/texture_pool.cpp @@ -9,20 +9,20 @@ namespace gl { class TexturePool::Impl : private util::noncopyable { public: - class Group : private util::noncopyable { + class Pool : private util::noncopyable { public: - Group(gl::ObjectStore& store) : pool(store.createTexturePool()), availableIDs(gl::TextureMax) { + Pool(gl::ObjectStore& store) : pool(store.createTexturePool()), availableIDs(gl::TextureMax) { std::copy(pool.get().begin(), pool.get().end(), availableIDs.begin()); } - Group(Group&& o) : pool(std::move(o.pool)), availableIDs(std::move(o.availableIDs)) {} - Group& operator=(Group&& o) { pool = std::move(o.pool); availableIDs = std::move(o.availableIDs); return *this; } + Pool(Pool&& o) : pool(std::move(o.pool)), availableIDs(std::move(o.availableIDs)) {} + Pool& operator=(Pool&& o) { pool = std::move(o.pool); availableIDs = std::move(o.availableIDs); return *this; } gl::UniqueTexturePool pool; std::vector<GLuint> availableIDs; }; - GLuint getTextureID(gl::ObjectStore& store) { + GLuint acquireTexture(gl::ObjectStore& store) { auto nextAvailableID = [](auto& pool_) { auto it = pool_.availableIDs.begin(); GLuint id = *it; @@ -36,15 +36,14 @@ public: } // All texture IDs are in use. - pools.emplace_back(Group { store }); + pools.emplace_back(Pool { store }); return nextAvailableID(pools.back()); } - void releaseTextureID(GLuint& id) { + void releaseTexture(GLuint id) { for (auto it = pools.begin(); it != pools.end(); ++it) { if (std::find(it->pool.get().begin(), it->pool.get().end(), id) != it->pool.get().end()) { it->availableIDs.push_back(id); - id = 0; if (GLsizei(it->availableIDs.size()) == gl::TextureMax) { pools.erase(it); } @@ -54,21 +53,22 @@ public: } private: - std::vector<Group> pools; + std::vector<Pool> pools; }; -TexturePool::TexturePool() : impl(std::make_unique<Impl>()) { +void TextureReleaser::operator()(GLuint id) const { + assert(pool); + pool->impl->releaseTexture(id); } -TexturePool::~TexturePool() { +TexturePool::TexturePool() : impl(std::make_unique<Impl>()) { } -GLuint TexturePool::getTextureID(gl::ObjectStore& store) { - return impl->getTextureID(store); +TexturePool::~TexturePool() { } -void TexturePool::releaseTextureID(GLuint& id) { - impl->releaseTextureID(id); +SharedTexture TexturePool::acquireTexture(gl::ObjectStore& store) { + return SharedTexture { impl->acquireTexture(store) , { this } }; } } // namespace gl diff --git a/src/mbgl/gl/texture_pool.hpp b/src/mbgl/gl/texture_pool.hpp index 9ab7d14ef6..3c38343f62 100644 --- a/src/mbgl/gl/texture_pool.hpp +++ b/src/mbgl/gl/texture_pool.hpp @@ -4,20 +4,32 @@ #include <mbgl/gl/gl.hpp> #include <mbgl/gl/object_store.hpp> +#include <unique_resource.hpp> + #include <memory> namespace mbgl { namespace gl { +class TexturePool; + +struct TextureReleaser { + TexturePool* pool; + void operator()(GLuint) const; +}; + +using SharedTexture = std_experimental::unique_resource<GLuint, TextureReleaser>; + class TexturePool : private util::noncopyable { public: TexturePool(); ~TexturePool(); - GLuint getTextureID(gl::ObjectStore&); - void releaseTextureID(GLuint&); + SharedTexture acquireTexture(gl::ObjectStore&); private: + friend TextureReleaser; + class Impl; const std::unique_ptr<Impl> impl; }; |