blob: 71e1b91c6371d4167cdcfb1e5991838d0a167f9e [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;
Jason Sams4815c0d2009-12-15 12:58:36 -080026 mFields = NULL;
27 mFieldCount = 0;
Jason Samse3929c92010-08-09 18:13:33 -070028 mHasReference = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070029}
30
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080031Element::~Element() {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -070032 clear();
33}
34
35void Element::preDestroy() const {
Jason Sams81549542010-02-17 15:38:10 -080036 for (uint32_t ct = 0; ct < mRSC->mStateElement.mElements.size(); ct++) {
37 if (mRSC->mStateElement.mElements[ct] == this) {
38 mRSC->mStateElement.mElements.removeAt(ct);
39 break;
40 }
41 }
Jason Sams326e0dd2009-05-22 14:03:28 -070042}
43
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080044void Element::clear() {
Jason Sams4815c0d2009-12-15 12:58:36 -080045 delete [] mFields;
46 mFields = NULL;
47 mFieldCount = 0;
Jason Samse3929c92010-08-09 18:13:33 -070048 mHasReference = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070049}
Jason Sams326e0dd2009-05-22 14:03:28 -070050
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080051size_t Element::getSizeBits() const {
Jason Sams4815c0d2009-12-15 12:58:36 -080052 if (!mFieldCount) {
53 return mBits;
54 }
55
Jason Sams326e0dd2009-05-22 14:03:28 -070056 size_t total = 0;
Jason Sams4815c0d2009-12-15 12:58:36 -080057 for (size_t ct=0; ct < mFieldCount; ct++) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070058 total += mFields[ct].e->mBits * mFields[ct].arraySize;
Jason Sams326e0dd2009-05-22 14:03:28 -070059 }
60 return total;
61}
62
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080063void Element::dumpLOGV(const char *prefix) const {
Jason Samse12c1c52009-09-27 17:50:38 -070064 ObjectBase::dumpLOGV(prefix);
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -080065 LOGV("%s Element: fieldCount: %zu, size bytes: %zu", prefix, mFieldCount, getSizeBytes());
Alex Sakhartchoukc700e642011-08-16 13:09:46 -070066 mComponent.dumpLOGV(prefix);
Jason Sams4815c0d2009-12-15 12:58:36 -080067 for (uint32_t ct = 0; ct < mFieldCount; ct++) {
Alex Sakhartchouk963bb452010-10-13 14:22:02 -070068 LOGV("%s Element field index: %u ------------------", prefix, ct);
69 LOGV("%s name: %s, offsetBits: %u, arraySize: %u",
70 prefix, mFields[ct].name.string(), mFields[ct].offsetBits, mFields[ct].arraySize);
71 mFields[ct].e->dumpLOGV(prefix);
Jason Samse12c1c52009-09-27 17:50:38 -070072 }
73}
74
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080075void Element::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070076 // Need to identify ourselves
77 stream->addU32((uint32_t)getClassId());
78
79 String8 name(getName());
80 stream->addString(&name);
81
82 mComponent.serialize(stream);
83
84 // Now serialize all the fields
85 stream->addU32(mFieldCount);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080086 for (uint32_t ct = 0; ct < mFieldCount; ct++) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070087 stream->addString(&mFields[ct].name);
Jason Sams46e45542010-09-02 17:35:23 -070088 stream->addU32(mFields[ct].arraySize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070089 mFields[ct].e->serialize(stream);
90 }
91}
92
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080093Element *Element::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070094 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -070095 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080096 if (classID != RS_A3D_CLASS_ID_ELEMENT) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070097 LOGE("element loading skipped due to invalid class id\n");
98 return NULL;
99 }
100
101 String8 name;
102 stream->loadString(&name);
103
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700104 Component component;
105 component.loadFromStream(stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700106
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700107 uint32_t fieldCount = stream->loadU32();
108 if (!fieldCount) {
109 return (Element *)Element::create(rsc,
110 component.getType(),
111 component.getKind(),
112 component.getIsNormalized(),
113 component.getVectorSize());;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700114 }
115
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700116 const Element **subElems = new const Element *[fieldCount];
117 const char **subElemNames = new const char *[fieldCount];
118 size_t *subElemNamesLengths = new size_t[fieldCount];
119 uint32_t *arraySizes = new uint32_t[fieldCount];
120
121 String8 elemName;
122 for (uint32_t ct = 0; ct < fieldCount; ct ++) {
123 stream->loadString(&elemName);
124 subElemNamesLengths[ct] = elemName.length();
125 char *tmpName = new char[subElemNamesLengths[ct]];
126 memcpy(tmpName, elemName.string(), subElemNamesLengths[ct]);
127 subElemNames[ct] = tmpName;
128 arraySizes[ct] = stream->loadU32();
129 subElems[ct] = Element::createFromStream(rsc, stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700130 }
131
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700132 const Element *elem = Element::create(rsc, fieldCount, subElems, subElemNames,
133 subElemNamesLengths, arraySizes);
134 for (uint32_t ct = 0; ct < fieldCount; ct ++) {
135 delete [] subElemNames[ct];
136 subElems[ct]->decUserRef();
137 }
138 delete[] subElems;
139 delete[] subElemNames;
140 delete[] subElemNamesLengths;
141 delete[] arraySizes;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700142
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700143 return (Element *)elem;
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700144}
Jason Samsd01d9702009-12-23 14:35:29 -0800145
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800146void Element::compute() {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800147 if (mFieldCount == 0) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800148 mBits = mComponent.getBits();
149 mHasReference = mComponent.isReference();
150 return;
151 }
152
153 size_t bits = 0;
154 for (size_t ct=0; ct < mFieldCount; ct++) {
155 mFields[ct].offsetBits = bits;
156 bits += mFields[ct].e->getSizeBits() * mFields[ct].arraySize;
157
158 if (mFields[ct].e->mHasReference) {
159 mHasReference = true;
160 }
161 }
162
163}
164
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700165ObjectBaseRef<const Element> Element::createRef(Context *rsc, RsDataType dt, RsDataKind dk,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800166 bool isNorm, uint32_t vecSize) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700167 ObjectBaseRef<const Element> returnRef;
Jason Sams81549542010-02-17 15:38:10 -0800168 // Look for an existing match.
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700169 ObjectBase::asyncLock();
Jason Sams81549542010-02-17 15:38:10 -0800170 for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
171 const Element *ee = rsc->mStateElement.mElements[ct];
172 if (!ee->getFieldCount() &&
173 (ee->getComponent().getType() == dt) &&
174 (ee->getComponent().getKind() == dk) &&
175 (ee->getComponent().getIsNormalized() == isNorm) &&
176 (ee->getComponent().getVectorSize() == vecSize)) {
177 // Match
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700178 returnRef.set(ee);
179 ObjectBase::asyncUnlock();
Jason Sams81549542010-02-17 15:38:10 -0800180 return ee;
181 }
182 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700183 ObjectBase::asyncUnlock();
Jason Sams81549542010-02-17 15:38:10 -0800184
Jason Sams4815c0d2009-12-15 12:58:36 -0800185 Element *e = new Element(rsc);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700186 returnRef.set(e);
Jason Samsd01d9702009-12-23 14:35:29 -0800187 e->mComponent.set(dt, dk, isNorm, vecSize);
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800188 e->compute();
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700189
190 ObjectBase::asyncLock();
Jason Sams81549542010-02-17 15:38:10 -0800191 rsc->mStateElement.mElements.push(e);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700192 ObjectBase::asyncUnlock();
193
194 return returnRef;
Jason Sams4815c0d2009-12-15 12:58:36 -0800195}
196
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700197ObjectBaseRef<const Element> Element::createRef(Context *rsc, size_t count, const Element **ein,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800198 const char **nin, const size_t * lengths, const uint32_t *asin) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700199
200 ObjectBaseRef<const Element> returnRef;
Jason Sams81549542010-02-17 15:38:10 -0800201 // Look for an existing match.
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700202 ObjectBase::asyncLock();
Jason Sams81549542010-02-17 15:38:10 -0800203 for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
204 const Element *ee = rsc->mStateElement.mElements[ct];
205 if (ee->getFieldCount() == count) {
206 bool match = true;
207 for (uint32_t i=0; i < count; i++) {
208 if ((ee->mFields[i].e.get() != ein[i]) ||
209 (ee->mFields[i].name.length() != lengths[i]) ||
Jason Sams46e45542010-09-02 17:35:23 -0700210 (ee->mFields[i].name != nin[i]) ||
211 (ee->mFields[i].arraySize != asin[i])) {
Jason Sams81549542010-02-17 15:38:10 -0800212 match = false;
213 break;
214 }
215 }
216 if (match) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700217 returnRef.set(ee);
218 ObjectBase::asyncUnlock();
219 return returnRef;
Jason Sams81549542010-02-17 15:38:10 -0800220 }
221 }
222 }
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700223 ObjectBase::asyncUnlock();
Jason Sams81549542010-02-17 15:38:10 -0800224
Jason Sams4815c0d2009-12-15 12:58:36 -0800225 Element *e = new Element(rsc);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700226 returnRef.set(e);
Jason Sams4815c0d2009-12-15 12:58:36 -0800227 e->mFields = new ElementField_t [count];
228 e->mFieldCount = count;
Jason Sams4815c0d2009-12-15 12:58:36 -0800229 for (size_t ct=0; ct < count; ct++) {
230 e->mFields[ct].e.set(ein[ct]);
231 e->mFields[ct].name.setTo(nin[ct], lengths[ct]);
Jason Sams46e45542010-09-02 17:35:23 -0700232 e->mFields[ct].arraySize = asin[ct];
Jason Sams4815c0d2009-12-15 12:58:36 -0800233 }
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800234 e->compute();
Jason Sams4815c0d2009-12-15 12:58:36 -0800235
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700236 ObjectBase::asyncLock();
Jason Sams81549542010-02-17 15:38:10 -0800237 rsc->mStateElement.mElements.push(e);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700238 ObjectBase::asyncUnlock();
Jason Sams4815c0d2009-12-15 12:58:36 -0800239
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700240 return returnRef;
Jason Samsb4d35682010-01-04 16:52:27 -0800241}
Jason Samsd01d9702009-12-23 14:35:29 -0800242
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800243void Element::incRefs(const void *ptr) const {
Jason Samse3929c92010-08-09 18:13:33 -0700244 if (!mFieldCount) {
245 if (mComponent.isReference()) {
246 ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr);
247 ObjectBase *ob = obp[0];
Jason Sams10e5e572010-08-12 12:44:02 -0700248 if (ob) ob->incSysRef();
Jason Samse3929c92010-08-09 18:13:33 -0700249 }
250 return;
251 }
252
253 const uint8_t *p = static_cast<const uint8_t *>(ptr);
254 for (uint32_t i=0; i < mFieldCount; i++) {
255 if (mFields[i].e->mHasReference) {
Alex Sakhartchouk49464a92011-07-12 13:25:14 -0700256 const uint8_t *p2 = &p[mFields[i].offsetBits >> 3];
Jason Sams46e45542010-09-02 17:35:23 -0700257 for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) {
Alex Sakhartchouk49464a92011-07-12 13:25:14 -0700258 mFields[i].e->incRefs(p2);
259 p2 += mFields[i].e->getSizeBytes();
Jason Sams46e45542010-09-02 17:35:23 -0700260 }
Jason Samse3929c92010-08-09 18:13:33 -0700261 }
262 }
263}
264
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800265void Element::decRefs(const void *ptr) const {
Jason Samse3929c92010-08-09 18:13:33 -0700266 if (!mFieldCount) {
267 if (mComponent.isReference()) {
268 ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr);
269 ObjectBase *ob = obp[0];
Jason Sams10e5e572010-08-12 12:44:02 -0700270 if (ob) ob->decSysRef();
Jason Samse3929c92010-08-09 18:13:33 -0700271 }
272 return;
273 }
274
275 const uint8_t *p = static_cast<const uint8_t *>(ptr);
276 for (uint32_t i=0; i < mFieldCount; i++) {
277 if (mFields[i].e->mHasReference) {
Alex Sakhartchouk49464a92011-07-12 13:25:14 -0700278 const uint8_t *p2 = &p[mFields[i].offsetBits >> 3];
Jason Sams46e45542010-09-02 17:35:23 -0700279 for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) {
Alex Sakhartchouk49464a92011-07-12 13:25:14 -0700280 mFields[i].e->decRefs(p2);
281 p2 += mFields[i].e->getSizeBytes();
Jason Sams46e45542010-09-02 17:35:23 -0700282 }
Jason Samse3929c92010-08-09 18:13:33 -0700283 }
284 }
285}
Jason Samsd01d9702009-12-23 14:35:29 -0800286
Alex Sakhartchouk98cc3552011-08-19 09:43:18 -0700287Element::Builder::Builder() {
288 const uint32_t initialCapacity = 32;
289 mBuilderElementRefs.setCapacity(initialCapacity);
290 mBuilderElements.setCapacity(initialCapacity);
291 mBuilderNameStrings.setCapacity(initialCapacity);
292 mBuilderNameLengths.setCapacity(initialCapacity);
293 mBuilderArrays.setCapacity(initialCapacity);
294}
295
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700296void Element::Builder::add(const Element *e, const char *nameStr, uint32_t arraySize) {
297 mBuilderElementRefs.push(ObjectBaseRef<const Element>(e));
298 mBuilderElements.push(e);
299 mBuilderNameStrings.push(nameStr);
300 mBuilderNameLengths.push(strlen(nameStr));
301 mBuilderArrays.push(arraySize);
302
303}
304
305ObjectBaseRef<const Element> Element::Builder::create(Context *rsc) {
306 return Element::createRef(rsc, mBuilderElements.size(),
307 &(mBuilderElements.editArray()[0]),
308 &(mBuilderNameStrings.editArray()[0]),
309 mBuilderNameLengths.editArray(),
310 mBuilderArrays.editArray());
311}
312
Jason Sams4815c0d2009-12-15 12:58:36 -0800313
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800314ElementState::ElementState() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700315}
316
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800317ElementState::~ElementState() {
Jason Sams81549542010-02-17 15:38:10 -0800318 rsAssert(!mElements.size());
Jason Sams326e0dd2009-05-22 14:03:28 -0700319}
320
321/////////////////////////////////////////
Jason Samse5ffb872009-08-09 17:01:55 -0700322//
Jason Sams326e0dd2009-05-22 14:03:28 -0700323
324namespace android {
325namespace renderscript {
326
Jason Samsd01d9702009-12-23 14:35:29 -0800327RsElement rsi_ElementCreate(Context *rsc,
328 RsDataType dt,
329 RsDataKind dk,
330 bool norm,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800331 uint32_t vecSize) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700332 return (RsElement)Element::create(rsc, dt, dk, norm, vecSize);
Jason Sams326e0dd2009-05-22 14:03:28 -0700333}
334
Jason Sams20087472011-05-06 14:14:30 -0700335
Jason Samsd01d9702009-12-23 14:35:29 -0800336RsElement rsi_ElementCreate2(Context *rsc,
Jason Samsd01d9702009-12-23 14:35:29 -0800337 const RsElement * ein,
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700338 size_t ein_length,
Jason Sams20087472011-05-06 14:14:30 -0700339
Jason Samsd01d9702009-12-23 14:35:29 -0800340 const char ** names,
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700341 size_t nameLengths_length,
Jason Sams20087472011-05-06 14:14:30 -0700342 const size_t * nameLengths,
343
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700344 const uint32_t * arraySizes,
345 size_t arraySizes_length) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700346 return (RsElement)Element::create(rsc, ein_length, (const Element **)ein,
347 names, nameLengths, arraySizes);
Jason Samsd01d9702009-12-23 14:35:29 -0800348}
349
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700350}
351}
352
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700353void rsaElementGetNativeData(RsContext con, RsElement elem,
354 uint32_t *elemData, uint32_t elemDataSize) {
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700355 rsAssert(elemDataSize == 5);
356 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
357 Element *e = static_cast<Element *>(elem);
358
359 (*elemData++) = (uint32_t)e->getType();
360 (*elemData++) = (uint32_t)e->getKind();
361 (*elemData++) = e->getComponent().getIsNormalized() ? 1 : 0;
362 (*elemData++) = e->getComponent().getVectorSize();
363 (*elemData++) = e->getFieldCount();
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700364}
365
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700366void rsaElementGetSubElements(RsContext con, RsElement elem, uint32_t *ids,
Alex Sakhartchouk31aca7d2011-10-18 11:08:31 -0700367 const char **names, uint32_t *arraySizes, uint32_t dataSize) {
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700368 Element *e = static_cast<Element *>(elem);
369 rsAssert(e->getFieldCount() == dataSize);
370
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800371 for (uint32_t i = 0; i < dataSize; i ++) {
Jason Samsf0c1df42010-10-26 13:09:17 -0700372 e->getField(i)->incUserRef();
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700373 ids[i] = (uint32_t)e->getField(i);
374 names[i] = e->getFieldName(i);
Alex Sakhartchouk31aca7d2011-10-18 11:08:31 -0700375 arraySizes[i] = e->getFieldArraySize(i);
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700376 }
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700377}