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