2009-05-22 14:03:28 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-05-21 12:53:13 -07:00
|
|
|
#ifndef ANDROID_RS_BUILD_FOR_HOST
|
|
|
|
#include "rsContext.h"
|
2009-06-03 16:04:54 -07:00
|
|
|
#include <GLES/gl.h>
|
2010-05-21 12:53:13 -07:00
|
|
|
#else
|
|
|
|
#include "rsContextHostStub.h"
|
|
|
|
#include <OpenGL/gl.h>
|
|
|
|
#endif
|
2009-06-03 16:04:54 -07:00
|
|
|
|
2009-05-22 14:03:28 -07:00
|
|
|
using namespace android;
|
|
|
|
using namespace android::renderscript;
|
|
|
|
|
|
|
|
|
2009-09-25 14:51:22 -07:00
|
|
|
Element::Element(Context *rsc) : ObjectBase(rsc)
|
2009-05-22 14:03:28 -07:00
|
|
|
{
|
2009-12-15 12:58:36 -08:00
|
|
|
mBits = 0;
|
2009-09-25 16:37:33 -07:00
|
|
|
mAllocFile = __FILE__;
|
|
|
|
mAllocLine = __LINE__;
|
2009-12-15 12:58:36 -08:00
|
|
|
mFields = NULL;
|
|
|
|
mFieldCount = 0;
|
2010-08-09 18:13:33 -07:00
|
|
|
mHasReference = false;
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Element::~Element()
|
|
|
|
{
|
2010-02-17 15:38:10 -08:00
|
|
|
for (uint32_t ct = 0; ct < mRSC->mStateElement.mElements.size(); ct++) {
|
|
|
|
if (mRSC->mStateElement.mElements[ct] == this) {
|
|
|
|
mRSC->mStateElement.mElements.removeAt(ct);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-05-22 14:03:28 -07:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Element::clear()
|
|
|
|
{
|
2009-12-15 12:58:36 -08:00
|
|
|
delete [] mFields;
|
|
|
|
mFields = NULL;
|
|
|
|
mFieldCount = 0;
|
2010-08-09 18:13:33 -07:00
|
|
|
mHasReference = false;
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Element::getSizeBits() const
|
|
|
|
{
|
2009-12-15 12:58:36 -08:00
|
|
|
if (!mFieldCount) {
|
|
|
|
return mBits;
|
|
|
|
}
|
|
|
|
|
2009-05-22 14:03:28 -07:00
|
|
|
size_t total = 0;
|
2009-12-15 12:58:36 -08:00
|
|
|
for (size_t ct=0; ct < mFieldCount; ct++) {
|
|
|
|
total += mFields[ct].e->mBits;
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2009-09-27 17:50:38 -07:00
|
|
|
void Element::dumpLOGV(const char *prefix) const
|
|
|
|
{
|
|
|
|
ObjectBase::dumpLOGV(prefix);
|
2009-12-15 12:58:36 -08:00
|
|
|
LOGV("%s Element: components %i, size %i", prefix, mFieldCount, mBits);
|
|
|
|
for (uint32_t ct = 0; ct < mFieldCount; ct++) {
|
2009-09-27 17:50:38 -07:00
|
|
|
char buf[1024];
|
|
|
|
sprintf(buf, "%s component %i: ", prefix, ct);
|
2009-12-15 12:58:36 -08:00
|
|
|
//mComponents[ct]->dumpLOGV(buf);
|
2009-09-27 17:50:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-21 12:53:13 -07:00
|
|
|
void Element::serialize(OStream *stream) const
|
|
|
|
{
|
|
|
|
// Need to identify ourselves
|
|
|
|
stream->addU32((uint32_t)getClassId());
|
|
|
|
|
|
|
|
String8 name(getName());
|
|
|
|
stream->addString(&name);
|
|
|
|
|
|
|
|
mComponent.serialize(stream);
|
|
|
|
|
|
|
|
// Now serialize all the fields
|
|
|
|
stream->addU32(mFieldCount);
|
|
|
|
for(uint32_t ct = 0; ct < mFieldCount; ct++) {
|
|
|
|
stream->addString(&mFields[ct].name);
|
|
|
|
mFields[ct].e->serialize(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Element *Element::createFromStream(Context *rsc, IStream *stream)
|
|
|
|
{
|
|
|
|
// First make sure we are reading the correct object
|
2010-06-04 10:06:50 -07:00
|
|
|
RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
|
|
|
|
if(classID != RS_A3D_CLASS_ID_ELEMENT) {
|
2010-05-21 12:53:13 -07:00
|
|
|
LOGE("element loading skipped due to invalid class id\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
String8 name;
|
|
|
|
stream->loadString(&name);
|
|
|
|
|
|
|
|
Element *elem = new Element(rsc);
|
|
|
|
elem->mComponent.loadFromStream(stream);
|
|
|
|
elem->mBits = elem->mComponent.getBits();
|
|
|
|
|
|
|
|
elem->mFieldCount = stream->loadU32();
|
|
|
|
if(elem->mFieldCount) {
|
2010-08-09 18:13:33 -07:00
|
|
|
uint32_t offset = 0;
|
2010-05-21 12:53:13 -07:00
|
|
|
elem->mFields = new ElementField_t [elem->mFieldCount];
|
|
|
|
for(uint32_t ct = 0; ct < elem->mFieldCount; ct ++) {
|
|
|
|
stream->loadString(&elem->mFields[ct].name);
|
|
|
|
Element *fieldElem = Element::createFromStream(rsc, stream);
|
|
|
|
elem->mFields[ct].e.set(fieldElem);
|
2010-08-09 18:13:33 -07:00
|
|
|
elem->mFields[ct].offsetBits = offset;
|
|
|
|
offset += fieldElem->getSizeBits();
|
2010-05-21 12:53:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to check if this already exists
|
|
|
|
for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
|
|
|
|
Element *ee = rsc->mStateElement.mElements[ct];
|
|
|
|
|
|
|
|
if (!ee->getFieldCount() ) {
|
|
|
|
|
|
|
|
if((ee->getComponent().getType() == elem->getComponent().getType()) &&
|
|
|
|
(ee->getComponent().getKind() == elem->getComponent().getKind()) &&
|
|
|
|
(ee->getComponent().getIsNormalized() == elem->getComponent().getIsNormalized()) &&
|
|
|
|
(ee->getComponent().getVectorSize() == elem->getComponent().getVectorSize())) {
|
|
|
|
// Match
|
|
|
|
delete elem;
|
|
|
|
ee->incUserRef();
|
|
|
|
return ee;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (ee->getFieldCount() == elem->mFieldCount) {
|
|
|
|
|
|
|
|
bool match = true;
|
|
|
|
for (uint32_t i=0; i < elem->mFieldCount; i++) {
|
|
|
|
if ((ee->mFields[i].e.get() != elem->mFields[i].e.get()) ||
|
|
|
|
(ee->mFields[i].name.length() != elem->mFields[i].name.length()) ||
|
|
|
|
(ee->mFields[i].name != elem->mFields[i].name)) {
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (match) {
|
|
|
|
delete elem;
|
|
|
|
ee->incUserRef();
|
|
|
|
return ee;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rsc->mStateElement.mElements.push(elem);
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
2009-12-23 14:35:29 -08:00
|
|
|
|
2010-02-17 15:38:10 -08:00
|
|
|
const Element * Element::create(Context *rsc, RsDataType dt, RsDataKind dk,
|
2009-12-23 14:35:29 -08:00
|
|
|
bool isNorm, uint32_t vecSize)
|
2009-12-15 12:58:36 -08:00
|
|
|
{
|
2010-02-17 15:38:10 -08:00
|
|
|
// Look for an existing match.
|
|
|
|
for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
|
|
|
|
const Element *ee = rsc->mStateElement.mElements[ct];
|
|
|
|
if (!ee->getFieldCount() &&
|
|
|
|
(ee->getComponent().getType() == dt) &&
|
|
|
|
(ee->getComponent().getKind() == dk) &&
|
|
|
|
(ee->getComponent().getIsNormalized() == isNorm) &&
|
|
|
|
(ee->getComponent().getVectorSize() == vecSize)) {
|
|
|
|
// Match
|
|
|
|
ee->incUserRef();
|
|
|
|
return ee;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-15 12:58:36 -08:00
|
|
|
Element *e = new Element(rsc);
|
2009-12-23 14:35:29 -08:00
|
|
|
e->mComponent.set(dt, dk, isNorm, vecSize);
|
|
|
|
e->mBits = e->mComponent.getBits();
|
2010-08-09 18:13:33 -07:00
|
|
|
e->mHasReference = e->mComponent.isReference();
|
2010-02-17 15:38:10 -08:00
|
|
|
rsc->mStateElement.mElements.push(e);
|
2009-12-15 12:58:36 -08:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2010-02-17 15:38:10 -08:00
|
|
|
const Element * Element::create(Context *rsc, size_t count, const Element **ein,
|
2009-12-23 14:35:29 -08:00
|
|
|
const char **nin, const size_t * lengths)
|
2009-12-15 12:58:36 -08:00
|
|
|
{
|
2010-02-17 15:38:10 -08:00
|
|
|
// Look for an existing match.
|
|
|
|
for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) {
|
|
|
|
const Element *ee = rsc->mStateElement.mElements[ct];
|
|
|
|
if (ee->getFieldCount() == count) {
|
|
|
|
bool match = true;
|
|
|
|
for (uint32_t i=0; i < count; i++) {
|
|
|
|
if ((ee->mFields[i].e.get() != ein[i]) ||
|
|
|
|
(ee->mFields[i].name.length() != lengths[i]) ||
|
|
|
|
(ee->mFields[i].name != nin[i])) {
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (match) {
|
|
|
|
ee->incUserRef();
|
|
|
|
return ee;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-15 12:58:36 -08:00
|
|
|
Element *e = new Element(rsc);
|
|
|
|
e->mFields = new ElementField_t [count];
|
|
|
|
e->mFieldCount = count;
|
2010-08-09 18:13:33 -07:00
|
|
|
size_t bits = 0;
|
2009-12-15 12:58:36 -08:00
|
|
|
for (size_t ct=0; ct < count; ct++) {
|
|
|
|
e->mFields[ct].e.set(ein[ct]);
|
|
|
|
e->mFields[ct].name.setTo(nin[ct], lengths[ct]);
|
2010-08-09 18:13:33 -07:00
|
|
|
e->mFields[ct].offsetBits = bits;
|
|
|
|
bits += ein[ct]->getSizeBits();
|
|
|
|
|
|
|
|
if (ein[ct]->mHasReference) {
|
|
|
|
e->mHasReference = true;
|
|
|
|
}
|
2009-12-15 12:58:36 -08:00
|
|
|
}
|
|
|
|
|
2010-02-17 15:38:10 -08:00
|
|
|
rsc->mStateElement.mElements.push(e);
|
2009-12-15 12:58:36 -08:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2010-01-04 16:52:27 -08:00
|
|
|
String8 Element::getGLSLType(uint32_t indent) const
|
|
|
|
{
|
|
|
|
String8 s;
|
|
|
|
for (uint32_t ct=0; ct < indent; ct++) {
|
|
|
|
s.append(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mFieldCount) {
|
|
|
|
// Basic component.
|
|
|
|
s.append(mComponent.getGLSLType());
|
|
|
|
} else {
|
|
|
|
rsAssert(0);
|
|
|
|
//s.append("struct ");
|
|
|
|
//s.append(getCStructBody(indent));
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
2009-12-23 14:35:29 -08:00
|
|
|
|
2010-08-09 18:13:33 -07:00
|
|
|
void Element::incRefs(const void *ptr) const
|
|
|
|
{
|
|
|
|
if (!mFieldCount) {
|
|
|
|
if (mComponent.isReference()) {
|
|
|
|
ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr);
|
|
|
|
ObjectBase *ob = obp[0];
|
|
|
|
ob->incSysRef();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *p = static_cast<const uint8_t *>(ptr);
|
|
|
|
for (uint32_t i=0; i < mFieldCount; i++) {
|
|
|
|
if (mFields[i].e->mHasReference) {
|
|
|
|
mFields[i].e->incRefs(&p[mFields[i].offsetBits >> 3]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Element::decRefs(const void *ptr) const
|
|
|
|
{
|
|
|
|
if (!mFieldCount) {
|
|
|
|
if (mComponent.isReference()) {
|
|
|
|
ObjectBase *const*obp = static_cast<ObjectBase *const*>(ptr);
|
|
|
|
ObjectBase *ob = obp[0];
|
|
|
|
ob->decSysRef();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *p = static_cast<const uint8_t *>(ptr);
|
|
|
|
for (uint32_t i=0; i < mFieldCount; i++) {
|
|
|
|
if (mFields[i].e->mHasReference) {
|
|
|
|
mFields[i].e->decRefs(&p[mFields[i].offsetBits >> 3]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-23 14:35:29 -08:00
|
|
|
|
2009-12-15 12:58:36 -08:00
|
|
|
|
2009-05-22 14:03:28 -07:00
|
|
|
ElementState::ElementState()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ElementState::~ElementState()
|
|
|
|
{
|
2010-02-17 15:38:10 -08:00
|
|
|
rsAssert(!mElements.size());
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 12:58:36 -08:00
|
|
|
|
2009-05-22 14:03:28 -07:00
|
|
|
/////////////////////////////////////////
|
2009-08-09 17:01:55 -07:00
|
|
|
//
|
2009-05-22 14:03:28 -07:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace renderscript {
|
|
|
|
|
2009-12-23 14:35:29 -08:00
|
|
|
RsElement rsi_ElementCreate(Context *rsc,
|
|
|
|
RsDataType dt,
|
|
|
|
RsDataKind dk,
|
|
|
|
bool norm,
|
|
|
|
uint32_t vecSize)
|
2009-05-22 14:03:28 -07:00
|
|
|
{
|
2009-12-23 14:35:29 -08:00
|
|
|
//LOGE("rsi_ElementCreate %i %i %i %i", dt, dk, norm, vecSize);
|
2010-02-17 15:38:10 -08:00
|
|
|
const Element *e = Element::create(rsc, dt, dk, norm, vecSize);
|
2009-12-23 14:35:29 -08:00
|
|
|
e->incUserRef();
|
2010-02-17 15:38:10 -08:00
|
|
|
return (RsElement)e;
|
2009-12-23 14:35:29 -08:00
|
|
|
}
|
2009-12-15 12:58:36 -08:00
|
|
|
|
2009-12-23 14:35:29 -08:00
|
|
|
RsElement rsi_ElementCreate2(Context *rsc,
|
|
|
|
size_t count,
|
|
|
|
const RsElement * ein,
|
|
|
|
const char ** names,
|
|
|
|
const size_t * nameLengths)
|
|
|
|
{
|
|
|
|
//LOGE("rsi_ElementCreate2 %i", count);
|
2010-02-17 15:38:10 -08:00
|
|
|
const Element *e = Element::create(rsc, count, (const Element **)ein, names, nameLengths);
|
2009-12-23 14:35:29 -08:00
|
|
|
e->incUserRef();
|
2010-02-17 15:38:10 -08:00
|
|
|
return (RsElement)e;
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
2010-07-15 11:33:03 -07:00
|
|
|
void rsi_ElementGetNativeData(Context *rsc, RsElement elem, uint32_t *elemData, uint32_t elemDataSize)
|
|
|
|
{
|
|
|
|
rsAssert(elemDataSize == 5);
|
|
|
|
// we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
|
|
|
|
Element *e = static_cast<Element *>(elem);
|
|
|
|
|
|
|
|
(*elemData++) = (uint32_t)e->getType();
|
|
|
|
(*elemData++) = (uint32_t)e->getKind();
|
|
|
|
(*elemData++) = e->getComponent().getIsNormalized() ? 1 : 0;
|
|
|
|
(*elemData++) = e->getComponent().getVectorSize();
|
|
|
|
(*elemData++) = e->getFieldCount();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void rsi_ElementGetSubElements(Context *rsc, RsElement elem, uint32_t *ids, const char **names, uint32_t dataSize)
|
|
|
|
{
|
|
|
|
Element *e = static_cast<Element *>(elem);
|
|
|
|
rsAssert(e->getFieldCount() == dataSize);
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < dataSize; i ++) {
|
|
|
|
ids[i] = (uint32_t)e->getField(i);
|
|
|
|
names[i] = e->getFieldName(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-05-22 14:03:28 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|