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// ---------------------------------------------------------------------------
26namespace android {
27namespace 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.
37class Element : public ObjectBase {
38public:
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    class Builder {
61    public:
62        Builder();
63        void add(const Element *e, const char *nameStr, uint32_t arraySize);
64        ObjectBaseRef<const Element> create(Context *rsc);
65    private:
66        Vector<ObjectBaseRef<const Element> > mBuilderElementRefs;
67        Vector<const Element *> mBuilderElements;
68        Vector<const char*> mBuilderNameStrings;
69        Vector<size_t> mBuilderNameLengths;
70        Vector<uint32_t> mBuilderArrays;
71    };
72    uint32_t getGLType() const;
73    uint32_t getGLFormat() const;
74
75    size_t getSizeBitsUnpadded() const;
76    size_t getSizeBytesUnpadded() const {
77        return (getSizeBitsUnpadded() + 7) >> 3;
78    }
79
80    size_t getSizeBits() const;
81    size_t getSizeBytes() const {
82        return (getSizeBits() + 7) >> 3;
83    }
84
85    size_t getFieldOffsetBits(uint32_t componentNumber) const {
86        return mFields[componentNumber].offsetBits;
87    }
88    size_t getFieldOffsetBytes(uint32_t componentNumber) const {
89        return mFields[componentNumber].offsetBits >> 3;
90    }
91
92    size_t getFieldOffsetBytesUnpadded(uint32_t componentNumber) const {
93        return mFields[componentNumber].offsetBitsUnpadded >> 3;
94    }
95
96    uint32_t getFieldCount() const {return mFieldCount;}
97    const Element * getField(uint32_t idx) const {return mFields[idx].e.get();}
98    const char * getFieldName(uint32_t idx) const {return mFields[idx].name.string();}
99    uint32_t getFieldArraySize(uint32_t idx) const {return mFields[idx].arraySize;}
100
101    const Component & getComponent() const {return mComponent;}
102    RsDataType getType() const {return mComponent.getType();}
103    RsDataKind getKind() const {return mComponent.getKind();}
104    uint32_t getBits() const {return mBits;}
105    uint32_t getBitsUnpadded() const {return mBitsUnpadded;}
106
107    void dumpLOGV(const char *prefix) const;
108    virtual void serialize(Context *rsc, OStream *stream) const;
109    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ELEMENT; }
110    static Element *createFromStream(Context *rsc, IStream *stream);
111
112    static ObjectBaseRef<const Element> createRef(Context *rsc,
113                                                  RsDataType dt,
114                                                  RsDataKind dk,
115                                                  bool isNorm,
116                                                  uint32_t vecSize);
117    static ObjectBaseRef<const Element> createRef(Context *rsc, size_t count,
118                                                  const Element **,
119                                                  const char **,
120                                                  const size_t * lengths,
121                                                  const uint32_t *asin);
122
123    static const Element* create(Context *rsc,
124                                 RsDataType dt,
125                                 RsDataKind dk,
126                                 bool isNorm,
127                                 uint32_t vecSize) {
128        ObjectBaseRef<const Element> elem = createRef(rsc, dt, dk, isNorm, vecSize);
129        elem->incUserRef();
130        return elem.get();
131    }
132    static const Element* create(Context *rsc, size_t count,
133                                 const Element **ein,
134                                 const char **nin,
135                                 const size_t * lengths,
136                                 const uint32_t *asin) {
137        ObjectBaseRef<const Element> elem = createRef(rsc, count, ein, nin, lengths, asin);
138        elem->incUserRef();
139        return elem.get();
140    }
141
142    void incRefs(const void *) const;
143    void decRefs(const void *) const;
144    bool getHasReferences() const {return mHasReference;}
145
146protected:
147    // deallocate any components that are part of this element.
148    void clear();
149
150    typedef struct {
151        String8 name;
152        ObjectBaseRef<const Element> e;
153        uint32_t offsetBits;
154        uint32_t offsetBitsUnpadded;
155        uint32_t arraySize;
156    } ElementField_t;
157    ElementField_t *mFields;
158    size_t mFieldCount;
159    bool mHasReference;
160
161
162    virtual ~Element();
163    Element(Context *);
164
165    Component mComponent;
166    uint32_t mBitsUnpadded;
167    uint32_t mBits;
168
169    void compute();
170
171    virtual void preDestroy() const;
172};
173
174
175class ElementState {
176public:
177    ElementState();
178    ~ElementState();
179
180    // Cache of all existing elements.
181    Vector<Element *> mElements;
182};
183
184
185}
186}
187#endif //ANDROID_STRUCTURED_ELEMENT_H
188