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