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; |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 26 | mBitsUnpadded = 0; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 27 | mFields = NULL; |
| 28 | mFieldCount = 0; |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 29 | mHasReference = false; |
Alex Sakhartchouk | b7c8c52 | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 30 | memset(&mHal, 0, sizeof(mHal)); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 33 | Element::~Element() { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 34 | clear(); |
| 35 | } |
| 36 | |
| 37 | void Element::preDestroy() const { |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 38 | for (uint32_t ct = 0; ct < mRSC->mStateElement.mElements.size(); ct++) { |
| 39 | if (mRSC->mStateElement.mElements[ct] == this) { |
| 40 | mRSC->mStateElement.mElements.removeAt(ct); |
| 41 | break; |
| 42 | } |
| 43 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 46 | void Element::clear() { |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 47 | if (mFields) { |
| 48 | for (size_t i = 0; i < mFieldCount; i++) { |
| 49 | delete[] mFields[i].name; |
| 50 | } |
| 51 | delete [] mFields; |
| 52 | } |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 53 | mFields = NULL; |
| 54 | mFieldCount = 0; |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 55 | mHasReference = false; |
Alex Sakhartchouk | 253325d | 2011-12-15 09:56:10 -0800 | [diff] [blame] | 56 | |
| 57 | delete [] mHal.state.fields; |
| 58 | delete [] mHal.state.fieldArraySizes; |
| 59 | delete [] mHal.state.fieldNames; |
| 60 | delete [] mHal.state.fieldNameLengths; |
| 61 | delete [] mHal.state.fieldOffsetBytes; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 62 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 63 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 64 | size_t Element::getSizeBits() const { |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 65 | if (!mFieldCount) { |
| 66 | return mBits; |
| 67 | } |
| 68 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 69 | size_t total = 0; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 70 | for (size_t ct=0; ct < mFieldCount; ct++) { |
Alex Sakhartchouk | 39f2ef6 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 71 | total += mFields[ct].e->mBits * mFields[ct].arraySize; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 72 | } |
| 73 | return total; |
| 74 | } |
| 75 | |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 76 | size_t Element::getSizeBitsUnpadded() const { |
| 77 | if (!mFieldCount) { |
| 78 | return mBitsUnpadded; |
| 79 | } |
| 80 | |
| 81 | size_t total = 0; |
| 82 | for (size_t ct=0; ct < mFieldCount; ct++) { |
| 83 | total += mFields[ct].e->mBitsUnpadded * mFields[ct].arraySize; |
| 84 | } |
| 85 | return total; |
| 86 | } |
| 87 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 88 | void Element::dumpLOGV(const char *prefix) const { |
Jason Sams | e12c1c5 | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 89 | ObjectBase::dumpLOGV(prefix); |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 90 | ALOGV("%s Element: fieldCount: %zu, size bytes: %zu", prefix, mFieldCount, getSizeBytes()); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 91 | mComponent.dumpLOGV(prefix); |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 92 | for (uint32_t ct = 0; ct < mFieldCount; ct++) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 93 | ALOGV("%s Element field index: %u ------------------", prefix, ct); |
| 94 | ALOGV("%s name: %s, offsetBits: %u, arraySize: %u", |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 95 | prefix, mFields[ct].name, mFields[ct].offsetBits, mFields[ct].arraySize); |
Alex Sakhartchouk | 963bb45 | 2010-10-13 14:22:02 -0700 | [diff] [blame] | 96 | mFields[ct].e->dumpLOGV(prefix); |
Jason Sams | e12c1c5 | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 100 | void Element::serialize(Context *rsc, OStream *stream) const { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 101 | // Need to identify ourselves |
| 102 | stream->addU32((uint32_t)getClassId()); |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 103 | stream->addString(getName()); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 104 | |
| 105 | mComponent.serialize(stream); |
| 106 | |
| 107 | // Now serialize all the fields |
| 108 | stream->addU32(mFieldCount); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 109 | for (uint32_t ct = 0; ct < mFieldCount; ct++) { |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 110 | stream->addString(mFields[ct].name); |
Jason Sams | 46e4554 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 111 | stream->addU32(mFields[ct].arraySize); |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 112 | mFields[ct].e->serialize(rsc, stream); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 116 | Element *Element::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 117 | // First make sure we are reading the correct object |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 118 | RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 119 | if (classID != RS_A3D_CLASS_ID_ELEMENT) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 120 | ALOGE("element loading skipped due to invalid class id\n"); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 121 | return NULL; |
| 122 | } |
| 123 | |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 124 | const char *name = stream->loadString(); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 125 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 126 | Component component; |
| 127 | component.loadFromStream(stream); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 128 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 129 | uint32_t fieldCount = stream->loadU32(); |
| 130 | if (!fieldCount) { |
| 131 | return (Element *)Element::create(rsc, |
| 132 | component.getType(), |
| 133 | component.getKind(), |
| 134 | component.getIsNormalized(), |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 135 | component.getVectorSize()); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 138 | const Element **subElems = new const Element *[fieldCount]; |
| 139 | const char **subElemNames = new const char *[fieldCount]; |
| 140 | size_t *subElemNamesLengths = new size_t[fieldCount]; |
| 141 | uint32_t *arraySizes = new uint32_t[fieldCount]; |
| 142 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 143 | for (uint32_t ct = 0; ct < fieldCount; ct ++) { |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 144 | subElemNames[ct] = stream->loadString(); |
| 145 | subElemNamesLengths[ct] = strlen(subElemNames[ct]); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 146 | arraySizes[ct] = stream->loadU32(); |
| 147 | subElems[ct] = Element::createFromStream(rsc, stream); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 150 | const Element *elem = Element::create(rsc, fieldCount, subElems, subElemNames, |
| 151 | subElemNamesLengths, arraySizes); |
| 152 | for (uint32_t ct = 0; ct < fieldCount; ct ++) { |
| 153 | delete [] subElemNames[ct]; |
| 154 | subElems[ct]->decUserRef(); |
| 155 | } |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 156 | delete[] name; |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 157 | delete[] subElems; |
| 158 | delete[] subElemNames; |
| 159 | delete[] subElemNamesLengths; |
| 160 | delete[] arraySizes; |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 161 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 162 | return (Element *)elem; |
Alex Sakhartchouk | 383e5b1 | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 163 | } |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 164 | |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 165 | void Element::compute() { |
Alex Sakhartchouk | 253325d | 2011-12-15 09:56:10 -0800 | [diff] [blame] | 166 | mHal.state.dataType = mComponent.getType(); |
| 167 | mHal.state.dataKind = mComponent.getKind(); |
| 168 | mHal.state.vectorSize = mComponent.getVectorSize(); |
| 169 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 170 | if (mFieldCount == 0) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 171 | mBits = mComponent.getBits(); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 172 | mBitsUnpadded = mComponent.getBitsUnpadded(); |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 173 | mHasReference = mComponent.isReference(); |
Alex Sakhartchouk | 253325d | 2011-12-15 09:56:10 -0800 | [diff] [blame] | 174 | |
| 175 | mHal.state.elementSizeBytes = getSizeBytes(); |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | |
Alex Sakhartchouk | b7c8c52 | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 179 | uint32_t noPaddingFieldCount = 0; |
| 180 | for (uint32_t ct = 0; ct < mFieldCount; ct ++) { |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 181 | if (mFields[ct].name[0] != '#') { |
Alex Sakhartchouk | b7c8c52 | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 182 | noPaddingFieldCount ++; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | mHal.state.fields = new const Element*[noPaddingFieldCount]; |
| 187 | mHal.state.fieldArraySizes = new uint32_t[noPaddingFieldCount]; |
| 188 | mHal.state.fieldNames = new const char*[noPaddingFieldCount]; |
| 189 | mHal.state.fieldNameLengths = new uint32_t[noPaddingFieldCount]; |
| 190 | mHal.state.fieldOffsetBytes = new uint32_t[noPaddingFieldCount]; |
| 191 | mHal.state.fieldsCount = noPaddingFieldCount; |
Alex Sakhartchouk | 253325d | 2011-12-15 09:56:10 -0800 | [diff] [blame] | 192 | |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 193 | size_t bits = 0; |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 194 | size_t bitsUnpadded = 0; |
Alex Sakhartchouk | b7c8c52 | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 195 | for (size_t ct = 0, ctNoPadding = 0; ct < mFieldCount; ct++) { |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 196 | mFields[ct].offsetBits = bits; |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 197 | mFields[ct].offsetBitsUnpadded = bitsUnpadded; |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 198 | bits += mFields[ct].e->getSizeBits() * mFields[ct].arraySize; |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 199 | bitsUnpadded += mFields[ct].e->getSizeBitsUnpadded() * mFields[ct].arraySize; |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 200 | |
| 201 | if (mFields[ct].e->mHasReference) { |
| 202 | mHasReference = true; |
| 203 | } |
Alex Sakhartchouk | 253325d | 2011-12-15 09:56:10 -0800 | [diff] [blame] | 204 | |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 205 | if (mFields[ct].name[0] == '#') { |
Alex Sakhartchouk | b7c8c52 | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 206 | continue; |
| 207 | } |
| 208 | |
| 209 | mHal.state.fields[ctNoPadding] = mFields[ct].e.get(); |
| 210 | mHal.state.fieldArraySizes[ctNoPadding] = mFields[ct].arraySize; |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 211 | mHal.state.fieldNames[ctNoPadding] = mFields[ct].name; |
| 212 | mHal.state.fieldNameLengths[ctNoPadding] = strlen(mFields[ct].name) + 1; // to include 0 |
Alex Sakhartchouk | b7c8c52 | 2011-12-22 13:11:48 -0800 | [diff] [blame] | 213 | mHal.state.fieldOffsetBytes[ctNoPadding] = mFields[ct].offsetBits >> 3; |
| 214 | |
| 215 | ctNoPadding ++; |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Alex Sakhartchouk | 253325d | 2011-12-15 09:56:10 -0800 | [diff] [blame] | 218 | mHal.state.elementSizeBytes = getSizeBytes(); |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 221 | ObjectBaseRef<const Element> Element::createRef(Context *rsc, RsDataType dt, RsDataKind dk, |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 222 | bool isNorm, uint32_t vecSize) { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 223 | ObjectBaseRef<const Element> returnRef; |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 224 | // Look for an existing match. |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 225 | ObjectBase::asyncLock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 226 | for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) { |
| 227 | const Element *ee = rsc->mStateElement.mElements[ct]; |
| 228 | if (!ee->getFieldCount() && |
| 229 | (ee->getComponent().getType() == dt) && |
| 230 | (ee->getComponent().getKind() == dk) && |
| 231 | (ee->getComponent().getIsNormalized() == isNorm) && |
| 232 | (ee->getComponent().getVectorSize() == vecSize)) { |
| 233 | // Match |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 234 | returnRef.set(ee); |
| 235 | ObjectBase::asyncUnlock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 236 | return ee; |
| 237 | } |
| 238 | } |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 239 | ObjectBase::asyncUnlock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 240 | |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 241 | Element *e = new Element(rsc); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 242 | returnRef.set(e); |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 243 | e->mComponent.set(dt, dk, isNorm, vecSize); |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 244 | e->compute(); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 245 | |
| 246 | ObjectBase::asyncLock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 247 | rsc->mStateElement.mElements.push(e); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 248 | ObjectBase::asyncUnlock(); |
| 249 | |
| 250 | return returnRef; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 253 | 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] | 254 | const char **nin, const size_t * lengths, const uint32_t *asin) { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 255 | |
| 256 | ObjectBaseRef<const Element> returnRef; |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 257 | // Look for an existing match. |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 258 | ObjectBase::asyncLock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 259 | for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) { |
| 260 | const Element *ee = rsc->mStateElement.mElements[ct]; |
| 261 | if (ee->getFieldCount() == count) { |
| 262 | bool match = true; |
| 263 | for (uint32_t i=0; i < count; i++) { |
Jason Sams | f313dc3 | 2013-07-09 14:29:39 -0700 | [diff] [blame] | 264 | size_t len; |
| 265 | uint32_t asize = 1; |
| 266 | if (lengths) { |
| 267 | len = lengths[i]; |
| 268 | } else { |
| 269 | len = strlen(nin[i]); |
| 270 | } |
| 271 | if (asin) { |
| 272 | asize = asin[i]; |
| 273 | } |
| 274 | |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 275 | if ((ee->mFields[i].e.get() != ein[i]) || |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 276 | (strlen(ee->mFields[i].name) != len) || |
| 277 | strcmp(ee->mFields[i].name, nin[i]) || |
Jason Sams | f313dc3 | 2013-07-09 14:29:39 -0700 | [diff] [blame] | 278 | (ee->mFields[i].arraySize != asize)) { |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 279 | match = false; |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | if (match) { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 284 | returnRef.set(ee); |
| 285 | ObjectBase::asyncUnlock(); |
| 286 | return returnRef; |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | } |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 290 | ObjectBase::asyncUnlock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 291 | |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 292 | Element *e = new Element(rsc); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 293 | returnRef.set(e); |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 294 | e->mFields = new ElementField_t [count]; |
| 295 | e->mFieldCount = count; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 296 | for (size_t ct=0; ct < count; ct++) { |
Jason Sams | f313dc3 | 2013-07-09 14:29:39 -0700 | [diff] [blame] | 297 | size_t len; |
| 298 | uint32_t asize = 1; |
| 299 | if (lengths) { |
| 300 | len = lengths[ct]; |
| 301 | } else { |
| 302 | len = strlen(nin[ct]); |
| 303 | } |
| 304 | if (asin) { |
| 305 | asize = asin[ct]; |
| 306 | } |
| 307 | |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 308 | e->mFields[ct].e.set(ein[ct]); |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 309 | e->mFields[ct].name = rsuCopyString(nin[ct], len); |
Jason Sams | f313dc3 | 2013-07-09 14:29:39 -0700 | [diff] [blame] | 310 | e->mFields[ct].arraySize = asize; |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 311 | } |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 312 | e->compute(); |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 313 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 314 | ObjectBase::asyncLock(); |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 315 | rsc->mStateElement.mElements.push(e); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 316 | ObjectBase::asyncUnlock(); |
Jason Sams | 4815c0d | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 317 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 318 | return returnRef; |
Jason Sams | b4d3568 | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 319 | } |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 320 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 321 | void Element::incRefs(const void *ptr) const { |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 322 | if (!mFieldCount) { |
| 323 | if (mComponent.isReference()) { |
| 324 | ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr); |
| 325 | ObjectBase *ob = obp[0]; |
Jason Sams | 10e5e57 | 2010-08-12 12:44:02 -0700 | [diff] [blame] | 326 | if (ob) ob->incSysRef(); |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 327 | } |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | const uint8_t *p = static_cast<const uint8_t *>(ptr); |
| 332 | for (uint32_t i=0; i < mFieldCount; i++) { |
| 333 | if (mFields[i].e->mHasReference) { |
Alex Sakhartchouk | 49464a9 | 2011-07-12 13:25:14 -0700 | [diff] [blame] | 334 | const uint8_t *p2 = &p[mFields[i].offsetBits >> 3]; |
Jason Sams | 46e4554 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 335 | for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) { |
Alex Sakhartchouk | 49464a9 | 2011-07-12 13:25:14 -0700 | [diff] [blame] | 336 | mFields[i].e->incRefs(p2); |
| 337 | p2 += mFields[i].e->getSizeBytes(); |
Jason Sams | 46e4554 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 338 | } |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 343 | void Element::decRefs(const void *ptr) const { |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 344 | if (!mFieldCount) { |
| 345 | if (mComponent.isReference()) { |
| 346 | ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr); |
| 347 | ObjectBase *ob = obp[0]; |
Jason Sams | 10e5e57 | 2010-08-12 12:44:02 -0700 | [diff] [blame] | 348 | if (ob) ob->decSysRef(); |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 349 | } |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | const uint8_t *p = static_cast<const uint8_t *>(ptr); |
| 354 | for (uint32_t i=0; i < mFieldCount; i++) { |
| 355 | if (mFields[i].e->mHasReference) { |
Alex Sakhartchouk | 49464a9 | 2011-07-12 13:25:14 -0700 | [diff] [blame] | 356 | const uint8_t *p2 = &p[mFields[i].offsetBits >> 3]; |
Jason Sams | 46e4554 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 357 | for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) { |
Alex Sakhartchouk | 49464a9 | 2011-07-12 13:25:14 -0700 | [diff] [blame] | 358 | mFields[i].e->decRefs(p2); |
| 359 | p2 += mFields[i].e->getSizeBytes(); |
Jason Sams | 46e4554 | 2010-09-02 17:35:23 -0700 | [diff] [blame] | 360 | } |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | } |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 364 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 365 | ElementState::ElementState() { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 368 | ElementState::~ElementState() { |
Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 369 | rsAssert(!mElements.size()); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | ///////////////////////////////////////// |
Jason Sams | e5ffb87 | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 373 | // |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 374 | |
| 375 | namespace android { |
| 376 | namespace renderscript { |
| 377 | |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 378 | RsElement rsi_ElementCreate(Context *rsc, |
| 379 | RsDataType dt, |
| 380 | RsDataKind dk, |
| 381 | bool norm, |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 382 | uint32_t vecSize) { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 383 | return (RsElement)Element::create(rsc, dt, dk, norm, vecSize); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 386 | |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 387 | RsElement rsi_ElementCreate2(Context *rsc, |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 388 | const RsElement * ein, |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 389 | size_t ein_length, |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 390 | |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 391 | const char ** names, |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 392 | size_t nameLengths_length, |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 393 | const size_t * nameLengths, |
| 394 | |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 395 | const uint32_t * arraySizes, |
| 396 | size_t arraySizes_length) { |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 397 | return (RsElement)Element::create(rsc, ein_length, (const Element **)ein, |
| 398 | names, nameLengths, arraySizes); |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Tim Murray | c2ce707 | 2013-07-17 18:38:53 -0700 | [diff] [blame] | 404 | extern "C" void rsaElementGetNativeData(RsContext con, RsElement elem, |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 405 | uint32_t *elemData, uint32_t elemDataSize) { |
Alex Sakhartchouk | 417e6a4 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 406 | rsAssert(elemDataSize == 5); |
| 407 | // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements |
| 408 | Element *e = static_cast<Element *>(elem); |
| 409 | |
| 410 | (*elemData++) = (uint32_t)e->getType(); |
| 411 | (*elemData++) = (uint32_t)e->getKind(); |
| 412 | (*elemData++) = e->getComponent().getIsNormalized() ? 1 : 0; |
| 413 | (*elemData++) = e->getComponent().getVectorSize(); |
| 414 | (*elemData++) = e->getFieldCount(); |
Alex Sakhartchouk | 417e6a4 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Tim Murray | c2ce707 | 2013-07-17 18:38:53 -0700 | [diff] [blame] | 417 | extern "C" void rsaElementGetSubElements(RsContext con, RsElement elem, uintptr_t *ids, |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 418 | const char **names, size_t *arraySizes, uint32_t dataSize) { |
Alex Sakhartchouk | 417e6a4 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 419 | Element *e = static_cast<Element *>(elem); |
| 420 | rsAssert(e->getFieldCount() == dataSize); |
| 421 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 422 | for (uint32_t i = 0; i < dataSize; i ++) { |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 423 | e->getField(i)->incUserRef(); |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 424 | ids[i] = (uintptr_t)e->getField(i); |
Alex Sakhartchouk | 417e6a4 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 425 | names[i] = e->getFieldName(i); |
Alex Sakhartchouk | 31aca7d | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 426 | arraySizes[i] = e->getFieldArraySize(i); |
Alex Sakhartchouk | 417e6a4 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 427 | } |
Alex Sakhartchouk | 417e6a4 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 428 | } |