GrFakeRefObj.h revision dd743fefad9764ad86d7f69deec32e9a3b5de47f
1
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef GrFakeRefObj_DEFINED
10#define GrFakeRefObj_DEFINED
11
12#include "gl/GrGLInterface.h"
13
14////////////////////////////////////////////////////////////////////////////////
15// This object is used to track the OpenGL objects. We don't use real
16// reference counting (i.e., we don't free the objects when their ref count
17// goes to 0) so that we can detect invalid memory accesses. The refs we
18// are tracking in this class are actually OpenGL's references to the objects
19// not "ours"
20// Each object also gets a unique globally identifying ID
21class GrFakeRefObj {
22public:
23    GrFakeRefObj()
24        : fRef(0)
25        , fHighRefCount(0)
26        , fMarkedForDeletion(false)
27        , fDeleted(false) {
28
29        static int fNextID = 0;  // source for globally unique IDs - 0 is reserved!
30
31        fID = ++fNextID;
32    }
33    virtual ~GrFakeRefObj() {};
34
35    void ref() {
36        fRef++;
37        if (fHighRefCount < fRef) {
38            fHighRefCount = fRef;
39        }
40    }
41    void unref() {
42        fRef--;
43        GrAlwaysAssert(fRef >= 0);
44
45        // often in OpenGL a given object may still be in use when the
46        // delete call is made. In these cases the object is marked
47        // for deletion and then freed when it is no longer in use
48        if (0 == fRef && fMarkedForDeletion) {
49            this->deleteAction();
50        }
51    }
52    int getRefCount() const { return fRef; }
53    int getHighRefCount() const { return fHighRefCount; }
54
55    GrGLuint getID() const { return fID; }
56
57    void setMarkedForDeletion() { fMarkedForDeletion = true; }
58    bool getMarkedForDeletion() const { return fMarkedForDeletion; }
59
60    bool getDeleted() const     { return fDeleted; }
61
62    // The deleteAction fires if the object has been marked for deletion but
63    // couldn't be deleted earlier due to refs
64    virtual void deleteAction() {
65        this->setDeleted();
66    }
67
68protected:
69private:
70    int         fRef;
71    int         fHighRefCount;      // high water mark of the ref count
72    GrGLuint    fID;
73    bool        fMarkedForDeletion;
74    // The deleted flag is only set when OpenGL thinks the object is deleted
75    // It is obviously still allocated w/in this framework
76    bool        fDeleted;
77
78    // setDeleted should only ever appear in the deleteAction method!
79    void setDeleted()           { fDeleted = true; }
80};
81
82////////////////////////////////////////////////////////////////////////////////
83// Each class derived from GrFakeRefObj should use this macro to add a
84// factory creation entry point. This entry point is used by the GrGLDebug
85// object to instantiate the various objects
86// all globally unique IDs
87#define GR_DEFINE_CREATOR(className)                        \
88    public:                                                 \
89    static GrFakeRefObj *create ## className() {            \
90        return SkNEW(className);                            \
91    }
92
93#endif // GrFakeRefObj_DEFINED
94