rsType.cpp revision 0b575de8ed0b628d84d256f5846500b0385979bd
1/*
2 * Copyright (C) 2013 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
19#ifndef RS_SERVER
20#include "system/graphics.h"
21#endif
22
23using namespace android;
24using namespace android::renderscript;
25
26Type::Type(Context *rsc) : ObjectBase(rsc) {
27    memset(&mHal, 0, sizeof(mHal));
28    mDimLOD = false;
29}
30
31void Type::preDestroy() const {
32    for (uint32_t ct = 0; ct < mRSC->mStateType.mTypes.size(); ct++) {
33        if (mRSC->mStateType.mTypes[ct] == this) {
34            mRSC->mStateType.mTypes.removeAt(ct);
35            break;
36        }
37    }
38}
39
40Type::~Type() {
41    clear();
42}
43
44void Type::clear() {
45    if (mHal.state.lodCount) {
46        delete [] mHal.state.lodDimX;
47        delete [] mHal.state.lodDimY;
48        delete [] mHal.state.lodDimZ;
49        delete [] mHal.state.lodOffset;
50    }
51    mElement.clear();
52    memset(&mHal, 0, sizeof(mHal));
53}
54
55TypeState::TypeState() {
56}
57
58TypeState::~TypeState() {
59    rsAssert(!mTypes.size());
60}
61
62size_t Type::getOffsetForFace(uint32_t face) const {
63    rsAssert(mHal.state.faces);
64    return 0;
65}
66
67void Type::compute() {
68    uint32_t oldLODCount = mHal.state.lodCount;
69    if (mDimLOD) {
70        uint32_t l2x = rsFindHighBit(mHal.state.dimX) + 1;
71        uint32_t l2y = rsFindHighBit(mHal.state.dimY) + 1;
72        uint32_t l2z = rsFindHighBit(mHal.state.dimZ) + 1;
73
74        mHal.state.lodCount = rsMax(l2x, l2y);
75        mHal.state.lodCount = rsMax(mHal.state.lodCount, l2z);
76    } else {
77        mHal.state.lodCount = 1;
78    }
79    if (mHal.state.lodCount != oldLODCount) {
80        if (oldLODCount) {
81            delete [] mHal.state.lodDimX;
82            delete [] mHal.state.lodDimY;
83            delete [] mHal.state.lodDimZ;
84            delete [] mHal.state.lodOffset;
85        }
86        mHal.state.lodDimX = new uint32_t[mHal.state.lodCount];
87        mHal.state.lodDimY = new uint32_t[mHal.state.lodCount];
88        mHal.state.lodDimZ = new uint32_t[mHal.state.lodCount];
89        mHal.state.lodOffset = new uint32_t[mHal.state.lodCount];
90    }
91
92    uint32_t tx = mHal.state.dimX;
93    uint32_t ty = mHal.state.dimY;
94    uint32_t tz = mHal.state.dimZ;
95    size_t offset = 0;
96    for (uint32_t lod=0; lod < mHal.state.lodCount; lod++) {
97        mHal.state.lodDimX[lod] = tx;
98        mHal.state.lodDimY[lod] = ty;
99        mHal.state.lodDimZ[lod]  = tz;
100        mHal.state.lodOffset[lod] = offset;
101        offset += tx * rsMax(ty, 1u) * rsMax(tz, 1u) * mElement->getSizeBytes();
102        if (tx > 1) tx >>= 1;
103        if (ty > 1) ty >>= 1;
104        if (tz > 1) tz >>= 1;
105    }
106
107    // At this point the offset is the size of a mipmap chain;
108    mMipChainSizeBytes = offset;
109
110    if (mHal.state.faces) {
111        offset *= 6;
112    }
113#ifndef RS_SERVER
114    // YUV only supports basic 2d
115    // so we can stash the plane pointers in the mipmap levels.
116    if (mHal.state.dimYuv) {
117        switch(mHal.state.dimYuv) {
118        case HAL_PIXEL_FORMAT_YV12:
119            mHal.state.lodOffset[1] = offset;
120            mHal.state.lodDimX[1] = mHal.state.lodDimX[0] / 2;
121            mHal.state.lodDimY[1] = mHal.state.lodDimY[0] / 2;
122            offset += offset / 4;
123            mHal.state.lodOffset[2] = offset;
124            mHal.state.lodDimX[2] = mHal.state.lodDimX[0] / 2;
125            mHal.state.lodDimY[2] = mHal.state.lodDimY[0] / 2;
126            offset += offset / 4;
127            break;
128        case HAL_PIXEL_FORMAT_YCrCb_420_SP:  // NV21
129            mHal.state.lodOffset[1] = offset;
130            mHal.state.lodDimX[1] = mHal.state.lodDimX[0];
131            mHal.state.lodDimY[1] = mHal.state.lodDimY[0] / 2;
132            offset += offset / 2;
133            break;
134        default:
135            rsAssert(0);
136        }
137    }
138#endif
139    mTotalSizeBytes = offset;
140    mHal.state.element = mElement.get();
141}
142
143uint32_t Type::getLODOffset(uint32_t lod, uint32_t x) const {
144    uint32_t offset = mHal.state.lodOffset[lod];
145    offset += x * mElement->getSizeBytes();
146    return offset;
147}
148
149uint32_t Type::getLODOffset(uint32_t lod, uint32_t x, uint32_t y) const {
150    uint32_t offset = mHal.state.lodOffset[lod];
151    offset += (x + y * mHal.state.lodDimX[lod]) * mElement->getSizeBytes();
152    return offset;
153}
154
155uint32_t Type::getLODOffset(uint32_t lod, uint32_t x, uint32_t y, uint32_t z) const {
156    uint32_t offset = mHal.state.lodOffset[lod];
157    offset += (x +
158               y * mHal.state.lodDimX[lod] +
159               z * mHal.state.lodDimX[lod] * mHal.state.lodDimY[lod]) * mElement->getSizeBytes();
160    return offset;
161}
162
163uint32_t Type::getLODFaceOffset(uint32_t lod, RsAllocationCubemapFace face,
164                                uint32_t x, uint32_t y) const {
165    uint32_t offset = mHal.state.lodOffset[lod];
166    offset += (x + y * mHal.state.lodDimX[lod]) * mElement->getSizeBytes();
167
168    if (face != 0) {
169        uint32_t faceOffset = getSizeBytes() / 6;
170        offset += faceOffset * face;
171    }
172    return offset;
173}
174
175void Type::dumpLOGV(const char *prefix) const {
176    char buf[1024];
177    ObjectBase::dumpLOGV(prefix);
178    ALOGV("%s   Type: x=%u y=%u z=%u mip=%i face=%i", prefix,
179                                                      mHal.state.dimX,
180                                                      mHal.state.dimY,
181                                                      mHal.state.dimZ,
182                                                      mHal.state.lodCount,
183                                                      mHal.state.faces);
184    snprintf(buf, sizeof(buf), "%s element: ", prefix);
185    mElement->dumpLOGV(buf);
186}
187
188void Type::serialize(Context *rsc, OStream *stream) const {
189    // Need to identify ourselves
190    stream->addU32((uint32_t)getClassId());
191
192    String8 name(getName());
193    stream->addString(&name);
194
195    mElement->serialize(rsc, stream);
196
197    stream->addU32(mHal.state.dimX);
198    stream->addU32(mHal.state.dimY);
199    stream->addU32(mHal.state.dimZ);
200
201    stream->addU8((uint8_t)(mHal.state.lodCount ? 1 : 0));
202    stream->addU8((uint8_t)(mHal.state.faces ? 1 : 0));
203}
204
205Type *Type::createFromStream(Context *rsc, IStream *stream) {
206    // First make sure we are reading the correct object
207    RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
208    if (classID != RS_A3D_CLASS_ID_TYPE) {
209        ALOGE("type loading skipped due to invalid class id\n");
210        return NULL;
211    }
212
213    String8 name;
214    stream->loadString(&name);
215
216    Element *elem = Element::createFromStream(rsc, stream);
217    if (!elem) {
218        return NULL;
219    }
220
221    uint32_t x = stream->loadU32();
222    uint32_t y = stream->loadU32();
223    uint32_t z = stream->loadU32();
224    uint8_t lod = stream->loadU8();
225    uint8_t faces = stream->loadU8();
226    Type *type = Type::getType(rsc, elem, x, y, z, lod != 0, faces !=0, 0);
227    elem->decUserRef();
228    return type;
229}
230
231bool Type::getIsNp2() const {
232    uint32_t x = getDimX();
233    uint32_t y = getDimY();
234    uint32_t z = getDimZ();
235
236    if (x && (x & (x-1))) {
237        return true;
238    }
239    if (y && (y & (y-1))) {
240        return true;
241    }
242    if (z && (z & (z-1))) {
243        return true;
244    }
245    return false;
246}
247
248ObjectBaseRef<Type> Type::getTypeRef(Context *rsc, const Element *e,
249                                     uint32_t dimX, uint32_t dimY, uint32_t dimZ,
250                                     bool dimLOD, bool dimFaces, uint32_t dimYuv) {
251    ObjectBaseRef<Type> returnRef;
252
253    TypeState * stc = &rsc->mStateType;
254
255    ObjectBase::asyncLock();
256    for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) {
257        Type *t = stc->mTypes[ct];
258        if (t->getElement() != e) continue;
259        if (t->getDimX() != dimX) continue;
260        if (t->getDimY() != dimY) continue;
261        if (t->getDimZ() != dimZ) continue;
262        if (t->getDimLOD() != dimLOD) continue;
263        if (t->getDimFaces() != dimFaces) continue;
264        if (t->getDimYuv() != dimYuv) continue;
265        returnRef.set(t);
266        ObjectBase::asyncUnlock();
267        return returnRef;
268    }
269    ObjectBase::asyncUnlock();
270
271
272    Type *nt = new Type(rsc);
273    nt->mDimLOD = dimLOD;
274    returnRef.set(nt);
275    nt->mElement.set(e);
276    nt->mHal.state.dimX = dimX;
277    nt->mHal.state.dimY = dimY;
278    nt->mHal.state.dimZ = dimZ;
279    nt->mHal.state.faces = dimFaces;
280    nt->mHal.state.dimYuv = dimYuv;
281    nt->compute();
282
283    ObjectBase::asyncLock();
284    stc->mTypes.push(nt);
285    ObjectBase::asyncUnlock();
286
287    return returnRef;
288}
289
290ObjectBaseRef<Type> Type::cloneAndResize1D(Context *rsc, uint32_t dimX) const {
291    return getTypeRef(rsc, mElement.get(), dimX,
292                      getDimY(), getDimZ(), getDimLOD(), getDimFaces(), getDimYuv());
293}
294
295ObjectBaseRef<Type> Type::cloneAndResize2D(Context *rsc,
296                              uint32_t dimX,
297                              uint32_t dimY) const {
298    return getTypeRef(rsc, mElement.get(), dimX, dimY,
299                      getDimZ(), getDimLOD(), getDimFaces(), getDimYuv());
300}
301
302
303void Type::incRefs(const void *ptr, size_t ct, size_t startOff) const {
304    const uint8_t *p = static_cast<const uint8_t *>(ptr);
305    const Element *e = mHal.state.element;
306    uint32_t stride = e->getSizeBytes();
307
308    p += stride * startOff;
309    while (ct > 0) {
310        e->incRefs(p);
311        ct--;
312        p += stride;
313    }
314}
315
316
317void Type::decRefs(const void *ptr, size_t ct, size_t startOff) const {
318    if (!mHal.state.element->getHasReferences()) {
319        return;
320    }
321    const uint8_t *p = static_cast<const uint8_t *>(ptr);
322    const Element *e = mHal.state.element;
323    uint32_t stride = e->getSizeBytes();
324
325    p += stride * startOff;
326    while (ct > 0) {
327        e->decRefs(p);
328        ct--;
329        p += stride;
330    }
331}
332
333
334//////////////////////////////////////////////////
335//
336namespace android {
337namespace renderscript {
338
339RsType rsi_TypeCreate(Context *rsc, RsElement _e, uint32_t dimX,
340                     uint32_t dimY, uint32_t dimZ, bool mips, bool faces, uint32_t yuv) {
341    Element *e = static_cast<Element *>(_e);
342
343    return Type::getType(rsc, e, dimX, dimY, dimZ, mips, faces, yuv);
344}
345
346}
347}
348
349void rsaTypeGetNativeData(RsContext con, RsType type, uint32_t *typeData, uint32_t typeDataSize) {
350    rsAssert(typeDataSize == 6);
351    // Pack the data in the follofing way mHal.state.dimX; mHal.state.dimY; mHal.state.dimZ;
352    // mHal.state.lodCount; mHal.state.faces; mElement; into typeData
353    Type *t = static_cast<Type *>(type);
354
355    (*typeData++) = t->getDimX();
356    (*typeData++) = t->getDimY();
357    (*typeData++) = t->getDimZ();
358    (*typeData++) = t->getDimLOD() ? 1 : 0;
359    (*typeData++) = t->getDimFaces() ? 1 : 0;
360    (*typeData++) = (uint32_t)t->getElement();
361    t->getElement()->incUserRef();
362}
363