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