Chromium Code Reviews| Index: android_webview/browser/gpu_memory_manager.cc |
| diff --git a/android_webview/browser/gpu_memory_manager.cc b/android_webview/browser/gpu_memory_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..602157f68e87c8a96f15775a6569ef355773b09b |
| --- /dev/null |
| +++ b/android_webview/browser/gpu_memory_manager.cc |
| @@ -0,0 +1,69 @@ |
| +// 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/gl_view_renderer_manager.h" |
| +#include "android_webview/browser/gpu_memory_manager.h" |
| +#include "base/lazy_instance.h" |
| + |
| +#include "base/logging.h" |
| +using base::AutoLock; |
| + |
| +namespace android_webview { |
| + |
| +namespace { |
| + base::LazyInstance<GpuMemoryManager>::Leaky g_gpu_memory_manager = |
| + LAZY_INSTANCE_INITIALIZER; |
| +} // namespace |
| + |
| +// TODO(hush): need to scale up/down this number based on your devide |
| +const size_t GpuMemoryManager::kGpuMemoryLimit = 256 * 1024 * 1024; |
|
boliu
2014/04/08 03:06:45
how about assume the limiting resource is always f
|
| +const size_t GpuMemoryManager::kNumResourcesLimit = 512; |
| + |
| +// static |
| +GpuMemoryManager* GpuMemoryManager::GetInstance() { |
| + return g_gpu_memory_manager.Pointer(); |
| +} |
| + |
| +MemoryPolicy GpuMemoryManager::EvictUntilSatisfied( |
| + MemoryPolicy desired_policy, Key key) { |
| + lock_.AssertAcquired(); |
| + MemoryPolicy total_evicted = |
| + GLViewRendererManager::GetInstance()-> |
| + EvictUntilSatisfied(desired_policy, key); |
| + |
| + return total_evicted; |
| +} |
| + |
| +void GpuMemoryManager::UpdateResources( |
| + MemoryPolicy& old_policy, MemoryPolicy& new_policy, Key key) { |
| + AutoLock auto_lock(lock_); |
| + size_t gpu_memory = allocated_.bytes_limit - old_policy.bytes_limit + |
| + new_policy.bytes_limit; |
| + size_t num_resources = allocated_.num_resources_limit - |
| + old_policy.num_resources_limit + new_policy.num_resources_limit; |
| + |
| + if (gpu_memory > kGpuMemoryLimit || num_resources > kNumResourcesLimit) { |
| + MemoryPolicy memory_to_evict; |
| + if (gpu_memory > kGpuMemoryLimit) { |
| + memory_to_evict.bytes_limit = gpu_memory - kGpuMemoryLimit; |
| + } |
| + |
| + if (num_resources > kNumResourcesLimit) { |
| + memory_to_evict.num_resources_limit = num_resources - kNumResourcesLimit; |
| + } |
| + |
| + MemoryPolicy total_evicted = EvictUntilSatisfied(memory_to_evict, key); |
| + gpu_memory -= total_evicted.bytes_limit; |
| + num_resources -= total_evicted.num_resources_limit; |
| + } |
| + |
| + allocated_.bytes_limit = gpu_memory; |
| + allocated_.num_resources_limit = num_resources; |
| +} |
| + |
| +GpuMemoryManager::GpuMemoryManager() {} |
| + |
| +GpuMemoryManager::~GpuMemoryManager() {} |
| + |
| +} // namespace webview |