rsAllocation.cpp revision e9f5c53929c6c46872c4e9ba7cc3d0e528f5ad01
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 <GLES/glext.h> 21 22using namespace android; 23using namespace android::renderscript; 24 25Allocation::Allocation(const Type *type) 26{ 27 mPtr = NULL; 28 29 mCpuWrite = false; 30 mCpuRead = false; 31 mGpuWrite = false; 32 mGpuRead = false; 33 34 mReadWriteRatio = 0; 35 mUpdateSize = 0; 36 37 mIsTexture = false; 38 mTextureID = 0; 39 40 mIsVertexBuffer = false; 41 mBufferID = 0; 42 43 mType.set(type); 44 mPtr = malloc(mType->getSizeBytes()); 45 if (!mPtr) { 46 LOGE("Allocation::Allocation, alloc failure"); 47 } 48 49} 50 51Allocation::~Allocation() 52{ 53} 54 55void Allocation::setCpuWritable(bool) 56{ 57} 58 59void Allocation::setGpuWritable(bool) 60{ 61} 62 63void Allocation::setCpuReadable(bool) 64{ 65} 66 67void Allocation::setGpuReadable(bool) 68{ 69} 70 71bool Allocation::fixAllocation() 72{ 73 return false; 74} 75 76void Allocation::uploadToTexture(uint32_t lodOffset) 77{ 78 //rsAssert(!mTextureId); 79 rsAssert(lodOffset < mType->getLODCount()); 80 81 GLenum type = mType->getElement()->getGLType(); 82 GLenum format = mType->getElement()->getGLFormat(); 83 84 if (!type || !format) { 85 return; 86 } 87 88 if (!mTextureID) { 89 glGenTextures(1, &mTextureID); 90 } 91 glBindTexture(GL_TEXTURE_2D, mTextureID); 92 93 Adapter2D adapt(this); 94 for(uint32_t lod = 0; (lod + lodOffset) < mType->getLODCount(); lod++) { 95 adapt.setLOD(lod+lodOffset); 96 97 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0)); 98 glTexImage2D(GL_TEXTURE_2D, lod, format, 99 adapt.getDimX(), adapt.getDimY(), 100 0, format, type, ptr); 101 } 102} 103 104void Allocation::uploadToBufferObject() 105{ 106 rsAssert(!mType->getDimY()); 107 rsAssert(!mType->getDimZ()); 108 109 if (!mBufferID) { 110 glGenBuffers(1, &mBufferID); 111 } 112 glBindBuffer(GL_ARRAY_BUFFER, mBufferID); 113 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW); 114 glBindBuffer(GL_ARRAY_BUFFER, 0); 115} 116 117void Allocation::data(const void *data) 118{ 119 memcpy(mPtr, data, mType->getSizeBytes()); 120} 121 122void Allocation::subData(uint32_t xoff, uint32_t count, const void *data) 123{ 124 uint32_t eSize = mType->getElementSizeBytes(); 125 uint8_t * ptr = static_cast<uint8_t *>(mPtr); 126 ptr += eSize * xoff; 127 memcpy(ptr, data, count * eSize); 128} 129 130void Allocation::subData(uint32_t xoff, uint32_t yoff, 131 uint32_t w, uint32_t h, const void *data) 132{ 133 uint32_t eSize = mType->getElementSizeBytes(); 134 uint32_t lineSize = eSize * w; 135 uint32_t destW = mType->getDimX(); 136 137 const uint8_t *src = static_cast<const uint8_t *>(data); 138 uint8_t *dst = static_cast<uint8_t *>(mPtr); 139 dst += eSize * (xoff + yoff * destW); 140 for (uint32_t line=yoff; line < (yoff+h); line++) { 141 uint8_t * ptr = static_cast<uint8_t *>(mPtr); 142 memcpy(dst, src, lineSize); 143 src += lineSize; 144 dst += destW * eSize; 145 } 146} 147 148void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff, 149 uint32_t w, uint32_t h, uint32_t d, const void *data) 150{ 151} 152 153 154 155///////////////// 156// 157 158 159namespace android { 160namespace renderscript { 161 162RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype) 163{ 164 const Type * type = static_cast<const Type *>(vtype); 165 166 Allocation * alloc = new Allocation(type); 167 alloc->incRef(); 168 return alloc; 169} 170 171RsAllocation rsi_AllocationCreatePredefSized(Context *rsc, RsElementPredefined t, size_t count) 172{ 173 RsElement e = rsi_ElementGetPredefined(rsc, t); 174 return rsi_AllocationCreateSized(rsc, e, count); 175} 176 177RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count) 178{ 179 Type * type = new Type(); 180 type->setDimX(count); 181 type->setElement(static_cast<Element *>(e)); 182 type->compute(); 183 return rsi_AllocationCreateTyped(rsc, type); 184} 185 186void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel) 187{ 188 Allocation *alloc = static_cast<Allocation *>(va); 189 alloc->uploadToTexture(baseMipLevel); 190} 191 192void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) 193{ 194 Allocation *alloc = static_cast<Allocation *>(va); 195 alloc->uploadToBufferObject(); 196} 197 198void rsi_AllocationDestroy(Context *rsc, RsAllocation) 199{ 200} 201 202static void mip565(const Adapter2D &out, const Adapter2D &in) 203{ 204 uint32_t w = out.getDimX(); 205 uint32_t h = out.getDimY(); 206 207 for (uint32_t y=0; y < h; y++) { 208 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y)); 209 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2)); 210 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1)); 211 212 for (uint32_t x=0; x < w; x++) { 213 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); 214 oPtr ++; 215 i1 += 2; 216 i2 += 2; 217 } 218 } 219} 220 221static void mip8888(const Adapter2D &out, const Adapter2D &in) 222{ 223 uint32_t w = out.getDimX(); 224 uint32_t h = out.getDimY(); 225 226 for (uint32_t y=0; y < h; y++) { 227 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y)); 228 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2)); 229 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1)); 230 231 for (uint32_t x=0; x < w; x++) { 232 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); 233 oPtr ++; 234 i1 += 2; 235 i2 += 2; 236 } 237 } 238} 239 240static void mip(const Adapter2D &out, const Adapter2D &in) 241{ 242 switch(out.getBaseType()->getElement()->getSizeBits()) { 243 case 32: 244 mip8888(out, in); 245 break; 246 case 16: 247 mip565(out, in); 248 break; 249 250 } 251 252} 253 254typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count); 255 256static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count) 257{ 258 memcpy(dst, src, count * 2); 259} 260static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count) 261{ 262 memcpy(dst, src, count); 263} 264static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count) 265{ 266 memcpy(dst, src, count * 4); 267} 268 269 270static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count) 271{ 272 uint16_t *d = static_cast<uint16_t *>(dst); 273 const uint8_t *s = static_cast<const uint8_t *>(src); 274 275 while(count--) { 276 *d = rs888to565(s[0], s[1], s[2]); 277 d++; 278 s+= 3; 279 } 280} 281 282static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count) 283{ 284 uint16_t *d = static_cast<uint16_t *>(dst); 285 const uint8_t *s = static_cast<const uint8_t *>(src); 286 287 while(count--) { 288 *d = rs888to565(s[0], s[1], s[2]); 289 d++; 290 s+= 4; 291 } 292} 293 294static ElementConverter_t pickConverter(RsElementPredefined dstFmt, RsElementPredefined srcFmt) 295{ 296 if ((dstFmt == RS_ELEMENT_RGB_565) && 297 (srcFmt == RS_ELEMENT_RGB_565)) { 298 return elementConverter_cpy_16; 299 } 300 301 if ((dstFmt == RS_ELEMENT_RGB_565) && 302 (srcFmt == RS_ELEMENT_RGB_888)) { 303 return elementConverter_888_to_565; 304 } 305 306 if ((dstFmt == RS_ELEMENT_RGB_565) && 307 (srcFmt == RS_ELEMENT_RGBA_8888)) { 308 return elementConverter_8888_to_565; 309 } 310 311 if ((dstFmt == RS_ELEMENT_RGBA_8888) && 312 (srcFmt == RS_ELEMENT_RGBA_8888)) { 313 return elementConverter_cpy_32; 314 } 315 316 LOGE("pickConverter, unsuported combo, src %i, dst %i", srcFmt, dstFmt); 317 return 0; 318} 319 320 321RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElementPredefined dstFmt, RsElementPredefined srcFmt, bool genMips, const void *data) 322{ 323 rsAssert(!(w & (w-1))); 324 rsAssert(!(h & (h-1))); 325 326 //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips); 327 rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, dstFmt)); 328 rsi_TypeAdd(rsc, RS_DIMENSION_X, w); 329 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h); 330 if (genMips) { 331 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1); 332 } 333 RsType type = rsi_TypeCreate(rsc); 334 335 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type); 336 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); 337 if (texAlloc == NULL) { 338 LOGE("Memory allocation failure"); 339 return NULL; 340 } 341 texAlloc->incRef(); 342 343 ElementConverter_t cvt = pickConverter(dstFmt, srcFmt); 344 cvt(texAlloc->getPtr(), data, w * h); 345 346 if (genMips) { 347 Adapter2D adapt(texAlloc); 348 Adapter2D adapt2(texAlloc); 349 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) { 350 adapt.setLOD(lod); 351 adapt2.setLOD(lod + 1); 352 mip(adapt2, adapt); 353 } 354 } 355 356 return texAlloc; 357} 358 359static uint32_t fmtToBits(RsElementPredefined fmt) 360{ 361 return 16; 362} 363 364RsAllocation rsi_AllocationCreateFromBitmapBoxed(Context *rsc, uint32_t w, uint32_t h, RsElementPredefined dstFmt, RsElementPredefined srcFmt, bool genMips, const void *data) 365{ 366 uint32_t w2 = rsHigherPow2(w); 367 uint32_t h2 = rsHigherPow2(h); 368 369 if ((w2 == w) && (h2 == h)) { 370 return rsi_AllocationCreateFromBitmap(rsc, w, h, dstFmt, srcFmt, genMips, data); 371 } 372 373 uint32_t bpp = fmtToBits(srcFmt) >> 3; 374 size_t size = w2 * h2 * bpp; 375 uint8_t *tmp = static_cast<uint8_t *>(malloc(size)); 376 memset(tmp, 0, size); 377 378 const uint8_t * src = static_cast<const uint8_t *>(data); 379 for (uint32_t y = 0; y < h; y++) { 380 uint8_t * ydst = &tmp[y + ((h2 - h) >> 1)]; 381 memcpy(&ydst[(w2 - w) >> 1], src, w * bpp); 382 src += h * bpp; 383 } 384 385 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, dstFmt, srcFmt, genMips, tmp); 386 free(tmp); 387 return ret; 388 389 390 391 392} 393 394 395RsAllocation rsi_AllocationCreateFromFile(Context *rsc, const char *file, bool genMips) 396{ 397 bool use32bpp = false; 398 399 typedef struct _Win3xBitmapHeader 400 { 401 uint16_t type; 402 uint32_t totalSize; 403 uint32_t reserved; 404 uint32_t offset; 405 int32_t hdrSize; /* Size of this header in bytes */ 406 int32_t width; /* Image width in pixels */ 407 int32_t height; /* Image height in pixels */ 408 int16_t planes; /* Number of color planes */ 409 int16_t bpp; /* Number of bits per pixel */ 410 /* Fields added for Windows 3.x follow this line */ 411 int32_t compression; /* Compression methods used */ 412 int32_t sizeOfBitmap; /* Size of bitmap in bytes */ 413 int32_t horzResolution; /* Horizontal resolution in pixels per meter */ 414 int32_t vertResolution; /* Vertical resolution in pixels per meter */ 415 int32_t colorsUsed; /* Number of colors in the image */ 416 int32_t colorsImportant; /* Minimum number of important colors */ 417 } __attribute__((__packed__)) WIN3XBITMAPHEADER; 418 419 _Win3xBitmapHeader hdr; 420 421 FILE *f = fopen(file, "rb"); 422 if (f == NULL) { 423 LOGE("rsAllocationCreateFromBitmap failed to open file %s", file); 424 return NULL; 425 } 426 memset(&hdr, 0, sizeof(hdr)); 427 fread(&hdr, sizeof(hdr), 1, f); 428 429 if (hdr.bpp != 24) { 430 LOGE("Unsuported BMP type"); 431 fclose(f); 432 return NULL; 433 } 434 435 int32_t texWidth = rsHigherPow2(hdr.width); 436 int32_t texHeight = rsHigherPow2(hdr.height); 437 438 if (use32bpp) { 439 rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGBA_8888)); 440 } else { 441 rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGB_565)); 442 } 443 rsi_TypeAdd(rsc, RS_DIMENSION_X, texWidth); 444 rsi_TypeAdd(rsc, RS_DIMENSION_Y, texHeight); 445 if (genMips) { 446 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1); 447 } 448 RsType type = rsi_TypeCreate(rsc); 449 450 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type); 451 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); 452 texAlloc->incRef(); 453 if (texAlloc == NULL) { 454 LOGE("Memory allocation failure"); 455 fclose(f); 456 return NULL; 457 } 458 459 // offset to letterbox if height is not pow2 460 Adapter2D adapt(texAlloc); 461 uint8_t * fileInBuf = new uint8_t[texWidth * 3]; 462 uint32_t yOffset = (hdr.width - hdr.height) / 2; 463 464 if (use32bpp) { 465 uint8_t *tmp = static_cast<uint8_t *>(adapt.getElement(0, yOffset)); 466 for (int y=0; y < hdr.height; y++) { 467 fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET); 468 fread(fileInBuf, 1, hdr.width * 3, f); 469 for(int x=0; x < hdr.width; x++) { 470 tmp[0] = fileInBuf[x*3 + 2]; 471 tmp[1] = fileInBuf[x*3 + 1]; 472 tmp[2] = fileInBuf[x*3]; 473 tmp[3] = 0xff; 474 tmp += 4; 475 } 476 } 477 } else { 478 uint16_t *tmp = static_cast<uint16_t *>(adapt.getElement(0, yOffset)); 479 for (int y=0; y < hdr.height; y++) { 480 fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET); 481 fread(fileInBuf, 1, hdr.width * 3, f); 482 for(int x=0; x < hdr.width; x++) { 483 *tmp = rs888to565(fileInBuf[x*3 + 2], fileInBuf[x*3 + 1], fileInBuf[x*3]); 484 tmp++; 485 } 486 } 487 } 488 489 fclose(f); 490 delete [] fileInBuf; 491 492 if (genMips) { 493 Adapter2D adapt2(texAlloc); 494 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) { 495 adapt.setLOD(lod); 496 adapt2.setLOD(lod + 1); 497 mip(adapt2, adapt); 498 } 499 } 500 501 return texAlloc; 502} 503 504 505void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data) 506{ 507 Allocation *a = static_cast<Allocation *>(va); 508 a->data(data); 509} 510 511void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data) 512{ 513 Allocation *a = static_cast<Allocation *>(va); 514 a->subData(xoff, count, data); 515} 516 517void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data) 518{ 519 Allocation *a = static_cast<Allocation *>(va); 520 a->subData(xoff, yoff, w, h, data); 521} 522 523 524} 525} 526