diff options
author | ExMix <rahuba.youri@mapswithme.com> | 2014-03-27 18:55:45 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:13:59 +0300 |
commit | 760e3679c35f407d8481503b4898744b850cae50 (patch) | |
tree | 65dd6f87eff3c429df5c0148f1489edc4d46f43a /drape | |
parent | 9eaf13fa8efdf6a071dbdd22c981c5adcc404db6 (diff) |
[drape] texture wrapper and texture manager
Diffstat (limited to 'drape')
-rw-r--r-- | drape/symbols_texture.cpp | 29 | ||||
-rw-r--r-- | drape/symbols_texture.hpp | 13 | ||||
-rw-r--r-- | drape/texture.cpp | 6 | ||||
-rw-r--r-- | drape/texture.hpp | 18 | ||||
-rw-r--r-- | drape/texture_manager.cpp | 96 | ||||
-rw-r--r-- | drape/texture_manager.hpp | 24 | ||||
-rw-r--r-- | drape/texture_set_controller.hpp | 9 | ||||
-rw-r--r-- | drape/texture_set_holder.hpp | 22 |
8 files changed, 197 insertions, 20 deletions
diff --git a/drape/symbols_texture.cpp b/drape/symbols_texture.cpp index c2917365b6..34327a3a76 100644 --- a/drape/symbols_texture.cpp +++ b/drape/symbols_texture.cpp @@ -4,6 +4,21 @@ #include "../platform/platform.hpp" +SymbolsTexture::SymbolKey::SymbolKey(const string & symbolName) + : m_symbolName(symbolName) +{ +} + +Texture::Key::Type SymbolsTexture::SymbolKey::GetType() const +{ + return Texture::Key::Symbol; +} + +const string & SymbolsTexture::SymbolKey::GetSymbolName() const +{ + return m_symbolName; +} + void SymbolsTexture::Load(const string & skinPathName) { uint32_t width, height; @@ -23,11 +38,19 @@ void SymbolsTexture::Load(const string & skinPathName) Create(width, height, Texture::RGBA8, MakeStackRefPointer<void>(&pngData[0])); } -m2::RectD SymbolsTexture::FindSymbol(const string & symbolName) const +bool SymbolsTexture::FindResource(const Texture::Key & key, m2::RectF & texRect, m2::PointU & pixelSize) const { + if (key.GetType() != Texture::Key::Symbol) + return false; + + const string & symbolName = static_cast<const SymbolKey &>(key).GetSymbolName(); + m2::RectU r; if (!m_desc.GetResource(symbolName, r)) - return m2::RectD(); + return false; - return m2::RectD(GetS(r.minX()), GetT(r.minY()), GetS(r.maxX()), GetT(r.maxY())); + pixelSize.x = r.SizeX(); + pixelSize.y = r.SizeY(); + texRect = m2::RectF(GetS(r.minX()), GetT(r.minY()), GetS(r.maxX()), GetT(r.maxY())); + return true; } diff --git a/drape/symbols_texture.hpp b/drape/symbols_texture.hpp index 1d124c4b0d..0455e14155 100644 --- a/drape/symbols_texture.hpp +++ b/drape/symbols_texture.hpp @@ -8,8 +8,19 @@ class SymbolsTexture : public Texture { public: + class SymbolKey : public Key + { + public: + SymbolKey(const string & symbolName); + virtual Type GetType() const; + const string & GetSymbolName() const; + + private: + string m_symbolName; + }; + void Load(string const & skinPathName); - m2::RectD FindSymbol(string const & symbolName) const; + bool FindResource(Key const & key, m2::RectF & texRect, m2::PointU & pixelSize) const; private: TextureStructureDesc m_desc; diff --git a/drape/texture.cpp b/drape/texture.cpp index dc39a62b08..9e53b02d06 100644 --- a/drape/texture.cpp +++ b/drape/texture.cpp @@ -12,6 +12,12 @@ Texture::Texture() { } +Texture::~Texture() +{ + if (m_textureID != -1) + GLFunctions::glDeleteTexture(m_textureID); +} + void Texture::Create(uint32_t width, uint32_t height, TextureFormat format) { Create(width, height, format, MakeStackRefPointer<void>(NULL)); diff --git a/drape/texture.hpp b/drape/texture.hpp index 18f74366dd..2c74bffa23 100644 --- a/drape/texture.hpp +++ b/drape/texture.hpp @@ -3,6 +3,8 @@ #include "pointers.hpp" #include "glconstants.hpp" +#include "../geometry/rect2d.hpp" + #include "../std/stdint.hpp" class Texture @@ -14,7 +16,21 @@ public: RGBA4 }; + class Key + { + public: + enum Type + { + Symbol, + Font, + UniformValue + }; + + virtual Type GetType() const = 0; + }; + Texture(); + virtual ~Texture(); void Create(uint32_t width, uint32_t height, TextureFormat format); void Create(uint32_t width, uint32_t height, TextureFormat format, RefPointer<void> data); @@ -24,6 +40,8 @@ public: void UploadData(uint32_t x, uint32_t y, uint32_t width, uint32_t height, TextureFormat format, RefPointer<void> data); + virtual bool FindResource(Key const & key, m2::RectF & texRect, m2::PointU & pixelSize) const = 0; + uint32_t GetWidth() const; uint32_t GetHeight() const; float GetS(uint32_t x) const; diff --git a/drape/texture_manager.cpp b/drape/texture_manager.cpp index c3a7b7f103..58fe6b1a3f 100644 --- a/drape/texture_manager.cpp +++ b/drape/texture_manager.cpp @@ -1,16 +1,102 @@ #include "texture_manager.hpp" +#include "symbols_texture.hpp" + +#include "glfunctions.hpp" + +#include "../base/stl_add.hpp" #include "../coding/file_name_utils.hpp" #include "../platform/platform.hpp" -void TextureManager::LoadExternalResources(const string & resourcePostfix) +#include "../std/vector.hpp" + +class TextureManager::TextureSet +{ +public: + TextureSet(uint32_t maxSize) + : m_maxSize(maxSize) + { + } + + ~TextureSet() + { + GetRangeDeletor(m_textures, MasterPointerDeleter())(); + } + + bool IsFull() const + { + return !(m_textures.size() < m_maxSize); + } + + void AddTexture(TransferPointer<Texture> texture) + { + ASSERT(!IsFull(), ()); + m_textures.push_back(MasterPointer<Texture>(texture)); + } + + int32_t FindResource(Texture::Key const & key, m2::RectF & texRect, m2::PointU & pixelSize) const + { + for (size_t i = 0; i < m_textures.size(); ++i) + { + if (m_textures[i]->FindResource(key, texRect, pixelSize)) + return i; + } + + return -1; + } + + void BindTextureSet() const + { + for (size_t i = 0; i < m_textures.size(); ++i) + { + GLFunctions::glActiveTexture(GLConst::GLTexture0 + i); + m_textures[i]->Bind(); + } + } + + uint32_t GetSize() const + { + return m_textures.size(); + } + +private: + vector<MasterPointer<Texture> > m_textures; + uint32_t m_maxSize; +}; + +void TextureManager::Init(const string & resourcePostfix) +{ + // in shader we handle only 8 textures + m_maxTextureBlocks = min(8, GLFunctions::glGetInteger(GLConst::GLMaxFragmentTextures)); + SymbolsTexture * symbols = new SymbolsTexture(); + symbols->Load(my::JoinFoldersToPath(string("resources-") + resourcePostfix, "symbols")); + + m_textures.Reset(new TextureSet(m_maxTextureBlocks)); + m_textures->AddTexture(MovePointer<Texture>(symbols)); +} + +void TextureManager::Release() +{ + m_textures.Destroy(); +} + +void TextureManager::GetSymbolRegion(const string & symbolName, TextureRegion & symbol) const +{ + SymbolsTexture::SymbolKey key(symbolName); + symbol.m_textureSet = 0; + symbol.m_textureOffset = m_textures->FindResource(key, symbol.m_stRect, symbol.m_pixelSize); + ASSERT(symbol.m_textureOffset != -1, ()); +} + +void TextureManager::BindTextureSet(uint32_t textureSet) const { - m_symbols.Load(my::JoinFoldersToPath(string("resources-") + resourcePostfix, "symbols")); + ASSERT_LESS(textureSet, 1, ()); // TODO replace 1 to m_textureSets.size() + m_textures->BindTextureSet(); } -void TextureManager::GetSymbolRect(const string & symbolName, TextureManager::Symbol & symbol) const +uint32_t TextureManager::GetTextureCount(uint32_t textureSet) const { - symbol.m_textureBlock = 0; - symbol.m_texCoord = m_symbols.FindSymbol(symbolName); + ASSERT_LESS(textureSet, 1, ()); // TODO replace 1 to m_textureSets.size() + return m_textures->GetSize(); } diff --git a/drape/texture_manager.hpp b/drape/texture_manager.hpp index db12de37f0..751a4b2275 100644 --- a/drape/texture_manager.hpp +++ b/drape/texture_manager.hpp @@ -1,20 +1,22 @@ #pragma once -#include "symbols_texture.hpp" +#include "pointers.hpp" +#include "texture_set_holder.hpp" +#include "texture_set_controller.hpp" -class TextureManager +class TextureManager : public TextureSetHolder, + public TextureSetController { public: - void LoadExternalResources(const string & resourcePostfix); + void Init(const string & resourcePrefix); + void Release(); + void GetSymbolRegion(const string & symbolName, TextureRegion & region) const; - struct Symbol - { - m2::RectD m_texCoord; - uint32_t m_textureBlock; - }; - - void GetSymbolRect(const string & symbolName, Symbol & symbol) const; + void BindTextureSet(uint32_t textureSet) const; + uint32_t GetTextureCount(uint32_t textureSet) const; private: - SymbolsTexture m_symbols; + class TextureSet; + MasterPointer<TextureSet> m_textures; + uint32_t m_maxTextureBlocks; }; diff --git a/drape/texture_set_controller.hpp b/drape/texture_set_controller.hpp new file mode 100644 index 0000000000..75ad546155 --- /dev/null +++ b/drape/texture_set_controller.hpp @@ -0,0 +1,9 @@ +#pragma once + +class TextureSetController +{ +public: + virtual ~TextureSetController() {} + virtual void BindTextureSet(uint32_t textureSet) const = 0; + virtual uint32_t GetTextureCount(uint32_t textureSet) const = 0; +}; diff --git a/drape/texture_set_holder.hpp b/drape/texture_set_holder.hpp new file mode 100644 index 0000000000..7dff74f9f1 --- /dev/null +++ b/drape/texture_set_holder.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "../geometry/point2d.hpp" +#include "../geometry/rect2d.hpp" + +class TextureSetHolder +{ +public: + virtual ~TextureSetHolder() {} + virtual void Init(const string & resourcePrefix) = 0; + virtual void Release() = 0; + + struct TextureRegion + { + m2::PointU m_pixelSize; + m2::RectF m_stRect; // texture coodinates in range [0, 1] + int32_t m_textureSet; // numer of texture set where region placed + int32_t m_textureOffset; // number of texture in set + }; + + virtual void GetSymbolRegion(const string & symbolName, TextureRegion & region) const = 0; +}; |