OLD | NEW |
| (Empty) |
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 GrProgramElementRef_DEFINED | |
9 #define GrProgramElementRef_DEFINED | |
10 | |
11 #include "SkRefCnt.h" | |
12 #include "GrTypes.h" | |
13 | |
14 /** | |
15 * Helper for owning a GrProgramElement subclass and being able to convert a ref
to pending | |
16 * execution. It is like an SkAutoTUnref for program elements whose execution ca
n be deferred. Once | |
17 * in the pending execution state it is illegal to change the object that is own
ed by the | |
18 * GrProgramElementRef. Its destructor will either unref the GrProgramElement or
signal that | |
19 * the pending execution has completed, depending on whether convertToPendingExe
c() was called. | |
20 */ | |
21 template <typename T> class GrProgramElementRef : SkNoncopyable { | |
22 public: | |
23 GrProgramElementRef() : fOwnPendingExec(false), fObj(NULL) {}; | |
24 | |
25 // Adopts a ref from the caller. | |
26 explicit GrProgramElementRef(T* obj) : fOwnPendingExec(false), fObj(obj) {} | |
27 | |
28 // Adopts a ref from the caller. Do not call after convertToPendingExec. | |
29 void reset(T* obj) { | |
30 SkASSERT(!fOwnPendingExec); | |
31 SkSafeUnref(fObj); | |
32 fObj = obj; | |
33 } | |
34 | |
35 void convertToPendingExec() { | |
36 SkASSERT(!fOwnPendingExec); | |
37 fObj->convertRefToPendingExecution(); | |
38 fOwnPendingExec = true; | |
39 } | |
40 | |
41 // In the short term we need to support copying a GrProcessorStage and makin
g the copy own | |
42 // the same type of ref as the source. This function exists to support this.
TODO: Once | |
43 // GrDrawState and GrOptDrawState no longer share a base class they won't ha
ve to share | |
44 // GrProcessorStage and we can have GrOptDrawState always own pending execut
ions rather than | |
45 // refs on GrProgramElements. At that point we should be able to delete this
function. | |
46 // This function makes assumptions that are valid in the GrProcessorStage us
e case and should | |
47 // not be used elsewhere. | |
48 void initAndRef(const GrProgramElementRef& that) { | |
49 SkASSERT(!fObj); | |
50 SkASSERT(that.fObj); | |
51 if (that.fOwnPendingExec) { | |
52 SkASSERT(that.fObj->fPendingExecutions > 0); | |
53 that.fObj->fPendingExecutions++; | |
54 } else { | |
55 that.fObj->ref(); | |
56 } | |
57 this->fOwnPendingExec = that.fOwnPendingExec; | |
58 this->fObj = that.fObj; | |
59 } | |
60 | |
61 T* get() const { return fObj; } | |
62 operator T*() { return fObj; } | |
63 | |
64 /** If T is const, the type returned from operator-> will also be const. */ | |
65 typedef typename SkTConstType<typename SkAutoTUnref<T>::template BlockRef<T>
, | |
66 SkTIsConst<T>::value>::type BlockRefType; | |
67 | |
68 /** | |
69 * GrProgramElementRef assumes ownership of the ref and manages converting t
he ref to a | |
70 * pending execution. As a result, it is an error for the user to ref or unr
ef through | |
71 * GrProgramElementRef. Therefore operator-> returns BlockRef<T>*. | |
72 */ | |
73 BlockRefType *operator->() const { | |
74 return static_cast<BlockRefType*>(fObj); | |
75 } | |
76 | |
77 ~GrProgramElementRef() { | |
78 if (fObj) { | |
79 if (fOwnPendingExec) { | |
80 fObj->completedExecution(); | |
81 } else { | |
82 fObj->unref(); | |
83 } | |
84 } | |
85 } | |
86 | |
87 private: | |
88 bool fOwnPendingExec; | |
89 T* fObj; | |
90 | |
91 typedef SkNoncopyable INHERITED; | |
92 }; | |
93 #endif | |
OLD | NEW |