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> |
Jason Sams | c2908e6 | 2010-02-23 17:44:28 -0800 | [diff] [blame] | 20 | #include <GLES2/gl2.h> |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 21 | #include <GLES/glext.h> |
| 22 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 23 | using namespace android; |
| 24 | using namespace android::renderscript; |
| 25 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 26 | Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 27 | { |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame^] | 28 | init(rsc, type); |
| 29 | |
| 30 | mPtr = malloc(mType->getSizeBytes()); |
| 31 | if (!mPtr) { |
| 32 | LOGE("Allocation::Allocation, alloc failure"); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | Allocation::Allocation(Context *rsc, const Type *type, void *bmp, |
| 37 | void *callbackData, RsBitmapCallback_t callback) |
| 38 | : ObjectBase(rsc) |
| 39 | { |
| 40 | init(rsc, type); |
| 41 | |
| 42 | mPtr = bmp; |
| 43 | mUserBitmapCallback = callback; |
| 44 | mUserBitmapCallbackData = callbackData; |
| 45 | } |
| 46 | |
| 47 | void Allocation::init(Context *rsc, const Type *type) |
| 48 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 49 | mAllocFile = __FILE__; |
| 50 | mAllocLine = __LINE__; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 51 | mPtr = NULL; |
| 52 | |
| 53 | mCpuWrite = false; |
| 54 | mCpuRead = false; |
| 55 | mGpuWrite = false; |
| 56 | mGpuRead = false; |
| 57 | |
| 58 | mReadWriteRatio = 0; |
| 59 | mUpdateSize = 0; |
| 60 | |
| 61 | mIsTexture = false; |
| 62 | mTextureID = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 63 | mIsVertexBuffer = false; |
| 64 | mBufferID = 0; |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 65 | mUploadDefered = false; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 66 | |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame^] | 67 | mUserBitmapCallback = NULL; |
| 68 | mUserBitmapCallbackData = NULL; |
| 69 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 70 | mType.set(type); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 71 | rsAssert(type); |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame^] | 72 | |
| 73 | mPtr = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | Allocation::~Allocation() |
| 77 | { |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame^] | 78 | if (mUserBitmapCallback != NULL) { |
| 79 | mUserBitmapCallback(mUserBitmapCallbackData); |
| 80 | } else { |
| 81 | free(mPtr); |
| 82 | } |
Jason Sams | b7a6c43 | 2009-11-02 14:25:10 -0800 | [diff] [blame] | 83 | mPtr = NULL; |
Jason Sams | 9d5e03d | 2009-11-03 11:25:42 -0800 | [diff] [blame] | 84 | |
| 85 | if (mBufferID) { |
| 86 | // Causes a SW crash.... |
| 87 | //LOGV(" mBufferID %i", mBufferID); |
| 88 | //glDeleteBuffers(1, &mBufferID); |
| 89 | //mBufferID = 0; |
| 90 | } |
| 91 | if (mTextureID) { |
| 92 | glDeleteTextures(1, &mTextureID); |
| 93 | mTextureID = 0; |
| 94 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void Allocation::setCpuWritable(bool) |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | void Allocation::setGpuWritable(bool) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | void Allocation::setCpuReadable(bool) |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | void Allocation::setGpuReadable(bool) |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | bool Allocation::fixAllocation() |
| 114 | { |
| 115 | return false; |
| 116 | } |
| 117 | |
Jason Sams | c2908e6 | 2010-02-23 17:44:28 -0800 | [diff] [blame] | 118 | void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset) |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 119 | { |
| 120 | rsAssert(lodOffset < mType->getLODCount()); |
| 121 | mIsTexture = true; |
| 122 | mTextureLOD = lodOffset; |
| 123 | mUploadDefered = true; |
Jason Sams | c2908e6 | 2010-02-23 17:44:28 -0800 | [diff] [blame] | 124 | mTextureGenMipmap = !mType->getDimLOD() && genMipmap; |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void Allocation::uploadToTexture(const Context *rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 128 | { |
| 129 | //rsAssert(!mTextureId); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 130 | |
| 131 | mIsTexture = true; |
| 132 | if (!rsc->checkDriver()) { |
| 133 | mUploadDefered = true; |
| 134 | return; |
| 135 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 136 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 137 | GLenum type = mType->getElement()->getComponent().getGLType(); |
| 138 | GLenum format = mType->getElement()->getComponent().getGLFormat(); |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 139 | |
| 140 | if (!type || !format) { |
| 141 | return; |
| 142 | } |
| 143 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 144 | if (!mTextureID) { |
| 145 | glGenTextures(1, &mTextureID); |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 146 | |
| 147 | if (!mTextureID) { |
| 148 | // This should not happen, however, its likely the cause of the |
| 149 | // white sqare bug. |
| 150 | // Force a crash to 1: restart the app, 2: make sure we get a bugreport. |
| 151 | LOGE("Upload to texture failed to gen mTextureID"); |
| 152 | rsc->dumpDebug(); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 153 | mUploadDefered = true; |
| 154 | return; |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 155 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 156 | } |
| 157 | glBindTexture(GL_TEXTURE_2D, mTextureID); |
Jason Sams | 0e27b5c | 2009-11-05 12:44:58 -0800 | [diff] [blame] | 158 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 159 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 160 | Adapter2D adapt(getContext(), this); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 161 | for(uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) { |
| 162 | adapt.setLOD(lod+mTextureLOD); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 163 | |
| 164 | uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0)); |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 165 | glTexImage2D(GL_TEXTURE_2D, lod, format, |
| 166 | adapt.getDimX(), adapt.getDimY(), |
| 167 | 0, format, type, ptr); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 168 | } |
Jason Sams | c2908e6 | 2010-02-23 17:44:28 -0800 | [diff] [blame] | 169 | if (mTextureGenMipmap) { |
| 170 | glGenerateMipmap(GL_TEXTURE_2D); |
| 171 | } |
| 172 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 175 | void Allocation::deferedUploadToBufferObject(const Context *rsc) |
| 176 | { |
| 177 | mIsVertexBuffer = true; |
| 178 | mUploadDefered = true; |
| 179 | } |
| 180 | |
| 181 | void Allocation::uploadToBufferObject(const Context *rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 182 | { |
| 183 | rsAssert(!mType->getDimY()); |
| 184 | rsAssert(!mType->getDimZ()); |
| 185 | |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 186 | mIsVertexBuffer = true; |
| 187 | if (!rsc->checkDriver()) { |
| 188 | mUploadDefered = true; |
| 189 | return; |
| 190 | } |
| 191 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 192 | if (!mBufferID) { |
| 193 | glGenBuffers(1, &mBufferID); |
| 194 | } |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 195 | if (!mBufferID) { |
| 196 | LOGE("Upload to buffer object failed"); |
| 197 | mUploadDefered = true; |
| 198 | return; |
| 199 | } |
| 200 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 201 | glBindBuffer(GL_ARRAY_BUFFER, mBufferID); |
| 202 | glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW); |
| 203 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 204 | } |
| 205 | |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 206 | void Allocation::uploadCheck(const Context *rsc) |
| 207 | { |
| 208 | if (mUploadDefered) { |
| 209 | mUploadDefered = false; |
| 210 | if (mIsVertexBuffer) { |
| 211 | uploadToBufferObject(rsc); |
| 212 | } |
| 213 | if (mIsTexture) { |
| 214 | uploadToTexture(rsc); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 219 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 220 | void Allocation::data(const void *data, uint32_t sizeBytes) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 221 | { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 222 | uint32_t size = mType->getSizeBytes(); |
| 223 | if (size != sizeBytes) { |
| 224 | LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes); |
| 225 | return; |
| 226 | } |
| 227 | memcpy(mPtr, data, size); |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 228 | sendDirty(); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 229 | mUploadDefered = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 232 | void Allocation::read(void *data) |
| 233 | { |
| 234 | memcpy(data, mPtr, mType->getSizeBytes()); |
| 235 | } |
| 236 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 237 | 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] | 238 | { |
| 239 | uint32_t eSize = mType->getElementSizeBytes(); |
| 240 | uint8_t * ptr = static_cast<uint8_t *>(mPtr); |
| 241 | ptr += eSize * xoff; |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 242 | uint32_t size = count * eSize; |
| 243 | |
| 244 | if (size != sizeBytes) { |
| 245 | 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] | 246 | mType->dumpLOGV("type info"); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | memcpy(ptr, data, size); |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 250 | sendDirty(); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 251 | mUploadDefered = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 254 | void Allocation::subData(uint32_t xoff, uint32_t yoff, |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 255 | uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 256 | { |
| 257 | uint32_t eSize = mType->getElementSizeBytes(); |
| 258 | uint32_t lineSize = eSize * w; |
| 259 | uint32_t destW = mType->getDimX(); |
| 260 | |
| 261 | const uint8_t *src = static_cast<const uint8_t *>(data); |
| 262 | uint8_t *dst = static_cast<uint8_t *>(mPtr); |
| 263 | dst += eSize * (xoff + yoff * destW); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 264 | |
| 265 | if ((lineSize * eSize * h) != sizeBytes) { |
| 266 | rsAssert(!"Allocation::subData called with mismatched size"); |
| 267 | return; |
| 268 | } |
| 269 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 270 | for (uint32_t line=yoff; line < (yoff+h); line++) { |
| 271 | uint8_t * ptr = static_cast<uint8_t *>(mPtr); |
| 272 | memcpy(dst, src, lineSize); |
| 273 | src += lineSize; |
| 274 | dst += destW * eSize; |
| 275 | } |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 276 | sendDirty(); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 277 | mUploadDefered = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff, |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 281 | 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] | 282 | { |
| 283 | } |
| 284 | |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 285 | void Allocation::addProgramToDirty(const Program *p) |
| 286 | { |
| 287 | mToDirtyList.add(p); |
| 288 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 289 | |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 290 | void Allocation::removeProgramToDirty(const Program *p) |
| 291 | { |
| 292 | for (size_t ct=0; ct < mToDirtyList.size(); ct++) { |
| 293 | if (mToDirtyList[ct] == p) { |
| 294 | mToDirtyList.removeAt(ct); |
| 295 | return; |
| 296 | } |
| 297 | } |
| 298 | rsAssert(0); |
| 299 | } |
| 300 | |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 301 | void Allocation::dumpLOGV(const char *prefix) const |
| 302 | { |
| 303 | ObjectBase::dumpLOGV(prefix); |
| 304 | |
| 305 | String8 s(prefix); |
| 306 | s.append(" type "); |
| 307 | if (mType.get()) { |
| 308 | mType->dumpLOGV(s.string()); |
| 309 | } |
| 310 | |
| 311 | LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i", |
| 312 | prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead); |
| 313 | |
Jason Sams | 36176536 | 2009-11-23 15:27:33 -0800 | [diff] [blame] | 314 | LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i", |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 315 | prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID); |
| 316 | |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 317 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 318 | |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 319 | void Allocation::sendDirty() const |
| 320 | { |
| 321 | for (size_t ct=0; ct < mToDirtyList.size(); ct++) { |
| 322 | mToDirtyList[ct]->forceDirty(); |
| 323 | } |
| 324 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 325 | |
| 326 | ///////////////// |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 327 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 328 | |
| 329 | |
| 330 | namespace android { |
| 331 | namespace renderscript { |
| 332 | |
| 333 | RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype) |
| 334 | { |
| 335 | const Type * type = static_cast<const Type *>(vtype); |
| 336 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 337 | Allocation * alloc = new Allocation(rsc, type); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 338 | alloc->incUserRef(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 339 | return alloc; |
| 340 | } |
| 341 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 342 | RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count) |
| 343 | { |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 344 | Type * type = new Type(rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 345 | type->setDimX(count); |
| 346 | type->setElement(static_cast<Element *>(e)); |
| 347 | type->compute(); |
| 348 | return rsi_AllocationCreateTyped(rsc, type); |
| 349 | } |
| 350 | |
Jason Sams | c2908e6 | 2010-02-23 17:44:28 -0800 | [diff] [blame] | 351 | void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 352 | { |
| 353 | Allocation *alloc = static_cast<Allocation *>(va); |
Jason Sams | c2908e6 | 2010-02-23 17:44:28 -0800 | [diff] [blame] | 354 | alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) |
| 358 | { |
| 359 | Allocation *alloc = static_cast<Allocation *>(va); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 360 | alloc->deferedUploadToBufferObject(rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 363 | static void mip565(const Adapter2D &out, const Adapter2D &in) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 364 | { |
| 365 | uint32_t w = out.getDimX(); |
| 366 | uint32_t h = out.getDimY(); |
| 367 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 368 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 369 | uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y)); |
| 370 | const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2)); |
| 371 | const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1)); |
| 372 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 373 | for (uint32_t x=0; x < w; x++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 374 | *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); |
| 375 | oPtr ++; |
| 376 | i1 += 2; |
| 377 | i2 += 2; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | static void mip8888(const Adapter2D &out, const Adapter2D &in) |
| 383 | { |
| 384 | uint32_t w = out.getDimX(); |
| 385 | uint32_t h = out.getDimY(); |
| 386 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 387 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 388 | uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y)); |
| 389 | const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2)); |
| 390 | const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1)); |
| 391 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 392 | for (uint32_t x=0; x < w; x++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 393 | *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 394 | oPtr ++; |
| 395 | i1 += 2; |
| 396 | i2 += 2; |
| 397 | } |
| 398 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Jason Sams | e20e3b4 | 2010-01-19 17:53:54 -0800 | [diff] [blame] | 401 | static void mip8(const Adapter2D &out, const Adapter2D &in) |
| 402 | { |
| 403 | uint32_t w = out.getDimX(); |
| 404 | uint32_t h = out.getDimY(); |
| 405 | |
| 406 | for (uint32_t y=0; y < h; y++) { |
| 407 | uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y)); |
| 408 | const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2)); |
| 409 | const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1)); |
| 410 | |
| 411 | for (uint32_t x=0; x < w; x++) { |
| 412 | *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f); |
| 413 | oPtr ++; |
| 414 | i1 += 2; |
| 415 | i2 += 2; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 420 | static void mip(const Adapter2D &out, const Adapter2D &in) |
| 421 | { |
| 422 | switch(out.getBaseType()->getElement()->getSizeBits()) { |
| 423 | case 32: |
| 424 | mip8888(out, in); |
| 425 | break; |
| 426 | case 16: |
| 427 | mip565(out, in); |
| 428 | break; |
Jason Sams | e20e3b4 | 2010-01-19 17:53:54 -0800 | [diff] [blame] | 429 | case 8: |
| 430 | mip8(out, in); |
| 431 | break; |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 432 | |
| 433 | } |
| 434 | |
| 435 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 436 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 437 | typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count); |
| 438 | |
| 439 | static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count) |
| 440 | { |
| 441 | memcpy(dst, src, count * 2); |
| 442 | } |
| 443 | static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count) |
| 444 | { |
| 445 | memcpy(dst, src, count); |
| 446 | } |
| 447 | static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count) |
| 448 | { |
| 449 | memcpy(dst, src, count * 4); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count) |
| 454 | { |
| 455 | uint16_t *d = static_cast<uint16_t *>(dst); |
| 456 | const uint8_t *s = static_cast<const uint8_t *>(src); |
| 457 | |
| 458 | while(count--) { |
| 459 | *d = rs888to565(s[0], s[1], s[2]); |
| 460 | d++; |
| 461 | s+= 3; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count) |
| 466 | { |
| 467 | uint16_t *d = static_cast<uint16_t *>(dst); |
| 468 | const uint8_t *s = static_cast<const uint8_t *>(src); |
| 469 | |
| 470 | while(count--) { |
| 471 | *d = rs888to565(s[0], s[1], s[2]); |
| 472 | d++; |
| 473 | s+= 4; |
| 474 | } |
| 475 | } |
| 476 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 477 | static ElementConverter_t pickConverter(const Element *dst, const Element *src) |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 478 | { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 479 | GLenum srcGLType = src->getComponent().getGLType(); |
| 480 | GLenum srcGLFmt = src->getComponent().getGLFormat(); |
| 481 | GLenum dstGLType = dst->getComponent().getGLType(); |
| 482 | GLenum dstGLFmt = dst->getComponent().getGLFormat(); |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 483 | |
| 484 | if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) { |
| 485 | switch(dst->getSizeBytes()) { |
| 486 | case 4: |
| 487 | return elementConverter_cpy_32; |
| 488 | case 2: |
| 489 | return elementConverter_cpy_16; |
| 490 | case 1: |
| 491 | return elementConverter_cpy_8; |
| 492 | } |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 495 | if (srcGLType == GL_UNSIGNED_BYTE && |
| 496 | srcGLFmt == GL_RGB && |
| 497 | dstGLType == GL_UNSIGNED_SHORT_5_6_5 && |
| 498 | dstGLType == GL_RGB) { |
| 499 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 500 | return elementConverter_888_to_565; |
| 501 | } |
| 502 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 503 | if (srcGLType == GL_UNSIGNED_BYTE && |
| 504 | srcGLFmt == GL_RGBA && |
| 505 | dstGLType == GL_UNSIGNED_SHORT_5_6_5 && |
| 506 | dstGLType == GL_RGB) { |
| 507 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 508 | return elementConverter_8888_to_565; |
| 509 | } |
| 510 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 511 | LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame^] | 515 | RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype, |
| 516 | void *bmp, void *callbackData, RsBitmapCallback_t callback) |
| 517 | { |
| 518 | const Type * type = static_cast<const Type *>(vtype); |
| 519 | Allocation * alloc = new Allocation(rsc, type, bmp, callbackData, callback); |
| 520 | alloc->incUserRef(); |
| 521 | return alloc; |
| 522 | } |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 523 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 524 | 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] | 525 | { |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 526 | const Element *src = static_cast<const Element *>(_src); |
| 527 | const Element *dst = static_cast<const Element *>(_dst); |
Jason Sams | 74e02ef | 2010-01-06 15:10:29 -0800 | [diff] [blame] | 528 | |
| 529 | // Check for pow2 on pre es 2.0 versions. |
| 530 | rsAssert(rsc->checkVersion2_0() || (!(w & (w-1)) && !(h & (h-1)))); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 531 | |
| 532 | //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] | 533 | rsi_TypeBegin(rsc, _dst); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 534 | rsi_TypeAdd(rsc, RS_DIMENSION_X, w); |
| 535 | rsi_TypeAdd(rsc, RS_DIMENSION_Y, h); |
| 536 | if (genMips) { |
| 537 | rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1); |
| 538 | } |
| 539 | RsType type = rsi_TypeCreate(rsc); |
| 540 | |
| 541 | RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type); |
| 542 | Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); |
| 543 | if (texAlloc == NULL) { |
| 544 | LOGE("Memory allocation failure"); |
| 545 | return NULL; |
| 546 | } |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 547 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 548 | ElementConverter_t cvt = pickConverter(dst, src); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 549 | cvt(texAlloc->getPtr(), data, w * h); |
| 550 | |
| 551 | if (genMips) { |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 552 | Adapter2D adapt(rsc, texAlloc); |
| 553 | Adapter2D adapt2(rsc, texAlloc); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 554 | for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) { |
| 555 | adapt.setLOD(lod); |
| 556 | adapt2.setLOD(lod + 1); |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 557 | mip(adapt2, adapt); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
| 561 | return texAlloc; |
| 562 | } |
| 563 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 564 | 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] | 565 | { |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 566 | const Element *srcE = static_cast<const Element *>(_src); |
| 567 | const Element *dstE = static_cast<const Element *>(_dst); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 568 | uint32_t w2 = rsHigherPow2(w); |
| 569 | uint32_t h2 = rsHigherPow2(h); |
| 570 | |
| 571 | if ((w2 == w) && (h2 == h)) { |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 572 | return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 575 | uint32_t bpp = srcE->getSizeBytes(); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 576 | size_t size = w2 * h2 * bpp; |
| 577 | uint8_t *tmp = static_cast<uint8_t *>(malloc(size)); |
| 578 | memset(tmp, 0, size); |
| 579 | |
| 580 | const uint8_t * src = static_cast<const uint8_t *>(data); |
| 581 | for (uint32_t y = 0; y < h; y++) { |
Jason Sams | faf1520 | 2009-07-29 20:55:44 -0700 | [diff] [blame] | 582 | uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp]; |
Marco Nelissen | 911458a | 2009-10-28 15:10:56 -0700 | [diff] [blame] | 583 | memcpy(&ydst[((w2 - w) >> 1) * bpp], src, w * bpp); |
Jason Sams | faf1520 | 2009-07-29 20:55:44 -0700 | [diff] [blame] | 584 | src += w * bpp; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 587 | RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 588 | free(tmp); |
| 589 | return ret; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 592 | 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] | 593 | { |
| 594 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 595 | a->data(data, sizeBytes); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 598 | 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] | 599 | { |
| 600 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 601 | a->subData(xoff, count, data, sizeBytes); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 604 | 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] | 605 | { |
| 606 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 607 | a->subData(xoff, yoff, w, h, data, sizeBytes); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 610 | void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) |
| 611 | { |
| 612 | Allocation *a = static_cast<Allocation *>(va); |
| 613 | a->read(data); |
| 614 | } |
| 615 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 616 | |
| 617 | } |
| 618 | } |