Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GrGpuResource.h
1 /*
2  * Copyright 2014 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef GrGpuResource_DEFINED
9 #define GrGpuResource_DEFINED
10 
11 #include "GrResourceKey.h"
12 #include "GrTypesPriv.h"
13 #include "SkData.h"
14 
15 class GrContext;
16 class GrGpu;
17 class GrResourceCache;
18 class SkTraceMemoryDump;
19 
47 template <typename DERIVED> class GrIORef : public SkNoncopyable {
48 public:
49  // Some of the signatures are written to mirror SkRefCnt so that GrGpuResource can work with
50  // templated helper classes (e.g. SkAutoTUnref). However, we have different categories of
51  // refs (e.g. pending reads). We also don't require thread safety as GrCacheable objects are
52  // not intended to cross thread boundaries.
53  void ref() const {
54  this->validate();
55  ++fRefCnt;
56  }
57 
58  void unref() const {
59  this->validate();
60 
61  if (!(--fRefCnt)) {
62  if (!static_cast<const DERIVED*>(this)->notifyRefCountIsZero()) {
63  return;
64  }
65  }
66 
67  this->didRemoveRefOrPendingIO(kRef_CntType);
68  }
69 
70  void validate() const {
71 #ifdef SK_DEBUG
72  SkASSERT(fRefCnt >= 0);
73  SkASSERT(fPendingReads >= 0);
74  SkASSERT(fPendingWrites >= 0);
75  SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 0);
76 #endif
77  }
78 
79 protected:
80  GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { }
81 
82  enum CntType {
83  kRef_CntType,
84  kPendingRead_CntType,
85  kPendingWrite_CntType,
86  };
87 
88  bool isPurgeable() const { return !this->internalHasRef() && !this->internalHasPendingIO(); }
89 
90  bool internalHasPendingRead() const { return SkToBool(fPendingReads); }
91  bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); }
92  bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendingReads); }
93 
94  bool internalHasRef() const { return SkToBool(fRefCnt); }
95 
96 private:
97  void addPendingRead() const {
98  this->validate();
99  ++fPendingReads;
100  }
101 
102  void completedRead() const {
103  this->validate();
104  --fPendingReads;
105  this->didRemoveRefOrPendingIO(kPendingRead_CntType);
106  }
107 
108  void addPendingWrite() const {
109  this->validate();
110  ++fPendingWrites;
111  }
112 
113  void completedWrite() const {
114  this->validate();
115  --fPendingWrites;
116  this->didRemoveRefOrPendingIO(kPendingWrite_CntType);
117  }
118 
119 private:
120  void didRemoveRefOrPendingIO(CntType cntTypeRemoved) const {
121  if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) {
122  static_cast<const DERIVED*>(this)->notifyAllCntsAreZero(cntTypeRemoved);
123  }
124  }
125 
126  mutable int32_t fRefCnt;
127  mutable int32_t fPendingReads;
128  mutable int32_t fPendingWrites;
129 
130  // This class is used to manage conversion of refs to pending reads/writes.
131  friend class GrGpuResourceRef;
132  friend class GrResourceCache; // to check IO ref counts.
133 
134  template <typename, GrIOType> friend class GrPendingIOResource;
135 };
136 
140 class SK_API GrGpuResource : public GrIORef<GrGpuResource> {
141 public:
142 
153  bool wasDestroyed() const { return NULL == fGpu; }
154 
161  const GrContext* getContext() const;
162  GrContext* getContext();
163 
171  size_t gpuMemorySize() const {
172  if (kInvalidGpuMemorySize == fGpuMemorySize) {
173  fGpuMemorySize = this->onGpuMemorySize();
174  SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
175  }
176  return fGpuMemorySize;
177  }
178 
184  uint32_t getUniqueID() const { return fUniqueID; }
185 
188  const GrUniqueKey& getUniqueKey() const { return fUniqueKey; }
189 
196  const SkData* setCustomData(const SkData* data);
197 
202  const SkData* getCustomData() const { return fData.get(); }
203 
207  class CacheAccess;
208  inline CacheAccess cacheAccess();
209  inline const CacheAccess cacheAccess() const;
210 
214  class ResourcePriv;
215  inline ResourcePriv resourcePriv();
216  inline const ResourcePriv resourcePriv() const;
217 
226  void abandon();
227 
233  virtual void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
234 
235 protected:
236  // This must be called by every non-wrapped GrGpuObject. It should be called once the object is
237  // fully initialized (i.e. only from the constructors of the final class).
238  void registerWithCache(SkBudgeted);
239 
240  // This must be called by every GrGpuObject that references any wrapped backend 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*);
246  virtual ~GrGpuResource();
247 
248  GrGpu* getGpu() const { return fGpu; }
249 
251  virtual void onRelease() { }
255  virtual void onAbandon() { }
256 
261  void didChangeGpuMemorySize() const;
262 
267  virtual void setMemoryBacking(SkTraceMemoryDump*, const SkString&) const {}
268 
269 private:
276  virtual void computeScratchKey(GrScratchKey*) const { };
277 
281  void release();
282 
283  virtual size_t onGpuMemorySize() const = 0;
284 
285  // See comments in CacheAccess and ResourcePriv.
286  void setUniqueKey(const GrUniqueKey&);
287  void removeUniqueKey();
288  void notifyAllCntsAreZero(CntType) const;
289  bool notifyRefCountIsZero() const;
290  void removeScratchKey();
291  void makeBudgeted();
292  void makeUnbudgeted();
293 
294 #ifdef SK_DEBUG
295  friend class GrGpu; // for assert in GrGpu to access getGpu
296 #endif
297 
298  static uint32_t CreateUniqueID();
299 
300  // An index into a heap when this resource is purgeable or an array when not. This is maintained
301  // by the cache.
302  int fCacheArrayIndex;
303  // This value reflects how recently this resource was accessed in the cache. This is maintained
304  // by the cache.
305  uint32_t fTimestamp;
306 
307  static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
308  GrScratchKey fScratchKey;
309  GrUniqueKey fUniqueKey;
310 
311  // This is not ref'ed but abandon() or release() will be called before the GrGpu object
312  // is destroyed. Those calls set will this to NULL.
313  GrGpu* fGpu;
314  mutable size_t fGpuMemorySize;
315 
316  SkBudgeted fBudgeted;
317  bool fRefsWrappedObjects;
318  const uint32_t fUniqueID;
319 
321 
322  typedef GrIORef<GrGpuResource> INHERITED;
323  friend class GrIORef<GrGpuResource>; // to access notifyAllCntsAreZero and notifyRefCntIsZero.
324 };
325 
326 #endif
Base class for objects that can be kept in the GrResourceCache.
Definition: GrGpuResource.h:140
virtual void onAbandon()
Overridden to abandon any internal handles, ptrs, etc to backend API resources.
Definition: GrGpuResource.h:255
uint32_t getUniqueID() const
Gets an id that is unique for this GrGpuResource object.
Definition: GrGpuResource.h:184
const GrUniqueKey & getUniqueKey() const
Returns the current unique key for the resource.
Definition: GrGpuResource.h:188
A key that allows for exclusive use of a resource for a use case (AKA "domain").
Definition: GrResourceKey.h:220
const SkData * getCustomData() const
Returns the custom data object that was attached to this resource by calling setCustomData.
Definition: GrGpuResource.h:202
This is similar to GrTGpuResourceRef but can only be in the pending IO state.
Definition: GrGpuResourceRef.h:156
virtual void onRelease()
Overridden to free GPU resources in the backend API.
Definition: GrGpuResource.h:251
bool wasDestroyed() const
Tests whether a object has been abandoned or released.
Definition: GrGpuResource.h:153
Base class for GrGpuResource.
Definition: GrGpuResource.h:47
SkData holds an immutable data buffer.
Definition: SkData.h:22
#define SkToBool(cond)
Returns 0 or 1 based on the condition.
Definition: SkTypes.h:287
This class is intended only for internal use in core Gr code.
Definition: GrGpuResourceRef.h:37
virtual void setMemoryBacking(SkTraceMemoryDump *, const SkString &) const
Allows subclasses to add additional backing information to the SkTraceMemoryDump. ...
Definition: GrGpuResource.h:267
size_t gpuMemorySize() const
Retrieves the amount of GPU memory used by this resource in bytes.
Definition: GrGpuResource.h:171
Definition: GrContext.h:48
A key used for scratch resources.
Definition: GrResourceKey.h:166
Interface for memory tracing.
Definition: SkTraceMemoryDump.h:20
virtual void computeScratchKey(GrScratchKey *) const
Called by the registerWithCache if the resource is available to be used as scratch.
Definition: GrGpuResource.h:276
Light weight class for managing strings.
Definition: SkString.h:121