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