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 30 matching lines...) Expand all Loading... | |
41 ResourcePool::~ResourcePool() { | 41 ResourcePool::~ResourcePool() { |
42 SetResourceUsageLimits(0, 0, 0); | 42 SetResourceUsageLimits(0, 0, 0); |
43 } | 43 } |
44 | 44 |
45 scoped_ptr<ResourcePool::Resource> ResourcePool::AcquireResource( | 45 scoped_ptr<ResourcePool::Resource> ResourcePool::AcquireResource( |
46 gfx::Size size, ResourceFormat format) { | 46 gfx::Size size, ResourceFormat format) { |
47 for (ResourceList::iterator it = unused_resources_.begin(); | 47 for (ResourceList::iterator it = unused_resources_.begin(); |
48 it != unused_resources_.end(); ++it) { | 48 it != unused_resources_.end(); ++it) { |
49 Resource* resource = *it; | 49 Resource* resource = *it; |
50 | 50 |
51 if (!resource_provider_->CanLockForWrite(resource->id())) | |
reveman
2013/11/02 13:31:14
nit: instead of removing this completely change it
| |
52 continue; | |
53 if (resource->size() != size) | 51 if (resource->size() != size) |
54 continue; | 52 continue; |
55 if (resource->format() != format) | 53 if (resource->format() != format) |
56 continue; | 54 continue; |
57 | 55 |
58 unused_resources_.erase(it); | 56 unused_resources_.erase(it); |
59 unused_memory_usage_bytes_ -= resource->bytes(); | 57 unused_memory_usage_bytes_ -= resource->bytes(); |
60 return make_scoped_ptr(resource); | 58 return make_scoped_ptr(resource); |
61 } | 59 } |
62 | 60 |
63 // Create new resource. | 61 // Create new resource. |
64 Resource* resource = new Resource(resource_provider_, size, format); | 62 Resource* resource = new Resource(resource_provider_, size, format); |
65 | 63 |
66 // Extend all read locks on all resources until the resource is | 64 // Extend all read locks on all resources until the resource is |
67 // finished being used, such that we know when resources are | 65 // finished being used, such that we know when resources are |
68 // truly safe to recycle. | 66 // truly safe to recycle. |
69 resource_provider_->EnableReadLockFences(resource->id(), true); | 67 resource_provider_->EnableReadLockFences(resource->id(), true); |
70 | 68 |
71 memory_usage_bytes_ += resource->bytes(); | 69 memory_usage_bytes_ += resource->bytes(); |
72 ++resource_count_; | 70 ++resource_count_; |
73 return make_scoped_ptr(resource); | 71 return make_scoped_ptr(resource); |
74 } | 72 } |
75 | 73 |
76 void ResourcePool::ReleaseResource( | 74 void ResourcePool::ReleaseResource( |
77 scoped_ptr<ResourcePool::Resource> resource) { | 75 scoped_ptr<ResourcePool::Resource> resource) { |
78 if (ResourceUsageTooHigh()) { | |
79 memory_usage_bytes_ -= resource->bytes(); | |
80 --resource_count_; | |
81 return; | |
82 } | |
83 | |
84 unused_memory_usage_bytes_ += resource->bytes(); | 76 unused_memory_usage_bytes_ += resource->bytes(); |
85 unused_resources_.push_back(resource.release()); | 77 busy_resources_.push_back(resource.release()); |
86 } | 78 } |
87 | 79 |
88 void ResourcePool::SetResourceUsageLimits( | 80 void ResourcePool::SetResourceUsageLimits( |
89 size_t max_memory_usage_bytes, | 81 size_t max_memory_usage_bytes, |
90 size_t max_unused_memory_usage_bytes, | 82 size_t max_unused_memory_usage_bytes, |
91 size_t max_resource_count) { | 83 size_t max_resource_count) { |
92 max_memory_usage_bytes_ = max_memory_usage_bytes; | 84 max_memory_usage_bytes_ = max_memory_usage_bytes; |
93 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes; | 85 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes; |
94 max_resource_count_ = max_resource_count; | 86 max_resource_count_ = max_resource_count; |
95 | 87 |
(...skipping 24 matching lines...) Expand all Loading... | |
120 bool ResourcePool::ResourceUsageTooHigh() { | 112 bool ResourcePool::ResourceUsageTooHigh() { |
121 if (resource_count_ > max_resource_count_) | 113 if (resource_count_ > max_resource_count_) |
122 return true; | 114 return true; |
123 if (memory_usage_bytes_ > max_memory_usage_bytes_) | 115 if (memory_usage_bytes_ > max_memory_usage_bytes_) |
124 return true; | 116 return true; |
125 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) | 117 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) |
126 return true; | 118 return true; |
127 return false; | 119 return false; |
128 } | 120 } |
129 | 121 |
122 void ResourcePool::CheckBusyResources() { | |
123 ResourceList::iterator it = busy_resources_.begin(); | |
124 | |
125 while (it != busy_resources_.end()) { | |
126 Resource* resource = *it; | |
127 | |
128 if (resource_provider_->CanLockForWrite(resource->id())) { | |
129 unused_resources_.push_back(resource); | |
130 it = busy_resources_.erase(it); | |
131 } else { | |
132 ++it; | |
133 } | |
134 } | |
135 } | |
136 | |
130 } // namespace cc | 137 } // namespace cc |
OLD | NEW |