OSDN Git Service

da19dda249fd54c86c95eecf5028a4fa1c849081
[android-x86/external-swiftshader.git] / src / Radiance / libRAD / RefCountObject.h
1 // SwiftShader Software Renderer
2 //
3 // Copyright(c) 2005-2012 TransGaming Inc.
4 //
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
6 // transcribed, stored in a retrieval system, translated into any human or computer
7 // language by any means, or disclosed to third parties without the explicit written
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9 // or implied, including but not limited to any patent rights, are granted to you.
10 //
11
12 // RefCountObject.h: Defines the RefCountObject base class that provides
13 // lifecycle support for GL objects using the traditional BindObject scheme, but
14 // that need to be reference counted for correct cross-context deletion.
15 // (Concretely, textures, buffers and renderbuffers.)
16
17 #ifndef LIBGLESV2_REFCOUNTOBJECT_H_
18 #define LIBGLESV2_REFCOUNTOBJECT_H_
19
20 #include "common/debug.h"
21
22 #define GL_APICALL
23 #include <GLES2/gl2.h>
24
25 #include <cstddef>
26
27 namespace rad
28 {
29
30 class RefCountObject
31 {
32   public:
33     explicit RefCountObject(GLuint id);
34     virtual ~RefCountObject();
35
36     virtual void addRef();
37         virtual void release();
38
39     GLuint id() const {return mId;}
40     
41   private:
42     GLuint mId;
43
44     volatile int referenceCount;
45 };
46
47 class RefCountObjectBindingPointer
48 {
49   protected:
50     RefCountObjectBindingPointer() : mObject(NULL) { }
51     ~RefCountObjectBindingPointer() { ASSERT(mObject == NULL); } // Objects have to be released before the resource manager is destroyed, so they must be explicitly cleaned up.
52
53     void set(RefCountObject *newObject);
54     RefCountObject *get() const { return mObject; }
55
56   public:
57     GLuint id() const { return (mObject != NULL) ? mObject->id() : 0; }
58     bool operator ! () const { return (get() == NULL); }
59
60   private:
61     RefCountObject *mObject;
62 };
63
64 template<class ObjectType>
65 class BindingPointer : public RefCountObjectBindingPointer
66 {
67   public:
68     void set(ObjectType *newObject) { RefCountObjectBindingPointer::set(newObject); }
69     ObjectType *get() const { return static_cast<ObjectType*>(RefCountObjectBindingPointer::get()); }
70     ObjectType *operator -> () const { return get(); }
71 };
72
73 }
74
75 #endif   // LIBGLESV2_REFCOUNTOBJECT_H_