rsAllocation.cpp revision afb743aca56c18beb7ab924e75cb6e070ef3e55a
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#ifndef ANDROID_RS_BUILD_FOR_HOST 17#include "rsContext.h" 18 19#include <GLES/gl.h> 20#include <GLES2/gl2.h> 21#include <GLES/glext.h> 22#else 23#include "rsContextHostStub.h" 24 25#include <OpenGL/gl.h> 26#include <OpenGl/glext.h> 27#endif 28 29#include "utils/StopWatch.h" 30 31using namespace android; 32using namespace android::renderscript; 33 34Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc) { 35 init(rsc, type); 36 37 mPtr = malloc(mType->getSizeBytes()); 38 if (mType->getElement()->getHasReferences()) { 39 memset(mPtr, 0, mType->getSizeBytes()); 40 } 41 if (!mPtr) { 42 LOGE("Allocation::Allocation, alloc failure"); 43 } 44} 45 46Allocation::Allocation(Context *rsc, const Type *type, void *bmp, 47 void *callbackData, RsBitmapCallback_t callback) 48 : ObjectBase(rsc) { 49 init(rsc, type); 50 51 mPtr = bmp; 52 mUserBitmapCallback = callback; 53 mUserBitmapCallbackData = callbackData; 54} 55 56void Allocation::init(Context *rsc, const Type *type) { 57 mPtr = NULL; 58 59 mCpuWrite = false; 60 mCpuRead = false; 61 mGpuWrite = false; 62 mGpuRead = false; 63 64 mReadWriteRatio = 0; 65 mUpdateSize = 0; 66 67 mIsTexture = false; 68 mTextureID = 0; 69 mIsVertexBuffer = false; 70 mBufferID = 0; 71 mUploadDefered = false; 72 73 mUserBitmapCallback = NULL; 74 mUserBitmapCallbackData = NULL; 75 76 mType.set(type); 77 rsAssert(type); 78 79 mPtr = NULL; 80} 81 82Allocation::~Allocation() { 83 if (mUserBitmapCallback != NULL) { 84 mUserBitmapCallback(mUserBitmapCallbackData); 85 } else { 86 free(mPtr); 87 } 88 mPtr = NULL; 89 90 if (mBufferID) { 91 // Causes a SW crash.... 92 //LOGV(" mBufferID %i", mBufferID); 93 //glDeleteBuffers(1, &mBufferID); 94 //mBufferID = 0; 95 } 96 if (mTextureID) { 97 glDeleteTextures(1, &mTextureID); 98 mTextureID = 0; 99 } 100} 101 102void Allocation::setCpuWritable(bool) { 103} 104 105void Allocation::setGpuWritable(bool) { 106} 107 108void Allocation::setCpuReadable(bool) { 109} 110 111void Allocation::setGpuReadable(bool) { 112} 113 114bool Allocation::fixAllocation() { 115 return false; 116} 117 118void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset) { 119 rsAssert(lodOffset < mType->getLODCount()); 120 mIsTexture = true; 121 mTextureLOD = lodOffset; 122 mUploadDefered = true; 123 mTextureGenMipmap = !mType->getDimLOD() && genMipmap; 124} 125 126void Allocation::uploadToTexture(const Context *rsc) { 127 //rsAssert(!mTextureId); 128 129 mIsTexture = true; 130 if (!rsc->checkDriver()) { 131 mUploadDefered = true; 132 return; 133 } 134 135 GLenum type = mType->getElement()->getComponent().getGLType(); 136 GLenum format = mType->getElement()->getComponent().getGLFormat(); 137 138 if (!type || !format) { 139 return; 140 } 141 142 bool isFirstUpload = false; 143 144 if (!mTextureID) { 145 glGenTextures(1, &mTextureID); 146 147 if (!mTextureID) { 148 // This should not happen, however, its likely the cause of the 149 // white sqare bug. 150 // Force a crash to 1: restart the app, 2: make sure we get a bugreport. 151 LOGE("Upload to texture failed to gen mTextureID"); 152 rsc->dumpDebug(); 153 mUploadDefered = true; 154 return; 155 } 156 isFirstUpload = true; 157 } 158 glBindTexture(GL_TEXTURE_2D, mTextureID); 159 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 160 161 Adapter2D adapt(getContext(), this); 162 for (uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) { 163 adapt.setLOD(lod+mTextureLOD); 164 165 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0)); 166 if (isFirstUpload) { 167 glTexImage2D(GL_TEXTURE_2D, lod, format, 168 adapt.getDimX(), adapt.getDimY(), 169 0, format, type, ptr); 170 } else { 171 glTexSubImage2D(GL_TEXTURE_2D, lod, 0, 0, 172 adapt.getDimX(), adapt.getDimY(), 173 format, type, ptr); 174 } 175 } 176 if (mTextureGenMipmap) { 177#ifndef ANDROID_RS_BUILD_FOR_HOST 178 glGenerateMipmap(GL_TEXTURE_2D); 179#endif //ANDROID_RS_BUILD_FOR_HOST 180 } 181 182 rsc->checkError("Allocation::uploadToTexture"); 183} 184 185void Allocation::deferedUploadToBufferObject(const Context *rsc) { 186 mIsVertexBuffer = true; 187 mUploadDefered = true; 188} 189 190void Allocation::uploadToBufferObject(const Context *rsc) { 191 rsAssert(!mType->getDimY()); 192 rsAssert(!mType->getDimZ()); 193 194 mIsVertexBuffer = true; 195 if (!rsc->checkDriver()) { 196 mUploadDefered = true; 197 return; 198 } 199 200 if (!mBufferID) { 201 glGenBuffers(1, &mBufferID); 202 } 203 if (!mBufferID) { 204 LOGE("Upload to buffer object failed"); 205 mUploadDefered = true; 206 return; 207 } 208 209 glBindBuffer(GL_ARRAY_BUFFER, mBufferID); 210 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW); 211 glBindBuffer(GL_ARRAY_BUFFER, 0); 212 rsc->checkError("Allocation::uploadToBufferObject"); 213} 214 215void Allocation::uploadCheck(const Context *rsc) { 216 if (mUploadDefered) { 217 mUploadDefered = false; 218 if (mIsVertexBuffer) { 219 uploadToBufferObject(rsc); 220 } 221 if (mIsTexture) { 222 uploadToTexture(rsc); 223 } 224 } 225} 226 227 228void Allocation::data(Context *rsc, const void *data, uint32_t sizeBytes) { 229 uint32_t size = mType->getSizeBytes(); 230 if (size != sizeBytes) { 231 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes); 232 return; 233 } 234 235 if (mType->getElement()->getHasReferences()) { 236 incRefs(data, sizeBytes / mType->getElement()->getSizeBytes()); 237 decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes()); 238 } 239 240 memcpy(mPtr, data, size); 241 sendDirty(); 242 mUploadDefered = true; 243} 244 245void Allocation::read(void *data) { 246 memcpy(data, mPtr, mType->getSizeBytes()); 247} 248 249void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) { 250 uint32_t eSize = mType->getElementSizeBytes(); 251 uint8_t * ptr = static_cast<uint8_t *>(mPtr); 252 ptr += eSize * xoff; 253 uint32_t size = count * eSize; 254 255 if (size != sizeBytes) { 256 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes); 257 mType->dumpLOGV("type info"); 258 return; 259 } 260 261 if (mType->getElement()->getHasReferences()) { 262 incRefs(data, count); 263 decRefs(ptr, count); 264 } 265 266 memcpy(ptr, data, size); 267 sendDirty(); 268 mUploadDefered = true; 269} 270 271void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff, 272 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) { 273 uint32_t eSize = mType->getElementSizeBytes(); 274 uint32_t lineSize = eSize * w; 275 uint32_t destW = mType->getDimX(); 276 277 const uint8_t *src = static_cast<const uint8_t *>(data); 278 uint8_t *dst = static_cast<uint8_t *>(mPtr); 279 dst += eSize * (xoff + yoff * destW); 280 281 if ((lineSize * eSize * h) != sizeBytes) { 282 rsAssert(!"Allocation::subData called with mismatched size"); 283 return; 284 } 285 286 for (uint32_t line=yoff; line < (yoff+h); line++) { 287 if (mType->getElement()->getHasReferences()) { 288 incRefs(src, w); 289 decRefs(dst, w); 290 } 291 memcpy(dst, src, lineSize); 292 src += lineSize; 293 dst += destW * eSize; 294 } 295 sendDirty(); 296 mUploadDefered = true; 297} 298 299void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, 300 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) { 301} 302 303void Allocation::subElementData(Context *rsc, uint32_t x, const void *data, 304 uint32_t cIdx, uint32_t sizeBytes) { 305 uint32_t eSize = mType->getElementSizeBytes(); 306 uint8_t * ptr = static_cast<uint8_t *>(mPtr); 307 ptr += eSize * x; 308 309 if (cIdx >= mType->getElement()->getFieldCount()) { 310 LOGE("Error Allocation::subElementData component %i out of range.", cIdx); 311 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range."); 312 return; 313 } 314 315 if (x >= mType->getDimX()) { 316 LOGE("Error Allocation::subElementData X offset %i out of range.", x); 317 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); 318 return; 319 } 320 321 const Element * e = mType->getElement()->getField(cIdx); 322 ptr += mType->getElement()->getFieldOffsetBytes(cIdx); 323 324 if (sizeBytes != e->getSizeBytes()) { 325 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes()); 326 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); 327 return; 328 } 329 330 if (e->getHasReferences()) { 331 e->incRefs(data); 332 e->decRefs(ptr); 333 } 334 335 memcpy(ptr, data, sizeBytes); 336 sendDirty(); 337 mUploadDefered = true; 338} 339 340void Allocation::subElementData(Context *rsc, uint32_t x, uint32_t y, 341 const void *data, uint32_t cIdx, uint32_t sizeBytes) { 342 uint32_t eSize = mType->getElementSizeBytes(); 343 uint8_t * ptr = static_cast<uint8_t *>(mPtr); 344 ptr += eSize * (x + y * mType->getDimX()); 345 346 if (x >= mType->getDimX()) { 347 LOGE("Error Allocation::subElementData X offset %i out of range.", x); 348 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); 349 return; 350 } 351 352 if (y >= mType->getDimY()) { 353 LOGE("Error Allocation::subElementData X offset %i out of range.", x); 354 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); 355 return; 356 } 357 358 if (cIdx >= mType->getElement()->getFieldCount()) { 359 LOGE("Error Allocation::subElementData component %i out of range.", cIdx); 360 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range."); 361 return; 362 } 363 364 const Element * e = mType->getElement()->getField(cIdx); 365 ptr += mType->getElement()->getFieldOffsetBytes(cIdx); 366 367 if (sizeBytes != e->getSizeBytes()) { 368 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes()); 369 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); 370 return; 371 } 372 373 if (e->getHasReferences()) { 374 e->incRefs(data); 375 e->decRefs(ptr); 376 } 377 378 memcpy(ptr, data, sizeBytes); 379 sendDirty(); 380 mUploadDefered = true; 381} 382 383void Allocation::addProgramToDirty(const Program *p) { 384 mToDirtyList.push(p); 385} 386 387void Allocation::removeProgramToDirty(const Program *p) { 388 for (size_t ct=0; ct < mToDirtyList.size(); ct++) { 389 if (mToDirtyList[ct] == p) { 390 mToDirtyList.removeAt(ct); 391 return; 392 } 393 } 394 rsAssert(0); 395} 396 397void Allocation::dumpLOGV(const char *prefix) const { 398 ObjectBase::dumpLOGV(prefix); 399 400 String8 s(prefix); 401 s.append(" type "); 402 if (mType.get()) { 403 mType->dumpLOGV(s.string()); 404 } 405 406 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i", 407 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead); 408 409 LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i", 410 prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID); 411} 412 413void Allocation::serialize(OStream *stream) const { 414 // Need to identify ourselves 415 stream->addU32((uint32_t)getClassId()); 416 417 String8 name(getName()); 418 stream->addString(&name); 419 420 // First thing we need to serialize is the type object since it will be needed 421 // to initialize the class 422 mType->serialize(stream); 423 424 uint32_t dataSize = mType->getSizeBytes(); 425 // Write how much data we are storing 426 stream->addU32(dataSize); 427 // Now write the data 428 stream->addByteArray(mPtr, dataSize); 429} 430 431Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) { 432 // First make sure we are reading the correct object 433 RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); 434 if (classID != RS_A3D_CLASS_ID_ALLOCATION) { 435 LOGE("allocation loading skipped due to invalid class id\n"); 436 return NULL; 437 } 438 439 String8 name; 440 stream->loadString(&name); 441 442 Type *type = Type::createFromStream(rsc, stream); 443 if (!type) { 444 return NULL; 445 } 446 type->compute(); 447 448 // Number of bytes we wrote out for this allocation 449 uint32_t dataSize = stream->loadU32(); 450 if (dataSize != type->getSizeBytes()) { 451 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n"); 452 ObjectBase::checkDelete(type); 453 return NULL; 454 } 455 456 Allocation *alloc = new Allocation(rsc, type); 457 alloc->setName(name.string(), name.size()); 458 459 // Read in all of our allocation data 460 alloc->data(rsc, stream->getPtr() + stream->getPos(), dataSize); 461 stream->reset(stream->getPos() + dataSize); 462 463 return alloc; 464} 465 466void Allocation::sendDirty() const { 467 for (size_t ct=0; ct < mToDirtyList.size(); ct++) { 468 mToDirtyList[ct]->forceDirty(); 469 } 470} 471 472void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const { 473 const uint8_t *p = static_cast<const uint8_t *>(ptr); 474 const Element *e = mType->getElement(); 475 uint32_t stride = e->getSizeBytes(); 476 477 p += stride * startOff; 478 while (ct > 0) { 479 e->incRefs(p); 480 ct --; 481 p += stride; 482 } 483} 484 485void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const { 486 const uint8_t *p = static_cast<const uint8_t *>(ptr); 487 const Element *e = mType->getElement(); 488 uint32_t stride = e->getSizeBytes(); 489 490 p += stride * startOff; 491 while (ct > 0) { 492 e->decRefs(p); 493 ct --; 494 p += stride; 495 } 496} 497 498void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) { 499} 500 501void Allocation::resize1D(Context *rsc, uint32_t dimX) { 502 Type *t = mType->cloneAndResize1D(rsc, dimX); 503 504 uint32_t oldDimX = mType->getDimX(); 505 if (dimX == oldDimX) { 506 return; 507 } 508 509 if (dimX < oldDimX) { 510 decRefs(mPtr, oldDimX - dimX, dimX); 511 } 512 mPtr = realloc(mPtr, t->getSizeBytes()); 513 514 if (dimX > oldDimX) { 515 const Element *e = mType->getElement(); 516 uint32_t stride = e->getSizeBytes(); 517 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX)); 518 } 519 mType.set(t); 520} 521 522void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) { 523 LOGE("not implemented"); 524} 525 526///////////////// 527// 528 529 530namespace android { 531namespace renderscript { 532 533void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) { 534 Allocation *alloc = static_cast<Allocation *>(va); 535 alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel); 536} 537 538void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) { 539 Allocation *alloc = static_cast<Allocation *>(va); 540 alloc->deferedUploadToBufferObject(rsc); 541} 542 543static void mip565(const Adapter2D &out, const Adapter2D &in) { 544 uint32_t w = out.getDimX(); 545 uint32_t h = out.getDimY(); 546 547 for (uint32_t y=0; y < h; y++) { 548 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y)); 549 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2)); 550 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1)); 551 552 for (uint32_t x=0; x < w; x++) { 553 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); 554 oPtr ++; 555 i1 += 2; 556 i2 += 2; 557 } 558 } 559} 560 561static void mip8888(const Adapter2D &out, const Adapter2D &in) { 562 uint32_t w = out.getDimX(); 563 uint32_t h = out.getDimY(); 564 565 for (uint32_t y=0; y < h; y++) { 566 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y)); 567 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2)); 568 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1)); 569 570 for (uint32_t x=0; x < w; x++) { 571 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); 572 oPtr ++; 573 i1 += 2; 574 i2 += 2; 575 } 576 } 577} 578 579static void mip8(const Adapter2D &out, const Adapter2D &in) { 580 uint32_t w = out.getDimX(); 581 uint32_t h = out.getDimY(); 582 583 for (uint32_t y=0; y < h; y++) { 584 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y)); 585 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2)); 586 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1)); 587 588 for (uint32_t x=0; x < w; x++) { 589 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f); 590 oPtr ++; 591 i1 += 2; 592 i2 += 2; 593 } 594 } 595} 596 597static void mip(const Adapter2D &out, const Adapter2D &in) { 598 switch (out.getBaseType()->getElement()->getSizeBits()) { 599 case 32: 600 mip8888(out, in); 601 break; 602 case 16: 603 mip565(out, in); 604 break; 605 case 8: 606 mip8(out, in); 607 break; 608 } 609} 610 611typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count); 612 613static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count) { 614 memcpy(dst, src, count * 2); 615} 616static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count) { 617 memcpy(dst, src, count); 618} 619static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count) { 620 memcpy(dst, src, count * 4); 621} 622 623static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count) { 624 uint16_t *d = static_cast<uint16_t *>(dst); 625 const uint8_t *s = static_cast<const uint8_t *>(src); 626 627 while (count--) { 628 *d = rs888to565(s[0], s[1], s[2]); 629 d++; 630 s+= 3; 631 } 632} 633 634static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count) { 635 uint16_t *d = static_cast<uint16_t *>(dst); 636 const uint8_t *s = static_cast<const uint8_t *>(src); 637 638 while (count--) { 639 *d = rs888to565(s[0], s[1], s[2]); 640 d++; 641 s+= 4; 642 } 643} 644 645static ElementConverter_t pickConverter(const Element *dst, const Element *src) { 646 GLenum srcGLType = src->getComponent().getGLType(); 647 GLenum srcGLFmt = src->getComponent().getGLFormat(); 648 GLenum dstGLType = dst->getComponent().getGLType(); 649 GLenum dstGLFmt = dst->getComponent().getGLFormat(); 650 651 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) { 652 switch (dst->getSizeBytes()) { 653 case 4: 654 return elementConverter_cpy_32; 655 case 2: 656 return elementConverter_cpy_16; 657 case 1: 658 return elementConverter_cpy_8; 659 } 660 } 661 662 if (srcGLType == GL_UNSIGNED_BYTE && 663 srcGLFmt == GL_RGB && 664 dstGLType == GL_UNSIGNED_SHORT_5_6_5 && 665 dstGLFmt == GL_RGB) { 666 667 return elementConverter_888_to_565; 668 } 669 670 if (srcGLType == GL_UNSIGNED_BYTE && 671 srcGLFmt == GL_RGBA && 672 dstGLType == GL_UNSIGNED_SHORT_5_6_5 && 673 dstGLFmt == GL_RGB) { 674 675 return elementConverter_8888_to_565; 676 } 677 678 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst); 679 LOGE("pickConverter, srcGLType = %x, srcGLFmt = %x", srcGLType, srcGLFmt); 680 LOGE("pickConverter, dstGLType = %x, dstGLFmt = %x", dstGLType, dstGLFmt); 681 src->dumpLOGV("SRC "); 682 dst->dumpLOGV("DST "); 683 return 0; 684} 685 686#ifndef ANDROID_RS_BUILD_FOR_HOST 687 688RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype, 689 void *bmp, void *callbackData, 690 RsBitmapCallback_t callback) { 691 const Type * type = static_cast<const Type *>(vtype); 692 Allocation * alloc = new Allocation(rsc, type, bmp, callbackData, callback); 693 alloc->incUserRef(); 694 return alloc; 695} 696 697void rsi_AllocationUpdateFromBitmap(Context *rsc, RsAllocation va, 698 RsElement _src, const void *data) { 699 Allocation *texAlloc = static_cast<Allocation *>(va); 700 const Element *src = static_cast<const Element *>(_src); 701 const Element *dst = texAlloc->getType()->getElement(); 702 uint32_t w = texAlloc->getType()->getDimX(); 703 uint32_t h = texAlloc->getType()->getDimY(); 704 bool genMips = texAlloc->getType()->getDimLOD(); 705 706 ElementConverter_t cvt = pickConverter(dst, src); 707 if (cvt) { 708 cvt(texAlloc->getPtr(), data, w * h); 709 if (genMips) { 710 Adapter2D adapt(rsc, texAlloc); 711 Adapter2D adapt2(rsc, texAlloc); 712 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) { 713 adapt.setLOD(lod); 714 adapt2.setLOD(lod + 1); 715 mip(adapt2, adapt); 716 } 717 } 718 } else { 719 rsc->setError(RS_ERROR_BAD_VALUE, "Unsupported bitmap format"); 720 } 721} 722 723void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes) { 724 Allocation *a = static_cast<Allocation *>(va); 725 a->data(rsc, data, sizeBytes); 726} 727 728void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) { 729 Allocation *a = static_cast<Allocation *>(va); 730 a->subData(rsc, xoff, count, data, sizeBytes); 731} 732 733void rsi_Allocation2DSubElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, const void *data, uint32_t eoff, uint32_t sizeBytes) { 734 Allocation *a = static_cast<Allocation *>(va); 735 a->subElementData(rsc, x, y, data, eoff, sizeBytes); 736} 737 738void rsi_Allocation1DSubElementData(Context *rsc, RsAllocation va, uint32_t x, const void *data, uint32_t eoff, uint32_t sizeBytes) { 739 Allocation *a = static_cast<Allocation *>(va); 740 a->subElementData(rsc, x, data, eoff, sizeBytes); 741} 742 743void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) { 744 Allocation *a = static_cast<Allocation *>(va); 745 a->subData(rsc, xoff, yoff, w, h, data, sizeBytes); 746} 747 748void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) { 749 Allocation *a = static_cast<Allocation *>(va); 750 a->read(data); 751} 752 753void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) { 754 Allocation *a = static_cast<Allocation *>(va); 755 a->resize1D(rsc, dimX); 756} 757 758void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) { 759 Allocation *a = static_cast<Allocation *>(va); 760 a->resize2D(rsc, dimX, dimY); 761} 762 763#endif //ANDROID_RS_BUILD_FOR_HOST 764 765} 766} 767 768const void * rsaAllocationGetType(RsContext con, RsAllocation va) { 769 Allocation *a = static_cast<Allocation *>(va); 770 a->getType()->incUserRef(); 771 772 return a->getType(); 773} 774 775RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype) { 776 Context *rsc = static_cast<Context *>(con); 777 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype)); 778 alloc->incUserRef(); 779 return alloc; 780} 781 782RsAllocation rsaAllocationCreateFromBitmap(RsContext con, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data) { 783 Context *rsc = static_cast<Context *>(con); 784 const Element *src = static_cast<const Element *>(_src); 785 const Element *dst = static_cast<const Element *>(_dst); 786 787 //LOGE("%p rsi_AllocationCreateFromBitmap %i %i %i", rsc, w, h, genMips); 788 RsDimension dims[] = {RS_DIMENSION_X, RS_DIMENSION_Y, RS_DIMENSION_LOD}; 789 uint32_t dimValues[] = {w, h, genMips}; 790 RsType type = rsaTypeCreate(rsc, _dst, 3, dims, dimValues); 791 792 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, type); 793 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); 794 if (texAlloc == NULL) { 795 LOGE("Memory allocation failure"); 796 return NULL; 797 } 798 799 ElementConverter_t cvt = pickConverter(dst, src); 800 if (cvt) { 801 cvt(texAlloc->getPtr(), data, w * h); 802 if (genMips) { 803 Adapter2D adapt(rsc, texAlloc); 804 Adapter2D adapt2(rsc, texAlloc); 805 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) { 806 adapt.setLOD(lod); 807 adapt2.setLOD(lod + 1); 808 mip(adapt2, adapt); 809 } 810 } 811 } else { 812 rsc->setError(RS_ERROR_BAD_VALUE, "Unsupported bitmap format"); 813 delete texAlloc; 814 return NULL; 815 } 816 817 return texAlloc; 818} 819