| 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 #include "cc/resources/scoped_resource.h" | 8 #include "cc/resources/scoped_resource.h" |
| 9 | 9 |
| 10 namespace cc { | 10 namespace cc { |
| 11 | 11 |
| 12 ResourcePool::ResourcePool(ResourceProvider* resource_provider, | 12 ResourcePool::ResourcePool(ResourceProvider* resource_provider, |
| 13 GLenum target, | 13 GLenum target, |
| 14 ResourceFormat format) | 14 ResourceFormat format) |
| 15 : resource_provider_(resource_provider), | 15 : resource_provider_(resource_provider), |
| 16 target_(target), | 16 target_(target), |
| 17 format_(format), | 17 format_(format), |
| 18 max_memory_usage_bytes_(0), | 18 max_memory_usage_bytes_(0), |
| 19 max_unused_memory_usage_bytes_(0), | 19 max_unused_memory_usage_bytes_(0), |
| 20 max_resource_count_(0), | |
| 21 memory_usage_bytes_(0), | 20 memory_usage_bytes_(0), |
| 22 unused_memory_usage_bytes_(0), | 21 unused_memory_usage_bytes_(0), |
| 23 resource_count_(0) {} | 22 resource_count_(0) {} |
| 24 | 23 |
| 25 ResourcePool::~ResourcePool() { | 24 ResourcePool::~ResourcePool() { |
| 26 while (!busy_resources_.empty()) { | 25 while (!busy_resources_.empty()) { |
| 27 DidFinishUsingResource(busy_resources_.front()); | 26 DidFinishUsingResource(busy_resources_.front()); |
| 28 busy_resources_.pop_front(); | 27 busy_resources_.pop_front(); |
| 29 } | 28 } |
| 30 | 29 |
| 31 SetResourceUsageLimits(0, 0, 0); | 30 SetResourceUsageLimits(0, 0); |
| 32 DCHECK_EQ(0u, unused_resources_.size()); | 31 DCHECK_EQ(0u, unused_resources_.size()); |
| 33 DCHECK_EQ(0u, memory_usage_bytes_); | 32 DCHECK_EQ(0u, memory_usage_bytes_); |
| 34 DCHECK_EQ(0u, unused_memory_usage_bytes_); | 33 DCHECK_EQ(0u, unused_memory_usage_bytes_); |
| 35 DCHECK_EQ(0u, resource_count_); | 34 DCHECK_EQ(0u, resource_count_); |
| 36 } | 35 } |
| 37 | 36 |
| 38 scoped_ptr<ScopedResource> ResourcePool::AcquireResource( | 37 scoped_ptr<ScopedResource> ResourcePool::AcquireResource( |
| 39 const gfx::Size& size) { | 38 const gfx::Size& size) { |
| 40 for (ResourceList::iterator it = unused_resources_.begin(); | 39 for (ResourceList::iterator it = unused_resources_.begin(); |
| 41 it != unused_resources_.end(); | 40 it != unused_resources_.end(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 57 | 56 |
| 58 memory_usage_bytes_ += resource->bytes(); | 57 memory_usage_bytes_ += resource->bytes(); |
| 59 ++resource_count_; | 58 ++resource_count_; |
| 60 return resource.Pass(); | 59 return resource.Pass(); |
| 61 } | 60 } |
| 62 | 61 |
| 63 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource) { | 62 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource) { |
| 64 busy_resources_.push_back(resource.release()); | 63 busy_resources_.push_back(resource.release()); |
| 65 } | 64 } |
| 66 | 65 |
| 67 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, | 66 void ResourcePool::SetResourceUsageLimits( |
| 68 size_t max_unused_memory_usage_bytes, | 67 size_t max_memory_usage_bytes, |
| 69 size_t max_resource_count) { | 68 size_t max_unused_memory_usage_bytes) { |
| 70 max_memory_usage_bytes_ = max_memory_usage_bytes; | 69 max_memory_usage_bytes_ = max_memory_usage_bytes; |
| 71 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes; | 70 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes; |
| 72 max_resource_count_ = max_resource_count; | |
| 73 | 71 |
| 74 ReduceResourceUsage(); | 72 ReduceResourceUsage(); |
| 75 } | 73 } |
| 76 | 74 |
| 77 void ResourcePool::ReduceResourceUsage() { | 75 void ResourcePool::ReduceResourceUsage() { |
| 78 while (!unused_resources_.empty()) { | 76 while (!unused_resources_.empty()) { |
| 79 if (!ResourceUsageTooHigh()) | 77 if (!ResourceUsageTooHigh()) |
| 80 break; | 78 break; |
| 81 | 79 |
| 82 // LRU eviction pattern. Most recently used might be blocked by | 80 // LRU eviction pattern. Most recently used might be blocked by |
| 83 // a read lock fence but it's still better to evict the least | 81 // a read lock fence but it's still better to evict the least |
| 84 // recently used as it prevents a resource that is hard to reuse | 82 // recently used as it prevents a resource that is hard to reuse |
| 85 // because of unique size from being kept around. Resources that | 83 // because of unique size from being kept around. Resources that |
| 86 // can't be locked for write might also not be truly free-able. | 84 // can't be locked for write might also not be truly free-able. |
| 87 // We can free the resource here but it doesn't mean that the | 85 // We can free the resource here but it doesn't mean that the |
| 88 // memory is necessarily returned to the OS. | 86 // memory is necessarily returned to the OS. |
| 89 ScopedResource* resource = unused_resources_.front(); | 87 ScopedResource* resource = unused_resources_.front(); |
| 90 unused_resources_.pop_front(); | 88 unused_resources_.pop_front(); |
| 91 memory_usage_bytes_ -= resource->bytes(); | 89 memory_usage_bytes_ -= resource->bytes(); |
| 92 unused_memory_usage_bytes_ -= resource->bytes(); | 90 unused_memory_usage_bytes_ -= resource->bytes(); |
| 93 --resource_count_; | 91 --resource_count_; |
| 94 delete resource; | 92 delete resource; |
| 95 } | 93 } |
| 96 } | 94 } |
| 97 | 95 |
| 98 bool ResourcePool::ResourceUsageTooHigh() { | 96 bool ResourcePool::ResourceUsageTooHigh() { |
| 99 if (resource_count_ > max_resource_count_) | |
| 100 return true; | |
| 101 if (memory_usage_bytes_ > max_memory_usage_bytes_) | 97 if (memory_usage_bytes_ > max_memory_usage_bytes_) |
| 102 return true; | 98 return true; |
| 103 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) | 99 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) |
| 104 return true; | 100 return true; |
| 105 return false; | 101 return false; |
| 106 } | 102 } |
| 107 | 103 |
| 108 void ResourcePool::CheckBusyResources() { | 104 void ResourcePool::CheckBusyResources() { |
| 109 ResourceList::iterator it = busy_resources_.begin(); | 105 ResourceList::iterator it = busy_resources_.begin(); |
| 110 | 106 |
| 111 while (it != busy_resources_.end()) { | 107 while (it != busy_resources_.end()) { |
| 112 ScopedResource* resource = *it; | 108 ScopedResource* resource = *it; |
| 113 | 109 |
| 114 if (resource_provider_->CanLockForWrite(resource->id())) { | 110 if (resource_provider_->CanLockForWrite(resource->id())) { |
| 115 DidFinishUsingResource(resource); | 111 DidFinishUsingResource(resource); |
| 116 it = busy_resources_.erase(it); | 112 it = busy_resources_.erase(it); |
| 117 } else { | 113 } else { |
| 118 ++it; | 114 ++it; |
| 119 } | 115 } |
| 120 } | 116 } |
| 121 } | 117 } |
| 122 | 118 |
| 123 void ResourcePool::DidFinishUsingResource(ScopedResource* resource) { | 119 void ResourcePool::DidFinishUsingResource(ScopedResource* resource) { |
| 124 unused_memory_usage_bytes_ += resource->bytes(); | 120 unused_memory_usage_bytes_ += resource->bytes(); |
| 125 unused_resources_.push_back(resource); | 121 unused_resources_.push_back(resource); |
| 126 } | 122 } |
| 127 | 123 |
| 128 } // namespace cc | 124 } // namespace cc |
| OLD | NEW |