• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
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.
5 //
6 
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.)
11 
12 #ifndef COMMON_REFCOUNTOBJECT_H_
13 #define COMMON_REFCOUNTOBJECT_H_
14 
15 #include <cstddef>
16 
17 #define GL_APICALL
18 #include <GLES2/gl2.h>
19 
20 #include "common/debug.h"
21 
22 class RefCountObject
23 {
24   public:
25     explicit RefCountObject(GLuint id);
26     virtual ~RefCountObject();
27 
28     virtual void addRef() const;
29     virtual void release() const;
30 
id()31     GLuint id() const { return mId; }
32 
33   private:
34     GLuint mId;
35 
36     mutable std::size_t mRefCount;
37 };
38 
39 class RefCountObjectBindingPointer
40 {
41   protected:
RefCountObjectBindingPointer()42     RefCountObjectBindingPointer() : mObject(NULL) { }
~RefCountObjectBindingPointer()43     ~RefCountObjectBindingPointer() { ASSERT(mObject == NULL); } // Objects have to be released before the resource manager is destroyed, so they must be explicitly cleaned up.
44 
45     void set(RefCountObject *newObject);
get()46     RefCountObject *get() const { return mObject; }
47 
48   public:
id()49     GLuint id() const { return (mObject != NULL) ? mObject->id() : 0; }
50     bool operator ! () const { return (get() == NULL); }
51 
52   private:
53     RefCountObject *mObject;
54 };
55 
56 template <class ObjectType>
57 class BindingPointer : public RefCountObjectBindingPointer
58 {
59   public:
set(ObjectType * newObject)60     void set(ObjectType *newObject) { RefCountObjectBindingPointer::set(newObject); }
get()61     ObjectType *get() const { return static_cast<ObjectType*>(RefCountObjectBindingPointer::get()); }
62     ObjectType *operator -> () const { return get(); }
63 };
64 
65 #endif   // COMMON_REFCOUNTOBJECT_H_
66