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/hardware_renderer.h" |
| 7 #include "base/lazy_instance.h" |
| 8 |
| 9 #include "base/logging.h" |
| 10 |
| 11 namespace android_webview { |
| 12 |
| 13 namespace { |
| 14 base::LazyInstance<GlobalTileManager>::Leaky g_tile_manager = |
| 15 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 |
| 19 // uses 2 FDs. |
| 20 const size_t kNumTilesLimit = 450; |
| 21 } // namespace |
| 22 |
| 23 // static |
| 24 GlobalTileManager* GlobalTileManager::GetInstance() { |
| 25 return g_tile_manager.Pointer(); |
| 26 } |
| 27 |
| 28 void GlobalTileManager::DidDrawGL(Key key) { |
| 29 DCHECK(mru_list_.end() != key); |
| 30 |
| 31 inactive_views_ = key; |
| 32 inactive_views_++; |
| 33 mru_list_.splice(mru_list_.begin(), mru_list_, key); |
| 34 } |
| 35 |
| 36 void GlobalTileManager::Remove(Key key) { |
| 37 DCHECK(mru_list_.end() != key); |
| 38 if (key == inactive_views_) |
| 39 inactive_views_++; |
| 40 mru_list_.erase(key); |
| 41 } |
| 42 |
| 43 size_t GlobalTileManager::EvictUntilSatisfied( |
| 44 size_t desired_num_tiles, Key key) { |
| 45 size_t total_evicted_tiles = 0; |
| 46 ListType::reverse_iterator it; |
| 47 |
| 48 // Evicts from the least recent drawn view, until the disired number of tiles |
| 49 // can be reclaimed. |
| 50 for (it = mru_list_.rbegin(); it != mru_list_.rend(); it++) { |
| 51 // key represents the view that requested the eviction, so we don't need to |
| 52 // evict the requester itself. |
| 53 if (*it == *key) |
| 54 continue; |
| 55 |
| 56 size_t evicted_tiles = (*it)->ForceDropTiles(); |
| 57 total_evicted_tiles += evicted_tiles; |
| 58 if (total_evicted_tiles >= desired_num_tiles) { |
| 59 break; |
| 60 } |
| 61 } |
| 62 |
| 63 return total_evicted_tiles; |
| 64 } |
| 65 |
| 66 size_t GlobalTileManager::RequestTiles(size_t old_num_of_tiles, |
| 67 size_t new_num_of_tiles, bool is_starved, Key key) { |
| 68 size_t num_of_active_views = |
| 69 std::distance(mru_list_.begin(), inactive_views_); |
| 70 size_t tiles_per_view_limit; |
| 71 if (num_of_active_views == 0) |
| 72 tiles_per_view_limit = kNumTilesLimit; |
| 73 else |
| 74 tiles_per_view_limit = kNumTilesLimit / num_of_active_views; |
| 75 new_num_of_tiles = std::min(new_num_of_tiles, tiles_per_view_limit); |
| 76 size_t tiles = allocated_tiles_ - old_num_of_tiles + new_num_of_tiles; |
| 77 // Has enough tiles to satisfy the request. |
| 78 if (tiles <= kNumTilesLimit) { |
| 79 allocated_tiles_ = tiles; |
| 80 return new_num_of_tiles; |
| 81 } |
| 82 |
| 83 // Does not have enough tiles, but the request is not starved. Gives what's |
| 84 // left to the requester. |
| 85 if (!is_starved) { |
| 86 size_t tiles_left = kNumTilesLimit - allocated_tiles_; |
| 87 allocated_tiles_ = kNumTilesLimit; |
| 88 return tiles_left + old_num_of_tiles; |
| 89 } |
| 90 |
| 91 // Does not have enough tiles and the requester is starved. |
| 92 size_t evicted_tiles = EvictUntilSatisfied(tiles - kNumTilesLimit, key); |
| 93 tiles -= evicted_tiles; |
| 94 allocated_tiles_ = tiles; |
| 95 return new_num_of_tiles; |
| 96 } |
| 97 |
| 98 GlobalTileManager::Key GlobalTileManager::PushBack( |
| 99 HardwareRenderer* hardware_renderer) { |
| 100 DCHECK(mru_list_.end() == |
| 101 std::find(mru_list_.begin(), mru_list_.end(), hardware_renderer)); |
| 102 mru_list_.push_back(hardware_renderer); |
| 103 Key back = mru_list_.end(); |
| 104 back--; |
| 105 return back; |
| 106 } |
| 107 |
| 108 GlobalTileManager::GlobalTileManager() { |
| 109 allocated_tiles_ = 0; |
| 110 } |
| 111 |
| 112 GlobalTileManager::~GlobalTileManager() {} |
| 113 |
| 114 } // namespace webview |
OLD | NEW |