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