From 83d349ee16f1918d7e0275d47b9c22abc1a4f50c Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 6 Jun 2016 20:17:12 +0300 Subject: [core] Use RAII for TexturePool textures TexturePool now disposes acquirable ids via SharedTexture, which guarantees that these are going back to TexturePool once released. --- src/mbgl/gl/texture_pool.cpp | 30 +++++++++++++++--------------- src/mbgl/gl/texture_pool.hpp | 16 ++++++++++++++-- 2 files changed, 29 insertions(+), 17 deletions(-) (limited to 'src/mbgl/gl') 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 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 pools; + std::vector pools; }; -TexturePool::TexturePool() : impl(std::make_unique()) { +void TextureReleaser::operator()(GLuint id) const { + assert(pool); + pool->impl->releaseTexture(id); } -TexturePool::~TexturePool() { +TexturePool::TexturePool() : impl(std::make_unique()) { } -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 #include +#include + #include namespace mbgl { namespace gl { +class TexturePool; + +struct TextureReleaser { + TexturePool* pool; + void operator()(GLuint) const; +}; + +using SharedTexture = std_experimental::unique_resource; + 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; }; -- cgit v1.2.1