rsType.cpp revision 48ecf6a5e85a9a832f41393ed2802385bb8b5db8
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 stream->addString(getName()); 192 193 mElement->serialize(rsc, stream); 194 195 stream->addU32(mHal.state.dimX); 196 stream->addU32(mHal.state.dimY); 197 stream->addU32(mHal.state.dimZ); 198 199 stream->addU8((uint8_t)(mHal.state.lodCount ? 1 : 0)); 200 stream->addU8((uint8_t)(mHal.state.faces ? 1 : 0)); 201} 202 203Type *Type::createFromStream(Context *rsc, IStream *stream) { 204 // First make sure we are reading the correct object 205 RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); 206 if (classID != RS_A3D_CLASS_ID_TYPE) { 207 ALOGE("type loading skipped due to invalid class id\n"); 208 return NULL; 209 } 210 211 const char *name = stream->loadString(); 212 213 Element *elem = Element::createFromStream(rsc, stream); 214 if (!elem) { 215 return NULL; 216 } 217 218 uint32_t x = stream->loadU32(); 219 uint32_t y = stream->loadU32(); 220 uint32_t z = stream->loadU32(); 221 uint8_t lod = stream->loadU8(); 222 uint8_t faces = stream->loadU8(); 223 Type *type = Type::getType(rsc, elem, x, y, z, lod != 0, faces !=0, 0); 224 elem->decUserRef(); 225 226 delete [] name; 227 return type; 228} 229 230bool Type::getIsNp2() const { 231 uint32_t x = getDimX(); 232 uint32_t y = getDimY(); 233 uint32_t z = getDimZ(); 234 235 if (x && (x & (x-1))) { 236 return true; 237 } 238 if (y && (y & (y-1))) { 239 return true; 240 } 241 if (z && (z & (z-1))) { 242 return true; 243 } 244 return false; 245} 246 247ObjectBaseRef<Type> Type::getTypeRef(Context *rsc, const Element *e, 248 uint32_t dimX, uint32_t dimY, uint32_t dimZ, 249 bool dimLOD, bool dimFaces, uint32_t dimYuv) { 250 ObjectBaseRef<Type> returnRef; 251 252 TypeState * stc = &rsc->mStateType; 253 254 ObjectBase::asyncLock(); 255 for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) { 256 Type *t = stc->mTypes[ct]; 257 if (t->getElement() != e) continue; 258 if (t->getDimX() != dimX) continue; 259 if (t->getDimY() != dimY) continue; 260 if (t->getDimZ() != dimZ) continue; 261 if (t->getDimLOD() != dimLOD) continue; 262 if (t->getDimFaces() != dimFaces) continue; 263 if (t->getDimYuv() != dimYuv) continue; 264 returnRef.set(t); 265 ObjectBase::asyncUnlock(); 266 return returnRef; 267 } 268 ObjectBase::asyncUnlock(); 269 270 271 Type *nt = new Type(rsc); 272 nt->mDimLOD = dimLOD; 273 returnRef.set(nt); 274 nt->mElement.set(e); 275 nt->mHal.state.dimX = dimX; 276 nt->mHal.state.dimY = dimY; 277 nt->mHal.state.dimZ = dimZ; 278 nt->mHal.state.faces = dimFaces; 279 nt->mHal.state.dimYuv = dimYuv; 280 nt->compute(); 281 282 ObjectBase::asyncLock(); 283 stc->mTypes.push(nt); 284 ObjectBase::asyncUnlock(); 285 286 return returnRef; 287} 288 289ObjectBaseRef<Type> Type::cloneAndResize1D(Context *rsc, uint32_t dimX) const { 290 return getTypeRef(rsc, mElement.get(), dimX, 291 getDimY(), getDimZ(), getDimLOD(), getDimFaces(), getDimYuv()); 292} 293 294ObjectBaseRef<Type> Type::cloneAndResize2D(Context *rsc, 295 uint32_t dimX, 296 uint32_t dimY) const { 297 return getTypeRef(rsc, mElement.get(), dimX, dimY, 298 getDimZ(), getDimLOD(), getDimFaces(), getDimYuv()); 299} 300 301 302void Type::incRefs(const void *ptr, size_t ct, size_t startOff) const { 303 const uint8_t *p = static_cast<const uint8_t *>(ptr); 304 const Element *e = mHal.state.element; 305 uint32_t stride = e->getSizeBytes(); 306 307 p += stride * startOff; 308 while (ct > 0) { 309 e->incRefs(p); 310 ct--; 311 p += stride; 312 } 313} 314 315 316void Type::decRefs(const void *ptr, size_t ct, size_t startOff) const { 317 if (!mHal.state.element->getHasReferences()) { 318 return; 319 } 320 const uint8_t *p = static_cast<const uint8_t *>(ptr); 321 const Element *e = mHal.state.element; 322 uint32_t stride = e->getSizeBytes(); 323 324 p += stride * startOff; 325 while (ct > 0) { 326 e->decRefs(p); 327 ct--; 328 p += stride; 329 } 330} 331 332 333////////////////////////////////////////////////// 334// 335namespace android { 336namespace renderscript { 337 338RsType rsi_TypeCreate(Context *rsc, RsElement _e, uint32_t dimX, 339 uint32_t dimY, uint32_t dimZ, bool mips, bool faces, uint32_t yuv) { 340 Element *e = static_cast<Element *>(_e); 341 342 return Type::getType(rsc, e, dimX, dimY, dimZ, mips, faces, yuv); 343} 344 345} 346} 347 348void rsaTypeGetNativeData(RsContext con, RsType type, uintptr_t *typeData, uint32_t typeDataSize) { 349 rsAssert(typeDataSize == 6); 350 // Pack the data in the follofing way mHal.state.dimX; mHal.state.dimY; mHal.state.dimZ; 351 // mHal.state.lodCount; mHal.state.faces; mElement; into typeData 352 Type *t = static_cast<Type *>(type); 353 354 (*typeData++) = t->getDimX(); 355 (*typeData++) = t->getDimY(); 356 (*typeData++) = t->getDimZ(); 357 (*typeData++) = t->getDimLOD() ? 1 : 0; 358 (*typeData++) = t->getDimFaces() ? 1 : 0; 359 (*typeData++) = (uintptr_t)t->getElement(); 360 t->getElement()->incUserRef(); 361} 362