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