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