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

gpu_drawer.cpp « render - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cc3f756e757381f77afbe07c8d3019e665d33cf7 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "gpu_drawer.hpp"
#include "feature_styler.hpp"
#include "proto_to_styles.hpp"
#include "path_info.hpp"
#include "area_info.hpp"

#include "indexer/drawing_rules.hpp"
#include "indexer/feature_decl.hpp"

#include "graphics/screen.hpp"

namespace
{
  graphics::OverlayElement::UserInfo ToUserInfo(FeatureID const & id)
  {
    graphics::OverlayElement::UserInfo info;
    info.m_featureID = id;
    return info;
  }
}

GPUDrawer::GPUDrawer(Params const & params)
  : TBase(params)
  , m_pScreen(new graphics::Screen(params.m_screenParams))
{
  for (unsigned i = 0; i < m_pScreen->pipelinesCount(); ++i)
    m_pScreen->addClearPageFn(i, bind(&GPUDrawer::ClearResourceCache, ThreadSlot(), i), 0);
}

graphics::Screen * GPUDrawer::Screen() const
{
  return m_pScreen.get();
}

void GPUDrawer::BeginFrame()
{
  m_pScreen->beginFrame();
}

void GPUDrawer::EndFrame()
{
  m_pScreen->endFrame();
}

void GPUDrawer::OnSize(int w, int h)
{
  m_pScreen->onSize(w, h);
}

graphics::GlyphCache * GPUDrawer::GetGlyphCache()
{
  return Screen()->glyphCache();
}

int GPUDrawer::ThreadSlot() const
{
  return m_pScreen->threadSlot();
}

void GPUDrawer::DrawCircle(m2::PointD const & pt,
                           graphics::EPosition pos,
                           di::DrawRule const & rule)
{
  graphics::Circle::Info ci;
  ConvertStyle(rule.m_rule->GetCircle(), VisualScale(), ci);

  graphics::CircleElement::Params params;

  params.m_depth = rule.m_depth;
  params.m_position = pos;
  params.m_pivot = pt;
  params.m_ci = ci;
  params.m_userInfo = ToUserInfo(GetCurrentFeatureID());

  m_pScreen->drawCircle(params);
}

void GPUDrawer::DrawSymbol(m2::PointD const & pt,
                           graphics::EPosition pos,
                           di::DrawRule const & rule)
{
  graphics::Icon::Info info;
  ConvertStyle(rule.m_rule->GetSymbol(), info);

  graphics::SymbolElement::Params params;
  params.m_depth = rule.m_depth;
  params.m_position = pos;
  params.m_pivot = pt;
  params.m_info = info;
  params.m_renderer = m_pScreen.get();
  params.m_userInfo = ToUserInfo(GetCurrentFeatureID());

  m_pScreen->drawSymbol(params);
}

void GPUDrawer::DrawCircledSymbol(m2::PointD const & pt,
                                  graphics::EPosition pos,
                                  di::DrawRule const & symbolRule,
                                  di::DrawRule const & circleRule)
{
  graphics::Icon::Info info;
  ConvertStyle(symbolRule.m_rule->GetSymbol(), info);

  graphics::Circle::Info ci;
  ConvertStyle(circleRule.m_rule->GetCircle(), VisualScale(), ci);

  graphics::SymbolElement::Params symParams;
  symParams.m_depth = symbolRule.m_depth;
  symParams.m_position = pos;
  symParams.m_pivot = pt;
  symParams.m_info = info;
  symParams.m_renderer = m_pScreen.get();
  symParams.m_userInfo = ToUserInfo(GetCurrentFeatureID());

  graphics::CircleElement::Params circleParams;
  circleParams.m_depth = circleRule.m_depth;
  circleParams.m_position = pos;
  circleParams.m_pivot = pt;
  circleParams.m_ci = ci;
  circleParams.m_userInfo = ToUserInfo(GetCurrentFeatureID());

  m_pScreen->drawCircledSymbol(symParams, circleParams);
}

void GPUDrawer::DrawPath(di::PathInfo const & path, di::DrawRule const * rules, size_t count)
{
  // if any rule needs caching - cache as a whole vector
  bool flag = false;
  for (size_t i = 0; i < count; ++i)
  {
    if (rules[i].GetID(m_pScreen->threadSlot()) == drule::BaseRule::empty_id)
    {
      flag = true;
      break;
    }
  }

  buffer_vector<graphics::Pen::Info, 8> penInfos(count);
  buffer_vector<graphics::Resource::Info const*, 8> infos(count);
  buffer_vector<uint32_t, 8> resIDs(count);

  if (flag)
  {
    // collect graphics::PenInfo into array and pack them as a whole
    for (size_t i = 0; i < count; ++i)
    {
      ConvertStyle(rules[i].m_rule->GetLine(), VisualScale(), penInfos[i]);

      infos[i] = &penInfos[i];

      resIDs[i] = m_pScreen->invalidHandle();
    }

    // map array of pens
    if (m_pScreen->mapInfo(&infos[0], &resIDs[0], count))
    {
      for (size_t i = 0; i < count; ++i)
        rules[i].SetID(ThreadSlot(), resIDs[i]);
    }
    else
    {
      buffer_vector<m2::PointU, 8> rects;
      for (unsigned i = 0; i < count; ++i)
        rects.push_back(infos[i]->resourceSize());
      LOG(LERROR, ("couldn't successfully pack a sequence of path styles as a whole:" , rects));

      return;
    }
  }

  // draw path with array of rules
  for (size_t i = 0; i < count; ++i)
    m_pScreen->drawPath(&path.m_path[0],
                        path.m_path.size(),
                        -path.GetOffset(),
                        rules[i].GetID(ThreadSlot()),
                        rules[i].m_depth);
}

