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

glyph_generator.cpp « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cf1970867418e00fbb4fe91a730ad7e2cd782912 (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
#include "drape/glyph_generator.hpp"

#include <iterator>

using namespace std::placeholders;

namespace dp
{
GlyphGenerator::GlyphGenerator(uint32_t sdfScale)
  : m_sdfScale(sdfScale)
{}

GlyphGenerator::~GlyphGenerator()
{
  {
    std::lock_guard<std::mutex> lock(m_mutex);
    m_listeners.clear();
  }

  // Here we have to wait for active tasks completion,
  // because they capture 'this' pointer.
  m_activeTasks.FinishAll();

  std::lock_guard<std::mutex> lock(m_mutex);
  for (auto & data : m_queue)
    data.DestroyGlyph();
}

bool GlyphGenerator::IsSuspended() const
{
  std::lock_guard<std::mutex> lock(m_mutex);
  return m_glyphsCounter == 0;
}

void GlyphGenerator::RegisterListener(ref_ptr<GlyphGenerator::Listener> listener)
{
  std::lock_guard<std::mutex> lock(m_mutex);
  m_listeners.insert(listener);
}

void GlyphGenerator::UnregisterListener(ref_ptr<GlyphGenerator::Listener> listener)
{
  std::lock_guard<std::mutex> lock(m_mutex);
  m_listeners.erase(listener);
}

void GlyphGenerator::GenerateGlyph(ref_ptr<Listener> listener, m2::RectU const & rect,
                                   GlyphManager::Glyph & glyph)
{
  GenerateGlyph(listener, GlyphGenerationData(rect, glyph));
}

void GlyphGenerator::GenerateGlyph(ref_ptr<Listener> listener, GlyphGenerationData && data)
{
  GenerateGlyphs(listener, {std::move(data)});
}

void GlyphGenerator::GenerateGlyphs(ref_ptr<Listener> listener,
                                    GlyphGenerationDataArray && generationData)
{
  std::lock_guard<std::mutex> lock(m_mutex);
  if (m_listeners.find(listener) == m_listeners.end())
  {
    for (auto & data : generationData)
      data.DestroyGlyph();
    return;
  }

  GlyphGenerationDataArray queue;
  std::move(generationData.begin(), generationData.end(), std::back_inserter(m_queue));
  std::swap(m_queue, queue);
  m_glyphsCounter += queue.size();

  // Generate glyphs on the separate thread.
  auto generateTask = std::make_shared<GenerateGlyphTask>(std::move(queue));
  auto result = DrapeRoutine::Run([this, listener, generateTask]() mutable
  {
    generateTask->Run(m_sdfScale);
    OnTaskFinished(listener, generateTask);
  });

  if (result)
    m_activeTasks.Add(generateTask, result);
  else
    generateTask->DestroyAllGlyphs();
}

void GlyphGenerator::OnTaskFinished(ref_ptr<Listener> listener,
                                    std::shared_ptr<GenerateGlyphTask> const & task)
{
  if (task->IsCancelled())
  {
    task->DestroyAllGlyphs();
    return;
  }

  auto glyphs = task->StealGeneratedGlyphs();

  std::lock_guard<std::mutex> lock(m_mutex);
  ASSERT_GREATER_OR_EQUAL(m_glyphsCounter, glyphs.size(), ());
  m_glyphsCounter -= glyphs.size();

  if (m_listeners.find(listener) != m_listeners.end())
    listener->OnCompleteGlyphGeneration(std::move(glyphs));

  m_activeTasks.Remove(task);
}

void GlyphGenerator::GenerateGlyphTask::Run(uint32_t sdfScale)
{
  m_generatedGlyphs.reserve(m_glyphs.size());
  for (auto & data : m_glyphs)
  {
    if (m_isCancelled)
      return;

    auto const g = GlyphManager::GenerateGlyph(data.m_glyph, sdfScale);
    data.DestroyGlyph();
    m_generatedGlyphs.emplace_back(GlyphGenerator::GlyphGenerationData{data.m_rect, g});
  }
}

void GlyphGenerator::GenerateGlyphTask::DestroyAllGlyphs()
{
  for (auto & data : m_glyphs)
    data.DestroyGlyph();

  for (auto & data : m_generatedGlyphs)
    data.DestroyGlyph();
}
}  // namespace dp