rsAllocation.cpp revision b8353c5943f4038fd7f08db3d958390ce9418798
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#include "rsAllocation.h" 19#include "rsAdapter.h" 20#include "rs_hal.h" 21 22#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) 23#include "system/window.h" 24#include "gui/GLConsumer.h" 25#endif 26 27using namespace android; 28using namespace android::renderscript; 29 30Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages, 31 RsAllocationMipmapControl mc, void * ptr) 32 : ObjectBase(rsc) { 33 34 memset(&mHal, 0, sizeof(mHal)); 35 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE; 36 mHal.state.usageFlags = usages; 37 mHal.state.mipmapControl = mc; 38 mHal.state.userProvidedPtr = ptr; 39 40 setType(type); 41 updateCache(); 42} 43 44Allocation::Allocation(Context *rsc, const Allocation *alloc, const Type *type) 45 : ObjectBase(rsc) { 46 47 memset(&mHal, 0, sizeof(mHal)); 48 49 50 mHal.state.baseAlloc = alloc; 51 mHal.state.type = type; 52 mHal.state.usageFlags = alloc->mHal.state.usageFlags; 53 mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE; 54 55 setType(type); 56 updateCache(); 57 58 59 60 61 struct Hal { 62 void * drv; 63 64 struct DrvState { 65 struct LodState { 66 void * mallocPtr; 67 size_t stride; 68 uint32_t dimX; 69 uint32_t dimY; 70 uint32_t dimZ; 71 } lod[android::renderscript::Allocation::MAX_LOD]; 72 size_t faceOffset; 73 uint32_t lodCount; 74 uint32_t faceCount; 75 76 struct YuvState { 77 uint32_t shift; 78 uint32_t step; 79 } yuv; 80 81 int grallocFlags; 82 uint32_t dimArray[Type::mMaxArrays]; 83 }; 84 mutable DrvState drvState; 85 86 }; 87 Hal mHal; 88 89} 90 91void Allocation::operator delete(void* ptr) { 92 if (ptr) { 93 Allocation *a = (Allocation*) ptr; 94 a->getContext()->mHal.funcs.freeRuntimeMem(ptr); 95 } 96} 97 98Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages, 99 RsAllocationMipmapControl mc, void * ptr) { 100 // Allocation objects must use allocator specified by the driver 101 void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Allocation), 0); 102 103 if (!allocMem) { 104 rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation"); 105 return nullptr; 106 } 107 108 Allocation *a = new (allocMem) Allocation(rsc, type, usages, mc, ptr); 109 110 if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) { 111 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure"); 112 delete a; 113 return nullptr; 114 } 115 116 return a; 117} 118 119Allocation * Allocation::createAdapter(Context *rsc, const Allocation *alloc, const Type *type) { 120 // Allocation objects must use allocator specified by the driver 121 void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Allocation), 0); 122 123 if (!allocMem) { 124 rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation"); 125 return nullptr; 126 } 127 128 Allocation *a = new (allocMem) Allocation(rsc, alloc, type); 129 130 if (!rsc->mHal.funcs.allocation.initAdapter(rsc, a)) { 131 rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure"); 132 delete a; 133 return nullptr; 134 } 135 136 return a; 137} 138 139 140void Allocation::updateCache() { 141 const Type *type = mHal.state.type; 142 mHal.state.yuv = type->getDimYuv(); 143 mHal.state.hasFaces = type->getDimFaces(); 144 mHal.state.hasMipmaps = type->getDimLOD(); 145 mHal.state.elementSizeBytes = type->getElementSizeBytes(); 146 mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences(); 147} 148 149Allocation::~Allocation() { 150#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) 151 if (mGrallocConsumer.get()) { 152 mGrallocConsumer->unlockBuffer(); 153 mGrallocConsumer = nullptr; 154 } 155#endif 156 157 freeChildrenUnlocked(); 158 mRSC->mHal.funcs.allocation.destroy(mRSC, this); 159} 160 161void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) { 162 rsc->mHal.funcs.allocation.syncAll(rsc, this, src); 163} 164 165void * Allocation::getPointer(const Context *rsc, uint32_t lod, RsAllocationCubemapFace face, 166 uint32_t z, uint32_t array, size_t *stride) { 167 168 if ((lod >= mHal.drvState.lodCount) || 169 (z && (z >= mHal.drvState.lod[lod].dimZ)) || 170 ((face != RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X) && !mHal.state.hasFaces) || 171 (array != 0)) { 172 return nullptr; 173 } 174 175 size_t s = 0; 176 //void *ptr = mRSC->mHal.funcs.allocation.lock1D(rsc, this); 177 if ((stride != nullptr) && mHal.drvState.lod[0].dimY) { 178 *stride = mHal.drvState.lod[lod].stride; 179 } 180 return mHal.drvState.lod[lod].mallocPtr; 181} 182 183void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod, 184 uint32_t count, const void *data, size_t sizeBytes) { 185 const size_t eSize = mHal.state.type->getElementSizeBytes(); 186 187 if ((count * eSize) != sizeBytes) { 188 char buf[1024]; 189 sprintf(buf, "Allocation::subData called with mismatched size expected %zu, got %zu", 190 (count * eSize), sizeBytes); 191 rsc->setError(RS_ERROR_BAD_VALUE, buf); 192 mHal.state.type->dumpLOGV("type info"); 193 return; 194 } 195 196 rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes); 197 sendDirty(rsc); 198} 199 200void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, 201 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) { 202 rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride); 203 sendDirty(rsc); 204} 205 206void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, 207 uint32_t lod, 208 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) { 209 rsc->mHal.funcs.allocation.data3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride); 210 sendDirty(rsc); 211} 212 213void Allocation::read(Context *rsc, uint32_t xoff, uint32_t lod, 214 uint32_t count, void *data, size_t sizeBytes) { 215 const size_t eSize = mHal.state.type->getElementSizeBytes(); 216 217 if ((count * eSize) != sizeBytes) { 218 char buf[1024]; 219 sprintf(buf, "Allocation::read called with mismatched size expected %zu, got %zu", 220 (count * eSize), sizeBytes); 221 rsc->setError(RS_ERROR_BAD_VALUE, buf); 222 mHal.state.type->dumpLOGV("type info"); 223 return; 224 } 225 226 rsc->mHal.funcs.allocation.read1D(rsc, this, xoff, lod, count, data, sizeBytes); 227} 228 229void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, 230 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) { 231 const size_t eSize = mHal.state.elementSizeBytes; 232 const size_t lineSize = eSize * w; 233 if (!stride) { 234 stride = lineSize; 235 } else { 236 if ((lineSize * h) != sizeBytes) { 237 char buf[1024]; 238 sprintf(buf, "Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes); 239 rsc->setError(RS_ERROR_BAD_VALUE, buf); 240 return; 241 } 242 } 243 244 rsc->mHal.funcs.allocation.read2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride); 245} 246 247void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, 248 uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride) { 249 const size_t eSize = mHal.state.elementSizeBytes; 250 const size_t lineSize = eSize * w; 251 if (!stride) { 252 stride = lineSize; 253 } 254 255 rsc->mHal.funcs.allocation.read3D(rsc, this, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride); 256 257} 258 259void Allocation::elementData(Context *rsc, uint32_t x, const void *data, 260 uint32_t cIdx, size_t sizeBytes) { 261 size_t eSize = mHal.state.elementSizeBytes; 262 263 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) { 264 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range."); 265 return; 266 } 267 268 if (x >= mHal.drvState.lod[0].dimX) { 269 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); 270 return; 271 } 272 273 const Element * e = mHal.state.type->getElement()->getField(cIdx); 274 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx); 275 if (sizeBytes != e->getSizeBytes() * elemArraySize) { 276 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); 277 return; 278 } 279 280 rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes); 281 sendDirty(rsc); 282} 283 284void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y, 285 const void *data, uint32_t cIdx, size_t sizeBytes) { 286 size_t eSize = mHal.state.elementSizeBytes; 287 288 if (x >= mHal.drvState.lod[0].dimX) { 289 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); 290 return; 291 } 292 293 if (y >= mHal.drvState.lod[0].dimY) { 294 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); 295 return; 296 } 297 298 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) { 299 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range."); 300 return; 301 } 302 303 const Element * e = mHal.state.type->getElement()->getField(cIdx); 304 uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx); 305 if (sizeBytes != e->getSizeBytes() * elemArraySize) { 306 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); 307 return; 308 } 309 310 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes); 311 sendDirty(rsc); 312} 313 314void Allocation::addProgramToDirty(const Program *p) { 315 mToDirtyList.push(p); 316} 317 318void Allocation::removeProgramToDirty(const Program *p) { 319 for (size_t ct=0; ct < mToDirtyList.size(); ct++) { 320 if (mToDirtyList[ct] == p) { 321 mToDirtyList.removeAt(ct); 322 return; 323 } 324 } 325 rsAssert(0); 326} 327 328void Allocation::dumpLOGV(const char *prefix) const { 329 ObjectBase::dumpLOGV(prefix); 330 char buf[1024]; 331 332 if ((strlen(prefix) + 10) < sizeof(buf)) { 333 sprintf(buf, "%s type ", prefix); 334 if (mHal.state.type) { 335 mHal.state.type->dumpLOGV(buf); 336 } 337 } 338 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x", 339 prefix, mHal.drvState.lod[0].mallocPtr, mHal.state.usageFlags, mHal.state.mipmapControl); 340} 341 342uint32_t Allocation::getPackedSize() const { 343 uint32_t numItems = mHal.state.type->getCellCount(); 344 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded(); 345} 346 347void Allocation::writePackedData(Context *rsc, const Type *type, 348 uint8_t *dst, const uint8_t *src, bool dstPadded) { 349 const Element *elem = type->getElement(); 350 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded(); 351 uint32_t paddedBytes = elem->getSizeBytes(); 352 uint32_t numItems = type->getPackedSizeBytes() / paddedBytes; 353 354 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes; 355 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes; 356 357 // no sub-elements 358 uint32_t fieldCount = elem->getFieldCount(); 359 if (fieldCount == 0) { 360 for (uint32_t i = 0; i < numItems; i ++) { 361 memcpy(dst, src, unpaddedBytes); 362 src += srcInc; 363 dst += dstInc; 364 } 365 return; 366 } 367 368 // Cache offsets 369 uint32_t *offsetsPadded = new uint32_t[fieldCount]; 370 uint32_t *offsetsUnpadded = new uint32_t[fieldCount]; 371 uint32_t *sizeUnpadded = new uint32_t[fieldCount]; 372 373 for (uint32_t i = 0; i < fieldCount; i++) { 374 offsetsPadded[i] = elem->getFieldOffsetBytes(i); 375 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i); 376 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded(); 377 } 378 379 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded; 380 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded; 381 382 // complex elements, need to copy subelem after subelem 383 for (uint32_t i = 0; i < numItems; i ++) { 384 for (uint32_t fI = 0; fI < fieldCount; fI++) { 385 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]); 386 } 387 src += srcInc; 388 dst += dstInc; 389 } 390 391 delete[] offsetsPadded; 392 delete[] offsetsUnpadded; 393 delete[] sizeUnpadded; 394} 395 396void Allocation::unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize) { 397 const uint8_t *src = (const uint8_t*)data; 398 uint8_t *dst = (uint8_t *)rsc->mHal.funcs.allocation.lock1D(rsc, this); 399 400 writePackedData(rsc, getType(), dst, src, true); 401 rsc->mHal.funcs.allocation.unlock1D(rsc, this); 402} 403 404void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const { 405 uint32_t paddedBytes = getType()->getElement()->getSizeBytes(); 406 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded(); 407 uint32_t numItems = mHal.state.type->getCellCount(); 408 409 const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this); 410 uint8_t *dst = new uint8_t[numItems * unpaddedBytes]; 411 412 writePackedData(rsc, getType(), dst, src, false); 413 stream->addByteArray(dst, getPackedSize()); 414 415 delete[] dst; 416 rsc->mHal.funcs.allocation.unlock1D(rsc, this); 417} 418 419void Allocation::serialize(Context *rsc, OStream *stream) const { 420 // Need to identify ourselves 421 stream->addU32((uint32_t)getClassId()); 422 stream->addString(getName()); 423 424 // First thing we need to serialize is the type object since it will be needed 425 // to initialize the class 426 mHal.state.type->serialize(rsc, stream); 427 428 uint32_t dataSize = mHal.state.type->getPackedSizeBytes(); 429 // 3 element vectors are padded to 4 in memory, but padding isn't serialized 430 uint32_t packedSize = getPackedSize(); 431 // Write how much data we are storing 432 stream->addU32(packedSize); 433 if (dataSize == packedSize) { 434 // Now write the data 435 stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize); 436 rsc->mHal.funcs.allocation.unlock1D(rsc, this); 437 } else { 438 // Now write the data 439 packVec3Allocation(rsc, stream); 440 } 441} 442 443Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) { 444 // First make sure we are reading the correct object 445 RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); 446 if (classID != RS_A3D_CLASS_ID_ALLOCATION) { 447 rsc->setError(RS_ERROR_FATAL_DRIVER, 448 "allocation loading failed due to corrupt file. (invalid id)\n"); 449 return nullptr; 450 } 451 452 const char *name = stream->loadString(); 453 454 Type *type = Type::createFromStream(rsc, stream); 455 if (!type) { 456 return nullptr; 457 } 458 type->compute(); 459 460 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT); 461 type->decUserRef(); 462 463 // Number of bytes we wrote out for this allocation 464 uint32_t dataSize = stream->loadU32(); 465 // 3 element vectors are padded to 4 in memory, but padding isn't serialized 466 uint32_t packedSize = alloc->getPackedSize(); 467 if (dataSize != type->getPackedSizeBytes() && 468 dataSize != packedSize) { 469 rsc->setError(RS_ERROR_FATAL_DRIVER, 470 "allocation loading failed due to corrupt file. (invalid size)\n"); 471 ObjectBase::checkDelete(alloc); 472 ObjectBase::checkDelete(type); 473 return nullptr; 474 } 475 476 alloc->assignName(name); 477 if (dataSize == type->getPackedSizeBytes()) { 478 uint32_t count = dataSize / type->getElementSizeBytes(); 479 // Read in all of our allocation data 480 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize); 481 } else { 482 alloc->unpackVec3Allocation(rsc, stream->getPtr() + stream->getPos(), dataSize); 483 } 484 stream->reset(stream->getPos() + dataSize); 485 486 return alloc; 487} 488 489void Allocation::sendDirty(const Context *rsc) const { 490#ifndef RS_COMPATIBILITY_LIB 491 for (size_t ct=0; ct < mToDirtyList.size(); ct++) { 492 mToDirtyList[ct]->forceDirty(); 493 } 494#endif 495 mRSC->mHal.funcs.allocation.markDirty(rsc, this); 496} 497 498void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const { 499 mHal.state.type->incRefs(ptr, ct, startOff); 500} 501 502void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const { 503 if (!mHal.state.hasReferences || !getIsScript()) { 504 return; 505 } 506 mHal.state.type->decRefs(ptr, ct, startOff); 507} 508 509void Allocation::callUpdateCacheObject(const Context *rsc, void *dstObj) const { 510 if (rsc->mHal.funcs.allocation.updateCachedObject != nullptr) { 511 rsc->mHal.funcs.allocation.updateCachedObject(rsc, this, (rs_allocation *)dstObj); 512 } else { 513 *((const void **)dstObj) = this; 514 } 515} 516 517 518void Allocation::freeChildrenUnlocked () { 519 void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this); 520 decRefs(ptr, mHal.state.type->getCellCount(), 0); 521 mRSC->mHal.funcs.allocation.unlock1D(mRSC, this); 522} 523 524bool Allocation::freeChildren() { 525 if (mHal.state.hasReferences) { 526 incSysRef(); 527 freeChildrenUnlocked(); 528 return decSysRef(); 529 } 530 return false; 531} 532 533void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) { 534} 535 536void Allocation::resize1D(Context *rsc, uint32_t dimX) { 537 uint32_t oldDimX = mHal.drvState.lod[0].dimX; 538 if (dimX == oldDimX) { 539 return; 540 } 541 542 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX); 543 if (dimX < oldDimX) { 544 decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX); 545 rsc->mHal.funcs.allocation.unlock1D(rsc, this); 546 } 547 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences); 548 setType(t.get()); 549 updateCache(); 550} 551 552void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) { 553 rsc->setError(RS_ERROR_FATAL_DRIVER, "resize2d not implemented"); 554} 555 556#ifndef RS_COMPATIBILITY_LIB 557void Allocation::NewBufferListener::onFrameAvailable() { 558 intptr_t ip = (intptr_t)alloc; 559 rsc->sendMessageToClient(&ip, RS_MESSAGE_TO_CLIENT_NEW_BUFFER, 0, sizeof(ip), true); 560} 561#endif 562 563void * Allocation::getSurface(const Context *rsc) { 564#ifndef RS_COMPATIBILITY_LIB 565 // Configure GrallocConsumer to be in asynchronous mode 566 sp<IGraphicBufferProducer> bp; 567 sp<IGraphicBufferConsumer> bc; 568 BufferQueue::createBufferQueue(&bp, &bc); 569 mGrallocConsumer = new GrallocConsumer(this, bc, mHal.drvState.grallocFlags); 570 bp->incStrong(nullptr); 571 572 mBufferListener = new NewBufferListener(); 573 mBufferListener->rsc = rsc; 574 mBufferListener->alloc = this; 575 576 mGrallocConsumer->setFrameAvailableListener(mBufferListener); 577 return bp.get(); 578#else 579 return nullptr; 580#endif 581 //return rsc->mHal.funcs.allocation.getSurface(rsc, this); 582} 583 584void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) { 585 ANativeWindow *nw = (ANativeWindow *)sur; 586 rsc->mHal.funcs.allocation.setSurface(rsc, this, nw); 587} 588 589void Allocation::ioSend(const Context *rsc) { 590 rsc->mHal.funcs.allocation.ioSend(rsc, this); 591} 592 593void Allocation::ioReceive(const Context *rsc) { 594 void *ptr = nullptr; 595 size_t stride = 0; 596#ifndef RS_COMPATIBILITY_LIB 597 if (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) { 598 status_t ret = mGrallocConsumer->lockNextBuffer(); 599 600 if (ret == OK) { 601 rsc->mHal.funcs.allocation.ioReceive(rsc, this); 602 } else if (ret == BAD_VALUE) { 603 // No new frame, don't do anything 604 } else { 605 rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer."); 606 } 607 608 } 609#endif 610} 611 612bool Allocation::hasSameDims(const Allocation *other) const { 613 const Type *type0 = this->getType(), 614 *type1 = other->getType(); 615 616 return (type0->getCellCount() == type1->getCellCount()) && 617 (type0->getDimLOD() == type1->getDimLOD()) && 618 (type0->getDimFaces() == type1->getDimFaces()) && 619 (type0->getDimYuv() == type1->getDimYuv()) && 620 (type0->getDimX() == type1->getDimX()) && 621 (type0->getDimY() == type1->getDimY()) && 622 (type0->getDimZ() == type1->getDimZ()); 623} 624 625 626///////////////// 627// 628 629namespace android { 630namespace renderscript { 631 632void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) { 633 Allocation *a = static_cast<Allocation *>(va); 634 a->sendDirty(rsc); 635 a->syncAll(rsc, src); 636} 637 638void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) { 639 Allocation *alloc = static_cast<Allocation *>(va); 640 rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc); 641} 642 643void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) { 644 Allocation *a = static_cast<Allocation *>(va); 645 const Type * t = a->getType(); 646 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 647 t->getDimX(), t->getDimY(), data, sizeBytes, 0); 648} 649 650void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod, 651 uint32_t count, const void *data, size_t sizeBytes) { 652 Allocation *a = static_cast<Allocation *>(va); 653 a->data(rsc, xoff, lod, count, data, sizeBytes); 654} 655 656void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face, 657 const void *data, size_t sizeBytes, size_t eoff) { 658 Allocation *a = static_cast<Allocation *>(va); 659 a->elementData(rsc, x, y, data, eoff, sizeBytes); 660} 661 662void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod, 663 const void *data, size_t sizeBytes, size_t eoff) { 664 Allocation *a = static_cast<Allocation *>(va); 665 a->elementData(rsc, x, data, eoff, sizeBytes); 666} 667 668void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, 669 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) { 670 Allocation *a = static_cast<Allocation *>(va); 671 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride); 672} 673 674void rsi_Allocation3DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, 675 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) { 676 Allocation *a = static_cast<Allocation *>(va); 677 a->data(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride); 678} 679 680 681void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) { 682 Allocation *a = static_cast<Allocation *>(va); 683 const Type * t = a->getType(); 684 if(t->getDimY()) { 685 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 686 t->getDimX(), t->getDimY(), data, sizeBytes, 0); 687 } else { 688 a->read(rsc, 0, 0, t->getDimX(), data, sizeBytes); 689 } 690 691} 692 693void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) { 694 Allocation *a = static_cast<Allocation *>(va); 695 a->resize1D(rsc, dimX); 696} 697 698void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) { 699 Allocation *a = static_cast<Allocation *>(va); 700 a->resize2D(rsc, dimX, dimY); 701} 702 703RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype, 704 RsAllocationMipmapControl mipmaps, 705 uint32_t usages, uintptr_t ptr) { 706 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mipmaps, (void*)ptr); 707 if (!alloc) { 708 return nullptr; 709 } 710 alloc->incUserRef(); 711 return alloc; 712} 713 714RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype, 715 RsAllocationMipmapControl mipmaps, 716 const void *data, size_t sizeBytes, uint32_t usages) { 717 Type *t = static_cast<Type *>(vtype); 718 719 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0); 720 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); 721 if (texAlloc == nullptr) { 722 ALOGE("Memory allocation failure"); 723 return nullptr; 724 } 725 726 texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 727 t->getDimX(), t->getDimY(), data, sizeBytes, 0); 728 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) { 729 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc); 730 } 731 732 texAlloc->sendDirty(rsc); 733 return texAlloc; 734} 735 736RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype, 737 RsAllocationMipmapControl mipmaps, 738 const void *data, size_t sizeBytes, uint32_t usages) { 739 Type *t = static_cast<Type *>(vtype); 740 741 // Cubemap allocation's faces should be Width by Width each. 742 // Source data should have 6 * Width by Width pixels 743 // Error checking is done in the java layer 744 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0); 745 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); 746 if (texAlloc == nullptr) { 747 ALOGE("Memory allocation failure"); 748 return nullptr; 749 } 750 751 uint32_t faceSize = t->getDimX(); 752 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes(); 753 uint32_t copySize = faceSize * t->getElementSizeBytes(); 754 755 uint8_t *sourcePtr = (uint8_t*)data; 756 for (uint32_t face = 0; face < 6; face ++) { 757 for (uint32_t dI = 0; dI < faceSize; dI ++) { 758 texAlloc->data(rsc, 0, dI, 0, (RsAllocationCubemapFace)face, 759 t->getDimX(), 1, sourcePtr + strideBytes * dI, copySize, 0); 760 } 761 762 // Move the data pointer to the next cube face 763 sourcePtr += copySize; 764 } 765 766 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) { 767 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc); 768 } 769 770 texAlloc->sendDirty(rsc); 771 return texAlloc; 772} 773 774void rsi_AllocationCopy2DRange(Context *rsc, 775 RsAllocation dstAlloc, 776 uint32_t dstXoff, uint32_t dstYoff, 777 uint32_t dstMip, uint32_t dstFace, 778 uint32_t width, uint32_t height, 779 RsAllocation srcAlloc, 780 uint32_t srcXoff, uint32_t srcYoff, 781 uint32_t srcMip, uint32_t srcFace) { 782 Allocation *dst = static_cast<Allocation *>(dstAlloc); 783 Allocation *src= static_cast<Allocation *>(srcAlloc); 784 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip, 785 (RsAllocationCubemapFace)dstFace, 786 width, height, 787 src, srcXoff, srcYoff,srcMip, 788 (RsAllocationCubemapFace)srcFace); 789} 790 791void rsi_AllocationCopy3DRange(Context *rsc, 792 RsAllocation dstAlloc, 793 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff, 794 uint32_t dstMip, 795 uint32_t width, uint32_t height, uint32_t depth, 796 RsAllocation srcAlloc, 797 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff, 798 uint32_t srcMip) { 799 Allocation *dst = static_cast<Allocation *>(dstAlloc); 800 Allocation *src= static_cast<Allocation *>(srcAlloc); 801 rsc->mHal.funcs.allocation.allocData3D(rsc, dst, dstXoff, dstYoff, dstZoff, dstMip, 802 width, height, depth, 803 src, srcXoff, srcYoff, srcZoff, srcMip); 804} 805 806 807void * rsi_AllocationGetSurface(Context *rsc, RsAllocation valloc) { 808 Allocation *alloc = static_cast<Allocation *>(valloc); 809 void *s = alloc->getSurface(rsc); 810 return s; 811} 812 813void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) { 814 Allocation *alloc = static_cast<Allocation *>(valloc); 815 alloc->setSurface(rsc, sur); 816} 817 818void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) { 819 Allocation *alloc = static_cast<Allocation *>(valloc); 820 alloc->ioSend(rsc); 821} 822 823void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) { 824 Allocation *alloc = static_cast<Allocation *>(valloc); 825 alloc->ioReceive(rsc); 826} 827 828void *rsi_AllocationGetPointer(Context *rsc, RsAllocation valloc, 829 uint32_t lod, RsAllocationCubemapFace face, 830 uint32_t z, uint32_t array, size_t *stride, size_t strideLen) { 831 Allocation *alloc = static_cast<Allocation *>(valloc); 832 rsAssert(strideLen == sizeof(size_t)); 833 834 return alloc->getPointer(rsc, lod, face, z, array, stride); 835} 836 837void rsi_Allocation1DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod, 838 uint32_t count, void *data, size_t sizeBytes) { 839 Allocation *a = static_cast<Allocation *>(va); 840 rsc->mHal.funcs.allocation.read1D(rsc, a, xoff, lod, count, data, sizeBytes); 841} 842 843void rsi_Allocation2DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, 844 uint32_t lod, RsAllocationCubemapFace face, uint32_t w, 845 uint32_t h, void *data, size_t sizeBytes, size_t stride) { 846 Allocation *a = static_cast<Allocation *>(va); 847 a->read(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride); 848} 849 850RsAllocation rsi_AllocationAdapterCreate(Context *rsc, RsType vwindow, RsAllocation vbase) { 851 852 853 Allocation * alloc = Allocation::createAdapter(rsc, 854 static_cast<Allocation *>(vbase), static_cast<Type *>(vwindow)); 855 if (!alloc) { 856 return nullptr; 857 } 858 alloc->incUserRef(); 859 return alloc; 860} 861 862void rsi_AllocationAdapterOffset(Context *rsc, RsAllocation va, const uint32_t *offsets, size_t len) { 863} 864 865 866} 867} 868 869extern "C" const void * rsaAllocationGetType(RsContext con, RsAllocation va) { 870 Allocation *a = static_cast<Allocation *>(va); 871 a->getType()->incUserRef(); 872 873 return a->getType(); 874} 875