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 
     23 namespace android {
     24 namespace renderscript {
     25 
     26 class Context;
     27 
     28 // An element is a group of Components that occupies one cell in a structure.
     29 class ObjectBase
     30 {
     31 public:
     32     ObjectBase(Context *rsc);
     33     virtual ~ObjectBase();
     34 
     35     void incSysRef() const;
     36     bool decSysRef() const;
     37 
     38     void incUserRef() const;
     39     bool decUserRef() const;
     40     bool zeroUserRef() const;
     41 
     42     const char * getName() const {
     43         return mName;
     44     }
     45     void setName(const char *);
     46     void setName(const char *, uint32_t len);
     47 
     48     Context * getContext() const {return mRSC;}
     49     void setContext(Context *);
     50 
     51     static void zeroAllUserRef(Context *rsc);
     52     static void dumpAll(Context *rsc);
     53 
     54     virtual void dumpLOGV(const char *prefix) const;
     55 
     56 protected:
     57     const char *mAllocFile;
     58     uint32_t mAllocLine;
     59     Context *mRSC;
     60 
     61 private:
     62     void add() const;
     63     void remove() const;
     64 
     65     bool checkDelete() const;
     66 
     67     char * mName;
     68     mutable int32_t mSysRefCount;
     69     mutable int32_t mUserRefCount;
     70 
     71     mutable const ObjectBase * mPrev;
     72     mutable const ObjectBase * mNext;
     73 };
     74 
     75 template<class T>
     76 class ObjectBaseRef
     77 {
     78 public:
     79     ObjectBaseRef() {
     80         mRef = NULL;
     81     }
     82 
     83     ObjectBaseRef(const ObjectBaseRef &ref) {
     84         mRef = ref.get();
     85         if (mRef) {
     86             mRef->incSysRef();
     87         }
     88     }
     89 
     90     ObjectBaseRef(T *ref) {
     91         mRef = ref;
     92         if (mRef) {
     93             ref->incSysRef();
     94         }
     95     }
     96 
     97     ~ObjectBaseRef() {
     98         clear();
     99     }
    100 
    101     void set(T *ref) {
    102         if (mRef != ref) {
    103             clear();
    104             mRef = ref;
    105             if (mRef) {
    106                 ref->incSysRef();
    107             }
    108         }
    109     }
    110 
    111     void set(const ObjectBaseRef &ref) {
    112         set(ref.mRef);
    113     }
    114 
    115     void clear() {
    116         if (mRef) {
    117             mRef->decSysRef();
    118         }
    119         mRef = NULL;
    120     }
    121 
    122     inline T * get() const {
    123         return mRef;
    124     }
    125 
    126     inline T * operator-> () const {
    127         return mRef;
    128     }
    129 
    130 protected:
    131     T * mRef;
    132 
    133 };
    134 
    135 
    136 }
    137 }
    138 
    139 #endif //ANDROID_RS_OBJECT_BASE_H
    140 
    141