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 #ifndef CC_PRIORITIZED_TEXTURE_MANAGER_H_ | |
6 #define CC_PRIORITIZED_TEXTURE_MANAGER_H_ | |
7 | |
8 #include <list> | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/hash_tables.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "cc/cc_export.h" | |
15 #include "cc/prioritized_texture.h" | |
16 #include "cc/priority_calculator.h" | |
17 #include "cc/texture.h" | |
18 #include "third_party/khronos/GLES2/gl2.h" | |
19 #include "ui/gfx/size.h" | |
20 | |
21 #if defined(COMPILER_GCC) | |
22 namespace BASE_HASH_NAMESPACE { | |
23 template<> | |
24 struct hash<cc::PrioritizedTexture*> { | |
25 size_t operator()(cc::PrioritizedTexture* ptr) const { | |
26 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); | |
27 } | |
28 }; | |
29 } // namespace BASE_HASH_NAMESPACE | |
30 #endif // COMPILER | |
31 | |
32 namespace cc { | |
33 | |
34 class PriorityCalculator; | |
35 | |
36 class CC_EXPORT PrioritizedTextureManager { | |
37 public: | |
38 static scoped_ptr<PrioritizedTextureManager> create(size_t maxMemoryLimitByt
es, int maxTextureSize, int pool) | |
39 { | |
40 return make_scoped_ptr(new PrioritizedTextureManager(maxMemoryLimitBytes
, maxTextureSize, pool)); | |
41 } | |
42 scoped_ptr<PrioritizedTexture> createTexture(gfx::Size size, GLenum format) | |
43 { | |
44 return make_scoped_ptr(new PrioritizedTexture(this, size, format)); | |
45 } | |
46 ~PrioritizedTextureManager(); | |
47 | |
48 typedef std::list<PrioritizedTexture::Backing*> BackingList; | |
49 | |
50 // FIXME (http://crbug.com/137094): This 64MB default is a straggler from th
e | |
51 // old texture manager and is just to give us a default memory allocation be
fore | |
52 // we get a callback from the GPU memory manager. We should probaby either: | |
53 // - wait for the callback before rendering anything instead | |
54 // - push this into the GPU memory manager somehow. | |
55 static size_t defaultMemoryAllocationLimit() { return 64 * 1024 * 1024; } | |
56 | |
57 // memoryUseBytes() describes the number of bytes used by existing allocated
textures. | |
58 // memoryAboveCutoffBytes() describes the number of bytes that would be used
if all | |
59 // textures that are above the cutoff were allocated. | |
60 // memoryUseBytes() <= memoryAboveCutoffBytes() should always be true. | |
61 size_t memoryUseBytes() const { return m_memoryUseBytes; } | |
62 size_t memoryAboveCutoffBytes() const { return m_memoryAboveCutoffBytes; } | |
63 size_t memoryForSelfManagedTextures() const { return m_maxMemoryLimitBytes -
m_memoryAvailableBytes; } | |
64 | |
65 void setMaxMemoryLimitBytes(size_t bytes) { m_maxMemoryLimitBytes = bytes; } | |
66 size_t maxMemoryLimitBytes() const { return m_maxMemoryLimitBytes; } | |
67 | |
68 // Sepecify a external priority cutoff. Only textures that have a strictly h
igher priority | |
69 // than this cutoff will be allowed. | |
70 void setExternalPriorityCutoff(int priorityCutoff) { m_externalPriorityCutof
f = priorityCutoff; } | |
71 | |
72 // Return the amount of texture memory required at particular cutoffs. | |
73 size_t memoryVisibleBytes() const; | |
74 size_t memoryVisibleAndNearbyBytes() const; | |
75 | |
76 void prioritizeTextures(); | |
77 void clearPriorities(); | |
78 | |
79 // Delete contents textures' backing resources until they use only bytesLimi
t bytes. This may | |
80 // be called on the impl thread while the main thread is running. Returns tr
ue if resources are | |
81 // indeed evicted as a result of this call. | |
82 bool reduceMemoryOnImplThread(size_t limitBytes, int priorityCutoff, Resourc
eProvider*); | |
83 // Returns true if there exist any textures that are linked to backings that
have had their | |
84 // resources evicted. Only when we commit a tree that has no textures linked
to evicted backings | |
85 // may we allow drawing. | |
86 bool linkedEvictedBackingsExist() const; | |
87 // Retrieve the list of all contents textures' backings that have been evict
ed, to pass to the | |
88 // main thread to unlink them from their owning textures. | |
89 void getEvictedBackings(BackingList& evictedBackings); | |
90 // Unlink the list of contents textures' backings from their owning textures
on the main thread | |
91 // before updating layers. | |
92 void unlinkEvictedBackings(const BackingList& evictedBackings); | |
93 | |
94 bool requestLate(PrioritizedTexture*); | |
95 | |
96 void reduceMemory(ResourceProvider*); | |
97 void clearAllMemory(ResourceProvider*); | |
98 | |
99 void acquireBackingTextureIfNeeded(PrioritizedTexture*, ResourceProvider*); | |
100 | |
101 void registerTexture(PrioritizedTexture*); | |
102 void unregisterTexture(PrioritizedTexture*); | |
103 void returnBackingTexture(PrioritizedTexture*); | |
104 | |
105 // Update all backings' priorities from their owning texture. | |
106 void pushTexturePrioritiesToBackings(); | |
107 | |
108 // Mark all textures' backings as being in the drawing impl tree. | |
109 void updateBackingsInDrawingImplTree(); | |
110 | |
111 private: | |
112 friend class PrioritizedTextureTest; | |
113 | |
114 enum EvictionPolicy { | |
115 EvictOnlyRecyclable, | |
116 EvictAnything, | |
117 }; | |
118 | |
119 // Compare textures. Highest priority first. | |
120 static inline bool compareTextures(PrioritizedTexture* a, PrioritizedTexture
* b) | |
121 { | |
122 if (a->requestPriority() == b->requestPriority()) | |
123 return a < b; | |
124 return PriorityCalculator::priorityIsHigher(a->requestPriority(), b->req
uestPriority()); | |
125 } | |
126 // Compare backings. Lowest priority first. | |
127 static inline bool compareBackings(PrioritizedTexture::Backing* a, Prioritiz
edTexture::Backing* b) | |
128 { | |
129 // Make textures that can be recycled appear first | |
130 if (a->canBeRecycled() != b->canBeRecycled()) | |
131 return (a->canBeRecycled() > b->canBeRecycled()); | |
132 // Then sort by being above or below the priority cutoff. | |
133 if (a->wasAbovePriorityCutoffAtLastPriorityUpdate() != b->wasAbovePriori
tyCutoffAtLastPriorityUpdate()) | |
134 return (a->wasAbovePriorityCutoffAtLastPriorityUpdate() < b->wasAbov
ePriorityCutoffAtLastPriorityUpdate()); | |
135 // Then sort by priority (note that backings that no longer have owners
will | |
136 // always have the lowest priority) | |
137 if (a->requestPriorityAtLastPriorityUpdate() != b->requestPriorityAtLast
PriorityUpdate()) | |
138 return PriorityCalculator::priorityIsLower(a->requestPriorityAtLastP
riorityUpdate(), b->requestPriorityAtLastPriorityUpdate()); | |
139 // Finally sort by being in the impl tree versus being completely unrefe
renced | |
140 if (a->inDrawingImplTree() != b->inDrawingImplTree()) | |
141 return (a->inDrawingImplTree() < b->inDrawingImplTree()); | |
142 return a < b; | |
143 } | |
144 | |
145 PrioritizedTextureManager(size_t maxMemoryLimitBytes, int maxTextureSize, in
t pool); | |
146 | |
147 bool evictBackingsToReduceMemory(size_t limitBytes, int priorityCutoff, Evic
tionPolicy, ResourceProvider*); | |
148 PrioritizedTexture::Backing* createBacking(gfx::Size, GLenum format, Resourc
eProvider*); | |
149 void evictFirstBackingResource(ResourceProvider*); | |
150 void deleteUnlinkedEvictedBackings(); | |
151 void sortBackings(); | |
152 | |
153 void assertInvariants(); | |
154 | |
155 size_t m_maxMemoryLimitBytes; | |
156 // The priority cutoff based on memory pressure. This is not a strict | |
157 // cutoff -- requestLate allows textures with priority equal to this | |
158 // cutoff to be allowed. | |
159 int m_priorityCutoff; | |
160 // The priority cutoff based on external memory policy. This is a strict | |
161 // cutoff -- no textures with priority equal to this cutoff will be allowed. | |
162 int m_externalPriorityCutoff; | |
163 size_t m_memoryUseBytes; | |
164 size_t m_memoryAboveCutoffBytes; | |
165 size_t m_memoryAvailableBytes; | |
166 int m_pool; | |
167 | |
168 typedef base::hash_set<PrioritizedTexture*> TextureSet; | |
169 typedef std::vector<PrioritizedTexture*> TextureVector; | |
170 | |
171 TextureSet m_textures; | |
172 // This list is always sorted in eviction order, with the exception the | |
173 // newly-allocated or recycled textures at the very end of the tail that | |
174 // are not sorted by priority. | |
175 BackingList m_backings; | |
176 bool m_backingsTailNotSorted; | |
177 BackingList m_evictedBackings; | |
178 | |
179 TextureVector m_tempTextureVector; | |
180 | |
181 // Statistics about memory usage at priority cutoffs, computed at prioritize
Textures. | |
182 size_t m_memoryVisibleBytes; | |
183 size_t m_memoryVisibleAndNearbyBytes; | |
184 | |
185 // Statistics copied at the time of pushTexturePrioritiesToBackings. | |
186 size_t m_memoryVisibleLastPushedBytes; | |
187 size_t m_memoryVisibleAndNearbyLastPushedBytes; | |
188 | |
189 DISALLOW_COPY_AND_ASSIGN(PrioritizedTextureManager); | |
190 }; | |
191 | |
192 } // namespace cc | |
193 | |
194 #endif // CC_PRIORITIZED_TEXTURE_MANAGER_H_ | |
OLD | NEW |