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