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