blob: ecc802317b8170cb2ef4e140e24cc075c6eae2d1 [file] [log] [blame]
bsalomon95740982014-09-04 13:12:37 -07001/*
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 can be deferred. Once
17 * in the pending execution state it is illegal to change the object that is owned by the
18 * GrProgramElementRef. Its destructor will either unref the GrProgramElement or signal that
19 * the pending execution has completed, depending on whether convertToPendingExec() was called.
20 */
21template <typename T> class GrProgramElementRef : SkNoncopyable {
22public:
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
joshualittb0a8a372014-09-23 09:50:21 -070041 // In the short term we need to support copying a GrProcessorStage and making the copy own
bsalomon655ad122014-09-08 12:06:20 -070042 // 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 have to share
joshualittb0a8a372014-09-23 09:50:21 -070044 // GrProcessorStage and we can have GrOptDrawState always own pending executions rather than
bsalomon655ad122014-09-08 12:06:20 -070045 // refs on GrProgramElements. At that point we should be able to delete this function.
joshualittb0a8a372014-09-23 09:50:21 -070046 // This function makes assumptions that are valid in the GrProcessorStage use case and should
bsalomon655ad122014-09-08 12:06:20 -070047 // 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
bsalomon95740982014-09-04 13:12:37 -070061 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. */
bsalomon2a9ca782014-09-05 14:27:43 -070065 typedef typename SkTConstType<typename SkAutoTUnref<T>::template BlockRef<T>,
bsalomon95740982014-09-04 13:12:37 -070066 SkTIsConst<T>::value>::type BlockRefType;
67
68 /**
69 * GrProgramElementRef assumes ownership of the ref and manages converting the ref to a
70 * pending execution. As a result, it is an error for the user to ref or unref through
71 * GrProgramElementRef. Therefore operator-> returns BlockRef<T>*.
72 */
73 BlockRefType *operator->() const {
74 return static_cast<BlockRefType*>(fObj);
75 }
76
77 ~GrProgramElementRef() {
bsalomon49f085d2014-09-05 13:34:00 -070078 if (fObj) {
bsalomon95740982014-09-04 13:12:37 -070079 if (fOwnPendingExec) {
80 fObj->completedExecution();
81 } else {
82 fObj->unref();
83 }
84 }
85 }
86
87private:
88 bool fOwnPendingExec;
89 T* fObj;
90
91 typedef SkNoncopyable INHERITED;
92};
93#endif