| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/resources/resource_pool.h" | 5 #include "cc/resources/resource_pool.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/allocator/allocator_extension.h" |
| 13 #include "base/format_macros.h" | 14 #include "base/format_macros.h" |
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "base/trace_event/memory_dump_manager.h" | 17 #include "base/trace_event/memory_dump_manager.h" |
| 17 #include "cc/base/container_util.h" | 18 #include "cc/base/container_util.h" |
| 18 #include "cc/resources/resource_provider.h" | 19 #include "cc/resources/resource_provider.h" |
| 19 #include "cc/resources/resource_util.h" | 20 #include "cc/resources/resource_util.h" |
| 20 #include "cc/resources/scoped_resource.h" | 21 #include "cc/resources/scoped_resource.h" |
| 21 | 22 |
| 22 namespace cc { | 23 namespace cc { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 in_use_memory_usage_bytes_(0), | 69 in_use_memory_usage_bytes_(0), |
| 69 total_memory_usage_bytes_(0), | 70 total_memory_usage_bytes_(0), |
| 70 total_resource_count_(0), | 71 total_resource_count_(0), |
| 71 task_runner_(task_runner), | 72 task_runner_(task_runner), |
| 72 evict_expired_resources_pending_(false), | 73 evict_expired_resources_pending_(false), |
| 73 resource_expiration_delay_( | 74 resource_expiration_delay_( |
| 74 base::TimeDelta::FromMilliseconds(kResourceExpirationDelayMs)), | 75 base::TimeDelta::FromMilliseconds(kResourceExpirationDelayMs)), |
| 75 weak_ptr_factory_(this) { | 76 weak_ptr_factory_(this) { |
| 76 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 77 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
| 77 this, "cc::ResourcePool", task_runner_.get()); | 78 this, "cc::ResourcePool", task_runner_.get()); |
| 79 |
| 80 memory_pressure_listener_.reset(new base::MemoryPressureListener( |
| 81 base::Bind(&ResourcePool::OnMemoryPressure, base::Unretained(this)))); |
| 78 } | 82 } |
| 79 | 83 |
| 80 ResourcePool::~ResourcePool() { | 84 ResourcePool::~ResourcePool() { |
| 81 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | 85 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
| 82 this); | 86 this); |
| 83 | 87 |
| 84 DCHECK_EQ(0u, in_use_resources_.size()); | 88 DCHECK_EQ(0u, in_use_resources_.size()); |
| 85 | 89 |
| 86 while (!busy_resources_.empty()) { | 90 while (!busy_resources_.empty()) { |
| 87 DidFinishUsingResource(PopBack(&busy_resources_)); | 91 DidFinishUsingResource(PopBack(&busy_resources_)); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 } | 221 } |
| 218 | 222 |
| 219 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, | 223 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, |
| 220 size_t max_resource_count) { | 224 size_t max_resource_count) { |
| 221 max_memory_usage_bytes_ = max_memory_usage_bytes; | 225 max_memory_usage_bytes_ = max_memory_usage_bytes; |
| 222 max_resource_count_ = max_resource_count; | 226 max_resource_count_ = max_resource_count; |
| 223 | 227 |
| 224 ReduceResourceUsage(); | 228 ReduceResourceUsage(); |
| 225 } | 229 } |
| 226 | 230 |
| 231 void ResourcePool::OnMemoryPressure( |
| 232 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 233 while (!unused_resources_.empty()) |
| 234 DeleteResource(PopBack(&unused_resources_)); |
| 235 base::allocator::ReleaseFreeMemory(); |
| 236 } |
| 237 |
| 227 void ResourcePool::ReduceResourceUsage() { | 238 void ResourcePool::ReduceResourceUsage() { |
| 228 while (!unused_resources_.empty()) { | 239 while (!unused_resources_.empty()) { |
| 229 if (!ResourceUsageTooHigh()) | 240 if (!ResourceUsageTooHigh()) |
| 230 break; | 241 break; |
| 231 | 242 |
| 232 // LRU eviction pattern. Most recently used might be blocked by | 243 // LRU eviction pattern. Most recently used might be blocked by |
| 233 // a read lock fence but it's still better to evict the least | 244 // a read lock fence but it's still better to evict the least |
| 234 // recently used as it prevents a resource that is hard to reuse | 245 // recently used as it prevents a resource that is hard to reuse |
| 235 // because of unique size from being kept around. Resources that | 246 // because of unique size from being kept around. Resources that |
| 236 // can't be locked for write might also not be truly free-able. | 247 // can't be locked for write might also not be truly free-able. |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 for (const auto& resource : busy_resources_) { | 361 for (const auto& resource : busy_resources_) { |
| 351 resource->OnMemoryDump(pmd, resource_provider_, false /* is_free */); | 362 resource->OnMemoryDump(pmd, resource_provider_, false /* is_free */); |
| 352 } | 363 } |
| 353 for (const auto& entry : in_use_resources_) { | 364 for (const auto& entry : in_use_resources_) { |
| 354 entry.second->OnMemoryDump(pmd, resource_provider_, false /* is_free */); | 365 entry.second->OnMemoryDump(pmd, resource_provider_, false /* is_free */); |
| 355 } | 366 } |
| 356 return true; | 367 return true; |
| 357 } | 368 } |
| 358 | 369 |
| 359 } // namespace cc | 370 } // namespace cc |
| OLD | NEW |