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 | 8 |
| 9 namespace cc { | 9 namespace cc { |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 | 23 |
| 24 ResourcePool::Resource::~Resource() { | 24 ResourcePool::Resource::~Resource() { |
| 25 DCHECK(id()); | 25 DCHECK(id()); |
| 26 DCHECK(resource_provider_); | 26 DCHECK(resource_provider_); |
| 27 resource_provider_->DeleteResource(id()); | 27 resource_provider_->DeleteResource(id()); |
| 28 } | 28 } |
| 29 | 29 |
| 30 ResourcePool::ResourcePool(ResourceProvider* resource_provider) | 30 ResourcePool::ResourcePool(ResourceProvider* resource_provider) |
| 31 : resource_provider_(resource_provider), | 31 : resource_provider_(resource_provider), |
| 32 max_memory_usage_bytes_(0), | 32 max_memory_usage_bytes_(0), |
| 33 memory_usage_bytes_(0) { | 33 memory_usage_bytes_(0), |
| 34 acquired_memory_usage_bytes_(0) { | |
| 34 } | 35 } |
| 35 | 36 |
| 36 ResourcePool::~ResourcePool() { | 37 ResourcePool::~ResourcePool() { |
| 37 SetMaxMemoryUsageBytes(0); | 38 SetMaxMemoryUsageBytes(0); |
| 38 } | 39 } |
| 39 | 40 |
| 40 scoped_ptr<ResourcePool::Resource> ResourcePool::AcquireResource( | 41 scoped_ptr<ResourcePool::Resource> ResourcePool::AcquireResource( |
| 41 const gfx::Size& size, GLenum format) { | 42 const gfx::Size& size, GLenum format) { |
| 42 for (ResourceList::iterator it = resources_.begin(); | 43 for (ResourceList::iterator it = resources_.begin(); |
| 43 it != resources_.end(); ++it) { | 44 it != resources_.end(); ++it) { |
| 44 Resource* resource = *it; | 45 Resource* resource = *it; |
| 45 | 46 |
| 46 // TODO(epenner): It would be nice to DCHECK that this | 47 // TODO(epenner): It would be nice to DCHECK that this |
| 47 // doesn't happen two frames in a row for any resource | 48 // doesn't happen two frames in a row for any resource |
| 48 // in this pool. | 49 // in this pool. |
| 49 if (!resource_provider_->CanLockForWrite(resource->id())) | 50 if (!resource_provider_->CanLockForWrite(resource->id())) |
| 50 continue; | 51 continue; |
| 51 | 52 |
| 52 if (resource->size() != size) | 53 if (resource->size() != size) |
| 53 continue; | 54 continue; |
| 54 if (resource->format() != format) | 55 if (resource->format() != format) |
| 55 continue; | 56 continue; |
| 56 | 57 |
| 57 resources_.erase(it); | 58 resources_.erase(it); |
| 59 acquired_memory_usage_bytes_ += resource->bytes(); | |
| 58 return make_scoped_ptr(resource); | 60 return make_scoped_ptr(resource); |
| 59 } | 61 } |
| 60 | 62 |
| 61 // Create new resource. | 63 // Create new resource. |
| 62 Resource* resource = new Resource( | 64 Resource* resource = new Resource( |
| 63 resource_provider_, size, format); | 65 resource_provider_, size, format); |
| 64 | 66 |
| 65 // Extend all read locks on all resources until the resource is | 67 // Extend all read locks on all resources until the resource is |
| 66 // finished being used, such that we know when resources are | 68 // finished being used, such that we know when resources are |
| 67 // truly safe to recycle. | 69 // truly safe to recycle. |
| 68 resource_provider_->EnableReadLockFences(resource->id(), true); | 70 resource_provider_->EnableReadLockFences(resource->id(), true); |
| 69 | 71 |
| 70 memory_usage_bytes_ += resource->bytes(); | 72 memory_usage_bytes_ += resource->bytes(); |
| 73 acquired_memory_usage_bytes_ += resource->bytes(); | |
| 71 return make_scoped_ptr(resource); | 74 return make_scoped_ptr(resource); |
| 72 } | 75 } |
| 73 | 76 |
| 74 void ResourcePool::ReleaseResource( | 77 void ResourcePool::ReleaseResource( |
| 75 scoped_ptr<ResourcePool::Resource> resource) { | 78 scoped_ptr<ResourcePool::Resource> resource) { |
| 79 acquired_memory_usage_bytes_ -= resource->bytes(); | |
| 76 if (memory_usage_bytes_ > max_memory_usage_bytes_) { | 80 if (memory_usage_bytes_ > max_memory_usage_bytes_) { |
| 77 memory_usage_bytes_ -= resource->bytes(); | 81 memory_usage_bytes_ -= resource->bytes(); |
| 78 return; | 82 return; |
| 79 } | 83 } |
| 80 | 84 |
| 81 resources_.push_back(resource.release()); | 85 resources_.push_back(resource.release()); |
| 82 } | 86 } |
| 83 | 87 |
| 84 void ResourcePool::SetMaxMemoryUsageBytes(size_t max_memory_usage_bytes) { | 88 void ResourcePool::SetMaxMemoryUsageBytes(size_t max_memory_usage_bytes) { |
| 85 max_memory_usage_bytes_ = max_memory_usage_bytes; | 89 max_memory_usage_bytes_ = max_memory_usage_bytes; |
| 86 | 90 |
| 87 while (!resources_.empty()) { | 91 while (!resources_.empty()) { |
| 88 if (memory_usage_bytes_ <= max_memory_usage_bytes_) | 92 if (memory_usage_bytes_ <= max_memory_usage_bytes_) |
| 89 break; | 93 break; |
| 90 Resource* resource = resources_.front(); | 94 |
| 91 resources_.pop_front(); | 95 EvictOneResource(); |
| 92 memory_usage_bytes_ -= resource->bytes(); | |
| 93 delete resource; | |
| 94 } | 96 } |
| 95 } | 97 } |
| 96 | 98 |
| 99 void ResourcePool::ReduceMemoryUsage() { | |
| 100 size_t ten_percent_of_memory = max_memory_usage_bytes_ / 10; | |
|
piman
2013/03/25 18:13:46
What would happen if we didn't try to keep anythin
reveman
2013/03/25 18:36:06
we have to pay the cost of allocating texture, pix
nduca
2013/03/25 18:37:49
How does this compare to a "please drop everything
reveman
2013/03/25 18:47:33
if we drop everything then initializing a new pend
| |
| 101 while (!resources_.empty()) { | |
| 102 size_t wasted_usage_bytes = | |
| 103 memory_usage_bytes_ - acquired_memory_usage_bytes_; | |
| 104 if (wasted_usage_bytes <= ten_percent_of_memory) | |
| 105 break; | |
| 106 | |
| 107 EvictOneResource(); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 void ResourcePool::EvictOneResource() { | |
| 112 DCHECK(!resources_.empty()); | |
| 113 // MRU eviction pattern as least recently used is less likely to | |
| 114 // be blocked by read lock fence. | |
| 115 Resource* resource = resources_.back(); | |
| 116 resources_.pop_back(); | |
| 117 memory_usage_bytes_ -= resource->bytes(); | |
| 118 delete resource; | |
| 119 } | |
| 120 | |
| 97 } // namespace cc | 121 } // namespace cc |
| OLD | NEW |