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