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