void GPUDrawer::DrawArea(di::AreaInfo const & area, di::DrawRule const & rule)
{
  // DO NOT cache 'id' in pRule, because one rule can use in drawPath and drawArea.
  // Leave CBaseRule::m_id for drawPath. mapColor working fast enough.

  graphics::Brush::Info info;
  ConvertStyle(rule.m_rule->GetArea(), info);

  uint32_t const id = m_pScreen->mapInfo(info);
  ASSERT ( id != -1, () );

  m_pScreen->drawTrianglesList(&area.m_path[0], area.m_path.size(), id, rule.m_depth);
}

void GPUDrawer::DrawText(m2::PointD const & pt,
                         graphics::EPosition pos,
                         di::FeatureStyler const & fs,
                         di::DrawRule const & rule)
{
  graphics::FontDesc primaryFont;
  m2::PointD primaryOffset;
  ConvertStyle(rule.m_rule->GetCaption(0), VisualScale(), primaryFont, primaryOffset);
  primaryFont.SetRank(fs.m_popRank);

  graphics::FontDesc secondaryFont;
  m2::PointD secondaryOffset;
  if (rule.m_rule->GetCaption(1))
  {
    ConvertStyle(rule.m_rule->GetCaption(1), VisualScale(), secondaryFont, secondaryOffset);
    secondaryFont.SetRank(fs.m_popRank);
  }

  graphics::StraightTextElement::Params params;
  params.m_depth = rule.m_depth;
  params.m_fontDesc = primaryFont;
  params.m_auxFontDesc = secondaryFont;
  params.m_offset = primaryOffset;
  params.m_log2vis = true;
  params.m_pivot = pt;
  params.m_position = pos;
  params.m_logText = strings::MakeUniString(fs.m_primaryText);
  params.m_auxLogText = strings::MakeUniString(fs.m_secondaryText);
  params.m_doSplit = true;
  params.m_useAllParts = false;
  params.m_userInfo = ToUserInfo(GetCurrentFeatureID());

  m_pScreen->drawTextEx(params);
}

void GPUDrawer::DrawPathText(di::PathInfo const & path,
                             di::FeatureStyler const & fs,
                             di::DrawRule const & rule)
{
  graphics::FontDesc font;
  m2::PointD offset;
  ConvertStyle(rule.m_rule->GetCaption(0), VisualScale(), font, offset);

  if (fs.m_offsets.empty())
    return;

  m_pScreen->drawPathText(font,
                          &path.m_path[0],
                          path.m_path.size(),
                          fs.GetPathName(),
                          path.GetFullLength(),
                          path.GetOffset(),
                          &fs.m_offsets[0],
                          fs.m_offsets.size(),
                          rule.m_depth);
}

void GPUDrawer::DrawPathNumber(di::PathInfo const & path,
                               di::FeatureStyler const & fs)
{
  GenerateRoadNumbers(path, fs, [this](m2::PointD const & pt, graphics::FontDesc const & font, string const & text)
  {
    m_pScreen->drawText(font, pt, graphics::EPosCenter, text, 0, true /*log2vis*/);
  });
}

namespace
{
  struct DoMakeInvalidRule
  {
    size_t m_threadSlot;
    uint32_t m_pipelineIDMask;

    DoMakeInvalidRule(size_t threadSlot, uint8_t pipelineID)
      : m_threadSlot(threadSlot), m_pipelineIDMask(pipelineID << 24)
    {}

    void operator() (int, int, int, drule::BaseRule * p)
    {
      if ((p->GetID(m_threadSlot) & 0xFF000000) == m_pipelineIDMask)
        p->MakeEmptyID(m_threadSlot);
    }
  };
}


void GPUDrawer::ClearResourceCache(size_t threadSlot, uint8_t pipelineID)
{
  drule::rules().ForEachRule(DoMakeInvalidRule(threadSlot, pipelineID));
}


graphics::Screen * GPUDrawer::GetScreen(Drawer * drawer)
{
  ASSERT(dynamic_cast<GPUDrawer *>(drawer) != nullptr, ());
  return static_cast<GPUDrawer *>(drawer)->Screen();
}