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..029958dffdba3e0ba662b923f43b8d793424a7c9 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,51 @@ 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; |
| + |
| + auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(), |
| + [id](const PoolResource& pr) { return pr.id == id; }); |
| + if (it == unused_resources_.end()) |
| + return nullptr; |
| + |
| + ScopedResource* resource = it->resource; |
| + DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| + if (resource->format() != format || resource->size() != size) |
| + return nullptr; |
| + |
| + unused_resources_.erase(it); |
| + unused_memory_usage_bytes_ -= resource->bytes(); |
| + return make_scoped_ptr(resource); |
| +} |
| + |
| +scoped_ptr<ScopedResource> ResourcePool::TryAcquireOldBusyResource( |
|
danakj
2015/05/15 03:19:34
I was thinking about it and I think this reusing b
piman
2015/05/15 03:38:35
It sounds ok for UI, but for the renderer, aren't
|
| + const gfx::Size& size, |
| + ResourceFormat format, |
| + uint64_t id) { |
| + if (!id) |
| + return nullptr; |
| + |
| + auto it = std::find_if(busy_resources_.begin(), busy_resources_.end(), |
| + [id](const PoolResource& pr) { return pr.id == id; }); |
| + if (it == busy_resources_.end()) |
| + return nullptr; |
| + |
| + ScopedResource* resource = it->resource; |
| + DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| + if (resource->format() != format || resource->size() != size) |
| + return nullptr; |
| + |
| + return make_scoped_ptr(resource); |
| +} |
| + |
| +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 +129,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 +152,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 +166,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)); |
| } |
| } // namespace cc |