OLD | NEW |
---|---|
(Empty) | |
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, | |
62 bool effective_immediately, | |
boliu
2014/05/02 20:28:34
You shouldn't need to pass this into Manager. Mana
hush (inactive)
2014/05/02 22:07:40
Done.
| |
63 Key key) { | |
64 DCHECK(IsConsistent()); | |
65 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
66 size_t old_num_of_tiles = (*key)->GetNumTiles(); | |
67 size_t num_of_active_views = std::distance(mru_list_.begin(), key) + 1; | |
68 size_t tiles_per_view_limit; | |
69 if (num_of_active_views == 0) | |
70 tiles_per_view_limit = kNumTilesLimit; | |
71 else | |
72 tiles_per_view_limit = kNumTilesLimit / num_of_active_views; | |
73 new_num_of_tiles = std::min(new_num_of_tiles, tiles_per_view_limit); | |
74 size_t new_total_allocated_tiles = | |
75 total_allocated_tiles_ - old_num_of_tiles + new_num_of_tiles; | |
76 // Has enough tiles to satisfy the request. | |
77 if (new_total_allocated_tiles <= kNumTilesLimit) { | |
78 total_allocated_tiles_ = new_total_allocated_tiles; | |
79 (*key)->SetNumTiles(new_num_of_tiles, effective_immediately); | |
80 return; | |
81 } | |
82 | |
83 // Does not have enough tiles. Now evict other clients' tiles. | |
84 size_t tiles_left = kNumTilesLimit - total_allocated_tiles_; | |
85 | |
86 size_t evicted_tiles = Evict(new_total_allocated_tiles - kNumTilesLimit, key); | |
87 if (evicted_tiles >= new_total_allocated_tiles - kNumTilesLimit) { | |
88 new_total_allocated_tiles -= evicted_tiles; | |
89 total_allocated_tiles_ = new_total_allocated_tiles; | |
90 (*key)->SetNumTiles(new_num_of_tiles, effective_immediately); | |
91 return; | |
92 } else { | |
93 total_allocated_tiles_ = kNumTilesLimit; | |
94 (*key)->SetNumTiles(tiles_left + old_num_of_tiles + evicted_tiles, | |
95 effective_immediately); | |
96 return; | |
97 } | |
98 } | |
99 | |
100 GlobalTileManager::Key GlobalTileManager::PushBack( | |
101 GlobalTileManagerClient* client) { | |
102 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
103 DCHECK(mru_list_.end() == | |
104 std::find(mru_list_.begin(), mru_list_.end(), client)); | |
105 mru_list_.push_back(client); | |
106 Key back = mru_list_.end(); | |
107 back--; | |
108 return back; | |
109 } | |
110 | |
111 void GlobalTileManager::DidUse(Key key) { | |
112 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
113 DCHECK(mru_list_.end() != key); | |
114 | |
115 mru_list_.splice(mru_list_.begin(), mru_list_, key); | |
116 } | |
117 | |
118 GlobalTileManager::GlobalTileManager() { | |
119 total_allocated_tiles_ = 0; | |
120 } | |
121 | |
122 GlobalTileManager::~GlobalTileManager() { | |
123 } | |
124 | |
125 bool GlobalTileManager::IsConsistent() const { | |
126 size_t total_tiles = 0; | |
127 ListType::const_iterator it; | |
128 for (it = mru_list_.begin(); it != mru_list_.end(); it++) { | |
129 total_tiles += (*it)->GetNumTiles(); | |
130 } | |
131 | |
132 bool is_consistent = | |
133 (total_tiles <= kNumTilesLimit && total_tiles == total_allocated_tiles_); | |
134 | |
135 return is_consistent; | |
136 } | |
137 | |
138 } // namespace webview | |
OLD | NEW |