| Index: android_webview/browser/global_tile_manager.cc
|
| diff --git a/android_webview/browser/global_tile_manager.cc b/android_webview/browser/global_tile_manager.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6e343fe61213d93c52525ab5a2526dc4ade5d20a
|
| --- /dev/null
|
| +++ b/android_webview/browser/global_tile_manager.cc
|
| @@ -0,0 +1,114 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "android_webview/browser/global_tile_manager.h"
|
| +#include "android_webview/browser/hardware_renderer.h"
|
| +#include "base/lazy_instance.h"
|
| +
|
| +#include "base/logging.h"
|
| +
|
| +namespace android_webview {
|
| +
|
| +namespace {
|
| + base::LazyInstance<GlobalTileManager>::Leaky g_tile_manager =
|
| + LAZY_INSTANCE_INITIALIZER;
|
| + // The soft limit of the number of file descriptors per process is 1024 on
|
| + // Android and gralloc buffers may not be the only thing that uses file
|
| + // descriptors. For each tile, there is a gralloc buffer backing it, which
|
| + // uses 2 FDs.
|
| + const size_t kNumTilesLimit = 450;
|
| +} // namespace
|
| +
|
| +// static
|
| +GlobalTileManager* GlobalTileManager::GetInstance() {
|
| + return g_tile_manager.Pointer();
|
| +}
|
| +
|
| +void GlobalTileManager::DidDrawGL(Key key) {
|
| + DCHECK(mru_list_.end() != key);
|
| +
|
| + inactive_views_ = key;
|
| + inactive_views_++;
|
| + mru_list_.splice(mru_list_.begin(), mru_list_, key);
|
| +}
|
| +
|
| +void GlobalTileManager::Remove(Key key) {
|
| + DCHECK(mru_list_.end() != key);
|
| + if (key == inactive_views_)
|
| + inactive_views_++;
|
| + mru_list_.erase(key);
|
| +}
|
| +
|
| +size_t GlobalTileManager::EvictUntilSatisfied(
|
| + size_t desired_num_tiles, Key key) {
|
| + size_t total_evicted_tiles = 0;
|
| + ListType::reverse_iterator it;
|
| +
|
| + // Evicts from the least recent drawn view, until the disired number of tiles
|
| + // can be reclaimed.
|
| + for (it = mru_list_.rbegin(); it != mru_list_.rend(); it++) {
|
| + // key represents the view that requested the eviction, so we don't need to
|
| + // evict the requester itself.
|
| + if (*it == *key)
|
| + continue;
|
| +
|
| + size_t evicted_tiles = (*it)->ForceDropTiles();
|
| + total_evicted_tiles += evicted_tiles;
|
| + if (total_evicted_tiles >= desired_num_tiles) {
|
| + break;
|
| + }
|
| + }
|
| +
|
| + return total_evicted_tiles;
|
| +}
|
| +
|
| +size_t GlobalTileManager::RequestTiles(size_t old_num_of_tiles,
|
| + size_t new_num_of_tiles, bool is_starved, Key key) {
|
| + size_t num_of_active_views =
|
| + std::distance(mru_list_.begin(), inactive_views_);
|
| + size_t tiles_per_view_limit;
|
| + if (num_of_active_views == 0)
|
| + tiles_per_view_limit = kNumTilesLimit;
|
| + else
|
| + tiles_per_view_limit = kNumTilesLimit / num_of_active_views;
|
| + new_num_of_tiles = std::min(new_num_of_tiles, tiles_per_view_limit);
|
| + size_t tiles = allocated_tiles_ - old_num_of_tiles + new_num_of_tiles;
|
| + // Has enough tiles to satisfy the request.
|
| + if (tiles <= kNumTilesLimit) {
|
| + allocated_tiles_ = tiles;
|
| + return new_num_of_tiles;
|
| + }
|
| +
|
| + // Does not have enough tiles, but the request is not starved. Gives what's
|
| + // left to the requester.
|
| + if (!is_starved) {
|
| + size_t tiles_left = kNumTilesLimit - allocated_tiles_;
|
| + allocated_tiles_ = kNumTilesLimit;
|
| + return tiles_left + old_num_of_tiles;
|
| + }
|
| +
|
| + // Does not have enough tiles and the requester is starved.
|
| + size_t evicted_tiles = EvictUntilSatisfied(tiles - kNumTilesLimit, key);
|
| + tiles -= evicted_tiles;
|
| + allocated_tiles_ = tiles;
|
| + return new_num_of_tiles;
|
| +}
|
| +
|
| +GlobalTileManager::Key GlobalTileManager::PushBack(
|
| + HardwareRenderer* hardware_renderer) {
|
| + DCHECK(mru_list_.end() ==
|
| + std::find(mru_list_.begin(), mru_list_.end(), hardware_renderer));
|
| + mru_list_.push_back(hardware_renderer);
|
| + Key back = mru_list_.end();
|
| + back--;
|
| + return back;
|
| +}
|
| +
|
| +GlobalTileManager::GlobalTileManager() {
|
| + allocated_tiles_ = 0;
|
| +}
|
| +
|
| +GlobalTileManager::~GlobalTileManager() {}
|
| +
|
| +} // namespace webview
|
|
|