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