| 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/resource_util.h" |
| 8 #include "cc/resources/scoped_resource.h" | 9 #include "cc/resources/scoped_resource.h" |
| 9 | 10 |
| 10 namespace cc { | 11 namespace cc { |
| 11 | 12 |
| 12 ResourcePool::ResourcePool(ResourceProvider* resource_provider, GLenum target) | 13 ResourcePool::ResourcePool(ResourceProvider* resource_provider, GLenum target) |
| 13 : resource_provider_(resource_provider), | 14 : resource_provider_(resource_provider), |
| 14 target_(target), | 15 target_(target), |
| 15 max_memory_usage_bytes_(0), | 16 max_memory_usage_bytes_(0), |
| 16 max_unused_memory_usage_bytes_(0), | 17 max_unused_memory_usage_bytes_(0), |
| 17 max_resource_count_(0), | 18 max_resource_count_(0), |
| (...skipping 22 matching lines...) Expand all Loading... |
| 40 ++it) { | 41 ++it) { |
| 41 ScopedResource* resource = it->resource; | 42 ScopedResource* resource = it->resource; |
| 42 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 43 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| 43 | 44 |
| 44 if (resource->format() != format) | 45 if (resource->format() != format) |
| 45 continue; | 46 continue; |
| 46 if (resource->size() != size) | 47 if (resource->size() != size) |
| 47 continue; | 48 continue; |
| 48 | 49 |
| 49 unused_resources_.erase(it); | 50 unused_resources_.erase(it); |
| 50 unused_memory_usage_bytes_ -= | 51 unused_memory_usage_bytes_ -= ResourceUtil::SizeInBytes(size, format); |
| 51 Resource::UncheckedMemorySizeBytes(size, format); | |
| 52 return make_scoped_ptr(resource); | 52 return make_scoped_ptr(resource); |
| 53 } | 53 } |
| 54 | 54 |
| 55 scoped_ptr<ScopedResource> resource = | 55 scoped_ptr<ScopedResource> resource = |
| 56 ScopedResource::Create(resource_provider_); | 56 ScopedResource::Create(resource_provider_); |
| 57 resource->AllocateManaged(size, target_, format); | 57 resource->AllocateManaged(size, target_, format); |
| 58 | 58 |
| 59 DCHECK(Resource::VerifySizeInBytes(resource->size(), resource->format())); | 59 DCHECK(ResourceUtil::VerifySizeInBytes(resource->size(), resource->format())); |
| 60 memory_usage_bytes_ += | 60 memory_usage_bytes_ += |
| 61 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format()); | 61 ResourceUtil::SizeInBytes(resource->size(), resource->format()); |
| 62 ++resource_count_; | 62 ++resource_count_; |
| 63 return resource.Pass(); | 63 return resource.Pass(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 scoped_ptr<ScopedResource> ResourcePool::TryAcquireResourceWithContentId( | 66 scoped_ptr<ScopedResource> ResourcePool::TryAcquireResourceWithContentId( |
| 67 uint64_t content_id) { | 67 uint64_t content_id) { |
| 68 DCHECK(content_id); | 68 DCHECK(content_id); |
| 69 | 69 |
| 70 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(), | 70 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(), |
| 71 [content_id](const PoolResource& pool_resource) { | 71 [content_id](const PoolResource& pool_resource) { |
| 72 return pool_resource.content_id == content_id; | 72 return pool_resource.content_id == content_id; |
| 73 }); | 73 }); |
| 74 if (it == unused_resources_.end()) | 74 if (it == unused_resources_.end()) |
| 75 return nullptr; | 75 return nullptr; |
| 76 | 76 |
| 77 ScopedResource* resource = it->resource; | 77 ScopedResource* resource = it->resource; |
| 78 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 78 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| 79 | 79 |
| 80 unused_resources_.erase(it); | 80 unused_resources_.erase(it); |
| 81 unused_memory_usage_bytes_ -= | 81 unused_memory_usage_bytes_ -= |
| 82 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format()); | 82 ResourceUtil::SizeInBytes(resource->size(), resource->format()); |
| 83 return make_scoped_ptr(resource); | 83 return make_scoped_ptr(resource); |
| 84 } | 84 } |
| 85 | 85 |
| 86 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource, | 86 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource, |
| 87 uint64_t content_id) { | 87 uint64_t content_id) { |
| 88 busy_resources_.push_back(PoolResource(resource.release(), content_id)); | 88 busy_resources_.push_back(PoolResource(resource.release(), content_id)); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, | 91 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, |
| 92 size_t max_unused_memory_usage_bytes, | 92 size_t max_unused_memory_usage_bytes, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 105 | 105 |
| 106 // LRU eviction pattern. Most recently used might be blocked by | 106 // LRU eviction pattern. Most recently used might be blocked by |
| 107 // 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 |
| 108 // 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 |
| 109 // because of unique size from being kept around. Resources that | 109 // because of unique size from being kept around. Resources that |
| 110 // 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. |
| 111 // 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 |
| 112 // memory is necessarily returned to the OS. | 112 // memory is necessarily returned to the OS. |
| 113 ScopedResource* resource = unused_resources_.front().resource; | 113 ScopedResource* resource = unused_resources_.front().resource; |
| 114 unused_resources_.pop_front(); | 114 unused_resources_.pop_front(); |
| 115 size_t resource_bytes = Resource::UncheckedMemorySizeBytes( | 115 size_t resource_bytes = |
| 116 resource->size(), resource->format()); | 116 ResourceUtil::SizeInBytes(resource->size(), resource->format()); |
| 117 memory_usage_bytes_ -= resource_bytes; | 117 memory_usage_bytes_ -= resource_bytes; |
| 118 unused_memory_usage_bytes_ -= resource_bytes; | 118 unused_memory_usage_bytes_ -= resource_bytes; |
| 119 --resource_count_; | 119 --resource_count_; |
| 120 delete resource; | 120 delete resource; |
| 121 } | 121 } |
| 122 } | 122 } |
| 123 | 123 |
| 124 bool ResourcePool::ResourceUsageTooHigh() { | 124 bool ResourcePool::ResourceUsageTooHigh() { |
| 125 if (resource_count_ > max_resource_count_) | 125 if (resource_count_ > max_resource_count_) |
| 126 return true; | 126 return true; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 145 it = busy_resources_.erase(it); | 145 it = busy_resources_.erase(it); |
| 146 } else { | 146 } else { |
| 147 ++it; | 147 ++it; |
| 148 } | 148 } |
| 149 } | 149 } |
| 150 } | 150 } |
| 151 | 151 |
| 152 void ResourcePool::DidFinishUsingResource(ScopedResource* resource, | 152 void ResourcePool::DidFinishUsingResource(ScopedResource* resource, |
| 153 uint64_t content_id) { | 153 uint64_t content_id) { |
| 154 unused_memory_usage_bytes_ += | 154 unused_memory_usage_bytes_ += |
| 155 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format()); | 155 ResourceUtil::SizeInBytes(resource->size(), resource->format()); |
| 156 unused_resources_.push_back(PoolResource(resource, content_id)); | 156 unused_resources_.push_back(PoolResource(resource, content_id)); |
| 157 } | 157 } |
| 158 | 158 |
| 159 } // namespace cc | 159 } // namespace cc |
| OLD | NEW |