Home | History | Annotate | Download | only in rs
      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 #define RS_OBJECT_DEBUG 0
     23 
     24 #include <utils/CallStack.h>
     25 
     26 namespace android {
     27 namespace renderscript {
     28 
     29 class Context;
     30 class OStream;
     31 
     32 // An element is a group of Components that occupies one cell in a structure.
     33 class ObjectBase {
     34 public:
     35     ObjectBase(Context *rsc);
     36 
     37     void incSysRef() const;
     38     bool decSysRef() const;
     39 
     40     void incUserRef() const;
     41     bool decUserRef() const;
     42     bool zeroUserRef() const;
     43 
     44     static bool checkDelete(const ObjectBase *);
     45 
     46     const char * getName() const {
     47         return mName.string();
     48     }
     49     void setName(const char *);
     50     void setName(const char *, uint32_t len);
     51 
     52     Context * getContext() const {return mRSC;}
     53     virtual bool freeChildren();
     54 
     55     static void zeroAllUserRef(Context *rsc);
     56     static void freeAllChildren(Context *rsc);
     57     static void dumpAll(Context *rsc);
     58 
     59     virtual void dumpLOGV(const char *prefix) const;
     60     virtual void serialize(OStream *stream) const = 0;
     61     virtual RsA3DClassID getClassId() const = 0;
     62 
     63     static bool isValid(const Context *rsc, const ObjectBase *obj);
     64 
     65     // The async lock is taken during object creation in non-rs threads
     66     // and object deletion in the rs thread.
     67     static void asyncLock();
     68     static void asyncUnlock();
     69 
     70 protected:
     71     // Called inside the async lock for any object list management that is
     72     // necessary in derived classes.
     73     virtual void preDestroy() const;
     74 
     75     Context *mRSC;
     76     virtual ~ObjectBase();
     77 
     78 private:
     79     static pthread_mutex_t gObjectInitMutex;
     80 
     81     void add() const;
     82     void remove() const;
     83 
     84     String8 mName;
     85     mutable int32_t mSysRefCount;
     86     mutable int32_t mUserRefCount;
     87 
     88     mutable const ObjectBase * mPrev;
     89     mutable const ObjectBase * mNext;
     90 
     91 #if RS_OBJECT_DEBUG
     92     CallStack mStack;
     93 #endif
     94 
     95 };
     96 
     97 template<class T>
     98 class ObjectBaseRef {
     99 public:
    100     ObjectBaseRef() {
    101         mRef = NULL;
    102     }
    103 
    104     ObjectBaseRef(const ObjectBaseRef &ref) {
    105         mRef = ref.get();
    106         if (mRef) {
    107             mRef->incSysRef();
    108         }
    109     }
    110 
    111     ObjectBaseRef(T *ref) {
    112         mRef = ref;
    113         if (mRef) {
    114             ref->incSysRef();
    115         }
    116     }
    117 
    118     ObjectBaseRef & operator= (const ObjectBaseRef &ref) {
    119         if (&ref != this) {
    120             set(ref);
    121         }
    122         return *this;
    123     }
    124 
    125     ~ObjectBaseRef() {
    126         clear();
    127     }
    128 
    129     void set(T *ref) {
    130         if (mRef != ref) {
    131             clear();
    132             mRef = ref;
    133             if (mRef) {
    134                 ref->incSysRef();
    135             }
    136         }
    137     }
    138 
    139     void set(const ObjectBaseRef &ref) {
    140         set(ref.mRef);
    141     }
    142 
    143     void clear() {
    144         if (mRef) {
    145             mRef->decSysRef();
    146         }
    147         mRef = NULL;
    148     }
    149 
    150     inline T * get() const {
    151         return mRef;
    152     }
    153 
    154     inline T * operator-> () const {
    155         return mRef;
    156     }
    157 
    158 protected:
    159     T * mRef;
    160 };
    161 
    162 }
    163 }
    164 
    165 #endif //ANDROID_RS_OBJECT_BASE_H
    166 
    167