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 #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, GLenum target) | 12 ResourcePool::ResourcePool(ResourceProvider* resource_provider, GLenum target) |
| 13 : resource_provider_(resource_provider), | 13 : resource_provider_(resource_provider), |
| 14 target_(target), | 14 target_(target), |
| 15 max_memory_usage_bytes_(0), | 15 max_memory_usage_bytes_(0), |
| 16 max_unused_memory_usage_bytes_(0), | 16 max_unused_memory_usage_bytes_(0), |
| 17 max_resource_count_(0), | 17 max_resource_count_(0), |
| 18 memory_usage_bytes_(0), | 18 memory_usage_bytes_(0), |
| 19 unused_memory_usage_bytes_(0), | 19 unused_memory_usage_bytes_(0), |
| 20 resource_count_(0) {} | 20 resource_count_(0) {} |
| 21 | 21 |
| 22 ResourcePool::~ResourcePool() { | 22 ResourcePool::~ResourcePool() { |
| 23 while (!busy_resources_.empty()) { | 23 while (!busy_resources_.empty()) { |
| 24 DidFinishUsingResource(busy_resources_.front()); | 24 auto const& front = busy_resources_.front(); |
| 25 DidFinishUsingResource(front.resource, front.id); | |
| 25 busy_resources_.pop_front(); | 26 busy_resources_.pop_front(); |
| 26 } | 27 } |
| 27 | 28 |
| 28 SetResourceUsageLimits(0, 0, 0); | 29 SetResourceUsageLimits(0, 0, 0); |
| 29 DCHECK_EQ(0u, unused_resources_.size()); | 30 DCHECK_EQ(0u, unused_resources_.size()); |
| 30 DCHECK_EQ(0u, memory_usage_bytes_); | 31 DCHECK_EQ(0u, memory_usage_bytes_); |
| 31 DCHECK_EQ(0u, unused_memory_usage_bytes_); | 32 DCHECK_EQ(0u, unused_memory_usage_bytes_); |
| 32 DCHECK_EQ(0u, resource_count_); | 33 DCHECK_EQ(0u, resource_count_); |
| 33 } | 34 } |
| 34 | 35 |
| 35 scoped_ptr<ScopedResource> ResourcePool::AcquireResource( | 36 scoped_ptr<ScopedResource> ResourcePool::AcquireResource( |
| 36 const gfx::Size& size, ResourceFormat format) { | 37 const gfx::Size& size, ResourceFormat format) { |
| 37 for (ResourceList::iterator it = unused_resources_.begin(); | 38 for (ResourceList::iterator it = unused_resources_.begin(); |
| 38 it != unused_resources_.end(); | 39 it != unused_resources_.end(); |
| 39 ++it) { | 40 ++it) { |
| 40 ScopedResource* resource = *it; | 41 ScopedResource* resource = it->resource; |
| 41 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 42 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| 42 | 43 |
| 43 if (resource->format() != format) | 44 if (resource->format() != format) |
| 44 continue; | 45 continue; |
| 45 if (resource->size() != size) | 46 if (resource->size() != size) |
| 46 continue; | 47 continue; |
| 47 | 48 |
| 48 unused_resources_.erase(it); | 49 unused_resources_.erase(it); |
| 49 unused_memory_usage_bytes_ -= resource->bytes(); | 50 unused_memory_usage_bytes_ -= resource->bytes(); |
| 50 return make_scoped_ptr(resource); | 51 return make_scoped_ptr(resource); |
| 51 } | 52 } |
| 52 | 53 |
| 53 scoped_ptr<ScopedResource> resource = | 54 scoped_ptr<ScopedResource> resource = |
| 54 ScopedResource::Create(resource_provider_); | 55 ScopedResource::Create(resource_provider_); |
| 55 resource->AllocateManaged(size, target_, format); | 56 resource->AllocateManaged(size, target_, format); |
| 56 | 57 |
| 57 memory_usage_bytes_ += resource->bytes(); | 58 memory_usage_bytes_ += resource->bytes(); |
| 58 ++resource_count_; | 59 ++resource_count_; |
| 59 return resource.Pass(); | 60 return resource.Pass(); |
| 60 } | 61 } |
| 61 | 62 |
| 62 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource) { | 63 scoped_ptr<ScopedResource> ResourcePool::TryAcquireOldResource( |
| 63 busy_resources_.push_back(resource.release()); | 64 const gfx::Size& size, |
| 65 ResourceFormat format, | |
| 66 uint64_t id) { | |
| 67 if (!id) | |
| 68 return nullptr; | |
| 69 | |
| 70 for (auto it = unused_resources_.begin(); it != unused_resources_.end(); | |
|
piman
2015/05/14 19:50:46
Should we check busy_resources_ too? We only need
danakj
2015/05/14 19:59:47
Oh that's a good point, we don't need to copy if i
danakj
2015/05/14 22:03:42
This was awesome. For on-screen animations we don'
| |
| 71 ++it) { | |
|
piman
2015/05/14 19:50:46
should unused_resources_ be a hash map indexed by
danakj
2015/05/14 19:59:47
i could also make it a base::SmallMap, i'm not act
danakj
2015/05/14 22:03:42
There are at most 32 things in the vector. https:/
| |
| 72 const PoolResource& pr = *it; | |
| 73 if (pr.id != id || pr.resource->format() != format || | |
| 74 pr.resource->size() != size) | |
|
piman
2015/05/14 19:50:46
If id match but format/size are different, can we
danakj
2015/05/14 19:59:47
We can reuse it rather than mallocing a new one in
| |
| 75 continue; | |
| 76 | |
| 77 ScopedResource* resource = pr.resource; | |
| 78 unused_memory_usage_bytes_ -= pr.resource->bytes(); | |
| 79 unused_resources_.erase(it); | |
| 80 return make_scoped_ptr(resource); | |
| 81 } | |
| 82 | |
| 83 return nullptr; | |
| 84 } | |
| 85 | |
| 86 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource, | |
| 87 uint64_t id) { | |
| 88 busy_resources_.push_back(PoolResource(resource.release(), id)); | |
| 64 } | 89 } |
| 65 | 90 |
| 66 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, | 91 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, |
| 67 size_t max_unused_memory_usage_bytes, | 92 size_t max_unused_memory_usage_bytes, |
| 68 size_t max_resource_count) { | 93 size_t max_resource_count) { |
| 69 max_memory_usage_bytes_ = max_memory_usage_bytes; | 94 max_memory_usage_bytes_ = max_memory_usage_bytes; |
| 70 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes; | 95 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes; |
| 71 max_resource_count_ = max_resource_count; | 96 max_resource_count_ = max_resource_count; |
| 72 | 97 |
| 73 ReduceResourceUsage(); | 98 ReduceResourceUsage(); |
| 74 } | 99 } |
| 75 | 100 |
| 76 void ResourcePool::ReduceResourceUsage() { | 101 void ResourcePool::ReduceResourceUsage() { |
| 77 while (!unused_resources_.empty()) { | 102 while (!unused_resources_.empty()) { |
| 78 if (!ResourceUsageTooHigh()) | 103 if (!ResourceUsageTooHigh()) |
| 79 break; | 104 break; |
| 80 | 105 |
| 81 // LRU eviction pattern. Most recently used might be blocked by | 106 // LRU eviction pattern. Most recently used might be blocked by |
| 82 // 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 |
| 83 // 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 |
| 84 // because of unique size from being kept around. Resources that | 109 // because of unique size from being kept around. Resources that |
| 85 // 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. |
| 86 // 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 |
| 87 // memory is necessarily returned to the OS. | 112 // memory is necessarily returned to the OS. |
| 88 ScopedResource* resource = unused_resources_.front(); | 113 ScopedResource* resource = unused_resources_.front().resource; |
| 89 unused_resources_.pop_front(); | 114 unused_resources_.pop_front(); |
| 90 memory_usage_bytes_ -= resource->bytes(); | 115 memory_usage_bytes_ -= resource->bytes(); |
| 91 unused_memory_usage_bytes_ -= resource->bytes(); | 116 unused_memory_usage_bytes_ -= resource->bytes(); |
| 92 --resource_count_; | 117 --resource_count_; |
| 93 delete resource; | 118 delete resource; |
| 94 } | 119 } |
| 95 } | 120 } |
| 96 | 121 |
| 97 bool ResourcePool::ResourceUsageTooHigh() { | 122 bool ResourcePool::ResourceUsageTooHigh() { |
| 98 if (resource_count_ > max_resource_count_) | 123 if (resource_count_ > max_resource_count_) |
| 99 return true; | 124 return true; |
| 100 if (memory_usage_bytes_ > max_memory_usage_bytes_) | 125 if (memory_usage_bytes_ > max_memory_usage_bytes_) |
| 101 return true; | 126 return true; |
| 102 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) | 127 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) |
| 103 return true; | 128 return true; |
| 104 return false; | 129 return false; |
| 105 } | 130 } |
| 106 | 131 |
| 107 void ResourcePool::CheckBusyResources(bool wait_if_needed) { | 132 void ResourcePool::CheckBusyResources(bool wait_if_needed) { |
| 108 ResourceList::iterator it = busy_resources_.begin(); | 133 ResourceList::iterator it = busy_resources_.begin(); |
| 109 | 134 |
| 110 while (it != busy_resources_.end()) { | 135 while (it != busy_resources_.end()) { |
| 111 ScopedResource* resource = *it; | 136 ScopedResource* resource = it->resource; |
| 112 | 137 |
| 113 if (wait_if_needed) | 138 if (wait_if_needed) |
| 114 resource_provider_->WaitReadLockIfNeeded(resource->id()); | 139 resource_provider_->WaitReadLockIfNeeded(resource->id()); |
| 115 | 140 |
| 116 if (resource_provider_->CanLockForWrite(resource->id())) { | 141 if (resource_provider_->CanLockForWrite(resource->id())) { |
| 117 DidFinishUsingResource(resource); | 142 DidFinishUsingResource(resource, it->id); |
| 118 it = busy_resources_.erase(it); | 143 it = busy_resources_.erase(it); |
| 119 } else { | 144 } else { |
| 120 ++it; | 145 ++it; |
| 121 } | 146 } |
| 122 } | 147 } |
| 123 } | 148 } |
| 124 | 149 |
| 125 void ResourcePool::DidFinishUsingResource(ScopedResource* resource) { | 150 void ResourcePool::DidFinishUsingResource(ScopedResource* resource, |
| 151 uint64_t id) { | |
| 126 unused_memory_usage_bytes_ += resource->bytes(); | 152 unused_memory_usage_bytes_ += resource->bytes(); |
| 127 unused_resources_.push_back(resource); | 153 unused_resources_.push_back(PoolResource(resource, id)); |
|
piman
2015/05/14 19:50:46
Do we need to check for duplicates here?
danakj
2015/05/14 19:59:47
I don't think so, id is unique.
| |
| 128 } | 154 } |
| 129 | 155 |
| 130 } // namespace cc | 156 } // namespace cc |
| OLD | NEW |