Home | History | Annotate | Download | only in common
      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 
     24 #include <set>
     25 
     26 typedef unsigned int GLuint;
     27 
     28 namespace gl
     29 {
     30 
     31 class Object
     32 {
     33 public:
     34 	Object();
     35 
     36 	virtual void addRef();
     37 	virtual void release();
     38 
     39 	inline bool hasSingleReference() const
     40 	{
     41 		return referenceCount == 1;
     42 	}
     43 
     44 protected:
     45 	virtual ~Object();
     46 
     47 	int dereference();
     48 	void destroy();
     49 
     50 	volatile int referenceCount;
     51 
     52 #ifndef NDEBUG
     53 public:
     54 	static std::set<Object*> instances;   // For leak checking
     55 #endif
     56 };
     57 
     58 class NamedObject : public Object
     59 {
     60 public:
     61 	explicit NamedObject(GLuint name);
     62 	virtual ~NamedObject();
     63 
     64 	const GLuint name;
     65 };
     66 
     67 template<class ObjectType>
     68 class BindingPointer
     69 {
     70 public:
     71 	BindingPointer() : object(nullptr) { }
     72 
     73 	BindingPointer(const BindingPointer<ObjectType> &other) : object(nullptr)
     74 	{
     75 		operator=(other.object);
     76 	}
     77 
     78 	~BindingPointer()
     79 	{
     80 		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.
     81 	}
     82 
     83 	ObjectType *operator=(ObjectType *newObject)
     84 	{
     85 		if(newObject) newObject->addRef();
     86 		if(object) object->release();
     87 
     88 		object = newObject;
     89 
     90 		return object;
     91 	}
     92 
     93 	ObjectType *operator=(const BindingPointer<ObjectType> &other)
     94 	{
     95 		return operator=(other.object);
     96 	}
     97 
     98 	operator ObjectType*() const { return object; }
     99 	ObjectType *operator->() const { return object; }
    100 	GLuint name() const { return object ? object->name : 0; }
    101 	bool operator!() const { return !object; }
    102 
    103 private:
    104 	ObjectType *object;
    105 };
    106 
    107 }
    108 
    109 #endif   // gl_Object_hpp
    110