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 LIBGLESV2_REFCOUNTOBJECT_H_
13#define LIBGLESV2_REFCOUNTOBJECT_H_
14
15#include <cstddef>
16
17#define GL_APICALL
18#include <GLES2/gl2.h>
19
20#include "common/debug.h"
21
22namespace gl
23{
24
25class RefCountObject
26{
27  public:
28    explicit RefCountObject(GLuint id);
29    virtual ~RefCountObject();
30
31    virtual void addRef() const;
32    virtual void release() const;
33
34    GLuint id() const { return mId; }
35
36  private:
37    GLuint mId;
38
39    mutable std::size_t mRefCount;
40};
41
42class RefCountObjectBindingPointer
43{
44  protected:
45    RefCountObjectBindingPointer() : mObject(NULL) { }
46    ~RefCountObjectBindingPointer() { ASSERT(mObject == NULL); } // Objects have to be released before the resource manager is destroyed, so they must be explicitly cleaned up.
47
48    void set(RefCountObject *newObject);
49    RefCountObject *get() const { return mObject; }
50
51  public:
52    GLuint id() const { return (mObject != NULL) ? mObject->id() : 0; }
53    bool operator ! () const { return (get() == NULL); }
54
55  private:
56    RefCountObject *mObject;
57};
58
59template <class ObjectType>
60class BindingPointer : public RefCountObjectBindingPointer
61{
62  public:
63    void set(ObjectType *newObject) { RefCountObjectBindingPointer::set(newObject); }
64    ObjectType *get() const { return static_cast<ObjectType*>(RefCountObjectBindingPointer::get()); }
65    ObjectType *operator -> () const { return get(); }
66};
67
68}
69
70#endif   // LIBGLESV2_REFCOUNTOBJECT_H_
71