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 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 133 |
134 template <typename, GrIOType> friend class GrPendingIOResource; | 134 template <typename, GrIOType> friend class GrPendingIOResource; |
135 }; | 135 }; |
136 | 136 |
137 /** | 137 /** |
138 * Base class for objects that can be kept in the GrResourceCache. | 138 * Base class for objects that can be kept in the GrResourceCache. |
139 */ | 139 */ |
140 class SK_API GrGpuResource : public GrIORef<GrGpuResource> { | 140 class SK_API GrGpuResource : public GrIORef<GrGpuResource> { |
141 public: | 141 public: |
142 | 142 |
143 | |
144 enum LifeCycle { | |
145 /** | |
146 * The resource is cached and owned by Skia. Resources with this status
may be kept alive | |
147 * by the cache as either scratch or unique resources even when there ar
e no refs to them. | |
148 * The cache may release them whenever there are no refs. | |
149 */ | |
150 kCached_LifeCycle, | |
151 | |
152 /** | |
153 * The resource is uncached. As soon as there are no more refs to it, it
is released. Under | |
154 * the hood the cache may opaquely recycle it as a cached resource. | |
155 */ | |
156 kUncached_LifeCycle, | |
157 | |
158 /** | |
159 * Similar to uncached, but Skia does not manage the lifetime of the und
erlying backend | |
160 * 3D API object(s). The client is responsible for freeing those. Used t
o inject client- | |
161 * created GPU resources into Skia (e.g. to render to a client-created t
exture). | |
162 */ | |
163 kBorrowed_LifeCycle, | |
164 | |
165 /** | |
166 * An external resource with ownership transfered into Skia. Skia will f
ree the resource. | |
167 */ | |
168 kAdopted_LifeCycle, | |
169 }; | |
170 | |
171 /** | 143 /** |
172 * Tests whether a object has been abandoned or released. All objects will | 144 * Tests whether a object has been abandoned or released. All objects will |
173 * be in this state after their creating GrContext is destroyed or has | 145 * be in this state after their creating GrContext is destroyed or has |
174 * contextLost called. It's up to the client to test wasDestroyed() before | 146 * contextLost called. It's up to the client to test wasDestroyed() before |
175 * attempting to use an object if it holds refs on objects across | 147 * attempting to use an object if it holds refs on objects across |
176 * ~GrContext, freeResources with the force flag, or contextLost. | 148 * ~GrContext, freeResources with the force flag, or contextLost. |
177 * | 149 * |
178 * @return true if the object has been released or abandoned, | 150 * @return true if the object has been released or abandoned, |
179 * false otherwise. | 151 * false otherwise. |
180 */ | 152 */ |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 void abandon(); | 226 void abandon(); |
255 | 227 |
256 /** | 228 /** |
257 * Dumps memory usage information for this GrGpuResource to traceMemoryDump. | 229 * Dumps memory usage information for this GrGpuResource to traceMemoryDump. |
258 * Typically, subclasses should not need to override this, and should only | 230 * Typically, subclasses should not need to override this, and should only |
259 * need to override setMemoryBacking. | 231 * need to override setMemoryBacking. |
260 **/ | 232 **/ |
261 virtual void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; | 233 virtual void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; |
262 | 234 |
263 protected: | 235 protected: |
264 // This must be called by every GrGpuObject. It should be called once the ob
ject is fully | 236 // This must be called by every non-wrapped GrGpuObject. It should be called
once the object is |
265 // initialized (i.e. not in a base class constructor). | 237 // fully initialized (i.e. only from the constructors of the final class). |
266 void registerWithCache(); | 238 void registerWithCache(SkBudgeted); |
267 | 239 |
268 GrGpuResource(GrGpu*, LifeCycle); | 240 // This must be called by every GrGpuObject that references any wrapped back
end objects. It |
| 241 // should be called once the object is fully initialized (i.e. only from the
constructors of the |
| 242 // final class). |
| 243 void registerWithCacheWrapped(); |
| 244 |
| 245 GrGpuResource(GrGpu*); |
269 virtual ~GrGpuResource(); | 246 virtual ~GrGpuResource(); |
270 | 247 |
271 GrGpu* getGpu() const { return fGpu; } | 248 GrGpu* getGpu() const { return fGpu; } |
272 | 249 |
273 /** Overridden to free GPU resources in the backend API. */ | 250 /** Overridden to free GPU resources in the backend API. */ |
274 virtual void onRelease() { } | 251 virtual void onRelease() { } |
275 /** Overridden to abandon any internal handles, ptrs, etc to backend API res
ources. | 252 /** Overridden to abandon any internal handles, ptrs, etc to backend API res
ources. |
276 This may be called when the underlying 3D context is no longer valid and
so no | 253 This may be called when the underlying 3D context is no longer valid and
so no |
277 backend API calls should be made. */ | 254 backend API calls should be made. */ |
278 virtual void onAbandon() { } | 255 virtual void onAbandon() { } |
279 | 256 |
280 bool shouldFreeResources() const { return fLifeCycle != kBorrowed_LifeCycle;
} | |
281 | |
282 bool isExternal() const { | |
283 return GrGpuResource::kAdopted_LifeCycle == fLifeCycle || | |
284 GrGpuResource::kBorrowed_LifeCycle == fLifeCycle; | |
285 } | |
286 | |
287 /** | 257 /** |
288 * This entry point should be called whenever gpuMemorySize() should report
a different size. | 258 * This entry point should be called whenever gpuMemorySize() should report
a different size. |
289 * The cache will call gpuMemorySize() to update the current size of the res
ource. | 259 * The cache will call gpuMemorySize() to update the current size of the res
ource. |
290 */ | 260 */ |
291 void didChangeGpuMemorySize() const; | 261 void didChangeGpuMemorySize() const; |
292 | 262 |
293 /** | 263 /** |
294 * Optionally called by the GrGpuResource subclass if the resource can be us
ed as scratch. | |
295 * By default resources are not usable as scratch. This should only be calle
d once. | |
296 **/ | |
297 void setScratchKey(const GrScratchKey& scratchKey); | |
298 | |
299 /** | |
300 * Allows subclasses to add additional backing information to the SkTraceMem
oryDump. Called by | 264 * Allows subclasses to add additional backing information to the SkTraceMem
oryDump. Called by |
301 * onMemoryDump. The default implementation adds no backing information. | 265 * onMemoryDump. The default implementation adds no backing information. |
302 **/ | 266 **/ |
303 virtual void setMemoryBacking(SkTraceMemoryDump*, const SkString&) const {} | 267 virtual void setMemoryBacking(SkTraceMemoryDump*, const SkString&) const {} |
304 | 268 |
305 private: | 269 private: |
306 /** | 270 /** |
| 271 * Called by the registerWithCache if the resource is available to be used a
s scratch. |
| 272 * Resource subclasses should override this if the instances should be recyc
led as scratch |
| 273 * resources and populate the scratchKey with the key. |
| 274 * By default resources are not recycled as scratch. |
| 275 **/ |
| 276 virtual void computeScratchKey(GrScratchKey*) const { }; |
| 277 |
| 278 /** |
307 * Frees the object in the underlying 3D API. Called by CacheAccess. | 279 * Frees the object in the underlying 3D API. Called by CacheAccess. |
308 */ | 280 */ |
309 void release(); | 281 void release(); |
310 | 282 |
311 virtual size_t onGpuMemorySize() const = 0; | 283 virtual size_t onGpuMemorySize() const = 0; |
312 | 284 |
313 // See comments in CacheAccess and ResourcePriv. | 285 // See comments in CacheAccess and ResourcePriv. |
314 void setUniqueKey(const GrUniqueKey&); | 286 void setUniqueKey(const GrUniqueKey&); |
315 void removeUniqueKey(); | 287 void removeUniqueKey(); |
316 void notifyAllCntsAreZero(CntType) const; | 288 void notifyAllCntsAreZero(CntType) const; |
(...skipping 17 matching lines...) Expand all Loading... |
334 | 306 |
335 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); | 307 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); |
336 GrScratchKey fScratchKey; | 308 GrScratchKey fScratchKey; |
337 GrUniqueKey fUniqueKey; | 309 GrUniqueKey fUniqueKey; |
338 | 310 |
339 // This is not ref'ed but abandon() or release() will be called before the G
rGpu object | 311 // This is not ref'ed but abandon() or release() will be called before the G
rGpu object |
340 // is destroyed. Those calls set will this to NULL. | 312 // is destroyed. Those calls set will this to NULL. |
341 GrGpu* fGpu; | 313 GrGpu* fGpu; |
342 mutable size_t fGpuMemorySize; | 314 mutable size_t fGpuMemorySize; |
343 | 315 |
344 LifeCycle fLifeCycle; | 316 SkBudgeted fBudgeted; |
| 317 bool fRefsWrappedObjects; |
345 const uint32_t fUniqueID; | 318 const uint32_t fUniqueID; |
346 | 319 |
347 SkAutoTUnref<const SkData> fData; | 320 SkAutoTUnref<const SkData> fData; |
348 | 321 |
349 typedef GrIORef<GrGpuResource> INHERITED; | 322 typedef GrIORef<GrGpuResource> INHERITED; |
350 friend class GrIORef<GrGpuResource>; // to access notifyAllCntsAreZero and n
otifyRefCntIsZero. | 323 friend class GrIORef<GrGpuResource>; // to access notifyAllCntsAreZero and n
otifyRefCntIsZero. |
351 }; | 324 }; |
352 | 325 |
353 #endif | 326 #endif |
OLD | NEW |