aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrPendingProgramElement.h
blob: ab1f4378930ed7fe4304f7c890a745779505af84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrPendingProgramElement_DEFINED
#define GrPendingProgramElement_DEFINED

#include "SkRefCnt.h"
#include "GrTypes.h"

/**
 * Helper for owning a pending execution on a GrProgramElement. Using this rather than ref allows
 * resources that are owned by the program element to be correctly tracked as having pending reads
 * and writes rather than refs.
 */
template <typename T> class GrPendingProgramElement : SkNoncopyable {
public:
    GrPendingProgramElement() : fObj(nullptr) { }

    // Adds a pending execution on obj.
    explicit GrPendingProgramElement(T* obj) : fObj(obj)  {
        if (obj) {
            obj->addPendingExecution();
        }
    }

    void reset(T* obj) {
        if (obj) {
            obj->addPendingExecution();
        }
        if (fObj) {
            fObj->completedExecution();
        }
        fObj = obj;
    }

    T* get() const { return fObj; }
    operator T*() { return fObj; }

    T *operator->() const { return fObj; }

    ~GrPendingProgramElement() {
        if (fObj) {
            fObj->completedExecution();
        }
    }

private:
    T*   fObj;

    typedef SkNoncopyable INHERITED;
};
#endif