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