rsElement.h revision 39f2ef6fed00a99c5c389e12c4597884027d4858
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
29// An element is a group of Components that occupies one cell in a structure.
30class Element : public ObjectBase
31{
32public:
33    ~Element();
34
35    uint32_t getGLType() const;
36    uint32_t getGLFormat() const;
37
38    size_t getSizeBits() const;
39    size_t getSizeBytes() const {
40        return (getSizeBits() + 7) >> 3;
41    }
42
43    size_t getFieldOffsetBits(uint32_t componentNumber) const {
44        return mFields[componentNumber].offsetBits;
45    }
46    size_t getFieldOffsetBytes(uint32_t componentNumber) const {
47        return mFields[componentNumber].offsetBits >> 3;
48    }
49
50    uint32_t getFieldCount() const {return mFieldCount;}
51    const Element * getField(uint32_t idx) const {return mFields[idx].e.get();}
52    const char * getFieldName(uint32_t idx) const {return mFields[idx].name.string();}
53    uint32_t getFieldArraySize(uint32_t idx) const {return mFields[idx].arraySize;}
54
55    const Component & getComponent() const {return mComponent;}
56    RsDataType getType() const {return mComponent.getType();}
57    RsDataKind getKind() const {return mComponent.getKind();}
58    uint32_t getBits() const {return mBits;}
59
60    String8 getGLSLType(uint32_t indent=0) const;
61
62    void dumpLOGV(const char *prefix) const;
63    virtual void serialize(OStream *stream) const;
64    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ELEMENT; }
65    static Element *createFromStream(Context *rsc, IStream *stream);
66
67    static const Element * create(Context *rsc, RsDataType dt, RsDataKind dk,
68                            bool isNorm, uint32_t vecSize);
69    static const Element * create(Context *rsc, size_t count, const Element **,
70                            const char **, const size_t * lengths, const uint32_t *asin);
71
72    void incRefs(const void *) const;
73    void decRefs(const void *) const;
74    bool getHasReferences() const {return mHasReference;}
75
76    bool isEqual(const Element *other) const;
77
78protected:
79    // deallocate any components that are part of this element.
80    void clear();
81
82    typedef struct {
83        String8 name;
84        ObjectBaseRef<const Element> e;
85        uint32_t offsetBits;
86        uint32_t arraySize;
87    } ElementField_t;
88    ElementField_t *mFields;
89    size_t mFieldCount;
90    bool mHasReference;
91
92
93    Element(Context *);
94
95    Component mComponent;
96    uint32_t mBits;
97};
98
99
100class ElementState {
101public:
102    ElementState();
103    ~ElementState();
104
105    void elementBuilderBegin();
106    void elementBuilderAdd(const Element *e, const char *nameStr, uint32_t arraySize);
107    const Element *elementBuilderCreate(Context *rsc);
108
109    // Cache of all existing elements.
110    Vector<Element *> mElements;
111private:
112    Vector<const Element *> mBuilderElements;
113    Vector<const char*> mBuilderNameStrings;
114    Vector<size_t> mBuilderNameLengths;
115    Vector<uint32_t> mBuilderArrays;
116};
117
118
119}
120}
121#endif //ANDROID_STRUCTURED_ELEMENT_H
122