Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GrProgramElement.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 GrProgramElement_DEFINED
9 #define GrProgramElement_DEFINED
10 
11 #include "../private/SkTArray.h"
12 #include "SkRefCnt.h"
13 
14 class GrGpuResourceRef;
15 
44 class GrProgramElement : public SkNoncopyable {
45 public:
46  virtual ~GrProgramElement() {
47  // fRefCnt can be one when an effect is created statically using GR_CREATE_STATIC_EFFECT
48  SkASSERT((0 == fRefCnt || 1 == fRefCnt) && 0 == fPendingExecutions);
49  // Set to invalid values.
50  SkDEBUGCODE(fRefCnt = fPendingExecutions = -10;)
51  }
52 
53  void ref() const {
54  this->validate();
55  // Once the ref cnt reaches zero it should never be ref'ed again.
56  SkASSERT(fRefCnt > 0);
57  ++fRefCnt;
58  this->validate();
59  }
60 
61  void unref() const {
62  this->validate();
63  --fRefCnt;
64  if (0 == fRefCnt) {
65  this->notifyRefCntIsZero();
66  if (0 == fPendingExecutions) {
67  delete this;
68  return;
69  } else {
70  this->removeRefs();
71  }
72  }
73  this->validate();
74  }
75 
79  uint32_t getUniqueID() const { return fUniqueID; }
80 
81  void validate() const {
82 #ifdef SK_DEBUG
83  SkASSERT(fRefCnt >= 0);
84  SkASSERT(fPendingExecutions >= 0);
85  SkASSERT(fRefCnt + fPendingExecutions > 0);
86 #endif
87  }
88 
89 protected:
90  GrProgramElement() : fRefCnt(1), fPendingExecutions(0), fUniqueID(CreateUniqueID()) {}
91 
96  void addGpuResource(const GrGpuResourceRef* res) {
97  fGpuResources.push_back(res);
98  }
99 
100  void addPendingExecution() const {
101  this->validate();
102  SkASSERT(fRefCnt > 0);
103  if (0 == fPendingExecutions) {
104  this->addPendingIOs();
105  }
106  ++fPendingExecutions;
107  this->validate();
108  }
109 
110  void completedExecution() const {
111  this->validate();
112  --fPendingExecutions;
113  if (0 == fPendingExecutions) {
114  if (0 == fRefCnt) {
115  delete this;
116  return;
117  } else {
118  this->pendingIOComplete();
119  }
120  }
121  this->validate();
122  }
123 
124 private:
127  virtual void notifyRefCntIsZero() const = 0;
128 
129  static uint32_t CreateUniqueID();
130 
131  void removeRefs() const;
132  void addPendingIOs() const;
133  void pendingIOComplete() const;
134 
135  mutable int32_t fRefCnt;
136  // Count of deferred executions not yet issued to the 3D API.
137  mutable int32_t fPendingExecutions;
138  uint32_t fUniqueID;
139 
140  SkSTArray<4, const GrGpuResourceRef*, true> fGpuResources;
141 
142  // Only this class can access addPendingExecution() and completedExecution().
143  template <typename T> friend class GrPendingProgramElement;
144 
145  typedef SkNoncopyable INHERITED;
146 };
147 
148 #endif
void addGpuResource(const GrGpuResourceRef *res)
Subclasses registers their resources using this function.
Definition: GrProgramElement.h:96
virtual void notifyRefCntIsZero() const =0
This will be called when the ref cnt is zero.
Base class for GrProcessor.
Definition: GrProgramElement.h:44
This class is intended only for internal use in core Gr code.
Definition: GrGpuResourceRef.h:37
uint32_t getUniqueID() const
Gets an id that is unique for this GrProgramElement object.
Definition: GrProgramElement.h:79