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 | |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 19 | #include <GLES/gl.h> |
| 20 | #include <GLES/glext.h> |
| 21 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 22 | using namespace android; |
| 23 | using namespace android::renderscript; |
| 24 | |
| 25 | Allocation::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 | |
| 51 | Allocation::~Allocation() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | void Allocation::setCpuWritable(bool) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | void Allocation::setGpuWritable(bool) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | void Allocation::setCpuReadable(bool) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | void Allocation::setGpuReadable(bool) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | bool Allocation::fixAllocation() |
| 72 | { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | void Allocation::uploadToTexture(uint32_t lodOffset) |
| 77 | { |
| 78 | //rsAssert(!mTextureId); |
| 79 | rsAssert(lodOffset < mType->getLODCount()); |
| 80 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 81 | GLenum type = mType->getElement()->getGLType(); |
| 82 | GLenum format = mType->getElement()->getGLFormat(); |
| 83 | |
| 84 | if (!type || !format) { |
| 85 | return; |
| 86 | } |
| 87 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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)); |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 98 | glTexImage2D(GL_TEXTURE_2D, lod, format, |
| 99 | adapt.getDimX(), adapt.getDimY(), |
| 100 | 0, format, type, ptr); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | void Allocation::uploadToBufferObject() |
| 105 | { |
| 106 | rsAssert(!mType->getDimY()); |
| 107 | rsAssert(!mType->getDimZ()); |
| 108 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | |
| 117 | void Allocation::data(const void *data) |
| 118 | { |
| 119 | memcpy(mPtr, data, mType->getSizeBytes()); |
| 120 | } |
| 121 | |
| 122 | void 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 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 130 | void Allocation::subData(uint32_t xoff, uint32_t yoff, |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | |
| 148 | void 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 | ///////////////// |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 156 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 157 | |
| 158 | |
| 159 | namespace android { |
| 160 | namespace renderscript { |
| 161 | |
| 162 | RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype) |
| 163 | { |
| 164 | const Type * type = static_cast<const Type *>(vtype); |
| 165 | |
| 166 | Allocation * alloc = new Allocation(type); |
Jason Sams | 4244afa | 2009-07-02 15:09:27 -0700 | [diff] [blame] | 167 | alloc->incRef(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 168 | return alloc; |
| 169 | } |
| 170 | |
| 171 | RsAllocation 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 | |
| 177 | RsAllocation 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 | |
| 186 | void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel) |
| 187 | { |
| 188 | Allocation *alloc = static_cast<Allocation *>(va); |
| 189 | alloc->uploadToTexture(baseMipLevel); |
| 190 | } |
| 191 | |
| 192 | void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) |
| 193 | { |
| 194 | Allocation *alloc = static_cast<Allocation *>(va); |
| 195 | alloc->uploadToBufferObject(); |
| 196 | } |
| 197 | |
| 198 | void rsi_AllocationDestroy(Context *rsc, RsAllocation) |
| 199 | { |
| 200 | } |
| 201 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 202 | static void mip565(const Adapter2D &out, const Adapter2D &in) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 203 | { |
| 204 | uint32_t w = out.getDimX(); |
| 205 | uint32_t h = out.getDimY(); |
| 206 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 207 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 212 | for (uint32_t x=0; x < w; x++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 213 | *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); |
| 214 | oPtr ++; |
| 215 | i1 += 2; |
| 216 | i2 += 2; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | static void mip8888(const Adapter2D &out, const Adapter2D &in) |
| 222 | { |
| 223 | uint32_t w = out.getDimX(); |
| 224 | uint32_t h = out.getDimY(); |
| 225 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 226 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 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 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 231 | for (uint32_t x=0; x < w; x++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 232 | *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 233 | oPtr ++; |
| 234 | i1 += 2; |
| 235 | i2 += 2; |
| 236 | } |
| 237 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 240 | static 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 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 253 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 254 | typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count); |
| 255 | |
| 256 | static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count) |
| 257 | { |
| 258 | memcpy(dst, src, count * 2); |
| 259 | } |
| 260 | static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count) |
| 261 | { |
| 262 | memcpy(dst, src, count); |
| 263 | } |
| 264 | static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count) |
| 265 | { |
| 266 | memcpy(dst, src, count * 4); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | static 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 | |
| 282 | static 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 | |
| 294 | static ElementConverter_t pickConverter(RsElementPredefined dstFmt, RsElementPredefined srcFmt) |
| 295 | { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 296 | if ((dstFmt == RS_ELEMENT_RGB_565) && |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 297 | (srcFmt == RS_ELEMENT_RGB_565)) { |
| 298 | return elementConverter_cpy_16; |
| 299 | } |
| 300 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 301 | if ((dstFmt == RS_ELEMENT_RGB_565) && |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 302 | (srcFmt == RS_ELEMENT_RGB_888)) { |
| 303 | return elementConverter_888_to_565; |
| 304 | } |
| 305 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 306 | if ((dstFmt == RS_ELEMENT_RGB_565) && |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 307 | (srcFmt == RS_ELEMENT_RGBA_8888)) { |
| 308 | return elementConverter_8888_to_565; |
| 309 | } |
| 310 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 311 | if ((dstFmt == RS_ELEMENT_RGBA_8888) && |
| 312 | (srcFmt == RS_ELEMENT_RGBA_8888)) { |
| 313 | return elementConverter_cpy_32; |
| 314 | } |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 315 | |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 316 | LOGE("pickConverter, unsuported combo, src %i, dst %i", srcFmt, dstFmt); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElementPredefined dstFmt, RsElementPredefined srcFmt, bool genMips, const void *data) |
| 322 | { |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 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)); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 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); |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 352 | mip(adapt2, adapt); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
| 356 | return texAlloc; |
| 357 | } |
| 358 | |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 359 | static uint32_t fmtToBits(RsElementPredefined fmt) |
| 360 | { |
| 361 | return 16; |
| 362 | } |
| 363 | |
| 364 | RsAllocation 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++) { |
Jason Sams | faf1520 | 2009-07-29 20:55:44 -0700 | [diff] [blame^] | 380 | uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp]; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 381 | memcpy(&ydst[(w2 - w) >> 1], src, w * bpp); |
Jason Sams | faf1520 | 2009-07-29 20:55:44 -0700 | [diff] [blame^] | 382 | src += w * bpp; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 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 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 395 | RsAllocation rsi_AllocationCreateFromFile(Context *rsc, const char *file, bool genMips) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 396 | { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 397 | bool use32bpp = false; |
| 398 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 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 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 463 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 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 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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); |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 497 | mip(adapt2, adapt); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
| 501 | return texAlloc; |
| 502 | } |
| 503 | |
| 504 | |
| 505 | void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data) |
| 506 | { |
| 507 | Allocation *a = static_cast<Allocation *>(va); |
| 508 | a->data(data); |
| 509 | } |
| 510 | |
| 511 | void 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 | |
| 517 | void 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 | } |