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