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.
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"
9 namespace android_webview
{
13 base::LazyInstance
<GlobalTileManager
>::Leaky g_tile_manager
=
14 LAZY_INSTANCE_INITIALIZER
;
16 // The soft limit of the number of file descriptors per process is 1024 on
17 // Android and gralloc buffers may not be the only thing that uses file
18 // descriptors. For each tile, there is a gralloc buffer backing it, which
20 const size_t kNumTilesLimit
= 450;
25 GlobalTileManager
* GlobalTileManager::GetInstance() {
26 return g_tile_manager
.Pointer();
29 void GlobalTileManager::Remove(Key key
) {
30 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
31 DCHECK(mru_list_
.end() != key
);
33 total_allocated_tiles_
-= (*key
)->GetNumTiles();
35 DCHECK(IsConsistent());
38 size_t GlobalTileManager::Evict(size_t desired_num_tiles
, Key key
) {
39 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
40 size_t total_evicted_tiles
= 0;
42 // Evicts from the least recent drawn view, until the disired number of tiles
43 // can be reclaimed, or until we've evicted all inactive views.
44 ListType::reverse_iterator it
;
45 for (it
= mru_list_
.rbegin(); it
!= mru_list_
.rend(); it
++) {
46 // key represents the view that requested the eviction, so we don't need to
47 // evict the requester itself. And we only evict the inactive views,
48 // which are all the views after the requester.
52 size_t evicted_tiles
= (*it
)->GetNumTiles();
53 (*it
)->SetNumTiles(0, true);
55 total_evicted_tiles
+= evicted_tiles
;
56 if (total_evicted_tiles
>= desired_num_tiles
)
60 return total_evicted_tiles
;
63 void GlobalTileManager::SetTileLimit(size_t num_tiles_limit
) {
64 num_tiles_limit_
= num_tiles_limit
;
67 void GlobalTileManager::RequestTiles(size_t new_num_of_tiles
, Key key
) {
68 DCHECK(IsConsistent());
69 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
70 size_t old_num_of_tiles
= (*key
)->GetNumTiles();
71 size_t num_of_active_views
= std::distance(mru_list_
.begin(), key
) + 1;
72 size_t tiles_per_view_limit
;
73 if (num_of_active_views
== 0)
74 tiles_per_view_limit
= num_tiles_limit_
;
76 tiles_per_view_limit
= num_tiles_limit_
/ num_of_active_views
;
77 new_num_of_tiles
= std::min(new_num_of_tiles
, tiles_per_view_limit
);
78 size_t new_total_allocated_tiles
=
79 total_allocated_tiles_
- old_num_of_tiles
+ new_num_of_tiles
;
80 // Has enough tiles to satisfy the request.
81 if (new_total_allocated_tiles
<= num_tiles_limit_
) {
82 total_allocated_tiles_
= new_total_allocated_tiles
;
83 (*key
)->SetNumTiles(new_num_of_tiles
, false);
87 // Does not have enough tiles. Now evict other clients' tiles.
88 size_t tiles_left
= num_tiles_limit_
- total_allocated_tiles_
;
90 size_t evicted_tiles
=
91 Evict(new_total_allocated_tiles
- num_tiles_limit_
, key
);
92 if (evicted_tiles
>= new_total_allocated_tiles
- num_tiles_limit_
) {
93 new_total_allocated_tiles
-= evicted_tiles
;
94 total_allocated_tiles_
= new_total_allocated_tiles
;
95 (*key
)->SetNumTiles(new_num_of_tiles
, false);
98 total_allocated_tiles_
= num_tiles_limit_
;
99 (*key
)->SetNumTiles(tiles_left
+ old_num_of_tiles
+ evicted_tiles
, false);
104 GlobalTileManager::Key
GlobalTileManager::PushBack(
105 GlobalTileManagerClient
* client
) {
106 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
107 DCHECK(mru_list_
.end() ==
108 std::find(mru_list_
.begin(), mru_list_
.end(), client
));
109 mru_list_
.push_back(client
);
110 Key back
= mru_list_
.end();
115 void GlobalTileManager::DidUse(Key key
) {
116 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
117 DCHECK(mru_list_
.end() != key
);
119 mru_list_
.splice(mru_list_
.begin(), mru_list_
, key
);
122 GlobalTileManager::GlobalTileManager()
123 : num_tiles_limit_(kNumTilesLimit
), total_allocated_tiles_(0) {
126 GlobalTileManager::~GlobalTileManager() {
129 bool GlobalTileManager::IsConsistent() const {
130 size_t total_tiles
= 0;
131 ListType::const_iterator it
;
132 for (it
= mru_list_
.begin(); it
!= mru_list_
.end(); it
++) {
133 total_tiles
+= (*it
)->GetNumTiles();
136 bool is_consistent
= (total_tiles
<= num_tiles_limit_
&&
137 total_tiles
== total_allocated_tiles_
);
139 return is_consistent
;
142 } // namespace webview