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 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
| 19 | #include "rsContext.h" |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 20 | #include <GLES/gl.h> |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame^] | 21 | #else |
| 22 | #include "rsContextHostStub.h" |
| 23 | #include <OpenGL/gl.h> |
| 24 | #endif |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 25 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 26 | using namespace android; |
| 27 | using namespace android::renderscript; |
| 28 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 29 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 30 | Element::Element(Context *rsc) : ObjectBase(rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 31 | { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 32 | mBits = 0; |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 33 | mAllocFile = __FILE__; |
| 34 | mAllocLine = __LINE__; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 35 | mFields = NULL; |
| 36 | mFieldCount = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 39 | |
| 40 | Element::~Element() |
| 41 | { |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 42 | for (uint32_t ct = 0; ct < mRSC->mStateElement.mElements.size(); ct++) { |
| 43 | if (mRSC->mStateElement.mElements[ct] == this) { |
| 44 | mRSC->mStateElement.mElements.removeAt(ct); |
| 45 | break; |
| 46 | } |
| 47 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 48 | clear(); |
| 49 | } |
| 50 | |
| 51 | void Element::clear() |
| 52 | { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 53 | delete [] mFields; |
| 54 | mFields = NULL; |
| 55 | mFieldCount = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 56 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 57 | |
| 58 | size_t Element::getSizeBits() const |
| 59 | { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 60 | if (!mFieldCount) { |
| 61 | return mBits; |
| 62 | } |
| 63 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 64 | size_t total = 0; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 65 | for (size_t ct=0; ct < mFieldCount; ct++) { |
| 66 | total += mFields[ct].e->mBits; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 67 | } |
| 68 | return total; |
| 69 | } |
| 70 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 71 | size_t Element::getFieldOffsetBits(uint32_t componentNumber) const |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 72 | { |
| 73 | size_t offset = 0; |
| 74 | for (uint32_t ct = 0; ct < componentNumber; ct++) { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 75 | offset += mFields[ct].e->mBits; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 76 | } |
| 77 | return offset; |
| 78 | } |
| 79 | |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 80 | void Element::dumpLOGV(const char *prefix) const |
| 81 | { |
| 82 | ObjectBase::dumpLOGV(prefix); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 83 | LOGV("%s Element: components %i, size %i", prefix, mFieldCount, mBits); |
| 84 | for (uint32_t ct = 0; ct < mFieldCount; ct++) { |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 85 | char buf[1024]; |
| 86 | sprintf(buf, "%s component %i: ", prefix, ct); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 87 | //mComponents[ct]->dumpLOGV(buf); |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame^] | 91 | void Element::serialize(OStream *stream) const |
| 92 | { |
| 93 | // Need to identify ourselves |
| 94 | stream->addU32((uint32_t)getClassId()); |
| 95 | |
| 96 | String8 name(getName()); |
| 97 | stream->addString(&name); |
| 98 | |
| 99 | mComponent.serialize(stream); |
| 100 | |
| 101 | // Now serialize all the fields |
| 102 | stream->addU32(mFieldCount); |
| 103 | for(uint32_t ct = 0; ct < mFieldCount; ct++) { |
| 104 | stream->addString(&mFields[ct].name); |
| 105 | mFields[ct].e->serialize(stream); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | Element *Element::createFromStream(Context *rsc, IStream *stream) |
| 110 | { |
| 111 | // First make sure we are reading the correct object |
| 112 | A3DClassID classID = (A3DClassID)stream->loadU32(); |
| 113 | if(classID != A3D_CLASS_ID_ELEMENT) { |
| 114 | LOGE("element loading skipped due to invalid class id\n"); |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | String8 name; |
| 119 | stream->loadString(&name); |
| 120 | |
| 121 | Element *elem = new Element(rsc); |
| 122 | elem->mComponent.loadFromStream(stream); |
| 123 | elem->mBits = elem->mComponent.getBits(); |
| 124 | |
| 125 | elem->mFieldCount = stream->loadU32(); |
| 126 | if(elem->mFieldCount) { |
| 127 | elem->mFields = new ElementField_t [elem->mFieldCount]; |
| 128 | for(uint32_t ct = 0; ct < elem->mFieldCount; ct ++) { |
| 129 | stream->loadString(&elem->mFields[ct].name); |
| 130 | Element *fieldElem = Element::createFromStream(rsc, stream); |
| 131 | elem->mFields[ct].e.set(fieldElem); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // We need to check if this already exists |
| 136 | for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) { |
| 137 | Element *ee = rsc->mStateElement.mElements[ct]; |
| 138 | |
| 139 | if (!ee->getFieldCount() ) { |
| 140 | |
| 141 | if((ee->getComponent().getType() == elem->getComponent().getType()) && |
| 142 | (ee->getComponent().getKind() == elem->getComponent().getKind()) && |
| 143 | (ee->getComponent().getIsNormalized() == elem->getComponent().getIsNormalized()) && |
| 144 | (ee->getComponent().getVectorSize() == elem->getComponent().getVectorSize())) { |
| 145 | // Match |
| 146 | delete elem; |
| 147 | ee->incUserRef(); |
| 148 | return ee; |
| 149 | } |
| 150 | |
| 151 | } else if (ee->getFieldCount() == elem->mFieldCount) { |
| 152 | |
| 153 | bool match = true; |
| 154 | for (uint32_t i=0; i < elem->mFieldCount; i++) { |
| 155 | if ((ee->mFields[i].e.get() != elem->mFields[i].e.get()) || |
| 156 | (ee->mFields[i].name.length() != elem->mFields[i].name.length()) || |
| 157 | (ee->mFields[i].name != elem->mFields[i].name)) { |
| 158 | match = false; |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | if (match) { |
| 163 | delete elem; |
| 164 | ee->incUserRef(); |
| 165 | return ee; |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | rsc->mStateElement.mElements.push(elem); |
| 172 | return elem; |
| 173 | } |
| 174 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 175 | |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 176 | const Element * Element::create(Context *rsc, RsDataType dt, RsDataKind dk, |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 177 | bool isNorm, uint32_t vecSize) |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 178 | { |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 179 | // Look for an existing match. |
| 180 | for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) { |
| 181 | const Element *ee = rsc->mStateElement.mElements[ct]; |
| 182 | if (!ee->getFieldCount() && |
| 183 | (ee->getComponent().getType() == dt) && |
| 184 | (ee->getComponent().getKind() == dk) && |
| 185 | (ee->getComponent().getIsNormalized() == isNorm) && |
| 186 | (ee->getComponent().getVectorSize() == vecSize)) { |
| 187 | // Match |
| 188 | ee->incUserRef(); |
| 189 | return ee; |
| 190 | } |
| 191 | } |
| 192 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 193 | Element *e = new Element(rsc); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 194 | e->mComponent.set(dt, dk, isNorm, vecSize); |
| 195 | e->mBits = e->mComponent.getBits(); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 196 | rsc->mStateElement.mElements.push(e); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 197 | return e; |
| 198 | } |
| 199 | |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 200 | const Element * Element::create(Context *rsc, size_t count, const Element **ein, |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 201 | const char **nin, const size_t * lengths) |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 202 | { |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 203 | // Look for an existing match. |
| 204 | for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) { |
| 205 | const Element *ee = rsc->mStateElement.mElements[ct]; |
| 206 | if (ee->getFieldCount() == count) { |
| 207 | bool match = true; |
| 208 | for (uint32_t i=0; i < count; i++) { |
| 209 | if ((ee->mFields[i].e.get() != ein[i]) || |
| 210 | (ee->mFields[i].name.length() != lengths[i]) || |
| 211 | (ee->mFields[i].name != nin[i])) { |
| 212 | match = false; |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | if (match) { |
| 217 | ee->incUserRef(); |
| 218 | return ee; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 223 | Element *e = new Element(rsc); |
| 224 | e->mFields = new ElementField_t [count]; |
| 225 | e->mFieldCount = count; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 226 | for (size_t ct=0; ct < count; ct++) { |
| 227 | e->mFields[ct].e.set(ein[ct]); |
| 228 | e->mFields[ct].name.setTo(nin[ct], lengths[ct]); |
| 229 | } |
| 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 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 235 | String8 Element::getCStructBody(uint32_t indent) const |
| 236 | { |
| 237 | String8 si; |
| 238 | for (uint32_t ct=0; ct < indent; ct++) { |
| 239 | si.append(" "); |
| 240 | } |
| 241 | |
| 242 | String8 s(si); |
| 243 | s.append("{\n"); |
| 244 | for (uint32_t ct = 0; ct < mFieldCount; ct++) { |
| 245 | s.append(si); |
| 246 | s.append(mFields[ct].e->getCType(indent+4)); |
| 247 | s.append(" "); |
| 248 | s.append(mFields[ct].name); |
| 249 | s.append(";\n"); |
| 250 | } |
| 251 | s.append(si); |
| 252 | s.append("}"); |
| 253 | return s; |
| 254 | } |
| 255 | |
| 256 | String8 Element::getCType(uint32_t indent) const |
| 257 | { |
| 258 | String8 s; |
| 259 | for (uint32_t ct=0; ct < indent; ct++) { |
| 260 | s.append(" "); |
| 261 | } |
| 262 | |
| 263 | if (!mFieldCount) { |
| 264 | // Basic component. |
| 265 | s.append(mComponent.getCType()); |
| 266 | } else { |
| 267 | s.append("struct "); |
| 268 | s.append(getCStructBody(indent)); |
| 269 | } |
| 270 | |
| 271 | return s; |
| 272 | } |
| 273 | |
Jason Sams | e17964e | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 274 | String8 Element::getGLSLType(uint32_t indent) const |
| 275 | { |
| 276 | String8 s; |
| 277 | for (uint32_t ct=0; ct < indent; ct++) { |
| 278 | s.append(" "); |
| 279 | } |
| 280 | |
| 281 | if (!mFieldCount) { |
| 282 | // Basic component. |
| 283 | s.append(mComponent.getGLSLType()); |
| 284 | } else { |
| 285 | rsAssert(0); |
| 286 | //s.append("struct "); |
| 287 | //s.append(getCStructBody(indent)); |
| 288 | } |
| 289 | |
| 290 | return s; |
| 291 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 292 | |
| 293 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 294 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 295 | ElementState::ElementState() |
| 296 | { |
| 297 | } |
| 298 | |
| 299 | ElementState::~ElementState() |
| 300 | { |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 301 | rsAssert(!mElements.size()); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 304 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 305 | ///////////////////////////////////////// |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 306 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 307 | |
| 308 | namespace android { |
| 309 | namespace renderscript { |
| 310 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 311 | RsElement rsi_ElementCreate(Context *rsc, |
| 312 | RsDataType dt, |
| 313 | RsDataKind dk, |
| 314 | bool norm, |
| 315 | uint32_t vecSize) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 316 | { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 317 | //LOGE("rsi_ElementCreate %i %i %i %i", dt, dk, norm, vecSize); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 318 | const Element *e = Element::create(rsc, dt, dk, norm, vecSize); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 319 | e->incUserRef(); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 320 | return (RsElement)e; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 323 | RsElement rsi_ElementCreate2(Context *rsc, |
| 324 | size_t count, |
| 325 | const RsElement * ein, |
| 326 | const char ** names, |
| 327 | const size_t * nameLengths) |
| 328 | { |
| 329 | //LOGE("rsi_ElementCreate2 %i", count); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 330 | const Element *e = Element::create(rsc, count, (const Element **)ein, names, nameLengths); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 331 | e->incUserRef(); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 332 | return (RsElement)e; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 335 | |
| 336 | } |
| 337 | } |