Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 17 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 18 | #include "rsContext.h" |
Jason Sams | 565ac36 | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 19 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 20 | using namespace android; |
| 21 | using namespace android::renderscript; |
| 22 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 23 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 24 | Element::Element(Context *rsc) : ObjectBase(rsc) { |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 25 | mBits = 0; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 26 | mFields = NULL; |
| 27 | mFieldCount = 0; |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 28 | mHasReference = false; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 31 | Element::~Element() { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 32 | clear(); |
| 33 | } |
| 34 | |
| 35 | void Element::preDestroy() const { |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 36 | 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 Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 44 | void Element::clear() { |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 45 | delete [] mFields; |
| 46 | mFields = NULL; |
| 47 | mFieldCount = 0; |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 48 | mHasReference = false; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 49 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 50 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 51 | size_t Element::getSizeBits() const { |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 52 | if (!mFieldCount) { |
| 53 | return mBits; |
| 54 | } |
| 55 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 56 | size_t total = 0; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 57 | for (size_t ct=0; ct < mFieldCount; ct++) { |
Alex Sakhartchouk | 39f2ef6 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 58 | total += mFields[ct].e->mBits * mFields[ct].arraySize; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 59 | } |
| 60 | return total; |
| 61 | } |
| 62 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 63 | void Element::dumpLOGV(const char *prefix) const { |
Jason Sams | e12c1c5 | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 64 | ObjectBase::dumpLOGV(prefix); |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 65 | LOGV("%s Element: fieldCount: %zu, size bytes: %zu", prefix, mFieldCount, getSizeBytes()); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 66 | mComponent.dumpLOGV(prefix); |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 67 | for (uint32_t ct = 0; ct < mFieldCount; ct++) { |
Alex Sakhartchouk | 963bb45 | 2010-10-13 14:22:02 -0700 | [diff] [blame] | 68 | 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 Sams | e12c1c5 | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 75 | void Element::serialize(OStream *stream) const { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 76 | // 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 Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 86 | for (uint32_t ct = 0; ct < mFieldCount; ct++) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 87 | stream->addString(&mFields[ct].name); |
Jason Sams | 46e4554 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 88 | stream->addU32(mFields[ct].arraySize); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 89 | mFields[ct].e->serialize(stream); |
| 90 | } |
| 91 | } |
| 92 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 93 | Element *Element::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 94 | // First make sure we are reading the correct object |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 95 | RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 96 | if (classID != RS_A3D_CLASS_ID_ELEMENT) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 97 | LOGE("element loading skipped due to invalid class id\n"); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | String8 name; |
| 102 | stream->loadString(&name); |
| 103 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 104 | Component component; |
| 105 | component.loadFromStream(stream); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 106 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 107 | 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 Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 116 | 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 Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 132 | 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 Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 142 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 143 | return (Element *)elem; |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 144 | } |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 145 | |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 146 | void Element::compute() { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 147 | if (mFieldCount == 0) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 148 | 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 Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 165 | ObjectBaseRef<const Element> Element::createRef(Context *rsc, RsDataType dt, RsDataKind dk, |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 166 | bool isNorm, uint32_t vecSize) { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 167 | ObjectBaseRef<const Element> returnRef; |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 168 | // Look for an existing match. |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 169 | ObjectBase::asyncLock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 170 | 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 Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 178 | returnRef.set(ee); |
| 179 | ObjectBase::asyncUnlock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 180 | return ee; |
| 181 | } |
| 182 | } |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 183 | ObjectBase::asyncUnlock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 184 | |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 185 | Element *e = new Element(rsc); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 186 | returnRef.set(e); |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 187 | e->mComponent.set(dt, dk, isNorm, vecSize); |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 188 | e->compute(); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 189 | |
| 190 | ObjectBase::asyncLock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 191 | rsc->mStateElement.mElements.push(e); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 192 | ObjectBase::asyncUnlock(); |
| 193 | |
| 194 | return returnRef; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 197 | ObjectBaseRef<const Element> Element::createRef(Context *rsc, size_t count, const Element **ein, |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 198 | const char **nin, const size_t * lengths, const uint32_t *asin) { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 199 | |
| 200 | ObjectBaseRef<const Element> returnRef; |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 201 | // Look for an existing match. |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 202 | ObjectBase::asyncLock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 203 | 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 Sams | 46e4554 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 210 | (ee->mFields[i].name != nin[i]) || |
| 211 | (ee->mFields[i].arraySize != asin[i])) { |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 212 | match = false; |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | if (match) { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 217 | returnRef.set(ee); |
| 218 | ObjectBase::asyncUnlock(); |
| 219 | return returnRef; |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | } |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 223 | ObjectBase::asyncUnlock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 224 | |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 225 | Element *e = new Element(rsc); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 226 | returnRef.set(e); |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 227 | e->mFields = new ElementField_t [count]; |
| 228 | e->mFieldCount = count; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 229 | 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 Sams | 46e4554 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 232 | e->mFields[ct].arraySize = asin[ct]; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 233 | } |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 234 | e->compute(); |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 235 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 236 | ObjectBase::asyncLock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 237 | rsc->mStateElement.mElements.push(e); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 238 | ObjectBase::asyncUnlock(); |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 239 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 240 | return returnRef; |
Jason Sams | b4d3568 | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 241 | } |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 242 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 243 | void Element::incRefs(const void *ptr) const { |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 244 | if (!mFieldCount) { |
| 245 | if (mComponent.isReference()) { |
| 246 | ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr); |
| 247 | ObjectBase *ob = obp[0]; |
Jason Sams | 10e5e57 | 2010-08-12 12:44:02 -0700 | [diff] [blame] | 248 | if (ob) ob->incSysRef(); |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 249 | } |
| 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 Sakhartchouk | 49464a9 | 2011-07-12 13:25:14 -0700 | [diff] [blame] | 256 | const uint8_t *p2 = &p[mFields[i].offsetBits >> 3]; |
Jason Sams | 46e4554 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 257 | for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) { |
Alex Sakhartchouk | 49464a9 | 2011-07-12 13:25:14 -0700 | [diff] [blame] | 258 | mFields[i].e->incRefs(p2); |
| 259 | p2 += mFields[i].e->getSizeBytes(); |
Jason Sams | 46e4554 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 260 | } |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 265 | void Element::decRefs(const void *ptr) const { |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 266 | if (!mFieldCount) { |
| 267 | if (mComponent.isReference()) { |
| 268 | ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr); |
| 269 | ObjectBase *ob = obp[0]; |
Jason Sams | 10e5e57 | 2010-08-12 12:44:02 -0700 | [diff] [blame] | 270 | if (ob) ob->decSysRef(); |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 271 | } |
| 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 Sakhartchouk | 49464a9 | 2011-07-12 13:25:14 -0700 | [diff] [blame] | 278 | const uint8_t *p2 = &p[mFields[i].offsetBits >> 3]; |
Jason Sams | 46e4554 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 279 | for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) { |
Alex Sakhartchouk | 49464a9 | 2011-07-12 13:25:14 -0700 | [diff] [blame] | 280 | mFields[i].e->decRefs(p2); |
| 281 | p2 += mFields[i].e->getSizeBytes(); |
Jason Sams | 46e4554 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 282 | } |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | } |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 286 | |
Alex Sakhartchouk | 98cc355 | 2011-08-19 09:43:18 -0700 | [diff] [blame] | 287 | Element::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 Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 296 | void 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 | |
| 305 | ObjectBaseRef<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 Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 313 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 314 | ElementState::ElementState() { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 317 | ElementState::~ElementState() { |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 318 | rsAssert(!mElements.size()); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | ///////////////////////////////////////// |
Jason Sams | e5ffb87 | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 322 | // |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 323 | |
| 324 | namespace android { |
| 325 | namespace renderscript { |
| 326 | |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 327 | RsElement rsi_ElementCreate(Context *rsc, |
| 328 | RsDataType dt, |
| 329 | RsDataKind dk, |
| 330 | bool norm, |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 331 | uint32_t vecSize) { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 332 | return (RsElement)Element::create(rsc, dt, dk, norm, vecSize); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 335 | |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 336 | RsElement rsi_ElementCreate2(Context *rsc, |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 337 | const RsElement * ein, |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 338 | size_t ein_length, |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 339 | |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 340 | const char ** names, |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 341 | size_t nameLengths_length, |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 342 | const size_t * nameLengths, |
| 343 | |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 344 | const uint32_t * arraySizes, |
| 345 | size_t arraySizes_length) { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 346 | return (RsElement)Element::create(rsc, ein_length, (const Element **)ein, |
| 347 | names, nameLengths, arraySizes); |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 353 | void rsaElementGetNativeData(RsContext con, RsElement elem, |
| 354 | uint32_t *elemData, uint32_t elemDataSize) { |
Alex Sakhartchouk | 417e6a4 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 355 | 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 Sakhartchouk | 417e6a4 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 366 | void rsaElementGetSubElements(RsContext con, RsElement elem, uint32_t *ids, |
Alex Sakhartchouk | 31aca7d | 2011-10-18 11:08:31 -0700 | [diff] [blame^] | 367 | const char **names, uint32_t *arraySizes, uint32_t dataSize) { |
Alex Sakhartchouk | 417e6a4 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 368 | Element *e = static_cast<Element *>(elem); |
| 369 | rsAssert(e->getFieldCount() == dataSize); |
| 370 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 371 | for (uint32_t i = 0; i < dataSize; i ++) { |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 372 | e->getField(i)->incUserRef(); |
Alex Sakhartchouk | 417e6a4 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 373 | ids[i] = (uint32_t)e->getField(i); |
| 374 | names[i] = e->getFieldName(i); |
Alex Sakhartchouk | 31aca7d | 2011-10-18 11:08:31 -0700 | [diff] [blame^] | 375 | arraySizes[i] = e->getFieldArraySize(i); |
Alex Sakhartchouk | 417e6a4 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 376 | } |
Alex Sakhartchouk | 417e6a4 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 377 | } |