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