1e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project/*
2e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project * Copyright (C) 2009 The Android Open Source Project
3e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project *
4e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project * you may not use this file except in compliance with the License.
6e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project * You may obtain a copy of the License at
7e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project *
8e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project *
10e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project * See the License for the specific language governing permissions and
14e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project * limitations under the License.
15e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project */
16e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
17e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project#ifndef ANDROID_RS_OBJECT_BASE_H
18e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project#define ANDROID_RS_OBJECT_BASE_H
19e1036abe5e8ef713e4091ee36907fe82343d2ed1Nick Kralevich
20e1036abe5e8ef713e4091ee36907fe82343d2ed1Nick Kralevich#include "rsUtils.h"
21e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project#include "rsDefines.h"
22e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
23e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project#define RS_OBJECT_DEBUG 0
24e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
25e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project#include <utils/CallStack.h>
26e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
27e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectnamespace android {
28e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectnamespace renderscript {
29e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
30e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectclass Context;
31e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectclass OStream;
32e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
33e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project// An element is a group of Components that occupies one cell in a structure.
34e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectclass ObjectBase {
35e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectpublic:
36e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    ObjectBase(Context *rsc);
37e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
38e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    void incSysRef() const;
39e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    bool decSysRef() const;
40e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
41e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    void incUserRef() const;
42e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    bool decUserRef() const;
43e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    bool zeroUserRef() const;
44e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
45e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    static bool checkDelete(const ObjectBase *);
46e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
47e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    const char * getName() const {
48e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        return mName.string();
49e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
50e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    void setName(const char *);
51e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    void setName(const char *, uint32_t len);
52e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
53e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    Context * getContext() const {return mRSC;}
54e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    virtual bool freeChildren();
55e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
56e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    static void zeroAllUserRef(Context *rsc);
57e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    static void freeAllChildren(Context *rsc);
58e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    static void dumpAll(Context *rsc);
59e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
60e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    virtual void dumpLOGV(const char *prefix) const;
61e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    virtual void serialize(Context *rsc, OStream *stream) const = 0;
62e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    virtual RsA3DClassID getClassId() const = 0;
6332c21d90edce22f4c0b55effc527403a51be70f1Chia-chi Yeh
64e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    static bool isValid(const Context *rsc, const ObjectBase *obj);
65e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
66e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    // The async lock is taken during object creation in non-rs threads
67e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    // and object deletion in the rs thread.
68e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    static void asyncLock();
69e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    static void asyncUnlock();
70e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
71e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectprotected:
72e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    // Called inside the async lock for any object list management that is
73e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    // necessary in derived classes.
74e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    virtual void preDestroy() const;
75e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
76e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    Context *mRSC;
77e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    virtual ~ObjectBase();
78e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
79e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectprivate:
80e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    static pthread_mutex_t gObjectInitMutex;
81e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
82e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    void add() const;
83e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    void remove() const;
84e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
85e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    String8 mName;
86e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    mutable int32_t mSysRefCount;
87e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    mutable int32_t mUserRefCount;
88e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
89e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    mutable const ObjectBase * mPrev;
90e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    mutable const ObjectBase * mNext;
91e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
92e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project#if RS_OBJECT_DEBUG
93e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    CallStack mStack;
94e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project#endif
95e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
96e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project};
97e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
98e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projecttemplate<class T>
99e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectclass ObjectBaseRef {
100e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectpublic:
101e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    ObjectBaseRef() {
102e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        mRef = NULL;
103e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
104e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
105e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    ObjectBaseRef(const ObjectBaseRef &ref) {
106e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        mRef = ref.get();
107e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        if (mRef) {
108e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project            mRef->incSysRef();
109e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        }
110e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
111e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
112e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    ObjectBaseRef(T *ref) {
113e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        mRef = ref;
114e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        if (mRef) {
115e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project            ref->incSysRef();
116e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        }
117e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
118e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
119e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    ObjectBaseRef & operator= (const ObjectBaseRef &ref) {
120e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        if (&ref != this) {
121e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project            set(ref);
122e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        }
123e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        return *this;
124e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
125e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
126e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    ~ObjectBaseRef() {
127e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        clear();
128e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
129e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
130e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    void set(T *ref) {
131e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        if (mRef != ref) {
132e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project            clear();
133e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project            mRef = ref;
134e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project            if (mRef) {
135e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project                ref->incSysRef();
136e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project            }
137e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        }
138e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
139e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
140e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    void set(const ObjectBaseRef &ref) {
141e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        set(ref.mRef);
14232c21d90edce22f4c0b55effc527403a51be70f1Chia-chi Yeh    }
14332c21d90edce22f4c0b55effc527403a51be70f1Chia-chi Yeh
144e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    void clear() {
145e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        if (mRef) {
146e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project            mRef->decSysRef();
147e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        }
148e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        mRef = NULL;
149e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
150e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
151e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    inline T * get() const {
152e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        return mRef;
153e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
154e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
155e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    inline T * operator-> () const {
156e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project        return mRef;
157e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    }
158e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project
159e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Projectprotected:
160e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project    T * mRef;
161e16cb84e2324f05334d18dcf5956f20f44262b62The Android Open Source Project};
162
163}
164}
165
166#endif //ANDROID_RS_OBJECT_BASE_H
167
168