blob: 271c9e2361a41d5bc1a8dc27ebd3d1a58b7c4d78 [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
17#include "rsContext.h"
18
19using namespace android;
20using namespace android::renderscript;
21
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080022Type::Type(Context *rsc) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -070023 mLODs = 0;
24 mLODCount = 0;
25 clear();
26}
27
Alex Sakhartchoukc700e642011-08-16 13:09:46 -070028void Type::preDestroy() const {
Jason Sams81549542010-02-17 15:38:10 -080029 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 }
Jason Sams225afd32010-10-21 14:06:55 -070035}
36
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080037Type::~Type() {
Jason Sams326e0dd2009-05-22 14:03:28 -070038 if (mLODs) {
39 delete [] mLODs;
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070040 mLODs = NULL;
41 }
Jason Sams326e0dd2009-05-22 14:03:28 -070042}
43
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080044void Type::clear() {
Jason Sams326e0dd2009-05-22 14:03:28 -070045 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080057TypeState::TypeState() {
Jason Sams326e0dd2009-05-22 14:03:28 -070058}
59
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080060TypeState::~TypeState() {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -070061 rsAssert(!mTypes.size());
Jason Sams326e0dd2009-05-22 14:03:28 -070062}
63
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080064size_t Type::getOffsetForFace(uint32_t face) const {
Jason Sams326e0dd2009-05-22 14:03:28 -070065 rsAssert(mFaces);
66 return 0;
67}
68
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080069void Type::compute() {
Jason Sams326e0dd2009-05-22 14:03:28 -070070 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) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080082 if (mLODs){
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070083 delete [] mLODs;
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -070084 }
Jason Sams326e0dd2009-05-22 14:03:28 -070085 mLODs = new LOD[mLODCount];
86 }
87
Jason Sams326e0dd2009-05-22 14:03:28 -070088 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;
Jason Sams326e0dd2009-05-22 14:03:28 -070097 offset += tx * rsMax(ty, 1u) * rsMax(tz, 1u) * mElement->getSizeBytes();
Jason Sams7c528982010-01-07 16:25:08 -080098 if (tx > 1) tx >>= 1;
99 if (ty > 1) ty >>= 1;
100 if (tz > 1) tz >>= 1;
Jason Sams326e0dd2009-05-22 14:03:28 -0700101 }
102
Jason Sams326e0dd2009-05-22 14:03:28 -0700103 // 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;
Jason Sams326e0dd2009-05-22 14:03:28 -0700110}
111
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800112uint32_t Type::getLODOffset(uint32_t lod, uint32_t x) const {
Jason Sams326e0dd2009-05-22 14:03:28 -0700113 uint32_t offset = mLODs[lod].mOffset;
114 offset += x * mElement->getSizeBytes();
115 return offset;
116}
117
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800118uint32_t Type::getLODOffset(uint32_t lod, uint32_t x, uint32_t y) const {
Jason Sams326e0dd2009-05-22 14:03:28 -0700119 uint32_t offset = mLODs[lod].mOffset;
120 offset += (x + y * mLODs[lod].mX) * mElement->getSizeBytes();
121 return offset;
122}
123
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800124uint32_t Type::getLODOffset(uint32_t lod, uint32_t x, uint32_t y, uint32_t z) const {
Jason Sams326e0dd2009-05-22 14:03:28 -0700125 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
Jason Samsbcac9342011-01-13 17:38:18 -0800130uint32_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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800141void Type::dumpLOGV(const char *prefix) const {
Jason Samse12c1c52009-09-27 17:50:38 -0700142 char buf[1024];
143 ObjectBase::dumpLOGV(prefix);
Steve Block65982012011-10-20 11:56:00 +0100144 ALOGV("%s Type: x=%zu y=%zu z=%zu mip=%i face=%i", prefix, mDimX, mDimY, mDimZ, mDimLOD, mFaces);
Jason Sams87319de2010-11-22 16:20:16 -0800145 snprintf(buf, sizeof(buf), "%s element: ", prefix);
Jason Samse12c1c52009-09-27 17:50:38 -0700146 mElement->dumpLOGV(buf);
147}
148
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800149void Type::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700150 // 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800166Type *Type::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700167 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700168 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800169 if (classID != RS_A3D_CLASS_ID_TYPE) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700170 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);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800178 if (!elem) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700179 return NULL;
180 }
181
Jason Samsf0c1df42010-10-26 13:09:17 -0700182 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();
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700187 Type *type = Type::getType(rsc, elem, x, y, z, lod != 0, faces !=0 );
188 elem->decUserRef();
189 return type;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700190}
191
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800192bool Type::getIsNp2() const {
Jason Samsef21edc2010-02-22 15:37:51 -0800193 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
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700209ObjectBaseRef<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;
Jason Samse12c1c52009-09-27 17:50:38 -0700213
Jason Sams96abf812010-10-05 13:32:49 -0700214 TypeState * stc = &rsc->mStateType;
Jason Samsf0c1df42010-10-26 13:09:17 -0700215
216 ObjectBase::asyncLock();
Jason Sams96abf812010-10-05 13:32:49 -0700217 for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) {
218 Type *t = stc->mTypes[ct];
Jason Samsf0c1df42010-10-26 13:09:17 -0700219 if (t->getElement() != e) continue;
Jason Sams96abf812010-10-05 13:32:49 -0700220 if (t->getDimX() != dimX) continue;
Jason Samsf0c1df42010-10-26 13:09:17 -0700221 if (t->getDimY() != dimY) continue;
222 if (t->getDimZ() != dimZ) continue;
223 if (t->getDimLOD() != dimLOD) continue;
224 if (t->getDimFaces() != dimFaces) continue;
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700225 returnRef.set(t);
Jason Samsf0c1df42010-10-26 13:09:17 -0700226 ObjectBase::asyncUnlock();
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700227 return returnRef;
Jason Sams96abf812010-10-05 13:32:49 -0700228 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700229 ObjectBase::asyncUnlock();
230
Jason Sams96abf812010-10-05 13:32:49 -0700231
232 Type *nt = new Type(rsc);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700233 returnRef.set(nt);
Jason Samsf0c1df42010-10-26 13:09:17 -0700234 nt->mElement.set(e);
Jason Sams96abf812010-10-05 13:32:49 -0700235 nt->mDimX = dimX;
Jason Samsf0c1df42010-10-26 13:09:17 -0700236 nt->mDimY = dimY;
237 nt->mDimZ = dimZ;
238 nt->mDimLOD = dimLOD;
239 nt->mFaces = dimFaces;
Jason Sams96abf812010-10-05 13:32:49 -0700240 nt->compute();
Jason Samsf0c1df42010-10-26 13:09:17 -0700241
242 ObjectBase::asyncLock();
243 stc->mTypes.push(nt);
244 ObjectBase::asyncUnlock();
245
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700246 return returnRef;
Jason Sams96abf812010-10-05 13:32:49 -0700247}
248
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700249ObjectBaseRef<Type> Type::cloneAndResize1D(Context *rsc, uint32_t dimX) const {
250 return getTypeRef(rsc, mElement.get(), dimX,
251 mDimY, mDimZ, mDimLOD, mFaces);
Jason Samsf0c1df42010-10-26 13:09:17 -0700252}
253
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700254ObjectBaseRef<Type> Type::cloneAndResize2D(Context *rsc,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800255 uint32_t dimX,
256 uint32_t dimY) const {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700257 return getTypeRef(rsc, mElement.get(), dimX, dimY,
258 mDimZ, mDimLOD, mFaces);
Jason Sams96abf812010-10-05 13:32:49 -0700259}
260
261
Jason Sams326e0dd2009-05-22 14:03:28 -0700262//////////////////////////////////////////////////
Jason Samse5ffb872009-08-09 17:01:55 -0700263//
Jason Sams326e0dd2009-05-22 14:03:28 -0700264namespace android {
265namespace renderscript {
266
Jason Samsc975cf42011-04-28 18:26:48 -0700267RsType rsi_TypeCreate(Context *rsc, RsElement _e, uint32_t dimX,
Jason Sams25413ec2010-12-06 15:59:59 -0800268 uint32_t dimY, uint32_t dimZ, bool mips, bool faces) {
Jason Sams2353ae32010-10-14 17:48:46 -0700269 Element *e = static_cast<Element *>(_e);
Jason Sams2353ae32010-10-14 17:48:46 -0700270
Jason Sams25413ec2010-12-06 15:59:59 -0800271 return Type::getType(rsc, e, dimX, dimY, dimZ, mips, faces);
Jason Sams2353ae32010-10-14 17:48:46 -0700272}
273
Jason Samsc975cf42011-04-28 18:26:48 -0700274}
275}
276
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800277void rsaTypeGetNativeData(RsContext con, RsType type, uint32_t *typeData, uint32_t typeDataSize) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700278 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}