| 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 { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 ++it) { | 40 ++it) { |
| 41 ScopedResource* resource = it->resource; | 41 ScopedResource* resource = it->resource; |
| 42 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 42 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| 43 | 43 |
| 44 if (resource->format() != format) | 44 if (resource->format() != format) |
| 45 continue; | 45 continue; |
| 46 if (resource->size() != size) | 46 if (resource->size() != size) |
| 47 continue; | 47 continue; |
| 48 | 48 |
| 49 unused_resources_.erase(it); | 49 unused_resources_.erase(it); |
| 50 unused_memory_usage_bytes_ -= resource->bytes(); | 50 unused_memory_usage_bytes_ -= |
| 51 Resource::UncheckedMemorySizeBytes(size, format); |
| 51 return make_scoped_ptr(resource); | 52 return make_scoped_ptr(resource); |
| 52 } | 53 } |
| 53 | 54 |
| 54 scoped_ptr<ScopedResource> resource = | 55 scoped_ptr<ScopedResource> resource = |
| 55 ScopedResource::Create(resource_provider_); | 56 ScopedResource::Create(resource_provider_); |
| 56 resource->AllocateManaged(size, target_, format); | 57 resource->AllocateManaged(size, target_, format); |
| 57 | 58 |
| 58 memory_usage_bytes_ += resource->bytes(); | 59 DCHECK(Resource::VerifySizeInBytes(resource->size(), resource->format())); |
| 60 memory_usage_bytes_ += |
| 61 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format()); |
| 59 ++resource_count_; | 62 ++resource_count_; |
| 60 return resource.Pass(); | 63 return resource.Pass(); |
| 61 } | 64 } |
| 62 | 65 |
| 63 scoped_ptr<ScopedResource> ResourcePool::TryAcquireResourceWithContentId( | 66 scoped_ptr<ScopedResource> ResourcePool::TryAcquireResourceWithContentId( |
| 64 uint64_t content_id) { | 67 uint64_t content_id) { |
| 65 DCHECK(content_id); | 68 DCHECK(content_id); |
| 66 | 69 |
| 67 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(), | 70 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(), |
| 68 [content_id](const PoolResource& pool_resource) { | 71 [content_id](const PoolResource& pool_resource) { |
| 69 return pool_resource.content_id == content_id; | 72 return pool_resource.content_id == content_id; |
| 70 }); | 73 }); |
| 71 if (it == unused_resources_.end()) | 74 if (it == unused_resources_.end()) |
| 72 return nullptr; | 75 return nullptr; |
| 73 | 76 |
| 74 ScopedResource* resource = it->resource; | 77 ScopedResource* resource = it->resource; |
| 75 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 78 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| 76 | 79 |
| 77 unused_resources_.erase(it); | 80 unused_resources_.erase(it); |
| 78 unused_memory_usage_bytes_ -= resource->bytes(); | 81 unused_memory_usage_bytes_ -= |
| 82 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format()); |
| 79 return make_scoped_ptr(resource); | 83 return make_scoped_ptr(resource); |
| 80 } | 84 } |
| 81 | 85 |
| 82 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource, | 86 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource, |
| 83 uint64_t content_id) { | 87 uint64_t content_id) { |
| 84 busy_resources_.push_back(PoolResource(resource.release(), content_id)); | 88 busy_resources_.push_back(PoolResource(resource.release(), content_id)); |
| 85 } | 89 } |
| 86 | 90 |
| 87 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, | 91 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, |
| 88 size_t max_unused_memory_usage_bytes, | 92 size_t max_unused_memory_usage_bytes, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 101 | 105 |
| 102 // LRU eviction pattern. Most recently used might be blocked by | 106 // LRU eviction pattern. Most recently used might be blocked by |
| 103 // a read lock fence but it's still better to evict the least | 107 // a read lock fence but it's still better to evict the least |
| 104 // recently used as it prevents a resource that is hard to reuse | 108 // recently used as it prevents a resource that is hard to reuse |
| 105 // because of unique size from being kept around. Resources that | 109 // because of unique size from being kept around. Resources that |
| 106 // can't be locked for write might also not be truly free-able. | 110 // can't be locked for write might also not be truly free-able. |
| 107 // We can free the resource here but it doesn't mean that the | 111 // We can free the resource here but it doesn't mean that the |
| 108 // memory is necessarily returned to the OS. | 112 // memory is necessarily returned to the OS. |
| 109 ScopedResource* resource = unused_resources_.front().resource; | 113 ScopedResource* resource = unused_resources_.front().resource; |
| 110 unused_resources_.pop_front(); | 114 unused_resources_.pop_front(); |
| 111 memory_usage_bytes_ -= resource->bytes(); | 115 size_t resource_bytes = Resource::UncheckedMemorySizeBytes( |
| 112 unused_memory_usage_bytes_ -= resource->bytes(); | 116 resource->size(), resource->format()); |
| 117 memory_usage_bytes_ -= resource_bytes; |
| 118 unused_memory_usage_bytes_ -= resource_bytes; |
| 113 --resource_count_; | 119 --resource_count_; |
| 114 delete resource; | 120 delete resource; |
| 115 } | 121 } |
| 116 } | 122 } |
| 117 | 123 |
| 118 bool ResourcePool::ResourceUsageTooHigh() { | 124 bool ResourcePool::ResourceUsageTooHigh() { |
| 119 if (resource_count_ > max_resource_count_) | 125 if (resource_count_ > max_resource_count_) |
| 120 return true; | 126 return true; |
| 121 if (memory_usage_bytes_ > max_memory_usage_bytes_) | 127 if (memory_usage_bytes_ > max_memory_usage_bytes_) |
| 122 return true; | 128 return true; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 138 DidFinishUsingResource(resource, it->content_id); | 144 DidFinishUsingResource(resource, it->content_id); |
| 139 it = busy_resources_.erase(it); | 145 it = busy_resources_.erase(it); |
| 140 } else { | 146 } else { |
| 141 ++it; | 147 ++it; |
| 142 } | 148 } |
| 143 } | 149 } |
| 144 } | 150 } |
| 145 | 151 |
| 146 void ResourcePool::DidFinishUsingResource(ScopedResource* resource, | 152 void ResourcePool::DidFinishUsingResource(ScopedResource* resource, |
| 147 uint64_t content_id) { | 153 uint64_t content_id) { |
| 148 unused_memory_usage_bytes_ += resource->bytes(); | 154 unused_memory_usage_bytes_ += |
| 155 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format()); |
| 149 unused_resources_.push_back(PoolResource(resource, content_id)); | 156 unused_resources_.push_back(PoolResource(resource, content_id)); |
| 150 } | 157 } |
| 151 | 158 |
| 152 } // namespace cc | 159 } // namespace cc |
| OLD | NEW |