aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/gpu/gl/debug/GrFakeRefObj.h
blob: accb5ce62152effa6b05f7ad10e7583af92ac782 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrFakeRefObj_DEFINED
#define GrFakeRefObj_DEFINED

#include <atomic>
#include "SkTypes.h"
#include "gl/GrGLInterface.h"

////////////////////////////////////////////////////////////////////////////////
// This object is used to track the OpenGL objects. We don't use real
// reference counting (i.e., we don't free the objects when their ref count
// goes to 0) so that we can detect invalid memory accesses. The refs we
// are tracking in this class are actually OpenGL's references to the objects
// not "ours"
// Each object also gets a unique globally identifying ID
class GrFakeRefObj : SkNoncopyable {
public:
    GrFakeRefObj()
        : fRef(0)
        , fMarkedForDeletion(false)
        , fDeleted(false) {

        // source for globally unique IDs - 0 is reserved!
        static std::atomic<int> fNextID{0};

        fID = ++fNextID;
    }
    virtual ~GrFakeRefObj() {}

    void ref() {
        fRef++;
    }
    void unref() {
        fRef--;
        GrAlwaysAssert(fRef >= 0);

        // often in OpenGL a given object may still be in use when the
        // delete call is made. In these cases the object is marked
        // for deletion and then freed when it is no longer in use
        if (0 == fRef && fMarkedForDeletion) {
            this->deleteAction();
        }
    }
    int getRefCount() const             { return fRef; }

    GrGLuint getID() const              { return fID; }

    void setMarkedForDeletion()         { fMarkedForDeletion = true; }
    bool getMarkedForDeletion() const   { return fMarkedForDeletion; }

    bool getDeleted() const             { return fDeleted; }

    // The deleteAction fires if the object has been marked for deletion but
    // couldn't be deleted earlier due to refs
    virtual void deleteAction() {
        this->setDeleted();
    }

protected:
private:
    int         fRef;               // ref count
    GrGLuint    fID;                // globally unique ID
    bool        fMarkedForDeletion;
    // The deleted flag is only set when OpenGL thinks the object is deleted
    // It is obviously still allocated w/in this framework
    bool        fDeleted;

    // setDeleted should only ever appear in the deleteAction method!
    void setDeleted()                   { fDeleted = true; }
};

////////////////////////////////////////////////////////////////////////////////
// Each class derived from GrFakeRefObj should use this macro to add a
// factory creation entry point. This entry point is used by the GrGLDebug
// object to instantiate the various objects
// all globally unique IDs
#define GR_DEFINE_CREATOR(className) \
public:                              \
    static GrFakeRefObj *create##className() { return new className; }

#endif // GrFakeRefObj_DEFINED