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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
#include "../base/SRC_FIRST.hpp"
#include "coverage_generator.hpp"
#include "screen_coverage.hpp"
#include "tile_renderer.hpp"
#include "tile_set.hpp"
#include "../graphics/skin.hpp"
#include "../graphics/opengl/gl_render_context.hpp"
#include "../base/logging.hpp"
#include "../std/bind.hpp"
bool g_coverageGeneratorDestroyed = false;
CoverageGenerator::CoverageGenerator(
string const & skinName,
TileRenderer * tileRenderer,
shared_ptr<WindowHandle> const & windowHandle,
shared_ptr<graphics::RenderContext> const & primaryRC,
shared_ptr<graphics::ResourceManager> const & rm,
graphics::PacketsQueue * glQueue,
RenderPolicy::TCountryNameFn countryNameFn)
: m_queue(1),
m_tileRenderer(tileRenderer),
m_sequenceID(0),
m_windowHandle(windowHandle),
m_countryNameFn(countryNameFn),
m_glQueue(glQueue),
m_skinName(skinName),
m_fenceManager(2),
m_currentFenceID(-1),
m_doForceUpdate(false),
m_isPaused(false)
{
g_coverageGeneratorDestroyed = false;
m_resourceManager = rm;
if (!m_glQueue)
m_renderContext = primaryRC->createShared();
m_workCoverage = CreateCoverage();
m_currentCoverage = CreateCoverage();
m_queue.AddInitCommand(bind(&CoverageGenerator::InitializeThreadGL, this));
m_queue.AddFinCommand(bind(&CoverageGenerator::FinalizeThreadGL, this));
m_queue.Start();
}
ScreenCoverage * CoverageGenerator::CreateCoverage()
{
graphics::Screen::Params params;
params.m_resourceManager = m_resourceManager;
params.m_renderQueue = m_glQueue;
params.m_doUnbindRT = false;
params.m_isSynchronized = false;
params.m_glyphCacheID = m_resourceManager->cacheThreadGlyphCacheID();
shared_ptr<graphics::Screen> screen(new graphics::Screen(params));
shared_ptr<graphics::Skin> skin(loadSkin(m_resourceManager, m_skinName));
screen->setSkin(skin);
return new ScreenCoverage(m_tileRenderer, this, screen);
}
void CoverageGenerator::InitializeThreadGL()
{
if (m_renderContext)
m_renderContext->makeCurrent();
}
void CoverageGenerator::FinalizeThreadGL()
{
if (m_renderContext)
m_renderContext->endThreadDrawing();
}
CoverageGenerator::~CoverageGenerator()
{
LOG(LINFO, ("cancelling coverage thread"));
Cancel();
LOG(LINFO, ("deleting workCoverage"));
delete m_workCoverage;
m_workCoverage = 0;
LOG(LINFO, ("deleting currentCoverage"));
delete m_currentCoverage;
m_currentCoverage = 0;
g_coverageGeneratorDestroyed = true;
}
void CoverageGenerator::Cancel()
{
m_queue.Cancel();
}
void CoverageGenerator::InvalidateTilesImpl(m2::AnyRectD const & r, int startScale)
{
{
threads::MutexGuard g(m_mutex);
m_currentCoverage->RemoveTiles(r, startScale);
}
TileCache & tileCache = m_tileRenderer->GetTileCache();
tileCache.Lock();
/// here we should copy elements as we've delete some of them later
set<Tiler::RectInfo> k = tileCache.Keys();
for (set<Tiler::RectInfo>::const_iterator it = k.begin(); it != k.end(); ++it)
{
Tiler::RectInfo const & ri = *it;
if ((ri.m_tileScale >= startScale) && r.IsIntersect(m2::AnyRectD(ri.m_rect)))
{
ASSERT(tileCache.LockCount(ri) == 0, ());
tileCache.Remove(ri);
}
}
tileCache.Unlock();
}
void CoverageGenerator::InvalidateTiles(m2::AnyRectD const & r, int startScale)
{
if (m_sequenceID == numeric_limits<int>::max())
return;
/// this automatically will skip the previous CoverScreen commands
/// and MergeTiles commands from previously generated ScreenCoverages
++m_sequenceID;
m_queue.AddCommand(bind(&CoverageGenerator::InvalidateTilesImpl, this, r, startScale));
}
void CoverageGenerator::AddCoverScreenTask(ScreenBase const & screen, bool doForce)
{
if ((screen == m_currentScreen) && (!doForce))
return;
if (m_sequenceID == numeric_limits<int>::max())
return;
m_currentScreen = screen;
++m_sequenceID;
m_queue.AddCommand(bind(&CoverageGenerator::CoverScreen, this, _1, screen, m_sequenceID));
}
int CoverageGenerator::InsertBenchmarkFence()
{
m_currentFenceID = m_fenceManager.insertFence();
return m_currentFenceID;
}
void CoverageGenerator::JoinBenchmarkFence(int fenceID)
{
CHECK(fenceID == m_currentFenceID, ("InsertBenchmarkFence without corresponding SignalBenchmarkFence detected"));
m_fenceManager.joinFence(fenceID);
}
void CoverageGenerator::SignalBenchmarkFence()
{
if (m_currentFenceID != -1)
m_fenceManager.signalFence(m_currentFenceID);
}
void CoverageGenerator::CoverScreen(core::CommandsQueue::Environment const & env,
ScreenBase const & screen,
int sequenceID)
{
if (sequenceID < m_sequenceID)
return;
m_currentCoverage->CopyInto(*m_workCoverage);
m_workCoverage->SetSequenceID(sequenceID);
m_workCoverage->SetScreen(screen);
if (!m_workCoverage->IsPartialCoverage() && m_workCoverage->IsEmptyDrawingCoverage())
{
m_workCoverage->ResetEmptyModelAtCoverageCenter();
AddCheckEmptyModelTask(sequenceID);
}
bool shouldSwap = !m_isPaused && m_workCoverage->Cache(env);
if (shouldSwap)
{
threads::MutexGuard g(m_mutex);
swap(m_currentCoverage, m_workCoverage);
}
else
{
/// we should skip all the following MergeTile commands
++m_sequenceID;
}
m_doForceUpdate = !shouldSwap;
m_workCoverage->Clear();
m_windowHandle->invalidate();
}
void CoverageGenerator::AddMergeTileTask(Tiler::RectInfo const & rectInfo,
int sequenceID)
{
if (g_coverageGeneratorDestroyed)
return;
m_queue.AddCommand(bind(&CoverageGenerator::MergeTile, this, _1, rectInfo, sequenceID));
}
void CoverageGenerator::MergeTile(core::CommandsQueue::Environment const & env,
Tiler::RectInfo const & rectInfo,
int sequenceID)
{
if (sequenceID < m_sequenceID)
{
m_tileRenderer->RemoveActiveTile(rectInfo);
return;
}
m_currentCoverage->CopyInto(*m_workCoverage);
m_workCoverage->SetSequenceID(sequenceID);
m_workCoverage->Merge(rectInfo);
if (!m_workCoverage->IsPartialCoverage() && m_workCoverage->IsEmptyDrawingCoverage())
{
m_workCoverage->ResetEmptyModelAtCoverageCenter();
AddCheckEmptyModelTask(sequenceID);
}
bool shouldSwap = !m_isPaused && m_workCoverage->Cache(env);
if (shouldSwap)
{
threads::MutexGuard g(m_mutex);
swap(m_currentCoverage, m_workCoverage);
}
m_doForceUpdate = !shouldSwap;
m_workCoverage->Clear();
m_windowHandle->invalidate();
}
void CoverageGenerator::AddCheckEmptyModelTask(int sequenceID)
{
m_queue.AddCommand(bind(&CoverageGenerator::CheckEmptyModel, this, sequenceID));
}
void CoverageGenerator::CheckEmptyModel(int sequenceID)
{
if (sequenceID < m_sequenceID)
return;
m_currentCoverage->CheckEmptyModelAtCoverageCenter();
m_windowHandle->invalidate();
}
void CoverageGenerator::AddFinishSequenceTask(int sequenceID)
{
if (g_coverageGeneratorDestroyed)
return;
m_queue.AddCommand(bind(&CoverageGenerator::FinishSequence, this, sequenceID));
}
void CoverageGenerator::FinishSequence(int sequenceID)
{
/* if (sequenceID < m_sequenceID)
{
LOG(LINFO, ("sequence", sequenceID, "was cancelled"));
return;
}*/
SignalBenchmarkFence();
}
void CoverageGenerator::WaitForEmptyAndFinished()
{
m_queue.Join();
}
ScreenCoverage & CoverageGenerator::CurrentCoverage()
{
return *m_currentCoverage;
}
threads::Mutex & CoverageGenerator::Mutex()
{
return m_mutex;
}
void CoverageGenerator::SetSequenceID(int sequenceID)
{
m_sequenceID = sequenceID;
}
shared_ptr<graphics::ResourceManager> const & CoverageGenerator::resourceManager() const
{
return m_resourceManager;
}
string CoverageGenerator::GetCountryName(m2::PointD const & pt) const
{
return m_countryNameFn(pt);
}
void CoverageGenerator::CancelCommands()
{
m_queue.CancelCommands();
}
void CoverageGenerator::SetIsPaused(bool flag)
{
m_isPaused = flag;
}
bool CoverageGenerator::DoForceUpdate() const
{
return m_doForceUpdate;
}
|