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