2 // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
7 // RefCountObject.h: Defines the gl::RefCountObject base class that provides
8 // lifecycle support for GL objects using the traditional BindObject scheme, but
9 // that need to be reference counted for correct cross-context deletion.
10 // (Concretely, textures, buffers and renderbuffers.)
12 #ifndef COMMON_REFCOUNTOBJECT_H_
13 #define COMMON_REFCOUNTOBJECT_H_
15 #include "common/debug.h"
24 explicit RefCountObject(GLuint id);
25 virtual ~RefCountObject();
27 virtual void addRef() const;
28 virtual void release() const;
30 GLuint id() const { return mId; }
35 mutable std::size_t mRefCount;
38 class RefCountObjectBindingPointer
41 RefCountObjectBindingPointer() : mObject(NULL) { }
42 ~RefCountObjectBindingPointer() { ASSERT(mObject == NULL); } // Objects have to be released before the resource manager is destroyed, so they must be explicitly cleaned up.
44 void set(RefCountObject *newObject);
45 RefCountObject *get() const { return mObject; }
48 GLuint id() const { return (mObject != NULL) ? mObject->id() : 0; }
49 bool operator!() const { return (get() == NULL); }
52 RefCountObject *mObject;
55 template <class ObjectType>
56 class BindingPointer : public RefCountObjectBindingPointer
59 void set(ObjectType *newObject) { RefCountObjectBindingPointer::set(newObject); }
60 ObjectType *get() const { return static_cast<ObjectType*>(RefCountObjectBindingPointer::get()); }
61 ObjectType *operator->() const { return get(); }
64 template <class ObjectType>
65 class OffsetBindingPointer : public RefCountObjectBindingPointer
68 OffsetBindingPointer() : mOffset(0), mSize(0) { }
70 void set(ObjectType *newObject)
72 RefCountObjectBindingPointer::set(newObject);
77 void set(ObjectType *newObject, GLintptr offset, GLsizeiptr size)
79 RefCountObjectBindingPointer::set(newObject);
84 GLintptr getOffset() const { return mOffset; }
85 GLsizeiptr getSize() const { return mSize; }
87 ObjectType *get() const { return static_cast<ObjectType*>(RefCountObjectBindingPointer::get()); }
88 ObjectType *operator->() const { return get(); }
95 #endif // COMMON_REFCOUNTOBJECT_H_