rsType.cpp revision 03f1910431433f3a35f1445aa0bba2b00417f0cc
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
17#include "rsContext.h"
18
19using namespace android;
20using namespace android::renderscript;
21
22Type::Type(Context *rsc) : ObjectBase(rsc) {
23    mLODs = 0;
24    mLODCount = 0;
25    clear();
26}
27
28void Type::preDestroy() const {
29    for (uint32_t ct = 0; ct < mRSC->mStateType.mTypes.size(); ct++) {
30        if (mRSC->mStateType.mTypes[ct] == this) {
31            mRSC->mStateType.mTypes.removeAt(ct);
32            break;
33        }
34    }
35}
36
37Type::~Type() {
38    if (mLODs) {
39        delete [] mLODs;
40        mLODs = NULL;
41    }
42}
43
44void Type::clear() {
45    if (mLODs) {
46        delete [] mLODs;
47        mLODs = NULL;
48    }
49    mElement.clear();
50    memset(&mHal, 0, sizeof(mHal));
51}
52
53TypeState::TypeState() {
54}
55
56TypeState::~TypeState() {
57    rsAssert(!mTypes.size());
58}
59
60size_t Type::getOffsetForFace(uint32_t face) const {
61    rsAssert(mHal.state.faces);
62    return 0;
63}
64
65void Type::compute() {
66    uint32_t oldLODCount = mLODCount;
67    if (mHal.state.dimLOD) {
68        uint32_t l2x = rsFindHighBit(mHal.state.dimX) + 1;
69        uint32_t l2y = rsFindHighBit(mHal.state.dimY) + 1;
70        uint32_t l2z = rsFindHighBit(mHal.state.dimZ) + 1;
71
72        mLODCount = rsMax(l2x, l2y);
73        mLODCount = rsMax(mLODCount, l2z);
74    } else {
75        mLODCount = 1;
76    }
77    if (mLODCount != oldLODCount) {
78        if (mLODs){
79            delete [] mLODs;
80        }
81        mLODs = new LOD[mLODCount];
82    }
83
84    uint32_t tx = mHal.state.dimX;
85    uint32_t ty = mHal.state.dimY;
86    uint32_t tz = mHal.state.dimZ;
87    size_t offset = 0;
88    for (uint32_t lod=0; lod < mLODCount; lod++) {
89        mLODs[lod].mX = tx;
90        mLODs[lod].mY = ty;
91        mLODs[lod].mZ = tz;
92        mLODs[lod].mOffset = offset;
93        offset += tx * rsMax(ty, 1u) * rsMax(tz, 1u) * mElement->getSizeBytes();
94        if (tx > 1) tx >>= 1;
95        if (ty > 1) ty >>= 1;
96        if (tz > 1) tz >>= 1;
97    }
98
99    // At this point the offset is the size of a mipmap chain;
100    mMipChainSizeBytes = offset;
101
102    if (mHal.state.faces) {
103        offset *= 6;
104    }
105    mTotalSizeBytes = offset;
106    mHal.state.element = mElement.get();
107}
108
109uint32_t Type::getLODOffset(uint32_t lod, uint32_t x) const {
110    uint32_t offset = mLODs[lod].mOffset;
111    offset += x * mElement->getSizeBytes();
112    return offset;
113}
114
115uint32_t Type::getLODOffset(uint32_t lod, uint32_t x, uint32_t y) const {
116    uint32_t offset = mLODs[lod].mOffset;
117    offset += (x + y * mLODs[lod].mX) * mElement->getSizeBytes();
118    return offset;
119}
120
121uint32_t Type::getLODOffset(uint32_t lod, uint32_t x, uint32_t y, uint32_t z) const {
122    uint32_t offset = mLODs[lod].mOffset;
123    offset += (x + y*mLODs[lod].mX + z*mLODs[lod].mX*mLODs[lod].mY) * mElement->getSizeBytes();
124    return offset;
125}
126
127uint32_t Type::getLODFaceOffset(uint32_t lod, RsAllocationCubemapFace face,
128                                uint32_t x, uint32_t y) const {
129    uint32_t offset = mLODs[lod].mOffset;
130    offset += (x + y * mLODs[lod].mX) * mElement->getSizeBytes();
131
132    if (face != 0) {
133        uint32_t faceOffset = getSizeBytes() / 6;
134        offset += faceOffset * face;
135    }
136    return offset;
137}
138
139void Type::dumpLOGV(const char *prefix) const {
140    char buf[1024];
141    ObjectBase::dumpLOGV(prefix);
142    ALOGV("%s   Type: x=%u y=%u z=%u mip=%i face=%i", prefix,
143                                                      mHal.state.dimX,
144                                                      mHal.state.dimY,
145                                                      mHal.state.dimZ,
146                                                      mHal.state.dimLOD,
147                                                      mHal.state.faces);
148    snprintf(buf, sizeof(buf), "%s element: ", prefix);
149    mElement->dumpLOGV(buf);
150}
151
152void Type::serialize(OStream *stream) const {
153    // Need to identify ourselves
154    stream->addU32((uint32_t)getClassId());
155
156    String8 name(getName());
157    stream->addString(&name);
158
159    mElement->serialize(stream);
160
161    stream->addU32(mHal.state.dimX);
162    stream->addU32(mHal.state.dimY);
163    stream->addU32(mHal.state.dimZ);
164
165    stream->addU8((uint8_t)(mHal.state.dimLOD ? 1 : 0));
166    stream->addU8((uint8_t)(mHal.state.faces ? 1 : 0));
167}
168
169Type *Type::createFromStream(Context *rsc, IStream *stream) {
170    // First make sure we are reading the correct object
171    RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
172    if (classID != RS_A3D_CLASS_ID_TYPE) {
173        ALOGE("type loading skipped due to invalid class id\n");
174        return NULL;
175    }
176
177    String8 name;
178    stream->loadString(&name);
179
180    Element *elem = Element::createFromStream(rsc, stream);
181    if (!elem) {
182        return NULL;
183    }
184
185    uint32_t x = stream->loadU32();
186    uint32_t y = stream->loadU32();
187    uint32_t z = stream->loadU32();
188    uint8_t lod = stream->loadU8();
189    uint8_t faces = stream->loadU8();
190    Type *type = Type::getType(rsc, elem, x, y, z, lod != 0, faces !=0 );
191    elem->decUserRef();
192    return type;
193}
194
195bool Type::getIsNp2() const {
196    uint32_t x = getDimX();
197    uint32_t y = getDimY();
198    uint32_t z = getDimZ();
199
200    if (x && (x & (x-1))) {
201        return true;
202    }
203    if (y && (y & (y-1))) {
204        return true;
205    }
206    if (z && (z & (z-1))) {
207        return true;
208    }
209    return false;
210}
211
212ObjectBaseRef<Type> Type::getTypeRef(Context *rsc, const Element *e,
213                                     uint32_t dimX, uint32_t dimY, uint32_t dimZ,
214                                     bool dimLOD, bool dimFaces) {
215    ObjectBaseRef<Type> returnRef;
216
217    TypeState * stc = &rsc->mStateType;
218
219    ObjectBase::asyncLock();
220    for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) {
221        Type *t = stc->mTypes[ct];
222        if (t->getElement() != e) continue;
223        if (t->getDimX() != dimX) continue;
224        if (t->getDimY() != dimY) continue;
225        if (t->getDimZ() != dimZ) continue;
226        if (t->getDimLOD() != dimLOD) continue;
227        if (t->getDimFaces() != dimFaces) continue;
228        returnRef.set(t);
229        ObjectBase::asyncUnlock();
230        return returnRef;
231    }
232    ObjectBase::asyncUnlock();
233
234
235    Type *nt = new Type(rsc);
236    returnRef.set(nt);
237    nt->mElement.set(e);
238    nt->mHal.state.dimX = dimX;
239    nt->mHal.state.dimY = dimY;
240    nt->mHal.state.dimZ = dimZ;
241    nt->mHal.state.dimLOD = dimLOD;
242    nt->mHal.state.faces = dimFaces;
243    nt->compute();
244
245    ObjectBase::asyncLock();
246    stc->mTypes.push(nt);
247    ObjectBase::asyncUnlock();
248
249    return returnRef;
250}
251
252ObjectBaseRef<Type> Type::cloneAndResize1D(Context *rsc, uint32_t dimX) const {
253    return getTypeRef(rsc, mElement.get(), dimX,
254                      mHal.state.dimY, mHal.state.dimZ, mHal.state.dimLOD, mHal.state.faces);
255}
256
257ObjectBaseRef<Type> Type::cloneAndResize2D(Context *rsc,
258                              uint32_t dimX,
259                              uint32_t dimY) const {
260    return getTypeRef(rsc, mElement.get(), dimX, dimY,
261                      mHal.state.dimZ, mHal.state.dimLOD, mHal.state.faces);
262}
263
264
265//////////////////////////////////////////////////
266//
267namespace android {
268namespace renderscript {
269
270RsType rsi_TypeCreate(Context *rsc, RsElement _e, uint32_t dimX,
271                     uint32_t dimY, uint32_t dimZ, bool mips, bool faces) {
272    Element *e = static_cast<Element *>(_e);
273
274    return Type::getType(rsc, e, dimX, dimY, dimZ, mips, faces);
275}
276
277}
278}
279
280void rsaTypeGetNativeData(RsContext con, RsType type, uint32_t *typeData, uint32_t typeDataSize) {
281    rsAssert(typeDataSize == 6);
282    // Pack the data in the follofing way mHal.state.dimX; mHal.state.dimY; mHal.state.dimZ;
283    // mHal.state.dimLOD; mHal.state.faces; mElement; into typeData
284    Type *t = static_cast<Type *>(type);
285
286    (*typeData++) = t->getDimX();
287    (*typeData++) = t->getDimY();
288    (*typeData++) = t->getDimZ();
289    (*typeData++) = t->getDimLOD();
290    (*typeData++) = t->getDimFaces() ? 1 : 0;
291    (*typeData++) = (uint32_t)t->getElement();
292    t->getElement()->incUserRef();
293}
294