Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: cc/resources/resource_pool.cc

Issue 1154393003: cc: Use CheckedNumeric for resource size calculations. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "cc/resources/scoped_resource.h" 8 #include "cc/resources/scoped_resource.h"
9 9
10 namespace cc { 10 namespace cc {
(...skipping 29 matching lines...) Expand all
40 ++it) { 40 ++it) {
41 ScopedResource* resource = it->resource; 41 ScopedResource* resource = it->resource;
42 DCHECK(resource_provider_->CanLockForWrite(resource->id())); 42 DCHECK(resource_provider_->CanLockForWrite(resource->id()));
43 43
44 if (resource->format() != format) 44 if (resource->format() != format)
45 continue; 45 continue;
46 if (resource->size() != size) 46 if (resource->size() != size)
47 continue; 47 continue;
48 48
49 unused_resources_.erase(it); 49 unused_resources_.erase(it);
50 unused_memory_usage_bytes_ -= resource->bytes(); 50 unused_memory_usage_bytes_ -=
51 Resource::UncheckedMemorySizeBytes(size, format);
51 return make_scoped_ptr(resource); 52 return make_scoped_ptr(resource);
52 } 53 }
53 54
55 DCHECK(Resource::VerifySizeInBytes(size, format, nullptr));
54 scoped_ptr<ScopedResource> resource = 56 scoped_ptr<ScopedResource> resource =
55 ScopedResource::Create(resource_provider_); 57 ScopedResource::Create(resource_provider_);
56 resource->AllocateManaged(size, target_, format); 58 resource->AllocateManaged(size, target_, format);
57 59
58 memory_usage_bytes_ += resource->bytes(); 60 memory_usage_bytes_ += Resource::UncheckedMemorySizeBytes(size, format);
59 ++resource_count_; 61 ++resource_count_;
60 return resource.Pass(); 62 return resource.Pass();
61 } 63 }
62 64
63 scoped_ptr<ScopedResource> ResourcePool::TryAcquireResourceWithContentId( 65 scoped_ptr<ScopedResource> ResourcePool::TryAcquireResourceWithContentId(
64 uint64_t content_id) { 66 uint64_t content_id) {
65 DCHECK(content_id); 67 DCHECK(content_id);
66 68
67 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(), 69 auto it = std::find_if(unused_resources_.begin(), unused_resources_.end(),
68 [content_id](const PoolResource& pool_resource) { 70 [content_id](const PoolResource& pool_resource) {
69 return pool_resource.content_id == content_id; 71 return pool_resource.content_id == content_id;
70 }); 72 });
71 if (it == unused_resources_.end()) 73 if (it == unused_resources_.end())
72 return nullptr; 74 return nullptr;
73 75
74 ScopedResource* resource = it->resource; 76 ScopedResource* resource = it->resource;
75 DCHECK(resource_provider_->CanLockForWrite(resource->id())); 77 DCHECK(resource_provider_->CanLockForWrite(resource->id()));
76 78
77 unused_resources_.erase(it); 79 unused_resources_.erase(it);
78 unused_memory_usage_bytes_ -= resource->bytes(); 80 unused_memory_usage_bytes_ -=
81 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format());
79 return make_scoped_ptr(resource); 82 return make_scoped_ptr(resource);
80 } 83 }
81 84
82 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource, 85 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource,
83 uint64_t content_id) { 86 uint64_t content_id) {
84 busy_resources_.push_back(PoolResource(resource.release(), content_id)); 87 busy_resources_.push_back(PoolResource(resource.release(), content_id));
85 } 88 }
86 89
87 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, 90 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes,
88 size_t max_unused_memory_usage_bytes, 91 size_t max_unused_memory_usage_bytes,
(...skipping 12 matching lines...) Expand all
101 104
102 // LRU eviction pattern. Most recently used might be blocked by 105 // LRU eviction pattern. Most recently used might be blocked by
103 // a read lock fence but it's still better to evict the least 106 // a read lock fence but it's still better to evict the least
104 // recently used as it prevents a resource that is hard to reuse 107 // recently used as it prevents a resource that is hard to reuse
105 // because of unique size from being kept around. Resources that 108 // because of unique size from being kept around. Resources that
106 // can't be locked for write might also not be truly free-able. 109 // can't be locked for write might also not be truly free-able.
107 // We can free the resource here but it doesn't mean that the 110 // We can free the resource here but it doesn't mean that the
108 // memory is necessarily returned to the OS. 111 // memory is necessarily returned to the OS.
109 ScopedResource* resource = unused_resources_.front().resource; 112 ScopedResource* resource = unused_resources_.front().resource;
110 unused_resources_.pop_front(); 113 unused_resources_.pop_front();
111 memory_usage_bytes_ -= resource->bytes(); 114 size_t resource_bytes = Resource::UncheckedMemorySizeBytes(
112 unused_memory_usage_bytes_ -= resource->bytes(); 115 resource->size(), resource->format());
116 memory_usage_bytes_ -= resource_bytes;
117 unused_memory_usage_bytes_ -= resource_bytes;
113 --resource_count_; 118 --resource_count_;
114 delete resource; 119 delete resource;
115 } 120 }
116 } 121 }
117 122
118 bool ResourcePool::ResourceUsageTooHigh() { 123 bool ResourcePool::ResourceUsageTooHigh() {
119 if (resource_count_ > max_resource_count_) 124 if (resource_count_ > max_resource_count_)
120 return true; 125 return true;
121 if (memory_usage_bytes_ > max_memory_usage_bytes_) 126 if (memory_usage_bytes_ > max_memory_usage_bytes_)
122 return true; 127 return true;
(...skipping 15 matching lines...) Expand all
138 DidFinishUsingResource(resource, it->content_id); 143 DidFinishUsingResource(resource, it->content_id);
139 it = busy_resources_.erase(it); 144 it = busy_resources_.erase(it);
140 } else { 145 } else {
141 ++it; 146 ++it;
142 } 147 }
143 } 148 }
144 } 149 }
145 150
146 void ResourcePool::DidFinishUsingResource(ScopedResource* resource, 151 void ResourcePool::DidFinishUsingResource(ScopedResource* resource,
147 uint64_t content_id) { 152 uint64_t content_id) {
148 unused_memory_usage_bytes_ += resource->bytes(); 153 unused_memory_usage_bytes_ +=
154 Resource::UncheckedMemorySizeBytes(resource->size(), resource->format());
149 unused_resources_.push_back(PoolResource(resource, content_id)); 155 unused_resources_.push_back(PoolResource(resource, content_id));
150 } 156 }
151 157
152 } // namespace cc 158 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698