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