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

font_texture.hpp « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b120ccfa9a2a20821dbf4fe53ac8a303c7409d5 (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
#pragma once

#include "drape/pointers.hpp"
#include "drape/texture.hpp"
#include "drape/glyph_manager.hpp"
#include "drape/dynamic_texture.hpp"

#include "std/atomic.hpp"
#include "std/condition_variable.hpp"
#include "std/list.hpp"
#include "std/map.hpp"
#include "std/vector.hpp"
#include "std/string.hpp"
#include "std/thread.hpp"

namespace dp
{

class GlyphPacker
{
public:
  GlyphPacker(m2::PointU const & size);

  bool PackGlyph(uint32_t width, uint32_t height, m2::RectU & rect);
  bool CanBePacked(uint32_t glyphsCount, uint32_t width, uint32_t height) const;
  m2::RectF MapTextureCoords(m2::RectU const & pixelRect) const;
  bool IsFull() const;

private:
  m2::PointU m_size = m2::PointU(0, 0);
  m2::PointU m_cursor = m2::PointU(0, 0);
  uint32_t m_yStep = 0;
  bool m_isFull = false;
};

class GlyphKey : public Texture::Key
{
public:
  GlyphKey(strings::UniChar unicodePoint) : m_unicodePoint(unicodePoint) {}

  Texture::ResourceType GetType() const { return Texture::Glyph; }
  strings::UniChar GetUnicodePoint() const { return m_unicodePoint; }

private:
  strings::UniChar m_unicodePoint;
};

class GlyphInfo : public Texture::ResourceInfo
{
  typedef Texture::ResourceInfo TBase;
public:
  GlyphInfo(m2::RectF const & texRect, GlyphManager::GlyphMetrics const & metrics)
    : TBase(texRect)
    , m_metrics(metrics)
  {}

  virtual Texture::ResourceType GetType() const { return Texture::Glyph; }
  GlyphManager::GlyphMetrics const & GetMetrics() const { return m_metrics; }

private:
  GlyphManager::GlyphMetrics m_metrics;
};

class GlyphGenerator
{
public:
  using TCompletionHandler = function<void(m2::RectU const &, GlyphManager::Glyph const &)>;

  struct GlyphGenerationData
  {
    m2::RectU m_rect;
    GlyphManager::Glyph m_glyph;

    GlyphGenerationData(m2::RectU const & rect, GlyphManager::Glyph const & glyph)
      : m_rect(rect), m_glyph(glyph)
    {}
  };

  GlyphGenerator(ref_ptr<GlyphManager> mng, TCompletionHandler const & completionHandler);
  ~GlyphGenerator();

  void GenerateGlyph(m2::RectU const & rect, GlyphManager::Glyph const & glyph);

  bool IsSuspended() const;

private:
  static void Routine(GlyphGenerator * generator);
  void WaitForGlyph(list<GlyphGenerationData> & queue);

  ref_ptr<GlyphManager> m_mng;
  TCompletionHandler m_completionHandler;

  list<GlyphGenerationData> m_queue;
  mutable mutex m_queueLock;

  atomic<bool> m_isRunning;
  condition_variable m_condition;
  bool m_isSuspended;
  thread m_thread;
};

class GlyphIndex
{
public:
  GlyphIndex(m2::PointU size, ref_ptr<GlyphManager> mng);
  ~GlyphIndex();

  // This function can return nullptr.
  ref_ptr<Texture::ResourceInfo> MapResource(GlyphKey const & key, bool & newResource);
  void UploadResources(ref_ptr<Texture> texture);

  bool CanBeGlyphPacked(uint32_t glyphsCount) const;

  bool HasAsyncRoutines() const;

  uint32_t GetAbsentGlyphsCount(strings::UniString const & text) const;

  // ONLY for unit-tests. DO NOT use this function anywhere else.
  size_t GetPendingNodesCount();

private:
  void OnGlyphGenerationCompletion(m2::RectU const & rect, GlyphManager::Glyph const & glyph);

  GlyphPacker m_packer;
  ref_ptr<GlyphManager> m_mng;
  unique_ptr<GlyphGenerator> m_generator;

  typedef map<strings::UniChar, GlyphInfo> TResourceMapping;
  typedef pair<m2::RectU, GlyphManager::Glyph> TPendingNode;
  typedef vector<TPendingNode> TPendingNodes;

  TResourceMapping m_index;
  TPendingNodes m_pendingNodes;
  threads::Mutex m_lock;
};

class FontTexture : public DynamicTexture<GlyphIndex, GlyphKey, Texture::Glyph>
{
  typedef DynamicTexture<GlyphIndex, GlyphKey, Texture::Glyph> TBase;
public:
  FontTexture(m2::PointU const & size, ref_ptr<GlyphManager> glyphMng, ref_ptr<HWTextureAllocator> allocator)
    : m_index(size, glyphMng)
  {
    TBase::TextureParams params;
    params.m_size = size;
    params.m_format = TextureFormat::ALPHA;
    params.m_filter = gl_const::GLLinear;

    vector<uint8_t> initData(params.m_size.x * params.m_size.y, 0);
    TBase::Init(allocator, make_ref(&m_index), params, make_ref(initData.data()));
  }

  ~FontTexture() { TBase::Reset(); }

  bool HasEnoughSpace(uint32_t newKeysCount) const override
  {
    return m_index.CanBeGlyphPacked(newKeysCount);
  }

  bool HasAsyncRoutines() const override
  {
    return m_index.HasAsyncRoutines();
  }

  uint32_t GetAbsentGlyphsCount(strings::UniString const & text) const
  {
    return m_index.GetAbsentGlyphsCount(text);
  }

private:
  GlyphIndex m_index;
};

}