| 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, | 12 ResourcePool::ResourcePool(ResourceProvider* resource_provider, |
| 13 GLenum target, | 13 GLenum target, |
| 14 ResourceFormat format) | 14 bool use_memory_efficient_format) |
| 15 : resource_provider_(resource_provider), | 15 : resource_provider_(resource_provider), |
| 16 target_(target), | 16 target_(target), |
| 17 format_(format), | 17 use_memory_efficient_format_(use_memory_efficient_format), |
| 18 max_memory_usage_bytes_(0), | 18 max_memory_usage_bytes_(0), |
| 19 max_unused_memory_usage_bytes_(0), | 19 max_unused_memory_usage_bytes_(0), |
| 20 max_resource_count_(0), | 20 max_resource_count_(0), |
| 21 memory_usage_bytes_(0), | 21 memory_usage_bytes_(0), |
| 22 unused_memory_usage_bytes_(0), | 22 unused_memory_usage_bytes_(0), |
| 23 resource_count_(0) {} | 23 resource_count_(0) { |
| 24 } |
| 24 | 25 |
| 25 ResourcePool::~ResourcePool() { | 26 ResourcePool::~ResourcePool() { |
| 26 while (!busy_resources_.empty()) { | 27 while (!busy_resources_.empty()) { |
| 27 DidFinishUsingResource(busy_resources_.front()); | 28 DidFinishUsingResource(busy_resources_.front()); |
| 28 busy_resources_.pop_front(); | 29 busy_resources_.pop_front(); |
| 29 } | 30 } |
| 30 | 31 |
| 31 SetResourceUsageLimits(0, 0, 0); | 32 SetResourceUsageLimits(0, 0, 0); |
| 32 DCHECK_EQ(0u, unused_resources_.size()); | 33 for (UnusedMap::const_iterator it = unused_resources_.begin(); |
| 34 it != unused_resources_.end(); ++it) { |
| 35 DCHECK_EQ(0u, it->second.size()); |
| 36 } |
| 33 DCHECK_EQ(0u, memory_usage_bytes_); | 37 DCHECK_EQ(0u, memory_usage_bytes_); |
| 34 DCHECK_EQ(0u, unused_memory_usage_bytes_); | 38 DCHECK_EQ(0u, unused_memory_usage_bytes_); |
| 35 DCHECK_EQ(0u, resource_count_); | 39 DCHECK_EQ(0u, resource_count_); |
| 36 } | 40 } |
| 37 | 41 |
| 38 scoped_ptr<ScopedResource> ResourcePool::AcquireResource( | 42 scoped_ptr<ScopedResource> ResourcePool::AcquireResource( |
| 39 const gfx::Size& size) { | 43 const gfx::Size& size, |
| 40 for (ResourceList::iterator it = unused_resources_.begin(); | 44 ResourceFormat format) { |
| 41 it != unused_resources_.end(); | 45 ResourceList& unused = unused_resources_[format]; |
| 42 ++it) { | 46 for (ResourceList::iterator it = unused.begin(); it != unused.end(); ++it) { |
| 43 ScopedResource* resource = *it; | 47 ScopedResource* resource = *it; |
| 44 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 48 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| 45 | 49 |
| 46 if (resource->size() != size) | 50 if (resource->size() != size) |
| 47 continue; | 51 continue; |
| 48 | 52 |
| 49 unused_resources_.erase(it); | 53 unused.erase(it); |
| 50 unused_memory_usage_bytes_ -= resource->bytes(); | 54 unused_memory_usage_bytes_ -= resource->bytes(); |
| 51 return make_scoped_ptr(resource); | 55 return make_scoped_ptr(resource); |
| 52 } | 56 } |
| 53 | 57 |
| 54 scoped_ptr<ScopedResource> resource = | 58 scoped_ptr<ScopedResource> resource = |
| 55 ScopedResource::Create(resource_provider_); | 59 ScopedResource::Create(resource_provider_); |
| 56 resource->AllocateManaged(size, target_, format_); | 60 resource->AllocateManaged(size, target_, format); |
| 57 | 61 |
| 58 memory_usage_bytes_ += resource->bytes(); | 62 memory_usage_bytes_ += resource->bytes(); |
| 59 ++resource_count_; | 63 ++resource_count_; |
| 60 return resource.Pass(); | 64 return resource.Pass(); |
| 61 } | 65 } |
| 62 | 66 |
| 67 scoped_ptr<ScopedResource> ResourcePool::AcquireResource( |
| 68 const gfx::Size& size, |
| 69 ResourceFormatUsage usage) { |
| 70 return AcquireResource(size, resource_format(usage)); |
| 71 } |
| 72 |
| 63 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource) { | 73 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource) { |
| 64 busy_resources_.push_back(resource.release()); | 74 busy_resources_.push_back(resource.release()); |
| 65 } | 75 } |
| 66 | 76 |
| 67 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, | 77 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, |
| 68 size_t max_unused_memory_usage_bytes, | 78 size_t max_unused_memory_usage_bytes, |
| 69 size_t max_resource_count) { | 79 size_t max_resource_count) { |
| 70 max_memory_usage_bytes_ = max_memory_usage_bytes; | 80 max_memory_usage_bytes_ = max_memory_usage_bytes; |
| 71 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes; | 81 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes; |
| 72 max_resource_count_ = max_resource_count; | 82 max_resource_count_ = max_resource_count; |
| 73 | 83 |
| 74 ReduceResourceUsage(); | 84 ReduceResourceUsage(); |
| 75 } | 85 } |
| 76 | 86 |
| 77 void ResourcePool::ReduceResourceUsage() { | 87 void ResourcePool::ReduceResourceUsage() { |
| 78 while (!unused_resources_.empty()) { | 88 for (UnusedMap::iterator it = unused_resources_.begin(); |
| 79 if (!ResourceUsageTooHigh()) | 89 it != unused_resources_.end(); ++it) { |
| 80 break; | 90 ResourceList& unused = it->second; |
| 91 while (!unused.empty()) { |
| 92 if (!ResourceUsageTooHigh()) |
| 93 break; |
| 81 | 94 |
| 82 // LRU eviction pattern. Most recently used might be blocked by | 95 // LRU eviction pattern. Most recently used might be blocked by |
| 83 // a read lock fence but it's still better to evict the least | 96 // a read lock fence but it's still better to evict the least |
| 84 // recently used as it prevents a resource that is hard to reuse | 97 // recently used as it prevents a resource that is hard to reuse |
| 85 // because of unique size from being kept around. Resources that | 98 // because of unique size from being kept around. Resources that |
| 86 // can't be locked for write might also not be truly free-able. | 99 // can't be locked for write might also not be truly free-able. |
| 87 // We can free the resource here but it doesn't mean that the | 100 // We can free the resource here but it doesn't mean that the |
| 88 // memory is necessarily returned to the OS. | 101 // memory is necessarily returned to the OS. |
| 89 ScopedResource* resource = unused_resources_.front(); | 102 ScopedResource* resource = unused.front(); |
| 90 unused_resources_.pop_front(); | 103 unused.pop_front(); |
| 91 memory_usage_bytes_ -= resource->bytes(); | 104 memory_usage_bytes_ -= resource->bytes(); |
| 92 unused_memory_usage_bytes_ -= resource->bytes(); | 105 unused_memory_usage_bytes_ -= resource->bytes(); |
| 93 --resource_count_; | 106 --resource_count_; |
| 94 delete resource; | 107 delete resource; |
| 108 } |
| 95 } | 109 } |
| 96 } | 110 } |
| 97 | 111 |
| 98 bool ResourcePool::ResourceUsageTooHigh() { | 112 bool ResourcePool::ResourceUsageTooHigh() { |
| 99 if (resource_count_ > max_resource_count_) | 113 if (resource_count_ > max_resource_count_) |
| 100 return true; | 114 return true; |
| 101 if (memory_usage_bytes_ > max_memory_usage_bytes_) | 115 if (memory_usage_bytes_ > max_memory_usage_bytes_) |
| 102 return true; | 116 return true; |
| 103 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) | 117 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) |
| 104 return true; | 118 return true; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 116 | 130 |
| 117 if (resource_provider_->CanLockForWrite(resource->id())) { | 131 if (resource_provider_->CanLockForWrite(resource->id())) { |
| 118 DidFinishUsingResource(resource); | 132 DidFinishUsingResource(resource); |
| 119 it = busy_resources_.erase(it); | 133 it = busy_resources_.erase(it); |
| 120 } else { | 134 } else { |
| 121 ++it; | 135 ++it; |
| 122 } | 136 } |
| 123 } | 137 } |
| 124 } | 138 } |
| 125 | 139 |
| 140 ResourceFormat ResourcePool::resource_format(ResourceFormatUsage usage) const { |
| 141 if (use_memory_efficient_format_) { |
| 142 return resource_provider_->memory_efficient_texture_format(usage); |
| 143 } else { |
| 144 return resource_provider_->best_texture_format(); |
| 145 } |
| 146 } |
| 147 |
| 148 size_t ResourcePool::acquired_resource_count() const { |
| 149 size_t result = resource_count_; |
| 150 for (UnusedMap::const_iterator it = unused_resources_.begin(); |
| 151 it != unused_resources_.end(); ++it) { |
| 152 result -= it->second.size(); |
| 153 } |
| 154 return resource_count_; |
| 155 } |
| 156 |
| 126 void ResourcePool::DidFinishUsingResource(ScopedResource* resource) { | 157 void ResourcePool::DidFinishUsingResource(ScopedResource* resource) { |
| 127 unused_memory_usage_bytes_ += resource->bytes(); | 158 unused_memory_usage_bytes_ += resource->bytes(); |
| 128 unused_resources_.push_back(resource); | 159 unused_resources_[resource->format()].push_back(resource); |
| 129 } | 160 } |
| 130 | 161 |
| 131 } // namespace cc | 162 } // namespace cc |
| OLD | NEW |