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