Chromium Code Reviews| Index: cc/resources/resource_pool.cc |
| diff --git a/cc/resources/resource_pool.cc b/cc/resources/resource_pool.cc |
| index 22847809ba3c196f66df1112d978d56342665b3a..7e216c180f8cea5e89978b3b1a7ae2e468d39200 100644 |
| --- a/cc/resources/resource_pool.cc |
| +++ b/cc/resources/resource_pool.cc |
| @@ -21,7 +21,8 @@ ResourcePool::ResourcePool(ResourceProvider* resource_provider, GLenum target) |
| ResourcePool::~ResourcePool() { |
| while (!busy_resources_.empty()) { |
| - DidFinishUsingResource(busy_resources_.front()); |
| + auto const& front = busy_resources_.front(); |
| + DidFinishUsingResource(front.resource, front.id); |
| busy_resources_.pop_front(); |
| } |
| @@ -37,7 +38,7 @@ scoped_ptr<ScopedResource> ResourcePool::AcquireResource( |
| for (ResourceList::iterator it = unused_resources_.begin(); |
| it != unused_resources_.end(); |
| ++it) { |
| - ScopedResource* resource = *it; |
| + ScopedResource* resource = it->resource; |
| DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| if (resource->format() != format) |
| @@ -59,8 +60,32 @@ scoped_ptr<ScopedResource> ResourcePool::AcquireResource( |
| return resource.Pass(); |
| } |
| -void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource) { |
| - busy_resources_.push_back(resource.release()); |
| +scoped_ptr<ScopedResource> ResourcePool::TryAcquireOldResource( |
| + const gfx::Size& size, |
| + ResourceFormat format, |
| + uint64_t id) { |
| + if (!id) |
| + return nullptr; |
| + |
| + 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'
|
| + ++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:/
|
| + const PoolResource& pr = *it; |
| + if (pr.id != id || pr.resource->format() != format || |
| + 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
|
| + continue; |
| + |
| + ScopedResource* resource = pr.resource; |
| + unused_memory_usage_bytes_ -= pr.resource->bytes(); |
| + unused_resources_.erase(it); |
| + return make_scoped_ptr(resource); |
| + } |
| + |
| + return nullptr; |
| +} |
| + |
| +void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource, |
| + uint64_t id) { |
| + busy_resources_.push_back(PoolResource(resource.release(), id)); |
| } |
| void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, |
| @@ -85,7 +110,7 @@ void ResourcePool::ReduceResourceUsage() { |
| // can't be locked for write might also not be truly free-able. |
| // We can free the resource here but it doesn't mean that the |
| // memory is necessarily returned to the OS. |
| - ScopedResource* resource = unused_resources_.front(); |
| + ScopedResource* resource = unused_resources_.front().resource; |
| unused_resources_.pop_front(); |
| memory_usage_bytes_ -= resource->bytes(); |
| unused_memory_usage_bytes_ -= resource->bytes(); |
| @@ -108,13 +133,13 @@ void ResourcePool::CheckBusyResources(bool wait_if_needed) { |
| ResourceList::iterator it = busy_resources_.begin(); |
| while (it != busy_resources_.end()) { |
| - ScopedResource* resource = *it; |
| + ScopedResource* resource = it->resource; |
| if (wait_if_needed) |
| resource_provider_->WaitReadLockIfNeeded(resource->id()); |
| if (resource_provider_->CanLockForWrite(resource->id())) { |
| - DidFinishUsingResource(resource); |
| + DidFinishUsingResource(resource, it->id); |
| it = busy_resources_.erase(it); |
| } else { |
| ++it; |
| @@ -122,9 +147,10 @@ void ResourcePool::CheckBusyResources(bool wait_if_needed) { |
| } |
| } |
| -void ResourcePool::DidFinishUsingResource(ScopedResource* resource) { |
| +void ResourcePool::DidFinishUsingResource(ScopedResource* resource, |
| + uint64_t id) { |
| unused_memory_usage_bytes_ += resource->bytes(); |
| - unused_resources_.push_back(resource); |
| + 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.
|
| } |
| } // namespace cc |