Home | History | Annotate | Download | only in common
      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 "common/debug.h"
     16 
     17 #include "angle_gl.h"
     18 
     19 #include <cstddef>
     20 
     21 class RefCountObject
     22 {
     23   public:
     24     explicit RefCountObject(GLuint id);
     25     virtual ~RefCountObject();
     26 
     27     virtual void addRef() const;
     28     virtual void release() const;
     29 
     30     GLuint id() const { return mId; }
     31 
     32   private:
     33     GLuint mId;
     34 
     35     mutable std::size_t mRefCount;
     36 };
     37 
     38 class RefCountObjectBindingPointer
     39 {
     40   protected:
     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.
     43 
     44     void set(RefCountObject *newObject);
     45     RefCountObject *get() const { return mObject; }
     46 
     47   public:
     48     GLuint id() const { return (mObject != NULL) ? mObject->id() : 0; }
     49     bool operator!() const { return (get() == NULL); }
     50 
     51   private:
     52     RefCountObject *mObject;
     53 };
     54 
     55 template <class ObjectType>
     56 class BindingPointer : public RefCountObjectBindingPointer
     57 {
     58   public:
     59     void set(ObjectType *newObject) { RefCountObjectBindingPointer::set(newObject); }
     60     ObjectType *get() const { return static_cast<ObjectType*>(RefCountObjectBindingPointer::get()); }
     61     ObjectType *operator->() const { return get(); }
     62 };
     63 
     64 template <class ObjectType>
     65 class OffsetBindingPointer : public RefCountObjectBindingPointer
     66 {
     67   public:
     68     OffsetBindingPointer() : mOffset(0), mSize(0) { }
     69 
     70     void set(ObjectType *newObject)
     71     {
     72         RefCountObjectBindingPointer::set(newObject);
     73         mOffset = 0;
     74         mSize = 0;
     75     }
     76 
     77     void set(ObjectType *newObject, GLintptr offset, GLsizeiptr size)
     78     {
     79         RefCountObjectBindingPointer::set(newObject);
     80         mOffset = offset;
     81         mSize = size;
     82     }
     83 
     84     GLintptr getOffset() const { return mOffset; }
     85     GLsizeiptr getSize() const { return mSize; }
     86 
     87     ObjectType *get() const { return static_cast<ObjectType*>(RefCountObjectBindingPointer::get()); }
     88     ObjectType *operator->() const { return get(); }
     89 
     90   private:
     91     GLintptr mOffset;
     92     GLsizeiptr mSize;
     93 };
     94 
     95 #endif   // COMMON_REFCOUNTOBJECT_H_
     96