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