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 | |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 29 | #include "utils/StopWatch.h" |
| 30 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 31 | using namespace android; |
| 32 | using namespace android::renderscript; |
| 33 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 34 | Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages) : ObjectBase(rsc) { |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 35 | init(rsc, type); |
| 36 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 37 | mUsageFlags = usages; |
| 38 | |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 39 | mPtr = malloc(mType->getSizeBytes()); |
Jason Sams | ee73498 | 2010-08-12 12:44:02 -0700 | [diff] [blame] | 40 | if (mType->getElement()->getHasReferences()) { |
| 41 | memset(mPtr, 0, mType->getSizeBytes()); |
| 42 | } |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 43 | if (!mPtr) { |
| 44 | LOGE("Allocation::Allocation, alloc failure"); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | Allocation::Allocation(Context *rsc, const Type *type, void *bmp, |
| 49 | void *callbackData, RsBitmapCallback_t callback) |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 50 | : ObjectBase(rsc) { |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 51 | init(rsc, type); |
| 52 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 53 | mUsageFlags = RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE; |
| 54 | |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 55 | mPtr = bmp; |
| 56 | mUserBitmapCallback = callback; |
| 57 | mUserBitmapCallbackData = callbackData; |
| 58 | } |
| 59 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 60 | void Allocation::init(Context *rsc, const Type *type) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 61 | mPtr = NULL; |
| 62 | |
| 63 | mCpuWrite = false; |
| 64 | mCpuRead = false; |
| 65 | mGpuWrite = false; |
| 66 | mGpuRead = false; |
| 67 | |
| 68 | mReadWriteRatio = 0; |
| 69 | mUpdateSize = 0; |
| 70 | |
| 71 | mIsTexture = false; |
| 72 | mTextureID = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 73 | mIsVertexBuffer = false; |
| 74 | mBufferID = 0; |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 75 | mUploadDefered = false; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 76 | |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 77 | mUserBitmapCallback = NULL; |
| 78 | mUserBitmapCallbackData = NULL; |
| 79 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 80 | mType.set(type); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 81 | rsAssert(type); |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 82 | |
| 83 | mPtr = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 86 | Allocation::~Allocation() { |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 87 | if (mUserBitmapCallback != NULL) { |
| 88 | mUserBitmapCallback(mUserBitmapCallbackData); |
| 89 | } else { |
| 90 | free(mPtr); |
| 91 | } |
Jason Sams | b7a6c43 | 2009-11-02 14:25:10 -0800 | [diff] [blame] | 92 | mPtr = NULL; |
Jason Sams | 9d5e03d | 2009-11-03 11:25:42 -0800 | [diff] [blame] | 93 | |
| 94 | if (mBufferID) { |
| 95 | // Causes a SW crash.... |
| 96 | //LOGV(" mBufferID %i", mBufferID); |
| 97 | //glDeleteBuffers(1, &mBufferID); |
| 98 | //mBufferID = 0; |
| 99 | } |
| 100 | if (mTextureID) { |
| 101 | glDeleteTextures(1, &mTextureID); |
| 102 | mTextureID = 0; |
| 103 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 106 | void Allocation::setCpuWritable(bool) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 109 | void Allocation::setGpuWritable(bool) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 112 | void Allocation::setCpuReadable(bool) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 115 | void Allocation::setGpuReadable(bool) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 118 | bool Allocation::fixAllocation() { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 122 | void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset) { |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 123 | rsAssert(lodOffset < mType->getLODCount()); |
| 124 | mIsTexture = true; |
| 125 | mTextureLOD = lodOffset; |
| 126 | mUploadDefered = true; |
Jason Sams | c2908e6 | 2010-02-23 17:44:28 -0800 | [diff] [blame] | 127 | mTextureGenMipmap = !mType->getDimLOD() && genMipmap; |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 130 | uint32_t Allocation::getGLTarget() const { |
| 131 | if (mIsTexture) { |
| 132 | if (mType->getDimFaces()) { |
| 133 | return GL_TEXTURE_CUBE_MAP; |
| 134 | } else { |
| 135 | return GL_TEXTURE_2D; |
| 136 | } |
| 137 | } |
| 138 | if (mIsVertexBuffer) { |
| 139 | return GL_ARRAY_BUFFER; |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 144 | void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) { |
| 145 | rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT); |
| 146 | |
| 147 | if (mIsTexture) { |
| 148 | uploadToTexture(rsc); |
| 149 | } |
| 150 | if (mIsVertexBuffer) { |
| 151 | uploadToBufferObject(rsc); |
| 152 | } |
| 153 | |
| 154 | mUploadDefered = false; |
| 155 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 156 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 157 | void Allocation::uploadToTexture(const Context *rsc) { |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 158 | |
| 159 | mIsTexture = true; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 160 | GLenum type = mType->getElement()->getComponent().getGLType(); |
| 161 | GLenum format = mType->getElement()->getComponent().getGLFormat(); |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 162 | |
| 163 | if (!type || !format) { |
| 164 | return; |
| 165 | } |
| 166 | |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 167 | bool isFirstUpload = false; |
| 168 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 169 | if (!mTextureID) { |
| 170 | glGenTextures(1, &mTextureID); |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 171 | |
| 172 | if (!mTextureID) { |
| 173 | // This should not happen, however, its likely the cause of the |
| 174 | // white sqare bug. |
| 175 | // Force a crash to 1: restart the app, 2: make sure we get a bugreport. |
| 176 | LOGE("Upload to texture failed to gen mTextureID"); |
| 177 | rsc->dumpDebug(); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 178 | mUploadDefered = true; |
| 179 | return; |
Jason Sams | 9dab667 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 180 | } |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 181 | isFirstUpload = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 182 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 183 | |
| 184 | GLenum target = (GLenum)getGLTarget(); |
| 185 | glBindTexture(target, mTextureID); |
Jason Sams | 0e27b5c | 2009-11-05 12:44:58 -0800 | [diff] [blame] | 186 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 187 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 188 | if (target == GL_TEXTURE_2D) { |
| 189 | upload2DTexture(isFirstUpload); |
| 190 | } else if (target == GL_TEXTURE_CUBE_MAP) { |
| 191 | uploadCubeTexture(isFirstUpload); |
| 192 | } |
| 193 | |
| 194 | if (mTextureGenMipmap) { |
| 195 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
| 196 | glGenerateMipmap(target); |
| 197 | #endif //ANDROID_RS_BUILD_FOR_HOST |
| 198 | } |
| 199 | |
| 200 | rsc->checkError("Allocation::uploadToTexture"); |
| 201 | } |
| 202 | |
| 203 | void Allocation::upload2DTexture(bool isFirstUpload) { |
| 204 | GLenum type = mType->getElement()->getComponent().getGLType(); |
| 205 | GLenum format = mType->getElement()->getComponent().getGLFormat(); |
| 206 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 207 | Adapter2D adapt(getContext(), this); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 208 | for (uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) { |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 209 | adapt.setLOD(lod+mTextureLOD); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 210 | |
| 211 | uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0)); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 212 | if (isFirstUpload) { |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 213 | glTexImage2D(GL_TEXTURE_2D, lod, format, |
| 214 | adapt.getDimX(), adapt.getDimY(), |
| 215 | 0, format, type, ptr); |
| 216 | } else { |
| 217 | glTexSubImage2D(GL_TEXTURE_2D, lod, 0, 0, |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 218 | adapt.getDimX(), adapt.getDimY(), |
| 219 | format, type, ptr); |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 220 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 221 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 222 | } |
Jason Sams | c2908e6 | 2010-02-23 17:44:28 -0800 | [diff] [blame] | 223 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 224 | void Allocation::uploadCubeTexture(bool isFirstUpload) { |
| 225 | GLenum type = mType->getElement()->getComponent().getGLType(); |
| 226 | GLenum format = mType->getElement()->getComponent().getGLFormat(); |
| 227 | |
| 228 | GLenum faceOrder[] = { |
| 229 | GL_TEXTURE_CUBE_MAP_POSITIVE_X, |
| 230 | GL_TEXTURE_CUBE_MAP_NEGATIVE_X, |
| 231 | GL_TEXTURE_CUBE_MAP_POSITIVE_Y, |
| 232 | GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, |
| 233 | GL_TEXTURE_CUBE_MAP_POSITIVE_Z, |
| 234 | GL_TEXTURE_CUBE_MAP_NEGATIVE_Z |
| 235 | }; |
| 236 | |
| 237 | Adapter2D adapt(getContext(), this); |
| 238 | for (uint32_t face = 0; face < 6; face ++) { |
| 239 | adapt.setFace(face); |
| 240 | |
| 241 | for (uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) { |
| 242 | adapt.setLOD(lod+mTextureLOD); |
| 243 | |
| 244 | uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0)); |
| 245 | |
| 246 | if (isFirstUpload) { |
| 247 | glTexImage2D(faceOrder[face], lod, format, |
| 248 | adapt.getDimX(), adapt.getDimY(), |
| 249 | 0, format, type, ptr); |
| 250 | } else { |
| 251 | glTexSubImage2D(faceOrder[face], lod, 0, 0, |
| 252 | adapt.getDimX(), adapt.getDimY(), |
| 253 | format, type, ptr); |
| 254 | } |
| 255 | } |
| 256 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 259 | void Allocation::deferedUploadToBufferObject(const Context *rsc) { |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 260 | mIsVertexBuffer = true; |
| 261 | mUploadDefered = true; |
| 262 | } |
| 263 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 264 | void Allocation::uploadToBufferObject(const Context *rsc) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 265 | rsAssert(!mType->getDimY()); |
| 266 | rsAssert(!mType->getDimZ()); |
| 267 | |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 268 | mIsVertexBuffer = true; |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 269 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 270 | if (!mBufferID) { |
| 271 | glGenBuffers(1, &mBufferID); |
| 272 | } |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 273 | if (!mBufferID) { |
| 274 | LOGE("Upload to buffer object failed"); |
| 275 | mUploadDefered = true; |
| 276 | return; |
| 277 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 278 | GLenum target = (GLenum)getGLTarget(); |
| 279 | glBindBuffer(target, mBufferID); |
| 280 | glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW); |
| 281 | glBindBuffer(target, 0); |
Jason Sams | f468700 | 2010-03-10 17:30:41 -0800 | [diff] [blame] | 282 | rsc->checkError("Allocation::uploadToBufferObject"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 285 | void Allocation::uploadCheck(Context *rsc) { |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 286 | if (mUploadDefered) { |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 287 | syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 291 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 292 | void Allocation::data(Context *rsc, const void *data, uint32_t sizeBytes) { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 293 | uint32_t size = mType->getSizeBytes(); |
| 294 | if (size != sizeBytes) { |
| 295 | LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes); |
| 296 | return; |
| 297 | } |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 298 | |
| 299 | if (mType->getElement()->getHasReferences()) { |
| 300 | incRefs(data, sizeBytes / mType->getElement()->getSizeBytes()); |
| 301 | decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes()); |
| 302 | } |
| 303 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 304 | memcpy(mPtr, data, size); |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 305 | sendDirty(); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 306 | mUploadDefered = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 309 | void Allocation::read(void *data) { |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 310 | memcpy(data, mPtr, mType->getSizeBytes()); |
| 311 | } |
| 312 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 313 | void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 314 | uint32_t eSize = mType->getElementSizeBytes(); |
| 315 | uint8_t * ptr = static_cast<uint8_t *>(mPtr); |
| 316 | ptr += eSize * xoff; |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 317 | uint32_t size = count * eSize; |
| 318 | |
| 319 | if (size != sizeBytes) { |
| 320 | 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] | 321 | mType->dumpLOGV("type info"); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 322 | return; |
| 323 | } |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 324 | |
| 325 | if (mType->getElement()->getHasReferences()) { |
| 326 | incRefs(data, count); |
| 327 | decRefs(ptr, count); |
| 328 | } |
| 329 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 330 | memcpy(ptr, data, size); |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 331 | sendDirty(); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 332 | mUploadDefered = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 335 | void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 336 | uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 337 | uint32_t eSize = mType->getElementSizeBytes(); |
| 338 | uint32_t lineSize = eSize * w; |
| 339 | uint32_t destW = mType->getDimX(); |
| 340 | |
| 341 | const uint8_t *src = static_cast<const uint8_t *>(data); |
| 342 | uint8_t *dst = static_cast<uint8_t *>(mPtr); |
| 343 | dst += eSize * (xoff + yoff * destW); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 344 | |
| 345 | if ((lineSize * eSize * h) != sizeBytes) { |
| 346 | rsAssert(!"Allocation::subData called with mismatched size"); |
| 347 | return; |
| 348 | } |
| 349 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 350 | for (uint32_t line=yoff; line < (yoff+h); line++) { |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 351 | if (mType->getElement()->getHasReferences()) { |
| 352 | incRefs(src, w); |
| 353 | decRefs(dst, w); |
| 354 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 355 | memcpy(dst, src, lineSize); |
| 356 | src += lineSize; |
| 357 | dst += destW * eSize; |
| 358 | } |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 359 | sendDirty(); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 360 | mUploadDefered = true; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 363 | void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 364 | 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] | 365 | } |
| 366 | |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 367 | void Allocation::subElementData(Context *rsc, uint32_t x, const void *data, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 368 | uint32_t cIdx, uint32_t sizeBytes) { |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 369 | uint32_t eSize = mType->getElementSizeBytes(); |
| 370 | uint8_t * ptr = static_cast<uint8_t *>(mPtr); |
| 371 | ptr += eSize * x; |
| 372 | |
| 373 | if (cIdx >= mType->getElement()->getFieldCount()) { |
| 374 | LOGE("Error Allocation::subElementData component %i out of range.", cIdx); |
| 375 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range."); |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | if (x >= mType->getDimX()) { |
| 380 | LOGE("Error Allocation::subElementData X offset %i out of range.", x); |
| 381 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | const Element * e = mType->getElement()->getField(cIdx); |
| 386 | ptr += mType->getElement()->getFieldOffsetBytes(cIdx); |
| 387 | |
| 388 | if (sizeBytes != e->getSizeBytes()) { |
| 389 | LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes()); |
| 390 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | if (e->getHasReferences()) { |
| 395 | e->incRefs(data); |
| 396 | e->decRefs(ptr); |
| 397 | } |
| 398 | |
| 399 | memcpy(ptr, data, sizeBytes); |
| 400 | sendDirty(); |
| 401 | mUploadDefered = true; |
| 402 | } |
| 403 | |
| 404 | void Allocation::subElementData(Context *rsc, uint32_t x, uint32_t y, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 405 | const void *data, uint32_t cIdx, uint32_t sizeBytes) { |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 406 | uint32_t eSize = mType->getElementSizeBytes(); |
| 407 | uint8_t * ptr = static_cast<uint8_t *>(mPtr); |
| 408 | ptr += eSize * (x + y * mType->getDimX()); |
| 409 | |
| 410 | if (x >= mType->getDimX()) { |
| 411 | LOGE("Error Allocation::subElementData X offset %i out of range.", x); |
| 412 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | if (y >= mType->getDimY()) { |
| 417 | LOGE("Error Allocation::subElementData X offset %i out of range.", x); |
| 418 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | if (cIdx >= mType->getElement()->getFieldCount()) { |
| 423 | LOGE("Error Allocation::subElementData component %i out of range.", cIdx); |
| 424 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range."); |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | const Element * e = mType->getElement()->getField(cIdx); |
| 429 | ptr += mType->getElement()->getFieldOffsetBytes(cIdx); |
| 430 | |
| 431 | if (sizeBytes != e->getSizeBytes()) { |
| 432 | LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes()); |
| 433 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); |
| 434 | return; |
| 435 | } |
| 436 | |
| 437 | if (e->getHasReferences()) { |
| 438 | e->incRefs(data); |
| 439 | e->decRefs(ptr); |
| 440 | } |
| 441 | |
| 442 | memcpy(ptr, data, sizeBytes); |
| 443 | sendDirty(); |
| 444 | mUploadDefered = true; |
| 445 | } |
| 446 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 447 | void Allocation::addProgramToDirty(const Program *p) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 448 | mToDirtyList.push(p); |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 449 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 450 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 451 | void Allocation::removeProgramToDirty(const Program *p) { |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 452 | for (size_t ct=0; ct < mToDirtyList.size(); ct++) { |
| 453 | if (mToDirtyList[ct] == p) { |
| 454 | mToDirtyList.removeAt(ct); |
| 455 | return; |
| 456 | } |
| 457 | } |
| 458 | rsAssert(0); |
| 459 | } |
| 460 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 461 | void Allocation::dumpLOGV(const char *prefix) const { |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 462 | ObjectBase::dumpLOGV(prefix); |
| 463 | |
| 464 | String8 s(prefix); |
| 465 | s.append(" type "); |
| 466 | if (mType.get()) { |
| 467 | mType->dumpLOGV(s.string()); |
| 468 | } |
| 469 | |
| 470 | LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i", |
| 471 | prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead); |
| 472 | |
Jason Sams | 36176536 | 2009-11-23 15:27:33 -0800 | [diff] [blame] | 473 | LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i", |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 474 | prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID); |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 475 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 476 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 477 | void Allocation::serialize(OStream *stream) const { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 478 | // Need to identify ourselves |
| 479 | stream->addU32((uint32_t)getClassId()); |
| 480 | |
| 481 | String8 name(getName()); |
| 482 | stream->addString(&name); |
| 483 | |
| 484 | // First thing we need to serialize is the type object since it will be needed |
| 485 | // to initialize the class |
| 486 | mType->serialize(stream); |
| 487 | |
| 488 | uint32_t dataSize = mType->getSizeBytes(); |
| 489 | // Write how much data we are storing |
| 490 | stream->addU32(dataSize); |
| 491 | // Now write the data |
| 492 | stream->addByteArray(mPtr, dataSize); |
| 493 | } |
| 494 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 495 | Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 496 | // First make sure we are reading the correct object |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 497 | RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 498 | if (classID != RS_A3D_CLASS_ID_ALLOCATION) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 499 | LOGE("allocation loading skipped due to invalid class id\n"); |
| 500 | return NULL; |
| 501 | } |
| 502 | |
| 503 | String8 name; |
| 504 | stream->loadString(&name); |
| 505 | |
| 506 | Type *type = Type::createFromStream(rsc, stream); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 507 | if (!type) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 508 | return NULL; |
| 509 | } |
| 510 | type->compute(); |
| 511 | |
| 512 | // Number of bytes we wrote out for this allocation |
| 513 | uint32_t dataSize = stream->loadU32(); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 514 | if (dataSize != type->getSizeBytes()) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 515 | LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n"); |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 516 | ObjectBase::checkDelete(type); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 517 | return NULL; |
| 518 | } |
| 519 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 520 | Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_ALL); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 521 | alloc->setName(name.string(), name.size()); |
| 522 | |
| 523 | // Read in all of our allocation data |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 524 | alloc->data(rsc, stream->getPtr() + stream->getPos(), dataSize); |
Alex Sakhartchouk | 2ce0e3f | 2010-08-11 10:30:44 -0700 | [diff] [blame] | 525 | stream->reset(stream->getPos() + dataSize); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 526 | |
| 527 | return alloc; |
| 528 | } |
| 529 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 530 | void Allocation::sendDirty() const { |
Jason Sams | 83f1c63 | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 531 | for (size_t ct=0; ct < mToDirtyList.size(); ct++) { |
| 532 | mToDirtyList[ct]->forceDirty(); |
| 533 | } |
| 534 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 535 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 536 | void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const { |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 537 | const uint8_t *p = static_cast<const uint8_t *>(ptr); |
| 538 | const Element *e = mType->getElement(); |
| 539 | uint32_t stride = e->getSizeBytes(); |
| 540 | |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 541 | p += stride * startOff; |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 542 | while (ct > 0) { |
| 543 | e->incRefs(p); |
| 544 | ct --; |
| 545 | p += stride; |
| 546 | } |
| 547 | } |
| 548 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 549 | void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const { |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 550 | const uint8_t *p = static_cast<const uint8_t *>(ptr); |
| 551 | const Element *e = mType->getElement(); |
| 552 | uint32_t stride = e->getSizeBytes(); |
| 553 | |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 554 | p += stride * startOff; |
Jason Sams | b28ca96f | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 555 | while (ct > 0) { |
| 556 | e->decRefs(p); |
| 557 | ct --; |
| 558 | p += stride; |
| 559 | } |
| 560 | } |
| 561 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 562 | void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) { |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 565 | void Allocation::resize1D(Context *rsc, uint32_t dimX) { |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 566 | Type *t = mType->cloneAndResize1D(rsc, dimX); |
| 567 | |
| 568 | uint32_t oldDimX = mType->getDimX(); |
| 569 | if (dimX == oldDimX) { |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | if (dimX < oldDimX) { |
| 574 | decRefs(mPtr, oldDimX - dimX, dimX); |
| 575 | } |
| 576 | mPtr = realloc(mPtr, t->getSizeBytes()); |
| 577 | |
| 578 | if (dimX > oldDimX) { |
| 579 | const Element *e = mType->getElement(); |
| 580 | uint32_t stride = e->getSizeBytes(); |
| 581 | memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX)); |
| 582 | } |
| 583 | mType.set(t); |
| 584 | } |
| 585 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 586 | void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) { |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 587 | LOGE("not implemented"); |
| 588 | } |
| 589 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 590 | ///////////////// |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 591 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 592 | |
| 593 | |
| 594 | namespace android { |
| 595 | namespace renderscript { |
| 596 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 597 | void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 598 | Allocation *alloc = static_cast<Allocation *>(va); |
Jason Sams | c2908e6 | 2010-02-23 17:44:28 -0800 | [diff] [blame] | 599 | alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 602 | void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 603 | Allocation *alloc = static_cast<Allocation *>(va); |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 604 | alloc->deferedUploadToBufferObject(rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 607 | static void mip565(const Adapter2D &out, const Adapter2D &in) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 608 | uint32_t w = out.getDimX(); |
| 609 | uint32_t h = out.getDimY(); |
| 610 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 611 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 612 | uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y)); |
| 613 | const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2)); |
| 614 | const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1)); |
| 615 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 616 | for (uint32_t x=0; x < w; x++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 617 | *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); |
| 618 | oPtr ++; |
| 619 | i1 += 2; |
| 620 | i2 += 2; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 625 | static void mip8888(const Adapter2D &out, const Adapter2D &in) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 626 | uint32_t w = out.getDimX(); |
| 627 | uint32_t h = out.getDimY(); |
| 628 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 629 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 630 | uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y)); |
| 631 | const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2)); |
| 632 | const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1)); |
| 633 | |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 634 | for (uint32_t x=0; x < w; x++) { |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 635 | *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 636 | oPtr ++; |
| 637 | i1 += 2; |
| 638 | i2 += 2; |
| 639 | } |
| 640 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 643 | static void mip8(const Adapter2D &out, const Adapter2D &in) { |
Jason Sams | e20e3b4 | 2010-01-19 17:53:54 -0800 | [diff] [blame] | 644 | uint32_t w = out.getDimX(); |
| 645 | uint32_t h = out.getDimY(); |
| 646 | |
| 647 | for (uint32_t y=0; y < h; y++) { |
| 648 | uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y)); |
| 649 | const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2)); |
| 650 | const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1)); |
| 651 | |
| 652 | for (uint32_t x=0; x < w; x++) { |
| 653 | *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f); |
| 654 | oPtr ++; |
| 655 | i1 += 2; |
| 656 | i2 += 2; |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 661 | static void mip(const Adapter2D &out, const Adapter2D &in) { |
| 662 | switch (out.getBaseType()->getElement()->getSizeBits()) { |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 663 | case 32: |
| 664 | mip8888(out, in); |
| 665 | break; |
| 666 | case 16: |
| 667 | mip565(out, in); |
| 668 | break; |
Jason Sams | e20e3b4 | 2010-01-19 17:53:54 -0800 | [diff] [blame] | 669 | case 8: |
| 670 | mip8(out, in); |
| 671 | break; |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 672 | } |
Jason Sams | 6f5c61c | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 673 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 674 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 675 | typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count); |
| 676 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 677 | static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count) { |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 678 | memcpy(dst, src, count * 2); |
| 679 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 680 | static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count) { |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 681 | memcpy(dst, src, count); |
| 682 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 683 | static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count) { |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 684 | memcpy(dst, src, count * 4); |
| 685 | } |
| 686 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 687 | static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count) { |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 688 | uint16_t *d = static_cast<uint16_t *>(dst); |
| 689 | const uint8_t *s = static_cast<const uint8_t *>(src); |
| 690 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 691 | while (count--) { |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 692 | *d = rs888to565(s[0], s[1], s[2]); |
| 693 | d++; |
| 694 | s+= 3; |
| 695 | } |
| 696 | } |
| 697 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 698 | static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count) { |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 699 | uint16_t *d = static_cast<uint16_t *>(dst); |
| 700 | const uint8_t *s = static_cast<const uint8_t *>(src); |
| 701 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 702 | while (count--) { |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 703 | *d = rs888to565(s[0], s[1], s[2]); |
| 704 | d++; |
| 705 | s+= 4; |
| 706 | } |
| 707 | } |
| 708 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 709 | static ElementConverter_t pickConverter(const Element *dst, const Element *src) { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 710 | GLenum srcGLType = src->getComponent().getGLType(); |
| 711 | GLenum srcGLFmt = src->getComponent().getGLFormat(); |
| 712 | GLenum dstGLType = dst->getComponent().getGLType(); |
| 713 | GLenum dstGLFmt = dst->getComponent().getGLFormat(); |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 714 | |
| 715 | if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) { |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 716 | switch (dst->getSizeBytes()) { |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 717 | case 4: |
| 718 | return elementConverter_cpy_32; |
| 719 | case 2: |
| 720 | return elementConverter_cpy_16; |
| 721 | case 1: |
| 722 | return elementConverter_cpy_8; |
| 723 | } |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 726 | if (srcGLType == GL_UNSIGNED_BYTE && |
| 727 | srcGLFmt == GL_RGB && |
| 728 | dstGLType == GL_UNSIGNED_SHORT_5_6_5 && |
Jason Sams | 0ebd569 | 2010-06-22 17:45:34 -0700 | [diff] [blame] | 729 | dstGLFmt == GL_RGB) { |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 730 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 731 | return elementConverter_888_to_565; |
| 732 | } |
| 733 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 734 | if (srcGLType == GL_UNSIGNED_BYTE && |
| 735 | srcGLFmt == GL_RGBA && |
| 736 | dstGLType == GL_UNSIGNED_SHORT_5_6_5 && |
Jason Sams | 0ebd569 | 2010-06-22 17:45:34 -0700 | [diff] [blame] | 737 | dstGLFmt == GL_RGB) { |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 738 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 739 | return elementConverter_8888_to_565; |
| 740 | } |
| 741 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 742 | LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst); |
Jason Sams | 0ebd569 | 2010-06-22 17:45:34 -0700 | [diff] [blame] | 743 | LOGE("pickConverter, srcGLType = %x, srcGLFmt = %x", srcGLType, srcGLFmt); |
| 744 | LOGE("pickConverter, dstGLType = %x, dstGLFmt = %x", dstGLType, dstGLFmt); |
| 745 | src->dumpLOGV("SRC "); |
| 746 | dst->dumpLOGV("DST "); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 747 | return 0; |
| 748 | } |
| 749 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 750 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
| 751 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 752 | void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) { |
| 753 | Allocation *a = static_cast<Allocation *>(va); |
| 754 | a->syncAll(rsc, src); |
| 755 | } |
| 756 | |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 757 | RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 758 | void *bmp, void *callbackData, |
| 759 | RsBitmapCallback_t callback) { |
Jason Sams | 8a64743f | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 760 | const Type * type = static_cast<const Type *>(vtype); |
| 761 | Allocation * alloc = new Allocation(rsc, type, bmp, callbackData, callback); |
| 762 | alloc->incUserRef(); |
| 763 | return alloc; |
| 764 | } |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 765 | |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 766 | void rsi_AllocationCopyFromBitmap(Context *rsc, RsAllocation va, const void *data, size_t dataLen) { |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 767 | Allocation *texAlloc = static_cast<Allocation *>(va); |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 768 | const Type * t = texAlloc->getType(); |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 769 | |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 770 | uint32_t w = t->getDimX(); |
| 771 | uint32_t h = t->getDimY(); |
| 772 | bool genMips = t->getDimLOD(); |
| 773 | size_t s = w * h * t->getElementSizeBytes(); |
| 774 | if (s != dataLen) { |
| 775 | rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size"); |
| 776 | return; |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 777 | } |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 778 | |
| 779 | memcpy(texAlloc->getPtr(), data, s); |
| 780 | if (genMips) { |
| 781 | Adapter2D adapt(rsc, texAlloc); |
| 782 | Adapter2D adapt2(rsc, texAlloc); |
| 783 | for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) { |
| 784 | adapt.setLOD(lod); |
| 785 | adapt2.setLOD(lod + 1); |
| 786 | mip(adapt2, adapt); |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) { |
| 792 | Allocation *texAlloc = static_cast<Allocation *>(va); |
| 793 | const Type * t = texAlloc->getType(); |
| 794 | |
| 795 | size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes(); |
| 796 | if (s != dataLen) { |
| 797 | rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size"); |
| 798 | return; |
| 799 | } |
| 800 | |
| 801 | memcpy(data, texAlloc->getPtr(), s); |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 804 | 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] | 805 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 806 | a->data(rsc, data, sizeBytes); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 809 | 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] | 810 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 811 | a->subData(rsc, xoff, count, data, sizeBytes); |
| 812 | } |
| 813 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 814 | void rsi_Allocation2DSubElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, const void *data, uint32_t eoff, uint32_t sizeBytes) { |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 815 | Allocation *a = static_cast<Allocation *>(va); |
| 816 | a->subElementData(rsc, x, y, data, eoff, sizeBytes); |
| 817 | } |
| 818 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 819 | void rsi_Allocation1DSubElementData(Context *rsc, RsAllocation va, uint32_t x, const void *data, uint32_t eoff, uint32_t sizeBytes) { |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 820 | Allocation *a = static_cast<Allocation *>(va); |
| 821 | a->subElementData(rsc, x, data, eoff, sizeBytes); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 824 | 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] | 825 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 826 | a->subData(rsc, xoff, yoff, w, h, data, sizeBytes); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 829 | void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) { |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 830 | Allocation *a = static_cast<Allocation *>(va); |
| 831 | a->read(data); |
| 832 | } |
| 833 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 834 | void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) { |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 835 | Allocation *a = static_cast<Allocation *>(va); |
| 836 | a->resize1D(rsc, dimX); |
| 837 | } |
| 838 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 839 | void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) { |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 840 | Allocation *a = static_cast<Allocation *>(va); |
| 841 | a->resize2D(rsc, dimX, dimY); |
| 842 | } |
| 843 | |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 844 | #endif //ANDROID_RS_BUILD_FOR_HOST |
| 845 | |
| 846 | } |
| 847 | } |
| 848 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 849 | const void * rsaAllocationGetType(RsContext con, RsAllocation va) { |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 850 | Allocation *a = static_cast<Allocation *>(va); |
| 851 | a->getType()->incUserRef(); |
| 852 | |
| 853 | return a->getType(); |
| 854 | } |
| 855 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 856 | RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype, |
| 857 | RsAllocationMipmapGenerationControl mips, |
| 858 | uint32_t usages) { |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 859 | Context *rsc = static_cast<Context *>(con); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 860 | Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages); |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 861 | alloc->incUserRef(); |
| 862 | return alloc; |
| 863 | } |
| 864 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 865 | |
| 866 | RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype, |
| 867 | RsAllocationMipmapGenerationControl mips, |
| 868 | const void *data, uint32_t usages) { |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 869 | Context *rsc = static_cast<Context *>(con); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 870 | Type *t = static_cast<Type *>(vtype); |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 871 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 872 | RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages); |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 873 | Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); |
| 874 | if (texAlloc == NULL) { |
| 875 | LOGE("Memory allocation failure"); |
| 876 | return NULL; |
| 877 | } |
| 878 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 879 | memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes()); |
| 880 | if (mips == RS_MIPMAP_FULL) { |
| 881 | Adapter2D adapt(rsc, texAlloc); |
| 882 | Adapter2D adapt2(rsc, texAlloc); |
| 883 | for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) { |
| 884 | adapt.setLOD(lod); |
| 885 | adapt2.setLOD(lod + 1); |
| 886 | mip(adapt2, adapt); |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 887 | } |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | return texAlloc; |
| 891 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 892 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 893 | RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype, |
| 894 | RsAllocationMipmapGenerationControl mips, |
| 895 | const void *data, uint32_t usages) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 896 | Context *rsc = static_cast<Context *>(con); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 897 | Type *t = static_cast<Type *>(vtype); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 898 | |
| 899 | // Cubemap allocation's faces should be Width by Width each. |
| 900 | // Source data should have 6 * Width by Width pixels |
| 901 | // Error checking is done in the java layer |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 902 | RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 903 | Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); |
| 904 | if (texAlloc == NULL) { |
| 905 | LOGE("Memory allocation failure"); |
| 906 | return NULL; |
| 907 | } |
| 908 | |
| 909 | uint8_t *sourcePtr = (uint8_t*)data; |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 910 | for (uint32_t face = 0; face < 6; face ++) { |
| 911 | Adapter2D faceAdapter(rsc, texAlloc); |
| 912 | faceAdapter.setFace(face); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 913 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 914 | size_t cpySize = t->getDimX() * t->getDimX() * t->getElementSizeBytes(); |
| 915 | memcpy(faceAdapter.getElement(0, 0), sourcePtr, cpySize); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 916 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 917 | // Move the data pointer to the next cube face |
| 918 | sourcePtr += cpySize; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 919 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 920 | if (mips == RS_MIPMAP_FULL) { |
| 921 | Adapter2D adapt(rsc, texAlloc); |
| 922 | Adapter2D adapt2(rsc, texAlloc); |
| 923 | adapt.setFace(face); |
| 924 | adapt2.setFace(face); |
| 925 | for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) { |
| 926 | adapt.setLOD(lod); |
| 927 | adapt2.setLOD(lod + 1); |
| 928 | mip(adapt2, adapt); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 929 | } |
| 930 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | return texAlloc; |
| 934 | } |