Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / android_webview / browser / global_tile_manager.cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "android_webview/browser/global_tile_manager.h"
6 #include "android_webview/browser/global_tile_manager_client.h"
7 #include "base/lazy_instance.h"
8
9 namespace android_webview {
10
11 namespace {
12 base::LazyInstance<GlobalTileManager>::Leaky g_tile_manager =
13     LAZY_INSTANCE_INITIALIZER;
14 // The soft limit of the number of file descriptors per process is 1024 on
15 // Android and gralloc buffers may not be the only thing that uses file
16 // descriptors. For each tile, there is a gralloc buffer backing it, which
17 // uses 2 FDs.
18 const size_t kNumTilesLimit = 450;
19
20 }  // namespace
21
22 // static
23 GlobalTileManager* GlobalTileManager::GetInstance() {
24   return g_tile_manager.Pointer();
25 }
26
27 void GlobalTileManager::Remove(Key key) {
28   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
29   DCHECK(mru_list_.end() != key);
30
31   total_allocated_tiles_ -= (*key)->GetNumTiles();
32   mru_list_.erase(key);
33   DCHECK(IsConsistent());
34 }
35
36 size_t GlobalTileManager::Evict(size_t desired_num_tiles, Key key) {
37   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
38   size_t total_evicted_tiles = 0;
39
40   // Evicts from the least recent drawn view, until the disired number of tiles
41   // can be reclaimed, or until we've evicted all inactive views.
42   ListType::reverse_iterator it;
43   for (it = mru_list_.rbegin(); it != mru_list_.rend(); it++) {
44     // key represents the view that requested the eviction, so we don't need to
45     // evict the requester itself. And we only evict the inactive views,
46     // which are all the views after the requester.
47     if (*it == *key)
48       break;
49
50     size_t evicted_tiles = (*it)->GetNumTiles();
51     (*it)->SetNumTiles(0, true);
52
53     total_evicted_tiles += evicted_tiles;
54     if (total_evicted_tiles >= desired_num_tiles)
55       break;
56   }
57
58   return total_evicted_tiles;
59 }
60
61 void GlobalTileManager::RequestTiles(size_t new_num_of_tiles, Key key) {
62   DCHECK(IsConsistent());
63   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
64   size_t old_num_of_tiles = (*key)->GetNumTiles();
65   size_t num_of_active_views = std::distance(mru_list_.begin(), key) + 1;
66   size_t tiles_per_view_limit;
67   if (num_of_active_views == 0)
68     tiles_per_view_limit = kNumTilesLimit;
69   else
70     tiles_per_view_limit = kNumTilesLimit / num_of_active_views;
71   new_num_of_tiles = std::min(new_num_of_tiles, tiles_per_view_limit);
72   size_t new_total_allocated_tiles =
73       total_allocated_tiles_ - old_num_of_tiles + new_num_of_tiles;
74   // Has enough tiles to satisfy the request.
75   if (new_total_allocated_tiles <= kNumTilesLimit) {
76     total_allocated_tiles_ = new_total_allocated_tiles;
77     (*key)->SetNumTiles(new_num_of_tiles, false);
78     return;
79   }
80
81   // Does not have enough tiles. Now evict other clients' tiles.
82   size_t tiles_left = kNumTilesLimit - total_allocated_tiles_;
83
84   size_t evicted_tiles = Evict(new_total_allocated_tiles - kNumTilesLimit, key);
85   if (evicted_tiles >= new_total_allocated_tiles - kNumTilesLimit) {
86     new_total_allocated_tiles -= evicted_tiles;
87     total_allocated_tiles_ = new_total_allocated_tiles;
88     (*key)->SetNumTiles(new_num_of_tiles, false);
89     return;
90   } else {
91     total_allocated_tiles_ = kNumTilesLimit;
92     (*key)->SetNumTiles(tiles_left + old_num_of_tiles + evicted_tiles, false);
93     return;
94   }
95 }
96
97 GlobalTileManager::Key GlobalTileManager::PushBack(
98     GlobalTileManagerClient* client) {
99   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
100   DCHECK(mru_list_.end() ==
101          std::find(mru_list_.begin(), mru_list_.end(), client));
102   mru_list_.push_back(client);
103   Key back = mru_list_.end();
104   back--;
105   return back;
106 }
107
108 void GlobalTileManager::DidUse(Key key) {
109   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
110   DCHECK(mru_list_.end() != key);
111
112   mru_list_.splice(mru_list_.begin(), mru_list_, key);
113 }
114
115 GlobalTileManager::GlobalTileManager() {
116   total_allocated_tiles_ = 0;
117 }
118
119 GlobalTileManager::~GlobalTileManager() {
120 }
121
122 bool GlobalTileManager::IsConsistent() const {
123   size_t total_tiles = 0;
124   ListType::const_iterator it;
125   for (it = mru_list_.begin(); it != mru_list_.end(); it++) {
126     total_tiles += (*it)->GetNumTiles();
127   }
128
129   bool is_consistent =
130       (total_tiles <= kNumTilesLimit && total_tiles == total_allocated_tiles_);
131
132   return is_consistent;
133 }
134
135 }  // namespace webview