roll libyuv to r1437 to resolve msan overread on odd width ARGBToYUY2.
[chromium-blink-merge.git] / cc / resources / resource_pool.cc
blobf5c71678f2740a5966f41958c08a0bf49f77ae70
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
3 // found in the LICENSE file.
5 #include "cc/resources/resource_pool.h"
7 #include "cc/resources/resource_provider.h"
8 #include "cc/resources/scoped_resource.h"
10 namespace cc {
12 ResourcePool::ResourcePool(ResourceProvider* resource_provider, GLenum target)
13 : resource_provider_(resource_provider),
14 target_(target),
15 max_memory_usage_bytes_(0),
16 max_unused_memory_usage_bytes_(0),
17 max_resource_count_(0),
18 memory_usage_bytes_(0),
19 unused_memory_usage_bytes_(0),
20 resource_count_(0) {}
22 ResourcePool::~ResourcePool() {
23 while (!busy_resources_.empty()) {
24 auto const& front = busy_resources_.front();
25 DidFinishUsingResource(front.resource, front.content_id);
26 busy_resources_.pop_front();
29 SetResourceUsageLimits(0, 0, 0);
30 DCHECK_EQ(0u, unused_resources_.size());
31 DCHECK_EQ(0u, memory_usage_bytes_);
32 DCHECK_EQ(0u, unused_memory_usage_bytes_);
33 DCHECK_EQ(0u, resource_count_);
36 scoped_ptr<ScopedResource> ResourcePool::AcquireResource(
37 const gfx::Size& size, ResourceFormat format) {
38 for (ResourceList::iterator it = unused_resources_.begin();
39 it != unused_resources_.end();
40 ++it) {
41 ScopedResource* resource = it->resource;
42 DCHECK(resource_provider_->CanLockForWrite(resource->id()));
44 if (resource->format() != format)
45 continue;
46 if (resource->size() != size)
47 continue;
49 unused_resources_.erase(it);
50 unused_memory_usage_bytes_ -=
51 Resource::UncheckedMemorySizeBytes(size, format);
52 return make_scoped_ptr(resource);
55 scoped_ptr<ScopedResource> resource =
56 ScopedResource::Create(resource_provider_);
57 resource->AllocateManaged(size, target_, format);
59 DCHECK(Resource::VerifySizeInBytes(resource->size(), resource->format()));
60 memory_usage_bytes_ +=
61 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format());
62 ++resource_count_;
63 return resource.Pass();
66 scoped_ptr<ScopedResource> ResourcePool::TryAcquireResourceWithContentId(
67 uint64_t content_id) {
68 DCHECK(content_id);
70 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(),
71 [content_id](const PoolResource& pool_resource) {
72 return pool_resource.content_id == content_id;
73 });
74 if (it == unused_resources_.end())
75 return nullptr;
77 ScopedResource* resource = it->resource;
78 DCHECK(resource_provider_->CanLockForWrite(resource->id()));
80 unused_resources_.erase(it);
81 unused_memory_usage_bytes_ -=
82 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format());
83 return make_scoped_ptr(resource);
86 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource,
87 uint64_t content_id) {
88 busy_resources_.push_back(PoolResource(resource.release(), content_id));
91 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes,
92 size_t max_unused_memory_usage_bytes,
93 size_t max_resource_count) {
94 max_memory_usage_bytes_ = max_memory_usage_bytes;
95 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes;
96 max_resource_count_ = max_resource_count;
98 ReduceResourceUsage();
101 void ResourcePool::ReduceResourceUsage() {
102 while (!unused_resources_.empty()) {
103 if (!ResourceUsageTooHigh())
104 break;
106 // LRU eviction pattern. Most recently used might be blocked by
107 // 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
109 // because of unique size from being kept around. Resources that
110 // 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
112 // memory is necessarily returned to the OS.
113 ScopedResource* resource = unused_resources_.front().resource;
114 unused_resources_.pop_front();
115 size_t resource_bytes = Resource::UncheckedMemorySizeBytes(
116 resource->size(), resource->format());
117 memory_usage_bytes_ -= resource_bytes;
118 unused_memory_usage_bytes_ -= resource_bytes;
119 --resource_count_;
120 delete resource;
124 bool ResourcePool::ResourceUsageTooHigh() {
125 if (resource_count_ > max_resource_count_)
126 return true;
127 if (memory_usage_bytes_ > max_memory_usage_bytes_)
128 return true;
129 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_)
130 return true;
131 return false;
134 void ResourcePool::CheckBusyResources(bool wait_if_needed) {
135 ResourceList::iterator it = busy_resources_.begin();
137 while (it != busy_resources_.end()) {
138 ScopedResource* resource = it->resource;
140 if (wait_if_needed)
141 resource_provider_->WaitReadLockIfNeeded(resource->id());
143 if (resource_provider_->CanLockForWrite(resource->id())) {
144 DidFinishUsingResource(resource, it->content_id);
145 it = busy_resources_.erase(it);
146 } else {
147 ++it;
152 void ResourcePool::DidFinishUsingResource(ScopedResource* resource,
153 uint64_t content_id) {
154 unused_memory_usage_bytes_ +=
155 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format());
156 unused_resources_.push_back(PoolResource(resource, content_id));
159 } // namespace cc