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 |
| 5 #ifndef CCPrioritizedTextureManager_h |
| 6 #define CCPrioritizedTextureManager_h |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/hash_tables.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "CCPrioritizedTexture.h" |
| 12 #include "CCPriorityCalculator.h" |
| 13 #include "CCTexture.h" |
| 14 #include "GraphicsContext3D.h" |
| 15 #include "IntRect.h" |
| 16 #include "IntSize.h" |
| 17 #include <wtf/ListHashSet.h> |
| 18 #include <wtf/Vector.h> |
| 19 |
| 20 #if defined(COMPILER_GCC) |
| 21 namespace BASE_HASH_NAMESPACE { |
| 22 template<> |
| 23 struct hash<cc::CCPrioritizedTexture*> { |
| 24 size_t operator()(cc::CCPrioritizedTexture* ptr) const { |
| 25 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); |
| 26 } |
| 27 }; |
| 28 } // namespace BASE_HASH_NAMESPACE |
| 29 #endif // COMPILER |
| 30 |
| 31 namespace cc { |
| 32 |
| 33 class CCPriorityCalculator; |
| 34 |
| 35 class CCPrioritizedTextureManager { |
| 36 public: |
| 37 static scoped_ptr<CCPrioritizedTextureManager> create(size_t maxMemoryLimitB
ytes, int maxTextureSize, int pool) |
| 38 { |
| 39 return make_scoped_ptr(new CCPrioritizedTextureManager(maxMemoryLimitByt
es, maxTextureSize, pool)); |
| 40 } |
| 41 scoped_ptr<CCPrioritizedTexture> createTexture(IntSize size, GC3Denum format
) |
| 42 { |
| 43 return make_scoped_ptr(new CCPrioritizedTexture(this, size, format)); |
| 44 } |
| 45 ~CCPrioritizedTextureManager(); |
| 46 |
| 47 typedef Vector<CCPrioritizedTexture::Backing*> BackingVector; |
| 48 |
| 49 // FIXME (http://crbug.com/137094): This 64MB default is a straggler from th
e |
| 50 // old texture manager and is just to give us a default memory allocation be
fore |
| 51 // we get a callback from the GPU memory manager. We should probaby either: |
| 52 // - wait for the callback before rendering anything instead |
| 53 // - push this into the GPU memory manager somehow. |
| 54 static size_t defaultMemoryAllocationLimit() { return 64 * 1024 * 1024; } |
| 55 |
| 56 // memoryUseBytes() describes the number of bytes used by existing allocated
textures. |
| 57 // memoryAboveCutoffBytes() describes the number of bytes that would be used
if all |
| 58 // textures that are above the cutoff were allocated. |
| 59 // memoryUseBytes() <= memoryAboveCutoffBytes() should always be true. |
| 60 size_t memoryUseBytes() const { return m_memoryUseBytes; } |
| 61 size_t memoryAboveCutoffBytes() const { return m_memoryAboveCutoffBytes; } |
| 62 size_t memoryForSelfManagedTextures() const { return m_maxMemoryLimitBytes -
m_memoryAvailableBytes; } |
| 63 |
| 64 void setMaxMemoryLimitBytes(size_t bytes) { m_maxMemoryLimitBytes = bytes; } |
| 65 size_t maxMemoryLimitBytes() const { return m_maxMemoryLimitBytes; } |
| 66 |
| 67 void prioritizeTextures(); |
| 68 void clearPriorities(); |
| 69 |
| 70 void reduceMemoryOnImplThread(size_t limitBytes, CCResourceProvider*); |
| 71 bool evictedBackingsExist() const { return !m_evictedBackings.isEmpty(); } |
| 72 void getEvictedBackings(BackingVector& evictedBackings); |
| 73 void unlinkEvictedBackings(const BackingVector& evictedBackings); |
| 74 // Deletes all evicted backings, unlinking them from their owning textures i
f needed. |
| 75 // Returns true if this function unlinked any backings from their owning tex
ture while |
| 76 // destroying them. |
| 77 bool deleteEvictedBackings(); |
| 78 |
| 79 bool requestLate(CCPrioritizedTexture*); |
| 80 |
| 81 void reduceMemory(CCResourceProvider*); |
| 82 void clearAllMemory(CCResourceProvider*); |
| 83 |
| 84 void acquireBackingTextureIfNeeded(CCPrioritizedTexture*, CCResourceProvider
*); |
| 85 |
| 86 void registerTexture(CCPrioritizedTexture*); |
| 87 void unregisterTexture(CCPrioritizedTexture*); |
| 88 void returnBackingTexture(CCPrioritizedTexture*); |
| 89 |
| 90 private: |
| 91 friend class CCPrioritizedTextureTest; |
| 92 |
| 93 enum EvictionPriorityPolicy { |
| 94 RespectManagerPriorityCutoff, |
| 95 DoNotRespectManagerPriorityCutoff, |
| 96 }; |
| 97 |
| 98 // Compare textures. Highest priority first. |
| 99 static inline bool compareTextures(CCPrioritizedTexture* a, CCPrioritizedTex
ture* b) |
| 100 { |
| 101 if (a->requestPriority() == b->requestPriority()) |
| 102 return a < b; |
| 103 return CCPriorityCalculator::priorityIsHigher(a->requestPriority(), b->r
equestPriority()); |
| 104 } |
| 105 // Compare backings. Lowest priority first. |
| 106 static inline bool compareBackings(CCPrioritizedTexture::Backing* a, CCPrior
itizedTexture::Backing* b) |
| 107 { |
| 108 int priorityA = a->requestPriorityAtLastPriorityUpdate(); |
| 109 int priorityB = b->requestPriorityAtLastPriorityUpdate(); |
| 110 if (priorityA != priorityB) |
| 111 return CCPriorityCalculator::priorityIsLower(priorityA, priorityB); |
| 112 bool aboveCutoffA = a->wasAbovePriorityCutoffAtLastPriorityUpdate(); |
| 113 bool aboveCutoffB = b->wasAbovePriorityCutoffAtLastPriorityUpdate(); |
| 114 if (!aboveCutoffA && aboveCutoffB) |
| 115 return true; |
| 116 if (aboveCutoffA && !aboveCutoffB) |
| 117 return false; |
| 118 return a < b; |
| 119 } |
| 120 |
| 121 CCPrioritizedTextureManager(size_t maxMemoryLimitBytes, int maxTextureSize,
int pool); |
| 122 |
| 123 void updateBackingsPriorities(); |
| 124 void evictBackingsToReduceMemory(size_t limitBytes, EvictionPriorityPolicy,
CCResourceProvider*); |
| 125 CCPrioritizedTexture::Backing* createBacking(IntSize, GC3Denum format, CCRes
ourceProvider*); |
| 126 void evictBackingResource(CCPrioritizedTexture::Backing*, CCResourceProvider
*); |
| 127 |
| 128 #if !ASSERT_DISABLED |
| 129 void assertInvariants(); |
| 130 #endif |
| 131 |
| 132 size_t m_maxMemoryLimitBytes; |
| 133 unsigned m_priorityCutoff; |
| 134 size_t m_memoryUseBytes; |
| 135 size_t m_memoryAboveCutoffBytes; |
| 136 size_t m_memoryAvailableBytes; |
| 137 int m_pool; |
| 138 |
| 139 typedef base::hash_set<CCPrioritizedTexture*> TextureSet; |
| 140 typedef ListHashSet<CCPrioritizedTexture::Backing*> BackingSet; |
| 141 typedef Vector<CCPrioritizedTexture*> TextureVector; |
| 142 |
| 143 TextureSet m_textures; |
| 144 BackingSet m_backings; |
| 145 BackingVector m_evictedBackings; |
| 146 |
| 147 TextureVector m_tempTextureVector; |
| 148 BackingVector m_tempBackingVector; |
| 149 |
| 150 // Set by the main thread when it adjust priorities in such a way that |
| 151 // the m_backings array's view of priorities is now out of date. |
| 152 bool m_needsUpdateBackingsPrioritites; |
| 153 |
| 154 DISALLOW_COPY_AND_ASSIGN(CCPrioritizedTextureManager); |
| 155 }; |
| 156 |
| 157 } // namespace cc |
| 158 |
| 159 #endif |
OLD | NEW |