rsAllocation.cpp revision c700e649ca44d0dcff8b271e42d949ea72fe3c63
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#include "rs_hal.h" 19 20 21using namespace android; 22using namespace android::renderscript; 23 24Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages, 25 RsAllocationMipmapControl mc) 26 : ObjectBase(rsc) { 27 28 memset(&mHal, 0, sizeof(mHal)); 29 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE; 30 mHal.state.usageFlags = usages; 31 mHal.state.mipmapControl = mc; 32 33 mHal.state.type.set(type); 34 updateCache(); 35} 36 37Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages, 38 RsAllocationMipmapControl mc) { 39 Allocation *a = new Allocation(rsc, type, usages, mc); 40 41 if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) { 42 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure"); 43 delete a; 44 return NULL; 45 } 46 return a; 47} 48 49void Allocation::updateCache() { 50 const Type *type = mHal.state.type.get(); 51 mHal.state.dimensionX = type->getDimX(); 52 mHal.state.dimensionY = type->getDimY(); 53 mHal.state.dimensionZ = type->getDimZ(); 54 mHal.state.hasFaces = type->getDimFaces(); 55 mHal.state.hasMipmaps = type->getDimLOD(); 56 mHal.state.elementSizeBytes = type->getElementSizeBytes(); 57 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences(); 58} 59 60Allocation::~Allocation() { 61 if (mHal.state.hasReferences && 62 (mHal.state.hasFaces || mHal.state.hasMipmaps)) { 63 LOGE("Cube/mip allocation with references unsupported, memory not cleaned up!"); 64 } 65 66 uint32_t elemCount = mHal.state.dimensionX; 67 if (mHal.state.dimensionY > 1) { 68 elemCount *= mHal.state.dimensionY; 69 } 70 if (mHal.state.dimensionZ > 1) { 71 elemCount *= mHal.state.dimensionZ; 72 } 73 decRefs(getPtr(), elemCount, 0); 74 mRSC->mHal.funcs.allocation.destroy(mRSC, this); 75} 76 77void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) { 78 rsc->mHal.funcs.allocation.syncAll(rsc, this, src); 79} 80 81void Allocation::read(void *data) { 82 memcpy(data, getPtr(), mHal.state.type->getSizeBytes()); 83} 84 85void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod, 86 uint32_t count, const void *data, uint32_t sizeBytes) { 87 const uint32_t eSize = mHal.state.type->getElementSizeBytes(); 88 89 if ((count * eSize) != sizeBytes) { 90 LOGE("Allocation::subData called with mismatched size expected %i, got %i", 91 (count * eSize), sizeBytes); 92 mHal.state.type->dumpLOGV("type info"); 93 return; 94 } 95 96 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes); 97 sendDirty(rsc); 98} 99 100void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, 101 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) { 102 const uint32_t eSize = mHal.state.elementSizeBytes; 103 const uint32_t lineSize = eSize * w; 104 105 //LOGE("data2d %p, %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes); 106 107 if ((lineSize * h) != sizeBytes) { 108 LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes); 109 rsAssert(!"Allocation::subData called with mismatched size"); 110 return; 111 } 112 113 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes); 114 sendDirty(rsc); 115} 116 117void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, 118 uint32_t lod, RsAllocationCubemapFace face, 119 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) { 120} 121 122void Allocation::elementData(Context *rsc, uint32_t x, const void *data, 123 uint32_t cIdx, uint32_t sizeBytes) { 124 uint32_t eSize = mHal.state.elementSizeBytes; 125 126 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) { 127 LOGE("Error Allocation::subElementData component %i out of range.", cIdx); 128 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range."); 129 return; 130 } 131 132 if (x >= mHal.state.dimensionX) { 133 LOGE("Error Allocation::subElementData X offset %i out of range.", x); 134 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); 135 return; 136 } 137 138 const Element * e = mHal.state.type->getElement()->getField(cIdx); 139 if (sizeBytes != e->getSizeBytes()) { 140 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes()); 141 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); 142 return; 143 } 144 145 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes); 146 sendDirty(rsc); 147} 148 149void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y, 150 const void *data, uint32_t cIdx, uint32_t sizeBytes) { 151 uint32_t eSize = mHal.state.elementSizeBytes; 152 153 if (x >= mHal.state.dimensionX) { 154 LOGE("Error Allocation::subElementData X offset %i out of range.", x); 155 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); 156 return; 157 } 158 159 if (y >= mHal.state.dimensionY) { 160 LOGE("Error Allocation::subElementData X offset %i out of range.", x); 161 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); 162 return; 163 } 164 165 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) { 166 LOGE("Error Allocation::subElementData component %i out of range.", cIdx); 167 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range."); 168 return; 169 } 170 171 const Element * e = mHal.state.type->getElement()->getField(cIdx); 172 173 if (sizeBytes != e->getSizeBytes()) { 174 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes()); 175 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); 176 return; 177 } 178 179 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes); 180 sendDirty(rsc); 181} 182 183void Allocation::addProgramToDirty(const Program *p) { 184 mToDirtyList.push(p); 185} 186 187void Allocation::removeProgramToDirty(const Program *p) { 188 for (size_t ct=0; ct < mToDirtyList.size(); ct++) { 189 if (mToDirtyList[ct] == p) { 190 mToDirtyList.removeAt(ct); 191 return; 192 } 193 } 194 rsAssert(0); 195} 196 197void Allocation::dumpLOGV(const char *prefix) const { 198 ObjectBase::dumpLOGV(prefix); 199 200 String8 s(prefix); 201 s.append(" type "); 202 if (mHal.state.type.get()) { 203 mHal.state.type->dumpLOGV(s.string()); 204 } 205 206 LOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x", 207 prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl); 208} 209 210void Allocation::serialize(OStream *stream) const { 211 // Need to identify ourselves 212 stream->addU32((uint32_t)getClassId()); 213 214 String8 name(getName()); 215 stream->addString(&name); 216 217 // First thing we need to serialize is the type object since it will be needed 218 // to initialize the class 219 mHal.state.type->serialize(stream); 220 221 uint32_t dataSize = mHal.state.type->getSizeBytes(); 222 // Write how much data we are storing 223 stream->addU32(dataSize); 224 // Now write the data 225 stream->addByteArray(getPtr(), dataSize); 226} 227 228Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) { 229 // First make sure we are reading the correct object 230 RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); 231 if (classID != RS_A3D_CLASS_ID_ALLOCATION) { 232 LOGE("allocation loading skipped due to invalid class id\n"); 233 return NULL; 234 } 235 236 String8 name; 237 stream->loadString(&name); 238 239 Type *type = Type::createFromStream(rsc, stream); 240 if (!type) { 241 return NULL; 242 } 243 type->compute(); 244 245 // Number of bytes we wrote out for this allocation 246 uint32_t dataSize = stream->loadU32(); 247 if (dataSize != type->getSizeBytes()) { 248 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n"); 249 ObjectBase::checkDelete(type); 250 return NULL; 251 } 252 253 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT); 254 alloc->setName(name.string(), name.size()); 255 type->decUserRef(); 256 257 uint32_t count = dataSize / type->getElementSizeBytes(); 258 259 // Read in all of our allocation data 260 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize); 261 stream->reset(stream->getPos() + dataSize); 262 263 return alloc; 264} 265 266void Allocation::sendDirty(const Context *rsc) const { 267 for (size_t ct=0; ct < mToDirtyList.size(); ct++) { 268 mToDirtyList[ct]->forceDirty(); 269 } 270 mRSC->mHal.funcs.allocation.markDirty(rsc, this); 271} 272 273void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const { 274 const uint8_t *p = static_cast<const uint8_t *>(ptr); 275 const Element *e = mHal.state.type->getElement(); 276 uint32_t stride = e->getSizeBytes(); 277 278 p += stride * startOff; 279 while (ct > 0) { 280 e->incRefs(p); 281 ct --; 282 p += stride; 283 } 284} 285 286void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const { 287 if (!mHal.state.hasReferences || !getIsScript()) { 288 return; 289 } 290 const uint8_t *p = static_cast<const uint8_t *>(ptr); 291 const Element *e = mHal.state.type->getElement(); 292 uint32_t stride = e->getSizeBytes(); 293 294 p += stride * startOff; 295 while (ct > 0) { 296 e->decRefs(p); 297 ct --; 298 p += stride; 299 } 300} 301 302void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) { 303} 304 305void Allocation::resize1D(Context *rsc, uint32_t dimX) { 306 uint32_t oldDimX = mHal.state.dimensionX; 307 if (dimX == oldDimX) { 308 return; 309 } 310 311 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX); 312 if (dimX < oldDimX) { 313 decRefs(getPtr(), oldDimX - dimX, dimX); 314 } 315 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences); 316 mHal.state.type.set(t.get()); 317 updateCache(); 318} 319 320void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) { 321 LOGE("not implemented"); 322} 323 324///////////////// 325// 326 327namespace android { 328namespace renderscript { 329 330static void AllocationGenerateScriptMips(RsContext con, RsAllocation va); 331 332static void mip565(const Adapter2D &out, const Adapter2D &in) { 333 uint32_t w = out.getDimX(); 334 uint32_t h = out.getDimY(); 335 336 for (uint32_t y=0; y < h; y++) { 337 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y)); 338 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2)); 339 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1)); 340 341 for (uint32_t x=0; x < w; x++) { 342 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); 343 oPtr ++; 344 i1 += 2; 345 i2 += 2; 346 } 347 } 348} 349 350static void mip8888(const Adapter2D &out, const Adapter2D &in) { 351 uint32_t w = out.getDimX(); 352 uint32_t h = out.getDimY(); 353 354 for (uint32_t y=0; y < h; y++) { 355 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y)); 356 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2)); 357 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1)); 358 359 for (uint32_t x=0; x < w; x++) { 360 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); 361 oPtr ++; 362 i1 += 2; 363 i2 += 2; 364 } 365 } 366} 367 368static void mip8(const Adapter2D &out, const Adapter2D &in) { 369 uint32_t w = out.getDimX(); 370 uint32_t h = out.getDimY(); 371 372 for (uint32_t y=0; y < h; y++) { 373 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y)); 374 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2)); 375 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1)); 376 377 for (uint32_t x=0; x < w; x++) { 378 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f); 379 oPtr ++; 380 i1 += 2; 381 i2 += 2; 382 } 383 } 384} 385 386static void mip(const Adapter2D &out, const Adapter2D &in) { 387 switch (out.getBaseType()->getElement()->getSizeBits()) { 388 case 32: 389 mip8888(out, in); 390 break; 391 case 16: 392 mip565(out, in); 393 break; 394 case 8: 395 mip8(out, in); 396 break; 397 } 398} 399 400void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) { 401 Allocation *a = static_cast<Allocation *>(va); 402 a->sendDirty(rsc); 403 a->syncAll(rsc, src); 404} 405 406void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) { 407 Allocation *texAlloc = static_cast<Allocation *>(va); 408 AllocationGenerateScriptMips(rsc, texAlloc); 409} 410 411void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) { 412 Allocation *texAlloc = static_cast<Allocation *>(va); 413 const Type * t = texAlloc->getType(); 414 415 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes(); 416 if (s != dataLen) { 417 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size"); 418 return; 419 } 420 421 memcpy(data, texAlloc->getPtr(), s); 422} 423 424void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod, 425 uint32_t count, const void *data, size_t sizeBytes) { 426 Allocation *a = static_cast<Allocation *>(va); 427 a->data(rsc, xoff, lod, count, data, sizeBytes); 428} 429 430void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face, 431 const void *data, size_t eoff, uint32_t sizeBytes) { // TODO: this seems wrong, eoff and sizeBytes may be swapped 432 Allocation *a = static_cast<Allocation *>(va); 433 a->elementData(rsc, x, y, data, eoff, sizeBytes); 434} 435 436void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod, 437 const void *data, size_t eoff, uint32_t sizeBytes) { // TODO: this seems wrong, eoff and sizeBytes may be swapped 438 Allocation *a = static_cast<Allocation *>(va); 439 a->elementData(rsc, x, data, eoff, sizeBytes); 440} 441 442void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, 443 uint32_t w, uint32_t h, const void *data, size_t sizeBytes) { 444 Allocation *a = static_cast<Allocation *>(va); 445 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes); 446} 447 448void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) { 449 Allocation *a = static_cast<Allocation *>(va); 450 a->read(data); 451} 452 453void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) { 454 Allocation *a = static_cast<Allocation *>(va); 455 a->resize1D(rsc, dimX); 456} 457 458void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) { 459 Allocation *a = static_cast<Allocation *>(va); 460 a->resize2D(rsc, dimX, dimY); 461} 462 463static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) { 464 Context *rsc = static_cast<Context *>(con); 465 Allocation *texAlloc = static_cast<Allocation *>(va); 466 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1; 467 for (uint32_t face = 0; face < numFaces; face ++) { 468 Adapter2D adapt(rsc, texAlloc); 469 Adapter2D adapt2(rsc, texAlloc); 470 adapt.setFace(face); 471 adapt2.setFace(face); 472 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) { 473 adapt.setLOD(lod); 474 adapt2.setLOD(lod + 1); 475 mip(adapt2, adapt); 476 } 477 } 478} 479 480RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype, 481 RsAllocationMipmapControl mips, 482 uint32_t usages) { 483 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips); 484 if (!alloc) { 485 return NULL; 486 } 487 alloc->incUserRef(); 488 return alloc; 489} 490 491RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype, 492 RsAllocationMipmapControl mips, 493 const void *data, size_t data_length, uint32_t usages) { 494 Type *t = static_cast<Type *>(vtype); 495 496 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages); 497 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); 498 if (texAlloc == NULL) { 499 LOGE("Memory allocation failure"); 500 return NULL; 501 } 502 503 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes()); 504 if (mips == RS_ALLOCATION_MIPMAP_FULL) { 505 AllocationGenerateScriptMips(rsc, texAlloc); 506 } 507 508 texAlloc->sendDirty(rsc); 509 return texAlloc; 510} 511 512RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype, 513 RsAllocationMipmapControl mips, 514 const void *data, size_t data_length, uint32_t usages) { 515 Type *t = static_cast<Type *>(vtype); 516 517 // Cubemap allocation's faces should be Width by Width each. 518 // Source data should have 6 * Width by Width pixels 519 // Error checking is done in the java layer 520 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages); 521 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); 522 if (texAlloc == NULL) { 523 LOGE("Memory allocation failure"); 524 return NULL; 525 } 526 527 uint32_t faceSize = t->getDimX(); 528 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes(); 529 uint32_t copySize = faceSize * t->getElementSizeBytes(); 530 531 uint8_t *sourcePtr = (uint8_t*)data; 532 for (uint32_t face = 0; face < 6; face ++) { 533 Adapter2D faceAdapter(rsc, texAlloc); 534 faceAdapter.setFace(face); 535 536 for (uint32_t dI = 0; dI < faceSize; dI ++) { 537 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize); 538 } 539 540 // Move the data pointer to the next cube face 541 sourcePtr += copySize; 542 } 543 544 if (mips == RS_ALLOCATION_MIPMAP_FULL) { 545 AllocationGenerateScriptMips(rsc, texAlloc); 546 } 547 548 texAlloc->sendDirty(rsc); 549 return texAlloc; 550} 551 552void rsi_AllocationCopy2DRange(Context *rsc, 553 RsAllocation dstAlloc, 554 uint32_t dstXoff, uint32_t dstYoff, 555 uint32_t dstMip, uint32_t dstFace, 556 uint32_t width, uint32_t height, 557 RsAllocation srcAlloc, 558 uint32_t srcXoff, uint32_t srcYoff, 559 uint32_t srcMip, uint32_t srcFace) { 560 Allocation *dst = static_cast<Allocation *>(dstAlloc); 561 Allocation *src= static_cast<Allocation *>(srcAlloc); 562 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip, 563 (RsAllocationCubemapFace)dstFace, 564 width, height, 565 src, srcXoff, srcYoff,srcMip, 566 (RsAllocationCubemapFace)srcFace); 567} 568 569} 570} 571 572const void * rsaAllocationGetType(RsContext con, RsAllocation va) { 573 Allocation *a = static_cast<Allocation *>(va); 574 a->getType()->incUserRef(); 575 576 return a->getType(); 577} 578