Welcome to mirror list, hosted at ThFree Co, Russian Federation.

texture_manager.cpp « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3e31a922b01328d01adf7dc54f9b2f91e6b31ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "texture_manager.hpp"
#include "symbols_texture.hpp"
#include "font_texture.hpp"
#include "dynamic_texture.hpp"
#include "stipple_pen_resource.hpp"
#include "texture_of_colors.hpp"

#include "glfunctions.hpp"

#include "../platform/platform.hpp"

#include "../coding/file_name_utils.hpp"

#include "../base/stl_add.hpp"

#include "../std/vector.hpp"
#include "../std/bind.hpp"

namespace dp
{

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));
  }

  Texture::ResourceInfo const * FindResource(Texture::Key const & key,
                                             TextureManager::TextureNode & node) const
  {
    for (size_t i = 0; i < m_textures.size(); ++i)
    {
      RefPointer<Texture> texture = m_textures[i].GetRefPointer();
      Texture::ResourceInfo const * info =  texture->FindResource(key);
      if (info != NULL)
      {
        node.m_width = texture->GetWidth();
        node.m_height = texture->GetHeight();
        node.m_textureOffset = i;
        return info;
      }
    }

    return NULL;
  }

  void UpdateDynamicTextures()
  {
    for_each(m_textures.begin(), m_textures.end(), bind(&Texture::UpdateState,
                                                        bind(&NonConstGetter<Texture>, _1)));
  }

  void BindTextureSet() const
  {
    for (size_t i = 0; i < m_textures.size(); ++i)
    {
      GLFunctions::glActiveTexture(gl_const::GLTexture0 + i);
      m_textures[i]->Bind();
    }
  }

  uint32_t GetSize() const
  {
    return m_textures.size();
  }

private:
  vector<MasterPointer<Texture> > m_textures;
  uint32_t m_maxSize;
};

int TextureManager::GetMaxTextureSet() const
{
  return m_textures.size();
}

void TextureManager::UpdateDynamicTextures()
{
  for_each(m_textures.begin(), m_textures.end(), bind(&TextureSet::UpdateDynamicTextures,
                                                        bind(&NonConstGetter<TextureSet>, _1)));
}

void TextureManager::Init(string const & resourcePrefix)
{
  // in shader we handle only 8 textures
  m_maxTextureBlocks = min(8, GLFunctions::glGetInteger(gl_const::GLMaxFragmentTextures));
  SymbolsTexture * symbols = new SymbolsTexture();
  symbols->Load(my::JoinFoldersToPath(string("resources-") + resourcePrefix, "symbols"));

  TextureSet * defaultSet = new TextureSet(m_maxTextureBlocks);
  defaultSet->AddTexture(MovePointer<Texture>(symbols));

  typedef DynamicTexture<StipplePenIndex, StipplePenKey, Texture::StipplePen> TStippleTexture;
  typedef DynamicTexture<ColorPalette, ColorKey, Texture::Color> TColorTexture;
  defaultSet->AddTexture(MovePointer<Texture>(new TStippleTexture(m2::PointU(1024, 1024), dp::ALPHA)));
  defaultSet->AddTexture(MovePointer<Texture>(new TColorTexture(m2::PointU(1024, 1024), dp::RGBA8)));

  m_textures.push_back(MasterPointer<TextureSet>(defaultSet));

  vector<TransferPointer<Texture> > tempTextures;
  LoadFont(string("resources-common/font"), tempTextures);
  for (size_t i = 0; i < tempTextures.size(); ++i)
  {
    RefPointer<TextureSet> set = m_textures.back().GetRefPointer();
    if (set->IsFull())
    {
      m_textures.push_back(MasterPointer<TextureSet>(new TextureSet(m_maxTextureBlocks)));
      set = m_textures.back().GetRefPointer();
    }

    set->AddTexture(tempTextures[i]);
  }

  RefPointer<TextureSet> textureSet = m_textures.back().GetRefPointer();
  if (textureSet->IsFull())
  {
    m_textures.push_back(MasterPointer<TextureSet>(new TextureSet(m_maxTextureBlocks)));
    textureSet = m_textures.back().GetRefPointer();
  }
}

void TextureManager::Release()
{
  DeleteRange(m_textures, MasterPointerDeleter());
}

void TextureManager::GetSymbolRegion(string const & symbolName, SymbolRegion & region) const
{
  SymbolsTexture::SymbolKey key(symbolName);
  TextureNode node;
  node.m_textureSet = 0;
  Texture::ResourceInfo const * info = m_textures[0]->FindResource(key, node);
  ASSERT(node.m_textureOffset != -1, ());
  region.SetResourceInfo(info);
  region.SetTextureNode(node);
}

template <typename TKey, typename TRegion>
bool TextureManager::FindResource(TKey const & key, TRegion & region) const
{
  for (size_t i = 0; i < m_textures.size(); ++i)
  {
    TextureNode node;
    Texture::ResourceInfo const * info = m_textures[i]->FindResource(key, node);
    if (info != NULL)
    {
      node.m_textureSet = i;
      region.SetTextureNode(node);
      region.SetResourceInfo(info);
      return true;
    }
  }

  return false;
}

bool TextureManager::GetGlyphRegion(strings::UniChar charCode, GlyphRegion & region) const
{
  FontTexture::GlyphKey key(charCode);
  return FindResource(key, region);
}

void TextureManager::GetStippleRegion(StipplePenKey const & pen, TextureSetHolder::StippleRegion & region) const
{
  VERIFY(FindResource(pen, region), ());
}

void TextureManager::GetColorRegion(ColorKey const & pen, TextureSetHolder::ColorRegion & region) const
{
  VERIFY(FindResource(pen, region), ());
}


void TextureManager::BindTextureSet(uint32_t textureSet) const
{
  ASSERT_LESS(textureSet, m_textures.size(), ());
  m_textures[textureSet]->BindTextureSet();
}

uint32_t TextureManager::GetTextureCount(uint32_t textureSet) const
{
  ASSERT_LESS(textureSet, m_textures.size(), ());
  return m_textures[textureSet]->GetSize();
}


TextureSetBinder::TextureSetBinder(RefPointer<TextureManager> manager)
  : m_manager(manager)
{
}

void TextureSetBinder::BindTextureSet(uint32_t textureSet) const
{
  m_manager->BindTextureSet(textureSet);
}

uint32_t TextureSetBinder::GetTextureCount(uint32_t textureSet) const
{
  return m_manager->GetTextureCount(textureSet);
}

} // namespace dp