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