Jason Sams | d19f10d | 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 | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 17 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 18 | #include "rsContext.h" |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 19 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 20 | using namespace android; |
| 21 | using namespace android::renderscript; |
| 22 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 23 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 24 | Element::Element(Context *rsc) : ObjectBase(rsc) { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 25 | mBits = 0; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 26 | mBitsUnpadded = 0; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 27 | mFields = NULL; |
| 28 | mFieldCount = 0; |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 29 | mHasReference = false; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 32 | Element::~Element() { |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 33 | clear(); |
| 34 | } |
| 35 | |
| 36 | void Element::preDestroy() const { |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 37 | for (uint32_t ct = 0; ct < mRSC->mStateElement.mElements.size(); ct++) { |
| 38 | if (mRSC->mStateElement.mElements[ct] == this) { |
| 39 | mRSC->mStateElement.mElements.removeAt(ct); |
| 40 | break; |
| 41 | } |
| 42 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 45 | void Element::clear() { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 46 | delete [] mFields; |
| 47 | mFields = NULL; |
| 48 | mFieldCount = 0; |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 49 | mHasReference = false; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 50 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 51 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 52 | size_t Element::getSizeBits() const { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 53 | if (!mFieldCount) { |
| 54 | return mBits; |
| 55 | } |
| 56 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 57 | size_t total = 0; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 58 | for (size_t ct=0; ct < mFieldCount; ct++) { |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 59 | total += mFields[ct].e->mBits * mFields[ct].arraySize; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 60 | } |
| 61 | return total; |
| 62 | } |
| 63 | |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 64 | size_t Element::getSizeBitsUnpadded() const { |
| 65 | if (!mFieldCount) { |
| 66 | return mBitsUnpadded; |
| 67 | } |
| 68 | |
| 69 | size_t total = 0; |
| 70 | for (size_t ct=0; ct < mFieldCount; ct++) { |
| 71 | total += mFields[ct].e->mBitsUnpadded * mFields[ct].arraySize; |
| 72 | } |
| 73 | return total; |
| 74 | } |
| 75 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 76 | void Element::dumpLOGV(const char *prefix) const { |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 77 | ObjectBase::dumpLOGV(prefix); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 78 | ALOGV("%s Element: fieldCount: %zu, size bytes: %zu", prefix, mFieldCount, getSizeBytes()); |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 79 | mComponent.dumpLOGV(prefix); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 80 | for (uint32_t ct = 0; ct < mFieldCount; ct++) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 81 | ALOGV("%s Element field index: %u ------------------", prefix, ct); |
| 82 | ALOGV("%s name: %s, offsetBits: %u, arraySize: %u", |
Alex Sakhartchouk | 9e401bc | 2010-10-13 14:22:02 -0700 | [diff] [blame] | 83 | prefix, mFields[ct].name.string(), mFields[ct].offsetBits, mFields[ct].arraySize); |
| 84 | mFields[ct].e->dumpLOGV(prefix); |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 88 | void Element::serialize(OStream *stream) const { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 89 | // Need to identify ourselves |
| 90 | stream->addU32((uint32_t)getClassId()); |
| 91 | |
| 92 | String8 name(getName()); |
| 93 | stream->addString(&name); |
| 94 | |
| 95 | mComponent.serialize(stream); |
| 96 | |
| 97 | // Now serialize all the fields |
| 98 | stream->addU32(mFieldCount); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 99 | for (uint32_t ct = 0; ct < mFieldCount; ct++) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 100 | stream->addString(&mFields[ct].name); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 101 | stream->addU32(mFields[ct].arraySize); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 102 | mFields[ct].e->serialize(stream); |
| 103 | } |
| 104 | } |
| 105 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 106 | Element *Element::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 107 | // First make sure we are reading the correct object |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 108 | RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 109 | if (classID != RS_A3D_CLASS_ID_ELEMENT) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 110 | ALOGE("element loading skipped due to invalid class id\n"); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | String8 name; |
| 115 | stream->loadString(&name); |
| 116 | |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 117 | Component component; |
| 118 | component.loadFromStream(stream); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 119 | |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 120 | uint32_t fieldCount = stream->loadU32(); |
| 121 | if (!fieldCount) { |
| 122 | return (Element *)Element::create(rsc, |
| 123 | component.getType(), |
| 124 | component.getKind(), |
| 125 | component.getIsNormalized(), |
| 126 | component.getVectorSize());; |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 129 | const Element **subElems = new const Element *[fieldCount]; |
| 130 | const char **subElemNames = new const char *[fieldCount]; |
| 131 | size_t *subElemNamesLengths = new size_t[fieldCount]; |
| 132 | uint32_t *arraySizes = new uint32_t[fieldCount]; |
| 133 | |
| 134 | String8 elemName; |
| 135 | for (uint32_t ct = 0; ct < fieldCount; ct ++) { |
| 136 | stream->loadString(&elemName); |
| 137 | subElemNamesLengths[ct] = elemName.length(); |
| 138 | char *tmpName = new char[subElemNamesLengths[ct]]; |
| 139 | memcpy(tmpName, elemName.string(), subElemNamesLengths[ct]); |
| 140 | subElemNames[ct] = tmpName; |
| 141 | arraySizes[ct] = stream->loadU32(); |
| 142 | subElems[ct] = Element::createFromStream(rsc, stream); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 145 | const Element *elem = Element::create(rsc, fieldCount, subElems, subElemNames, |
| 146 | subElemNamesLengths, arraySizes); |
| 147 | for (uint32_t ct = 0; ct < fieldCount; ct ++) { |
| 148 | delete [] subElemNames[ct]; |
| 149 | subElems[ct]->decUserRef(); |
| 150 | } |
| 151 | delete[] subElems; |
| 152 | delete[] subElemNames; |
| 153 | delete[] subElemNamesLengths; |
| 154 | delete[] arraySizes; |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 155 | |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 156 | return (Element *)elem; |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 157 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 158 | |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 159 | void Element::compute() { |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 160 | if (mFieldCount == 0) { |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 161 | mBits = mComponent.getBits(); |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 162 | mBitsUnpadded = mComponent.getBitsUnpadded(); |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 163 | mHasReference = mComponent.isReference(); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | size_t bits = 0; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 168 | size_t bitsUnpadded = 0; |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 169 | for (size_t ct=0; ct < mFieldCount; ct++) { |
| 170 | mFields[ct].offsetBits = bits; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 171 | mFields[ct].offsetBitsUnpadded = bitsUnpadded; |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 172 | bits += mFields[ct].e->getSizeBits() * mFields[ct].arraySize; |
Alex Sakhartchouk | e60149d | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 173 | bitsUnpadded += mFields[ct].e->getSizeBitsUnpadded() * mFields[ct].arraySize; |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 174 | |
| 175 | if (mFields[ct].e->mHasReference) { |
| 176 | mHasReference = true; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | } |
| 181 | |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 182 | ObjectBaseRef<const Element> Element::createRef(Context *rsc, RsDataType dt, RsDataKind dk, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 183 | bool isNorm, uint32_t vecSize) { |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 184 | ObjectBaseRef<const Element> returnRef; |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 185 | // Look for an existing match. |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 186 | ObjectBase::asyncLock(); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 187 | for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) { |
| 188 | const Element *ee = rsc->mStateElement.mElements[ct]; |
| 189 | if (!ee->getFieldCount() && |
| 190 | (ee->getComponent().getType() == dt) && |
| 191 | (ee->getComponent().getKind() == dk) && |
| 192 | (ee->getComponent().getIsNormalized() == isNorm) && |
| 193 | (ee->getComponent().getVectorSize() == vecSize)) { |
| 194 | // Match |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 195 | returnRef.set(ee); |
| 196 | ObjectBase::asyncUnlock(); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 197 | return ee; |
| 198 | } |
| 199 | } |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 200 | ObjectBase::asyncUnlock(); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 201 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 202 | Element *e = new Element(rsc); |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 203 | returnRef.set(e); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 204 | e->mComponent.set(dt, dk, isNorm, vecSize); |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 205 | e->compute(); |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 206 | |
| 207 | ObjectBase::asyncLock(); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 208 | rsc->mStateElement.mElements.push(e); |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 209 | ObjectBase::asyncUnlock(); |
| 210 | |
| 211 | return returnRef; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 214 | ObjectBaseRef<const Element> Element::createRef(Context *rsc, size_t count, const Element **ein, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 215 | const char **nin, const size_t * lengths, const uint32_t *asin) { |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 216 | |
| 217 | ObjectBaseRef<const Element> returnRef; |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 218 | // Look for an existing match. |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 219 | ObjectBase::asyncLock(); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 220 | for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) { |
| 221 | const Element *ee = rsc->mStateElement.mElements[ct]; |
| 222 | if (ee->getFieldCount() == count) { |
| 223 | bool match = true; |
| 224 | for (uint32_t i=0; i < count; i++) { |
| 225 | if ((ee->mFields[i].e.get() != ein[i]) || |
| 226 | (ee->mFields[i].name.length() != lengths[i]) || |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 227 | (ee->mFields[i].name != nin[i]) || |
| 228 | (ee->mFields[i].arraySize != asin[i])) { |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 229 | match = false; |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | if (match) { |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 234 | returnRef.set(ee); |
| 235 | ObjectBase::asyncUnlock(); |
| 236 | return returnRef; |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 240 | ObjectBase::asyncUnlock(); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 241 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 242 | Element *e = new Element(rsc); |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 243 | returnRef.set(e); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 244 | e->mFields = new ElementField_t [count]; |
| 245 | e->mFieldCount = count; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 246 | for (size_t ct=0; ct < count; ct++) { |
| 247 | e->mFields[ct].e.set(ein[ct]); |
| 248 | e->mFields[ct].name.setTo(nin[ct], lengths[ct]); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 249 | e->mFields[ct].arraySize = asin[ct]; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 250 | } |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 251 | e->compute(); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 252 | |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 253 | ObjectBase::asyncLock(); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 254 | rsc->mStateElement.mElements.push(e); |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 255 | ObjectBase::asyncUnlock(); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 256 | |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 257 | return returnRef; |
Jason Sams | e17964e | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 258 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 259 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 260 | void Element::incRefs(const void *ptr) const { |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 261 | if (!mFieldCount) { |
| 262 | if (mComponent.isReference()) { |
| 263 | ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr); |
| 264 | ObjectBase *ob = obp[0]; |
Jason Sams | ee73498 | 2010-08-12 12:44:02 -0700 | [diff] [blame] | 265 | if (ob) ob->incSysRef(); |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 266 | } |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | const uint8_t *p = static_cast<const uint8_t *>(ptr); |
| 271 | for (uint32_t i=0; i < mFieldCount; i++) { |
| 272 | if (mFields[i].e->mHasReference) { |
Alex Sakhartchouk | 4b013ee | 2011-07-12 13:25:14 -0700 | [diff] [blame] | 273 | const uint8_t *p2 = &p[mFields[i].offsetBits >> 3]; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 274 | for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) { |
Alex Sakhartchouk | 4b013ee | 2011-07-12 13:25:14 -0700 | [diff] [blame] | 275 | mFields[i].e->incRefs(p2); |
| 276 | p2 += mFields[i].e->getSizeBytes(); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 277 | } |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 282 | void Element::decRefs(const void *ptr) const { |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 283 | if (!mFieldCount) { |
| 284 | if (mComponent.isReference()) { |
| 285 | ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr); |
| 286 | ObjectBase *ob = obp[0]; |
Jason Sams | ee73498 | 2010-08-12 12:44:02 -0700 | [diff] [blame] | 287 | if (ob) ob->decSysRef(); |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 288 | } |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | const uint8_t *p = static_cast<const uint8_t *>(ptr); |
| 293 | for (uint32_t i=0; i < mFieldCount; i++) { |
| 294 | if (mFields[i].e->mHasReference) { |
Alex Sakhartchouk | 4b013ee | 2011-07-12 13:25:14 -0700 | [diff] [blame] | 295 | const uint8_t *p2 = &p[mFields[i].offsetBits >> 3]; |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 296 | for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) { |
Alex Sakhartchouk | 4b013ee | 2011-07-12 13:25:14 -0700 | [diff] [blame] | 297 | mFields[i].e->decRefs(p2); |
| 298 | p2 += mFields[i].e->getSizeBytes(); |
Jason Sams | 70d4e50 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 299 | } |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 303 | |
Alex Sakhartchouk | a8bb921 | 2011-08-19 09:43:18 -0700 | [diff] [blame] | 304 | Element::Builder::Builder() { |
| 305 | const uint32_t initialCapacity = 32; |
| 306 | mBuilderElementRefs.setCapacity(initialCapacity); |
| 307 | mBuilderElements.setCapacity(initialCapacity); |
| 308 | mBuilderNameStrings.setCapacity(initialCapacity); |
| 309 | mBuilderNameLengths.setCapacity(initialCapacity); |
| 310 | mBuilderArrays.setCapacity(initialCapacity); |
| 311 | } |
| 312 | |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 313 | void Element::Builder::add(const Element *e, const char *nameStr, uint32_t arraySize) { |
| 314 | mBuilderElementRefs.push(ObjectBaseRef<const Element>(e)); |
| 315 | mBuilderElements.push(e); |
| 316 | mBuilderNameStrings.push(nameStr); |
| 317 | mBuilderNameLengths.push(strlen(nameStr)); |
| 318 | mBuilderArrays.push(arraySize); |
| 319 | |
| 320 | } |
| 321 | |
| 322 | ObjectBaseRef<const Element> Element::Builder::create(Context *rsc) { |
| 323 | return Element::createRef(rsc, mBuilderElements.size(), |
| 324 | &(mBuilderElements.editArray()[0]), |
| 325 | &(mBuilderNameStrings.editArray()[0]), |
| 326 | mBuilderNameLengths.editArray(), |
| 327 | mBuilderArrays.editArray()); |
| 328 | } |
| 329 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 330 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 331 | ElementState::ElementState() { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 334 | ElementState::~ElementState() { |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 335 | rsAssert(!mElements.size()); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | ///////////////////////////////////////// |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 339 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 340 | |
| 341 | namespace android { |
| 342 | namespace renderscript { |
| 343 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 344 | RsElement rsi_ElementCreate(Context *rsc, |
| 345 | RsDataType dt, |
| 346 | RsDataKind dk, |
| 347 | bool norm, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 348 | uint32_t vecSize) { |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 349 | return (RsElement)Element::create(rsc, dt, dk, norm, vecSize); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Jason Sams | 7a22e10 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 352 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 353 | RsElement rsi_ElementCreate2(Context *rsc, |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 354 | const RsElement * ein, |
Alex Sakhartchouk | e7c4a75 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 355 | size_t ein_length, |
Jason Sams | 7a22e10 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 356 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 357 | const char ** names, |
Alex Sakhartchouk | e7c4a75 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 358 | size_t nameLengths_length, |
Jason Sams | 7a22e10 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 359 | const size_t * nameLengths, |
| 360 | |
Alex Sakhartchouk | e7c4a75 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 361 | const uint32_t * arraySizes, |
| 362 | size_t arraySizes_length) { |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 363 | return (RsElement)Element::create(rsc, ein_length, (const Element **)ein, |
| 364 | names, nameLengths, arraySizes); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 370 | void rsaElementGetNativeData(RsContext con, RsElement elem, |
| 371 | uint32_t *elemData, uint32_t elemDataSize) { |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 372 | rsAssert(elemDataSize == 5); |
| 373 | // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements |
| 374 | Element *e = static_cast<Element *>(elem); |
| 375 | |
| 376 | (*elemData++) = (uint32_t)e->getType(); |
| 377 | (*elemData++) = (uint32_t)e->getKind(); |
| 378 | (*elemData++) = e->getComponent().getIsNormalized() ? 1 : 0; |
| 379 | (*elemData++) = e->getComponent().getVectorSize(); |
| 380 | (*elemData++) = e->getFieldCount(); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Alex Sakhartchouk | 117abdb | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 383 | void rsaElementGetSubElements(RsContext con, RsElement elem, uint32_t *ids, |
Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 384 | const char **names, uint32_t *arraySizes, uint32_t dataSize) { |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 385 | Element *e = static_cast<Element *>(elem); |
| 386 | rsAssert(e->getFieldCount() == dataSize); |
| 387 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 388 | for (uint32_t i = 0; i < dataSize; i ++) { |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 389 | e->getField(i)->incUserRef(); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 390 | ids[i] = (uint32_t)e->getField(i); |
| 391 | names[i] = e->getFieldName(i); |
Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 392 | arraySizes[i] = e->getFieldArraySize(i); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 393 | } |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 394 | } |