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