forked from organicmaps/organicmaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_manager.cpp
executable file
·384 lines (326 loc) · 11 KB
/
read_manager.cpp
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "drape_frontend/read_manager.hpp"
#include "drape_frontend/message_subclasses.hpp"
#include "drape_frontend/metaline_manager.hpp"
#include "drape_frontend/visual_params.hpp"
#include "drape/constants.hpp"
#include "base/buffer_vector.hpp"
#include "base/stl_helpers.hpp"
#include <algorithm>
#include <functional>
namespace df
{
namespace
{
struct LessCoverageCell
{
bool operator()(std::shared_ptr<TileInfo> const & l,
TileKey const & r) const
{
return l->GetTileKey() < r;
}
bool operator()(TileKey const & l,
std::shared_ptr<TileInfo> const & r) const
{
return l < r->GetTileKey();
}
bool operator()(std::shared_ptr<TileInfo> const & l,
std::shared_ptr<TileInfo> const & r) const
{
return l->GetTileKey() < r->GetTileKey();
}
};
} // namespace
bool ReadManager::LessByTileInfo::operator()(std::shared_ptr<TileInfo> const & l,
std::shared_ptr<TileInfo> const & r) const
{
return *l < *r;
}
ReadManager::ReadManager(ref_ptr<ThreadsCommutator> commutator, MapDataProvider & model,
bool allow3dBuildings, bool trafficEnabled, bool isolinesEnabled)
: m_commutator(commutator)
, m_model(model)
, m_have3dBuildings(false)
, m_allow3dBuildings(allow3dBuildings)
, m_trafficEnabled(trafficEnabled)
, m_isolinesEnabled(isolinesEnabled)
, m_modeChanged(false)
, m_tasksPool(64, ReadMWMTaskFactory(m_model))
, m_counter(0)
, m_generationCounter(0)
, m_userMarksGenerationCounter(0)
{
Start();
}
void ReadManager::Start()
{
if (m_pool != nullptr)
return;
using namespace std::placeholders;
m_pool = make_unique_dp<base::thread_pool::routine::ThreadPool>(kReadingThreadsCount,
std::bind(&ReadManager::OnTaskFinished, this, _1));
}
void ReadManager::Stop()
{
InvalidateAll();
if (m_pool != nullptr)
m_pool->Stop();
m_pool.reset();
}
void ReadManager::Restart()
{
Stop();
Start();
}
void ReadManager::OnTaskFinished(threads::IRoutine * task)
{
ASSERT(dynamic_cast<ReadMWMTask *>(task) != NULL, ());
auto t = static_cast<ReadMWMTask *>(task);
// finish tiles
{
std::lock_guard<std::mutex> lock(m_finishedTilesMutex);
// decrement counter
ASSERT(m_counter > 0, ());
--m_counter;
if (m_counter == 0)
{
m_commutator->PostMessage(ThreadsCommutator::ResourceUploadThread,
make_unique_dp<FinishReadingMessage>(),
MessagePriority::Normal);
}
if (!task->IsCancelled())
{
auto const it = m_activeTiles.find(t->GetTileKey());
ASSERT(it != m_activeTiles.end(), ());
// Use the tile key from active tiles with the actual user marks generation.
auto const tileKey = *it;
TTilesCollection tiles;
tiles.emplace(tileKey);
m_activeTiles.erase(it);
m_commutator->PostMessage(ThreadsCommutator::ResourceUploadThread,
make_unique_dp<FinishTileReadMessage>(std::move(tiles),
true /* forceUpdateUserMarks */),
MessagePriority::Normal);
}
}
t->Reset();
m_tasksPool.Return(t);
}
void ReadManager::UpdateCoverage(ScreenBase const & screen, bool have3dBuildings,
bool forceUpdate, bool forceUpdateUserMarks,
TTilesCollection const & tiles,
ref_ptr<dp::TextureManager> texMng,
ref_ptr<MetalineManager> metalineMng)
{
m_modeChanged |= (m_have3dBuildings != have3dBuildings);
m_have3dBuildings = have3dBuildings;
if (m_modeChanged || forceUpdate || MustDropAllTiles(screen))
{
m_modeChanged = false;
for (auto const & info : m_tileInfos)
CancelTileInfo(info);
m_tileInfos.clear();
IncreaseCounter(static_cast<int>(tiles.size()));
++m_generationCounter;
++m_userMarksGenerationCounter;
for (auto const & tileKey : tiles)
PushTaskBackForTileKey(tileKey, texMng, metalineMng);
}
else
{
// Find rects that go out from viewport.
TTileInfoCollection outdatedTiles;
std::set_difference(m_tileInfos.begin(), m_tileInfos.end(),
tiles.begin(), tiles.end(),
std::back_inserter(outdatedTiles), LessCoverageCell());
for (auto const & info : outdatedTiles)
ClearTileInfo(info);
// Find rects that go in into viewport.
buffer_vector<TileKey, 8> newTiles;
std::set_difference(tiles.begin(), tiles.end(),
m_tileInfos.begin(), m_tileInfos.end(),
std::back_inserter(newTiles), LessCoverageCell());
// Find ready tiles.
TTileInfoCollection readyTiles;
std::set_difference(m_tileInfos.begin(), m_tileInfos.end(),
outdatedTiles.begin(), outdatedTiles.end(),
std::back_inserter(readyTiles), LessCoverageCell());
IncreaseCounter(static_cast<int>(newTiles.size()));
if (forceUpdateUserMarks)
++m_userMarksGenerationCounter;
CheckFinishedTiles(readyTiles, forceUpdateUserMarks);
for (auto const & tileKey : newTiles)
PushTaskBackForTileKey(tileKey, texMng, metalineMng);
}
m_currentViewport = screen;
}
void ReadManager::Invalidate(TTilesCollection const & keyStorage)
{
TTileSet tilesToErase;
for (auto const & info : m_tileInfos)
{
if (keyStorage.find(info->GetTileKey()) != keyStorage.end())
tilesToErase.insert(info);
}
for (auto const & info : tilesToErase)
{
CancelTileInfo(info);
m_tileInfos.erase(info);
}
}
void ReadManager::InvalidateAll()
{
for (auto const & info : m_tileInfos)
CancelTileInfo(info);
m_tileInfos.clear();
m_modeChanged = true;
}
bool ReadManager::CheckTileKey(TileKey const & tileKey) const
{
for (auto const & tileInfo : m_tileInfos)
{
if (tileInfo->GetTileKey() == tileKey)
return !tileInfo->IsCancelled();
}
return false;
}
bool ReadManager::MustDropAllTiles(ScreenBase const & screen) const
{
int const oldScale = df::GetDrawTileScale(m_currentViewport);
int const newScale = df::GetDrawTileScale(screen);
return (oldScale != newScale) || !m_currentViewport.GlobalRect().IsIntersect(screen.GlobalRect());
}
void ReadManager::PushTaskBackForTileKey(TileKey const & tileKey,
ref_ptr<dp::TextureManager> texMng,
ref_ptr<MetalineManager> metalineMng)
{
ASSERT(m_pool != nullptr, ());
auto context = make_unique_dp<EngineContext>(TileKey(tileKey, m_generationCounter,
m_userMarksGenerationCounter),
m_commutator, texMng, metalineMng,
m_customFeaturesContext,
m_have3dBuildings && m_allow3dBuildings,
m_trafficEnabled, m_isolinesEnabled);
std::shared_ptr<TileInfo> tileInfo = std::make_shared<TileInfo>(std::move(context));
m_tileInfos.insert(tileInfo);
ReadMWMTask * task = m_tasksPool.Get();
task->Init(tileInfo);
{
std::lock_guard<std::mutex> lock(m_finishedTilesMutex);
m_activeTiles.insert(TileKey(tileKey, m_generationCounter, m_userMarksGenerationCounter));
}
m_pool->PushBack(task);
}
void ReadManager::CheckFinishedTiles(TTileInfoCollection const & requestedTiles, bool forceUpdateUserMarks)
{
if (requestedTiles.empty())
return;
TTilesCollection finishedTiles;
std::lock_guard<std::mutex> lock(m_finishedTilesMutex);
for (auto const & tile : requestedTiles)
{
auto const it = m_activeTiles.find(tile->GetTileKey());
if (it == m_activeTiles.end())
{
finishedTiles.emplace(tile->GetTileKey(), m_generationCounter, m_userMarksGenerationCounter);
}
else if (forceUpdateUserMarks)
{
// In case of active tile reading update its user marks generation.
// User marks will be generated after finishing of tile reading with correct tile key.
auto tileKey = *it;
tileKey.m_userMarksGeneration = m_userMarksGenerationCounter;
m_activeTiles.erase(it);
m_activeTiles.insert(tileKey);
}
}
if (!finishedTiles.empty())
{
m_commutator->PostMessage(ThreadsCommutator::ResourceUploadThread,
make_unique_dp<FinishTileReadMessage>(std::move(finishedTiles),
forceUpdateUserMarks),
MessagePriority::Normal);
}
}
void ReadManager::CancelTileInfo(std::shared_ptr<TileInfo> const & tileToCancel)
{
std::lock_guard<std::mutex> lock(m_finishedTilesMutex);
m_activeTiles.erase(tileToCancel->GetTileKey());
tileToCancel->Cancel();
}
void ReadManager::ClearTileInfo(std::shared_ptr<TileInfo> const & tileToClear)
{
CancelTileInfo(tileToClear);
m_tileInfos.erase(tileToClear);
}
void ReadManager::IncreaseCounter(int value)
{
std::lock_guard<std::mutex> lock(m_finishedTilesMutex);
m_counter += value;
if (m_counter == 0)
{
m_commutator->PostMessage(ThreadsCommutator::ResourceUploadThread,
make_unique_dp<FinishReadingMessage>(),
MessagePriority::Normal);
}
}
void ReadManager::Allow3dBuildings(bool allow3dBuildings)
{
if (m_allow3dBuildings != allow3dBuildings)
{
m_modeChanged = true;
m_allow3dBuildings = allow3dBuildings;
}
}
void ReadManager::SetTrafficEnabled(bool trafficEnabled)
{
if (m_trafficEnabled != trafficEnabled)
{
m_modeChanged = true;
m_trafficEnabled = trafficEnabled;
}
}
void ReadManager::SetIsolinesEnabled(bool isolinesEnabled)
{
if (m_isolinesEnabled != isolinesEnabled)
{
m_modeChanged = true;
m_isolinesEnabled = isolinesEnabled;
}
}
void ReadManager::SetCustomFeatures(CustomFeatures && ids)
{
m_customFeaturesContext = std::make_shared<CustomFeaturesContext>(std::move(ids));
}
std::vector<FeatureID> ReadManager::GetCustomFeaturesArray() const
{
if (!m_customFeaturesContext)
return {};
std::vector<FeatureID> features;
features.reserve(m_customFeaturesContext->m_features.size());
for (auto const & s : m_customFeaturesContext->m_features)
features.push_back(s.first);
return features;
}
bool ReadManager::RemoveCustomFeatures(MwmSet::MwmId const & mwmId)
{
if (!m_customFeaturesContext)
return false;
CustomFeatures features;
for (auto const & s : m_customFeaturesContext->m_features)
{
if (s.first.m_mwmId != mwmId)
features.insert(s);
}
if (features.size() == m_customFeaturesContext->m_features.size())
return false;
m_customFeaturesContext = std::make_shared<CustomFeaturesContext>(std::move(features));
return true;
}
bool ReadManager::RemoveAllCustomFeatures()
{
if (!m_customFeaturesContext || m_customFeaturesContext->m_features.empty())
return false;
m_customFeaturesContext = std::make_shared<CustomFeaturesContext>(CustomFeatures());
return true;
}
} // namespace df