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

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

#include "drape/drape_routine.hpp"
#include "drape/glyph_manager.hpp"
#include "drape/pointers.hpp"

#include "geometry/rect2d.hpp"

#include <atomic>
#include <functional>
#include <memory>
#include <mutex>
#include <set>
#include <vector>

namespace dp
{
class GlyphGenerator
{
public:
  struct GlyphGenerationData
  {
    m2::RectU m_rect;
    GlyphManager::Glyph m_glyph;

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

    void DestroyGlyph()
    {
      m_glyph.m_image.Destroy();
    }
  };

  using GlyphGenerationDataArray = std::vector<GlyphGenerationData>;

  class Listener
  {
  public:
    virtual ~Listener() = default;
    virtual void OnCompleteGlyphGeneration(GlyphGenerationDataArray && glyphs) = 0;
  };

  class GenerateGlyphTask
  {
  public:
    explicit GenerateGlyphTask(GlyphGenerationDataArray && glyphs)
      : m_glyphs(std::move(glyphs))
      , m_isCancelled(false)
    {}
    void Run(uint32_t sdfScale);
    void Cancel() { m_isCancelled = true; }

    GlyphGenerationDataArray && StealGeneratedGlyphs() { return std::move(m_generatedGlyphs); }
    bool IsCancelled() const { return m_isCancelled; }
    void DestroyAllGlyphs();

  private:
    GlyphGenerationDataArray m_glyphs;
    GlyphGenerationDataArray m_generatedGlyphs;
    std::atomic<bool> m_isCancelled;
  };

  explicit GlyphGenerator(uint32_t sdfScale);
  ~GlyphGenerator();

  void RegisterListener(ref_ptr<Listener> listener);
  void UnregisterListener(ref_ptr<Listener> listener);

  void GenerateGlyph(ref_ptr<Listener> listener, m2::RectU const & rect, GlyphManager::Glyph & glyph);
  void GenerateGlyph(ref_ptr<Listener> listener, GlyphGenerationData && data);
  void GenerateGlyphs(ref_ptr<Listener> listener, GlyphGenerationDataArray && generationData);
  bool IsSuspended() const;

  void FinishGeneration();

private:
  void OnTaskFinished(ref_ptr<Listener> listener, std::shared_ptr<GenerateGlyphTask> const & task);

  uint32_t m_sdfScale;
  std::set<ref_ptr<Listener>> m_listeners;
  ActiveTasks<GenerateGlyphTask> m_activeTasks;

  GlyphGenerationDataArray m_queue;
  size_t m_glyphsCounter = 0;
  mutable std::mutex m_mutex;
};
}  // namespace dp