OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrGpuResource_DEFINED | 8 #ifndef GrGpuResource_DEFINED |
9 #define GrGpuResource_DEFINED | 9 #define GrGpuResource_DEFINED |
10 | 10 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 template <typename, GrIOType> friend class GrPendingIOResource; | 118 template <typename, GrIOType> friend class GrPendingIOResource; |
119 }; | 119 }; |
120 | 120 |
121 /** | 121 /** |
122 * Base class for objects that can be kept in the GrResourceCache2. | 122 * Base class for objects that can be kept in the GrResourceCache2. |
123 */ | 123 */ |
124 class SK_API GrGpuResource : public GrIORef<GrGpuResource> { | 124 class SK_API GrGpuResource : public GrIORef<GrGpuResource> { |
125 public: | 125 public: |
126 SK_DECLARE_INST_COUNT(GrGpuResource) | 126 SK_DECLARE_INST_COUNT(GrGpuResource) |
127 | 127 |
| 128 enum LifeCycle { |
| 129 /** |
| 130 * The resource is cached and owned by Skia. Resources with this status
may be kept alive |
| 131 * by the cache as either scratch or content resources even when there a
re no refs to them. |
| 132 * The cache may release them whenever there are no refs. |
| 133 */ |
| 134 kCached_LifeCycle, |
| 135 /** |
| 136 * The resource is uncached. As soon as there are no more refs to it, it
is released. Under |
| 137 * the hood the cache may opaquely recycle it as a cached resource. |
| 138 */ |
| 139 kUncached_LifeCycle, |
| 140 /** |
| 141 * Similar to uncached, but Skia does not manage the lifetime of the und
erlying backend |
| 142 * 3D API object(s). The client is responsible for freeing those. Used t
o inject client- |
| 143 * created GPU resources into Skia (e.g. to render to a client-created t
exture). |
| 144 */ |
| 145 kWrapped_LifeCycle, |
| 146 }; |
| 147 |
128 /** | 148 /** |
129 * Tests whether a object has been abandoned or released. All objects will | 149 * Tests whether a object has been abandoned or released. All objects will |
130 * be in this state after their creating GrContext is destroyed or has | 150 * be in this state after their creating GrContext is destroyed or has |
131 * contextLost called. It's up to the client to test wasDestroyed() before | 151 * contextLost called. It's up to the client to test wasDestroyed() before |
132 * attempting to use an object if it holds refs on objects across | 152 * attempting to use an object if it holds refs on objects across |
133 * ~GrContext, freeResources with the force flag, or contextLost. | 153 * ~GrContext, freeResources with the force flag, or contextLost. |
134 * | 154 * |
135 * @return true if the object has been released or abandoned, | 155 * @return true if the object has been released or abandoned, |
136 * false otherwise. | 156 * false otherwise. |
137 */ | 157 */ |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 * when a texture becomes unsafe to use after having been shared through | 217 * when a texture becomes unsafe to use after having been shared through |
198 * a texture mailbox. | 218 * a texture mailbox. |
199 */ | 219 */ |
200 void abandon(); | 220 void abandon(); |
201 | 221 |
202 protected: | 222 protected: |
203 // This must be called by every GrGpuObject. It should be called once the ob
ject is fully | 223 // This must be called by every GrGpuObject. It should be called once the ob
ject is fully |
204 // initialized (i.e. not in a base class constructor). | 224 // initialized (i.e. not in a base class constructor). |
205 void registerWithCache(); | 225 void registerWithCache(); |
206 | 226 |
207 GrGpuResource(GrGpu*, bool isWrapped); | 227 GrGpuResource(GrGpu*, LifeCycle); |
208 virtual ~GrGpuResource(); | 228 virtual ~GrGpuResource(); |
209 | 229 |
210 GrGpu* getGpu() const { return fGpu; } | 230 GrGpu* getGpu() const { return fGpu; } |
211 | 231 |
212 /** Overridden to free GPU resources in the backend API. */ | 232 /** Overridden to free GPU resources in the backend API. */ |
213 virtual void onRelease() { } | 233 virtual void onRelease() { } |
214 /** Overridden to abandon any internal handles, ptrs, etc to backend API res
ources. | 234 /** Overridden to abandon any internal handles, ptrs, etc to backend API res
ources. |
215 This may be called when the underlying 3D context is no longer valid and
so no | 235 This may be called when the underlying 3D context is no longer valid and
so no |
216 backend API calls should be made. */ | 236 backend API calls should be made. */ |
217 virtual void onAbandon() { } | 237 virtual void onAbandon() { } |
218 | 238 |
219 bool isWrapped() const { return SkToBool(kWrapped_Flag & fFlags); } | 239 bool isWrapped() const { return kWrapped_LifeCycle == fLifeCycle; } |
220 | 240 |
221 /** | 241 /** |
222 * This entry point should be called whenever gpuMemorySize() should report
a different size. | 242 * This entry point should be called whenever gpuMemorySize() should report
a different size. |
223 * The cache will call gpuMemorySize() to update the current size of the res
ource. | 243 * The cache will call gpuMemorySize() to update the current size of the res
ource. |
224 */ | 244 */ |
225 void didChangeGpuMemorySize() const; | 245 void didChangeGpuMemorySize() const; |
226 | 246 |
227 /** | 247 /** |
228 * Optionally called by the GrGpuResource subclass if the resource can be us
ed as scratch. | 248 * Optionally called by the GrGpuResource subclass if the resource can be us
ed as scratch. |
229 * By default resources are not usable as scratch. This should only be calle
d once. | 249 * By default resources are not usable as scratch. This should only be calle
d once. |
230 **/ | 250 **/ |
231 void setScratchKey(const GrScratchKey& scratchKey); | 251 void setScratchKey(const GrScratchKey& scratchKey); |
232 | 252 |
233 private: | 253 private: |
234 /** | 254 /** |
235 * Frees the object in the underlying 3D API. Called by CacheAccess. | 255 * Frees the object in the underlying 3D API. Called by CacheAccess. |
236 */ | 256 */ |
237 void release(); | 257 void release(); |
238 | 258 |
239 virtual size_t onGpuMemorySize() const = 0; | 259 virtual size_t onGpuMemorySize() const = 0; |
240 | 260 |
241 // See comments in CacheAccess. | 261 // See comments in CacheAccess. |
242 bool setContentKey(const GrResourceKey& contentKey); | 262 bool setContentKey(const GrResourceKey& contentKey); |
243 void setBudgeted(bool countsAgainstBudget); | |
244 void notifyIsPurgable() const; | 263 void notifyIsPurgable() const; |
245 void removeScratchKey(); | 264 void removeScratchKey(); |
246 | 265 |
247 #ifdef SK_DEBUG | 266 #ifdef SK_DEBUG |
248 friend class GrGpu; // for assert in GrGpu to access getGpu | 267 friend class GrGpu; // for assert in GrGpu to access getGpu |
249 #endif | 268 #endif |
250 | 269 |
251 static uint32_t CreateUniqueID(); | 270 static uint32_t CreateUniqueID(); |
252 | 271 |
253 // We're in an internal doubly linked list owned by GrResourceCache2 | 272 // We're in an internal doubly linked list owned by GrResourceCache2 |
254 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuResource); | 273 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuResource); |
255 | 274 |
256 | 275 |
257 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); | 276 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); |
258 enum Flags { | 277 enum Flags { |
259 /** | 278 /** |
260 * The resource counts against the resource cache's budget. | |
261 */ | |
262 kBudgeted_Flag = 0x1, | |
263 | |
264 /** | |
265 * This object wraps a GPU object given to us by Skia's client. Skia wil
l not free the | |
266 * underlying backend API GPU resources when the GrGpuResource is destro
yed. This also | |
267 * implies that kBudgeted_Flag is not set. | |
268 */ | |
269 kWrapped_Flag = 0x2, | |
270 | |
271 /** | |
272 * If set then fContentKey is valid and the resource is cached based on
its content. | 279 * If set then fContentKey is valid and the resource is cached based on
its content. |
273 */ | 280 */ |
274 kContentKeySet_Flag = 0x4, | 281 kContentKeySet_Flag = 0x1, |
275 }; | 282 }; |
276 | 283 |
277 GrScratchKey fScratchKey; | 284 GrScratchKey fScratchKey; |
278 // TODO(bsalomon): Remove GrResourceKey and use different simpler type for c
ontent keys. | 285 // TODO(bsalomon): Remove GrResourceKey and use different simpler type for c
ontent keys. |
279 GrResourceKey fContentKey; | 286 GrResourceKey fContentKey; |
280 | 287 |
281 // This is not ref'ed but abandon() or release() will be called before the G
rGpu object | 288 // This is not ref'ed but abandon() or release() will be called before the G
rGpu object |
282 // is destroyed. Those calls set will this to NULL. | 289 // is destroyed. Those calls set will this to NULL. |
283 GrGpu* fGpu; | 290 GrGpu* fGpu; |
284 mutable size_t fGpuMemorySize; | 291 mutable size_t fGpuMemorySize; |
285 | 292 |
286 uint32_t fFlags; | 293 uint32_t fFlags; |
| 294 LifeCycle fLifeCycle; |
287 const uint32_t fUniqueID; | 295 const uint32_t fUniqueID; |
288 | 296 |
289 SkAutoTUnref<const SkData> fData; | 297 SkAutoTUnref<const SkData> fData; |
290 | 298 |
291 typedef GrIORef<GrGpuResource> INHERITED; | 299 typedef GrIORef<GrGpuResource> INHERITED; |
292 friend class GrIORef<GrGpuResource>; // to access notifyIsPurgable. | 300 friend class GrIORef<GrGpuResource>; // to access notifyIsPurgable. |
293 }; | 301 }; |
294 | 302 |
295 #endif | 303 #endif |
OLD | NEW |