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 <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 | 84 |
| 85 SetResourceUsageLimits(0, 0); | 85 SetResourceUsageLimits(0, 0); |
| 86 DCHECK_EQ(0u, unused_resources_.size()); | 86 DCHECK_EQ(0u, unused_resources_.size()); |
| 87 DCHECK_EQ(0u, in_use_memory_usage_bytes_); | 87 DCHECK_EQ(0u, in_use_memory_usage_bytes_); |
| 88 DCHECK_EQ(0u, total_memory_usage_bytes_); | 88 DCHECK_EQ(0u, total_memory_usage_bytes_); |
| 89 DCHECK_EQ(0u, total_resource_count_); | 89 DCHECK_EQ(0u, total_resource_count_); |
| 90 } | 90 } |
| 91 | 91 |
| 92 Resource* ResourcePool::AcquireResource(const gfx::Size& size, | 92 Resource* ResourcePool::AcquireResource(const gfx::Size& size, |
| 93 ResourceFormat format) { | 93 ResourceFormat format) { |
| 94 for (ResourceDeque::iterator it = unused_resources_.begin(); | 94 // Traverse the |unused_resources_| from rear end as MRU resources are |
| 95 it != unused_resources_.end(); ++it) { | 95 // held at the rear end of the queue. This touches LRU resources only if |
| 96 // needed, which inreases possibility of expiring more LRU resources | |
| 97 // within kResourceExpirationDelayMs. | |
| 98 for (ResourceDeque::reverse_iterator it = unused_resources_.rbegin(); | |
| 99 it != unused_resources_.rend(); ++it) { | |
| 96 ScopedResource* resource = *it; | 100 ScopedResource* resource = *it; |
| 97 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 101 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| 98 | 102 |
| 99 if (resource->format() != format) | 103 if (resource->format() != format) |
| 100 continue; | 104 continue; |
| 101 if (resource->size() != size) | 105 if (resource->size() != size) |
| 102 continue; | 106 continue; |
| 103 | 107 |
| 104 // Transfer resource to |in_use_resources_|. | 108 // Transfer resource to |in_use_resources_|. |
| 105 in_use_resources_.set(resource->id(), unused_resources_.take(it)); | 109 in_use_resources_.set(resource->id(), unused_resources_.rtake(it)); |
| 106 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | 110 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 107 resource->size(), resource->format()); | 111 resource->size(), resource->format()); |
| 108 return resource; | 112 return resource; |
| 109 } | 113 } |
| 110 | 114 |
| 111 scoped_ptr<PoolResource> pool_resource = | 115 scoped_ptr<PoolResource> pool_resource = |
| 112 PoolResource::Create(resource_provider_); | 116 PoolResource::Create(resource_provider_); |
| 113 GLenum target = | 117 GLenum target = |
| 114 target_ ? target_ : resource_provider_->GetImageTextureTarget(format); | 118 target_ ? target_ : resource_provider_->GetImageTextureTarget(format); |
| 115 pool_resource->AllocateManaged(size, target, format); | 119 pool_resource->AllocateManaged(size, target, format); |
| 116 | 120 |
| 117 DCHECK(ResourceUtil::VerifySizeInBytes<size_t>(pool_resource->size(), | 121 DCHECK(ResourceUtil::VerifySizeInBytes<size_t>(pool_resource->size(), |
| 118 pool_resource->format())); | 122 pool_resource->format())); |
| 119 total_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | 123 total_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 120 pool_resource->size(), pool_resource->format()); | 124 pool_resource->size(), pool_resource->format()); |
| 121 ++total_resource_count_; | 125 ++total_resource_count_; |
| 122 | 126 |
| 123 Resource* resource = pool_resource.get(); | 127 Resource* resource = pool_resource.get(); |
| 124 in_use_resources_.set(resource->id(), pool_resource.Pass()); | 128 in_use_resources_.set(resource->id(), pool_resource.Pass()); |
| 125 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | 129 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 126 resource->size(), resource->format()); | 130 resource->size(), resource->format()); |
| 127 return resource; | 131 return resource; |
| 128 } | 132 } |
| 129 | 133 |
| 130 Resource* ResourcePool::TryAcquireResourceWithContentId(uint64_t content_id) { | 134 Resource* ResourcePool::TryAcquireResourceWithContentId(uint64_t content_id) { |
|
reveman
2015/11/02 01:47:07
It shouldn't matter in what order we search the re
prashant.n
2015/11/02 04:07:20
Ahh! I missed one content id = one resource (next_
| |
| 131 DCHECK(content_id); | 135 DCHECK(content_id); |
| 132 | 136 |
| 133 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(), | 137 // TODO(prashant.n): Move resource finding logic to common function. |
|
reveman
2015/11/02 01:47:07
I don't see much duplication of resource finding l
prashant.n
2015/11/02 04:07:20
Yes most of the code in getting resource from unus
reveman
2015/11/02 04:32:59
Ok, feel free to put up a patch for that and I'll
| |
| 134 [content_id](const PoolResource* pool_resource) { | 138 ResourceDeque::reverse_iterator it = unused_resources_.rbegin(); |
| 135 return pool_resource->content_id() == content_id; | 139 for (; it != unused_resources_.rend(); ++it) { |
| 136 }); | 140 PoolResource* pool_resource = *it; |
| 137 if (it == unused_resources_.end()) | 141 if (pool_resource->content_id() != content_id) |
| 142 continue; | |
| 143 } | |
| 144 | |
| 145 if (it == unused_resources_.rend()) | |
| 138 return nullptr; | 146 return nullptr; |
| 139 | 147 |
| 140 Resource* resource = *it; | 148 Resource* resource = *it; |
| 141 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 149 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| 142 | 150 |
| 143 // Transfer resource to |in_use_resources_|. | 151 // Transfer resource to |in_use_resources_|. |
| 144 in_use_resources_.set(resource->id(), unused_resources_.take(it)); | 152 in_use_resources_.set(resource->id(), unused_resources_.rtake(it)); |
| 145 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | 153 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 146 resource->size(), resource->format()); | 154 resource->size(), resource->format()); |
| 147 return resource; | 155 return resource; |
| 148 } | 156 } |
| 149 | 157 |
| 150 void ResourcePool::ReleaseResource(Resource* resource, uint64_t content_id) { | 158 void ResourcePool::ReleaseResource(Resource* resource, uint64_t content_id) { |
| 151 auto it = in_use_resources_.find(resource->id()); | 159 auto it = in_use_resources_.find(resource->id()); |
| 152 DCHECK(it != in_use_resources_.end()); | 160 DCHECK(it != in_use_resources_.end()); |
| 153 | 161 |
| 154 PoolResource* pool_resource = it->second; | 162 PoolResource* pool_resource = it->second; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 296 for (const auto& resource : busy_resources_) { | 304 for (const auto& resource : busy_resources_) { |
| 297 resource->OnMemoryDump(pmd, resource_provider_, false /* is_free */); | 305 resource->OnMemoryDump(pmd, resource_provider_, false /* is_free */); |
| 298 } | 306 } |
| 299 for (const auto& entry : in_use_resources_) { | 307 for (const auto& entry : in_use_resources_) { |
| 300 entry.second->OnMemoryDump(pmd, resource_provider_, false /* is_free */); | 308 entry.second->OnMemoryDump(pmd, resource_provider_, false /* is_free */); |
| 301 } | 309 } |
| 302 return true; | 310 return true; |
| 303 } | 311 } |
| 304 | 312 |
| 305 } // namespace cc | 313 } // namespace cc |
| OLD | NEW |