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