blob: d6ab0dac064c68628ff02ed975037c92edc1c257 [file] [log] [blame]
Jason Samsd19f10d2009-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 Samsd19f10d2009-05-22 14:03:28 -070017
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070018#include "rsContext.h"
Jason Samse2ae85f2009-06-03 16:04:54 -070019
Jason Samsd19f10d2009-05-22 14:03:28 -070020using namespace android;
21using namespace android::renderscript;
22
Jason Samsd19f10d2009-05-22 14:03:28 -070023
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080024Element::Element(Context *rsc) : ObjectBase(rsc) {
Jason Sams0011bcf2009-12-15 12:58:36 -080025 mBits = 0;
Jason Sams0011bcf2009-12-15 12:58:36 -080026 mFields = NULL;
27 mFieldCount = 0;
Jason Samsb28ca96f2010-08-09 18:13:33 -070028 mHasReference = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070029}
30
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080031Element::~Element() {
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -070032 clear();
33}
34
35void Element::preDestroy() const {
Jason Samse4c487a2010-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 Samsd19f10d2009-05-22 14:03:28 -070042}
43
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080044void Element::clear() {
Jason Sams0011bcf2009-12-15 12:58:36 -080045 delete [] mFields;
46 mFields = NULL;
47 mFieldCount = 0;
Jason Samsb28ca96f2010-08-09 18:13:33 -070048 mHasReference = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070049}
Jason Samsd19f10d2009-05-22 14:03:28 -070050
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080051size_t Element::getSizeBits() const {
Jason Sams0011bcf2009-12-15 12:58:36 -080052 if (!mFieldCount) {
53 return mBits;
54 }
55
Jason Samsd19f10d2009-05-22 14:03:28 -070056 size_t total = 0;
Jason Sams0011bcf2009-12-15 12:58:36 -080057 for (size_t ct=0; ct < mFieldCount; ct++) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -070058 total += mFields[ct].e->mBits * mFields[ct].arraySize;
Jason Samsd19f10d2009-05-22 14:03:28 -070059 }
60 return total;
61}
62
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080063void Element::dumpLOGV(const char *prefix) const {
Jason Sams3c0dfba2009-09-27 17:50:38 -070064 ObjectBase::dumpLOGV(prefix);
Alex Sakhartchouka3b59602011-01-28 09:31:47 -080065 LOGV("%s Element: fieldCount: %zu, size bytes: %zu", prefix, mFieldCount, getSizeBytes());
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -070066 mComponent.dumpLOGV(prefix);
Jason Sams0011bcf2009-12-15 12:58:36 -080067 for (uint32_t ct = 0; ct < mFieldCount; ct++) {
Alex Sakhartchouk9e401bc2010-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 Sams3c0dfba2009-09-27 17:50:38 -070072 }
73}
74
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080075void Element::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-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 Sakhartchouked9f2102010-11-09 17:00:54 -080086 for (uint32_t ct = 0; ct < mFieldCount; ct++) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070087 stream->addString(&mFields[ct].name);
Jason Sams70d4e502010-09-02 17:35:23 -070088 stream->addU32(mFields[ct].arraySize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070089 mFields[ct].e->serialize(stream);
90 }
91}
92
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080093Element *Element::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070094 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070095 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080096 if (classID != RS_A3D_CLASS_ID_ELEMENT) {
Alex Sakhartchoukaa7d2882010-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 Sakhartchouk117abdb2011-08-16 13:09:46 -0700104 Component component;
105 component.loadFromStream(stream);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700106
Alex Sakhartchouk117abdb2011-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 Sakhartchoukaa7d2882010-05-21 12:53:13 -0700114 }
115
Alex Sakhartchouk117abdb2011-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 Sakhartchoukaa7d2882010-05-21 12:53:13 -0700130 }
131
Alex Sakhartchouk117abdb2011-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 Sakhartchoukaa7d2882010-05-21 12:53:13 -0700142
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700143 return (Element *)elem;
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700144}
Jason Sams718cd1f2009-12-23 14:35:29 -0800145
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800146void Element::compute() {
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800147 if (mFieldCount == 0) {
Alex Sakhartchouk9d71e212010-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 Sakhartchouk117abdb2011-08-16 13:09:46 -0700165ObjectBaseRef<const Element> Element::createRef(Context *rsc, RsDataType dt, RsDataKind dk,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800166 bool isNorm, uint32_t vecSize) {
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700167 ObjectBaseRef<const Element> returnRef;
Jason Samse4c487a2010-02-17 15:38:10 -0800168 // Look for an existing match.
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700169 ObjectBase::asyncLock();
Jason Samse4c487a2010-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 Sakhartchouk117abdb2011-08-16 13:09:46 -0700178 returnRef.set(ee);
179 ObjectBase::asyncUnlock();
Jason Samse4c487a2010-02-17 15:38:10 -0800180 return ee;
181 }
182 }
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700183 ObjectBase::asyncUnlock();
Jason Samse4c487a2010-02-17 15:38:10 -0800184
Jason Sams0011bcf2009-12-15 12:58:36 -0800185 Element *e = new Element(rsc);
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700186 returnRef.set(e);
Jason Sams718cd1f2009-12-23 14:35:29 -0800187 e->mComponent.set(dt, dk, isNorm, vecSize);
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800188 e->compute();
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700189
190 ObjectBase::asyncLock();
Jason Samse4c487a2010-02-17 15:38:10 -0800191 rsc->mStateElement.mElements.push(e);
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700192 ObjectBase::asyncUnlock();
193
194 return returnRef;
Jason Sams0011bcf2009-12-15 12:58:36 -0800195}
196
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700197ObjectBaseRef<const Element> Element::createRef(Context *rsc, size_t count, const Element **ein,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800198 const char **nin, const size_t * lengths, const uint32_t *asin) {
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700199
200 ObjectBaseRef<const Element> returnRef;
Jason Samse4c487a2010-02-17 15:38:10 -0800201 // Look for an existing match.
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700202 ObjectBase::asyncLock();
Jason Samse4c487a2010-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 Sams70d4e502010-09-02 17:35:23 -0700210 (ee->mFields[i].name != nin[i]) ||
211 (ee->mFields[i].arraySize != asin[i])) {
Jason Samse4c487a2010-02-17 15:38:10 -0800212 match = false;
213 break;
214 }
215 }
216 if (match) {
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700217 returnRef.set(ee);
218 ObjectBase::asyncUnlock();
219 return returnRef;
Jason Samse4c487a2010-02-17 15:38:10 -0800220 }
221 }
222 }
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700223 ObjectBase::asyncUnlock();
Jason Samse4c487a2010-02-17 15:38:10 -0800224
Jason Sams0011bcf2009-12-15 12:58:36 -0800225 Element *e = new Element(rsc);
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700226 returnRef.set(e);
Jason Sams0011bcf2009-12-15 12:58:36 -0800227 e->mFields = new ElementField_t [count];
228 e->mFieldCount = count;
Jason Sams0011bcf2009-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 Sams70d4e502010-09-02 17:35:23 -0700232 e->mFields[ct].arraySize = asin[ct];
Jason Sams0011bcf2009-12-15 12:58:36 -0800233 }
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800234 e->compute();
Jason Sams0011bcf2009-12-15 12:58:36 -0800235
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700236 ObjectBase::asyncLock();
Jason Samse4c487a2010-02-17 15:38:10 -0800237 rsc->mStateElement.mElements.push(e);
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700238 ObjectBase::asyncUnlock();
Jason Sams0011bcf2009-12-15 12:58:36 -0800239
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700240 return returnRef;
Jason Samse17964e2010-01-04 16:52:27 -0800241}
Jason Sams718cd1f2009-12-23 14:35:29 -0800242
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800243void Element::incRefs(const void *ptr) const {
Jason Samsb28ca96f2010-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 Samsee734982010-08-12 12:44:02 -0700248 if (ob) ob->incSysRef();
Jason Samsb28ca96f2010-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 Sakhartchouk4b013ee2011-07-12 13:25:14 -0700256 const uint8_t *p2 = &p[mFields[i].offsetBits >> 3];
Jason Sams70d4e502010-09-02 17:35:23 -0700257 for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) {
Alex Sakhartchouk4b013ee2011-07-12 13:25:14 -0700258 mFields[i].e->incRefs(p2);
259 p2 += mFields[i].e->getSizeBytes();
Jason Sams70d4e502010-09-02 17:35:23 -0700260 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700261 }
262 }
263}
264
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800265void Element::decRefs(const void *ptr) const {
Jason Samsb28ca96f2010-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 Samsee734982010-08-12 12:44:02 -0700270 if (ob) ob->decSysRef();
Jason Samsb28ca96f2010-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 Sakhartchouk4b013ee2011-07-12 13:25:14 -0700278 const uint8_t *p2 = &p[mFields[i].offsetBits >> 3];
Jason Sams70d4e502010-09-02 17:35:23 -0700279 for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) {
Alex Sakhartchouk4b013ee2011-07-12 13:25:14 -0700280 mFields[i].e->decRefs(p2);
281 p2 += mFields[i].e->getSizeBytes();
Jason Sams70d4e502010-09-02 17:35:23 -0700282 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700283 }
284 }
285}
Jason Sams718cd1f2009-12-23 14:35:29 -0800286
Alex Sakhartchouka8bb9212011-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 Sakhartchouk117abdb2011-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 Sams0011bcf2009-12-15 12:58:36 -0800313
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800314ElementState::ElementState() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700315}
316
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800317ElementState::~ElementState() {
Jason Samse4c487a2010-02-17 15:38:10 -0800318 rsAssert(!mElements.size());
Jason Samsd19f10d2009-05-22 14:03:28 -0700319}
320
321/////////////////////////////////////////
Jason Sams1bada8c2009-08-09 17:01:55 -0700322//
Jason Samsd19f10d2009-05-22 14:03:28 -0700323
324namespace android {
325namespace renderscript {
326
Jason Sams718cd1f2009-12-23 14:35:29 -0800327RsElement rsi_ElementCreate(Context *rsc,
328 RsDataType dt,
329 RsDataKind dk,
330 bool norm,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800331 uint32_t vecSize) {
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700332 return (RsElement)Element::create(rsc, dt, dk, norm, vecSize);
Jason Samsd19f10d2009-05-22 14:03:28 -0700333}
334
Jason Sams7a22e102011-05-06 14:14:30 -0700335
Jason Sams718cd1f2009-12-23 14:35:29 -0800336RsElement rsi_ElementCreate2(Context *rsc,
Jason Sams718cd1f2009-12-23 14:35:29 -0800337 const RsElement * ein,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -0700338 size_t ein_length,
Jason Sams7a22e102011-05-06 14:14:30 -0700339
Jason Sams718cd1f2009-12-23 14:35:29 -0800340 const char ** names,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -0700341 size_t nameLengths_length,
Jason Sams7a22e102011-05-06 14:14:30 -0700342 const size_t * nameLengths,
343
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -0700344 const uint32_t * arraySizes,
345 size_t arraySizes_length) {
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700346 return (RsElement)Element::create(rsc, ein_length, (const Element **)ein,
347 names, nameLengths, arraySizes);
Jason Sams718cd1f2009-12-23 14:35:29 -0800348}
349
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700350}
351}
352
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700353void rsaElementGetNativeData(RsContext con, RsElement elem,
354 uint32_t *elemData, uint32_t elemDataSize) {
Alex Sakhartchoukdfac8142010-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 Sakhartchoukdfac8142010-07-15 11:33:03 -0700364}
365
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700366void rsaElementGetSubElements(RsContext con, RsElement elem, uint32_t *ids,
367 const char **names, uint32_t dataSize) {
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700368 Element *e = static_cast<Element *>(elem);
369 rsAssert(e->getFieldCount() == dataSize);
370
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800371 for (uint32_t i = 0; i < dataSize; i ++) {
Jason Sams31a7e422010-10-26 13:09:17 -0700372 e->getField(i)->incUserRef();
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700373 ids[i] = (uint32_t)e->getField(i);
374 names[i] = e->getFieldName(i);
375 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700376}