| 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 23 matching lines...) Expand all Loading... |
| 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_ -= |
| 51 Resource::UncheckedMemorySizeBytes(size, format); | 52 ResourceUtil::UncheckedSizeInBytes(size, format); |
| 52 return make_scoped_ptr(resource); | 53 return make_scoped_ptr(resource); |
| 53 } | 54 } |
| 54 | 55 |
| 55 scoped_ptr<ScopedResource> resource = | 56 scoped_ptr<ScopedResource> resource = |
| 56 ScopedResource::Create(resource_provider_); | 57 ScopedResource::Create(resource_provider_); |
| 57 resource->AllocateManaged(size, target_, format); | 58 resource->AllocateManaged(size, target_, format); |
| 58 | 59 |
| 59 DCHECK(Resource::VerifySizeInBytes(resource->size(), resource->format())); | 60 DCHECK(ResourceUtil::VerifySizeInBytes<size_t>(resource->size(), |
| 61 resource->format())); |
| 60 memory_usage_bytes_ += | 62 memory_usage_bytes_ += |
| 61 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format()); | 63 ResourceUtil::UncheckedSizeInBytes(resource->size(), resource->format()); |
| 62 ++resource_count_; | 64 ++resource_count_; |
| 63 return resource.Pass(); | 65 return resource.Pass(); |
| 64 } | 66 } |
| 65 | 67 |
| 66 scoped_ptr<ScopedResource> ResourcePool::TryAcquireResourceWithContentId( | 68 scoped_ptr<ScopedResource> ResourcePool::TryAcquireResourceWithContentId( |
| 67 uint64_t content_id) { | 69 uint64_t content_id) { |
| 68 DCHECK(content_id); | 70 DCHECK(content_id); |
| 69 | 71 |
| 70 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(), | 72 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(), |
| 71 [content_id](const PoolResource& pool_resource) { | 73 [content_id](const PoolResource& pool_resource) { |
| 72 return pool_resource.content_id == content_id; | 74 return pool_resource.content_id == content_id; |
| 73 }); | 75 }); |
| 74 if (it == unused_resources_.end()) | 76 if (it == unused_resources_.end()) |
| 75 return nullptr; | 77 return nullptr; |
| 76 | 78 |
| 77 ScopedResource* resource = it->resource; | 79 ScopedResource* resource = it->resource; |
| 78 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 80 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| 79 | 81 |
| 80 unused_resources_.erase(it); | 82 unused_resources_.erase(it); |
| 81 unused_memory_usage_bytes_ -= | 83 unused_memory_usage_bytes_ -= |
| 82 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format()); | 84 ResourceUtil::UncheckedSizeInBytes(resource->size(), resource->format()); |
| 83 return make_scoped_ptr(resource); | 85 return make_scoped_ptr(resource); |
| 84 } | 86 } |
| 85 | 87 |
| 86 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource, | 88 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource, |
| 87 uint64_t content_id) { | 89 uint64_t content_id) { |
| 88 busy_resources_.push_back(PoolResource(resource.release(), content_id)); | 90 busy_resources_.push_back(PoolResource(resource.release(), content_id)); |
| 89 } | 91 } |
| 90 | 92 |
| 91 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, | 93 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, |
| 92 size_t max_unused_memory_usage_bytes, | 94 size_t max_unused_memory_usage_bytes, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 105 | 107 |
| 106 // LRU eviction pattern. Most recently used might be blocked by | 108 // LRU eviction pattern. Most recently used might be blocked by |
| 107 // a read lock fence but it's still better to evict the least | 109 // 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 | 110 // recently used as it prevents a resource that is hard to reuse |
| 109 // because of unique size from being kept around. Resources that | 111 // because of unique size from being kept around. Resources that |
| 110 // can't be locked for write might also not be truly free-able. | 112 // 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 | 113 // We can free the resource here but it doesn't mean that the |
| 112 // memory is necessarily returned to the OS. | 114 // memory is necessarily returned to the OS. |
| 113 ScopedResource* resource = unused_resources_.front().resource; | 115 ScopedResource* resource = unused_resources_.front().resource; |
| 114 unused_resources_.pop_front(); | 116 unused_resources_.pop_front(); |
| 115 size_t resource_bytes = Resource::UncheckedMemorySizeBytes( | 117 size_t resource_bytes = ResourceUtil::UncheckedSizeInBytes( |
| 116 resource->size(), resource->format()); | 118 resource->size(), resource->format()); |
| 117 memory_usage_bytes_ -= resource_bytes; | 119 memory_usage_bytes_ -= resource_bytes; |
| 118 unused_memory_usage_bytes_ -= resource_bytes; | 120 unused_memory_usage_bytes_ -= resource_bytes; |
| 119 --resource_count_; | 121 --resource_count_; |
| 120 delete resource; | 122 delete resource; |
| 121 } | 123 } |
| 122 } | 124 } |
| 123 | 125 |
| 124 bool ResourcePool::ResourceUsageTooHigh() { | 126 bool ResourcePool::ResourceUsageTooHigh() { |
| 125 if (resource_count_ > max_resource_count_) | 127 if (resource_count_ > max_resource_count_) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 145 it = busy_resources_.erase(it); | 147 it = busy_resources_.erase(it); |
| 146 } else { | 148 } else { |
| 147 ++it; | 149 ++it; |
| 148 } | 150 } |
| 149 } | 151 } |
| 150 } | 152 } |
| 151 | 153 |
| 152 void ResourcePool::DidFinishUsingResource(ScopedResource* resource, | 154 void ResourcePool::DidFinishUsingResource(ScopedResource* resource, |
| 153 uint64_t content_id) { | 155 uint64_t content_id) { |
| 154 unused_memory_usage_bytes_ += | 156 unused_memory_usage_bytes_ += |
| 155 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format()); | 157 ResourceUtil::UncheckedSizeInBytes(resource->size(), resource->format()); |
| 156 unused_resources_.push_back(PoolResource(resource, content_id)); | 158 unused_resources_.push_back(PoolResource(resource, content_id)); |
| 157 } | 159 } |
| 158 | 160 |
| 159 } // namespace cc | 161 } // namespace cc |
| OLD | NEW |