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