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