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 <cstddef>
     16 
     17 #include <GLES3/gl3.h>
     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 
     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:
     42     RefCountObjectBindingPointer() : mObject(NULL) { }
     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);
     46     RefCountObject *get() const { return mObject; }
     47 
     48   public:
     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:
     60     void set(ObjectType *newObject) { RefCountObjectBindingPointer::set(newObject); }
     61     ObjectType *get() const { return static_cast<ObjectType*>(RefCountObjectBindingPointer::get()); }
     62     ObjectType *operator->() const { return get(); }
     63 };
     64 
     65 template <class ObjectType>
     66 class FramebufferTextureBindingPointer : public RefCountObjectBindingPointer
     67 {
     68 public:
     69     FramebufferTextureBindingPointer() : mType(GL_NONE), mMipLevel(0), mLayer(0) { }
     70 
     71     void set(ObjectType *newObject, GLenum type, GLint mipLevel, GLint layer)
     72     {
     73         RefCountObjectBindingPointer::set(newObject);
     74         mType = type;
     75         mMipLevel = mipLevel;
     76         mLayer = layer;
     77     }
     78 
     79     ObjectType *get() const { return static_cast<ObjectType*>(RefCountObjectBindingPointer::get()); }
     80     ObjectType *operator->() const { return get(); }
     81 
     82     GLenum type() const { return mType; }
     83     GLint mipLevel() const { return mMipLevel; }
     84     GLint layer() const { return mLayer; }
     85 
     86 private:
     87     GLenum mType;
     88     GLint mMipLevel;
     89     GLint mLayer;
     90 };
     91 
     92 template <class ObjectType>
     93 class OffsetBindingPointer : public RefCountObjectBindingPointer
     94 {
     95   public:
     96     OffsetBindingPointer() : mOffset(0), mSize(0) { }
     97 
     98     void set(ObjectType *newObject)
     99     {
    100         RefCountObjectBindingPointer::set(newObject);
    101         mOffset = 0;
    102         mSize = 0;
    103     }
    104 
    105     void set(ObjectType *newObject, GLintptr offset, GLsizeiptr size)
    106     {
    107         RefCountObjectBindingPointer::set(newObject);
    108         mOffset = offset;
    109         mSize = size;
    110     }
    111 
    112     GLintptr getOffset() const { return mOffset; }
    113     GLsizeiptr getSize() const { return mSize; }
    114 
    115     ObjectType *get() const { return static_cast<ObjectType*>(RefCountObjectBindingPointer::get()); }
    116     ObjectType *operator->() const { return get(); }
    117 
    118   private:
    119     GLintptr mOffset;
    120     GLsizeiptr mSize;
    121 };
    122 
    123 #endif   // COMMON_REFCOUNTOBJECT_H_
    124