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 "cc/prioritized_texture_manager.h" | |
8 | |
9 #include "base/debug/trace_event.h" | |
10 #include "base/stl_util.h" | |
11 #include "cc/prioritized_texture.h" | |
12 #include "cc/priority_calculator.h" | |
13 #include "cc/proxy.h" | |
14 #include <algorithm> | |
15 | |
16 using namespace std; | |
17 | |
18 namespace cc { | |
19 | |
20 PrioritizedTextureManager::PrioritizedTextureManager(size_t maxMemoryLimitBytes,
int, int pool) | |
21 : m_maxMemoryLimitBytes(maxMemoryLimitBytes) | |
22 , m_externalPriorityCutoff(PriorityCalculator::allowEverythingCutoff()) | |
23 , m_memoryUseBytes(0) | |
24 , m_memoryAboveCutoffBytes(0) | |
25 , m_memoryAvailableBytes(0) | |
26 , m_pool(pool) | |
27 , m_backingsTailNotSorted(false) | |
28 , m_memoryVisibleBytes(0) | |
29 , m_memoryVisibleAndNearbyBytes(0) | |
30 , m_memoryVisibleLastPushedBytes(0) | |
31 , m_memoryVisibleAndNearbyLastPushedBytes(0) | |
32 { | |
33 } | |
34 | |
35 PrioritizedTextureManager::~PrioritizedTextureManager() | |
36 { | |
37 while (m_textures.size() > 0) | |
38 unregisterTexture(*m_textures.begin()); | |
39 | |
40 deleteUnlinkedEvictedBackings(); | |
41 DCHECK(m_evictedBackings.empty()); | |
42 | |
43 // Each remaining backing is a leaked opengl texture. There should be none. | |
44 DCHECK(m_backings.empty()); | |
45 } | |
46 | |
47 size_t PrioritizedTextureManager::memoryVisibleBytes() const | |
48 { | |
49 DCHECK(Proxy::isImplThread()); | |
50 return m_memoryVisibleLastPushedBytes; | |
51 } | |
52 | |
53 size_t PrioritizedTextureManager::memoryVisibleAndNearbyBytes() const | |
54 { | |
55 DCHECK(Proxy::isImplThread()); | |
56 return m_memoryVisibleAndNearbyLastPushedBytes; | |
57 } | |
58 | |
59 void PrioritizedTextureManager::prioritizeTextures() | |
60 { | |
61 TRACE_EVENT0("cc", "PrioritizedTextureManager::prioritizeTextures"); | |
62 DCHECK(Proxy::isMainThread()); | |
63 | |
64 // Sorting textures in this function could be replaced by a slightly | |
65 // modified O(n) quick-select to partition textures rather than | |
66 // sort them (if performance of the sort becomes an issue). | |
67 | |
68 TextureVector& sortedTextures = m_tempTextureVector; | |
69 sortedTextures.clear(); | |
70 | |
71 // Copy all textures into a vector, sort them, and collect memory requiremen
ts statistics. | |
72 m_memoryVisibleBytes = 0; | |
73 m_memoryVisibleAndNearbyBytes = 0; | |
74 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); +
+it) { | |
75 PrioritizedTexture* texture = (*it); | |
76 sortedTextures.push_back(texture); | |
77 if (PriorityCalculator::priorityIsHigher(texture->requestPriority(), Pri
orityCalculator::allowVisibleOnlyCutoff())) | |
78 m_memoryVisibleBytes += texture->bytes(); | |
79 if (PriorityCalculator::priorityIsHigher(texture->requestPriority(), Pri
orityCalculator::allowVisibleAndNearbyCutoff())) | |
80 m_memoryVisibleAndNearbyBytes += texture->bytes(); | |
81 } | |
82 std::sort(sortedTextures.begin(), sortedTextures.end(), compareTextures); | |
83 | |
84 // Compute a priority cutoff based on memory pressure | |
85 m_memoryAvailableBytes = m_maxMemoryLimitBytes; | |
86 m_priorityCutoff = m_externalPriorityCutoff; | |
87 size_t memoryBytes = 0; | |
88 for (TextureVector::iterator it = sortedTextures.begin(); it != sortedTextur
es.end(); ++it) { | |
89 if ((*it)->isSelfManaged()) { | |
90 // Account for self-managed memory immediately by reducing the memor
y | |
91 // available (since it never gets acquired). | |
92 size_t newMemoryBytes = memoryBytes + (*it)->bytes(); | |
93 if (newMemoryBytes > m_memoryAvailableBytes) { | |
94 m_priorityCutoff = (*it)->requestPriority(); | |
95 m_memoryAvailableBytes = memoryBytes; | |
96 break; | |
97 } | |
98 m_memoryAvailableBytes -= (*it)->bytes(); | |
99 } else { | |
100 size_t newMemoryBytes = memoryBytes + (*it)->bytes(); | |
101 if (newMemoryBytes > m_memoryAvailableBytes) { | |
102 m_priorityCutoff = (*it)->requestPriority(); | |
103 break; | |
104 } | |
105 memoryBytes = newMemoryBytes; | |
106 } | |
107 } | |
108 | |
109 // Disallow any textures with priority below the external cutoff to have bac
kings. | |
110 size_t memoryLinkedTexturesBytes = 0; | |
111 for (TextureVector::iterator it = sortedTextures.begin(); it != sortedTextur
es.end(); ++it) { | |
112 PrioritizedTexture* texture = (*it); | |
113 if (!PriorityCalculator::priorityIsHigher(texture->requestPriority(), m_
externalPriorityCutoff) && | |
114 texture->haveBackingTexture()) | |
115 texture->unlink(); | |
116 } | |
117 DCHECK(memoryLinkedTexturesBytes <= m_memoryAvailableBytes); | |
118 | |
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 | |
121 // being partially allowed randomly. | |
122 m_memoryAboveCutoffBytes = 0; | |
123 for (TextureVector::iterator it = sortedTextures.begin(); it != sortedTextur
es.end(); ++it) { | |
124 bool isAbovePriorityCutoff = PriorityCalculator::priorityIsHigher((*it)-
>requestPriority(), m_priorityCutoff); | |
125 (*it)->setAbovePriorityCutoff(isAbovePriorityCutoff); | |
126 if (isAbovePriorityCutoff && !(*it)->isSelfManaged()) | |
127 m_memoryAboveCutoffBytes += (*it)->bytes(); | |
128 } | |
129 sortedTextures.clear(); | |
130 | |
131 DCHECK(m_memoryAboveCutoffBytes <= m_memoryAvailableBytes); | |
132 DCHECK(memoryAboveCutoffBytes() <= maxMemoryLimitBytes()); | |
133 } | |
134 | |
135 void PrioritizedTextureManager::pushTexturePrioritiesToBackings() | |
136 { | |
137 TRACE_EVENT0("cc", "PrioritizedTextureManager::pushTexturePrioritiesToBackin
gs"); | |
138 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); | |
139 | |
140 assertInvariants(); | |
141 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end();
++it) | |
142 (*it)->updatePriority(); | |
143 sortBackings(); | |
144 assertInvariants(); | |
145 | |
146 // Push memory requirements to the impl thread structure. | |
147 m_memoryVisibleLastPushedBytes = m_memoryVisibleBytes; | |
148 m_memoryVisibleAndNearbyLastPushedBytes = m_memoryVisibleAndNearbyBytes; | |
149 } | |
150 | |
151 void PrioritizedTextureManager::updateBackingsInDrawingImplTree() | |
152 { | |
153 TRACE_EVENT0("cc", "PrioritizedTextureManager::updateBackingsInDrawingImplTr
ee"); | |
154 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); | |
155 | |
156 assertInvariants(); | |
157 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end();
++it) { | |
158 PrioritizedTexture::Backing* backing = (*it); | |
159 backing->updateInDrawingImplTree(); | |
160 } | |
161 sortBackings(); | |
162 assertInvariants(); | |
163 } | |
164 | |
165 void PrioritizedTextureManager::sortBackings() | |
166 { | |
167 TRACE_EVENT0("cc", "PrioritizedTextureManager::sortBackings"); | |
168 DCHECK(Proxy::isImplThread()); | |
169 | |
170 // Put backings in eviction/recycling order. | |
171 m_backings.sort(compareBackings); | |
172 m_backingsTailNotSorted = false; | |
173 } | |
174 | |
175 void PrioritizedTextureManager::clearPriorities() | |
176 { | |
177 DCHECK(Proxy::isMainThread()); | |
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 | |
180 // PriorityCalculator::lowestPriority() once we have priorities | |
181 // for all textures (we can't currently calculate distances for | |
182 // off-screen textures). | |
183 (*it)->setRequestPriority(PriorityCalculator::lingeringPriority((*it)->r
equestPriority())); | |
184 } | |
185 } | |
186 | |
187 bool PrioritizedTextureManager::requestLate(PrioritizedTexture* texture) | |
188 { | |
189 DCHECK(Proxy::isMainThread()); | |
190 | |
191 // This is already above cutoff, so don't double count it's memory below. | |
192 if (texture->isAbovePriorityCutoff()) | |
193 return true; | |
194 | |
195 // Allow textures that have priority equal to the cutoff, but not strictly l
ower. | |
196 if (PriorityCalculator::priorityIsLower(texture->requestPriority(), m_priori
tyCutoff)) | |
197 return false; | |
198 | |
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)) | |
201 return false; | |
202 | |
203 size_t newMemoryBytes = m_memoryAboveCutoffBytes + texture->bytes(); | |
204 if (newMemoryBytes > m_memoryAvailableBytes) | |
205 return false; | |
206 | |
207 m_memoryAboveCutoffBytes = newMemoryBytes; | |
208 texture->setAbovePriorityCutoff(true); | |
209 return true; | |
210 } | |
211 | |
212 void PrioritizedTextureManager::acquireBackingTextureIfNeeded(PrioritizedTexture
* texture, ResourceProvider* resourceProvider) | |
213 { | |
214 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); | |
215 DCHECK(!texture->isSelfManaged()); | |
216 DCHECK(texture->isAbovePriorityCutoff()); | |
217 if (texture->backing() || !texture->isAbovePriorityCutoff()) | |
218 return; | |
219 | |
220 // Find a backing below, by either recycling or allocating. | |
221 PrioritizedTexture::Backing* backing = 0; | |
222 | |
223 // First try to recycle | |
224 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end();
++it) { | |
225 if (!(*it)->canBeRecycled()) | |
226 break; | |
227 if ((*it)->size() == texture->size() && (*it)->format() == texture->form
at()) { | |
228 backing = (*it); | |
229 m_backings.erase(it); | |
230 break; | |
231 } | |
232 } | |
233 | |
234 // Otherwise reduce memory and just allocate a new backing texures. | |
235 if (!backing) { | |
236 evictBackingsToReduceMemory(m_memoryAvailableBytes - texture->bytes(), P
riorityCalculator::allowEverythingCutoff(), EvictOnlyRecyclable, resourceProvide
r); | |
237 backing = createBacking(texture->size(), texture->format(), resourceProv
ider); | |
238 } | |
239 | |
240 // Move the used backing to the end of the eviction list, and note that | |
241 // the tail is not sorted. | |
242 if (backing->owner()) | |
243 backing->owner()->unlink(); | |
244 texture->link(backing); | |
245 m_backings.push_back(backing); | |
246 m_backingsTailNotSorted = true; | |
247 | |
248 // Update the backing's priority from its new owner. | |
249 backing->updatePriority(); | |
250 } | |
251 | |
252 bool PrioritizedTextureManager::evictBackingsToReduceMemory(size_t limitBytes, i
nt priorityCutoff, EvictionPolicy evictionPolicy, ResourceProvider* resourceProv
ider) | |
253 { | |
254 DCHECK(Proxy::isImplThread()); | |
255 if (memoryUseBytes() <= limitBytes && PriorityCalculator::allowEverythingCut
off() == priorityCutoff) | |
256 return false; | |
257 | |
258 // Destroy backings until we are below the limit, | |
259 // or until all backings remaining are above the cutoff. | |
260 while (m_backings.size() > 0) { | |
261 PrioritizedTexture::Backing* backing = m_backings.front(); | |
262 if (memoryUseBytes() <= limitBytes && | |
263 PriorityCalculator::priorityIsHigher(backing->requestPriorityAtLastP
riorityUpdate(), priorityCutoff)) | |
264 break; | |
265 if (evictionPolicy == EvictOnlyRecyclable && !backing->canBeRecycled()) | |
266 break; | |
267 evictFirstBackingResource(resourceProvider); | |
268 } | |
269 return true; | |
270 } | |
271 | |
272 void PrioritizedTextureManager::reduceMemory(ResourceProvider* resourceProvider) | |
273 { | |
274 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); | |
275 | |
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 | |
278 // get more space. | |
279 | |
280 evictBackingsToReduceMemory(m_memoryAvailableBytes, PriorityCalculator::allo
wEverythingCutoff(), EvictOnlyRecyclable, resourceProvider); | |
281 | |
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 | |
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 | |
286 // backing textures (any more than 10%). | |
287 size_t wastedMemory = 0; | |
288 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end();
++it) { | |
289 if ((*it)->owner()) | |
290 break; | |
291 wastedMemory += (*it)->bytes(); | |
292 } | |
293 size_t tenPercentOfMemory = m_memoryAvailableBytes / 10; | |
294 if (wastedMemory > tenPercentOfMemory) | |
295 evictBackingsToReduceMemory(memoryUseBytes() - (wastedMemory - tenPercen
tOfMemory), PriorityCalculator::allowEverythingCutoff(), EvictOnlyRecyclable, re
sourceProvider); | |
296 | |
297 // Unlink all evicted backings | |
298 for (BackingList::const_iterator it = m_evictedBackings.begin(); it != m_evi
ctedBackings.end(); ++it) { | |
299 if ((*it)->owner()) | |
300 (*it)->owner()->unlink(); | |
301 } | |
302 | |
303 // And clear the list of evicted backings | |
304 deleteUnlinkedEvictedBackings(); | |
305 } | |
306 | |
307 void PrioritizedTextureManager::clearAllMemory(ResourceProvider* resourceProvide
r) | |
308 { | |
309 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); | |
310 DCHECK(resourceProvider); | |
311 evictBackingsToReduceMemory(0, PriorityCalculator::allowEverythingCutoff(),
EvictAnything, resourceProvider); | |
312 } | |
313 | |
314 bool PrioritizedTextureManager::reduceMemoryOnImplThread(size_t limitBytes, int
priorityCutoff, ResourceProvider* resourceProvider) | |
315 { | |
316 DCHECK(Proxy::isImplThread()); | |
317 DCHECK(resourceProvider); | |
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. | |
320 if (m_backingsTailNotSorted) | |
321 sortBackings(); | |
322 return evictBackingsToReduceMemory(limitBytes, priorityCutoff, EvictAnything
, resourceProvider); | |
323 } | |
324 | |
325 void PrioritizedTextureManager::getEvictedBackings(BackingList& evictedBackings) | |
326 { | |
327 DCHECK(Proxy::isImplThread()); | |
328 evictedBackings.clear(); | |
329 evictedBackings.insert(evictedBackings.begin(), m_evictedBackings.begin(), m
_evictedBackings.end()); | |
330 } | |
331 | |
332 void PrioritizedTextureManager::unlinkEvictedBackings(const BackingList& evicted
Backings) | |
333 { | |
334 DCHECK(Proxy::isMainThread()); | |
335 for (BackingList::const_iterator it = evictedBackings.begin(); it != evicted
Backings.end(); ++it) { | |
336 PrioritizedTexture::Backing* backing = (*it); | |
337 if (backing->owner()) | |
338 backing->owner()->unlink(); | |
339 } | |
340 } | |
341 | |
342 void PrioritizedTextureManager::deleteUnlinkedEvictedBackings() | |
343 { | |
344 DCHECK(Proxy::isMainThread() || (Proxy::isImplThread() && Proxy::isMainThrea
dBlocked())); | |
345 BackingList newEvictedBackings; | |
346 for (BackingList::const_iterator it = m_evictedBackings.begin(); it != m_evi
ctedBackings.end(); ++it) { | |
347 PrioritizedTexture::Backing* backing = (*it); | |
348 if (backing->owner()) | |
349 newEvictedBackings.push_back(backing); | |
350 else | |
351 delete backing; | |
352 } | |
353 m_evictedBackings.swap(newEvictedBackings); | |
354 } | |
355 | |
356 bool PrioritizedTextureManager::linkedEvictedBackingsExist() const | |
357 { | |
358 for (BackingList::const_iterator it = m_evictedBackings.begin(); it != m_evi
ctedBackings.end(); ++it) { | |
359 if ((*it)->owner()) | |
360 return true; | |
361 } | |
362 return false; | |
363 } | |
364 | |
365 void PrioritizedTextureManager::registerTexture(PrioritizedTexture* texture) | |
366 { | |
367 DCHECK(Proxy::isMainThread()); | |
368 DCHECK(texture); | |
369 DCHECK(!texture->textureManager()); | |
370 DCHECK(!texture->backing()); | |
371 DCHECK(!ContainsKey(m_textures, texture)); | |
372 | |
373 texture->setManagerInternal(this); | |
374 m_textures.insert(texture); | |
375 | |
376 } | |
377 | |
378 void PrioritizedTextureManager::unregisterTexture(PrioritizedTexture* texture) | |
379 { | |
380 DCHECK(Proxy::isMainThread() || (Proxy::isImplThread() && Proxy::isMainThrea
dBlocked())); | |
381 DCHECK(texture); | |
382 DCHECK(ContainsKey(m_textures, texture)); | |
383 | |
384 returnBackingTexture(texture); | |
385 texture->setManagerInternal(0); | |
386 m_textures.erase(texture); | |
387 texture->setAbovePriorityCutoff(false); | |
388 } | |
389 | |
390 void PrioritizedTextureManager::returnBackingTexture(PrioritizedTexture* texture
) | |
391 { | |
392 DCHECK(Proxy::isMainThread() || (Proxy::isImplThread() && Proxy::isMainThrea
dBlocked())); | |
393 if (texture->backing()) | |
394 texture->unlink(); | |
395 } | |
396 | |
397 PrioritizedTexture::Backing* PrioritizedTextureManager::createBacking(gfx::Size
size, GLenum format, ResourceProvider* resourceProvider) | |
398 { | |
399 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); | |
400 DCHECK(resourceProvider); | |
401 ResourceProvider::ResourceId resourceId = resourceProvider->createResource(m
_pool, size, format, ResourceProvider::TextureUsageAny); | |
402 PrioritizedTexture::Backing* backing = new PrioritizedTexture::Backing(resou
rceId, resourceProvider, size, format); | |
403 m_memoryUseBytes += backing->bytes(); | |
404 return backing; | |
405 } | |
406 | |
407 void PrioritizedTextureManager::evictFirstBackingResource(ResourceProvider* reso
urceProvider) | |
408 { | |
409 DCHECK(Proxy::isImplThread()); | |
410 DCHECK(resourceProvider); | |
411 DCHECK(!m_backings.empty()); | |
412 PrioritizedTexture::Backing* backing = m_backings.front(); | |
413 | |
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 | |
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. | |
418 backing->deleteResource(resourceProvider); | |
419 m_memoryUseBytes -= backing->bytes(); | |
420 m_backings.pop_front(); | |
421 m_evictedBackings.push_back(backing); | |
422 } | |
423 | |
424 void PrioritizedTextureManager::assertInvariants() | |
425 { | |
426 #ifndef NDEBUG | |
427 DCHECK(Proxy::isImplThread() && Proxy::isMainThreadBlocked()); | |
428 | |
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 | |
431 // every public function. | |
432 | |
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) { | |
435 if ((*it)->owner()) { | |
436 DCHECK(ContainsKey(m_textures, (*it)->owner())); | |
437 DCHECK((*it)->owner()->backing() == (*it)); | |
438 } | |
439 } | |
440 for (TextureSet::iterator it = m_textures.begin(); it != m_textures.end(); +
+it) { | |
441 PrioritizedTexture* texture = (*it); | |
442 PrioritizedTexture::Backing* backing = texture->backing(); | |
443 if (backing) { | |
444 if (backing->resourceHasBeenDeleted()) { | |
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()); | |
447 } else { | |
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()); | |
450 } | |
451 DCHECK(backing->owner() == texture); | |
452 } | |
453 } | |
454 | |
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 | |
457 // reduceMemory will not find all textures available for eviction/recycling)
. | |
458 bool reachedUnrecyclable = false; | |
459 PrioritizedTexture::Backing* previous_backing = NULL; | |
460 for (BackingList::iterator it = m_backings.begin(); it != m_backings.end();
++it) { | |
461 PrioritizedTexture::Backing* backing = *it; | |
462 if (previous_backing && (!m_backingsTailNotSorted || !backing->wasAboveP
riorityCutoffAtLastPriorityUpdate())) | |
463 DCHECK(compareBackings(previous_backing, backing)); | |
464 if (!backing->canBeRecycled()) | |
465 reachedUnrecyclable = true; | |
466 if (reachedUnrecyclable) | |
467 DCHECK(!backing->canBeRecycled()); | |
468 else | |
469 DCHECK(backing->canBeRecycled()); | |
470 previous_backing = backing; | |
471 } | |
472 #endif | |
473 } | |
474 | |
475 } // namespace cc | |
OLD | NEW |