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