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