Chromium Code Reviews| 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 "cc/resources/resource_provider.h" | 7 #include "cc/resources/resource_provider.h" |
| 8 | 8 |
| 9 namespace cc { | 9 namespace cc { |
| 10 | 10 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 | 40 |
| 41 ResourcePool::~ResourcePool() { | 41 ResourcePool::~ResourcePool() { |
| 42 SetResourceUsageLimits(0, 0, 0); | 42 SetResourceUsageLimits(0, 0, 0); |
| 43 } | 43 } |
| 44 | 44 |
| 45 scoped_ptr<ResourcePool::Resource> ResourcePool::AcquireResource( | 45 scoped_ptr<ResourcePool::Resource> ResourcePool::AcquireResource( |
| 46 gfx::Size size, ResourceFormat format) { | 46 gfx::Size size, ResourceFormat format) { |
| 47 for (ResourceList::iterator it = unused_resources_.begin(); | 47 for (ResourceList::iterator it = unused_resources_.begin(); |
| 48 it != unused_resources_.end(); ++it) { | 48 it != unused_resources_.end(); ++it) { |
| 49 Resource* resource = *it; | 49 Resource* resource = *it; |
| 50 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | |
| 50 | 51 |
| 51 if (!resource_provider_->CanLockForWrite(resource->id())) | |
| 52 continue; | |
| 53 if (resource->size() != size) | 52 if (resource->size() != size) |
| 54 continue; | 53 continue; |
| 55 if (resource->format() != format) | 54 if (resource->format() != format) |
| 56 continue; | 55 continue; |
| 57 | 56 |
| 58 unused_resources_.erase(it); | 57 unused_resources_.erase(it); |
| 59 unused_memory_usage_bytes_ -= resource->bytes(); | 58 unused_memory_usage_bytes_ -= resource->bytes(); |
| 60 return make_scoped_ptr(resource); | 59 return make_scoped_ptr(resource); |
| 61 } | 60 } |
| 62 | 61 |
| 63 // Create new resource. | 62 // Create new resource. |
| 64 Resource* resource = new Resource(resource_provider_, size, format); | 63 Resource* resource = new Resource(resource_provider_, size, format); |
| 65 | 64 |
| 66 // Extend all read locks on all resources until the resource is | 65 // Extend all read locks on all resources until the resource is |
| 67 // finished being used, such that we know when resources are | 66 // finished being used, such that we know when resources are |
| 68 // truly safe to recycle. | 67 // truly safe to recycle. |
| 69 resource_provider_->EnableReadLockFences(resource->id(), true); | 68 resource_provider_->EnableReadLockFences(resource->id(), true); |
| 70 | 69 |
| 71 memory_usage_bytes_ += resource->bytes(); | 70 memory_usage_bytes_ += resource->bytes(); |
| 72 ++resource_count_; | 71 ++resource_count_; |
| 73 return make_scoped_ptr(resource); | 72 return make_scoped_ptr(resource); |
| 74 } | 73 } |
| 75 | 74 |
| 76 void ResourcePool::ReleaseResource( | 75 void ResourcePool::ReleaseResource( |
| 77 scoped_ptr<ResourcePool::Resource> resource) { | 76 scoped_ptr<ResourcePool::Resource> resource) { |
| 78 if (ResourceUsageTooHigh()) { | |
| 79 memory_usage_bytes_ -= resource->bytes(); | |
| 80 --resource_count_; | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 unused_memory_usage_bytes_ += resource->bytes(); | 77 unused_memory_usage_bytes_ += resource->bytes(); |
|
reveman
2013/11/05 15:38:27
This needs to be moved to CheckBusyResources() as
| |
| 85 unused_resources_.push_back(resource.release()); | 78 busy_resources_.push_back(resource.release()); |
| 86 } | 79 } |
| 87 | 80 |
| 88 void ResourcePool::SetResourceUsageLimits( | 81 void ResourcePool::SetResourceUsageLimits( |
| 89 size_t max_memory_usage_bytes, | 82 size_t max_memory_usage_bytes, |
| 90 size_t max_unused_memory_usage_bytes, | 83 size_t max_unused_memory_usage_bytes, |
| 91 size_t max_resource_count) { | 84 size_t max_resource_count) { |
| 92 max_memory_usage_bytes_ = max_memory_usage_bytes; | 85 max_memory_usage_bytes_ = max_memory_usage_bytes; |
| 93 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes; | 86 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes; |
| 94 max_resource_count_ = max_resource_count; | 87 max_resource_count_ = max_resource_count; |
| 95 | 88 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 120 bool ResourcePool::ResourceUsageTooHigh() { | 113 bool ResourcePool::ResourceUsageTooHigh() { |
| 121 if (resource_count_ > max_resource_count_) | 114 if (resource_count_ > max_resource_count_) |
| 122 return true; | 115 return true; |
| 123 if (memory_usage_bytes_ > max_memory_usage_bytes_) | 116 if (memory_usage_bytes_ > max_memory_usage_bytes_) |
| 124 return true; | 117 return true; |
| 125 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) | 118 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) |
| 126 return true; | 119 return true; |
| 127 return false; | 120 return false; |
| 128 } | 121 } |
| 129 | 122 |
| 123 void ResourcePool::CheckBusyResources() { | |
| 124 ResourceList::iterator it = busy_resources_.begin(); | |
| 125 | |
| 126 while (it != busy_resources_.end()) { | |
| 127 Resource* resource = *it; | |
| 128 | |
| 129 if (resource_provider_->CanLockForWrite(resource->id())) { | |
| 130 unused_resources_.push_back(resource); | |
| 131 it = busy_resources_.erase(it); | |
| 132 } else { | |
| 133 ++it; | |
| 134 } | |
| 135 } | |
| 136 } | |
| 137 | |
| 130 } // namespace cc | 138 } // namespace cc |
| OLD | NEW |