Chromium Code Reviews| 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 | |
| 15 // The soft limit of the number of file descriptors per process is 1024 on | |
| 16 // Android and gralloc buffers may not be the only thing that uses file | |
| 17 // descriptors. For each tile, there is a gralloc buffer backing it, which | |
| 18 // uses 2 FDs. | |
| 19 const size_t kNumTilesLimit = 450; | |
| 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(mru_list_.end() != key); | |
| 29 if (key == inactive_views_) | |
| 30 inactive_views_++; | |
| 31 mru_list_.erase(key); | |
| 32 } | |
| 33 | |
| 34 size_t GlobalTileManager::Evict(size_t desired_num_tiles, Key key) { | |
| 35 size_t total_evicted_tiles = 0; | |
| 36 | |
| 37 // Evicts from the least recent drawn view, until the disired number of tiles | |
| 38 // can be reclaimed, or until we've evicted all inactive views. | |
| 39 for (Key it = inactive_views_; it != mru_list_.end(); it++) { | |
|
boliu
2014/05/01 00:37:40
No...I was wrong.
You should use rbegin, but *sto
hush (inactive)
2014/05/01 19:03:04
I will backwards traverse from the end of the list
| |
| 40 // key represents the view that requested the eviction, so we don't need to | |
| 41 // evict the requester itself. | |
| 42 if (*it == *key) | |
| 43 continue; | |
| 44 | |
| 45 size_t evicted_tiles = (*it)->ForceDropTiles(); | |
| 46 total_evicted_tiles += evicted_tiles; | |
| 47 if (total_evicted_tiles >= desired_num_tiles) | |
| 48 break; | |
| 49 } | |
| 50 | |
| 51 return total_evicted_tiles; | |
| 52 } | |
| 53 | |
| 54 size_t GlobalTileManager::RequestTiles(size_t old_num_of_tiles, | |
| 55 size_t new_num_of_tiles, | |
| 56 bool is_starved, | |
|
boliu
2014/05/01 00:37:40
var name mismatch with header, so are you keeping
hush (inactive)
2014/05/01 19:03:04
I will change the bool name to needs_eviction
On 2
| |
| 57 Key key) { | |
| 58 DCHECK(CheckConsistency()); | |
| 59 size_t num_of_active_views = | |
| 60 std::distance(mru_list_.begin(), inactive_views_); | |
| 61 size_t tiles_per_view_limit; | |
| 62 if (num_of_active_views == 0) | |
| 63 tiles_per_view_limit = kNumTilesLimit; | |
| 64 else | |
| 65 tiles_per_view_limit = kNumTilesLimit / num_of_active_views; | |
| 66 new_num_of_tiles = std::min(new_num_of_tiles, tiles_per_view_limit); | |
| 67 size_t new_total_allocated_tiles = | |
| 68 total_allocated_tiles_ - old_num_of_tiles + new_num_of_tiles; | |
| 69 // Has enough tiles to satisfy the request. | |
| 70 if (new_total_allocated_tiles <= kNumTilesLimit) { | |
| 71 total_allocated_tiles_ = new_total_allocated_tiles; | |
| 72 return new_num_of_tiles; | |
| 73 } | |
| 74 | |
| 75 // Does not have enough tiles, but the request is not starved. Gives what's | |
| 76 // left to the requester. | |
| 77 if (!is_starved) { | |
| 78 size_t tiles_left = kNumTilesLimit - total_allocated_tiles_; | |
| 79 total_allocated_tiles_ = kNumTilesLimit; | |
| 80 return tiles_left + old_num_of_tiles; | |
| 81 } | |
| 82 | |
| 83 // Does not have enough tiles and the requester is starved. | |
| 84 size_t evicted_tiles = Evict(new_total_allocated_tiles - kNumTilesLimit, key); | |
| 85 new_total_allocated_tiles -= evicted_tiles; | |
| 86 total_allocated_tiles_ = new_total_allocated_tiles; | |
| 87 return new_num_of_tiles; | |
| 88 } | |
| 89 | |
| 90 GlobalTileManager::Key GlobalTileManager::PushBack( | |
| 91 GlobalTileManagerClient* client) { | |
| 92 DCHECK(mru_list_.end() == | |
| 93 std::find(mru_list_.begin(), mru_list_.end(), client)); | |
| 94 mru_list_.push_back(client); | |
| 95 Key back = mru_list_.end(); | |
| 96 back--; | |
| 97 return back; | |
| 98 } | |
| 99 | |
| 100 void GlobalTileManager::DidUse(Key key) { | |
| 101 DCHECK(mru_list_.end() != key); | |
| 102 | |
| 103 inactive_views_ = key; | |
| 104 inactive_views_++; | |
|
boliu
2014/05/01 00:37:40
Ehh, I thought we could do this without keeping in
hush (inactive)
2014/05/01 19:03:04
Manager needs a pointer to inactive_views_ anyways
| |
| 105 mru_list_.splice(mru_list_.begin(), mru_list_, key); | |
| 106 } | |
| 107 | |
| 108 GlobalTileManager::GlobalTileManager() { | |
| 109 total_allocated_tiles_ = 0; | |
| 110 inactive_views_ = mru_list_.end(); | |
| 111 } | |
| 112 | |
| 113 GlobalTileManager::~GlobalTileManager() { | |
| 114 } | |
| 115 | |
| 116 bool GlobalTileManager::CheckConsistency() const { | |
| 117 size_t total_tiles = 0; | |
| 118 ListType::const_iterator it; | |
| 119 for (it = mru_list_.begin(); it != mru_list_.end(); it++) { | |
| 120 total_tiles += (*it)->GetTilesNum(); | |
| 121 } | |
| 122 | |
| 123 return total_tiles == total_allocated_tiles_; | |
| 124 } | |
| 125 | |
| 126 } // namespace webview | |
| OLD | NEW |