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

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

Issue 2243013002: Refactor ResourcePool::AcquireResource() and add test. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Test failure. Created 4 years, 4 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
« no previous file with comments | « cc/resources/resource_pool.h ('k') | cc/resources/resource_pool_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 DidFinishUsingResource(PopBack(&busy_resources_)); 87 DidFinishUsingResource(PopBack(&busy_resources_));
88 } 88 }
89 89
90 SetResourceUsageLimits(0, 0); 90 SetResourceUsageLimits(0, 0);
91 DCHECK_EQ(0u, unused_resources_.size()); 91 DCHECK_EQ(0u, unused_resources_.size());
92 DCHECK_EQ(0u, in_use_memory_usage_bytes_); 92 DCHECK_EQ(0u, in_use_memory_usage_bytes_);
93 DCHECK_EQ(0u, total_memory_usage_bytes_); 93 DCHECK_EQ(0u, total_memory_usage_bytes_);
94 DCHECK_EQ(0u, total_resource_count_); 94 DCHECK_EQ(0u, total_resource_count_);
95 } 95 }
96 96
97 Resource* ResourcePool::AcquireResource(const gfx::Size& size, 97 Resource* ResourcePool::ReuseResource(const gfx::Size& size,
98 ResourceFormat format, 98 ResourceFormat format,
99 const gfx::ColorSpace& color_space) { 99 const gfx::ColorSpace& color_space) {
100 // Finding resources in |unused_resources_| from MRU to LRU direction, touches 100 // Finding resources in |unused_resources_| from MRU to LRU direction, touches
101 // LRU resources only if needed, which increases possibility of expiring more 101 // LRU resources only if needed, which increases possibility of expiring more
102 // LRU resources within kResourceExpirationDelayMs. 102 // LRU resources within kResourceExpirationDelayMs.
103 for (ResourceDeque::iterator it = unused_resources_.begin(); 103 for (ResourceDeque::iterator it = unused_resources_.begin();
104 it != unused_resources_.end(); ++it) { 104 it != unused_resources_.end(); ++it) {
105 ScopedResource* resource = it->get(); 105 ScopedResource* resource = it->get();
106 DCHECK(resource_provider_->CanLockForWrite(resource->id())); 106 DCHECK(resource_provider_->CanLockForWrite(resource->id()));
107 107
108 if (resource->format() != format) 108 if (resource->format() != format)
109 continue; 109 continue;
110 if (resource->size() != size) 110 if (resource->size() != size)
111 continue; 111 continue;
112 if (resource->color_space() != color_space) 112 if (resource->color_space() != color_space)
113 continue; 113 continue;
114 114
115 // Transfer resource to |in_use_resources_|. 115 // Transfer resource to |in_use_resources_|.
116 in_use_resources_[resource->id()] = std::move(*it); 116 in_use_resources_[resource->id()] = std::move(*it);
117 unused_resources_.erase(it); 117 unused_resources_.erase(it);
118 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( 118 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>(
119 resource->size(), resource->format()); 119 resource->size(), resource->format());
120 return resource; 120 return resource;
121 } 121 }
122 return nullptr;
123 }
122 124
125 Resource* ResourcePool::CreateResource(const gfx::Size& size,
126 ResourceFormat format,
127 const gfx::ColorSpace& color_space) {
123 std::unique_ptr<PoolResource> pool_resource = 128 std::unique_ptr<PoolResource> pool_resource =
124 PoolResource::Create(resource_provider_); 129 PoolResource::Create(resource_provider_);
125 130
126 if (use_gpu_memory_buffers_) { 131 if (use_gpu_memory_buffers_) {
127 pool_resource->AllocateWithGpuMemoryBuffer(size, format, usage_, 132 pool_resource->AllocateWithGpuMemoryBuffer(size, format, usage_,
128 color_space); 133 color_space);
129 } else { 134 } else {
130 pool_resource->Allocate(size, ResourceProvider::TEXTURE_HINT_IMMUTABLE, 135 pool_resource->Allocate(size, ResourceProvider::TEXTURE_HINT_IMMUTABLE,
131 format, color_space); 136 format, color_space);
132 } 137 }
(...skipping 10 matching lines...) Expand all
143 pool_resource->set_content_id(0); 148 pool_resource->set_content_id(0);
144 149
145 Resource* resource = pool_resource.get(); 150 Resource* resource = pool_resource.get();
146 in_use_resources_[resource->id()] = std::move(pool_resource); 151 in_use_resources_[resource->id()] = std::move(pool_resource);
147 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( 152 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>(
148 resource->size(), resource->format()); 153 resource->size(), resource->format());
149 154
150 return resource; 155 return resource;
151 } 156 }
152 157
158 Resource* ResourcePool::AcquireResource(const gfx::Size& size,
159 ResourceFormat format,
160 const gfx::ColorSpace& color_space) {
161 Resource* reused_resource = ReuseResource(size, format, color_space);
162 if (reused_resource)
163 return reused_resource;
164
165 return CreateResource(size, format, color_space);
166 }
167
153 // Iterate over all three resource lists (unused, in-use, and busy), updating 168 // Iterate over all three resource lists (unused, in-use, and busy), updating
154 // the invalidation and content IDs to allow for future partial raster. The 169 // the invalidation and content IDs to allow for future partial raster. The
155 // first unused resource found (if any) will be returned and used for partial 170 // first unused resource found (if any) will be returned and used for partial
156 // raster directly. 171 // raster directly.
157 // 172 //
158 // Note that this may cause us to have multiple resources with the same content 173 // Note that this may cause us to have multiple resources with the same content
159 // ID. This is not a correctness risk, as all these resources will have valid 174 // ID. This is not a correctness risk, as all these resources will have valid
160 // invalidations can can be used safely. Note that we could improve raster 175 // invalidations can can be used safely. Note that we could improve raster
161 // performance at the cost of search time if we found the resource with the 176 // performance at the cost of search time if we found the resource with the
162 // smallest invalidation ID to raster in to. 177 // smallest invalidation ID to raster in to.
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 for (const auto& resource : busy_resources_) { 451 for (const auto& resource : busy_resources_) {
437 resource->OnMemoryDump(pmd, resource_provider_, false /* is_free */); 452 resource->OnMemoryDump(pmd, resource_provider_, false /* is_free */);
438 } 453 }
439 for (const auto& entry : in_use_resources_) { 454 for (const auto& entry : in_use_resources_) {
440 entry.second->OnMemoryDump(pmd, resource_provider_, false /* is_free */); 455 entry.second->OnMemoryDump(pmd, resource_provider_, false /* is_free */);
441 } 456 }
442 return true; 457 return true;
443 } 458 }
444 459
445 } // namespace cc 460 } // namespace cc
OLDNEW
« no previous file with comments | « cc/resources/resource_pool.h ('k') | cc/resources/resource_pool_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698