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 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 25 | Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 26 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 27 | mAllocFile = __FILE__; |
| 28 | mAllocLine = __LINE__; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 29 | mPtr = NULL; |
| 30 | |
| 31 | mCpuWrite = false; |
| 32 | mCpuRead = false; |
| 33 | mGpuWrite = false; |
| 34 | mGpuRead = false; |
| 35 | |
| 36 | mReadWriteRatio = 0; |
| 37 | mUpdateSize = 0; |
| 38 | |
| 39 | mIsTexture = false; |
| 40 | mTextureID = 0; |
| 41 | |
| 42 | mIsVertexBuffer = false; |
| 43 | mBufferID = 0; |
| 44 | |
| 45 | mType.set(type); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 46 | rsAssert(type); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 47 | mPtr = malloc(mType->getSizeBytes()); |
| 48 | if (!mPtr) { |
| 49 | LOGE("Allocation::Allocation, alloc failure"); |
| 50 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | Allocation::~Allocation() |
| 54 | { |
Jason Sams | b7a6c43 | 2009-11-02 14:25:10 -0800 | [diff] [blame] | 55 | free(mPtr); |
| 56 | mPtr = NULL; |
Jason Sams | 9d5e03d | 2009-11-03 11:25:42 -0800 | [diff] [blame] | 57 | |
| 58 | if (mBufferID) { |
| 59 | // Causes a SW crash.... |
| 60 | //LOGV(" mBufferID %i", mBufferID); |
| 61 | //glDeleteBuffers(1, &mBufferID); |
| 62 | //mBufferID = 0; |
| 63 | } |
| 64 | if (mTextureID) { |
| 65 | glDeleteTextures(1, &mTextureID); |
| 66 | mTextureID = 0; |
| 67 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void Allocation::setCpuWritable(bool) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | void Allocation::setGpuWritable(bool) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | void Allocation::setCpuReadable(bool) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | void Allocation::setGpuReadable(bool) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | bool Allocation::fixAllocation() |
| 87 | { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | void Allocation::uploadToTexture(uint32_t lodOffset) |
| 92 | { |
| 93 | //rsAssert(!mTextureId); |
| 94 | rsAssert(lodOffset < mType->getLODCount()); |
| 95 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 96 | GLenum type = mType->getElement()->getGLType(); |
| 97 | GLenum format = mType->getElement()->getGLFormat(); |
| 98 | |
| 99 | if (!type || !format) { |
| 100 | return; |
| 101 | } |
| 102 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 103 | if (!mTextureID) { |
| 104 | glGenTextures(1, &mTextureID); |
| 105 | } |
| 106 | glBindTexture(GL_TEXTURE_2D, mTextureID); |
Jason Sams | 0e27b5c | 2009-11-05 12:44:58 -0800 | [diff] [blame] | 107 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 108 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 109 | Adapter2D adapt(getContext(), this); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 110 | for(uint32_t lod = 0; (lod + lodOffset) < mType->getLODCount(); lod++) { |
| 111 | adapt.setLOD(lod+lodOffset); |
| 112 | |
| 113 | uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0)); |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 114 | glTexImage2D(GL_TEXTURE_2D, lod, format, |
| 115 | adapt.getDimX(), adapt.getDimY(), |
| 116 | 0, format, type, ptr); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | void Allocation::uploadToBufferObject() |
| 121 | { |
| 122 | rsAssert(!mType->getDimY()); |
| 123 | rsAssert(!mType->getDimZ()); |
| 124 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 125 | if (!mBufferID) { |
| 126 | glGenBuffers(1, &mBufferID); |
| 127 | } |
| 128 | glBindBuffer(GL_ARRAY_BUFFER, mBufferID); |
| 129 | glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW); |
| 130 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 131 | } |
| 132 | |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 133 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 134 | void Allocation::data(const void *data, uint32_t sizeBytes) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 135 | { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 136 | uint32_t size = mType->getSizeBytes(); |
| 137 | if (size != sizeBytes) { |
| 138 | LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes); |
| 139 | return; |
| 140 | } |
| 141 | memcpy(mPtr, data, size); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 144 | void Allocation::read(void *data) |
| 145 | { |
| 146 | memcpy(data, mPtr, mType->getSizeBytes()); |
| 147 | } |
| 148 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 149 | void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 150 | { |
| 151 | uint32_t eSize = mType->getElementSizeBytes(); |
| 152 | uint8_t * ptr = static_cast<uint8_t *>(mPtr); |
| 153 | ptr += eSize * xoff; |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 154 | uint32_t size = count * eSize; |
| 155 | |
| 156 | if (size != sizeBytes) { |
| 157 | LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes); |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 158 | mType->dumpLOGV("type info"); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | memcpy(ptr, data, size); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 164 | void Allocation::subData(uint32_t xoff, uint32_t yoff, |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 165 | uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 166 | { |
| 167 | uint32_t eSize = mType->getElementSizeBytes(); |
| 168 | uint32_t lineSize = eSize * w; |
| 169 | uint32_t destW = mType->getDimX(); |
| 170 | |
| 171 | const uint8_t *src = static_cast<const uint8_t *>(data); |
| 172 | uint8_t *dst = static_cast<uint8_t *>(mPtr); |
| 173 | dst += eSize * (xoff + yoff * destW); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 174 | |
| 175 | if ((lineSize * eSize * h) != sizeBytes) { |
| 176 | rsAssert(!"Allocation::subData called with mismatched size"); |
| 177 | return; |
| 178 | } |
| 179 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 180 | for (uint32_t line=yoff; line < (yoff+h); line++) { |
| 181 | uint8_t * ptr = static_cast<uint8_t *>(mPtr); |
| 182 | memcpy(dst, src, lineSize); |
| 183 | src += lineSize; |
| 184 | dst += destW * eSize; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff, |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 189 | uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 190 | { |
| 191 | } |
| 192 | |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 193 | void Allocation::dumpLOGV(const char *prefix) const |
| 194 | { |
| 195 | ObjectBase::dumpLOGV(prefix); |
| 196 | |
| 197 | String8 s(prefix); |
| 198 | s.append(" type "); |
| 199 | if (mType.get()) { |
| 200 | mType->dumpLOGV(s.string()); |
| 201 | } |
| 202 | |
| 203 | LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i", |
| 204 | prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead); |
| 205 | |
| 206 | LOGV("%s allocation mIsTexture=%i mIsTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i", |
| 207 | prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID); |
| 208 | |
| 209 | |
| 210 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 211 | |
| 212 | |
| 213 | ///////////////// |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 214 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 215 | |
| 216 | |
| 217 | namespace android { |
| 218 | namespace renderscript { |
| 219 | |
| 220 | RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype) |
| 221 | { |
| 222 | const Type * type = static_cast<const Type *>(vtype); |
| 223 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 224 | Allocation * alloc = new Allocation(rsc, type); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 225 | alloc->incUserRef(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 226 | return alloc; |
| 227 | } |
| 228 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 229 | RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count) |
| 230 | { |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 231 | Type * type = new Type(rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 232 | type->setDimX(count); |
| 233 | type->setElement(static_cast<Element *>(e)); |
| 234 | type->compute(); |
| 235 | return rsi_AllocationCreateTyped(rsc, type); |
| 236 | } |
| 237 | |
| 238 | void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel) |
| 239 | { |
| 240 | Allocation *alloc = static_cast<Allocation *>(va); |
| 241 | alloc->uploadToTexture(baseMipLevel); |
| 242 | } |
| 243 | |
| 244 | void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) |
| 245 | { |
| 246 | Allocation *alloc = static_cast<Allocation *>(va); |
| 247 | alloc->uploadToBufferObject(); |
| 248 | } |
| 249 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 250 | static void mip565(const Adapter2D &out, const Adapter2D &in) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 251 | { |
| 252 | uint32_t w = out.getDimX(); |
| 253 | uint32_t h = out.getDimY(); |
| 254 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 255 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 256 | uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y)); |
| 257 | const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2)); |
| 258 | const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1)); |
| 259 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 260 | for (uint32_t x=0; x < w; x++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 261 | *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); |
| 262 | oPtr ++; |
| 263 | i1 += 2; |
| 264 | i2 += 2; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | static void mip8888(const Adapter2D &out, const Adapter2D &in) |
| 270 | { |
| 271 | uint32_t w = out.getDimX(); |
| 272 | uint32_t h = out.getDimY(); |
| 273 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 274 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 275 | uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y)); |
| 276 | const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2)); |
| 277 | const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1)); |
| 278 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 279 | for (uint32_t x=0; x < w; x++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 280 | *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 281 | oPtr ++; |
| 282 | i1 += 2; |
| 283 | i2 += 2; |
| 284 | } |
| 285 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 288 | static void mip(const Adapter2D &out, const Adapter2D &in) |
| 289 | { |
| 290 | switch(out.getBaseType()->getElement()->getSizeBits()) { |
| 291 | case 32: |
| 292 | mip8888(out, in); |
| 293 | break; |
| 294 | case 16: |
| 295 | mip565(out, in); |
| 296 | break; |
| 297 | |
| 298 | } |
| 299 | |
| 300 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 301 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 302 | typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count); |
| 303 | |
| 304 | static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count) |
| 305 | { |
| 306 | memcpy(dst, src, count * 2); |
| 307 | } |
| 308 | static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count) |
| 309 | { |
| 310 | memcpy(dst, src, count); |
| 311 | } |
| 312 | static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count) |
| 313 | { |
| 314 | memcpy(dst, src, count * 4); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count) |
| 319 | { |
| 320 | uint16_t *d = static_cast<uint16_t *>(dst); |
| 321 | const uint8_t *s = static_cast<const uint8_t *>(src); |
| 322 | |
| 323 | while(count--) { |
| 324 | *d = rs888to565(s[0], s[1], s[2]); |
| 325 | d++; |
| 326 | s+= 3; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count) |
| 331 | { |
| 332 | uint16_t *d = static_cast<uint16_t *>(dst); |
| 333 | const uint8_t *s = static_cast<const uint8_t *>(src); |
| 334 | |
| 335 | while(count--) { |
| 336 | *d = rs888to565(s[0], s[1], s[2]); |
| 337 | d++; |
| 338 | s+= 4; |
| 339 | } |
| 340 | } |
| 341 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 342 | static ElementConverter_t pickConverter(const Element *dst, const Element *src) |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 343 | { |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 344 | GLenum srcGLType = src->getGLType(); |
| 345 | GLenum srcGLFmt = src->getGLFormat(); |
| 346 | GLenum dstGLType = dst->getGLType(); |
| 347 | GLenum dstGLFmt = dst->getGLFormat(); |
| 348 | |
| 349 | if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) { |
| 350 | switch(dst->getSizeBytes()) { |
| 351 | case 4: |
| 352 | return elementConverter_cpy_32; |
| 353 | case 2: |
| 354 | return elementConverter_cpy_16; |
| 355 | case 1: |
| 356 | return elementConverter_cpy_8; |
| 357 | } |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 360 | if (srcGLType == GL_UNSIGNED_BYTE && |
| 361 | srcGLFmt == GL_RGB && |
| 362 | dstGLType == GL_UNSIGNED_SHORT_5_6_5 && |
| 363 | dstGLType == GL_RGB) { |
| 364 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 365 | return elementConverter_888_to_565; |
| 366 | } |
| 367 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 368 | if (srcGLType == GL_UNSIGNED_BYTE && |
| 369 | srcGLFmt == GL_RGBA && |
| 370 | dstGLType == GL_UNSIGNED_SHORT_5_6_5 && |
| 371 | dstGLType == GL_RGB) { |
| 372 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 373 | return elementConverter_8888_to_565; |
| 374 | } |
| 375 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 376 | LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 381 | RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data) |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 382 | { |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 383 | const Element *src = static_cast<const Element *>(_src); |
| 384 | const Element *dst = static_cast<const Element *>(_dst); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 385 | rsAssert(!(w & (w-1))); |
| 386 | rsAssert(!(h & (h-1))); |
| 387 | |
| 388 | //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips); |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 389 | rsi_TypeBegin(rsc, _dst); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 390 | rsi_TypeAdd(rsc, RS_DIMENSION_X, w); |
| 391 | rsi_TypeAdd(rsc, RS_DIMENSION_Y, h); |
| 392 | if (genMips) { |
| 393 | rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1); |
| 394 | } |
| 395 | RsType type = rsi_TypeCreate(rsc); |
| 396 | |
| 397 | RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type); |
| 398 | Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); |
| 399 | if (texAlloc == NULL) { |
| 400 | LOGE("Memory allocation failure"); |
| 401 | return NULL; |
| 402 | } |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 403 | texAlloc->incUserRef(); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 404 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 405 | ElementConverter_t cvt = pickConverter(dst, src); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 406 | cvt(texAlloc->getPtr(), data, w * h); |
| 407 | |
| 408 | if (genMips) { |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 409 | Adapter2D adapt(rsc, texAlloc); |
| 410 | Adapter2D adapt2(rsc, texAlloc); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 411 | for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) { |
| 412 | adapt.setLOD(lod); |
| 413 | adapt2.setLOD(lod + 1); |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 414 | mip(adapt2, adapt); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
| 418 | return texAlloc; |
| 419 | } |
| 420 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 421 | RsAllocation rsi_AllocationCreateFromBitmapBoxed(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data) |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 422 | { |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 423 | const Element *srcE = static_cast<const Element *>(_src); |
| 424 | const Element *dstE = static_cast<const Element *>(_dst); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 425 | uint32_t w2 = rsHigherPow2(w); |
| 426 | uint32_t h2 = rsHigherPow2(h); |
| 427 | |
| 428 | if ((w2 == w) && (h2 == h)) { |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 429 | return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 432 | uint32_t bpp = srcE->getSizeBytes(); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 433 | size_t size = w2 * h2 * bpp; |
| 434 | uint8_t *tmp = static_cast<uint8_t *>(malloc(size)); |
| 435 | memset(tmp, 0, size); |
| 436 | |
| 437 | const uint8_t * src = static_cast<const uint8_t *>(data); |
| 438 | for (uint32_t y = 0; y < h; y++) { |
Jason Sams | faf1520 | 2009-07-29 20:55:44 -0700 | [diff] [blame] | 439 | uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp]; |
Marco Nelissen | 911458a | 2009-10-28 15:10:56 -0700 | [diff] [blame] | 440 | memcpy(&ydst[((w2 - w) >> 1) * bpp], src, w * bpp); |
Jason Sams | faf1520 | 2009-07-29 20:55:44 -0700 | [diff] [blame] | 441 | src += w * bpp; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 444 | RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 445 | free(tmp); |
| 446 | return ret; |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | } |
| 452 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 453 | void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 454 | { |
| 455 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 456 | a->data(data, sizeBytes); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 457 | rsc->allocationCheck(a); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 460 | void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 461 | { |
| 462 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 463 | a->subData(xoff, count, data, sizeBytes); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 464 | rsc->allocationCheck(a); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 467 | void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 468 | { |
| 469 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 470 | a->subData(xoff, yoff, w, h, data, sizeBytes); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 471 | rsc->allocationCheck(a); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 474 | void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) |
| 475 | { |
| 476 | Allocation *a = static_cast<Allocation *>(va); |
| 477 | a->read(data); |
| 478 | } |
| 479 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 480 | |
| 481 | } |
| 482 | } |