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 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
41    // In the short term we need to support copying a GrProcessorStage and making 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 have to share
44    // GrProcessorStage and we can have GrOptDrawState always own pending executions 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 use 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 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() {
78        if (fObj) {
79            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
94