Chromium Code Reviews| 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 "config.h" | 5 #include "config.h" |
| 6 | 6 |
| 7 #include "CCPrioritizedTextureManager.h" | 7 #include "CCPrioritizedTextureManager.h" |
| 8 | 8 |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "CCPrioritizedTexture.h" | 10 #include "CCPrioritizedTexture.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 , m_pool(pool) | 25 , m_pool(pool) |
| 26 { | 26 { |
| 27 } | 27 } |
| 28 | 28 |
| 29 CCPrioritizedTextureManager::~CCPrioritizedTextureManager() | 29 CCPrioritizedTextureManager::~CCPrioritizedTextureManager() |
| 30 { | 30 { |
| 31 while (m_textures.size() > 0) | 31 while (m_textures.size() > 0) |
| 32 unregisterTexture(*m_textures.begin()); | 32 unregisterTexture(*m_textures.begin()); |
| 33 | 33 |
| 34 deleteUnlinkedEvictedBackings(); | 34 deleteUnlinkedEvictedBackings(); |
| 35 ASSERT(m_evictedBackings.isEmpty()); | 35 ASSERT(m_evictedBackings.empty()); |
| 36 | 36 |
| 37 // Each remaining backing is a leaked opengl texture. There should be none. | 37 // Each remaining backing is a leaked opengl texture. There should be none. |
| 38 ASSERT(m_backings.isEmpty()); | 38 ASSERT(m_backings.empty()); |
| 39 } | 39 } |
| 40 | 40 |
| 41 void CCPrioritizedTextureManager::prioritizeTextures() | 41 void CCPrioritizedTextureManager::prioritizeTextures() |
| 42 { | 42 { |
| 43 TRACE_EVENT0("cc", "CCPrioritizedTextureManager::prioritizeTextures"); | 43 TRACE_EVENT0("cc", "CCPrioritizedTextureManager::prioritizeTextures"); |
| 44 ASSERT(CCProxy::isMainThread()); | 44 ASSERT(CCProxy::isMainThread()); |
| 45 | 45 |
| 46 // Sorting textures in this function could be replaced by a slightly | 46 // Sorting textures in this function could be replaced by a slightly |
| 47 // modified O(n) quick-select to partition textures rather than | 47 // modified O(n) quick-select to partition textures rather than |
| 48 // sort them (if performance of the sort becomes an issue). | 48 // sort them (if performance of the sort becomes an issue). |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 | 96 |
| 97 ASSERT(m_memoryAboveCutoffBytes <= m_memoryAvailableBytes); | 97 ASSERT(m_memoryAboveCutoffBytes <= m_memoryAvailableBytes); |
| 98 ASSERT(memoryAboveCutoffBytes() <= maxMemoryLimitBytes()); | 98 ASSERT(memoryAboveCutoffBytes() <= maxMemoryLimitBytes()); |
| 99 } | 99 } |
| 100 | 100 |
| 101 void CCPrioritizedTextureManager::pushTexturePrioritiesToBackings() | 101 void CCPrioritizedTextureManager::pushTexturePrioritiesToBackings() |
| 102 { | 102 { |
| 103 TRACE_EVENT0("cc", "CCPrioritizedTextureManager::pushTexturePrioritiesToBack ings"); | 103 TRACE_EVENT0("cc", "CCPrioritizedTextureManager::pushTexturePrioritiesToBack ings"); |
| 104 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | 104 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); |
| 105 | 105 |
| 106 for (BackingSet::iterator it = m_backings.begin(); it != m_backings.end(); + +it) | 106 assertInvariants(); |
| 107 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) | |
| 107 (*it)->updatePriority(); | 108 (*it)->updatePriority(); |
| 108 | |
| 109 sortBackings(); | 109 sortBackings(); |
| 110 assertInvariants(); | |
| 110 } | 111 } |
| 111 | 112 |
| 112 void CCPrioritizedTextureManager::updateBackingsInDrawingImplTree() | 113 void CCPrioritizedTextureManager::updateBackingsInDrawingImplTree() |
| 113 { | 114 { |
| 114 TRACE_EVENT0("cc", "CCPrioritizedTextureManager::updateBackingsInDrawingImpl Tree"); | 115 TRACE_EVENT0("cc", "CCPrioritizedTextureManager::updateBackingsInDrawingImpl Tree"); |
| 115 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | 116 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); |
| 116 | 117 |
| 117 for (BackingSet::iterator it = m_backings.begin(); it != m_backings.end(); + +it) { | 118 assertInvariants(); |
| 119 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) { | |
| 118 CCPrioritizedTexture::Backing* backing = (*it); | 120 CCPrioritizedTexture::Backing* backing = (*it); |
| 119 backing->updateInDrawingImplTree(); | 121 backing->updateInDrawingImplTree(); |
| 120 } | 122 } |
| 121 | |
| 122 sortBackings(); | 123 sortBackings(); |
| 124 assertInvariants(); | |
| 123 } | 125 } |
| 124 | 126 |
| 125 void CCPrioritizedTextureManager::sortBackings() | 127 void CCPrioritizedTextureManager::sortBackings() |
| 126 { | 128 { |
| 127 TRACE_EVENT0("cc", "CCPrioritizedTextureManager::updateBackingsPriorities"); | 129 TRACE_EVENT0("cc", "CCPrioritizedTextureManager::sortBackings"); |
| 128 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | 130 ASSERT(CCProxy::isImplThread()); |
| 129 | 131 |
| 130 // Update backings' priorities and put backings in eviction/recycling order. | 132 // Put backings in eviction/recycling order. |
| 131 BackingVector& sortedBackings = m_tempBackingVector; | 133 m_backings.sort(compareBackings); |
| 132 sortedBackings.clear(); | |
| 133 for (BackingSet::iterator it = m_backings.begin(); it != m_backings.end(); + +it) | |
| 134 sortedBackings.append(*it); | |
| 135 std::sort(sortedBackings.begin(), sortedBackings.end(), compareBackings); | |
| 136 | |
| 137 for (BackingVector::iterator it = sortedBackings.begin(); it != sortedBackin gs.end(); ++it) { | |
| 138 m_backings.remove(*it); | |
| 139 m_backings.add(*it); | |
| 140 } | |
| 141 sortedBackings.clear(); | |
| 142 | |
| 143 #if !ASSERT_DISABLED | |
| 144 assertInvariants(); | |
| 145 #endif | |
| 146 } | 134 } |
| 147 | 135 |
| 148 void CCPrioritizedTextureManager::clearPriorities() | 136 void CCPrioritizedTextureManager::clearPriorities() |
| 149 { | 137 { |
| 150 ASSERT(CCProxy::isMainThread()); | 138 ASSERT(CCProxy::isMainThread()); |
| 151 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); + +it) { | 139 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); + +it) { |
| 152 // FIXME: We should remove this and just set all priorities to | 140 // FIXME: We should remove this and just set all priorities to |
| 153 // CCPriorityCalculator::lowestPriority() once we have priorities | 141 // CCPriorityCalculator::lowestPriority() once we have priorities |
| 154 // for all textures (we can't currently calculate distances for | 142 // for all textures (we can't currently calculate distances for |
| 155 // off-screen textures). | 143 // off-screen textures). |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 182 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | 170 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); |
| 183 ASSERT(!texture->isSelfManaged()); | 171 ASSERT(!texture->isSelfManaged()); |
| 184 ASSERT(texture->isAbovePriorityCutoff()); | 172 ASSERT(texture->isAbovePriorityCutoff()); |
| 185 if (texture->backing() || !texture->isAbovePriorityCutoff()) | 173 if (texture->backing() || !texture->isAbovePriorityCutoff()) |
| 186 return; | 174 return; |
| 187 | 175 |
| 188 // Find a backing below, by either recycling or allocating. | 176 // Find a backing below, by either recycling or allocating. |
| 189 CCPrioritizedTexture::Backing* backing = 0; | 177 CCPrioritizedTexture::Backing* backing = 0; |
| 190 | 178 |
| 191 // First try to recycle | 179 // First try to recycle |
| 192 for (BackingSet::iterator it = m_backings.begin(); it != m_backings.end(); + +it) { | 180 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) { |
| 193 if (!(*it)->canBeRecycled()) | 181 if (!(*it)->canBeRecycled()) |
| 194 break; | 182 break; |
| 195 if ((*it)->size() == texture->size() && (*it)->format() == texture->form at()) { | 183 if ((*it)->size() == texture->size() && (*it)->format() == texture->form at()) { |
| 196 backing = (*it); | 184 backing = (*it); |
| 185 m_backings.erase(it); | |
| 197 break; | 186 break; |
| 198 } | 187 } |
| 199 } | 188 } |
| 200 | 189 |
| 201 // Otherwise reduce memory and just allocate a new backing texures. | 190 // Otherwise reduce memory and just allocate a new backing texures. |
| 202 if (!backing) { | 191 if (!backing) { |
| 203 evictBackingsToReduceMemory(m_memoryAvailableBytes - texture->bytes(), R espectManagerPriorityCutoff, resourceProvider); | 192 evictBackingsToReduceMemory(m_memoryAvailableBytes - texture->bytes(), R espectManagerPriorityCutoff, resourceProvider); |
| 204 backing = createBacking(texture->size(), texture->format(), resourceProv ider); | 193 backing = createBacking(texture->size(), texture->format(), resourceProv ider); |
| 205 } | 194 } |
| 206 | 195 |
| 207 // Move the used backing texture to the end of the eviction list. | 196 // Move the used backing texture to the end of the eviction list. |
| 208 if (backing->owner()) | 197 if (backing->owner()) |
| 209 backing->owner()->unlink(); | 198 backing->owner()->unlink(); |
| 210 texture->link(backing); | 199 texture->link(backing); |
| 211 m_backings.remove(backing); | 200 m_backings.push_back(backing); |
| 212 m_backings.add(backing); | |
| 213 | 201 |
| 214 // Update the backing's priority from its new owner. | 202 // Update the backing's priority from its new owner. |
| 215 backing->updatePriority(); | 203 backing->updatePriority(); |
| 216 } | 204 } |
| 217 | 205 |
| 218 void CCPrioritizedTextureManager::evictBackingsToReduceMemory(size_t limitBytes, EvictionPriorityPolicy evictionPolicy, CCResourceProvider* resourceProvider) | 206 void CCPrioritizedTextureManager::evictBackingsToReduceMemory(size_t limitBytes, EvictionPriorityPolicy evictionPolicy, CCResourceProvider* resourceProvider) |
| 219 { | 207 { |
| 220 ASSERT(CCProxy::isImplThread()); | 208 ASSERT(CCProxy::isImplThread()); |
| 221 if (memoryUseBytes() <= limitBytes) | 209 if (memoryUseBytes() <= limitBytes) |
| 222 return; | 210 return; |
| 223 | 211 |
| 224 // Destroy backings until we are below the limit, | 212 // Destroy backings until we are below the limit, |
| 225 // or until all backings remaining are above the cutoff. | 213 // or until all backings remaining are above the cutoff. |
| 226 while (memoryUseBytes() > limitBytes && m_backings.size() > 0) { | 214 while (memoryUseBytes() > limitBytes && m_backings.size() > 0) { |
| 227 CCPrioritizedTexture::Backing* backing = *m_backings.begin(); | 215 CCPrioritizedTexture::Backing* backing = m_backings.front(); |
| 228 if (evictionPolicy == RespectManagerPriorityCutoff) | 216 if (evictionPolicy == RespectManagerPriorityCutoff) |
| 229 if (backing->wasAbovePriorityCutoffAtLastPriorityUpdate()) | 217 if (backing->wasAbovePriorityCutoffAtLastPriorityUpdate()) |
| 230 break; | 218 break; |
| 231 evictBackingResource(backing, resourceProvider); | 219 evictFirstBackingResource(resourceProvider); |
| 232 } | 220 } |
| 233 } | 221 } |
| 234 | 222 |
| 235 void CCPrioritizedTextureManager::reduceMemory(CCResourceProvider* resourceProvi der) | 223 void CCPrioritizedTextureManager::reduceMemory(CCResourceProvider* resourceProvi der) |
| 236 { | 224 { |
| 237 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | 225 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); |
| 238 | 226 |
| 239 evictBackingsToReduceMemory(m_memoryAvailableBytes, RespectManagerPriorityCu toff, resourceProvider); | 227 evictBackingsToReduceMemory(m_memoryAvailableBytes, RespectManagerPriorityCu toff, resourceProvider); |
| 240 ASSERT(memoryUseBytes() <= maxMemoryLimitBytes()); | 228 ASSERT(memoryUseBytes() <= maxMemoryLimitBytes()); |
| 241 | 229 |
| 242 // We currently collect backings from deleted textures for later recycling. | 230 // We currently collect backings from deleted textures for later recycling. |
| 243 // However, if we do that forever we will always use the max limit even if | 231 // However, if we do that forever we will always use the max limit even if |
| 244 // we really need very little memory. This should probably be solved by redu cing the | 232 // we really need very little memory. This should probably be solved by redu cing the |
| 245 // limit externally, but until then this just does some "clean up" of unused | 233 // limit externally, but until then this just does some "clean up" of unused |
| 246 // backing textures (any more than 10%). | 234 // backing textures (any more than 10%). |
| 247 size_t wastedMemory = 0; | 235 size_t wastedMemory = 0; |
| 248 for (BackingSet::iterator it = m_backings.begin(); it != m_backings.end(); + +it) { | 236 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) { |
| 249 if ((*it)->owner()) | 237 if ((*it)->owner()) |
| 250 break; | 238 break; |
| 251 wastedMemory += (*it)->bytes(); | 239 wastedMemory += (*it)->bytes(); |
| 252 } | 240 } |
| 253 size_t tenPercentOfMemory = m_memoryAvailableBytes / 10; | 241 size_t tenPercentOfMemory = m_memoryAvailableBytes / 10; |
| 254 if (wastedMemory > tenPercentOfMemory) | 242 if (wastedMemory > tenPercentOfMemory) |
| 255 evictBackingsToReduceMemory(memoryUseBytes() - (wastedMemory - tenPercen tOfMemory), RespectManagerPriorityCutoff, resourceProvider); | 243 evictBackingsToReduceMemory(memoryUseBytes() - (wastedMemory - tenPercen tOfMemory), RespectManagerPriorityCutoff, resourceProvider); |
| 256 | 244 |
| 257 // Unlink all evicted backings | 245 // Unlink all evicted backings |
| 258 for (BackingVector::const_iterator it = m_evictedBackings.begin(); it != m_e victedBackings.end(); ++it) { | 246 for (BackingList::const_iterator it = m_evictedBackings.begin(); it != m_evi ctedBackings.end(); ++it) { |
| 259 if ((*it)->owner()) | 247 if ((*it)->owner()) |
| 260 (*it)->owner()->unlink(); | 248 (*it)->owner()->unlink(); |
| 261 } | 249 } |
| 262 | 250 |
| 263 // And clear the list of evicted backings | 251 // And clear the list of evicted backings |
| 264 deleteUnlinkedEvictedBackings(); | 252 deleteUnlinkedEvictedBackings(); |
| 265 } | 253 } |
| 266 | 254 |
| 267 void CCPrioritizedTextureManager::clearAllMemory(CCResourceProvider* resourcePro vider) | 255 void CCPrioritizedTextureManager::clearAllMemory(CCResourceProvider* resourcePro vider) |
| 268 { | 256 { |
| 269 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | 257 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); |
| 270 ASSERT(resourceProvider); | 258 ASSERT(resourceProvider); |
| 271 evictBackingsToReduceMemory(0, DoNotRespectManagerPriorityCutoff, resourcePr ovider); | 259 evictBackingsToReduceMemory(0, DoNotRespectManagerPriorityCutoff, resourcePr ovider); |
| 272 } | 260 } |
| 273 | 261 |
| 274 void CCPrioritizedTextureManager::reduceMemoryOnImplThread(size_t limitBytes, CC ResourceProvider* resourceProvider) | 262 void CCPrioritizedTextureManager::reduceMemoryOnImplThread(size_t limitBytes, CC ResourceProvider* resourceProvider) |
| 275 { | 263 { |
| 276 ASSERT(CCProxy::isImplThread()); | 264 ASSERT(CCProxy::isImplThread()); |
| 277 ASSERT(resourceProvider); | 265 ASSERT(resourceProvider); |
| 266 // If we are in the process of uploading a new frame then the backings at th e very end of | |
| 267 // the list are not sorted by priority. Sort them before doing the eviction. | |
| 268 sortBackings(); | |
|
jamesr
2012/10/17 02:20:59
This will be unnecessary a lot of the time, won't
| |
| 278 evictBackingsToReduceMemory(limitBytes, DoNotRespectManagerPriorityCutoff, r esourceProvider); | 269 evictBackingsToReduceMemory(limitBytes, DoNotRespectManagerPriorityCutoff, r esourceProvider); |
| 279 } | 270 } |
| 280 | 271 |
| 281 void CCPrioritizedTextureManager::getEvictedBackings(BackingVector& evictedBacki ngs) | 272 void CCPrioritizedTextureManager::getEvictedBackings(BackingList& evictedBacking s) |
| 282 { | 273 { |
| 283 ASSERT(CCProxy::isImplThread()); | 274 ASSERT(CCProxy::isImplThread()); |
| 284 evictedBackings.clear(); | 275 evictedBackings.clear(); |
| 285 evictedBackings.append(m_evictedBackings); | 276 evictedBackings.insert(evictedBackings.begin(), m_evictedBackings.begin(), m _evictedBackings.end()); |
| 286 } | 277 } |
| 287 | 278 |
| 288 void CCPrioritizedTextureManager::unlinkEvictedBackings(const BackingVector& evi ctedBackings) | 279 void CCPrioritizedTextureManager::unlinkEvictedBackings(const BackingList& evict edBackings) |
| 289 { | 280 { |
| 290 ASSERT(CCProxy::isMainThread()); | 281 ASSERT(CCProxy::isMainThread()); |
| 291 for (BackingVector::const_iterator it = evictedBackings.begin(); it != evict edBackings.end(); ++it) { | 282 for (BackingList::const_iterator it = evictedBackings.begin(); it != evicted Backings.end(); ++it) { |
| 292 CCPrioritizedTexture::Backing* backing = (*it); | 283 CCPrioritizedTexture::Backing* backing = (*it); |
| 293 if (backing->owner()) | 284 if (backing->owner()) |
| 294 backing->owner()->unlink(); | 285 backing->owner()->unlink(); |
| 295 } | 286 } |
| 296 } | 287 } |
| 297 | 288 |
| 298 void CCPrioritizedTextureManager::deleteUnlinkedEvictedBackings() | 289 void CCPrioritizedTextureManager::deleteUnlinkedEvictedBackings() |
| 299 { | 290 { |
| 300 ASSERT(CCProxy::isMainThread() || (CCProxy::isImplThread() && CCProxy::isMai nThreadBlocked())); | 291 ASSERT(CCProxy::isMainThread() || (CCProxy::isImplThread() && CCProxy::isMai nThreadBlocked())); |
| 301 BackingVector newEvictedBackings; | 292 BackingList newEvictedBackings; |
| 302 for (BackingVector::const_iterator it = m_evictedBackings.begin(); it != m_e victedBackings.end(); ++it) { | 293 for (BackingList::const_iterator it = m_evictedBackings.begin(); it != m_evi ctedBackings.end(); ++it) { |
| 303 CCPrioritizedTexture::Backing* backing = (*it); | 294 CCPrioritizedTexture::Backing* backing = (*it); |
| 304 if (backing->owner()) | 295 if (backing->owner()) |
| 305 newEvictedBackings.append(backing); | 296 newEvictedBackings.push_back(backing); |
| 306 else | 297 else |
| 307 delete backing; | 298 delete backing; |
| 308 } | 299 } |
| 309 m_evictedBackings.swap(newEvictedBackings); | 300 m_evictedBackings.swap(newEvictedBackings); |
| 310 } | 301 } |
| 311 | 302 |
| 312 bool CCPrioritizedTextureManager::linkedEvictedBackingsExist() const | 303 bool CCPrioritizedTextureManager::linkedEvictedBackingsExist() const |
| 313 { | 304 { |
| 314 for (BackingVector::const_iterator it = m_evictedBackings.begin(); it != m_e victedBackings.end(); ++it) { | 305 for (BackingList::const_iterator it = m_evictedBackings.begin(); it != m_evi ctedBackings.end(); ++it) { |
| 315 if ((*it)->owner()) | 306 if ((*it)->owner()) |
| 316 return true; | 307 return true; |
| 317 } | 308 } |
| 318 return false; | 309 return false; |
| 319 } | 310 } |
| 320 | 311 |
| 321 void CCPrioritizedTextureManager::registerTexture(CCPrioritizedTexture* texture) | 312 void CCPrioritizedTextureManager::registerTexture(CCPrioritizedTexture* texture) |
| 322 { | 313 { |
| 323 ASSERT(CCProxy::isMainThread()); | 314 ASSERT(CCProxy::isMainThread()); |
| 324 ASSERT(texture); | 315 ASSERT(texture); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 350 texture->unlink(); | 341 texture->unlink(); |
| 351 } | 342 } |
| 352 | 343 |
| 353 CCPrioritizedTexture::Backing* CCPrioritizedTextureManager::createBacking(IntSiz e size, GC3Denum format, CCResourceProvider* resourceProvider) | 344 CCPrioritizedTexture::Backing* CCPrioritizedTextureManager::createBacking(IntSiz e size, GC3Denum format, CCResourceProvider* resourceProvider) |
| 354 { | 345 { |
| 355 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | 346 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); |
| 356 ASSERT(resourceProvider); | 347 ASSERT(resourceProvider); |
| 357 CCResourceProvider::ResourceId resourceId = resourceProvider->createResource (m_pool, size, format, CCResourceProvider::TextureUsageAny); | 348 CCResourceProvider::ResourceId resourceId = resourceProvider->createResource (m_pool, size, format, CCResourceProvider::TextureUsageAny); |
| 358 CCPrioritizedTexture::Backing* backing = new CCPrioritizedTexture::Backing(r esourceId, resourceProvider, size, format); | 349 CCPrioritizedTexture::Backing* backing = new CCPrioritizedTexture::Backing(r esourceId, resourceProvider, size, format); |
| 359 m_memoryUseBytes += backing->bytes(); | 350 m_memoryUseBytes += backing->bytes(); |
| 360 // Put backing texture at the front for eviction, since it isn't in use yet. | |
| 361 m_backings.insertBefore(m_backings.begin(), backing); | |
| 362 return backing; | 351 return backing; |
| 363 } | 352 } |
| 364 | 353 |
| 365 void CCPrioritizedTextureManager::evictBackingResource(CCPrioritizedTexture::Bac king* backing, CCResourceProvider* resourceProvider) | 354 void CCPrioritizedTextureManager::evictFirstBackingResource(CCResourceProvider* resourceProvider) |
| 366 { | 355 { |
| 367 ASSERT(CCProxy::isImplThread()); | 356 ASSERT(CCProxy::isImplThread()); |
| 368 ASSERT(backing); | |
| 369 ASSERT(resourceProvider); | 357 ASSERT(resourceProvider); |
| 370 ASSERT(m_backings.find(backing) != m_backings.end()); | 358 ASSERT(!m_backings.empty()); |
| 359 CCPrioritizedTexture::Backing* backing = m_backings.front(); | |
| 371 | 360 |
| 372 // Note that we create a backing and its resource at the same time, but we | 361 // Note that we create a backing and its resource at the same time, but we |
| 373 // delete the backing structure and its resource in two steps. This is becau se | 362 // delete the backing structure and its resource in two steps. This is becau se |
| 374 // we can delete the resource while the main thread is running, but we canno t | 363 // we can delete the resource while the main thread is running, but we canno t |
| 375 // unlink backings while the main thread is running. | 364 // unlink backings while the main thread is running. |
| 376 backing->deleteResource(resourceProvider); | 365 backing->deleteResource(resourceProvider); |
| 377 m_memoryUseBytes -= backing->bytes(); | 366 m_memoryUseBytes -= backing->bytes(); |
| 378 m_backings.remove(backing); | 367 m_backings.pop_front(); |
| 379 m_evictedBackings.append(backing); | 368 m_evictedBackings.push_back(backing); |
| 380 } | 369 } |
| 381 | 370 |
| 382 #if !ASSERT_DISABLED | |
| 383 void CCPrioritizedTextureManager::assertInvariants() | 371 void CCPrioritizedTextureManager::assertInvariants() |
| 384 { | 372 { |
| 373 #if !ASSERT_DISABLED | |
| 385 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | 374 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); |
| 386 | 375 |
| 387 // If we hit any of these asserts, there is a bug in this class. To see | 376 // If we hit any of these asserts, there is a bug in this class. To see |
| 388 // where the bug is, call this function at the beginning and end of | 377 // where the bug is, call this function at the beginning and end of |
| 389 // every public function. | 378 // every public function. |
| 390 | 379 |
| 391 // Backings/textures must be doubly-linked and only to other backings/textur es in this manager. | 380 // Backings/textures must be doubly-linked and only to other backings/textur es in this manager. |
| 392 for (BackingSet::iterator it = m_backings.begin(); it != m_backings.end(); + +it) { | 381 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) { |
| 393 if ((*it)->owner()) { | 382 if ((*it)->owner()) { |
| 394 ASSERT(ContainsKey(m_textures, (*it)->owner())); | 383 ASSERT(ContainsKey(m_textures, (*it)->owner())); |
| 395 ASSERT((*it)->owner()->backing() == (*it)); | 384 ASSERT((*it)->owner()->backing() == (*it)); |
| 396 } | 385 } |
| 397 } | 386 } |
| 398 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); + +it) { | 387 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); + +it) { |
| 399 CCPrioritizedTexture* texture = (*it); | 388 CCPrioritizedTexture* texture = (*it); |
| 400 CCPrioritizedTexture::Backing* backing = texture->backing(); | 389 CCPrioritizedTexture::Backing* backing = texture->backing(); |
| 401 if (backing) { | 390 if (backing) { |
| 402 if (backing->resourceHasBeenDeleted()) { | 391 if (backing->resourceHasBeenDeleted()) { |
| 403 ASSERT(m_backings.find(backing) == m_backings.end()); | 392 ASSERT(std::find(m_backings.begin(), m_backings.end(), backing) == m_backings.end()); |
| 404 ASSERT(m_evictedBackings.contains(backing)); | 393 ASSERT(std::find(m_evictedBackings.begin(), m_evictedBackings.en d(), backing) != m_evictedBackings.end()); |
| 405 } else { | 394 } else { |
| 406 ASSERT(m_backings.find(backing) != m_backings.end()); | 395 ASSERT(std::find(m_backings.begin(), m_backings.end(), backing) != m_backings.end()); |
| 407 ASSERT(!m_evictedBackings.contains(backing)); | 396 ASSERT(std::find(m_evictedBackings.begin(), m_evictedBackings.en d(), backing) == m_evictedBackings.end()); |
| 408 } | 397 } |
| 409 ASSERT(backing->owner() == texture); | 398 ASSERT(backing->owner() == texture); |
| 410 } | 399 } |
| 411 } | 400 } |
| 412 | 401 |
| 413 // At all times, backings that can be evicted must always come before | 402 // At all times, backings that can be evicted must always come before |
| 414 // backings that can't be evicted in the backing texture list (otherwise | 403 // backings that can't be evicted in the backing texture list (otherwise |
| 415 // reduceMemory will not find all textures available for eviction/recycling) . | 404 // reduceMemory will not find all textures available for eviction/recycling) . |
| 416 bool reachedUnrecyclable = false; | 405 bool reachedUnrecyclable = false; |
| 417 for (BackingSet::iterator it = m_backings.begin(); it != m_backings.end(); + +it) { | 406 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end(); ++it) { |
| 418 if (!(*it)->canBeRecycled()) | 407 if (!(*it)->canBeRecycled()) |
| 419 reachedUnrecyclable = true; | 408 reachedUnrecyclable = true; |
| 420 if (reachedUnrecyclable) | 409 if (reachedUnrecyclable) |
| 421 ASSERT(!(*it)->canBeRecycled()); | 410 ASSERT(!(*it)->canBeRecycled()); |
| 422 else | 411 else |
| 423 ASSERT((*it)->canBeRecycled()); | 412 ASSERT((*it)->canBeRecycled()); |
| 424 } | 413 } |
| 414 #endif | |
| 425 } | 415 } |
| 426 #endif | |
| 427 | |
| 428 | 416 |
| 429 } // namespace cc | 417 } // namespace cc |
| OLD | NEW |