Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GrGpuResourceRef.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 GrGpuResourceRef_DEFINED
9 #define GrGpuResourceRef_DEFINED
10 
11 #include "GrGpuResource.h"
12 #include "GrRenderTarget.h"
13 #include "GrTexture.h"
14 #include "SkRefCnt.h"
15 
37 class GrGpuResourceRef : SkNoncopyable {
38 public:
40 
41  GrGpuResource* getResource() const { return fResource; }
42 
44  bool ownsPendingIO() const { return fPendingIO; }
45 
48  void reset();
49 
50 protected:
52 
55  GrGpuResourceRef(GrGpuResource*, GrIOType);
56 
59  void setResource(GrGpuResource*, GrIOType);
60 
61 private:
64  void markPendingIO() const;
65 
70  void removeRef() const;
71 
76  void pendingIOComplete() const;
77 
78  friend class GrProgramElement;
79 
80  GrGpuResource* fResource;
81  mutable bool fOwnRef;
82  mutable bool fPendingIO;
83  GrIOType fIOType;
84 
85  typedef SkNoncopyable INHERITED;
86 };
87 
91 template <typename T> class GrTGpuResourceRef : public GrGpuResourceRef {
92 public:
94 
97  GrTGpuResourceRef(T* resource, GrIOType ioType) : INHERITED(resource, ioType) { }
98 
99  T* get() const { return static_cast<T*>(this->getResource()); }
100 
103  void set(T* resource, GrIOType ioType) { this->setResource(resource, ioType); }
104 
105 private:
106  typedef GrGpuResourceRef INHERITED;
107 };
108 
109 // Specializations for GrTexture and GrRenderTarget because they use virtual inheritance.
110 template<> class GrTGpuResourceRef<GrTexture> : public GrGpuResourceRef {
111 public:
112  GrTGpuResourceRef() {}
113 
114  GrTGpuResourceRef(GrTexture* texture, GrIOType ioType) : INHERITED(texture, ioType) { }
115 
116  GrTexture* get() const {
117  GrSurface* surface = static_cast<GrSurface*>(this->getResource());
118  if (surface) {
119  return surface->asTexture();
120  } else {
121  return NULL;
122  }
123  }
124 
125  void set(GrTexture* texture, GrIOType ioType) { this->setResource(texture, ioType); }
126 
127 private:
128  typedef GrGpuResourceRef INHERITED;
129 };
130 
132 public:
133  GrTGpuResourceRef() {}
134 
135  GrTGpuResourceRef(GrRenderTarget* rt, GrIOType ioType) : INHERITED(rt, ioType) { }
136 
137  GrRenderTarget* get() const {
138  GrSurface* surface = static_cast<GrSurface*>(this->getResource());
139  if (surface) {
140  return surface->asRenderTarget();
141  } else {
142  return NULL;
143  }
144  }
145 
146  void set(GrRenderTarget* rt, GrIOType ioType) { this->setResource(rt, ioType); }
147 
148 private:
149  typedef GrGpuResourceRef INHERITED;
150 };
151 
156 template <typename T, GrIOType IO_TYPE> class GrPendingIOResource : SkNoncopyable {
157 public:
158  GrPendingIOResource(T* resource = NULL) : fResource(NULL) {
159  this->reset(resource);
160  }
161 
162  void reset(T* resource) {
163  if (resource) {
164  switch (IO_TYPE) {
165  case kRead_GrIOType:
166  resource->addPendingRead();
167  break;
168  case kWrite_GrIOType:
169  resource->addPendingWrite();
170  break;
171  case kRW_GrIOType:
172  resource->addPendingRead();
173  resource->addPendingWrite();
174  break;
175  }
176  }
177  this->release();
178  fResource = resource;
179  }
180 
182  this->release();
183  }
184 
185  explicit operator bool() const { return SkToBool(fResource); }
186 
187  bool operator==(const GrPendingIOResource& other) const {
188  return fResource == other.fResource;
189  }
190 
191  T* get() const { return fResource; }
192 
193 private:
194  void release() {
195  if (fResource) {
196  switch (IO_TYPE) {
197  case kRead_GrIOType:
198  fResource->completedRead();
199  break;
200  case kWrite_GrIOType:
201  fResource->completedWrite();
202  break;
203  case kRW_GrIOType:
204  fResource->completedRead();
205  fResource->completedWrite();
206  break;
207  }
208  }
209  }
210 
211  T* fResource;
212 };
213 #endif
Base class for objects that can be kept in the GrResourceCache.
Definition: GrGpuResource.h:140
void pendingIOComplete() const
Called to indicate that the previous pending IO is complete.
void removeRef() const
Called when the program element/draw state is no longer owned by GrDrawTarget-client code...
Templated version of GrGpuResourceRef to enforce type safety.
Definition: GrGpuResourceRef.h:91
void set(T *resource, GrIOType ioType)
Adopts a ref from the caller.
Definition: GrGpuResourceRef.h:103
This is similar to GrTGpuResourceRef but can only be in the pending IO state.
Definition: GrGpuResourceRef.h:156
void setResource(GrGpuResource *, GrIOType)
Adopts a ref from the caller.
virtual GrRenderTarget * asRenderTarget()
Definition: GrSurface.h:66
#define SkToBool(cond)
Returns 0 or 1 based on the condition.
Definition: SkTypes.h:287
Base class for GrProcessor.
Definition: GrProgramElement.h:44
virtual GrTexture * asTexture()
Definition: GrSurface.h:60
Definition: GrSurface.h:21
This class is intended only for internal use in core Gr code.
Definition: GrGpuResourceRef.h:37
void reset()
Shortcut for calling setResource() with NULL.
void markPendingIO() const
Called by owning GrProgramElement when the program element is first scheduled for execution...
GrTGpuResourceRef(T *resource, GrIOType ioType)
Adopts a ref from the caller.
Definition: GrGpuResourceRef.h:97
GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
Definition: GrRenderTarget.h:26
bool ownsPendingIO() const
Does this object own a pending read or write on the resource it is wrapping.
Definition: GrGpuResourceRef.h:44
Definition: GrTexture.h:19