rsObjectBase.h revision fb6b614bcea88a587a7ea4530be45ff0ffa0210e
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_RS_OBJECT_BASE_H
18#define ANDROID_RS_OBJECT_BASE_H
19
20#include "rsUtils.h"
21
22
23namespace android {
24namespace renderscript {
25
26class Context;
27class OStream;
28
29// An element is a group of Components that occupies one cell in a structure.
30class ObjectBase
31{
32public:
33    ObjectBase(Context *rsc);
34    virtual ~ObjectBase();
35
36    void incSysRef() const;
37    bool decSysRef() const;
38
39    void incUserRef() const;
40    bool decUserRef() const;
41    bool zeroUserRef() const;
42
43    const char * getName() const {
44        return mName.string();
45    }
46    void setName(const char *);
47    void setName(const char *, uint32_t len);
48
49    Context * getContext() const {return mRSC;}
50    void setContext(Context *);
51
52    static void zeroAllUserRef(Context *rsc);
53    static void dumpAll(Context *rsc);
54
55    virtual void dumpLOGV(const char *prefix) const;
56    virtual void serialize(OStream *stream) const = 0;
57    virtual A3DClassID getClassId() const = 0;
58
59protected:
60    const char *mAllocFile;
61    uint32_t mAllocLine;
62    Context *mRSC;
63
64private:
65    void add() const;
66    void remove() const;
67
68    bool checkDelete() const;
69
70    String8 mName;
71    mutable int32_t mSysRefCount;
72    mutable int32_t mUserRefCount;
73
74    mutable const ObjectBase * mPrev;
75    mutable const ObjectBase * mNext;
76};
77
78template<class T>
79class ObjectBaseRef
80{
81public:
82    ObjectBaseRef() {
83        mRef = NULL;
84    }
85
86    ObjectBaseRef(const ObjectBaseRef &ref) {
87        mRef = ref.get();
88        if (mRef) {
89            mRef->incSysRef();
90        }
91    }
92
93    ObjectBaseRef(T *ref) {
94        mRef = ref;
95        if (mRef) {
96            ref->incSysRef();
97        }
98    }
99
100    ~ObjectBaseRef() {
101        clear();
102    }
103
104    void set(T *ref) {
105        if (mRef != ref) {
106            clear();
107            mRef = ref;
108            if (mRef) {
109                ref->incSysRef();
110            }
111        }
112    }
113
114    void set(const ObjectBaseRef &ref) {
115        set(ref.mRef);
116    }
117
118    void clear() {
119        if (mRef) {
120            mRef->decSysRef();
121        }
122        mRef = NULL;
123    }
124
125    inline T * get() const {
126        return mRef;
127    }
128
129    inline T * operator-> () const {
130        return mRef;
131    }
132
133protected:
134    T * mRef;
135
136};
137
138
139}
140}
141
142#endif //ANDROID_RS_OBJECT_BASE_H
143
144