| 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/resource_util.h" |
| 8 #include "cc/resources/scoped_resource.h" | 9 #include "cc/resources/scoped_resource.h" |
| 9 | 10 |
| 10 namespace cc { | 11 namespace cc { |
| 11 | 12 |
| 12 ResourcePool::ResourcePool(ResourceProvider* resource_provider, GLenum target) | 13 ResourcePool::ResourcePool(ResourceProvider* resource_provider, GLenum target) |
| 13 : resource_provider_(resource_provider), | 14 : resource_provider_(resource_provider), |
| 14 target_(target), | 15 target_(target), |
| 15 max_memory_usage_bytes_(0), | 16 max_memory_usage_bytes_(0), |
| 16 max_unused_memory_usage_bytes_(0), | 17 max_unused_memory_usage_bytes_(0), |
| 17 max_resource_count_(0), | 18 max_resource_count_(0), |
| (...skipping 23 matching lines...) Expand all Loading... |
| 41 ScopedResource* resource = it->resource; | 42 ScopedResource* resource = it->resource; |
| 42 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 43 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| 43 | 44 |
| 44 if (resource->format() != format) | 45 if (resource->format() != format) |
| 45 continue; | 46 continue; |
| 46 if (resource->size() != size) | 47 if (resource->size() != size) |
| 47 continue; | 48 continue; |
| 48 | 49 |
| 49 unused_resources_.erase(it); | 50 unused_resources_.erase(it); |
| 50 unused_memory_usage_bytes_ -= | 51 unused_memory_usage_bytes_ -= |
| 51 Resource::UncheckedMemorySizeBytes(size, format); | 52 ResourceUtil::UncheckedSizeInBytes<size_t>(size, format); |
| 52 return make_scoped_ptr(resource); | 53 return make_scoped_ptr(resource); |
| 53 } | 54 } |
| 54 | 55 |
| 55 scoped_ptr<ScopedResource> resource = | 56 scoped_ptr<ScopedResource> resource = |
| 56 ScopedResource::Create(resource_provider_); | 57 ScopedResource::Create(resource_provider_); |
| 57 resource->AllocateManaged(size, target_, format); | 58 resource->AllocateManaged(size, target_, format); |
| 58 | 59 |
| 59 DCHECK(Resource::VerifySizeInBytes(resource->size(), resource->format())); | 60 DCHECK(ResourceUtil::VerifySizeInBytes<size_t>(resource->size(), |
| 60 memory_usage_bytes_ += | 61 resource->format())); |
| 61 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format()); | 62 memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 63 resource->size(), resource->format()); |
| 62 ++resource_count_; | 64 ++resource_count_; |
| 63 return resource.Pass(); | 65 return resource.Pass(); |
| 64 } | 66 } |
| 65 | 67 |
| 66 scoped_ptr<ScopedResource> ResourcePool::TryAcquireResourceWithContentId( | 68 scoped_ptr<ScopedResource> ResourcePool::TryAcquireResourceWithContentId( |
| 67 uint64_t content_id) { | 69 uint64_t content_id) { |
| 68 DCHECK(content_id); | 70 DCHECK(content_id); |
| 69 | 71 |
| 70 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(), | 72 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(), |
| 71 [content_id](const PoolResource& pool_resource) { | 73 [content_id](const PoolResource& pool_resource) { |
| 72 return pool_resource.content_id == content_id; | 74 return pool_resource.content_id == content_id; |
| 73 }); | 75 }); |
| 74 if (it == unused_resources_.end()) | 76 if (it == unused_resources_.end()) |
| 75 return nullptr; | 77 return nullptr; |
| 76 | 78 |
| 77 ScopedResource* resource = it->resource; | 79 ScopedResource* resource = it->resource; |
| 78 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 80 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| 79 | 81 |
| 80 unused_resources_.erase(it); | 82 unused_resources_.erase(it); |
| 81 unused_memory_usage_bytes_ -= | 83 unused_memory_usage_bytes_ -= ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 82 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format()); | 84 resource->size(), resource->format()); |
| 83 return make_scoped_ptr(resource); | 85 return make_scoped_ptr(resource); |
| 84 } | 86 } |
| 85 | 87 |
| 86 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource, | 88 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource, |
| 87 uint64_t content_id) { | 89 uint64_t content_id) { |
| 88 busy_resources_.push_back(PoolResource(resource.release(), content_id)); | 90 busy_resources_.push_back(PoolResource(resource.release(), content_id)); |
| 89 } | 91 } |
| 90 | 92 |
| 91 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, | 93 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, |
| 92 size_t max_unused_memory_usage_bytes, | 94 size_t max_unused_memory_usage_bytes, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 105 | 107 |
| 106 // LRU eviction pattern. Most recently used might be blocked by | 108 // LRU eviction pattern. Most recently used might be blocked by |
| 107 // a read lock fence but it's still better to evict the least | 109 // a read lock fence but it's still better to evict the least |
| 108 // recently used as it prevents a resource that is hard to reuse | 110 // recently used as it prevents a resource that is hard to reuse |
| 109 // because of unique size from being kept around. Resources that | 111 // because of unique size from being kept around. Resources that |
| 110 // can't be locked for write might also not be truly free-able. | 112 // can't be locked for write might also not be truly free-able. |
| 111 // We can free the resource here but it doesn't mean that the | 113 // We can free the resource here but it doesn't mean that the |
| 112 // memory is necessarily returned to the OS. | 114 // memory is necessarily returned to the OS. |
| 113 ScopedResource* resource = unused_resources_.front().resource; | 115 ScopedResource* resource = unused_resources_.front().resource; |
| 114 unused_resources_.pop_front(); | 116 unused_resources_.pop_front(); |
| 115 unused_memory_usage_bytes_ -= Resource::UncheckedMemorySizeBytes( | 117 unused_memory_usage_bytes_ -= ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 116 resource->size(), resource->format()); | 118 resource->size(), resource->format()); |
| 117 DeleteResource(resource); | 119 DeleteResource(resource); |
| 118 } | 120 } |
| 119 } | 121 } |
| 120 | 122 |
| 121 bool ResourcePool::ResourceUsageTooHigh() { | 123 bool ResourcePool::ResourceUsageTooHigh() { |
| 122 if (resource_count_ > max_resource_count_) | 124 if (resource_count_ > max_resource_count_) |
| 123 return true; | 125 return true; |
| 124 if (memory_usage_bytes_ > max_memory_usage_bytes_) | 126 if (memory_usage_bytes_ > max_memory_usage_bytes_) |
| 125 return true; | 127 return true; |
| 126 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) | 128 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) |
| 127 return true; | 129 return true; |
| 128 return false; | 130 return false; |
| 129 } | 131 } |
| 130 | 132 |
| 131 void ResourcePool::DeleteResource(ScopedResource* resource) { | 133 void ResourcePool::DeleteResource(ScopedResource* resource) { |
| 132 size_t resource_bytes = | 134 size_t resource_bytes = ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 133 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format()); | 135 resource->size(), resource->format()); |
| 134 memory_usage_bytes_ -= resource_bytes; | 136 memory_usage_bytes_ -= resource_bytes; |
| 135 --resource_count_; | 137 --resource_count_; |
| 136 delete resource; | 138 delete resource; |
| 137 } | 139 } |
| 138 | 140 |
| 139 void ResourcePool::CheckBusyResources(bool wait_if_needed) { | 141 void ResourcePool::CheckBusyResources(bool wait_if_needed) { |
| 140 ResourceList::iterator it = busy_resources_.begin(); | 142 ResourceList::iterator it = busy_resources_.begin(); |
| 141 | 143 |
| 142 while (it != busy_resources_.end()) { | 144 while (it != busy_resources_.end()) { |
| 143 ScopedResource* resource = it->resource; | 145 ScopedResource* resource = it->resource; |
| 144 | 146 |
| 145 if (wait_if_needed) | 147 if (wait_if_needed) |
| 146 resource_provider_->WaitReadLockIfNeeded(resource->id()); | 148 resource_provider_->WaitReadLockIfNeeded(resource->id()); |
| 147 | 149 |
| 148 if (resource_provider_->CanLockForWrite(resource->id())) { | 150 if (resource_provider_->CanLockForWrite(resource->id())) { |
| 149 DidFinishUsingResource(resource, it->content_id); | 151 DidFinishUsingResource(resource, it->content_id); |
| 150 it = busy_resources_.erase(it); | 152 it = busy_resources_.erase(it); |
| 151 } else if (resource_provider_->IsLost(resource->id())) { | 153 } else if (resource_provider_->IsLost(resource->id())) { |
| 152 // Remove lost resources from pool. | 154 // Remove lost resources from pool. |
| 153 DeleteResource(resource); | 155 DeleteResource(resource); |
| 154 it = busy_resources_.erase(it); | 156 it = busy_resources_.erase(it); |
| 155 } else { | 157 } else { |
| 156 ++it; | 158 ++it; |
| 157 } | 159 } |
| 158 } | 160 } |
| 159 } | 161 } |
| 160 | 162 |
| 161 void ResourcePool::DidFinishUsingResource(ScopedResource* resource, | 163 void ResourcePool::DidFinishUsingResource(ScopedResource* resource, |
| 162 uint64_t content_id) { | 164 uint64_t content_id) { |
| 163 unused_memory_usage_bytes_ += | 165 unused_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 164 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format()); | 166 resource->size(), resource->format()); |
| 165 unused_resources_.push_back(PoolResource(resource, content_id)); | 167 unused_resources_.push_back(PoolResource(resource, content_id)); |
| 166 } | 168 } |
| 167 | 169 |
| 168 } // namespace cc | 170 } // namespace cc |
| OLD | NEW |