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