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

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

#include "../platform/platform.hpp"

#include "../base/buffer_vector.hpp"
#include "../base/stl_add.hpp"

#include "../std/bind.hpp"
#include "../std/algorithm.hpp"

namespace df
{

namespace
{

struct LessCoverageCell
{
  bool operator()(shared_ptr<TileInfo> const & l, TileKey const & r) const
  {
    return l->GetTileKey() < r;
  }

  bool operator()(TileKey const & l, shared_ptr<TileInfo> const & r) const
  {
    return l < r->GetTileKey();
  }
};

} // namespace

ReadManager::ReadManager(EngineContext & context, MapDataProvider & model)
  : m_context(context)
  , m_model(model)
  , myPool(64, ReadMWMTaskFactory(m_memIndex, m_model, m_context))
{
  m_pool.Reset(new threads::ThreadPool(ReadCount(), bind(&ReadManager::OnTaskFinished, this, _1)));
}

void ReadManager::OnTaskFinished(threads::IRoutine * task)
{
  ASSERT(dynamic_cast<ReadMWMTask *>(task) != NULL, ());
  ReadMWMTask * t = static_cast<ReadMWMTask *>(task);
  t->Reset();
  myPool.Return(t);
}

void ReadManager::UpdateCoverage(ScreenBase const & screen, set<TileKey> const & tiles)
{
  if (screen == m_currentViewport)
    return;

  if (MustDropAllTiles(screen))
  {
    for_each(m_tileInfos.begin(), m_tileInfos.end(), bind(&ReadManager::CancelTileInfo, this, _1));
    m_tileInfos.clear();

    for_each(tiles.begin(), tiles.end(), bind(&ReadManager::PushTaskBackForTileKey, this, _1));
  }
  else
  {
    // Find rects that go out from viewport
    buffer_vector<tileinfo_ptr, 8> outdatedTiles;
#ifdef _MSC_VER
    vs_bug::
#endif
    set_difference(m_tileInfos.begin(), m_tileInfos.end(),
                   tiles.begin(), tiles.end(),
                   back_inserter(outdatedTiles), LessCoverageCell());

    // Find rects that go in into viewport
    buffer_vector<TileKey, 8> inputRects;
#ifdef _MSC_VER
    vs_bug::
#endif
    set_difference(tiles.begin(), tiles.end(),
                   m_tileInfos.begin(), m_tileInfos.end(),
                   back_inserter(inputRects), LessCoverageCell());

    for_each(outdatedTiles.begin(), outdatedTiles.end(), bind(&ReadManager::ClearTileInfo, this, _1));
    for_each(m_tileInfos.begin(), m_tileInfos.end(), bind(&ReadManager::PushTaskFront, this, _1));
    for_each(inputRects.begin(),  inputRects.end(),  bind(&ReadManager::PushTaskBackForTileKey, this, _1));
  }
  m_currentViewport = screen;
}

void ReadManager::Invalidate(set<TileKey> const & keyStorage)
{
  tile_set_t::iterator it = m_tileInfos.begin();
  for (; it != m_tileInfos.end(); ++it)
  {
    if (keyStorage.find((*it)->GetTileKey()) != keyStorage.end())
    {
      CancelTileInfo(*it);
      PushTaskFront(*it);
    }
  }
}

void ReadManager::Stop()
{
  for_each(m_tileInfos.begin(), m_tileInfos.end(), bind(&ReadManager::CancelTileInfo, this, _1));
  m_tileInfos.clear();

  m_pool->Stop();
  m_pool.Destroy();
}

size_t ReadManager::ReadCount()
{
  return max(GetPlatform().CpuCores() - 2, 1);
}

bool ReadManager::MustDropAllTiles(ScreenBase const & screen) const
{
  int const oldScale = df::GetTileScaleBase(m_currentViewport);
  int const newScale = df::GetTileScaleBase(screen);
  return (oldScale != newScale) || !m_currentViewport.GlobalRect().IsIntersect(screen.GlobalRect());
}

void ReadManager::PushTaskBackForTileKey(TileKey const & tileKey)
{
  tileinfo_ptr tileInfo(new TileInfo(tileKey));
  m_tileInfos.insert(tileInfo);
  ReadMWMTask * task = myPool.Get();
  task->Init(tileInfo);
  m_pool->PushBack(task);
}

void ReadManager::PushTaskFront(tileinfo_ptr const & tileToReread)
{
  ReadMWMTask * task = myPool.Get();
  task->Init(tileToReread);
  m_pool->PushFront(task);
}

void ReadManager::CancelTileInfo(tileinfo_ptr const & tileToCancel)
{
  tileToCancel->Cancel(m_memIndex);
}

void ReadManager::ClearTileInfo(tileinfo_ptr const & tileToClear)
{
  CancelTileInfo(tileToClear);
  m_tileInfos.erase(tileToClear);
}

} // namespace df