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