blob: 1eae46df6ca28d6e42fb3a195ddccc847c93877a [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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
Jason Samsd01d9702009-12-23 14:35:29 -080020#include "rsComponent.h"
Jason Sams4815c0d2009-12-15 12:58:36 -080021#include "rsUtils.h"
Alex Sakhartchouke23d2392012-03-09 09:24:39 -080022#include "rsDefines.h"
Jason Sams4815c0d2009-12-15 12:58:36 -080023#include "rsObjectBase.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070024
25// ---------------------------------------------------------------------------
26namespace android {
27namespace renderscript {
Alex Sakhartchouk4a43e3e2011-12-28 13:47:05 -080028/*****************************************************************************
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 *****************************************************************************/
Jason Sams326e0dd2009-05-22 14:03:28 -070036// An element is a group of Components that occupies one cell in a structure.
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080037class Element : public ObjectBase {
Jason Sams326e0dd2009-05-22 14:03:28 -070038public:
Alex Sakhartchouk253325d2011-12-15 09:56:10 -080039 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
Alex Sakhartchoukc700e642011-08-16 13:09:46 -070060 class Builder {
61 public:
Alex Sakhartchouk98cc3552011-08-19 09:43:18 -070062 Builder();
Alex Sakhartchoukc700e642011-08-16 13:09:46 -070063 void add(const Element *e, const char *nameStr, uint32_t arraySize);
64 ObjectBaseRef<const Element> create(Context *rsc);
65 private:
66 Vector<ObjectBaseRef<const Element> > mBuilderElementRefs;
67 Vector<const Element *> mBuilderElements;
68 Vector<const char*> mBuilderNameStrings;
69 Vector<size_t> mBuilderNameLengths;
70 Vector<uint32_t> mBuilderArrays;
71 };
Jason Sams565ac362009-06-03 16:04:54 -070072 uint32_t getGLType() const;
73 uint32_t getGLFormat() const;
Jason Sams326e0dd2009-05-22 14:03:28 -070074
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -080075 size_t getSizeBitsUnpadded() const;
76 size_t getSizeBytesUnpadded() const {
77 return (getSizeBitsUnpadded() + 7) >> 3;
78 }
79
Jason Sams326e0dd2009-05-22 14:03:28 -070080 size_t getSizeBits() const;
Jason Samse5ffb872009-08-09 17:01:55 -070081 size_t getSizeBytes() const {
82 return (getSizeBits() + 7) >> 3;
Jason Sams326e0dd2009-05-22 14:03:28 -070083 }
84
Jason Samse3929c92010-08-09 18:13:33 -070085 size_t getFieldOffsetBits(uint32_t componentNumber) const {
86 return mFields[componentNumber].offsetBits;
87 }
Jason Sams4815c0d2009-12-15 12:58:36 -080088 size_t getFieldOffsetBytes(uint32_t componentNumber) const {
Jason Samse3929c92010-08-09 18:13:33 -070089 return mFields[componentNumber].offsetBits >> 3;
Jason Sams326e0dd2009-05-22 14:03:28 -070090 }
91
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -080092 size_t getFieldOffsetBytesUnpadded(uint32_t componentNumber) const {
93 return mFields[componentNumber].offsetBitsUnpadded >> 3;
94 }
95
Jason Sams4815c0d2009-12-15 12:58:36 -080096 uint32_t getFieldCount() const {return mFieldCount;}
97 const Element * getField(uint32_t idx) const {return mFields[idx].e.get();}
98 const char * getFieldName(uint32_t idx) const {return mFields[idx].name.string();}
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070099 uint32_t getFieldArraySize(uint32_t idx) const {return mFields[idx].arraySize;}
Jason Sams326e0dd2009-05-22 14:03:28 -0700100
Jason Samsd01d9702009-12-23 14:35:29 -0800101 const Component & getComponent() const {return mComponent;}
102 RsDataType getType() const {return mComponent.getType();}
Jason Samsd01d9702009-12-23 14:35:29 -0800103 RsDataKind getKind() const {return mComponent.getKind();}
Jason Sams4815c0d2009-12-15 12:58:36 -0800104 uint32_t getBits() const {return mBits;}
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800105 uint32_t getBitsUnpadded() const {return mBitsUnpadded;}
Jason Samsc905efd2012-11-26 15:20:18 -0800106 uint32_t getVectorSize() const {return mComponent.getVectorSize();}
Jason Samsd01d9702009-12-23 14:35:29 -0800107
Jason Samse12c1c52009-09-27 17:50:38 -0700108 void dumpLOGV(const char *prefix) const;
Jason Samse3150cf2012-07-24 18:10:20 -0700109 virtual void serialize(Context *rsc, OStream *stream) const;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700110 virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ELEMENT; }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700111 static Element *createFromStream(Context *rsc, IStream *stream);
Jason Samse12c1c52009-09-27 17:50:38 -0700112
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700113 static ObjectBaseRef<const Element> createRef(Context *rsc,
114 RsDataType dt,
115 RsDataKind dk,
116 bool isNorm,
117 uint32_t vecSize);
118 static ObjectBaseRef<const Element> createRef(Context *rsc, size_t count,
119 const Element **,
120 const char **,
121 const size_t * lengths,
122 const uint32_t *asin);
123
124 static const Element* create(Context *rsc,
125 RsDataType dt,
126 RsDataKind dk,
127 bool isNorm,
128 uint32_t vecSize) {
129 ObjectBaseRef<const Element> elem = createRef(rsc, dt, dk, isNorm, vecSize);
130 elem->incUserRef();
131 return elem.get();
132 }
133 static const Element* create(Context *rsc, size_t count,
134 const Element **ein,
135 const char **nin,
136 const size_t * lengths,
137 const uint32_t *asin) {
138 ObjectBaseRef<const Element> elem = createRef(rsc, count, ein, nin, lengths, asin);
139 elem->incUserRef();
140 return elem.get();
141 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800142
Jason Samse3929c92010-08-09 18:13:33 -0700143 void incRefs(const void *) const;
144 void decRefs(const void *) const;
145 bool getHasReferences() const {return mHasReference;}
146
Jason Sams326e0dd2009-05-22 14:03:28 -0700147protected:
148 // deallocate any components that are part of this element.
149 void clear();
150
Jason Sams4815c0d2009-12-15 12:58:36 -0800151 typedef struct {
152 String8 name;
Jason Samsd01d9702009-12-23 14:35:29 -0800153 ObjectBaseRef<const Element> e;
Jason Samse3929c92010-08-09 18:13:33 -0700154 uint32_t offsetBits;
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800155 uint32_t offsetBitsUnpadded;
Jason Sams46e45542010-09-02 17:35:23 -0700156 uint32_t arraySize;
Jason Sams4815c0d2009-12-15 12:58:36 -0800157 } ElementField_t;
158 ElementField_t *mFields;
159 size_t mFieldCount;
Jason Samse3929c92010-08-09 18:13:33 -0700160 bool mHasReference;
Jason Sams4815c0d2009-12-15 12:58:36 -0800161
Jason Sams326e0dd2009-05-22 14:03:28 -0700162
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700163 virtual ~Element();
Jason Samse514b452009-09-25 14:51:22 -0700164 Element(Context *);
Jason Sams4815c0d2009-12-15 12:58:36 -0800165
Jason Samsd01d9702009-12-23 14:35:29 -0800166 Component mComponent;
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800167 uint32_t mBitsUnpadded;
Jason Sams4815c0d2009-12-15 12:58:36 -0800168 uint32_t mBits;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800169
170 void compute();
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700171
172 virtual void preDestroy() const;
Jason Sams326e0dd2009-05-22 14:03:28 -0700173};
174
175
176class ElementState {
177public:
178 ElementState();
179 ~ElementState();
180
Jason Sams81549542010-02-17 15:38:10 -0800181 // Cache of all existing elements.
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700182 Vector<Element *> mElements;
Jason Sams326e0dd2009-05-22 14:03:28 -0700183};
184
185
186}
187}
188#endif //ANDROID_STRUCTURED_ELEMENT_H