rsAllocation.cpp revision c1ed589021e280cda59a0521cb96b3e9eb629e1b
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "rsContext.h" 18 19#include <GLES/gl.h> 20#include <GLES2/gl2.h> 21#include <GLES/glext.h> 22 23using namespace android; 24using namespace android::renderscript; 25 26Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc) 27{ 28 init(rsc, type); 29 30 mPtr = malloc(mType->getSizeBytes()); 31 if (!mPtr) { 32 LOGE("Allocation::Allocation, alloc failure"); 33 } 34} 35 36Allocation::Allocation(Context *rsc, const Type *type, void *bmp, 37 void *callbackData, RsBitmapCallback_t callback) 38: ObjectBase(rsc) 39{ 40 init(rsc, type); 41 42 mPtr = bmp; 43 mUserBitmapCallback = callback; 44 mUserBitmapCallbackData = callbackData; 45} 46 47void Allocation::init(Context *rsc, const Type *type) 48{ 49 mAllocFile = __FILE__; 50 mAllocLine = __LINE__; 51 mPtr = NULL; 52 53 mCpuWrite = false; 54 mCpuRead = false; 55 mGpuWrite = false; 56 mGpuRead = false; 57 58 mReadWriteRatio = 0; 59 mUpdateSize = 0; 60 61 mIsTexture = false; 62 mTextureID = 0; 63 mIsVertexBuffer = false; 64 mBufferID = 0; 65 mUploadDefered = false; 66 67 mUserBitmapCallback = NULL; 68 mUserBitmapCallbackData = NULL; 69 70 mType.set(type); 71 rsAssert(type); 72 73 mPtr = NULL; 74} 75 76Allocation::~Allocation() 77{ 78 if (mUserBitmapCallback != NULL) { 79 mUserBitmapCallback(mUserBitmapCallbackData); 80 } else { 81 free(mPtr); 82 } 83 mPtr = NULL; 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} 100 101void Allocation::setGpuWritable(bool) 102{ 103} 104 105void Allocation::setCpuReadable(bool) 106{ 107} 108 109void Allocation::setGpuReadable(bool) 110{ 111} 112 113bool Allocation::fixAllocation() 114{ 115 return false; 116} 117 118void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset) 119{ 120 rsAssert(lodOffset < mType->getLODCount()); 121 mIsTexture = true; 122 mTextureLOD = lodOffset; 123 mUploadDefered = true; 124 mTextureGenMipmap = !mType->getDimLOD() && genMipmap; 125} 126 127void Allocation::uploadToTexture(const Context *rsc) 128{ 129 //rsAssert(!mTextureId); 130 131 mIsTexture = true; 132 if (!rsc->checkDriver()) { 133 mUploadDefered = true; 134 return; 135 } 136 137 GLenum type = mType->getElement()->getComponent().getGLType(); 138 GLenum format = mType->getElement()->getComponent().getGLFormat(); 139 140 if (!type || !format) { 141 return; 142 } 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 } 157 glBindTexture(GL_TEXTURE_2D, mTextureID); 158 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 159 160 Adapter2D adapt(getContext(), this); 161 for(uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) { 162 adapt.setLOD(lod+mTextureLOD); 163 164 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0)); 165 glTexImage2D(GL_TEXTURE_2D, lod, format, 166 adapt.getDimX(), adapt.getDimY(), 167 0, format, type, ptr); 168 } 169 if (mTextureGenMipmap) { 170 glGenerateMipmap(GL_TEXTURE_2D); 171 } 172 173 rsc->checkError("Allocation::uploadToTexture"); 174} 175 176void Allocation::deferedUploadToBufferObject(const Context *rsc) 177{ 178 mIsVertexBuffer = true; 179 mUploadDefered = true; 180} 181 182void Allocation::uploadToBufferObject(const Context *rsc) 183{ 184 rsAssert(!mType->getDimY()); 185 rsAssert(!mType->getDimZ()); 186 187 mIsVertexBuffer = true; 188 if (!rsc->checkDriver()) { 189 mUploadDefered = true; 190 return; 191 } 192 193 if (!mBufferID) { 194 glGenBuffers(1, &mBufferID); 195 } 196 if (!mBufferID) { 197 LOGE("Upload to buffer object failed"); 198 mUploadDefered = true; 199 return; 200 } 201 202 glBindBuffer(GL_ARRAY_BUFFER, mBufferID); 203 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW); 204 glBindBuffer(GL_ARRAY_BUFFER, 0); 205 rsc->checkError("Allocation::uploadToBufferObject"); 206} 207 208void Allocation::uploadCheck(const Context *rsc) 209{ 210 if (mUploadDefered) { 211 mUploadDefered = false; 212 if (mIsVertexBuffer) { 213 uploadToBufferObject(rsc); 214 } 215 if (mIsTexture) { 216 uploadToTexture(rsc); 217 } 218 } 219} 220 221 222void Allocation::data(const void *data, uint32_t sizeBytes) 223{ 224 uint32_t size = mType->getSizeBytes(); 225 if (size != sizeBytes) { 226 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes); 227 return; 228 } 229 memcpy(mPtr, data, size); 230 sendDirty(); 231 mUploadDefered = true; 232} 233 234void Allocation::read(void *data) 235{ 236 memcpy(data, mPtr, mType->getSizeBytes()); 237} 238 239void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) 240{ 241 uint32_t eSize = mType->getElementSizeBytes(); 242 uint8_t * ptr = static_cast<uint8_t *>(mPtr); 243 ptr += eSize * xoff; 244 uint32_t size = count * eSize; 245 246 if (size != sizeBytes) { 247 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes); 248 mType->dumpLOGV("type info"); 249 return; 250 } 251 memcpy(ptr, data, size); 252 sendDirty(); 253 mUploadDefered = true; 254} 255 256void Allocation::subData(uint32_t xoff, uint32_t yoff, 257 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) 258{ 259 uint32_t eSize = mType->getElementSizeBytes(); 260 uint32_t lineSize = eSize * w; 261 uint32_t destW = mType->getDimX(); 262 263 const uint8_t *src = static_cast<const uint8_t *>(data); 264 uint8_t *dst = static_cast<uint8_t *>(mPtr); 265 dst += eSize * (xoff + yoff * destW); 266 267 if ((lineSize * eSize * h) != sizeBytes) { 268 rsAssert(!"Allocation::subData called with mismatched size"); 269 return; 270 } 271 272 for (uint32_t line=yoff; line < (yoff+h); line++) { 273 uint8_t * ptr = static_cast<uint8_t *>(mPtr); 274 memcpy(dst, src, lineSize); 275 src += lineSize; 276 dst += destW * eSize; 277 } 278 sendDirty(); 279 mUploadDefered = true; 280} 281 282void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff, 283 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) 284{ 285} 286 287void Allocation::addProgramToDirty(const Program *p) 288{ 289 mToDirtyList.add(p); 290} 291 292void Allocation::removeProgramToDirty(const Program *p) 293{ 294 for (size_t ct=0; ct < mToDirtyList.size(); ct++) { 295 if (mToDirtyList[ct] == p) { 296 mToDirtyList.removeAt(ct); 297 return; 298 } 299 } 300 rsAssert(0); 301} 302 303void Allocation::dumpLOGV(const char *prefix) const 304{ 305 ObjectBase::dumpLOGV(prefix); 306 307 String8 s(prefix); 308 s.append(" type "); 309 if (mType.get()) { 310 mType->dumpLOGV(s.string()); 311 } 312 313 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i", 314 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead); 315 316 LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i", 317 prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID); 318 319} 320 321void Allocation::sendDirty() const 322{ 323 for (size_t ct=0; ct < mToDirtyList.size(); ct++) { 324 mToDirtyList[ct]->forceDirty(); 325 } 326} 327 328///////////////// 329// 330 331 332namespace android { 333namespace renderscript { 334 335RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype) 336{ 337 const Type * type = static_cast<const Type *>(vtype); 338 339 Allocation * alloc = new Allocation(rsc, type); 340 alloc->incUserRef(); 341 return alloc; 342} 343 344RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count) 345{ 346 Type * type = new Type(rsc); 347 type->setDimX(count); 348 type->setElement(static_cast<Element *>(e)); 349 type->compute(); 350 return rsi_AllocationCreateTyped(rsc, type); 351} 352 353void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) 354{ 355 Allocation *alloc = static_cast<Allocation *>(va); 356 alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel); 357} 358 359void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) 360{ 361 Allocation *alloc = static_cast<Allocation *>(va); 362 alloc->deferedUploadToBufferObject(rsc); 363} 364 365static void mip565(const Adapter2D &out, const Adapter2D &in) 366{ 367 uint32_t w = out.getDimX(); 368 uint32_t h = out.getDimY(); 369 370 for (uint32_t y=0; y < h; y++) { 371 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y)); 372 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2)); 373 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1)); 374 375 for (uint32_t x=0; x < w; x++) { 376 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); 377 oPtr ++; 378 i1 += 2; 379 i2 += 2; 380 } 381 } 382} 383 384static void mip8888(const Adapter2D &out, const Adapter2D &in) 385{ 386 uint32_t w = out.getDimX(); 387 uint32_t h = out.getDimY(); 388 389 for (uint32_t y=0; y < h; y++) { 390 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y)); 391 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2)); 392 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1)); 393 394 for (uint32_t x=0; x < w; x++) { 395 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); 396 oPtr ++; 397 i1 += 2; 398 i2 += 2; 399 } 400 } 401} 402 403static void mip8(const Adapter2D &out, const Adapter2D &in) 404{ 405 uint32_t w = out.getDimX(); 406 uint32_t h = out.getDimY(); 407 408 for (uint32_t y=0; y < h; y++) { 409 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y)); 410 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2)); 411 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1)); 412 413 for (uint32_t x=0; x < w; x++) { 414 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f); 415 oPtr ++; 416 i1 += 2; 417 i2 += 2; 418 } 419 } 420} 421 422static void mip(const Adapter2D &out, const Adapter2D &in) 423{ 424 switch(out.getBaseType()->getElement()->getSizeBits()) { 425 case 32: 426 mip8888(out, in); 427 break; 428 case 16: 429 mip565(out, in); 430 break; 431 case 8: 432 mip8(out, in); 433 break; 434 435 } 436 437} 438 439typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count); 440 441static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count) 442{ 443 memcpy(dst, src, count * 2); 444} 445static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count) 446{ 447 memcpy(dst, src, count); 448} 449static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count) 450{ 451 memcpy(dst, src, count * 4); 452} 453 454 455static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count) 456{ 457 uint16_t *d = static_cast<uint16_t *>(dst); 458 const uint8_t *s = static_cast<const uint8_t *>(src); 459 460 while(count--) { 461 *d = rs888to565(s[0], s[1], s[2]); 462 d++; 463 s+= 3; 464 } 465} 466 467static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count) 468{ 469 uint16_t *d = static_cast<uint16_t *>(dst); 470 const uint8_t *s = static_cast<const uint8_t *>(src); 471 472 while(count--) { 473 *d = rs888to565(s[0], s[1], s[2]); 474 d++; 475 s+= 4; 476 } 477} 478 479static ElementConverter_t pickConverter(const Element *dst, const Element *src) 480{ 481 GLenum srcGLType = src->getComponent().getGLType(); 482 GLenum srcGLFmt = src->getComponent().getGLFormat(); 483 GLenum dstGLType = dst->getComponent().getGLType(); 484 GLenum dstGLFmt = dst->getComponent().getGLFormat(); 485 486 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) { 487 switch(dst->getSizeBytes()) { 488 case 4: 489 return elementConverter_cpy_32; 490 case 2: 491 return elementConverter_cpy_16; 492 case 1: 493 return elementConverter_cpy_8; 494 } 495 } 496 497 if (srcGLType == GL_UNSIGNED_BYTE && 498 srcGLFmt == GL_RGB && 499 dstGLType == GL_UNSIGNED_SHORT_5_6_5 && 500 dstGLType == GL_RGB) { 501 502 return elementConverter_888_to_565; 503 } 504 505 if (srcGLType == GL_UNSIGNED_BYTE && 506 srcGLFmt == GL_RGBA && 507 dstGLType == GL_UNSIGNED_SHORT_5_6_5 && 508 dstGLType == GL_RGB) { 509 510 return elementConverter_8888_to_565; 511 } 512 513 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst); 514 return 0; 515} 516 517RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype, 518 void *bmp, void *callbackData, RsBitmapCallback_t callback) 519{ 520 const Type * type = static_cast<const Type *>(vtype); 521 Allocation * alloc = new Allocation(rsc, type, bmp, callbackData, callback); 522 alloc->incUserRef(); 523 return alloc; 524} 525 526RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data) 527{ 528 const Element *src = static_cast<const Element *>(_src); 529 const Element *dst = static_cast<const Element *>(_dst); 530 531 // Check for pow2 on pre es 2.0 versions. 532 rsAssert(rsc->checkVersion2_0() || (!(w & (w-1)) && !(h & (h-1)))); 533 534 //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips); 535 rsi_TypeBegin(rsc, _dst); 536 rsi_TypeAdd(rsc, RS_DIMENSION_X, w); 537 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h); 538 if (genMips) { 539 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1); 540 } 541 RsType type = rsi_TypeCreate(rsc); 542 543 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type); 544 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); 545 if (texAlloc == NULL) { 546 LOGE("Memory allocation failure"); 547 return NULL; 548 } 549 550 ElementConverter_t cvt = pickConverter(dst, src); 551 cvt(texAlloc->getPtr(), data, w * h); 552 553 if (genMips) { 554 Adapter2D adapt(rsc, texAlloc); 555 Adapter2D adapt2(rsc, texAlloc); 556 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) { 557 adapt.setLOD(lod); 558 adapt2.setLOD(lod + 1); 559 mip(adapt2, adapt); 560 } 561 } 562 563 return texAlloc; 564} 565 566RsAllocation rsi_AllocationCreateFromBitmapBoxed(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data) 567{ 568 const Element *srcE = static_cast<const Element *>(_src); 569 const Element *dstE = static_cast<const Element *>(_dst); 570 uint32_t w2 = rsHigherPow2(w); 571 uint32_t h2 = rsHigherPow2(h); 572 573 if ((w2 == w) && (h2 == h)) { 574 return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data); 575 } 576 577 uint32_t bpp = srcE->getSizeBytes(); 578 size_t size = w2 * h2 * bpp; 579 uint8_t *tmp = static_cast<uint8_t *>(malloc(size)); 580 memset(tmp, 0, size); 581 582 const uint8_t * src = static_cast<const uint8_t *>(data); 583 for (uint32_t y = 0; y < h; y++) { 584 uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp]; 585 memcpy(&ydst[((w2 - w) >> 1) * bpp], src, w * bpp); 586 src += w * bpp; 587 } 588 589 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp); 590 free(tmp); 591 return ret; 592} 593 594void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes) 595{ 596 Allocation *a = static_cast<Allocation *>(va); 597 a->data(data, sizeBytes); 598} 599 600void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) 601{ 602 Allocation *a = static_cast<Allocation *>(va); 603 a->subData(xoff, count, data, sizeBytes); 604} 605 606void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) 607{ 608 Allocation *a = static_cast<Allocation *>(va); 609 a->subData(xoff, yoff, w, h, data, sizeBytes); 610} 611 612void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) 613{ 614 Allocation *a = static_cast<Allocation *>(va); 615 a->read(data); 616} 617 618 619} 620} 621