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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 pool_resource->Allocate(size, ResourceProvider::TEXTURE_HINT_IMMUTABLE, | 126 pool_resource->Allocate(size, ResourceProvider::TEXTURE_HINT_IMMUTABLE, |
| 127 format); | 127 format); |
| 128 } | 128 } |
| 129 | 129 |
| 130 DCHECK(ResourceUtil::VerifySizeInBytes<size_t>(pool_resource->size(), | 130 DCHECK(ResourceUtil::VerifySizeInBytes<size_t>(pool_resource->size(), |
| 131 pool_resource->format())); | 131 pool_resource->format())); |
| 132 total_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | 132 total_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 133 pool_resource->size(), pool_resource->format()); | 133 pool_resource->size(), pool_resource->format()); |
| 134 ++total_resource_count_; | 134 ++total_resource_count_; |
| 135 | 135 |
| 136 // Clear the invalidated rect and content ID, as we are about to raster new | |
| 137 // content. These will be re-set when rasterization completes successfully. | |
| 138 pool_resource->set_invalidated_rect(gfx::Rect()); | |
| 139 pool_resource->set_content_id(0); | |
| 140 | |
| 136 Resource* resource = pool_resource.get(); | 141 Resource* resource = pool_resource.get(); |
| 137 in_use_resources_[resource->id()] = std::move(pool_resource); | 142 in_use_resources_[resource->id()] = std::move(pool_resource); |
| 138 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | 143 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 139 resource->size(), resource->format()); | 144 resource->size(), resource->format()); |
| 145 | |
| 140 return resource; | 146 return resource; |
| 141 } | 147 } |
| 142 | 148 |
| 143 Resource* ResourcePool::TryAcquireResourceWithContentId(uint64_t content_id) { | 149 Resource* ResourcePool::TryAcquireResourceForPartialRaster( |
| 144 DCHECK(content_id); | 150 uint64_t new_content_id, |
| 151 const gfx::Rect& new_invalidated_rect, | |
| 152 uint64_t previous_content_id, | |
| 153 gfx::Rect* total_invalidated_rect) { | |
| 154 DCHECK(new_content_id); | |
| 155 DCHECK(previous_content_id); | |
| 156 *total_invalidated_rect = gfx::Rect(); | |
| 145 | 157 |
| 146 auto it = std::find_if( | 158 // Try to find |previous_content_id| in |unused_resources_|. |
| 159 auto found_unused = std::find_if( | |
| 147 unused_resources_.begin(), unused_resources_.end(), | 160 unused_resources_.begin(), unused_resources_.end(), |
| 148 [content_id](const std::unique_ptr<PoolResource>& pool_resource) { | 161 [previous_content_id](const ResourceDeque::value_type& pool_resource) { |
| 149 return pool_resource->content_id() == content_id; | 162 return pool_resource->content_id() == previous_content_id; |
| 150 }); | 163 }); |
| 151 if (it == unused_resources_.end()) | 164 if (found_unused != unused_resources_.end()) { |
| 152 return nullptr; | 165 PoolResource* found_resource = found_unused->get(); |
| 166 DCHECK(resource_provider_->CanLockForWrite(found_resource->id())); | |
| 153 | 167 |
| 154 Resource* resource = it->get(); | 168 // Transfer resource to |in_use_resources_|. |
| 155 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 169 in_use_resources_[found_resource->id()] = std::move(*found_unused); |
| 170 unused_resources_.erase(found_unused); | |
| 171 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | |
| 172 found_resource->size(), found_resource->format()); | |
| 156 | 173 |
| 157 // Transfer resource to |in_use_resources_|. | 174 *total_invalidated_rect = new_invalidated_rect; |
| 158 in_use_resources_[resource->id()] = std::move(*it); | 175 if (!found_resource->invalidated_rect().IsEmpty()) |
| 159 unused_resources_.erase(it); | 176 total_invalidated_rect->Union(found_resource->invalidated_rect()); |
| 160 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | 177 |
| 161 resource->size(), resource->format()); | 178 // Clear the invalidated rect and content ID on the resource being retunred. |
| 162 return resource; | 179 // These will be updated when raster completes successfully. |
| 180 found_resource->set_invalidated_rect(gfx::Rect()); | |
| 181 found_resource->set_content_id(0); | |
| 182 | |
| 183 return found_resource; | |
| 184 } | |
| 185 | |
| 186 // Couldn't find an unused previous resource. If possible, update the | |
|
enne (OOO)
2016/07/12 17:42:53
Thanks for this. :)
| |
| 187 // invalidation on the in-use resource to allow for future partial raster. | |
| 188 // | |
| 189 // We could update all in-use resources (rather than just the first one we | |
| 190 // find), however the latency on resources being returned by the browser | |
| 191 // is typically one frame, so this extra searching would be largely wasted. | |
| 192 // | |
| 193 // Skipping any additional resources is not a correctness risk, as the | |
| 194 // un-updated resources will have their old content ID, and will not be found | |
| 195 // by future partial raster searches, which will use the new content ID. | |
| 196 auto found_in_use = std::find_if( | |
| 197 in_use_resources_.begin(), in_use_resources_.end(), | |
| 198 [previous_content_id](const InUseResourceMap::value_type& in_use_entry) { | |
| 199 return in_use_entry.second->content_id() == previous_content_id; | |
| 200 }); | |
| 201 if (found_in_use != in_use_resources_.end()) { | |
| 202 PoolResource* found_resource = found_in_use->second.get(); | |
| 203 gfx::Rect updated_invalidated_rect = new_invalidated_rect; | |
| 204 if (!found_resource->invalidated_rect().IsEmpty()) | |
| 205 updated_invalidated_rect.Union(found_resource->invalidated_rect()); | |
| 206 | |
| 207 found_resource->set_content_id(new_content_id); | |
| 208 found_resource->set_invalidated_rect(updated_invalidated_rect); | |
| 209 } | |
| 210 | |
| 211 return nullptr; | |
| 163 } | 212 } |
| 164 | 213 |
| 165 void ResourcePool::ReleaseResource(Resource* resource, uint64_t content_id) { | 214 void ResourcePool::ReleaseResource(Resource* resource) { |
| 166 // Ensure that the provided resource is valid. | 215 // Ensure that the provided resource is valid. |
| 167 // TODO(ericrk): Remove this once we've investigated further. | 216 // TODO(ericrk): Remove this once we've investigated further. |
| 168 // crbug.com/598286. | 217 // crbug.com/598286. |
| 169 CHECK(resource); | 218 CHECK(resource); |
| 170 CHECK(resource->id()); | 219 CHECK(resource->id()); |
| 171 | 220 |
| 172 auto it = in_use_resources_.find(resource->id()); | 221 auto it = in_use_resources_.find(resource->id()); |
| 173 if (it == in_use_resources_.end()) { | 222 if (it == in_use_resources_.end()) { |
| 174 // We should never hit this. Do some digging to try to determine the cause. | 223 // We should never hit this. Do some digging to try to determine the cause. |
| 175 // TODO(ericrk): Remove this once we've investigated further. | 224 // TODO(ericrk): Remove this once we've investigated further. |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 195 // Resource doesn't exist in any of our lists. CHECK. | 244 // Resource doesn't exist in any of our lists. CHECK. |
| 196 CHECK(false); | 245 CHECK(false); |
| 197 } | 246 } |
| 198 | 247 |
| 199 // Also ensure that the resource wasn't null in our list. | 248 // Also ensure that the resource wasn't null in our list. |
| 200 // TODO(ericrk): Remove this once we've investigated further. | 249 // TODO(ericrk): Remove this once we've investigated further. |
| 201 // crbug.com/598286. | 250 // crbug.com/598286. |
| 202 CHECK(it->second.get()); | 251 CHECK(it->second.get()); |
| 203 | 252 |
| 204 PoolResource* pool_resource = it->second.get(); | 253 PoolResource* pool_resource = it->second.get(); |
| 205 pool_resource->set_content_id(content_id); | |
| 206 pool_resource->set_last_usage(base::TimeTicks::Now()); | 254 pool_resource->set_last_usage(base::TimeTicks::Now()); |
| 207 | 255 |
| 208 // Transfer resource to |busy_resources_|. | 256 // Transfer resource to |busy_resources_|. |
| 209 busy_resources_.push_front(std::move(it->second)); | 257 busy_resources_.push_front(std::move(it->second)); |
| 210 in_use_resources_.erase(it); | 258 in_use_resources_.erase(it); |
| 211 in_use_memory_usage_bytes_ -= ResourceUtil::UncheckedSizeInBytes<size_t>( | 259 in_use_memory_usage_bytes_ -= ResourceUtil::UncheckedSizeInBytes<size_t>( |
| 212 pool_resource->size(), pool_resource->format()); | 260 pool_resource->size(), pool_resource->format()); |
| 213 | 261 |
| 214 // Now that we have evictable resources, schedule an eviction call for this | 262 // Now that we have evictable resources, schedule an eviction call for this |
| 215 // resource if necessary. | 263 // resource if necessary. |
| 216 ScheduleEvictExpiredResourcesIn(resource_expiration_delay_); | 264 ScheduleEvictExpiredResourcesIn(resource_expiration_delay_); |
| 217 } | 265 } |
| 218 | 266 |
| 267 void ResourcePool::OnContentReplaced(ResourceId resource_id, | |
| 268 uint64_t content_id) { | |
| 269 auto found = in_use_resources_.find(resource_id); | |
| 270 DCHECK(found != in_use_resources_.end()); | |
| 271 found->second->set_content_id(content_id); | |
| 272 found->second->set_invalidated_rect(gfx::Rect()); | |
| 273 } | |
| 274 | |
| 219 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, | 275 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, |
| 220 size_t max_resource_count) { | 276 size_t max_resource_count) { |
| 221 max_memory_usage_bytes_ = max_memory_usage_bytes; | 277 max_memory_usage_bytes_ = max_memory_usage_bytes; |
| 222 max_resource_count_ = max_resource_count; | 278 max_resource_count_ = max_resource_count; |
| 223 | 279 |
| 224 ReduceResourceUsage(); | 280 ReduceResourceUsage(); |
| 225 } | 281 } |
| 226 | 282 |
| 227 void ResourcePool::ReduceResourceUsage() { | 283 void ResourcePool::ReduceResourceUsage() { |
| 228 while (!unused_resources_.empty()) { | 284 while (!unused_resources_.empty()) { |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 350 for (const auto& resource : busy_resources_) { | 406 for (const auto& resource : busy_resources_) { |
| 351 resource->OnMemoryDump(pmd, resource_provider_, false /* is_free */); | 407 resource->OnMemoryDump(pmd, resource_provider_, false /* is_free */); |
| 352 } | 408 } |
| 353 for (const auto& entry : in_use_resources_) { | 409 for (const auto& entry : in_use_resources_) { |
| 354 entry.second->OnMemoryDump(pmd, resource_provider_, false /* is_free */); | 410 entry.second->OnMemoryDump(pmd, resource_provider_, false /* is_free */); |
| 355 } | 411 } |
| 356 return true; | 412 return true; |
| 357 } | 413 } |
| 358 | 414 |
| 359 } // namespace cc | 415 } // namespace cc |
| OLD | NEW |