| 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 | 8 |
| 9 namespace cc { | 9 namespace cc { |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 ResourcePool::Resource::~Resource() { | 24 ResourcePool::Resource::~Resource() { |
| 25 DCHECK(id()); | 25 DCHECK(id()); |
| 26 DCHECK(resource_provider_); | 26 DCHECK(resource_provider_); |
| 27 resource_provider_->DeleteResource(id()); | 27 resource_provider_->DeleteResource(id()); |
| 28 } | 28 } |
| 29 | 29 |
| 30 ResourcePool::ResourcePool(ResourceProvider* resource_provider) | 30 ResourcePool::ResourcePool(ResourceProvider* resource_provider) |
| 31 : resource_provider_(resource_provider), | 31 : resource_provider_(resource_provider), |
| 32 max_memory_usage_bytes_(0), | 32 max_memory_usage_bytes_(0), |
| 33 memory_usage_bytes_(0) { | 33 max_unused_memory_usage_bytes_(0), |
| 34 memory_usage_bytes_(0), |
| 35 unused_memory_usage_bytes_(0) { |
| 34 } | 36 } |
| 35 | 37 |
| 36 ResourcePool::~ResourcePool() { | 38 ResourcePool::~ResourcePool() { |
| 37 SetMaxMemoryUsageBytes(0); | 39 SetMaxMemoryUsageBytes(0, 0); |
| 38 } | 40 } |
| 39 | 41 |
| 40 scoped_ptr<ResourcePool::Resource> ResourcePool::AcquireResource( | 42 scoped_ptr<ResourcePool::Resource> ResourcePool::AcquireResource( |
| 41 const gfx::Size& size, GLenum format) { | 43 const gfx::Size& size, GLenum format) { |
| 42 for (ResourceList::iterator it = resources_.begin(); | 44 for (ResourceList::iterator it = resources_.begin(); |
| 43 it != resources_.end(); ++it) { | 45 it != resources_.end(); ++it) { |
| 44 Resource* resource = *it; | 46 Resource* resource = *it; |
| 45 | 47 |
| 46 // TODO(epenner): It would be nice to DCHECK that this | 48 // TODO(epenner): It would be nice to DCHECK that this |
| 47 // doesn't happen two frames in a row for any resource | 49 // doesn't happen two frames in a row for any resource |
| 48 // in this pool. | 50 // in this pool. |
| 49 if (!resource_provider_->CanLockForWrite(resource->id())) | 51 if (!resource_provider_->CanLockForWrite(resource->id())) |
| 50 continue; | 52 continue; |
| 51 | 53 |
| 52 if (resource->size() != size) | 54 if (resource->size() != size) |
| 53 continue; | 55 continue; |
| 54 if (resource->format() != format) | 56 if (resource->format() != format) |
| 55 continue; | 57 continue; |
| 56 | 58 |
| 57 resources_.erase(it); | 59 resources_.erase(it); |
| 60 unused_memory_usage_bytes_ -= resource->bytes(); |
| 58 return make_scoped_ptr(resource); | 61 return make_scoped_ptr(resource); |
| 59 } | 62 } |
| 60 | 63 |
| 61 // Create new resource. | 64 // Create new resource. |
| 62 Resource* resource = new Resource( | 65 Resource* resource = new Resource( |
| 63 resource_provider_, size, format); | 66 resource_provider_, size, format); |
| 64 | 67 |
| 65 // Extend all read locks on all resources until the resource is | 68 // Extend all read locks on all resources until the resource is |
| 66 // finished being used, such that we know when resources are | 69 // finished being used, such that we know when resources are |
| 67 // truly safe to recycle. | 70 // truly safe to recycle. |
| 68 resource_provider_->EnableReadLockFences(resource->id(), true); | 71 resource_provider_->EnableReadLockFences(resource->id(), true); |
| 69 | 72 |
| 70 memory_usage_bytes_ += resource->bytes(); | 73 memory_usage_bytes_ += resource->bytes(); |
| 71 return make_scoped_ptr(resource); | 74 return make_scoped_ptr(resource); |
| 72 } | 75 } |
| 73 | 76 |
| 74 void ResourcePool::ReleaseResource( | 77 void ResourcePool::ReleaseResource( |
| 75 scoped_ptr<ResourcePool::Resource> resource) { | 78 scoped_ptr<ResourcePool::Resource> resource) { |
| 76 if (memory_usage_bytes_ > max_memory_usage_bytes_) { | 79 if (MemoryUsageTooHigh()) { |
| 77 memory_usage_bytes_ -= resource->bytes(); | 80 memory_usage_bytes_ -= resource->bytes(); |
| 78 return; | 81 return; |
| 79 } | 82 } |
| 80 | 83 |
| 84 unused_memory_usage_bytes_ += resource->bytes(); |
| 81 resources_.push_back(resource.release()); | 85 resources_.push_back(resource.release()); |
| 82 } | 86 } |
| 83 | 87 |
| 84 void ResourcePool::SetMaxMemoryUsageBytes(size_t max_memory_usage_bytes) { | 88 void ResourcePool::SetMaxMemoryUsageBytes( |
| 89 size_t max_memory_usage_bytes, |
| 90 size_t max_unused_memory_usage_bytes) { |
| 85 max_memory_usage_bytes_ = max_memory_usage_bytes; | 91 max_memory_usage_bytes_ = max_memory_usage_bytes; |
| 92 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes; |
| 86 | 93 |
| 87 while (!resources_.empty()) { | 94 while (!resources_.empty()) { |
| 88 if (memory_usage_bytes_ <= max_memory_usage_bytes_) | 95 if (!MemoryUsageTooHigh()) |
| 89 break; | 96 break; |
| 90 Resource* resource = resources_.front(); | 97 |
| 91 resources_.pop_front(); | 98 // MRU eviction pattern as least recently used is less likely to |
| 99 // be blocked by read lock fence. |
| 100 Resource* resource = resources_.back(); |
| 101 resources_.pop_back(); |
| 92 memory_usage_bytes_ -= resource->bytes(); | 102 memory_usage_bytes_ -= resource->bytes(); |
| 103 unused_memory_usage_bytes_ -= resource->bytes(); |
| 93 delete resource; | 104 delete resource; |
| 94 } | 105 } |
| 95 } | 106 } |
| 96 | 107 |
| 108 bool ResourcePool::MemoryUsageTooHigh() { |
| 109 if (memory_usage_bytes_ > max_memory_usage_bytes_) |
| 110 return true; |
| 111 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) |
| 112 return true; |
| 113 return false; |
| 114 } |
| 115 |
| 97 } // namespace cc | 116 } // namespace cc |
| OLD | NEW |