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

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

Issue 817133006: Support different formats in the same resource pool. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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 {
11 11
12 ResourcePool::ResourcePool(ResourceProvider* resource_provider, 12 ResourcePool::ResourcePool(ResourceProvider* resource_provider,
13 GLenum target, 13 GLenum target,
14 ResourceFormat format) 14 bool use_memory_efficient_format)
15 : resource_provider_(resource_provider), 15 : resource_provider_(resource_provider),
16 target_(target), 16 target_(target),
17 format_(format), 17 use_memory_efficient_format_(use_memory_efficient_format),
18 max_memory_usage_bytes_(0), 18 max_memory_usage_bytes_(0),
19 max_unused_memory_usage_bytes_(0), 19 max_unused_memory_usage_bytes_(0),
20 max_resource_count_(0), 20 max_resource_count_(0),
21 memory_usage_bytes_(0), 21 memory_usage_bytes_(0),
22 unused_memory_usage_bytes_(0), 22 unused_memory_usage_bytes_(0),
23 resource_count_(0) {} 23 resource_count_(0) {}
24 24
25 ResourcePool::~ResourcePool() { 25 ResourcePool::~ResourcePool() {
26 while (!busy_resources_.empty()) { 26 while (!busy_resources_.empty()) {
27 DidFinishUsingResource(busy_resources_.front()); 27 DidFinishUsingResource(busy_resources_.front());
28 busy_resources_.pop_front(); 28 busy_resources_.pop_front();
29 } 29 }
30 30
31 SetResourceUsageLimits(0, 0, 0); 31 SetResourceUsageLimits(0, 0, 0);
32 DCHECK_EQ(0u, unused_resources_.size()); 32 for (UnusedMap::const_iterator it = unused_resources_.begin();
33 it != unused_resources_.end();
34 ++it) {
35 DCHECK_EQ(0u, it->second.size());
36 }
33 DCHECK_EQ(0u, memory_usage_bytes_); 37 DCHECK_EQ(0u, memory_usage_bytes_);
34 DCHECK_EQ(0u, unused_memory_usage_bytes_); 38 DCHECK_EQ(0u, unused_memory_usage_bytes_);
35 DCHECK_EQ(0u, resource_count_); 39 DCHECK_EQ(0u, resource_count_);
36 } 40 }
37 41
38 scoped_ptr<ScopedResource> ResourcePool::AcquireResource( 42 scoped_ptr<ScopedResource> ResourcePool::AcquireResource(
39 const gfx::Size& size) { 43 const gfx::Size& size, ResourceFormat format) {
40 for (ResourceList::iterator it = unused_resources_.begin(); 44 ResourceList& unused = unused_resources_[format];
41 it != unused_resources_.end(); 45 for (ResourceList::iterator it = unused.begin(); it != unused.end(); ++it) {
42 ++it) {
43 ScopedResource* resource = *it; 46 ScopedResource* resource = *it;
44 DCHECK(resource_provider_->CanLockForWrite(resource->id())); 47 DCHECK(resource_provider_->CanLockForWrite(resource->id()));
45 48
46 if (resource->size() != size) 49 if (resource->size() != size)
47 continue; 50 continue;
48 51
49 unused_resources_.erase(it); 52 unused.erase(it);
50 unused_memory_usage_bytes_ -= resource->bytes(); 53 unused_memory_usage_bytes_ -= resource->bytes();
51 return make_scoped_ptr(resource); 54 return make_scoped_ptr(resource);
52 } 55 }
53 56
54 scoped_ptr<ScopedResource> resource = 57 scoped_ptr<ScopedResource> resource =
55 ScopedResource::Create(resource_provider_); 58 ScopedResource::Create(resource_provider_);
56 resource->AllocateManaged(size, target_, format_); 59 resource->AllocateManaged(size, target_, format);
57 60
58 memory_usage_bytes_ += resource->bytes(); 61 memory_usage_bytes_ += resource->bytes();
59 ++resource_count_; 62 ++resource_count_;
60 return resource.Pass(); 63 return resource.Pass();
61 } 64 }
62 65
66 scoped_ptr<ScopedResource> ResourcePool::AcquireResource(
67 const gfx::Size& size, ResourceFormatUsage usage) {
68 return AcquireResource(size, resource_format(usage));
69 }
70
63 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource) { 71 void ResourcePool::ReleaseResource(scoped_ptr<ScopedResource> resource) {
64 busy_resources_.push_back(resource.release()); 72 busy_resources_.push_back(resource.release());
65 } 73 }
66 74
67 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes, 75 void ResourcePool::SetResourceUsageLimits(size_t max_memory_usage_bytes,
68 size_t max_unused_memory_usage_bytes, 76 size_t max_unused_memory_usage_bytes,
69 size_t max_resource_count) { 77 size_t max_resource_count) {
70 max_memory_usage_bytes_ = max_memory_usage_bytes; 78 max_memory_usage_bytes_ = max_memory_usage_bytes;
71 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes; 79 max_unused_memory_usage_bytes_ = max_unused_memory_usage_bytes;
72 max_resource_count_ = max_resource_count; 80 max_resource_count_ = max_resource_count;
73 81
74 ReduceResourceUsage(); 82 ReduceResourceUsage();
75 } 83 }
76 84
77 void ResourcePool::ReduceResourceUsage() { 85 void ResourcePool::ReduceResourceUsage() {
78 while (!unused_resources_.empty()) { 86 for (UnusedMap::iterator it = unused_resources_.begin();
79 if (!ResourceUsageTooHigh()) 87 it != unused_resources_.end();
80 break; 88 ++it) {
89 ResourceList& unused = it->second;
90 while (!unused.empty()) {
91 if (!ResourceUsageTooHigh())
92 break;
81 93
82 // LRU eviction pattern. Most recently used might be blocked by 94 // LRU eviction pattern. Most recently used might be blocked by
83 // a read lock fence but it's still better to evict the least 95 // a read lock fence but it's still better to evict the least
84 // recently used as it prevents a resource that is hard to reuse 96 // recently used as it prevents a resource that is hard to reuse
85 // because of unique size from being kept around. Resources that 97 // because of unique size from being kept around. Resources that
86 // can't be locked for write might also not be truly free-able. 98 // can't be locked for write might also not be truly free-able.
87 // We can free the resource here but it doesn't mean that the 99 // We can free the resource here but it doesn't mean that the
88 // memory is necessarily returned to the OS. 100 // memory is necessarily returned to the OS.
89 ScopedResource* resource = unused_resources_.front(); 101 ScopedResource* resource = unused.front();
90 unused_resources_.pop_front(); 102 unused.pop_front();
91 memory_usage_bytes_ -= resource->bytes(); 103 memory_usage_bytes_ -= resource->bytes();
92 unused_memory_usage_bytes_ -= resource->bytes(); 104 unused_memory_usage_bytes_ -= resource->bytes();
93 --resource_count_; 105 --resource_count_;
94 delete resource; 106 delete resource;
107 }
95 } 108 }
96 } 109 }
97 110
98 bool ResourcePool::ResourceUsageTooHigh() { 111 bool ResourcePool::ResourceUsageTooHigh() {
99 if (resource_count_ > max_resource_count_) 112 if (resource_count_ > max_resource_count_)
100 return true; 113 return true;
101 if (memory_usage_bytes_ > max_memory_usage_bytes_) 114 if (memory_usage_bytes_ > max_memory_usage_bytes_)
102 return true; 115 return true;
103 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_) 116 if (unused_memory_usage_bytes_ > max_unused_memory_usage_bytes_)
104 return true; 117 return true;
105 return false; 118 return false;
106 } 119 }
107 120
121 ResourceFormat ResourcePool::resource_format(ResourceFormatUsage usage) const {
122 if (use_memory_efficient_format_) {
123 return resource_provider_->memory_efficient_texture_format(usage);
124 } else {
125 return resource_provider_->best_texture_format();
126 }
127 }
128
129 size_t ResourcePool::acquired_resource_count() const {
130 size_t result = resource_count_;
131 for (UnusedMap::const_iterator it = unused_resources_.begin();
132 it != unused_resources_.end(); ++it) {
133 result -= it->second.size();
134 }
135 return result;
136 }
137
108 void ResourcePool::CheckBusyResources(bool wait_if_needed) { 138 void ResourcePool::CheckBusyResources(bool wait_if_needed) {
109 ResourceList::iterator it = busy_resources_.begin(); 139 ResourceList::iterator it = busy_resources_.begin();
110 140
111 while (it != busy_resources_.end()) { 141 while (it != busy_resources_.end()) {
112 ScopedResource* resource = *it; 142 ScopedResource* resource = *it;
113 143
114 if (wait_if_needed) 144 if (wait_if_needed)
115 resource_provider_->WaitReadLockIfNeeded(resource->id()); 145 resource_provider_->WaitReadLockIfNeeded(resource->id());
116 146
117 if (resource_provider_->CanLockForWrite(resource->id())) { 147 if (resource_provider_->CanLockForWrite(resource->id())) {
118 DidFinishUsingResource(resource); 148 DidFinishUsingResource(resource);
119 it = busy_resources_.erase(it); 149 it = busy_resources_.erase(it);
120 } else { 150 } else {
121 ++it; 151 ++it;
122 } 152 }
123 } 153 }
124 } 154 }
125 155
126 void ResourcePool::DidFinishUsingResource(ScopedResource* resource) { 156 void ResourcePool::DidFinishUsingResource(ScopedResource* resource) {
127 unused_memory_usage_bytes_ += resource->bytes(); 157 unused_memory_usage_bytes_ += resource->bytes();
128 unused_resources_.push_back(resource); 158 unused_resources_[resource->format()].push_back(resource);
129 } 159 }
130 160
131 } // namespace cc 161 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698