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

Side by Side Diff: cc/prioritized_resource_manager.cc

Issue 11369109: cc: Rename PrioritizedTexture to PrioritizedResource. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « cc/prioritized_resource_manager.h ('k') | cc/prioritized_resource_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 "config.h" 5 #include "config.h"
6 6
7 #include "cc/prioritized_texture_manager.h" 7 #include "cc/prioritized_resource_manager.h"
8 8
9 #include "base/debug/trace_event.h" 9 #include "base/debug/trace_event.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "cc/prioritized_texture.h" 11 #include "cc/prioritized_resource.h"
12 #include "cc/priority_calculator.h" 12 #include "cc/priority_calculator.h"
13 #include "cc/proxy.h" 13 #include "cc/proxy.h"
14 #include <algorithm> 14 #include <algorithm>
15 15
16 using namespace std; 16 using namespace std;
17 17
18 namespace cc { 18 namespace cc {
19 19
20 PrioritizedTextureManager::PrioritizedTextureManager(size_t maxMemoryLimitBytes, int, int pool) 20 PrioritizedResourceManager::PrioritizedResourceManager(size_t maxMemoryLimitByte s, int, int pool)
21 : m_maxMemoryLimitBytes(maxMemoryLimitBytes) 21 : m_maxMemoryLimitBytes(maxMemoryLimitBytes)
22 , m_externalPriorityCutoff(PriorityCalculator::allowEverythingCutoff()) 22 , m_externalPriorityCutoff(PriorityCalculator::allowEverythingCutoff())
23 , m_memoryUseBytes(0) 23 , m_memoryUseBytes(0)
24 , m_memoryAboveCutoffBytes(0) 24 , m_memoryAboveCutoffBytes(0)
25 , m_memoryAvailableBytes(0) 25 , m_memoryAvailableBytes(0)
26 , m_pool(pool) 26 , m_pool(pool)
27 , m_backingsTailNotSorted(false) 27 , m_backingsTailNotSorted(false)
28 , m_memoryVisibleBytes(0) 28 , m_memoryVisibleBytes(0)
29 , m_memoryVisibleAndNearbyBytes(0) 29 , m_memoryVisibleAndNearbyBytes(0)
30 , m_memoryVisibleLastPushedBytes(0) 30 , m_memoryVisibleLastPushedBytes(0)
31 , m_memoryVisibleAndNearbyLastPushedBytes(0) 31 , m_memoryVisibleAndNearbyLastPushedBytes(0)
32 { 32 {
33 } 33 }
34 34
35 PrioritizedTextureManager::~PrioritizedTextureManager() 35 PrioritizedResourceManager::~PrioritizedResourceManager()
36 { 36 {
37 while (m_textures.size() > 0) 37 while (m_textures.size() > 0)
38 unregisterTexture(*m_textures.begin()); 38 unregisterTexture(*m_textures.begin());
39 39
40 deleteUnlinkedEvictedBackings(); 40 deleteUnlinkedEvictedBackings();
41 DCHECK(m_evictedBackings.empty()); 41 DCHECK(m_evictedBackings.empty());
42 42
43 // Each remaining backing is a leaked opengl texture. There should be none. 43 // Each remaining backing is a leaked opengl texture. There should be none.
44 DCHECK(m_backings.empty()); 44 DCHECK(m_backings.empty());
45 } 45 }
46 46
47 size_t PrioritizedTextureManager::memoryVisibleBytes() const 47 size_t PrioritizedResourceManager::memoryVisibleBytes() const
48 { 48 {
49 DCHECK(Proxy::isImplThread()); 49 DCHECK(Proxy::isImplThread());
50 return m_memoryVisibleLastPushedBytes; 50 return m_memoryVisibleLastPushedBytes;
51 } 51 }
52 52
53 size_t PrioritizedTextureManager::memoryVisibleAndNearbyBytes() const 53 size_t PrioritizedResourceManager::memoryVisibleAndNearbyBytes() const
54 { 54 {
55 DCHECK(Proxy::isImplThread()); 55 DCHECK(Proxy::isImplThread());
56 return m_memoryVisibleAndNearbyLastPushedBytes; 56 return m_memoryVisibleAndNearbyLastPushedBytes;
57 } 57 }
58 58
59 void PrioritizedTextureManager::prioritizeTextures() 59 void PrioritizedResourceManager::prioritizeTextures()
60 { 60 {
61 TRACE_EVENT0("cc", "PrioritizedTextureManager::prioritizeTextures"); 61 TRACE_EVENT0("cc", "PrioritizedResourceManager::prioritizeTextures");
62 DCHECK(Proxy::isMainThread()); 62 DCHECK(Proxy::isMainThread());
63 63
64 // Sorting textures in this function could be replaced by a slightly 64 // Sorting textures in this function could be replaced by a slightly
65 // modified O(n) quick-select to partition textures rather than 65 // modified O(n) quick-select to partition textures rather than
66 // sort them (if performance of the sort becomes an issue). 66 // sort them (if performance of the sort becomes an issue).
67 67
68 TextureVector& sortedTextures = m_tempTextureVector; 68 TextureVector& sortedTextures = m_tempTextureVector;
69 sortedTextures.clear(); 69 sortedTextures.clear();
70 70
71 // Copy all textures into a vector, sort them, and collect memory requiremen ts statistics. 71 // Copy all textures into a vector, sort them, and collect memory requiremen ts statistics.
72 m_memoryVisibleBytes = 0; 72 m_memoryVisibleBytes = 0;
73 m_memoryVisibleAndNearbyBytes = 0; 73 m_memoryVisibleAndNearbyBytes = 0;
74 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); + +it) { 74 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); + +it) {
75 PrioritizedTexture* texture = (*it); 75 PrioritizedResource* texture = (*it);
76 sortedTextures.push_back(texture); 76 sortedTextures.push_back(texture);
77 if (PriorityCalculator::priorityIsHigher(texture->requestPriority(), Pri orityCalculator::allowVisibleOnlyCutoff())) 77 if (PriorityCalculator::priorityIsHigher(texture->requestPriority(), Pri orityCalculator::allowVisibleOnlyCutoff()))
78 m_memoryVisibleBytes += texture->bytes(); 78 m_memoryVisibleBytes += texture->bytes();
79 if (PriorityCalculator::priorityIsHigher(texture->requestPriority(), Pri orityCalculator::allowVisibleAndNearbyCutoff())) 79 if (PriorityCalculator::priorityIsHigher(texture->requestPriority(), Pri orityCalculator::allowVisibleAndNearbyCutoff()))
80 m_memoryVisibleAndNearbyBytes += texture->bytes(); 80 m_memoryVisibleAndNearbyBytes += texture->bytes();
81 } 81 }
82 std::sort(sortedTextures.begin(), sortedTextures.end(), compareTextures); 82 std::sort(sortedTextures.begin(), sortedTextures.end(), compareTextures);
83 83
84 // Compute a priority cutoff based on memory pressure 84 // Compute a priority cutoff based on memory pressure
85 m_memoryAvailableBytes = m_maxMemoryLimitBytes; 85 m_memoryAvailableBytes = m_maxMemoryLimitBytes;
86 m_priorityCutoff = m_externalPriorityCutoff; 86 m_priorityCutoff = m_externalPriorityCutoff;
87 size_t memoryBytes = 0; 87 size_t memoryBytes = 0;
88 for (TextureVector::iterator it = sortedTextures.begin(); it != sortedTextur es.end(); ++it) { 88 for (TextureVector::iterator it = sortedTextures.begin(); it != sortedTextur es.end(); ++it) {
89 if ((*it)->isSelfManaged()) { 89 if ((*it)->isSelfManaged()) {
90 // Account for self-managed memory immediately by reducing the memor y 90 // Account for self-managed memory immediately by reducing the memor y
(...skipping 11 matching lines...) Expand all
102 m_priorityCutoff = (*it)->requestPriority(); 102 m_priorityCutoff = (*it)->requestPriority();
103 break; 103 break;
104 } 104 }
105 memoryBytes = newMemoryBytes; 105 memoryBytes = newMemoryBytes;
106 } 106 }
107 } 107 }
108 108
109 // Disallow any textures with priority below the external cutoff to have bac kings. 109 // Disallow any textures with priority below the external cutoff to have bac kings.
110 size_t memoryLinkedTexturesBytes = 0; 110 size_t memoryLinkedTexturesBytes = 0;
111 for (TextureVector::iterator it = sortedTextures.begin(); it != sortedTextur es.end(); ++it) { 111 for (TextureVector::iterator it = sortedTextures.begin(); it != sortedTextur es.end(); ++it) {
112 PrioritizedTexture* texture = (*it); 112 PrioritizedResource* texture = (*it);
113 if (!PriorityCalculator::priorityIsHigher(texture->requestPriority(), m_ externalPriorityCutoff) && 113 if (!PriorityCalculator::priorityIsHigher(texture->requestPriority(), m_ externalPriorityCutoff) &&
114 texture->haveBackingTexture()) 114 texture->haveBackingTexture())
115 texture->unlink(); 115 texture->unlink();
116 } 116 }
117 DCHECK(memoryLinkedTexturesBytes <= m_memoryAvailableBytes); 117 DCHECK(memoryLinkedTexturesBytes <= m_memoryAvailableBytes);
118 118
119 // Only allow textures if they are higher than the cutoff. All textures 119 // Only allow textures if they are higher than the cutoff. All textures
120 // of the same priority are accepted or rejected together, rather than 120 // of the same priority are accepted or rejected together, rather than
121 // being partially allowed randomly. 121 // being partially allowed randomly.
122 m_memoryAboveCutoffBytes = 0; 122 m_memoryAboveCutoffBytes = 0;
123 for (TextureVector::iterator it = sortedTextures.begin(); it != sortedTextur es.end(); ++it) { 123 for (TextureVector::iterator it = sortedTextures.begin(); it != sortedTextur es.end(); ++it) {
124 bool isAbovePriorityCutoff = PriorityCalculator::priorityIsHigher((*it)- >requestPriority(), m_priorityCutoff); 124 bool isAbovePriorityCutoff = PriorityCalculator::priorityIsHigher((*it)- >requestPriority(), m_priorityCutoff);
125 (*it)->setAbovePriorityCutoff(isAbovePriorityCutoff); 125 (*it)->setAbovePriorityCutoff(isAbovePriorityCutoff);
126 if (isAbovePriorityCutoff && !(*it)->isSelfManaged()) 126 if (isAbovePriorityCutoff && !(*it)->isSelfManaged())
127 m_memoryAboveCutoffBytes += (*it)->bytes(); 127 m_memoryAboveCutoffBytes += (*it)->bytes();
128 } 128 }
129 sortedTextures.clear(); 129 sortedTextures.clear();
130 130
131 DCHECK(m_memoryAboveCutoffBytes <= m_memoryAvailableBytes); 131 DCHECK(m_memoryAboveCutoffBytes <= m_memoryAvailableBytes);
132 DCHECK(memoryAboveCutoffBytes() <= maxMemoryLimitBytes()); 132 DCHECK(memoryAboveCutoffBytes() <= maxMemoryLimitBytes());
133 } 133 }
134 134
135 void PrioritizedTextureManager::pushTexturePrioritiesToBackings() 135 void PrioritizedResourceManager::pushTexturePrioritiesToBackings()
136 { 136 {
137 TRACE_EVENT0("cc", "PrioritizedTextureManager::pushTexturePrioritiesToBackin gs"); 137 TRACE_EVENT0("cc", "PrioritizedResourceManager::pushTexturePrioritiesToBacki ngs");
138 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); 138 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked());
139 139
140 assertInvariants(); 140 assertInvariants();
141 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) 141 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it)
142 (*it)->updatePriority(); 142 (*it)->updatePriority();
143 sortBackings(); 143 sortBackings();
144 assertInvariants(); 144 assertInvariants();
145 145
146 // Push memory requirements to the impl thread structure. 146 // Push memory requirements to the impl thread structure.
147 m_memoryVisibleLastPushedBytes = m_memoryVisibleBytes; 147 m_memoryVisibleLastPushedBytes = m_memoryVisibleBytes;
148 m_memoryVisibleAndNearbyLastPushedBytes = m_memoryVisibleAndNearbyBytes; 148 m_memoryVisibleAndNearbyLastPushedBytes = m_memoryVisibleAndNearbyBytes;
149 } 149 }
150 150
151 void PrioritizedTextureManager::updateBackingsInDrawingImplTree() 151 void PrioritizedResourceManager::updateBackingsInDrawingImplTree()
152 { 152 {
153 TRACE_EVENT0("cc", "PrioritizedTextureManager::updateBackingsInDrawingImplTr ee"); 153 TRACE_EVENT0("cc", "PrioritizedResourceManager::updateBackingsInDrawingImplT ree");
154 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); 154 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked());
155 155
156 assertInvariants(); 156 assertInvariants();
157 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) { 157 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) {
158 PrioritizedTexture::Backing* backing = (*it); 158 PrioritizedResource::Backing* backing = (*it);
159 backing->updateInDrawingImplTree(); 159 backing->updateInDrawingImplTree();
160 } 160 }
161 sortBackings(); 161 sortBackings();
162 assertInvariants(); 162 assertInvariants();
163 } 163 }
164 164
165 void PrioritizedTextureManager::sortBackings() 165 void PrioritizedResourceManager::sortBackings()
166 { 166 {
167 TRACE_EVENT0("cc", "PrioritizedTextureManager::sortBackings"); 167 TRACE_EVENT0("cc", "PrioritizedResourceManager::sortBackings");
168 DCHECK(Proxy::isImplThread()); 168 DCHECK(Proxy::isImplThread());
169 169
170 // Put backings in eviction/recycling order. 170 // Put backings in eviction/recycling order.
171 m_backings.sort(compareBackings); 171 m_backings.sort(compareBackings);
172 m_backingsTailNotSorted = false; 172 m_backingsTailNotSorted = false;
173 } 173 }
174 174
175 void PrioritizedTextureManager::clearPriorities() 175 void PrioritizedResourceManager::clearPriorities()
176 { 176 {
177 DCHECK(Proxy::isMainThread()); 177 DCHECK(Proxy::isMainThread());
178 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); + +it) { 178 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); + +it) {
179 // FIXME: We should remove this and just set all priorities to 179 // FIXME: We should remove this and just set all priorities to
180 // PriorityCalculator::lowestPriority() once we have priorities 180 // PriorityCalculator::lowestPriority() once we have priorities
181 // for all textures (we can't currently calculate distances for 181 // for all textures (we can't currently calculate distances for
182 // off-screen textures). 182 // off-screen textures).
183 (*it)->setRequestPriority(PriorityCalculator::lingeringPriority((*it)->r equestPriority())); 183 (*it)->setRequestPriority(PriorityCalculator::lingeringPriority((*it)->r equestPriority()));
184 } 184 }
185 } 185 }
186 186
187 bool PrioritizedTextureManager::requestLate(PrioritizedTexture* texture) 187 bool PrioritizedResourceManager::requestLate(PrioritizedResource* texture)
188 { 188 {
189 DCHECK(Proxy::isMainThread()); 189 DCHECK(Proxy::isMainThread());
190 190
191 // This is already above cutoff, so don't double count it's memory below. 191 // This is already above cutoff, so don't double count it's memory below.
192 if (texture->isAbovePriorityCutoff()) 192 if (texture->isAbovePriorityCutoff())
193 return true; 193 return true;
194 194
195 // Allow textures that have priority equal to the cutoff, but not strictly l ower. 195 // Allow textures that have priority equal to the cutoff, but not strictly l ower.
196 if (PriorityCalculator::priorityIsLower(texture->requestPriority(), m_priori tyCutoff)) 196 if (PriorityCalculator::priorityIsLower(texture->requestPriority(), m_priori tyCutoff))
197 return false; 197 return false;
198 198
199 // Disallow textures that do not have a priority strictly higher than the ex ternal cutoff. 199 // Disallow textures that do not have a priority strictly higher than the ex ternal cutoff.
200 if (!PriorityCalculator::priorityIsHigher(texture->requestPriority(), m_exte rnalPriorityCutoff)) 200 if (!PriorityCalculator::priorityIsHigher(texture->requestPriority(), m_exte rnalPriorityCutoff))
201 return false; 201 return false;
202 202
203 size_t newMemoryBytes = m_memoryAboveCutoffBytes + texture->bytes(); 203 size_t newMemoryBytes = m_memoryAboveCutoffBytes + texture->bytes();
204 if (newMemoryBytes > m_memoryAvailableBytes) 204 if (newMemoryBytes > m_memoryAvailableBytes)
205 return false; 205 return false;
206 206
207 m_memoryAboveCutoffBytes = newMemoryBytes; 207 m_memoryAboveCutoffBytes = newMemoryBytes;
208 texture->setAbovePriorityCutoff(true); 208 texture->setAbovePriorityCutoff(true);
209 return true; 209 return true;
210 } 210 }
211 211
212 void PrioritizedTextureManager::acquireBackingTextureIfNeeded(PrioritizedTexture * texture, ResourceProvider* resourceProvider) 212 void PrioritizedResourceManager::acquireBackingTextureIfNeeded(PrioritizedResour ce* texture, ResourceProvider* resourceProvider)
213 { 213 {
214 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); 214 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked());
215 DCHECK(!texture->isSelfManaged()); 215 DCHECK(!texture->isSelfManaged());
216 DCHECK(texture->isAbovePriorityCutoff()); 216 DCHECK(texture->isAbovePriorityCutoff());
217 if (texture->backing() || !texture->isAbovePriorityCutoff()) 217 if (texture->backing() || !texture->isAbovePriorityCutoff())
218 return; 218 return;
219 219
220 // Find a backing below, by either recycling or allocating. 220 // Find a backing below, by either recycling or allocating.
221 PrioritizedTexture::Backing* backing = 0; 221 PrioritizedResource::Backing* backing = 0;
222 222
223 // First try to recycle 223 // First try to recycle
224 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) { 224 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) {
225 if (!(*it)->canBeRecycled()) 225 if (!(*it)->canBeRecycled())
226 break; 226 break;
227 if ((*it)->size() == texture->size() && (*it)->format() == texture->form at()) { 227 if ((*it)->size() == texture->size() && (*it)->format() == texture->form at()) {
228 backing = (*it); 228 backing = (*it);
229 m_backings.erase(it); 229 m_backings.erase(it);
230 break; 230 break;
231 } 231 }
(...skipping 10 matching lines...) Expand all
242 if (backing->owner()) 242 if (backing->owner())
243 backing->owner()->unlink(); 243 backing->owner()->unlink();
244 texture->link(backing); 244 texture->link(backing);
245 m_backings.push_back(backing); 245 m_backings.push_back(backing);
246 m_backingsTailNotSorted = true; 246 m_backingsTailNotSorted = true;
247 247
248 // Update the backing's priority from its new owner. 248 // Update the backing's priority from its new owner.
249 backing->updatePriority(); 249 backing->updatePriority();
250 } 250 }
251 251
252 bool PrioritizedTextureManager::evictBackingsToReduceMemory(size_t limitBytes, i nt priorityCutoff, EvictionPolicy evictionPolicy, ResourceProvider* resourceProv ider) 252 bool PrioritizedResourceManager::evictBackingsToReduceMemory(size_t limitBytes, int priorityCutoff, EvictionPolicy evictionPolicy, ResourceProvider* resourcePro vider)
253 { 253 {
254 DCHECK(Proxy::isImplThread()); 254 DCHECK(Proxy::isImplThread());
255 if (memoryUseBytes() <= limitBytes && PriorityCalculator::allowEverythingCut off() == priorityCutoff) 255 if (memoryUseBytes() <= limitBytes && PriorityCalculator::allowEverythingCut off() == priorityCutoff)
256 return false; 256 return false;
257 257
258 // Destroy backings until we are below the limit, 258 // Destroy backings until we are below the limit,
259 // or until all backings remaining are above the cutoff. 259 // or until all backings remaining are above the cutoff.
260 while (m_backings.size() > 0) { 260 while (m_backings.size() > 0) {
261 PrioritizedTexture::Backing* backing = m_backings.front(); 261 PrioritizedResource::Backing* backing = m_backings.front();
262 if (memoryUseBytes() <= limitBytes && 262 if (memoryUseBytes() <= limitBytes &&
263 PriorityCalculator::priorityIsHigher(backing->requestPriorityAtLastP riorityUpdate(), priorityCutoff)) 263 PriorityCalculator::priorityIsHigher(backing->requestPriorityAtLastP riorityUpdate(), priorityCutoff))
264 break; 264 break;
265 if (evictionPolicy == EvictOnlyRecyclable && !backing->canBeRecycled()) 265 if (evictionPolicy == EvictOnlyRecyclable && !backing->canBeRecycled())
266 break; 266 break;
267 evictFirstBackingResource(resourceProvider); 267 evictFirstBackingResource(resourceProvider);
268 } 268 }
269 return true; 269 return true;
270 } 270 }
271 271
272 void PrioritizedTextureManager::reduceMemory(ResourceProvider* resourceProvider) 272 void PrioritizedResourceManager::reduceMemory(ResourceProvider* resourceProvider )
273 { 273 {
274 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); 274 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked());
275 275
276 // Note that it will not always be the case that memoryUseBytes() <= maxMemo ryLimitBytes(), 276 // Note that it will not always be the case that memoryUseBytes() <= maxMemo ryLimitBytes(),
277 // because we are not at liberty to delete textures that are referenced by t he impl tree to 277 // because we are not at liberty to delete textures that are referenced by t he impl tree to
278 // get more space. 278 // get more space.
279 279
280 evictBackingsToReduceMemory(m_memoryAvailableBytes, PriorityCalculator::allo wEverythingCutoff(), EvictOnlyRecyclable, resourceProvider); 280 evictBackingsToReduceMemory(m_memoryAvailableBytes, PriorityCalculator::allo wEverythingCutoff(), EvictOnlyRecyclable, resourceProvider);
281 281
282 // We currently collect backings from deleted textures for later recycling. 282 // We currently collect backings from deleted textures for later recycling.
283 // However, if we do that forever we will always use the max limit even if 283 // However, if we do that forever we will always use the max limit even if
284 // we really need very little memory. This should probably be solved by redu cing the 284 // we really need very little memory. This should probably be solved by redu cing the
285 // limit externally, but until then this just does some "clean up" of unused 285 // limit externally, but until then this just does some "clean up" of unused
286 // backing textures (any more than 10%). 286 // backing textures (any more than 10%).
287 size_t wastedMemory = 0; 287 size_t wastedMemory = 0;
288 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) { 288 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) {
289 if ((*it)->owner()) 289 if ((*it)->owner())
290 break; 290 break;
291 wastedMemory += (*it)->bytes(); 291 wastedMemory += (*it)->bytes();
292 } 292 }
293 size_t tenPercentOfMemory = m_memoryAvailableBytes / 10; 293 size_t tenPercentOfMemory = m_memoryAvailableBytes / 10;
294 if (wastedMemory > tenPercentOfMemory) 294 if (wastedMemory > tenPercentOfMemory)
295 evictBackingsToReduceMemory(memoryUseBytes() - (wastedMemory - tenPercen tOfMemory), PriorityCalculator::allowEverythingCutoff(), EvictOnlyRecyclable, re sourceProvider); 295 evictBackingsToReduceMemory(memoryUseBytes() - (wastedMemory - tenPercen tOfMemory), PriorityCalculator::allowEverythingCutoff(), EvictOnlyRecyclable, re sourceProvider);
296 296
297 // Unlink all evicted backings 297 // Unlink all evicted backings
298 for (BackingList::const_iterator it = m_evictedBackings.begin(); it != m_evi ctedBackings.end(); ++it) { 298 for (BackingList::const_iterator it = m_evictedBackings.begin(); it != m_evi ctedBackings.end(); ++it) {
299 if ((*it)->owner()) 299 if ((*it)->owner())
300 (*it)->owner()->unlink(); 300 (*it)->owner()->unlink();
301 } 301 }
302 302
303 // And clear the list of evicted backings 303 // And clear the list of evicted backings
304 deleteUnlinkedEvictedBackings(); 304 deleteUnlinkedEvictedBackings();
305 } 305 }
306 306
307 void PrioritizedTextureManager::clearAllMemory(ResourceProvider* resourceProvide r) 307 void PrioritizedResourceManager::clearAllMemory(ResourceProvider* resourceProvid er)
308 { 308 {
309 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); 309 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked());
310 DCHECK(resourceProvider); 310 DCHECK(resourceProvider);
311 evictBackingsToReduceMemory(0, PriorityCalculator::allowEverythingCutoff(), EvictAnything, resourceProvider); 311 evictBackingsToReduceMemory(0, PriorityCalculator::allowEverythingCutoff(), EvictAnything, resourceProvider);
312 } 312 }
313 313
314 bool PrioritizedTextureManager::reduceMemoryOnImplThread(size_t limitBytes, int priorityCutoff, ResourceProvider* resourceProvider) 314 bool PrioritizedResourceManager::reduceMemoryOnImplThread(size_t limitBytes, int priorityCutoff, ResourceProvider* resourceProvider)
315 { 315 {
316 DCHECK(Proxy::isImplThread()); 316 DCHECK(Proxy::isImplThread());
317 DCHECK(resourceProvider); 317 DCHECK(resourceProvider);
318 // If we are in the process of uploading a new frame then the backings at th e very end of 318 // If we are in the process of uploading a new frame then the backings at th e very end of
319 // the list are not sorted by priority. Sort them before doing the eviction. 319 // the list are not sorted by priority. Sort them before doing the eviction.
320 if (m_backingsTailNotSorted) 320 if (m_backingsTailNotSorted)
321 sortBackings(); 321 sortBackings();
322 return evictBackingsToReduceMemory(limitBytes, priorityCutoff, EvictAnything , resourceProvider); 322 return evictBackingsToReduceMemory(limitBytes, priorityCutoff, EvictAnything , resourceProvider);
323 } 323 }
324 324
325 void PrioritizedTextureManager::getEvictedBackings(BackingList& evictedBackings) 325 void PrioritizedResourceManager::getEvictedBackings(BackingList& evictedBackings )
326 { 326 {
327 DCHECK(Proxy::isImplThread()); 327 DCHECK(Proxy::isImplThread());
328 evictedBackings.clear(); 328 evictedBackings.clear();
329 evictedBackings.insert(evictedBackings.begin(), m_evictedBackings.begin(), m _evictedBackings.end()); 329 evictedBackings.insert(evictedBackings.begin(), m_evictedBackings.begin(), m _evictedBackings.end());
330 } 330 }
331 331
332 void PrioritizedTextureManager::unlinkEvictedBackings(const BackingList& evicted Backings) 332 void PrioritizedResourceManager::unlinkEvictedBackings(const BackingList& evicte dBackings)
333 { 333 {
334 DCHECK(Proxy::isMainThread()); 334 DCHECK(Proxy::isMainThread());
335 for (BackingList::const_iterator it = evictedBackings.begin(); it != evicted Backings.end(); ++it) { 335 for (BackingList::const_iterator it = evictedBackings.begin(); it != evicted Backings.end(); ++it) {
336 PrioritizedTexture::Backing* backing = (*it); 336 PrioritizedResource::Backing* backing = (*it);
337 if (backing->owner()) 337 if (backing->owner())
338 backing->owner()->unlink(); 338 backing->owner()->unlink();
339 } 339 }
340 } 340 }
341 341
342 void PrioritizedTextureManager::deleteUnlinkedEvictedBackings() 342 void PrioritizedResourceManager::deleteUnlinkedEvictedBackings()
343 { 343 {
344 DCHECK(Proxy::isMainThread() || (Proxy::isImplThread() && Proxy::isMainThrea dBlocked())); 344 DCHECK(Proxy::isMainThread() || (Proxy::isImplThread() && Proxy::isMainThrea dBlocked()));
345 BackingList newEvictedBackings; 345 BackingList newEvictedBackings;
346 for (BackingList::const_iterator it = m_evictedBackings.begin(); it != m_evi ctedBackings.end(); ++it) { 346 for (BackingList::const_iterator it = m_evictedBackings.begin(); it != m_evi ctedBackings.end(); ++it) {
347 PrioritizedTexture::Backing* backing = (*it); 347 PrioritizedResource::Backing* backing = (*it);
348 if (backing->owner()) 348 if (backing->owner())
349 newEvictedBackings.push_back(backing); 349 newEvictedBackings.push_back(backing);
350 else 350 else
351 delete backing; 351 delete backing;
352 } 352 }
353 m_evictedBackings.swap(newEvictedBackings); 353 m_evictedBackings.swap(newEvictedBackings);
354 } 354 }
355 355
356 bool PrioritizedTextureManager::linkedEvictedBackingsExist() const 356 bool PrioritizedResourceManager::linkedEvictedBackingsExist() const
357 { 357 {
358 for (BackingList::const_iterator it = m_evictedBackings.begin(); it != m_evi ctedBackings.end(); ++it) { 358 for (BackingList::const_iterator it = m_evictedBackings.begin(); it != m_evi ctedBackings.end(); ++it) {
359 if ((*it)->owner()) 359 if ((*it)->owner())
360 return true; 360 return true;
361 } 361 }
362 return false; 362 return false;
363 } 363 }
364 364
365 void PrioritizedTextureManager::registerTexture(PrioritizedTexture* texture) 365 void PrioritizedResourceManager::registerTexture(PrioritizedResource* texture)
366 { 366 {
367 DCHECK(Proxy::isMainThread()); 367 DCHECK(Proxy::isMainThread());
368 DCHECK(texture); 368 DCHECK(texture);
369 DCHECK(!texture->textureManager()); 369 DCHECK(!texture->resourceManager());
370 DCHECK(!texture->backing()); 370 DCHECK(!texture->backing());
371 DCHECK(!ContainsKey(m_textures, texture)); 371 DCHECK(!ContainsKey(m_textures, texture));
372 372
373 texture->setManagerInternal(this); 373 texture->setManagerInternal(this);
374 m_textures.insert(texture); 374 m_textures.insert(texture);
375 375
376 } 376 }
377 377
378 void PrioritizedTextureManager::unregisterTexture(PrioritizedTexture* texture) 378 void PrioritizedResourceManager::unregisterTexture(PrioritizedResource* texture)
379 { 379 {
380 DCHECK(Proxy::isMainThread() || (Proxy::isImplThread() && Proxy::isMainThrea dBlocked())); 380 DCHECK(Proxy::isMainThread() || (Proxy::isImplThread() && Proxy::isMainThrea dBlocked()));
381 DCHECK(texture); 381 DCHECK(texture);
382 DCHECK(ContainsKey(m_textures, texture)); 382 DCHECK(ContainsKey(m_textures, texture));
383 383
384 returnBackingTexture(texture); 384 returnBackingTexture(texture);
385 texture->setManagerInternal(0); 385 texture->setManagerInternal(0);
386 m_textures.erase(texture); 386 m_textures.erase(texture);
387 texture->setAbovePriorityCutoff(false); 387 texture->setAbovePriorityCutoff(false);
388 } 388 }
389 389
390 void PrioritizedTextureManager::returnBackingTexture(PrioritizedTexture* texture ) 390 void PrioritizedResourceManager::returnBackingTexture(PrioritizedResource* textu re)
391 { 391 {
392 DCHECK(Proxy::isMainThread() || (Proxy::isImplThread() && Proxy::isMainThrea dBlocked())); 392 DCHECK(Proxy::isMainThread() || (Proxy::isImplThread() && Proxy::isMainThrea dBlocked()));
393 if (texture->backing()) 393 if (texture->backing())
394 texture->unlink(); 394 texture->unlink();
395 } 395 }
396 396
397 PrioritizedTexture::Backing* PrioritizedTextureManager::createBacking(gfx::Size size, GLenum format, ResourceProvider* resourceProvider) 397 PrioritizedResource::Backing* PrioritizedResourceManager::createBacking(gfx::Siz e size, GLenum format, ResourceProvider* resourceProvider)
398 { 398 {
399 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); 399 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked());
400 DCHECK(resourceProvider); 400 DCHECK(resourceProvider);
401 ResourceProvider::ResourceId resourceId = resourceProvider->createResource(m _pool, size, format, ResourceProvider::TextureUsageAny); 401 ResourceProvider::ResourceId resourceId = resourceProvider->createResource(m _pool, size, format, ResourceProvider::TextureUsageAny);
402 PrioritizedTexture::Backing* backing = new PrioritizedTexture::Backing(resou rceId, resourceProvider, size, format); 402 PrioritizedResource::Backing* backing = new PrioritizedResource::Backing(res ourceId, resourceProvider, size, format);
403 m_memoryUseBytes += backing->bytes(); 403 m_memoryUseBytes += backing->bytes();
404 return backing; 404 return backing;
405 } 405 }
406 406
407 void PrioritizedTextureManager::evictFirstBackingResource(ResourceProvider* reso urceProvider) 407 void PrioritizedResourceManager::evictFirstBackingResource(ResourceProvider* res ourceProvider)
408 { 408 {
409 DCHECK(Proxy::isImplThread()); 409 DCHECK(Proxy::isImplThread());
410 DCHECK(resourceProvider); 410 DCHECK(resourceProvider);
411 DCHECK(!m_backings.empty()); 411 DCHECK(!m_backings.empty());
412 PrioritizedTexture::Backing* backing = m_backings.front(); 412 PrioritizedResource::Backing* backing = m_backings.front();
413 413
414 // Note that we create a backing and its resource at the same time, but we 414 // Note that we create a backing and its resource at the same time, but we
415 // delete the backing structure and its resource in two steps. This is becau se 415 // delete the backing structure and its resource in two steps. This is becau se
416 // we can delete the resource while the main thread is running, but we canno t 416 // we can delete the resource while the main thread is running, but we canno t
417 // unlink backings while the main thread is running. 417 // unlink backings while the main thread is running.
418 backing->deleteResource(resourceProvider); 418 backing->deleteResource(resourceProvider);
419 m_memoryUseBytes -= backing->bytes(); 419 m_memoryUseBytes -= backing->bytes();
420 m_backings.pop_front(); 420 m_backings.pop_front();
421 m_evictedBackings.push_back(backing); 421 m_evictedBackings.push_back(backing);
422 } 422 }
423 423
424 void PrioritizedTextureManager::assertInvariants() 424 void PrioritizedResourceManager::assertInvariants()
425 { 425 {
426 #ifndef NDEBUG 426 #ifndef NDEBUG
427 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); 427 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked());
428 428
429 // If we hit any of these asserts, there is a bug in this class. To see 429 // If we hit any of these asserts, there is a bug in this class. To see
430 // where the bug is, call this function at the beginning and end of 430 // where the bug is, call this function at the beginning and end of
431 // every public function. 431 // every public function.
432 432
433 // Backings/textures must be doubly-linked and only to other backings/textur es in this manager. 433 // Backings/textures must be doubly-linked and only to other backings/textur es in this manager.
434 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) { 434 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) {
435 if ((*it)->owner()) { 435 if ((*it)->owner()) {
436 DCHECK(ContainsKey(m_textures, (*it)->owner())); 436 DCHECK(ContainsKey(m_textures, (*it)->owner()));
437 DCHECK((*it)->owner()->backing() == (*it)); 437 DCHECK((*it)->owner()->backing() == (*it));
438 } 438 }
439 } 439 }
440 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); + +it) { 440 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); + +it) {
441 PrioritizedTexture* texture = (*it); 441 PrioritizedResource* texture = (*it);
442 PrioritizedTexture::Backing* backing = texture->backing(); 442 PrioritizedResource::Backing* backing = texture->backing();
443 if (backing) { 443 if (backing) {
444 if (backing->resourceHasBeenDeleted()) { 444 if (backing->resourceHasBeenDeleted()) {
445 DCHECK(std::find(m_backings.begin(), m_backings.end(), backing) == m_backings.end()); 445 DCHECK(std::find(m_backings.begin(), m_backings.end(), backing) == m_backings.end());
446 DCHECK(std::find(m_evictedBackings.begin(), m_evictedBackings.en d(), backing) != m_evictedBackings.end()); 446 DCHECK(std::find(m_evictedBackings.begin(), m_evictedBackings.en d(), backing) != m_evictedBackings.end());
447 } else { 447 } else {
448 DCHECK(std::find(m_backings.begin(), m_backings.end(), backing) != m_backings.end()); 448 DCHECK(std::find(m_backings.begin(), m_backings.end(), backing) != m_backings.end());
449 DCHECK(std::find(m_evictedBackings.begin(), m_evictedBackings.en d(), backing) == m_evictedBackings.end()); 449 DCHECK(std::find(m_evictedBackings.begin(), m_evictedBackings.en d(), backing) == m_evictedBackings.end());
450 } 450 }
451 DCHECK(backing->owner() == texture); 451 DCHECK(backing->owner() == texture);
452 } 452 }
453 } 453 }
454 454
455 // At all times, backings that can be evicted must always come before 455 // At all times, backings that can be evicted must always come before
456 // backings that can't be evicted in the backing texture list (otherwise 456 // backings that can't be evicted in the backing texture list (otherwise
457 // reduceMemory will not find all textures available for eviction/recycling) . 457 // reduceMemory will not find all textures available for eviction/recycling) .
458 bool reachedUnrecyclable = false; 458 bool reachedUnrecyclable = false;
459 PrioritizedTexture::Backing* previous_backing = NULL; 459 PrioritizedResource::Backing* previous_backing = NULL;
460 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) { 460 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) {
461 PrioritizedTexture::Backing* backing = *it; 461 PrioritizedResource::Backing* backing = *it;
462 if (previous_backing && (!m_backingsTailNotSorted || !backing->wasAboveP riorityCutoffAtLastPriorityUpdate())) 462 if (previous_backing && (!m_backingsTailNotSorted || !backing->wasAboveP riorityCutoffAtLastPriorityUpdate()))
463 DCHECK(compareBackings(previous_backing, backing)); 463 DCHECK(compareBackings(previous_backing, backing));
464 if (!backing->canBeRecycled()) 464 if (!backing->canBeRecycled())
465 reachedUnrecyclable = true; 465 reachedUnrecyclable = true;
466 if (reachedUnrecyclable) 466 if (reachedUnrecyclable)
467 DCHECK(!backing->canBeRecycled()); 467 DCHECK(!backing->canBeRecycled());
468 else 468 else
469 DCHECK(backing->canBeRecycled()); 469 DCHECK(backing->canBeRecycled());
470 previous_backing = backing; 470 previous_backing = backing;
471 } 471 }
472 #endif 472 #endif
473 } 473 }
474 474
475 } // namespace cc 475 } // namespace cc
OLDNEW
« no previous file with comments | « cc/prioritized_resource_manager.h ('k') | cc/prioritized_resource_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698