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_STRUCTURED_ELEMENT_H
     18 #define ANDROID_STRUCTURED_ELEMENT_H
     19 
     20 #include "rsComponent.h"
     21 #include "rsUtils.h"
     22 #include "rsDefines.h"
     23 #include "rsObjectBase.h"
     24 
     25 // ---------------------------------------------------------------------------
     26 namespace android {
     27 namespace renderscript {
     28 /*****************************************************************************
     29  * CAUTION
     30  *
     31  * Any layout changes for this class may require a corresponding change to be
     32  * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains
     33  * a partial copy of the information below.
     34  *
     35  *****************************************************************************/
     36 // An element is a group of Components that occupies one cell in a structure.
     37 class Element : public ObjectBase {
     38 public:
     39     struct Hal {
     40         mutable void *drv;
     41 
     42         struct State {
     43             RsDataType dataType;
     44             RsDataKind dataKind;
     45             uint32_t vectorSize;
     46             uint32_t elementSizeBytes;
     47 
     48             // Subelements
     49             const Element **fields;
     50             uint32_t *fieldArraySizes;
     51             const char **fieldNames;
     52             uint32_t *fieldNameLengths;
     53             uint32_t *fieldOffsetBytes;
     54             uint32_t fieldsCount;
     55         };
     56         State state;
     57     };
     58     Hal mHal;
     59 
     60     uint32_t getGLType() const;
     61     uint32_t getGLFormat() const;
     62 
     63     size_t getSizeBitsUnpadded() const;
     64     size_t getSizeBytesUnpadded() const {
     65         return (getSizeBitsUnpadded() + 7) >> 3;
     66     }
     67 
     68     size_t getSizeBits() const;
     69     size_t getSizeBytes() const {
     70         return (getSizeBits() + 7) >> 3;
     71     }
     72 
     73     size_t getFieldOffsetBits(uint32_t componentNumber) const {
     74         return mFields[componentNumber].offsetBits;
     75     }
     76     size_t getFieldOffsetBytes(uint32_t componentNumber) const {
     77         return mFields[componentNumber].offsetBits >> 3;
     78     }
     79 
     80     size_t getFieldOffsetBytesUnpadded(uint32_t componentNumber) const {
     81         return mFields[componentNumber].offsetBitsUnpadded >> 3;
     82     }
     83 
     84     uint32_t getFieldCount() const {return mFieldCount;}
     85     const Element * getField(uint32_t idx) const {return mFields[idx].e.get();}
     86     const char * getFieldName(uint32_t idx) const {return mFields[idx].name;}
     87     uint32_t getFieldArraySize(uint32_t idx) const {return mFields[idx].arraySize;}
     88 
     89     const Component & getComponent() const {return mComponent;}
     90     RsDataType getType() const {return mComponent.getType();}
     91     RsDataKind getKind() const {return mComponent.getKind();}
     92     uint32_t getBits() const {return mBits;}
     93     uint32_t getBitsUnpadded() const {return mBitsUnpadded;}
     94     uint32_t getVectorSize() const {return mComponent.getVectorSize();}
     95 
     96     void dumpLOGV(const char *prefix) const;
     97     virtual void serialize(Context *rsc, OStream *stream) const;
     98     virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ELEMENT; }
     99     static Element *createFromStream(Context *rsc, IStream *stream);
    100 
    101     static ObjectBaseRef<const Element> createRef(Context *rsc,
    102                                                   RsDataType dt,
    103                                                   RsDataKind dk,
    104                                                   bool isNorm,
    105                                                   uint32_t vecSize);
    106     static ObjectBaseRef<const Element> createRef(Context *rsc, size_t count,
    107                                                   const Element **,
    108                                                   const char **,
    109                                                   const size_t * lengths,
    110                                                   const uint32_t *asin);
    111 
    112     static const Element* create(Context *rsc,
    113                                  RsDataType dt,
    114                                  RsDataKind dk,
    115                                  bool isNorm,
    116                                  uint32_t vecSize) {
    117         ObjectBaseRef<const Element> elem = createRef(rsc, dt, dk, isNorm, vecSize);
    118         elem->incUserRef();
    119         return elem.get();
    120     }
    121     static const Element* create(Context *rsc, size_t count,
    122                                  const Element **ein,
    123                                  const char **nin,
    124                                  const size_t * lengths = NULL,
    125                                  const uint32_t *asin = NULL) {
    126         ObjectBaseRef<const Element> elem = createRef(rsc, count, ein, nin, lengths, asin);
    127         elem->incUserRef();
    128         return elem.get();
    129     }
    130 
    131     void incRefs(const void *) const;
    132     void decRefs(const void *) const;
    133     bool getHasReferences() const {return mHasReference;}
    134 
    135 protected:
    136     // deallocate any components that are part of this element.
    137     void clear();
    138 
    139     typedef struct {
    140         const char *name;
    141         ObjectBaseRef<const Element> e;
    142         uint32_t offsetBits;
    143         uint32_t offsetBitsUnpadded;
    144         uint32_t arraySize;
    145     } ElementField_t;
    146     ElementField_t *mFields;
    147     size_t mFieldCount;
    148     bool mHasReference;
    149 
    150 
    151     virtual ~Element();
    152     Element(Context *);
    153 
    154     Component mComponent;
    155     uint32_t mBitsUnpadded;
    156     uint32_t mBits;
    157 
    158     void compute();
    159 
    160     virtual void preDestroy() const;
    161 };
    162 
    163 
    164 class ElementState {
    165 public:
    166     ElementState();
    167     ~ElementState();
    168 
    169     // Cache of all existing elements.
    170     Vector<Element *> mElements;
    171 };
    172 
    173 
    174 }
    175 }
    176 #endif //ANDROID_STRUCTURED_ELEMENT_H
    177