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