OSDN Git Service

b69b0c7071c8db3410641dd17223cd1bd014f2f1
[android-x86/external-swiftshader.git] / src / Radiance / libRAD / RefCountObject.cpp
1 // SwiftShader Software Renderer
2 //
3 // Copyright(c) 2005-2012 TransGaming Inc.
4 //
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
6 // transcribed, stored in a retrieval system, translated into any human or computer
7 // language by any means, or disclosed to third parties without the explicit written
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9 // or implied, including but not limited to any patent rights, are granted to you.
10 //
11
12 // RefCountObject.cpp: Defines the RefCountObject base class that provides
13 // lifecycle support for GL objects using the traditional BindObject scheme, but
14 // that need to be reference counted for correct cross-context deletion.
15 // (Concretely, textures, buffers and renderbuffers.)
16
17 #include "RefCountObject.h"
18
19 #include "Common/Thread.hpp"
20
21 namespace rad
22 {
23
24 RefCountObject::RefCountObject(GLuint id)
25 {
26     mId = id;
27     
28         referenceCount = 0;
29 }
30
31 RefCountObject::~RefCountObject()
32 {
33     ASSERT(referenceCount == 0);
34 }
35
36 void RefCountObject::addRef()
37 {
38         sw::atomicIncrement(&referenceCount);
39 }
40
41 void RefCountObject::release()
42 {
43     ASSERT(referenceCount > 0);
44
45     if(referenceCount > 0)
46         {
47                 sw::atomicDecrement(&referenceCount);
48         }
49
50         if(referenceCount == 0)
51         {
52                 delete this;
53         }
54 }
55
56 void RefCountObjectBindingPointer::set(RefCountObject *newObject)
57 {
58     // addRef first in case newObject == mObject and this is the last reference to it.
59     if(newObject != NULL) newObject->addRef();
60     if(mObject != NULL) mObject->release();
61
62     mObject = newObject;
63 }
64
65 }