blob: 9e6fbd5f97a6d73cdfa1a29cd247046fcd506da9 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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 Sams326e0dd2009-05-22 14:03:28 -070017
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070018#ifndef ANDROID_RS_BUILD_FOR_HOST
19#include "rsContext.h"
Jason Sams565ac362009-06-03 16:04:54 -070020#include <GLES/gl.h>
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070021#else
22#include "rsContextHostStub.h"
23#include <OpenGL/gl.h>
24#endif
Jason Sams565ac362009-06-03 16:04:54 -070025
Jason Sams326e0dd2009-05-22 14:03:28 -070026using namespace android;
27using namespace android::renderscript;
28
Jason Sams326e0dd2009-05-22 14:03:28 -070029
Jason Samse514b452009-09-25 14:51:22 -070030Element::Element(Context *rsc) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070031{
Jason Sams4815c0d2009-12-15 12:58:36 -080032 mBits = 0;
Jason Samsf2649a92009-09-25 16:37:33 -070033 mAllocFile = __FILE__;
34 mAllocLine = __LINE__;
Jason Sams4815c0d2009-12-15 12:58:36 -080035 mFields = NULL;
36 mFieldCount = 0;
Jason Samse3929c92010-08-09 18:13:33 -070037 mHasReference = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070038}
39
Jason Sams326e0dd2009-05-22 14:03:28 -070040
41Element::~Element()
42{
Jason Sams81549542010-02-17 15:38:10 -080043 for (uint32_t ct = 0; ct < mRSC->mStateElement.mElements.size(); ct++) {
44 if (mRSC->mStateElement.mElements[ct] == this) {
45 mRSC->mStateElement.mElements.removeAt(ct);
46 break;
47 }
48 }
Jason Sams326e0dd2009-05-22 14:03:28 -070049 clear();
50}
51
52void Element::clear()
53{
Jason Sams4815c0d2009-12-15 12:58:36 -080054 delete [] mFields;
55 mFields = NULL;
56 mFieldCount = 0;
Jason Samse3929c92010-08-09 18:13:33 -070057 mHasReference = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070058}
Jason Sams326e0dd2009-05-22 14:03:28 -070059
60size_t Element::getSizeBits() const
61{
Jason Sams4815c0d2009-12-15 12:58:36 -080062 if (!mFieldCount) {
63 return mBits;
64 }
65
Jason Sams326e0dd2009-05-22 14:03:28 -070066 size_t total = 0;
Jason Sams4815c0d2009-12-15 12:58:36 -080067 for (size_t ct=0; ct < mFieldCount; ct++) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070068 total += mFields[ct].e->mBits * mFields[ct].arraySize;
Jason Sams326e0dd2009-05-22 14:03:28 -070069 }
70 return total;
71}
72
Jason Samse12c1c52009-09-27 17:50:38 -070073void Element::dumpLOGV(const char *prefix) const
74{
75 ObjectBase::dumpLOGV(prefix);
Alex Sakhartchouk963bb452010-10-13 14:22:02 -070076 LOGV("%s Element: fieldCount: %i, size bytes: %i", prefix, mFieldCount, getSizeBytes());
Jason Sams4815c0d2009-12-15 12:58:36 -080077 for (uint32_t ct = 0; ct < mFieldCount; ct++) {
Alex Sakhartchouk963bb452010-10-13 14:22:02 -070078 LOGV("%s Element field index: %u ------------------", prefix, ct);
79 LOGV("%s name: %s, offsetBits: %u, arraySize: %u",
80 prefix, mFields[ct].name.string(), mFields[ct].offsetBits, mFields[ct].arraySize);
81 mFields[ct].e->dumpLOGV(prefix);
Jason Samse12c1c52009-09-27 17:50:38 -070082 }
83}
84
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070085void Element::serialize(OStream *stream) const
86{
87 // Need to identify ourselves
88 stream->addU32((uint32_t)getClassId());
89
90 String8 name(getName());
91 stream->addString(&name);
92
93 mComponent.serialize(stream);
94
95 // Now serialize all the fields
96 stream->addU32(mFieldCount);
97 for(uint32_t ct = 0; ct < mFieldCount; ct++) {
98 stream->addString(&mFields[ct].name);
Jason Sams46e45542010-09-02 17:35:23 -070099 stream->addU32(mFields[ct].arraySize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700100 mFields[ct].e->serialize(stream);
101 }
102}
103
104Element *Element::createFromStream(Context *rsc, IStream *stream)
105{
106 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700107 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
108 if(classID != RS_A3D_CLASS_ID_ELEMENT) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700109 LOGE("element loading skipped due to invalid class id\n");
110 return NULL;
111 }
112
113 String8 name;
114 stream->loadString(&name);
115
116 Element *elem = new Element(rsc);
117 elem->mComponent.loadFromStream(stream);
118 elem->mBits = elem->mComponent.getBits();
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700119 elem->mHasReference = elem->mComponent.isReference();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700120
121 elem->mFieldCount = stream->loadU32();
122 if(elem->mFieldCount) {
Jason Samse3929c92010-08-09 18:13:33 -0700123 uint32_t offset = 0;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700124 elem->mFields = new ElementField_t [elem->mFieldCount];
125 for(uint32_t ct = 0; ct < elem->mFieldCount; ct ++) {
126 stream->loadString(&elem->mFields[ct].name);
Jason Sams46e45542010-09-02 17:35:23 -0700127 elem->mFields[ct].arraySize = stream->loadU32();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700128 Element *fieldElem = Element::createFromStream(rsc, stream);
129 elem->mFields[ct].e.set(fieldElem);
Jason Samse3929c92010-08-09 18:13:33 -0700130 elem->mFields[ct].offsetBits = offset;
131 offset += fieldElem->getSizeBits();
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700132 // Check if our sub-elements have references
133 if(fieldElem->mHasReference) {
134 elem->mHasReference = true;
135 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700136 }
137 }
138
139 // We need to check if this already exists
140 for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
141 Element *ee = rsc->mStateElement.mElements[ct];
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700142 if(ee->isEqual(elem)) {
143 delete elem;
144 ee->incUserRef();
145 return ee;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700146 }
147 }
148
149 rsc->mStateElement.mElements.push(elem);
150 return elem;
151}
152
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700153bool Element::isEqual(const Element *other) const {
154 if(other == NULL) {
155 return false;
156 }
157 if (!other->getFieldCount() && !mFieldCount) {
158 if((other->getType() == getType()) &&
159 (other->getKind() == getKind()) &&
160 (other->getComponent().getIsNormalized() == getComponent().getIsNormalized()) &&
161 (other->getComponent().getVectorSize() == getComponent().getVectorSize())) {
162 return true;
163 }
164 return false;
165 }
166 if (other->getFieldCount() == mFieldCount) {
167 for (uint32_t i=0; i < mFieldCount; i++) {
168 if ((!other->mFields[i].e->isEqual(mFields[i].e.get())) ||
169 (other->mFields[i].name.length() != mFields[i].name.length()) ||
170 (other->mFields[i].name != mFields[i].name) ||
171 (other->mFields[i].arraySize != mFields[i].arraySize)) {
172 return false;
173 }
174 }
175 return true;
176 }
177 return false;
178}
Jason Samsd01d9702009-12-23 14:35:29 -0800179
Jason Sams81549542010-02-17 15:38:10 -0800180const Element * Element::create(Context *rsc, RsDataType dt, RsDataKind dk,
Jason Samsd01d9702009-12-23 14:35:29 -0800181 bool isNorm, uint32_t vecSize)
Jason Sams4815c0d2009-12-15 12:58:36 -0800182{
Jason Sams81549542010-02-17 15:38:10 -0800183 // Look for an existing match.
184 for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
185 const Element *ee = rsc->mStateElement.mElements[ct];
186 if (!ee->getFieldCount() &&
187 (ee->getComponent().getType() == dt) &&
188 (ee->getComponent().getKind() == dk) &&
189 (ee->getComponent().getIsNormalized() == isNorm) &&
190 (ee->getComponent().getVectorSize() == vecSize)) {
191 // Match
192 ee->incUserRef();
193 return ee;
194 }
195 }
196
Jason Sams4815c0d2009-12-15 12:58:36 -0800197 Element *e = new Element(rsc);
Jason Samsd01d9702009-12-23 14:35:29 -0800198 e->mComponent.set(dt, dk, isNorm, vecSize);
199 e->mBits = e->mComponent.getBits();
Jason Samse3929c92010-08-09 18:13:33 -0700200 e->mHasReference = e->mComponent.isReference();
Jason Sams81549542010-02-17 15:38:10 -0800201 rsc->mStateElement.mElements.push(e);
Jason Sams4815c0d2009-12-15 12:58:36 -0800202 return e;
203}
204
Jason Sams81549542010-02-17 15:38:10 -0800205const Element * Element::create(Context *rsc, size_t count, const Element **ein,
Jason Sams46e45542010-09-02 17:35:23 -0700206 const char **nin, const size_t * lengths, const uint32_t *asin)
Jason Sams4815c0d2009-12-15 12:58:36 -0800207{
Jason Sams81549542010-02-17 15:38:10 -0800208 // Look for an existing match.
209 for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
210 const Element *ee = rsc->mStateElement.mElements[ct];
211 if (ee->getFieldCount() == count) {
212 bool match = true;
213 for (uint32_t i=0; i < count; i++) {
214 if ((ee->mFields[i].e.get() != ein[i]) ||
215 (ee->mFields[i].name.length() != lengths[i]) ||
Jason Sams46e45542010-09-02 17:35:23 -0700216 (ee->mFields[i].name != nin[i]) ||
217 (ee->mFields[i].arraySize != asin[i])) {
Jason Sams81549542010-02-17 15:38:10 -0800218 match = false;
219 break;
220 }
221 }
222 if (match) {
223 ee->incUserRef();
224 return ee;
225 }
226 }
227 }
228
Jason Sams4815c0d2009-12-15 12:58:36 -0800229 Element *e = new Element(rsc);
230 e->mFields = new ElementField_t [count];
231 e->mFieldCount = count;
Jason Samse3929c92010-08-09 18:13:33 -0700232 size_t bits = 0;
Jason Sams4815c0d2009-12-15 12:58:36 -0800233 for (size_t ct=0; ct < count; ct++) {
234 e->mFields[ct].e.set(ein[ct]);
235 e->mFields[ct].name.setTo(nin[ct], lengths[ct]);
Jason Samse3929c92010-08-09 18:13:33 -0700236 e->mFields[ct].offsetBits = bits;
Jason Sams46e45542010-09-02 17:35:23 -0700237 e->mFields[ct].arraySize = asin[ct];
Jason Samse3929c92010-08-09 18:13:33 -0700238 bits += ein[ct]->getSizeBits();
239
240 if (ein[ct]->mHasReference) {
241 e->mHasReference = true;
242 }
Jason Sams4815c0d2009-12-15 12:58:36 -0800243 }
244
Jason Sams81549542010-02-17 15:38:10 -0800245 rsc->mStateElement.mElements.push(e);
Jason Sams4815c0d2009-12-15 12:58:36 -0800246 return e;
247}
248
Jason Samsb4d35682010-01-04 16:52:27 -0800249String8 Element::getGLSLType(uint32_t indent) const
250{
251 String8 s;
252 for (uint32_t ct=0; ct < indent; ct++) {
253 s.append(" ");
254 }
255
256 if (!mFieldCount) {
257 // Basic component.
258 s.append(mComponent.getGLSLType());
259 } else {
260 rsAssert(0);
261 //s.append("struct ");
262 //s.append(getCStructBody(indent));
263 }
264
265 return s;
266}
Jason Samsd01d9702009-12-23 14:35:29 -0800267
Jason Samse3929c92010-08-09 18:13:33 -0700268void Element::incRefs(const void *ptr) const
269{
270 if (!mFieldCount) {
271 if (mComponent.isReference()) {
272 ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr);
273 ObjectBase *ob = obp[0];
Jason Sams10e5e572010-08-12 12:44:02 -0700274 if (ob) ob->incSysRef();
Jason Samse3929c92010-08-09 18:13:33 -0700275 }
276 return;
277 }
278
279 const uint8_t *p = static_cast<const uint8_t *>(ptr);
280 for (uint32_t i=0; i < mFieldCount; i++) {
281 if (mFields[i].e->mHasReference) {
Jason Sams46e45542010-09-02 17:35:23 -0700282 p = &p[mFields[i].offsetBits >> 3];
283 for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) {
284 mFields[i].e->incRefs(p);
285 p += mFields[i].e->getSizeBytes();
286 }
Jason Samse3929c92010-08-09 18:13:33 -0700287 }
288 }
289}
290
291void Element::decRefs(const void *ptr) const
292{
293 if (!mFieldCount) {
294 if (mComponent.isReference()) {
295 ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr);
296 ObjectBase *ob = obp[0];
Jason Sams10e5e572010-08-12 12:44:02 -0700297 if (ob) ob->decSysRef();
Jason Samse3929c92010-08-09 18:13:33 -0700298 }
299 return;
300 }
301
302 const uint8_t *p = static_cast<const uint8_t *>(ptr);
303 for (uint32_t i=0; i < mFieldCount; i++) {
304 if (mFields[i].e->mHasReference) {
Jason Sams46e45542010-09-02 17:35:23 -0700305 p = &p[mFields[i].offsetBits >> 3];
306 for (uint32_t ct=0; ct < mFields[i].arraySize; ct++) {
307 mFields[i].e->decRefs(p);
308 p += mFields[i].e->getSizeBytes();
309 }
Jason Samse3929c92010-08-09 18:13:33 -0700310 }
311 }
312}
Jason Samsd01d9702009-12-23 14:35:29 -0800313
Jason Sams4815c0d2009-12-15 12:58:36 -0800314
Jason Sams326e0dd2009-05-22 14:03:28 -0700315ElementState::ElementState()
316{
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700317 const uint32_t initialCapacity = 32;
318 mBuilderElements.setCapacity(initialCapacity);
319 mBuilderNameStrings.setCapacity(initialCapacity);
320 mBuilderNameLengths.setCapacity(initialCapacity);
321 mBuilderArrays.setCapacity(initialCapacity);
Jason Sams326e0dd2009-05-22 14:03:28 -0700322}
323
324ElementState::~ElementState()
325{
Jason Sams81549542010-02-17 15:38:10 -0800326 rsAssert(!mElements.size());
Jason Sams326e0dd2009-05-22 14:03:28 -0700327}
328
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700329void ElementState::elementBuilderBegin() {
330 mBuilderElements.clear();
331 mBuilderNameStrings.clear();
332 mBuilderNameLengths.clear();
333 mBuilderArrays.clear();
334}
335
336void ElementState::elementBuilderAdd(const Element *e, const char *nameStr, uint32_t arraySize) {
337 mBuilderElements.push(e);
338 mBuilderNameStrings.push(nameStr);
339 mBuilderNameLengths.push(strlen(nameStr));
340 mBuilderArrays.push(arraySize);
341
342}
343
344const Element *ElementState::elementBuilderCreate(Context *rsc) {
345 return Element::create(rsc, mBuilderElements.size(),
346 &(mBuilderElements.editArray()[0]),
347 &(mBuilderNameStrings.editArray()[0]),
348 mBuilderNameLengths.editArray(),
349 mBuilderArrays.editArray());
350}
351
Jason Sams4815c0d2009-12-15 12:58:36 -0800352
Jason Sams326e0dd2009-05-22 14:03:28 -0700353/////////////////////////////////////////
Jason Samse5ffb872009-08-09 17:01:55 -0700354//
Jason Sams326e0dd2009-05-22 14:03:28 -0700355
356namespace android {
357namespace renderscript {
358
Jason Samsd01d9702009-12-23 14:35:29 -0800359RsElement rsi_ElementCreate(Context *rsc,
360 RsDataType dt,
361 RsDataKind dk,
362 bool norm,
363 uint32_t vecSize)
Jason Sams326e0dd2009-05-22 14:03:28 -0700364{
Jason Samsd01d9702009-12-23 14:35:29 -0800365 //LOGE("rsi_ElementCreate %i %i %i %i", dt, dk, norm, vecSize);
Jason Sams81549542010-02-17 15:38:10 -0800366 const Element *e = Element::create(rsc, dt, dk, norm, vecSize);
Jason Samsd01d9702009-12-23 14:35:29 -0800367 e->incUserRef();
Jason Sams81549542010-02-17 15:38:10 -0800368 return (RsElement)e;
Jason Sams326e0dd2009-05-22 14:03:28 -0700369}
370
Jason Samsd01d9702009-12-23 14:35:29 -0800371RsElement rsi_ElementCreate2(Context *rsc,
372 size_t count,
373 const RsElement * ein,
374 const char ** names,
Jason Sams46e45542010-09-02 17:35:23 -0700375 const size_t * nameLengths,
376 const uint32_t * arraySizes)
Jason Samsd01d9702009-12-23 14:35:29 -0800377{
378 //LOGE("rsi_ElementCreate2 %i", count);
Jason Sams46e45542010-09-02 17:35:23 -0700379 const Element *e = Element::create(rsc, count, (const Element **)ein, names, nameLengths, arraySizes);
Jason Samsd01d9702009-12-23 14:35:29 -0800380 e->incUserRef();
Jason Sams81549542010-02-17 15:38:10 -0800381 return (RsElement)e;
Jason Samsd01d9702009-12-23 14:35:29 -0800382}
383
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700384void rsi_ElementGetNativeData(Context *rsc, RsElement elem, uint32_t *elemData, uint32_t elemDataSize)
385{
386 rsAssert(elemDataSize == 5);
387 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
388 Element *e = static_cast<Element *>(elem);
389
390 (*elemData++) = (uint32_t)e->getType();
391 (*elemData++) = (uint32_t)e->getKind();
392 (*elemData++) = e->getComponent().getIsNormalized() ? 1 : 0;
393 (*elemData++) = e->getComponent().getVectorSize();
394 (*elemData++) = e->getFieldCount();
395
396}
397
398void rsi_ElementGetSubElements(Context *rsc, RsElement elem, uint32_t *ids, const char **names, uint32_t dataSize)
399{
400 Element *e = static_cast<Element *>(elem);
401 rsAssert(e->getFieldCount() == dataSize);
402
403 for(uint32_t i = 0; i < dataSize; i ++) {
404 ids[i] = (uint32_t)e->getField(i);
405 names[i] = e->getFieldName(i);
406 }
407
408}
409
Jason Sams326e0dd2009-05-22 14:03:28 -0700410
411}
412}