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
{
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
18 const size_t kNumTilesLimit
= 450;
23 GlobalTileManager
* GlobalTileManager::GetInstance() {
24 return g_tile_manager
.Pointer();
27 void GlobalTileManager::Remove(Key key
) {
28 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
29 DCHECK(mru_list_
.end() != key
);
31 total_allocated_tiles_
-= (*key
)->GetNumTiles();
33 DCHECK(IsConsistent());
36 size_t GlobalTileManager::Evict(size_t desired_num_tiles
, Key key
) {
37 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
38 size_t total_evicted_tiles
= 0;
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.
50 size_t evicted_tiles
= (*it
)->GetNumTiles();
51 (*it
)->SetNumTiles(0, true);
53 total_evicted_tiles
+= evicted_tiles
;
54 if (total_evicted_tiles
>= desired_num_tiles
)
58 return total_evicted_tiles
;
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
;
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);
81 // Does not have enough tiles. Now evict other clients' tiles.
82 size_t tiles_left
= kNumTilesLimit
- total_allocated_tiles_
;
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);
91 total_allocated_tiles_
= kNumTilesLimit
;
92 (*key
)->SetNumTiles(tiles_left
+ old_num_of_tiles
+ evicted_tiles
, false);
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();
108 void GlobalTileManager::DidUse(Key key
) {
109 DCHECK(sequence_checker_
.CalledOnValidSequencedThread());
110 DCHECK(mru_list_
.end() != key
);
112 mru_list_
.splice(mru_list_
.begin(), mru_list_
, key
);
115 GlobalTileManager::GlobalTileManager() {
116 total_allocated_tiles_
= 0;
119 GlobalTileManager::~GlobalTileManager() {
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();
130 (total_tiles
<= kNumTilesLimit
&& total_tiles
== total_allocated_tiles_
);
132 return is_consistent
;
135 } // namespace webview