OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "config.h" | |
6 | |
7 #include "CCPrioritizedTextureManager.h" | |
8 | |
9 #include "base/stl_util.h" | |
10 #include "CCPrioritizedTexture.h" | |
11 #include "CCPriorityCalculator.h" | |
12 #include "CCProxy.h" | |
13 #include "TraceEvent.h" | |
14 #include <algorithm> | |
15 | |
16 using namespace std; | |
17 | |
18 namespace cc { | |
19 | |
20 CCPrioritizedTextureManager::CCPrioritizedTextureManager(size_t maxMemoryLimitBy
tes, int, int pool) | |
21 : m_maxMemoryLimitBytes(maxMemoryLimitBytes) | |
22 , m_memoryUseBytes(0) | |
23 , m_memoryAboveCutoffBytes(0) | |
24 , m_memoryAvailableBytes(0) | |
25 , m_pool(pool) | |
26 , m_needsUpdateBackingsPrioritites(false) | |
27 { | |
28 } | |
29 | |
30 CCPrioritizedTextureManager::~CCPrioritizedTextureManager() | |
31 { | |
32 while (m_textures.size() > 0) | |
33 unregisterTexture(*m_textures.begin()); | |
34 | |
35 deleteEvictedBackings(); | |
36 | |
37 // Each remaining backing is a leaked opengl texture. There should be none. | |
38 ASSERT(m_backings.isEmpty()); | |
39 } | |
40 | |
41 void CCPrioritizedTextureManager::prioritizeTextures() | |
42 { | |
43 TRACE_EVENT0("cc", "CCPrioritizedTextureManager::prioritizeTextures"); | |
44 ASSERT(CCProxy::isMainThread()); | |
45 | |
46 // Sorting textures in this function could be replaced by a slightly | |
47 // modified O(n) quick-select to partition textures rather than | |
48 // sort them (if performance of the sort becomes an issue). | |
49 | |
50 TextureVector& sortedTextures = m_tempTextureVector; | |
51 sortedTextures.clear(); | |
52 | |
53 // Copy all textures into a vector and sort them. | |
54 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); +
+it) | |
55 sortedTextures.append(*it); | |
56 std::sort(sortedTextures.begin(), sortedTextures.end(), compareTextures); | |
57 | |
58 m_memoryAvailableBytes = m_maxMemoryLimitBytes; | |
59 m_priorityCutoff = CCPriorityCalculator::lowestPriority(); | |
60 size_t memoryBytes = 0; | |
61 for (TextureVector::iterator it = sortedTextures.begin(); it != sortedTextur
es.end(); ++it) { | |
62 if ((*it)->requestPriority() == CCPriorityCalculator::lowestPriority()) | |
63 break; | |
64 | |
65 if ((*it)->isSelfManaged()) { | |
66 // Account for self-managed memory immediately by reducing the memor
y | |
67 // available (since it never gets acquired). | |
68 size_t newMemoryBytes = memoryBytes + (*it)->bytes(); | |
69 if (newMemoryBytes > m_memoryAvailableBytes) { | |
70 m_priorityCutoff = (*it)->requestPriority(); | |
71 m_memoryAvailableBytes = memoryBytes; | |
72 break; | |
73 } | |
74 m_memoryAvailableBytes -= (*it)->bytes(); | |
75 } else { | |
76 size_t newMemoryBytes = memoryBytes + (*it)->bytes(); | |
77 if (newMemoryBytes > m_memoryAvailableBytes) { | |
78 m_priorityCutoff = (*it)->requestPriority(); | |
79 break; | |
80 } | |
81 memoryBytes = newMemoryBytes; | |
82 } | |
83 } | |
84 | |
85 // Only allow textures if they are higher than the cutoff. All textures | |
86 // of the same priority are accepted or rejected together, rather than | |
87 // being partially allowed randomly. | |
88 m_memoryAboveCutoffBytes = 0; | |
89 for (TextureVector::iterator it = sortedTextures.begin(); it != sortedTextur
es.end(); ++it) { | |
90 bool isAbovePriorityCutoff = CCPriorityCalculator::priorityIsHigher((*it
)->requestPriority(), m_priorityCutoff); | |
91 (*it)->setAbovePriorityCutoff(isAbovePriorityCutoff); | |
92 if (isAbovePriorityCutoff && !(*it)->isSelfManaged()) | |
93 m_memoryAboveCutoffBytes += (*it)->bytes(); | |
94 } | |
95 sortedTextures.clear(); | |
96 | |
97 m_needsUpdateBackingsPrioritites = true; | |
98 | |
99 ASSERT(m_memoryAboveCutoffBytes <= m_memoryAvailableBytes); | |
100 ASSERT(memoryAboveCutoffBytes() <= maxMemoryLimitBytes()); | |
101 } | |
102 | |
103 void CCPrioritizedTextureManager::updateBackingsPriorities() | |
104 { | |
105 TRACE_EVENT0("cc", "CCPrioritizedTextureManager::updateBackingsPriorities"); | |
106 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | |
107 | |
108 if (!m_needsUpdateBackingsPrioritites) | |
109 return; | |
110 | |
111 #if !ASSERT_DISABLED | |
112 assertInvariants(); | |
113 #endif | |
114 | |
115 // Update backings' priorities and put backings in eviction/recycling order. | |
116 BackingVector& sortedBackings = m_tempBackingVector; | |
117 sortedBackings.clear(); | |
118 for (BackingSet::iterator it = m_backings.begin(); it != m_backings.end(); +
+it) { | |
119 (*it)->updatePriority(); | |
120 sortedBackings.append(*it); | |
121 } | |
122 std::sort(sortedBackings.begin(), sortedBackings.end(), compareBackings); | |
123 | |
124 for (BackingVector::iterator it = sortedBackings.begin(); it != sortedBackin
gs.end(); ++it) { | |
125 m_backings.remove(*it); | |
126 m_backings.add(*it); | |
127 } | |
128 sortedBackings.clear(); | |
129 m_needsUpdateBackingsPrioritites = false; | |
130 | |
131 #if !ASSERT_DISABLED | |
132 assertInvariants(); | |
133 #endif | |
134 } | |
135 | |
136 void CCPrioritizedTextureManager::clearPriorities() | |
137 { | |
138 ASSERT(CCProxy::isMainThread()); | |
139 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); +
+it) { | |
140 // FIXME: We should remove this and just set all priorities to | |
141 // CCPriorityCalculator::lowestPriority() once we have priorities | |
142 // for all textures (we can't currently calculate distances for | |
143 // off-screen textures). | |
144 (*it)->setRequestPriority(CCPriorityCalculator::lingeringPriority((*it)-
>requestPriority())); | |
145 } | |
146 } | |
147 | |
148 bool CCPrioritizedTextureManager::requestLate(CCPrioritizedTexture* texture) | |
149 { | |
150 ASSERT(CCProxy::isMainThread()); | |
151 | |
152 // This is already above cutoff, so don't double count it's memory below. | |
153 if (texture->isAbovePriorityCutoff()) | |
154 return true; | |
155 | |
156 if (CCPriorityCalculator::priorityIsLower(texture->requestPriority(), m_prio
rityCutoff)) | |
157 return false; | |
158 | |
159 size_t newMemoryBytes = m_memoryAboveCutoffBytes + texture->bytes(); | |
160 if (newMemoryBytes > m_memoryAvailableBytes) | |
161 return false; | |
162 | |
163 m_memoryAboveCutoffBytes = newMemoryBytes; | |
164 texture->setAbovePriorityCutoff(true); | |
165 m_needsUpdateBackingsPrioritites = true; | |
166 return true; | |
167 } | |
168 | |
169 void CCPrioritizedTextureManager::acquireBackingTextureIfNeeded(CCPrioritizedTex
ture* texture, CCResourceProvider* resourceProvider) | |
170 { | |
171 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | |
172 ASSERT(!texture->isSelfManaged()); | |
173 ASSERT(texture->isAbovePriorityCutoff()); | |
174 if (texture->backing() || !texture->isAbovePriorityCutoff()) | |
175 return; | |
176 | |
177 // Make sure that the backings list is up to date and sorted before traversi
ng it. | |
178 updateBackingsPriorities(); | |
179 | |
180 // Find a backing below, by either recycling or allocating. | |
181 CCPrioritizedTexture::Backing* backing = 0; | |
182 | |
183 // First try to recycle | |
184 for (BackingSet::iterator it = m_backings.begin(); it != m_backings.end(); +
+it) { | |
185 if ((*it)->hadOwnerAtLastPriorityUpdate() && (*it)->wasAbovePriorityCuto
ffAtLastPriorityUpdate()) | |
186 break; | |
187 if ((*it)->size() == texture->size() && (*it)->format() == texture->form
at()) { | |
188 backing = (*it); | |
189 break; | |
190 } | |
191 } | |
192 | |
193 // Otherwise reduce memory and just allocate a new backing texures. | |
194 if (!backing) { | |
195 evictBackingsToReduceMemory(m_memoryAvailableBytes - texture->bytes(), R
espectManagerPriorityCutoff, resourceProvider); | |
196 backing = createBacking(texture->size(), texture->format(), resourceProv
ider); | |
197 } | |
198 | |
199 // Move the used backing texture to the end of the eviction list. | |
200 if (backing->owner()) | |
201 backing->owner()->unlink(); | |
202 texture->link(backing); | |
203 m_backings.remove(backing); | |
204 m_backings.add(backing); | |
205 | |
206 // Update the backing's priority from its new owner. | |
207 backing->updatePriority(); | |
208 } | |
209 | |
210 void CCPrioritizedTextureManager::evictBackingsToReduceMemory(size_t limitBytes,
EvictionPriorityPolicy evictionPolicy, CCResourceProvider* resourceProvider) | |
211 { | |
212 ASSERT(CCProxy::isImplThread()); | |
213 if (memoryUseBytes() <= limitBytes) | |
214 return; | |
215 | |
216 // Destroy backings until we are below the limit, | |
217 // or until all backings remaining are above the cutoff. | |
218 while (memoryUseBytes() > limitBytes && m_backings.size() > 0) { | |
219 CCPrioritizedTexture::Backing* backing = *m_backings.begin(); | |
220 if (evictionPolicy == RespectManagerPriorityCutoff) | |
221 if (backing->hadOwnerAtLastPriorityUpdate() && backing->wasAbovePrio
rityCutoffAtLastPriorityUpdate()) | |
222 break; | |
223 evictBackingResource(backing, resourceProvider); | |
224 } | |
225 } | |
226 | |
227 void CCPrioritizedTextureManager::reduceMemory(CCResourceProvider* resourceProvi
der) | |
228 { | |
229 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | |
230 | |
231 // Make sure that the backings list is up to date and sorted before traversi
ng it. | |
232 updateBackingsPriorities(); | |
233 | |
234 evictBackingsToReduceMemory(m_memoryAvailableBytes, RespectManagerPriorityCu
toff, resourceProvider); | |
235 ASSERT(memoryUseBytes() <= maxMemoryLimitBytes()); | |
236 | |
237 // We currently collect backings from deleted textures for later recycling. | |
238 // However, if we do that forever we will always use the max limit even if | |
239 // we really need very little memory. This should probably be solved by redu
cing the | |
240 // limit externally, but until then this just does some "clean up" of unused | |
241 // backing textures (any more than 10%). | |
242 size_t wastedMemory = 0; | |
243 for (BackingSet::iterator it = m_backings.begin(); it != m_backings.end(); +
+it) { | |
244 if ((*it)->owner()) | |
245 break; | |
246 wastedMemory += (*it)->bytes(); | |
247 } | |
248 size_t tenPercentOfMemory = m_memoryAvailableBytes / 10; | |
249 if (wastedMemory > tenPercentOfMemory) | |
250 evictBackingsToReduceMemory(memoryUseBytes() - (wastedMemory - tenPercen
tOfMemory), RespectManagerPriorityCutoff, resourceProvider); | |
251 | |
252 deleteEvictedBackings(); | |
253 } | |
254 | |
255 void CCPrioritizedTextureManager::clearAllMemory(CCResourceProvider* resourcePro
vider) | |
256 { | |
257 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | |
258 ASSERT(resourceProvider); | |
259 evictBackingsToReduceMemory(0, DoNotRespectManagerPriorityCutoff, resourcePr
ovider); | |
260 deleteEvictedBackings(); | |
261 } | |
262 | |
263 void CCPrioritizedTextureManager::reduceMemoryOnImplThread(size_t limitBytes, CC
ResourceProvider* resourceProvider) | |
264 { | |
265 ASSERT(CCProxy::isImplThread()); | |
266 ASSERT(resourceProvider); | |
267 evictBackingsToReduceMemory(limitBytes, DoNotRespectManagerPriorityCutoff, r
esourceProvider); | |
268 } | |
269 | |
270 void CCPrioritizedTextureManager::getEvictedBackings(BackingVector& evictedBacki
ngs) | |
271 { | |
272 ASSERT(CCProxy::isImplThread()); | |
273 evictedBackings.clear(); | |
274 evictedBackings.append(m_evictedBackings); | |
275 } | |
276 | |
277 void CCPrioritizedTextureManager::unlinkEvictedBackings(const BackingVector& evi
ctedBackings) | |
278 { | |
279 ASSERT(CCProxy::isMainThread()); | |
280 for (BackingVector::const_iterator it = evictedBackings.begin(); it != evict
edBackings.end(); ++it) { | |
281 CCPrioritizedTexture::Backing* backing = (*it); | |
282 if (backing->owner()) | |
283 backing->owner()->unlink(); | |
284 } | |
285 } | |
286 | |
287 bool CCPrioritizedTextureManager::deleteEvictedBackings() | |
288 { | |
289 ASSERT(CCProxy::isMainThread() || (CCProxy::isImplThread() && CCProxy::isMai
nThreadBlocked())); | |
290 bool linkedEvictedBackingsExisted = false; | |
291 for (BackingVector::const_iterator it = m_evictedBackings.begin(); it != m_e
victedBackings.end(); ++it) { | |
292 CCPrioritizedTexture::Backing* backing = (*it); | |
293 if (backing->owner()) { | |
294 linkedEvictedBackingsExisted = true; | |
295 backing->owner()->unlink(); | |
296 } | |
297 delete backing; | |
298 } | |
299 m_evictedBackings.clear(); | |
300 return linkedEvictedBackingsExisted; | |
301 } | |
302 | |
303 void CCPrioritizedTextureManager::registerTexture(CCPrioritizedTexture* texture) | |
304 { | |
305 ASSERT(CCProxy::isMainThread()); | |
306 ASSERT(texture); | |
307 ASSERT(!texture->textureManager()); | |
308 ASSERT(!texture->backing()); | |
309 ASSERT(!ContainsKey(m_textures, texture)); | |
310 | |
311 texture->setManagerInternal(this); | |
312 m_textures.insert(texture); | |
313 | |
314 } | |
315 | |
316 void CCPrioritizedTextureManager::unregisterTexture(CCPrioritizedTexture* textur
e) | |
317 { | |
318 ASSERT(CCProxy::isMainThread() || (CCProxy::isImplThread() && CCProxy::isMai
nThreadBlocked())); | |
319 ASSERT(texture); | |
320 ASSERT(ContainsKey(m_textures, texture)); | |
321 | |
322 returnBackingTexture(texture); | |
323 texture->setManagerInternal(0); | |
324 m_textures.erase(texture); | |
325 texture->setAbovePriorityCutoff(false); | |
326 } | |
327 | |
328 void CCPrioritizedTextureManager::returnBackingTexture(CCPrioritizedTexture* tex
ture) | |
329 { | |
330 ASSERT(CCProxy::isMainThread() || (CCProxy::isImplThread() && CCProxy::isMai
nThreadBlocked())); | |
331 if (texture->backing()) { | |
332 texture->unlink(); | |
333 m_needsUpdateBackingsPrioritites = true; | |
334 } | |
335 } | |
336 | |
337 CCPrioritizedTexture::Backing* CCPrioritizedTextureManager::createBacking(IntSiz
e size, GC3Denum format, CCResourceProvider* resourceProvider) | |
338 { | |
339 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | |
340 ASSERT(resourceProvider); | |
341 CCResourceProvider::ResourceId resourceId = resourceProvider->createResource
(m_pool, size, format, CCResourceProvider::TextureUsageAny); | |
342 CCPrioritizedTexture::Backing* backing = new CCPrioritizedTexture::Backing(r
esourceId, resourceProvider, size, format); | |
343 m_memoryUseBytes += backing->bytes(); | |
344 // Put backing texture at the front for eviction, since it isn't in use yet. | |
345 m_backings.insertBefore(m_backings.begin(), backing); | |
346 return backing; | |
347 } | |
348 | |
349 void CCPrioritizedTextureManager::evictBackingResource(CCPrioritizedTexture::Bac
king* backing, CCResourceProvider* resourceProvider) | |
350 { | |
351 ASSERT(CCProxy::isImplThread()); | |
352 ASSERT(backing); | |
353 ASSERT(resourceProvider); | |
354 ASSERT(m_backings.find(backing) != m_backings.end()); | |
355 | |
356 // Note that we create a backing and its resource at the same time, but we | |
357 // delete the backing structure and its resource in two steps. This is becau
se | |
358 // we can delete the resource while the main thread is running, but we canno
t | |
359 // unlink backings while the main thread is running. | |
360 backing->deleteResource(resourceProvider); | |
361 m_memoryUseBytes -= backing->bytes(); | |
362 m_backings.remove(backing); | |
363 m_evictedBackings.append(backing); | |
364 } | |
365 | |
366 #if !ASSERT_DISABLED | |
367 void CCPrioritizedTextureManager::assertInvariants() | |
368 { | |
369 ASSERT(CCProxy::isImplThread() && CCProxy::isMainThreadBlocked()); | |
370 | |
371 // If we hit any of these asserts, there is a bug in this class. To see | |
372 // where the bug is, call this function at the beginning and end of | |
373 // every public function. | |
374 | |
375 // Backings/textures must be doubly-linked and only to other backings/textur
es in this manager. | |
376 for (BackingSet::iterator it = m_backings.begin(); it != m_backings.end(); +
+it) { | |
377 if ((*it)->owner()) { | |
378 ASSERT(ContainsKey(m_textures, (*it)->owner())); | |
379 ASSERT((*it)->owner()->backing() == (*it)); | |
380 } | |
381 } | |
382 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); +
+it) { | |
383 if ((*it)->backing()) { | |
384 ASSERT(m_backings.find((*it)->backing()) != m_backings.end()); | |
385 ASSERT((*it)->backing()->owner() == (*it)); | |
386 } | |
387 } | |
388 | |
389 // At all times, backings that can be evicted must always come before | |
390 // backings that can't be evicted in the backing texture list (otherwise | |
391 // reduceMemory will not find all textures available for eviction/recycling)
. | |
392 bool reachedOwned = false; | |
393 bool reachedAboveCutoff = false; | |
394 for (BackingSet::iterator it = m_backings.begin(); it != m_backings.end(); +
+it) { | |
395 if ((*it)->hadOwnerAtLastPriorityUpdate()) | |
396 reachedOwned = true; | |
397 if ((*it)->wasAbovePriorityCutoffAtLastPriorityUpdate()) | |
398 reachedAboveCutoff = true; | |
399 if (reachedOwned) | |
400 ASSERT((*it)->hadOwnerAtLastPriorityUpdate()); | |
401 if (reachedAboveCutoff) { | |
402 ASSERT((*it)->hadOwnerAtLastPriorityUpdate() && (*it)->wasAbovePrior
ityCutoffAtLastPriorityUpdate()); | |
403 ASSERT(reachedOwned); | |
404 } | |
405 } | |
406 } | |
407 #endif | |
408 | |
409 | |
410 } // namespace cc | |
OLD | NEW |