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