blob: a734400fb65771671b20d8be67c50aeb1558e08e [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
Jason Sams326e0dd2009-05-22 14:03:28 -070017
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070018#include "rsContext.h"
Jason Sams565ac362009-06-03 16:04:54 -070019
Jason Sams326e0dd2009-05-22 14:03:28 -070020using namespace android;
21using namespace android::renderscript;
22
Jason Sams326e0dd2009-05-22 14:03:28 -070023
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080024Element::Element(Context *rsc) : ObjectBase(rsc) {
Jason Sams4815c0d2009-12-15 12:58:36 -080025 mBits = 0;
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -080026 mBitsUnpadded = 0;
Chris Wailes44bef6f2014-08-12 13:51:10 -070027 mFields = nullptr;
Jason Sams4815c0d2009-12-15 12:58:36 -080028 mFieldCount = 0;
Jason Samse3929c92010-08-09 18:13:33 -070029 mHasReference = false;
Alex Sakhartchoukb7c8c522011-12-22 13:11:48 -080030 memset(&mHal, 0, sizeof(mHal));
Jason Sams326e0dd2009-05-22 14:03:28 -070031}
32
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080033Element::~Element() {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -070034 clear();
35}
36
Tim Murray49a87772014-07-10 10:00:00 -070037void Element::operator delete(void* ptr) {
38 if (ptr) {
39 Element *e = (Element*) ptr;
40 e->getContext()->mHal.funcs.freeRuntimeMem(ptr);
41 }
42}
43
Alex Sakhartchoukc700e642011-08-16 13:09:46 -070044void Element::preDestroy() const {
Yang Nib8353c52015-02-14 18:00:59 -080045 for (uint32_t ct = 0; ct < mRSC->mStateElement.mElements.size(); ct++) {
46 if (mRSC->mStateElement.mElements[ct] == this) {
47 mRSC->mStateElement.mElements.removeAt(ct);
48 break;
Jason Sams81549542010-02-17 15:38:10 -080049 }
50 }
Jason Sams326e0dd2009-05-22 14:03:28 -070051}
52
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080053void Element::clear() {
Jason Sams48ecf6a2013-07-09 15:35:29 -070054 if (mFields) {
55 for (size_t i = 0; i < mFieldCount; i++) {
56 delete[] mFields[i].name;
57 }
58 delete [] mFields;
59 }
Chris Wailes44bef6f2014-08-12 13:51:10 -070060 mFields = nullptr;
Jason Sams4815c0d2009-12-15 12:58:36 -080061 mFieldCount = 0;
Jason Samse3929c92010-08-09 18:13:33 -070062 mHasReference = false;
Alex Sakhartchouk253325d2011-12-15 09:56:10 -080063
64 delete [] mHal.state.fields;
65 delete [] mHal.state.fieldArraySizes;
66 delete [] mHal.state.fieldNames;
67 delete [] mHal.state.fieldNameLengths;
68 delete [] mHal.state.fieldOffsetBytes;
Jason Sams326e0dd2009-05-22 14:03:28 -070069}
Jason Sams326e0dd2009-05-22 14:03:28 -070070
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080071size_t Element::getSizeBits() const {
Jason Sams4815c0d2009-12-15 12:58:36 -080072 if (!mFieldCount) {
73 return mBits;
74 }
75
Jason Sams326e0dd2009-05-22 14:03:28 -070076 size_t total = 0;
Jason Sams4815c0d2009-12-15 12:58:36 -080077 for (size_t ct=0; ct < mFieldCount; ct++) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070078 total += mFields[ct].e->mBits * mFields[ct].arraySize;
Jason Sams326e0dd2009-05-22 14:03:28 -070079 }
80 return total;
81}
82
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -080083size_t Element::getSizeBitsUnpadded() const {
84 if (!mFieldCount) {
85 return mBitsUnpadded;
86 }
87
88 size_t total = 0;
89 for (size_t ct=0; ct < mFieldCount; ct++) {
90 total += mFields[ct].e->mBitsUnpadded * mFields[ct].arraySize;
91 }
92 return total;
93}
94
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080095void Element::dumpLOGV(const char *prefix) const {
Jason Samse12c1c52009-09-27 17:50:38 -070096 ObjectBase::dumpLOGV(prefix);
Steve Block65982012011-10-20 11:56:00 +010097 ALOGV("%s Element: fieldCount: %zu, size bytes: %zu", prefix, mFieldCount, getSizeBytes());
Alex Sakhartchoukc700e642011-08-16 13:09:46 -070098 mComponent.dumpLOGV(prefix);
Jason Sams4815c0d2009-12-15 12:58:36 -080099 for (uint32_t ct = 0; ct < mFieldCount; ct++) {
Steve Block65982012011-10-20 11:56:00 +0100100 ALOGV("%s Element field index: %u ------------------", prefix, ct);
101 ALOGV("%s name: %s, offsetBits: %u, arraySize: %u",
Jason Sams48ecf6a2013-07-09 15:35:29 -0700102 prefix, mFields[ct].name, mFields[ct].offsetBits, mFields[ct].arraySize);
Alex Sakhartchouk963bb452010-10-13 14:22:02 -0700103 mFields[ct].e->dumpLOGV(prefix);
Jason Samse12c1c52009-09-27 17:50:38 -0700104 }
105}
106
Jason Samse3150cf2012-07-24 18:10:20 -0700107void Element::serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700108 // Need to identify ourselves
109 stream->addU32((uint32_t)getClassId());
Jason Sams48ecf6a2013-07-09 15:35:29 -0700110 stream->addString(getName());
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700111
112 mComponent.serialize(stream);
113
114 // Now serialize all the fields
115 stream->addU32(mFieldCount);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800116 for (uint32_t ct = 0; ct < mFieldCount; ct++) {
Jason Sams48ecf6a2013-07-09 15:35:29 -0700117 stream->addString(mFields[ct].name);
Jason Sams46e45542010-09-02 17:35:23 -0700118 stream->addU32(mFields[ct].arraySize);
Jason Samse3150cf2012-07-24 18:10:20 -0700119 mFields[ct].e->serialize(rsc, stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700120 }
121}
122
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800123Element *Element::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700124 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700125 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800126 if (classID != RS_A3D_CLASS_ID_ELEMENT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000127 ALOGE("element loading skipped due to invalid class id\n");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700128 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700129 }
130
Jason Sams48ecf6a2013-07-09 15:35:29 -0700131 const char *name = stream->loadString();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700132
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700133 Component component;
134 component.loadFromStream(stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700135
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700136 uint32_t fieldCount = stream->loadU32();
137 if (!fieldCount) {
138 return (Element *)Element::create(rsc,
139 component.getType(),
140 component.getKind(),
141 component.getIsNormalized(),
Jason Sams807fdc42012-07-25 17:55:39 -0700142 component.getVectorSize());
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700143 }
144
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700145 const Element **subElems = new const Element *[fieldCount];
146 const char **subElemNames = new const char *[fieldCount];
147 size_t *subElemNamesLengths = new size_t[fieldCount];
148 uint32_t *arraySizes = new uint32_t[fieldCount];
149
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700150 for (uint32_t ct = 0; ct < fieldCount; ct ++) {
Jason Sams48ecf6a2013-07-09 15:35:29 -0700151 subElemNames[ct] = stream->loadString();
152 subElemNamesLengths[ct] = strlen(subElemNames[ct]);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700153 arraySizes[ct] = stream->loadU32();
154 subElems[ct] = Element::createFromStream(rsc, stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700155 }
156
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700157 const Element *elem = Element::create(rsc, fieldCount, subElems, subElemNames,
158 subElemNamesLengths, arraySizes);
159 for (uint32_t ct = 0; ct < fieldCount; ct ++) {
160 delete [] subElemNames[ct];
161 subElems[ct]->decUserRef();
162 }
Jason Sams48ecf6a2013-07-09 15:35:29 -0700163 delete[] name;
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700164 delete[] subElems;
165 delete[] subElemNames;
166 delete[] subElemNamesLengths;
167 delete[] arraySizes;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700168
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700169 return (Element *)elem;
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700170}
Jason Samsd01d9702009-12-23 14:35:29 -0800171
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800172void Element::compute() {
Alex Sakhartchouk253325d2011-12-15 09:56:10 -0800173 mHal.state.dataType = mComponent.getType();
174 mHal.state.dataKind = mComponent.getKind();
175 mHal.state.vectorSize = mComponent.getVectorSize();
176
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800177 if (mFieldCount == 0) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800178 mBits = mComponent.getBits();
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800179 mBitsUnpadded = mComponent.getBitsUnpadded();
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800180 mHasReference = mComponent.isReference();
Alex Sakhartchouk253325d2011-12-15 09:56:10 -0800181
182 mHal.state.elementSizeBytes = getSizeBytes();
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800183 return;
184 }
185
Alex Sakhartchoukb7c8c522011-12-22 13:11:48 -0800186 uint32_t noPaddingFieldCount = 0;
187 for (uint32_t ct = 0; ct < mFieldCount; ct ++) {
Jason Sams48ecf6a2013-07-09 15:35:29 -0700188 if (mFields[ct].name[0] != '#') {
Alex Sakhartchoukb7c8c522011-12-22 13:11:48 -0800189 noPaddingFieldCount ++;
190 }
191 }
192
193 mHal.state.fields = new const Element*[noPaddingFieldCount];
194 mHal.state.fieldArraySizes = new uint32_t[noPaddingFieldCount];
195 mHal.state.fieldNames = new const char*[noPaddingFieldCount];
196 mHal.state.fieldNameLengths = new uint32_t[noPaddingFieldCount];
197 mHal.state.fieldOffsetBytes = new uint32_t[noPaddingFieldCount];
198 mHal.state.fieldsCount = noPaddingFieldCount;
Alex Sakhartchouk253325d2011-12-15 09:56:10 -0800199
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800200 size_t bits = 0;
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800201 size_t bitsUnpadded = 0;
Alex Sakhartchoukb7c8c522011-12-22 13:11:48 -0800202 for (size_t ct = 0, ctNoPadding = 0; ct < mFieldCount; ct++) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800203 mFields[ct].offsetBits = bits;
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800204 mFields[ct].offsetBitsUnpadded = bitsUnpadded;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800205 bits += mFields[ct].e->getSizeBits() * mFields[ct].arraySize;
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800206 bitsUnpadded += mFields[ct].e->getSizeBitsUnpadded() * mFields[ct].arraySize;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800207
208 if (mFields[ct].e->mHasReference) {
209 mHasReference = true;
210 }
Alex Sakhartchouk253325d2011-12-15 09:56:10 -0800211
Jason Sams48ecf6a2013-07-09 15:35:29 -0700212 if (mFields[ct].name[0] == '#') {
Alex Sakhartchoukb7c8c522011-12-22 13:11:48 -0800213 continue;
214 }
215
216 mHal.state.fields[ctNoPadding] = mFields[ct].e.get();
217 mHal.state.fieldArraySizes[ctNoPadding] = mFields[ct].arraySize;
Jason Sams48ecf6a2013-07-09 15:35:29 -0700218 mHal.state.fieldNames[ctNoPadding] = mFields[ct].name;
219 mHal.state.fieldNameLengths[ctNoPadding] = strlen(mFields[ct].name) + 1; // to include 0
Alex Sakhartchoukb7c8c522011-12-22 13:11:48 -0800220 mHal.state.fieldOffsetBytes[ctNoPadding] = mFields[ct].offsetBits >> 3;
221
222 ctNoPadding ++;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800223 }
224
Alex Sakhartchouk253325d2011-12-15 09:56:10 -0800225 mHal.state.elementSizeBytes = getSizeBytes();
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800226}
227
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700228ObjectBaseRef<const Element> Element::createRef(Context *rsc, RsDataType dt, RsDataKind dk,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800229 bool isNorm, uint32_t vecSize) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700230 ObjectBaseRef<const Element> returnRef;
Jason Sams81549542010-02-17 15:38:10 -0800231 // Look for an existing match.
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700232 ObjectBase::asyncLock();
Jason Sams81549542010-02-17 15:38:10 -0800233 for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
234 const Element *ee = rsc->mStateElement.mElements[ct];
235 if (!ee->getFieldCount() &&
236 (ee->getComponent().getType() == dt) &&
237 (ee->getComponent().getKind() == dk) &&
238 (ee->getComponent().getIsNormalized() == isNorm) &&
239 (ee->getComponent().getVectorSize() == vecSize)) {
240 // Match
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700241 returnRef.set(ee);
242 ObjectBase::asyncUnlock();
Jason Sams81549542010-02-17 15:38:10 -0800243 return ee;
244 }
245 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700246 ObjectBase::asyncUnlock();
Jason Sams81549542010-02-17 15:38:10 -0800247
Tim Murray665eafe2014-07-01 10:04:48 -0700248 // Element objects must use allocator specified by the driver
249 void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Element), 0);
250 if (!allocMem) {
251 rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Element");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700252 return nullptr;
Tim Murray665eafe2014-07-01 10:04:48 -0700253 }
254
255 Element *e = new (allocMem) Element(rsc);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700256 returnRef.set(e);
Jason Samsd01d9702009-12-23 14:35:29 -0800257 e->mComponent.set(dt, dk, isNorm, vecSize);
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800258 e->compute();
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700259
Tim Murraye3af53b2014-06-10 09:46:51 -0700260#ifdef RS_FIND_OFFSETS
261 ALOGE("pointer for element: %p", e);
262 ALOGE("pointer for element.drv: %p", &e->mHal.drv);
263#endif
264
265
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700266 ObjectBase::asyncLock();
Yang Nib8353c52015-02-14 18:00:59 -0800267 rsc->mStateElement.mElements.push(e);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700268 ObjectBase::asyncUnlock();
269
270 return returnRef;
Jason Sams4815c0d2009-12-15 12:58:36 -0800271}
272
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700273ObjectBaseRef<const Element> Element::createRef(Context *rsc, size_t count, const Element **ein,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800274 const char **nin, const size_t * lengths, const uint32_t *asin) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700275
276 ObjectBaseRef<const Element> returnRef;
Jason Sams81549542010-02-17 15:38:10 -0800277 // Look for an existing match.
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700278 ObjectBase::asyncLock();
Jason Sams81549542010-02-17 15:38:10 -0800279 for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
280 const Element *ee = rsc->mStateElement.mElements[ct];
281 if (ee->getFieldCount() == count) {
282 bool match = true;
283 for (uint32_t i=0; i < count; i++) {
Jason Samsf313dc32013-07-09 14:29:39 -0700284 size_t len;
285 uint32_t asize = 1;
286 if (lengths) {
287 len = lengths[i];
288 } else {
289 len = strlen(nin[i]);
290 }
291 if (asin) {
292 asize = asin[i];
293 }
294
Jason Sams81549542010-02-17 15:38:10 -0800295 if ((ee->mFields[i].e.get() != ein[i]) ||
Jason Sams48ecf6a2013-07-09 15:35:29 -0700296 (strlen(ee->mFields[i].name) != len) ||
297 strcmp(ee->mFields[i].name, nin[i]) ||
Jason Samsf313dc32013-07-09 14:29:39 -0700298 (ee->mFields[i].arraySize != asize)) {
Jason Sams81549542010-02-17 15:38:10 -0800299 match = false;
300 break;
301 }
302 }
303 if (match) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700304 returnRef.set(ee);
305 ObjectBase::asyncUnlock();
306 return returnRef;
Jason Sams81549542010-02-17 15:38:10 -0800307 }
308 }
309 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700310 ObjectBase::asyncUnlock();
Jason Sams81549542010-02-17 15:38:10 -0800311
Tim Murray665eafe2014-07-01 10:04:48 -0700312 // Element objects must use allocator specified by the driver
313 void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Element), 0);
314 if (!allocMem) {
315 rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Element");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700316 return nullptr;
Tim Murray665eafe2014-07-01 10:04:48 -0700317 }
318
319 Element *e = new (allocMem) Element(rsc);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700320 returnRef.set(e);
Jason Sams4815c0d2009-12-15 12:58:36 -0800321 e->mFields = new ElementField_t [count];
322 e->mFieldCount = count;
Jason Sams4815c0d2009-12-15 12:58:36 -0800323 for (size_t ct=0; ct < count; ct++) {
Jason Samsf313dc32013-07-09 14:29:39 -0700324 size_t len;
325 uint32_t asize = 1;
326 if (lengths) {
327 len = lengths[ct];
328 } else {
329 len = strlen(nin[ct]);
330 }
331 if (asin) {
332 asize = asin[ct];
333 }
334
Jason Sams4815c0d2009-12-15 12:58:36 -0800335 e->mFields[ct].e.set(ein[ct]);
Jason Sams48ecf6a2013-07-09 15:35:29 -0700336 e->mFields[ct].name = rsuCopyString(nin[ct], len);
Jason Samsf313dc32013-07-09 14:29:39 -0700337 e->mFields[ct].arraySize = asize;
Jason Sams4815c0d2009-12-15 12:58:36 -0800338 }
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800339 e->compute();
Jason Sams4815c0d2009-12-15 12:58:36 -0800340
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700341 ObjectBase::asyncLock();
Yang Nib8353c52015-02-14 18:00:59 -0800342 rsc->mStateElement.mElements.push(e);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700343 ObjectBase::asyncUnlock();
Jason Sams4815c0d2009-12-15 12:58:36 -0800344
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700345 return returnRef;
Jason Samsb4d35682010-01-04 16:52:27 -0800346}
Jason Samsd01d9702009-12-23 14:35:29 -0800347
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800348void Element::incRefs(const void *ptr) const {
Jason Samse3929c92010-08-09 18:13:33 -0700349 if (!mFieldCount) {
350 if (mComponent.isReference()) {
351 ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr);
352 ObjectBase *ob = obp[0];
Jason Sams10e5e572010-08-12 12:44:02 -0700353 if (ob) ob->incSysRef();
Jason Samse3929c92010-08-09 18:13:33 -0700354 }
355 return;
356 }
357
358 const uint8_t *p = static_cast<const uint8_t *>(ptr);
359 for (uint32_t i=0; i < mFieldCount; i++) {
360 if (mFields[i].e->mHasReference) {
Alex Sakhartchouk49464a92011-07-12 13:25:14 -0700361 const uint8_t *p2 = &p[mFields[i].offsetBits >> 3];
Jason Sams46e45542010-09-02 17:35:23 -0700362 for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) {
Alex Sakhartchouk49464a92011-07-12 13:25:14 -0700363 mFields[i].e->incRefs(p2);
364 p2 += mFields[i].e->getSizeBytes();
Jason Sams46e45542010-09-02 17:35:23 -0700365 }
Jason Samse3929c92010-08-09 18:13:33 -0700366 }
367 }
368}
369
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800370void Element::decRefs(const void *ptr) const {
Jason Samse3929c92010-08-09 18:13:33 -0700371 if (!mFieldCount) {
372 if (mComponent.isReference()) {
373 ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr);
374 ObjectBase *ob = obp[0];
Jason Sams10e5e572010-08-12 12:44:02 -0700375 if (ob) ob->decSysRef();
Jason Samse3929c92010-08-09 18:13:33 -0700376 }
377 return;
378 }
379
380 const uint8_t *p = static_cast<const uint8_t *>(ptr);
381 for (uint32_t i=0; i < mFieldCount; i++) {
382 if (mFields[i].e->mHasReference) {
Alex Sakhartchouk49464a92011-07-12 13:25:14 -0700383 const uint8_t *p2 = &p[mFields[i].offsetBits >> 3];
Jason Sams46e45542010-09-02 17:35:23 -0700384 for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) {
Alex Sakhartchouk49464a92011-07-12 13:25:14 -0700385 mFields[i].e->decRefs(p2);
386 p2 += mFields[i].e->getSizeBytes();
Jason Sams46e45542010-09-02 17:35:23 -0700387 }
Jason Samse3929c92010-08-09 18:13:33 -0700388 }
389 }
390}
Jason Samsd01d9702009-12-23 14:35:29 -0800391
Jason Samsa36c50a2014-06-17 12:06:06 -0700392void Element::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700393 if (rsc->mHal.funcs.element.updateCachedObject != nullptr) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700394 rsc->mHal.funcs.element.updateCachedObject(rsc, this, (rs_element *)dstObj);
395 } else {
396 *((const void **)dstObj) = this;
397 }
398}
399
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800400ElementState::ElementState() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700401}
402
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800403ElementState::~ElementState() {
Jason Sams81549542010-02-17 15:38:10 -0800404 rsAssert(!mElements.size());
Jason Sams326e0dd2009-05-22 14:03:28 -0700405}
406
407/////////////////////////////////////////
Jason Samse5ffb872009-08-09 17:01:55 -0700408//
Jason Sams326e0dd2009-05-22 14:03:28 -0700409
410namespace android {
411namespace renderscript {
412
Jason Samsd01d9702009-12-23 14:35:29 -0800413RsElement rsi_ElementCreate(Context *rsc,
414 RsDataType dt,
415 RsDataKind dk,
416 bool norm,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800417 uint32_t vecSize) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700418 return (RsElement)Element::create(rsc, dt, dk, norm, vecSize);
Jason Sams326e0dd2009-05-22 14:03:28 -0700419}
420
Jason Sams20087472011-05-06 14:14:30 -0700421
Jason Samsd01d9702009-12-23 14:35:29 -0800422RsElement rsi_ElementCreate2(Context *rsc,
Jason Samsd01d9702009-12-23 14:35:29 -0800423 const RsElement * ein,
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700424 size_t ein_length,
Jason Sams20087472011-05-06 14:14:30 -0700425
Jason Samsd01d9702009-12-23 14:35:29 -0800426 const char ** names,
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700427 size_t nameLengths_length,
Jason Sams20087472011-05-06 14:14:30 -0700428 const size_t * nameLengths,
429
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700430 const uint32_t * arraySizes,
431 size_t arraySizes_length) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700432 return (RsElement)Element::create(rsc, ein_length, (const Element **)ein,
433 names, nameLengths, arraySizes);
Jason Samsd01d9702009-12-23 14:35:29 -0800434}
435
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700436}
437}
438
Tim Murrayc2ce7072013-07-17 18:38:53 -0700439extern "C" void rsaElementGetNativeData(RsContext con, RsElement elem,
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700440 uint32_t *elemData, uint32_t elemDataSize) {
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700441 rsAssert(elemDataSize == 5);
442 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
443 Element *e = static_cast<Element *>(elem);
444
445 (*elemData++) = (uint32_t)e->getType();
446 (*elemData++) = (uint32_t)e->getKind();
447 (*elemData++) = e->getComponent().getIsNormalized() ? 1 : 0;
448 (*elemData++) = e->getComponent().getVectorSize();
449 (*elemData++) = e->getFieldCount();
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700450}
451
Tim Murrayc2ce7072013-07-17 18:38:53 -0700452extern "C" void rsaElementGetSubElements(RsContext con, RsElement elem, uintptr_t *ids,
Tim Murray099bc262013-03-20 16:54:03 -0700453 const char **names, size_t *arraySizes, uint32_t dataSize) {
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700454 Element *e = static_cast<Element *>(elem);
455 rsAssert(e->getFieldCount() == dataSize);
456
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800457 for (uint32_t i = 0; i < dataSize; i ++) {
Jason Samsf0c1df42010-10-26 13:09:17 -0700458 e->getField(i)->incUserRef();
Tim Murray099bc262013-03-20 16:54:03 -0700459 ids[i] = (uintptr_t)e->getField(i);
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700460 names[i] = e->getFieldName(i);
Alex Sakhartchouk31aca7d2011-10-18 11:08:31 -0700461 arraySizes[i] = e->getFieldArraySize(i);
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700462 }
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700463}