2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrFakeRefObj_DEFINED
9 #define GrFakeRefObj_DEFINED
12 #include "gl/GrGLInterface.h"
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
20 // Each object also gets a unique globally identifying ID
21 class GrFakeRefObj : SkNoncopyable {
26 , fMarkedForDeletion(false)
29 // source for globally unique IDs - 0 is reserved!
30 static int fNextID = 0;
34 virtual ~GrFakeRefObj() {};
38 if (fHighRefCount < fRef) {
44 GrAlwaysAssert(fRef >= 0);
46 // often in OpenGL a given object may still be in use when the
47 // delete call is made. In these cases the object is marked
48 // for deletion and then freed when it is no longer in use
49 if (0 == fRef && fMarkedForDeletion) {
53 int getRefCount() const { return fRef; }
54 int getHighRefCount() const { return fHighRefCount; }
56 GrGLuint getID() const { return fID; }
58 void setMarkedForDeletion() { fMarkedForDeletion = true; }
59 bool getMarkedForDeletion() const { return fMarkedForDeletion; }
61 bool getDeleted() const { return fDeleted; }
63 // The deleteAction fires if the object has been marked for deletion but
64 // couldn't be deleted earlier due to refs
65 virtual void deleteAction() {
71 int fRef; // ref count
72 int fHighRefCount; // high water mark of the ref count
73 GrGLuint fID; // globally unique ID
74 bool fMarkedForDeletion;
75 // The deleted flag is only set when OpenGL thinks the object is deleted
76 // It is obviously still allocated w/in this framework
79 // setDeleted should only ever appear in the deleteAction method!
80 void setDeleted() { fDeleted = true; }
83 ////////////////////////////////////////////////////////////////////////////////
84 // Each class derived from GrFakeRefObj should use this macro to add a
85 // factory creation entry point. This entry point is used by the GrGLDebug
86 // object to instantiate the various objects
87 // all globally unique IDs
88 #define GR_DEFINE_CREATOR(className) \
90 static GrFakeRefObj *create ## className() { \
91 return SkNEW(className); \
94 #endif // GrFakeRefObj_DEFINED