rsAllocation.cpp revision cfea6c13075aa255712e5a09a54eccbc84b0b122
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, 295 "subElementData X offset out of range."); 296 return; 297 } 298 299 if (cIdx >= mHal.state.type->getElement()->getFieldCount()) { 300 rsc->setError(RS_ERROR_BAD_VALUE, 301 "subElementData component out of range."); 302 return; 303 } 304 305 const Element * e = mHal.state.type->getElement()->getField(cIdx); 306 uint32_t elemArraySize = 307 mHal.state.type->getElement()->getFieldArraySize(cIdx); 308 if (sizeBytes != e->getSizeBytes() * elemArraySize) { 309 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); 310 return; 311 } 312 313 rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, 314 sizeBytes); 315 sendDirty(rsc); 316} 317 318void Allocation::addProgramToDirty(const Program *p) { 319 mToDirtyList.push_back(p); 320} 321 322void Allocation::removeProgramToDirty(const Program *p) { 323 for (auto entryIter = mToDirtyList.begin(), endIter = mToDirtyList.end(); 324 entryIter != endIter; entryIter++) { 325 326 if (p == *entryIter) { 327 mToDirtyList.erase(entryIter); 328 return; 329 } 330 } 331 rsAssert(0); 332} 333 334void Allocation::dumpLOGV(const char *prefix) const { 335 ObjectBase::dumpLOGV(prefix); 336 char buf[1024]; 337 338 if ((strlen(prefix) + 10) < sizeof(buf)) { 339 sprintf(buf, "%s type ", prefix); 340 if (mHal.state.type) { 341 mHal.state.type->dumpLOGV(buf); 342 } 343 } 344 ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x", 345 prefix, mHal.drvState.lod[0].mallocPtr, mHal.state.usageFlags, 346 mHal.state.mipmapControl); 347} 348 349uint32_t Allocation::getPackedSize() const { 350 uint32_t numItems = mHal.state.type->getCellCount(); 351 return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded(); 352} 353 354void Allocation::writePackedData(Context *rsc, const Type *type, 355 uint8_t *dst, const uint8_t *src, bool dstPadded) { 356 const Element *elem = type->getElement(); 357 uint32_t unpaddedBytes = elem->getSizeBytesUnpadded(); 358 uint32_t paddedBytes = elem->getSizeBytes(); 359 uint32_t numItems = type->getPackedSizeBytes() / paddedBytes; 360 361 uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes; 362 uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes; 363 364 // no sub-elements 365 uint32_t fieldCount = elem->getFieldCount(); 366 if (fieldCount == 0) { 367 for (uint32_t i = 0; i < numItems; i ++) { 368 memcpy(dst, src, unpaddedBytes); 369 src += srcInc; 370 dst += dstInc; 371 } 372 return; 373 } 374 375 // Cache offsets 376 uint32_t *offsetsPadded = new uint32_t[fieldCount]; 377 uint32_t *offsetsUnpadded = new uint32_t[fieldCount]; 378 uint32_t *sizeUnpadded = new uint32_t[fieldCount]; 379 380 for (uint32_t i = 0; i < fieldCount; i++) { 381 offsetsPadded[i] = elem->getFieldOffsetBytes(i); 382 offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i); 383 sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded(); 384 } 385 386 uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded; 387 uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded; 388 389 // complex elements, need to copy subelem after subelem 390 for (uint32_t i = 0; i < numItems; i ++) { 391 for (uint32_t fI = 0; fI < fieldCount; fI++) { 392 memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]); 393 } 394 src += srcInc; 395 dst += dstInc; 396 } 397 398 delete[] offsetsPadded; 399 delete[] offsetsUnpadded; 400 delete[] sizeUnpadded; 401} 402 403void Allocation::unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize) { 404 const uint8_t *src = (const uint8_t*)data; 405 uint8_t *dst = (uint8_t *)rsc->mHal.funcs.allocation.lock1D(rsc, this); 406 407 writePackedData(rsc, getType(), dst, src, true); 408 rsc->mHal.funcs.allocation.unlock1D(rsc, this); 409} 410 411void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const { 412 uint32_t paddedBytes = getType()->getElement()->getSizeBytes(); 413 uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded(); 414 uint32_t numItems = mHal.state.type->getCellCount(); 415 416 const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this); 417 uint8_t *dst = new uint8_t[numItems * unpaddedBytes]; 418 419 writePackedData(rsc, getType(), dst, src, false); 420 stream->addByteArray(dst, getPackedSize()); 421 422 delete[] dst; 423 rsc->mHal.funcs.allocation.unlock1D(rsc, this); 424} 425 426void Allocation::serialize(Context *rsc, OStream *stream) const { 427 // Need to identify ourselves 428 stream->addU32((uint32_t)getClassId()); 429 stream->addString(getName()); 430 431 // First thing we need to serialize is the type object since it will be needed 432 // to initialize the class 433 mHal.state.type->serialize(rsc, stream); 434 435 uint32_t dataSize = mHal.state.type->getPackedSizeBytes(); 436 // 3 element vectors are padded to 4 in memory, but padding isn't serialized 437 uint32_t packedSize = getPackedSize(); 438 // Write how much data we are storing 439 stream->addU32(packedSize); 440 if (dataSize == packedSize) { 441 // Now write the data 442 stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize); 443 rsc->mHal.funcs.allocation.unlock1D(rsc, this); 444 } else { 445 // Now write the data 446 packVec3Allocation(rsc, stream); 447 } 448} 449 450Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) { 451 // First make sure we are reading the correct object 452 RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); 453 if (classID != RS_A3D_CLASS_ID_ALLOCATION) { 454 rsc->setError(RS_ERROR_FATAL_DRIVER, 455 "allocation loading failed due to corrupt file. (invalid id)\n"); 456 return nullptr; 457 } 458 459 const char *name = stream->loadString(); 460 461 Type *type = Type::createFromStream(rsc, stream); 462 if (!type) { 463 return nullptr; 464 } 465 type->compute(); 466 467 Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT); 468 type->decUserRef(); 469 470 // Number of bytes we wrote out for this allocation 471 uint32_t dataSize = stream->loadU32(); 472 // 3 element vectors are padded to 4 in memory, but padding isn't serialized 473 uint32_t packedSize = alloc->getPackedSize(); 474 if (dataSize != type->getPackedSizeBytes() && 475 dataSize != packedSize) { 476 rsc->setError(RS_ERROR_FATAL_DRIVER, 477 "allocation loading failed due to corrupt file. (invalid size)\n"); 478 ObjectBase::checkDelete(alloc); 479 ObjectBase::checkDelete(type); 480 return nullptr; 481 } 482 483 alloc->assignName(name); 484 if (dataSize == type->getPackedSizeBytes()) { 485 uint32_t count = dataSize / type->getElementSizeBytes(); 486 // Read in all of our allocation data 487 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize); 488 } else { 489 alloc->unpackVec3Allocation(rsc, stream->getPtr() + stream->getPos(), dataSize); 490 } 491 stream->reset(stream->getPos() + dataSize); 492 493 return alloc; 494} 495 496void Allocation::sendDirty(const Context *rsc) const { 497#ifndef RS_COMPATIBILITY_LIB 498 for (size_t ct=0; ct < mToDirtyList.size(); ct++) { 499 mToDirtyList[ct]->forceDirty(); 500 } 501#endif 502 mRSC->mHal.funcs.allocation.markDirty(rsc, this); 503} 504 505void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const { 506 mHal.state.type->incRefs(ptr, ct, startOff); 507} 508 509void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const { 510 if (!mHal.state.hasReferences || !getIsScript()) { 511 return; 512 } 513 mHal.state.type->decRefs(ptr, ct, startOff); 514} 515 516void Allocation::callUpdateCacheObject(const Context *rsc, void *dstObj) const { 517 if (rsc->mHal.funcs.allocation.updateCachedObject != nullptr) { 518 rsc->mHal.funcs.allocation.updateCachedObject(rsc, this, (rs_allocation *)dstObj); 519 } else { 520 *((const void **)dstObj) = this; 521 } 522} 523 524 525void Allocation::freeChildrenUnlocked () { 526 void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this); 527 decRefs(ptr, mHal.state.type->getCellCount(), 0); 528 mRSC->mHal.funcs.allocation.unlock1D(mRSC, this); 529} 530 531bool Allocation::freeChildren() { 532 if (mHal.state.hasReferences) { 533 incSysRef(); 534 freeChildrenUnlocked(); 535 return decSysRef(); 536 } 537 return false; 538} 539 540void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) { 541} 542 543void Allocation::resize1D(Context *rsc, uint32_t dimX) { 544 uint32_t oldDimX = mHal.drvState.lod[0].dimX; 545 if (dimX == oldDimX) { 546 return; 547 } 548 549 ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX); 550 if (dimX < oldDimX) { 551 decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX); 552 rsc->mHal.funcs.allocation.unlock1D(rsc, this); 553 } 554 rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences); 555 setType(t.get()); 556 updateCache(); 557} 558 559void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) { 560 rsc->setError(RS_ERROR_FATAL_DRIVER, "resize2d not implemented"); 561} 562 563#ifndef RS_COMPATIBILITY_LIB 564void Allocation::NewBufferListener::onFrameAvailable(const BufferItem& /* item */) { 565 intptr_t ip = (intptr_t)alloc; 566 rsc->sendMessageToClient(&ip, RS_MESSAGE_TO_CLIENT_NEW_BUFFER, 0, sizeof(ip), true); 567} 568#endif 569 570void * Allocation::getSurface(const Context *rsc) { 571#ifndef RS_COMPATIBILITY_LIB 572 // Configure GrallocConsumer to be in asynchronous mode 573 sp<IGraphicBufferProducer> bp; 574 sp<IGraphicBufferConsumer> bc; 575 BufferQueue::createBufferQueue(&bp, &bc); 576 mGrallocConsumer = new GrallocConsumer(this, bc, mHal.drvState.grallocFlags); 577 bp->incStrong(nullptr); 578 579 mBufferListener = new NewBufferListener(); 580 mBufferListener->rsc = rsc; 581 mBufferListener->alloc = this; 582 583 mGrallocConsumer->setFrameAvailableListener(mBufferListener); 584 return bp.get(); 585#else 586 return nullptr; 587#endif 588 //return rsc->mHal.funcs.allocation.getSurface(rsc, this); 589} 590 591void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) { 592 ANativeWindow *nw = (ANativeWindow *)sur; 593 rsc->mHal.funcs.allocation.setSurface(rsc, this, nw); 594} 595 596void Allocation::ioSend(const Context *rsc) { 597 rsc->mHal.funcs.allocation.ioSend(rsc, this); 598} 599 600void Allocation::ioReceive(const Context *rsc) { 601 void *ptr = nullptr; 602 size_t stride = 0; 603#ifndef RS_COMPATIBILITY_LIB 604 if (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) { 605 status_t ret = mGrallocConsumer->lockNextBuffer(); 606 607 if (ret == OK) { 608 rsc->mHal.funcs.allocation.ioReceive(rsc, this); 609 } else if (ret == BAD_VALUE) { 610 // No new frame, don't do anything 611 } else { 612 rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer."); 613 } 614 615 } 616#endif 617} 618 619bool Allocation::hasSameDims(const Allocation *other) const { 620 const Type *type0 = this->getType(), 621 *type1 = other->getType(); 622 623 return (type0->getCellCount() == type1->getCellCount()) && 624 (type0->getDimLOD() == type1->getDimLOD()) && 625 (type0->getDimFaces() == type1->getDimFaces()) && 626 (type0->getDimYuv() == type1->getDimYuv()) && 627 (type0->getDimX() == type1->getDimX()) && 628 (type0->getDimY() == type1->getDimY()) && 629 (type0->getDimZ() == type1->getDimZ()); 630} 631 632 633///////////////// 634// 635 636namespace android { 637namespace renderscript { 638 639void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) { 640 Allocation *a = static_cast<Allocation *>(va); 641 a->sendDirty(rsc); 642 a->syncAll(rsc, src); 643} 644 645void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) { 646 Allocation *alloc = static_cast<Allocation *>(va); 647 rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc); 648} 649 650void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) { 651 Allocation *a = static_cast<Allocation *>(va); 652 const Type * t = a->getType(); 653 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 654 t->getDimX(), t->getDimY(), data, sizeBytes, 0); 655} 656 657void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod, 658 uint32_t count, const void *data, size_t sizeBytes) { 659 Allocation *a = static_cast<Allocation *>(va); 660 a->data(rsc, xoff, lod, count, data, sizeBytes); 661} 662 663void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face, 664 const void *data, size_t sizeBytes, size_t eoff) { 665 Allocation *a = static_cast<Allocation *>(va); 666 a->elementData(rsc, x, y, data, eoff, sizeBytes); 667} 668 669void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod, 670 const void *data, size_t sizeBytes, size_t eoff) { 671 Allocation *a = static_cast<Allocation *>(va); 672 a->elementData(rsc, x, data, eoff, sizeBytes); 673} 674 675void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, 676 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) { 677 Allocation *a = static_cast<Allocation *>(va); 678 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride); 679} 680 681void rsi_Allocation3DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, 682 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride) { 683 Allocation *a = static_cast<Allocation *>(va); 684 a->data(rsc, xoff, yoff, zoff, lod, w, h, d, data, sizeBytes, stride); 685} 686 687 688void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) { 689 Allocation *a = static_cast<Allocation *>(va); 690 const Type * t = a->getType(); 691 if(t->getDimY()) { 692 a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 693 t->getDimX(), t->getDimY(), data, sizeBytes, 0); 694 } else { 695 a->read(rsc, 0, 0, t->getDimX(), data, sizeBytes); 696 } 697 698} 699 700void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) { 701 Allocation *a = static_cast<Allocation *>(va); 702 a->resize1D(rsc, dimX); 703} 704 705void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) { 706 Allocation *a = static_cast<Allocation *>(va); 707 a->resize2D(rsc, dimX, dimY); 708} 709 710RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype, 711 RsAllocationMipmapControl mipmaps, 712 uint32_t usages, uintptr_t ptr) { 713 Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mipmaps, (void*)ptr); 714 if (!alloc) { 715 return nullptr; 716 } 717 alloc->incUserRef(); 718 return alloc; 719} 720 721RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype, 722 RsAllocationMipmapControl mipmaps, 723 const void *data, size_t sizeBytes, uint32_t usages) { 724 Type *t = static_cast<Type *>(vtype); 725 726 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0); 727 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); 728 if (texAlloc == nullptr) { 729 ALOGE("Memory allocation failure"); 730 return nullptr; 731 } 732 733 texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 734 t->getDimX(), t->getDimY(), data, sizeBytes, 0); 735 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) { 736 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc); 737 } 738 739 texAlloc->sendDirty(rsc); 740 return texAlloc; 741} 742 743RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype, 744 RsAllocationMipmapControl mipmaps, 745 const void *data, size_t sizeBytes, uint32_t usages) { 746 Type *t = static_cast<Type *>(vtype); 747 748 // Cubemap allocation's faces should be Width by Width each. 749 // Source data should have 6 * Width by Width pixels 750 // Error checking is done in the java layer 751 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mipmaps, usages, 0); 752 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); 753 if (texAlloc == nullptr) { 754 ALOGE("Memory allocation failure"); 755 return nullptr; 756 } 757 758 uint32_t faceSize = t->getDimX(); 759 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes(); 760 uint32_t copySize = faceSize * t->getElementSizeBytes(); 761 762 uint8_t *sourcePtr = (uint8_t*)data; 763 for (uint32_t face = 0; face < 6; face ++) { 764 for (uint32_t dI = 0; dI < faceSize; dI ++) { 765 texAlloc->data(rsc, 0, dI, 0, (RsAllocationCubemapFace)face, 766 t->getDimX(), 1, sourcePtr + strideBytes * dI, copySize, 0); 767 } 768 769 // Move the data pointer to the next cube face 770 sourcePtr += copySize; 771 } 772 773 if (mipmaps == RS_ALLOCATION_MIPMAP_FULL) { 774 rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc); 775 } 776 777 texAlloc->sendDirty(rsc); 778 return texAlloc; 779} 780 781void rsi_AllocationCopy2DRange(Context *rsc, 782 RsAllocation dstAlloc, 783 uint32_t dstXoff, uint32_t dstYoff, 784 uint32_t dstMip, uint32_t dstFace, 785 uint32_t width, uint32_t height, 786 RsAllocation srcAlloc, 787 uint32_t srcXoff, uint32_t srcYoff, 788 uint32_t srcMip, uint32_t srcFace) { 789 Allocation *dst = static_cast<Allocation *>(dstAlloc); 790 Allocation *src= static_cast<Allocation *>(srcAlloc); 791 rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip, 792 (RsAllocationCubemapFace)dstFace, 793 width, height, 794 src, srcXoff, srcYoff,srcMip, 795 (RsAllocationCubemapFace)srcFace); 796} 797 798void rsi_AllocationCopy3DRange(Context *rsc, 799 RsAllocation dstAlloc, 800 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff, 801 uint32_t dstMip, 802 uint32_t width, uint32_t height, uint32_t depth, 803 RsAllocation srcAlloc, 804 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff, 805 uint32_t srcMip) { 806 Allocation *dst = static_cast<Allocation *>(dstAlloc); 807 Allocation *src= static_cast<Allocation *>(srcAlloc); 808 rsc->mHal.funcs.allocation.allocData3D(rsc, dst, dstXoff, dstYoff, dstZoff, dstMip, 809 width, height, depth, 810 src, srcXoff, srcYoff, srcZoff, srcMip); 811} 812 813 814void * rsi_AllocationGetSurface(Context *rsc, RsAllocation valloc) { 815 Allocation *alloc = static_cast<Allocation *>(valloc); 816 void *s = alloc->getSurface(rsc); 817 return s; 818} 819 820void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) { 821 Allocation *alloc = static_cast<Allocation *>(valloc); 822 alloc->setSurface(rsc, sur); 823} 824 825void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) { 826 Allocation *alloc = static_cast<Allocation *>(valloc); 827 alloc->ioSend(rsc); 828} 829 830void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) { 831 Allocation *alloc = static_cast<Allocation *>(valloc); 832 alloc->ioReceive(rsc); 833} 834 835void *rsi_AllocationGetPointer(Context *rsc, RsAllocation valloc, 836 uint32_t lod, RsAllocationCubemapFace face, 837 uint32_t z, uint32_t array, size_t *stride, size_t strideLen) { 838 Allocation *alloc = static_cast<Allocation *>(valloc); 839 rsAssert(strideLen == sizeof(size_t)); 840 841 return alloc->getPointer(rsc, lod, face, z, array, stride); 842} 843 844void rsi_Allocation1DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod, 845 uint32_t count, void *data, size_t sizeBytes) { 846 Allocation *a = static_cast<Allocation *>(va); 847 rsc->mHal.funcs.allocation.read1D(rsc, a, xoff, lod, count, data, sizeBytes); 848} 849 850void rsi_Allocation2DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, 851 uint32_t lod, RsAllocationCubemapFace face, uint32_t w, 852 uint32_t h, void *data, size_t sizeBytes, size_t stride) { 853 Allocation *a = static_cast<Allocation *>(va); 854 a->read(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride); 855} 856 857RsAllocation rsi_AllocationAdapterCreate(Context *rsc, RsType vwindow, RsAllocation vbase) { 858 859 860 Allocation * alloc = Allocation::createAdapter(rsc, 861 static_cast<Allocation *>(vbase), static_cast<Type *>(vwindow)); 862 if (!alloc) { 863 return nullptr; 864 } 865 alloc->incUserRef(); 866 return alloc; 867} 868 869void rsi_AllocationAdapterOffset(Context *rsc, RsAllocation va, const uint32_t *offsets, size_t len) { 870} 871 872 873} 874} 875 876extern "C" const void * rsaAllocationGetType(RsContext con, RsAllocation va) { 877 Allocation *a = static_cast<Allocation *>(va); 878 a->getType()->incUserRef(); 879 880 return a->getType(); 881} 882