[core] Do not expose TexturePool::Impl

Now using the same Pimpl mechanism from mbgl::Map.
This commit is contained in:
Bruno de Oliveira Abinader
2016-06-06 19:44:22 +03:00
parent 5e6f8d4ce5
commit 858311d19b
2 changed files with 60 additions and 38 deletions

View File

@@ -7,35 +7,68 @@
namespace mbgl {
namespace gl {
GLuint TexturePool::getTextureID(gl::ObjectStore& store) {
auto nextAvailableID = [](auto& pool_) {
auto it = pool_.availableIDs.begin();
GLuint id = *it;
pool_.availableIDs.erase(it);
return id;
class TexturePool::Impl : private util::noncopyable {
public:
class Group : private util::noncopyable {
public:
Group(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; }
gl::UniqueTexturePool pool;
std::vector<GLuint> availableIDs;
};
for (auto& pool : pools) {
if (pool.availableIDs.empty()) continue;
return nextAvailableID(pool);
GLuint getTextureID(gl::ObjectStore& store) {
auto nextAvailableID = [](auto& pool_) {
auto it = pool_.availableIDs.begin();
GLuint id = *it;
pool_.availableIDs.erase(it);
return id;
};
for (auto& pool : pools) {
if (pool.availableIDs.empty()) continue;
return nextAvailableID(pool);
}
// All texture IDs are in use.
pools.emplace_back(Group { store });
return nextAvailableID(pools.back());
}
// All texture IDs are in use.
pools.emplace_back(Impl { store });
return nextAvailableID(pools.back());
void releaseTextureID(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);
}
return;
}
}
}
private:
std::vector<Group> pools;
};
TexturePool::TexturePool() : impl(std::make_unique<Impl>()) {
}
TexturePool::~TexturePool() {
}
GLuint TexturePool::getTextureID(gl::ObjectStore& store) {
return impl->getTextureID(store);
}
void TexturePool::releaseTextureID(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);
}
return;
}
}
impl->releaseTextureID(id);
}
} // namespace gl

View File

@@ -4,33 +4,22 @@
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/object_store.hpp>
#include <algorithm>
#include <memory>
#include <vector>
namespace mbgl {
namespace gl {
class TexturePool : private util::noncopyable {
public:
TexturePool();
~TexturePool();
GLuint getTextureID(gl::ObjectStore&);
void releaseTextureID(GLuint&);
private:
class Impl : private util::noncopyable {
public:
Impl(gl::ObjectStore& store) : pool(store.createTexturePool()), availableIDs(gl::TextureMax) {
std::copy(pool.get().begin(), pool.get().end(), availableIDs.begin());
}
Impl(Impl&& o) : pool(std::move(o.pool)), availableIDs(std::move(o.availableIDs)) {}
Impl& operator=(Impl&& o) { pool = std::move(o.pool); availableIDs = std::move(o.availableIDs); return *this; }
gl::UniqueTexturePool pool;
std::vector<GLuint> availableIDs;
};
std::vector<Impl> pools;
class Impl;
const std::unique_ptr<Impl> impl;
};
} // namespace gl