| 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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 // Finding resources in |unused_resources_| from MRU to LRU direction, touches | 94 // Finding resources in |unused_resources_| from MRU to LRU direction, touches |
| 95 // LRU resources only if needed, which increases possibility of expiring more | 95 // LRU resources only if needed, which increases possibility of expiring more |
| 96 // LRU resources within kResourceExpirationDelayMs. | 96 // LRU resources within kResourceExpirationDelayMs. |
| 97 for (ResourceDeque::iterator it = unused_resources_.begin(); | 97 { |
| 98 it != unused_resources_.end(); ++it) { | 98 TRACE_EVENT1("cc, pras", "ResourcePool::AcquireResource", "total_unused", |
| 99 ScopedResource* resource = *it; | 99 unused_resources_.size()); |
| 100 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | |
| 101 | 100 |
| 102 if (resource->format() != format) | 101 for (ResourceDeque::iterator it = unused_resources_.begin(); |
| 103 continue; | 102 it != unused_resources_.end(); ++it) { |
| 104 if (resource->size() != size) | 103 ScopedResource* resource = *it; |
| 105 continue; | 104 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| 106 | 105 |
| 107 // Transfer resource to |in_use_resources_|. | 106 if (resource->format() != format) |
| 108 in_use_resources_.set(resource->id(), unused_resources_.take(it)); | 107 continue; |
| 109 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | 108 if (resource->size() != size) |
| 110 resource->size(), resource->format()); | 109 continue; |
| 111 return resource; | 110 |
| 111 // Transfer resource to |in_use_resources_|. |
| 112 in_use_resources_.set(resource->id(), unused_resources_.take(it)); |
| 113 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 114 resource->size(), resource->format()); |
| 115 return resource; |
| 116 } |
| 112 } | 117 } |
| 113 | 118 |
| 114 scoped_ptr<PoolResource> pool_resource = | 119 scoped_ptr<PoolResource> pool_resource = |
| 115 PoolResource::Create(resource_provider_); | 120 PoolResource::Create(resource_provider_); |
| 116 GLenum target = | 121 GLenum target = |
| 117 target_ ? target_ : resource_provider_->GetImageTextureTarget(format); | 122 target_ ? target_ : resource_provider_->GetImageTextureTarget(format); |
| 118 pool_resource->AllocateManaged(size, target, format); | 123 pool_resource->AllocateManaged(size, target, format); |
| 119 | 124 |
| 120 DCHECK(ResourceUtil::VerifySizeInBytes<size_t>(pool_resource->size(), | 125 DCHECK(ResourceUtil::VerifySizeInBytes<size_t>(pool_resource->size(), |
| 121 pool_resource->format())); | 126 pool_resource->format())); |
| 122 total_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | 127 total_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 123 pool_resource->size(), pool_resource->format()); | 128 pool_resource->size(), pool_resource->format()); |
| 124 ++total_resource_count_; | 129 ++total_resource_count_; |
| 125 | 130 |
| 126 Resource* resource = pool_resource.get(); | 131 Resource* resource = pool_resource.get(); |
| 127 in_use_resources_.set(resource->id(), pool_resource.Pass()); | 132 in_use_resources_.set(resource->id(), pool_resource.Pass()); |
| 128 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | 133 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 129 resource->size(), resource->format()); | 134 resource->size(), resource->format()); |
| 130 return resource; | 135 return resource; |
| 131 } | 136 } |
| 132 | 137 |
| 133 Resource* ResourcePool::TryAcquireResourceWithContentId(uint64_t content_id) { | 138 Resource* ResourcePool::TryAcquireResourceWithContentId(uint64_t content_id) { |
| 134 DCHECK(content_id); | 139 DCHECK(content_id); |
| 135 | 140 |
| 141 TRACE_EVENT_BEGIN1("cc, pras", "ResourcePool::TryAcquireResource", |
| 142 "total_unused", unused_resources_.size()); |
| 143 |
| 136 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(), | 144 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(), |
| 137 [content_id](const PoolResource* pool_resource) { | 145 [content_id](const PoolResource* pool_resource) { |
| 138 return pool_resource->content_id() == content_id; | 146 return pool_resource->content_id() == content_id; |
| 139 }); | 147 }); |
| 148 |
| 149 TRACE_EVENT_END0("cc, pras", "ResourcePool::TryAcquireResource"); |
| 150 |
| 140 if (it == unused_resources_.end()) | 151 if (it == unused_resources_.end()) |
| 141 return nullptr; | 152 return nullptr; |
| 142 | 153 |
| 143 Resource* resource = *it; | 154 Resource* resource = *it; |
| 144 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 155 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
| 145 | 156 |
| 146 // Transfer resource to |in_use_resources_|. | 157 // Transfer resource to |in_use_resources_|. |
| 147 in_use_resources_.set(resource->id(), unused_resources_.take(it)); | 158 in_use_resources_.set(resource->id(), unused_resources_.take(it)); |
| 148 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | 159 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 149 resource->size(), resource->format()); | 160 resource->size(), resource->format()); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 for (const auto& resource : busy_resources_) { | 310 for (const auto& resource : busy_resources_) { |
| 300 resource->OnMemoryDump(pmd, resource_provider_, false /* is_free */); | 311 resource->OnMemoryDump(pmd, resource_provider_, false /* is_free */); |
| 301 } | 312 } |
| 302 for (const auto& entry : in_use_resources_) { | 313 for (const auto& entry : in_use_resources_) { |
| 303 entry.second->OnMemoryDump(pmd, resource_provider_, false /* is_free */); | 314 entry.second->OnMemoryDump(pmd, resource_provider_, false /* is_free */); |
| 304 } | 315 } |
| 305 return true; | 316 return true; |
| 306 } | 317 } |
| 307 | 318 |
| 308 } // namespace cc | 319 } // namespace cc |
| OLD | NEW |