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