1// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Object.hpp: Defines the Object base class that provides
16// lifecycle support for GL objects using the traditional BindObject scheme, but
17// that need to be reference counted for correct cross-context deletion.
18
19#ifndef gl_Object_hpp
20#define gl_Object_hpp
21
22#include "common/debug.h"
23#include "Common/MutexLock.hpp"
24
25#include <set>
26
27typedef unsigned int GLuint;
28
29namespace gl
30{
31
32class [[clang::lto_visibility_public]] Object
33{
34public:
35 Object();
36
37 virtual void addRef();
38 virtual void release();
39
40 inline bool hasSingleReference() const
41 {
42 return referenceCount == 1;
43 }
44
45protected:
46 virtual ~Object();
47
48 int dereference();
49 void destroy();
50
51 volatile int referenceCount;
52
53#ifndef NDEBUG
54public:
55 static sw::MutexLock instances_mutex;
56 static std::set<Object*> instances; // For leak checking
57#endif
58};
59
60class NamedObject : public Object
61{
62public:
63 explicit NamedObject(GLuint name);
64 virtual ~NamedObject();
65
66 const GLuint name;
67};
68
69template<class ObjectType>
70class BindingPointer
71{
72public:
73 BindingPointer() : object(nullptr) { }
74
75 BindingPointer(const BindingPointer<ObjectType> &other) : object(nullptr)
76 {
77 operator=(other.object);
78 }
79
80 ~BindingPointer()
81 {
82 ASSERT(!object); // Objects have to be released before the resource manager is destroyed, so they must be explicitly cleaned up. Assign null to all binding pointers to make the reference count go to zero.
83 }
84
85 ObjectType *operator=(ObjectType *newObject)
86 {
87 if(newObject) newObject->addRef();
88 if(object) object->release();
89
90 object = newObject;
91
92 return object;
93 }
94
95 ObjectType *operator=(const BindingPointer<ObjectType> &other)
96 {
97 return operator=(other.object);
98 }
99
100 operator ObjectType*() const { return object; }
101 ObjectType *operator->() const { return object; }
102 GLuint name() const { return object ? object->name : 0; }
103 bool operator!() const { return !object; }
104
105private:
106 ObjectType *object;
107};
108
109}
110
111#endif // gl_Object_hpp
112