blob: 4ade7140d35073bb363e85222911948d41b85ad8 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
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 Sakhartchoukaa7d2882010-05-21 12:53:13 -070016#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsd19f10d2009-05-22 14:03:28 -070017#include "rsContext.h"
18
Jason Sams4b962e52009-06-22 17:15:15 -070019#include <GLES/gl.h>
Jason Samsc2908e62010-02-23 17:44:28 -080020#include <GLES2/gl2.h>
Jason Sams4b962e52009-06-22 17:15:15 -070021#include <GLES/glext.h>
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070022#else
23#include "rsContextHostStub.h"
24
25#include <OpenGL/gl.h>
26#include <OpenGl/glext.h>
27#endif
Jason Sams4b962e52009-06-22 17:15:15 -070028
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -070029#include "utils/StopWatch.h"
30
Jason Samsd19f10d2009-05-22 14:03:28 -070031using namespace android;
32using namespace android::renderscript;
33
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080034Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc) {
Jason Sams8a64743f2010-03-01 15:31:04 -080035 init(rsc, type);
36
37 mPtr = malloc(mType->getSizeBytes());
Jason Samsee734982010-08-12 12:44:02 -070038 if (mType->getElement()->getHasReferences()) {
39 memset(mPtr, 0, mType->getSizeBytes());
40 }
Jason Sams8a64743f2010-03-01 15:31:04 -080041 if (!mPtr) {
42 LOGE("Allocation::Allocation, alloc failure");
43 }
44}
45
46Allocation::Allocation(Context *rsc, const Type *type, void *bmp,
47 void *callbackData, RsBitmapCallback_t callback)
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080048 : ObjectBase(rsc) {
Jason Sams8a64743f2010-03-01 15:31:04 -080049 init(rsc, type);
50
51 mPtr = bmp;
52 mUserBitmapCallback = callback;
53 mUserBitmapCallbackData = callbackData;
54}
55
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080056void Allocation::init(Context *rsc, const Type *type) {
Jason Samsd19f10d2009-05-22 14:03:28 -070057 mPtr = NULL;
58
59 mCpuWrite = false;
60 mCpuRead = false;
61 mGpuWrite = false;
62 mGpuRead = false;
63
64 mReadWriteRatio = 0;
65 mUpdateSize = 0;
66
67 mIsTexture = false;
68 mTextureID = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070069 mIsVertexBuffer = false;
70 mBufferID = 0;
Jason Sams3b7d39b2009-12-14 12:57:40 -080071 mUploadDefered = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070072
Jason Sams8a64743f2010-03-01 15:31:04 -080073 mUserBitmapCallback = NULL;
74 mUserBitmapCallbackData = NULL;
75
Jason Samsd19f10d2009-05-22 14:03:28 -070076 mType.set(type);
Jason Sams1bada8c2009-08-09 17:01:55 -070077 rsAssert(type);
Jason Sams8a64743f2010-03-01 15:31:04 -080078
79 mPtr = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -070080}
81
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080082Allocation::~Allocation() {
Jason Sams8a64743f2010-03-01 15:31:04 -080083 if (mUserBitmapCallback != NULL) {
84 mUserBitmapCallback(mUserBitmapCallbackData);
85 } else {
86 free(mPtr);
87 }
Jason Samsb7a6c432009-11-02 14:25:10 -080088 mPtr = NULL;
Jason Sams9d5e03d2009-11-03 11:25:42 -080089
90 if (mBufferID) {
91 // Causes a SW crash....
92 //LOGV(" mBufferID %i", mBufferID);
93 //glDeleteBuffers(1, &mBufferID);
94 //mBufferID = 0;
95 }
96 if (mTextureID) {
97 glDeleteTextures(1, &mTextureID);
98 mTextureID = 0;
99 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700100}
101
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800102void Allocation::setCpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700103}
104
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800105void Allocation::setGpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700106}
107
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800108void Allocation::setCpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700109}
110
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800111void Allocation::setGpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700112}
113
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800114bool Allocation::fixAllocation() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700115 return false;
116}
117
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800118void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800119 rsAssert(lodOffset < mType->getLODCount());
120 mIsTexture = true;
121 mTextureLOD = lodOffset;
122 mUploadDefered = true;
Jason Samsc2908e62010-02-23 17:44:28 -0800123 mTextureGenMipmap = !mType->getDimLOD() && genMipmap;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800124}
125
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800126uint32_t Allocation::getGLTarget() const {
127 if (mIsTexture) {
128 if (mType->getDimFaces()) {
129 return GL_TEXTURE_CUBE_MAP;
130 } else {
131 return GL_TEXTURE_2D;
132 }
133 }
134 if (mIsVertexBuffer) {
135 return GL_ARRAY_BUFFER;
136 }
137 return 0;
138}
139
140
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800141void Allocation::uploadToTexture(const Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800142
143 mIsTexture = true;
144 if (!rsc->checkDriver()) {
145 mUploadDefered = true;
146 return;
147 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700148
Jason Sams718cd1f2009-12-23 14:35:29 -0800149 GLenum type = mType->getElement()->getComponent().getGLType();
150 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Samse2ae85f2009-06-03 16:04:54 -0700151
152 if (!type || !format) {
153 return;
154 }
155
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700156 bool isFirstUpload = false;
157
Jason Samsd19f10d2009-05-22 14:03:28 -0700158 if (!mTextureID) {
159 glGenTextures(1, &mTextureID);
Jason Sams9dab6672009-11-24 12:26:35 -0800160
161 if (!mTextureID) {
162 // This should not happen, however, its likely the cause of the
163 // white sqare bug.
164 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
165 LOGE("Upload to texture failed to gen mTextureID");
166 rsc->dumpDebug();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800167 mUploadDefered = true;
168 return;
Jason Sams9dab6672009-11-24 12:26:35 -0800169 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700170 isFirstUpload = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700171 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800172
173 GLenum target = (GLenum)getGLTarget();
174 glBindTexture(target, mTextureID);
Jason Sams0e27b5c2009-11-05 12:44:58 -0800175 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -0700176
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800177 if (target == GL_TEXTURE_2D) {
178 upload2DTexture(isFirstUpload);
179 } else if (target == GL_TEXTURE_CUBE_MAP) {
180 uploadCubeTexture(isFirstUpload);
181 }
182
183 if (mTextureGenMipmap) {
184#ifndef ANDROID_RS_BUILD_FOR_HOST
185 glGenerateMipmap(target);
186#endif //ANDROID_RS_BUILD_FOR_HOST
187 }
188
189 rsc->checkError("Allocation::uploadToTexture");
190}
191
192void Allocation::upload2DTexture(bool isFirstUpload) {
193 GLenum type = mType->getElement()->getComponent().getGLType();
194 GLenum format = mType->getElement()->getComponent().getGLFormat();
195
Jason Samsa9e7a052009-09-25 14:51:22 -0700196 Adapter2D adapt(getContext(), this);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800197 for (uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800198 adapt.setLOD(lod+mTextureLOD);
Jason Samsd19f10d2009-05-22 14:03:28 -0700199
200 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800201 if (isFirstUpload) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700202 glTexImage2D(GL_TEXTURE_2D, lod, format,
203 adapt.getDimX(), adapt.getDimY(),
204 0, format, type, ptr);
205 } else {
206 glTexSubImage2D(GL_TEXTURE_2D, lod, 0, 0,
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800207 adapt.getDimX(), adapt.getDimY(),
208 format, type, ptr);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700209 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700210 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800211}
Jason Samsc2908e62010-02-23 17:44:28 -0800212
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800213void Allocation::uploadCubeTexture(bool isFirstUpload) {
214 GLenum type = mType->getElement()->getComponent().getGLType();
215 GLenum format = mType->getElement()->getComponent().getGLFormat();
216
217 GLenum faceOrder[] = {
218 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
219 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
220 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
221 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
222 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
223 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
224 };
225
226 Adapter2D adapt(getContext(), this);
227 for (uint32_t face = 0; face < 6; face ++) {
228 adapt.setFace(face);
229
230 for (uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
231 adapt.setLOD(lod+mTextureLOD);
232
233 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
234
235 if (isFirstUpload) {
236 glTexImage2D(faceOrder[face], lod, format,
237 adapt.getDimX(), adapt.getDimY(),
238 0, format, type, ptr);
239 } else {
240 glTexSubImage2D(faceOrder[face], lod, 0, 0,
241 adapt.getDimX(), adapt.getDimY(),
242 format, type, ptr);
243 }
244 }
245 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700246}
247
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800248void Allocation::deferedUploadToBufferObject(const Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800249 mIsVertexBuffer = true;
250 mUploadDefered = true;
251}
252
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800253void Allocation::uploadToBufferObject(const Context *rsc) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700254 rsAssert(!mType->getDimY());
255 rsAssert(!mType->getDimZ());
256
Jason Sams3b7d39b2009-12-14 12:57:40 -0800257 mIsVertexBuffer = true;
258 if (!rsc->checkDriver()) {
259 mUploadDefered = true;
260 return;
261 }
262
Jason Samsd19f10d2009-05-22 14:03:28 -0700263 if (!mBufferID) {
264 glGenBuffers(1, &mBufferID);
265 }
Jason Sams3b7d39b2009-12-14 12:57:40 -0800266 if (!mBufferID) {
267 LOGE("Upload to buffer object failed");
268 mUploadDefered = true;
269 return;
270 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800271 GLenum target = (GLenum)getGLTarget();
272 glBindBuffer(target, mBufferID);
273 glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
274 glBindBuffer(target, 0);
Jason Samsf4687002010-03-10 17:30:41 -0800275 rsc->checkError("Allocation::uploadToBufferObject");
Jason Samsd19f10d2009-05-22 14:03:28 -0700276}
277
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800278void Allocation::uploadCheck(const Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800279 if (mUploadDefered) {
280 mUploadDefered = false;
281 if (mIsVertexBuffer) {
282 uploadToBufferObject(rsc);
283 }
284 if (mIsTexture) {
285 uploadToTexture(rsc);
286 }
287 }
288}
289
Jason Sams1bada8c2009-08-09 17:01:55 -0700290
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800291void Allocation::data(Context *rsc, const void *data, uint32_t sizeBytes) {
Jason Sams07ae4062009-08-27 20:23:34 -0700292 uint32_t size = mType->getSizeBytes();
293 if (size != sizeBytes) {
294 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
295 return;
296 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700297
298 if (mType->getElement()->getHasReferences()) {
299 incRefs(data, sizeBytes / mType->getElement()->getSizeBytes());
300 decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes());
301 }
302
Jason Sams07ae4062009-08-27 20:23:34 -0700303 memcpy(mPtr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700304 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800305 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700306}
307
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800308void Allocation::read(void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700309 memcpy(data, mPtr, mType->getSizeBytes());
310}
311
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800312void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700313 uint32_t eSize = mType->getElementSizeBytes();
314 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
315 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700316 uint32_t size = count * eSize;
317
318 if (size != sizeBytes) {
319 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Sams3c0dfba2009-09-27 17:50:38 -0700320 mType->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -0700321 return;
322 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700323
324 if (mType->getElement()->getHasReferences()) {
325 incRefs(data, count);
326 decRefs(ptr, count);
327 }
328
Jason Sams07ae4062009-08-27 20:23:34 -0700329 memcpy(ptr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700330 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800331 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700332}
333
Jason Sams49bdaf02010-08-31 13:50:42 -0700334void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800335 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700336 uint32_t eSize = mType->getElementSizeBytes();
337 uint32_t lineSize = eSize * w;
338 uint32_t destW = mType->getDimX();
339
340 const uint8_t *src = static_cast<const uint8_t *>(data);
341 uint8_t *dst = static_cast<uint8_t *>(mPtr);
342 dst += eSize * (xoff + yoff * destW);
Jason Sams07ae4062009-08-27 20:23:34 -0700343
344 if ((lineSize * eSize * h) != sizeBytes) {
345 rsAssert(!"Allocation::subData called with mismatched size");
346 return;
347 }
348
Jason Samsd19f10d2009-05-22 14:03:28 -0700349 for (uint32_t line=yoff; line < (yoff+h); line++) {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700350 if (mType->getElement()->getHasReferences()) {
351 incRefs(src, w);
352 decRefs(dst, w);
353 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700354 memcpy(dst, src, lineSize);
355 src += lineSize;
356 dst += destW * eSize;
357 }
Jason Sams83f1c632009-10-26 15:19:28 -0700358 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800359 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700360}
361
Jason Sams49bdaf02010-08-31 13:50:42 -0700362void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800363 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700364}
365
Jason Sams49bdaf02010-08-31 13:50:42 -0700366void Allocation::subElementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800367 uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700368 uint32_t eSize = mType->getElementSizeBytes();
369 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
370 ptr += eSize * x;
371
372 if (cIdx >= mType->getElement()->getFieldCount()) {
373 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
374 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
375 return;
376 }
377
378 if (x >= mType->getDimX()) {
379 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
380 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
381 return;
382 }
383
384 const Element * e = mType->getElement()->getField(cIdx);
385 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
386
387 if (sizeBytes != e->getSizeBytes()) {
388 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
389 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
390 return;
391 }
392
393 if (e->getHasReferences()) {
394 e->incRefs(data);
395 e->decRefs(ptr);
396 }
397
398 memcpy(ptr, data, sizeBytes);
399 sendDirty();
400 mUploadDefered = true;
401}
402
403void Allocation::subElementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800404 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700405 uint32_t eSize = mType->getElementSizeBytes();
406 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
407 ptr += eSize * (x + y * mType->getDimX());
408
409 if (x >= mType->getDimX()) {
410 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
411 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
412 return;
413 }
414
415 if (y >= mType->getDimY()) {
416 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
417 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
418 return;
419 }
420
421 if (cIdx >= mType->getElement()->getFieldCount()) {
422 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
423 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
424 return;
425 }
426
427 const Element * e = mType->getElement()->getField(cIdx);
428 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
429
430 if (sizeBytes != e->getSizeBytes()) {
431 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
432 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
433 return;
434 }
435
436 if (e->getHasReferences()) {
437 e->incRefs(data);
438 e->decRefs(ptr);
439 }
440
441 memcpy(ptr, data, sizeBytes);
442 sendDirty();
443 mUploadDefered = true;
444}
445
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800446void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700447 mToDirtyList.push(p);
Jason Sams83f1c632009-10-26 15:19:28 -0700448}
Jason Samsd19f10d2009-05-22 14:03:28 -0700449
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800450void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams83f1c632009-10-26 15:19:28 -0700451 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
452 if (mToDirtyList[ct] == p) {
453 mToDirtyList.removeAt(ct);
454 return;
455 }
456 }
457 rsAssert(0);
458}
459
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800460void Allocation::dumpLOGV(const char *prefix) const {
Jason Sams715333b2009-11-17 17:26:46 -0800461 ObjectBase::dumpLOGV(prefix);
462
463 String8 s(prefix);
464 s.append(" type ");
465 if (mType.get()) {
466 mType->dumpLOGV(s.string());
467 }
468
469 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
470 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
471
Jason Sams361765362009-11-23 15:27:33 -0800472 LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i",
Jason Sams715333b2009-11-17 17:26:46 -0800473 prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID);
Jason Sams715333b2009-11-17 17:26:46 -0800474}
Jason Samsd19f10d2009-05-22 14:03:28 -0700475
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800476void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700477 // Need to identify ourselves
478 stream->addU32((uint32_t)getClassId());
479
480 String8 name(getName());
481 stream->addString(&name);
482
483 // First thing we need to serialize is the type object since it will be needed
484 // to initialize the class
485 mType->serialize(stream);
486
487 uint32_t dataSize = mType->getSizeBytes();
488 // Write how much data we are storing
489 stream->addU32(dataSize);
490 // Now write the data
491 stream->addByteArray(mPtr, dataSize);
492}
493
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800494Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700495 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700496 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800497 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700498 LOGE("allocation loading skipped due to invalid class id\n");
499 return NULL;
500 }
501
502 String8 name;
503 stream->loadString(&name);
504
505 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800506 if (!type) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700507 return NULL;
508 }
509 type->compute();
510
511 // Number of bytes we wrote out for this allocation
512 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800513 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700514 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Samsb38d5342010-10-21 14:06:55 -0700515 ObjectBase::checkDelete(type);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700516 return NULL;
517 }
518
519 Allocation *alloc = new Allocation(rsc, type);
520 alloc->setName(name.string(), name.size());
521
522 // Read in all of our allocation data
Jason Sams49bdaf02010-08-31 13:50:42 -0700523 alloc->data(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2ce0e3f2010-08-11 10:30:44 -0700524 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700525
526 return alloc;
527}
528
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800529void Allocation::sendDirty() const {
Jason Sams83f1c632009-10-26 15:19:28 -0700530 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
531 mToDirtyList[ct]->forceDirty();
532 }
533}
Jason Samsd19f10d2009-05-22 14:03:28 -0700534
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800535void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700536 const uint8_t *p = static_cast<const uint8_t *>(ptr);
537 const Element *e = mType->getElement();
538 uint32_t stride = e->getSizeBytes();
539
Jason Sams5edc6082010-10-05 13:32:49 -0700540 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700541 while (ct > 0) {
542 e->incRefs(p);
543 ct --;
544 p += stride;
545 }
546}
547
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800548void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700549 const uint8_t *p = static_cast<const uint8_t *>(ptr);
550 const Element *e = mType->getElement();
551 uint32_t stride = e->getSizeBytes();
552
Jason Sams5edc6082010-10-05 13:32:49 -0700553 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700554 while (ct > 0) {
555 e->decRefs(p);
556 ct --;
557 p += stride;
558 }
559}
560
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800561void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams5edc6082010-10-05 13:32:49 -0700562}
563
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800564void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700565 Type *t = mType->cloneAndResize1D(rsc, dimX);
566
567 uint32_t oldDimX = mType->getDimX();
568 if (dimX == oldDimX) {
569 return;
570 }
571
572 if (dimX < oldDimX) {
573 decRefs(mPtr, oldDimX - dimX, dimX);
574 }
575 mPtr = realloc(mPtr, t->getSizeBytes());
576
577 if (dimX > oldDimX) {
578 const Element *e = mType->getElement();
579 uint32_t stride = e->getSizeBytes();
580 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
581 }
582 mType.set(t);
583}
584
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800585void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700586 LOGE("not implemented");
587}
588
Jason Samsd19f10d2009-05-22 14:03:28 -0700589/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700590//
Jason Samsd19f10d2009-05-22 14:03:28 -0700591
592
593namespace android {
594namespace renderscript {
595
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800596void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700597 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samsc2908e62010-02-23 17:44:28 -0800598 alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel);
Jason Samsd19f10d2009-05-22 14:03:28 -0700599}
600
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800601void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700602 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800603 alloc->deferedUploadToBufferObject(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700604}
605
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800606static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700607 uint32_t w = out.getDimX();
608 uint32_t h = out.getDimY();
609
Jason Sams6f5c61c2009-07-28 17:20:11 -0700610 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700611 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
612 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
613 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
614
Jason Sams6f5c61c2009-07-28 17:20:11 -0700615 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700616 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
617 oPtr ++;
618 i1 += 2;
619 i2 += 2;
620 }
621 }
622}
623
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800624static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700625 uint32_t w = out.getDimX();
626 uint32_t h = out.getDimY();
627
Jason Sams6f5c61c2009-07-28 17:20:11 -0700628 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700629 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
630 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
631 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
632
Jason Sams6f5c61c2009-07-28 17:20:11 -0700633 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700634 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700635 oPtr ++;
636 i1 += 2;
637 i2 += 2;
638 }
639 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700640}
641
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800642static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Samse20e3b42010-01-19 17:53:54 -0800643 uint32_t w = out.getDimX();
644 uint32_t h = out.getDimY();
645
646 for (uint32_t y=0; y < h; y++) {
647 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
648 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
649 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
650
651 for (uint32_t x=0; x < w; x++) {
652 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
653 oPtr ++;
654 i1 += 2;
655 i2 += 2;
656 }
657 }
658}
659
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800660static void mip(const Adapter2D &out, const Adapter2D &in) {
661 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Sams6f5c61c2009-07-28 17:20:11 -0700662 case 32:
663 mip8888(out, in);
664 break;
665 case 16:
666 mip565(out, in);
667 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800668 case 8:
669 mip8(out, in);
670 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700671 }
Jason Sams6f5c61c2009-07-28 17:20:11 -0700672}
Jason Samsd19f10d2009-05-22 14:03:28 -0700673
Jason Samsfe08d992009-05-27 14:45:32 -0700674typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
675
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800676static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count) {
Jason Samsfe08d992009-05-27 14:45:32 -0700677 memcpy(dst, src, count * 2);
678}
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800679static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count) {
Jason Samsfe08d992009-05-27 14:45:32 -0700680 memcpy(dst, src, count);
681}
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800682static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count) {
Jason Samsfe08d992009-05-27 14:45:32 -0700683 memcpy(dst, src, count * 4);
684}
685
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800686static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count) {
Jason Samsfe08d992009-05-27 14:45:32 -0700687 uint16_t *d = static_cast<uint16_t *>(dst);
688 const uint8_t *s = static_cast<const uint8_t *>(src);
689
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800690 while (count--) {
Jason Samsfe08d992009-05-27 14:45:32 -0700691 *d = rs888to565(s[0], s[1], s[2]);
692 d++;
693 s+= 3;
694 }
695}
696
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800697static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count) {
Jason Samsfe08d992009-05-27 14:45:32 -0700698 uint16_t *d = static_cast<uint16_t *>(dst);
699 const uint8_t *s = static_cast<const uint8_t *>(src);
700
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800701 while (count--) {
Jason Samsfe08d992009-05-27 14:45:32 -0700702 *d = rs888to565(s[0], s[1], s[2]);
703 d++;
704 s+= 4;
705 }
706}
707
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800708static ElementConverter_t pickConverter(const Element *dst, const Element *src) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800709 GLenum srcGLType = src->getComponent().getGLType();
710 GLenum srcGLFmt = src->getComponent().getGLFormat();
711 GLenum dstGLType = dst->getComponent().getGLType();
712 GLenum dstGLFmt = dst->getComponent().getGLFormat();
Jason Samsea84a7c2009-09-04 14:42:41 -0700713
714 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800715 switch (dst->getSizeBytes()) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700716 case 4:
717 return elementConverter_cpy_32;
718 case 2:
719 return elementConverter_cpy_16;
720 case 1:
721 return elementConverter_cpy_8;
722 }
Jason Samsfe08d992009-05-27 14:45:32 -0700723 }
724
Jason Samsea84a7c2009-09-04 14:42:41 -0700725 if (srcGLType == GL_UNSIGNED_BYTE &&
726 srcGLFmt == GL_RGB &&
727 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
Jason Sams0ebd5692010-06-22 17:45:34 -0700728 dstGLFmt == GL_RGB) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700729
Jason Samsfe08d992009-05-27 14:45:32 -0700730 return elementConverter_888_to_565;
731 }
732
Jason Samsea84a7c2009-09-04 14:42:41 -0700733 if (srcGLType == GL_UNSIGNED_BYTE &&
734 srcGLFmt == GL_RGBA &&
735 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
Jason Sams0ebd5692010-06-22 17:45:34 -0700736 dstGLFmt == GL_RGB) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700737
Jason Samsfe08d992009-05-27 14:45:32 -0700738 return elementConverter_8888_to_565;
739 }
740
Jason Samsea84a7c2009-09-04 14:42:41 -0700741 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst);
Jason Sams0ebd5692010-06-22 17:45:34 -0700742 LOGE("pickConverter, srcGLType = %x, srcGLFmt = %x", srcGLType, srcGLFmt);
743 LOGE("pickConverter, dstGLType = %x, dstGLFmt = %x", dstGLType, dstGLFmt);
744 src->dumpLOGV("SRC ");
745 dst->dumpLOGV("DST ");
Jason Samsfe08d992009-05-27 14:45:32 -0700746 return 0;
747}
748
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700749#ifndef ANDROID_RS_BUILD_FOR_HOST
750
Jason Sams8a64743f2010-03-01 15:31:04 -0800751RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800752 void *bmp, void *callbackData,
753 RsBitmapCallback_t callback) {
Jason Sams8a64743f2010-03-01 15:31:04 -0800754 const Type * type = static_cast<const Type *>(vtype);
755 Allocation * alloc = new Allocation(rsc, type, bmp, callbackData, callback);
756 alloc->incUserRef();
757 return alloc;
758}
Jason Samsfe08d992009-05-27 14:45:32 -0700759
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800760void rsi_AllocationUpdateFromBitmap(Context *rsc, RsAllocation va,
761 RsElement _src, const void *data) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700762 Allocation *texAlloc = static_cast<Allocation *>(va);
763 const Element *src = static_cast<const Element *>(_src);
764 const Element *dst = texAlloc->getType()->getElement();
765 uint32_t w = texAlloc->getType()->getDimX();
766 uint32_t h = texAlloc->getType()->getDimY();
767 bool genMips = texAlloc->getType()->getDimLOD();
768
769 ElementConverter_t cvt = pickConverter(dst, src);
770 if (cvt) {
771 cvt(texAlloc->getPtr(), data, w * h);
772 if (genMips) {
773 Adapter2D adapt(rsc, texAlloc);
774 Adapter2D adapt2(rsc, texAlloc);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800775 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700776 adapt.setLOD(lod);
777 adapt2.setLOD(lod + 1);
778 mip(adapt2, adapt);
779 }
780 }
781 } else {
782 rsc->setError(RS_ERROR_BAD_VALUE, "Unsupported bitmap format");
783 }
784}
785
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800786void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700787 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700788 a->data(rsc, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700789}
790
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800791void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700792 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700793 a->subData(rsc, xoff, count, data, sizeBytes);
794}
795
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800796void rsi_Allocation2DSubElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700797 Allocation *a = static_cast<Allocation *>(va);
798 a->subElementData(rsc, x, y, data, eoff, sizeBytes);
799}
800
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800801void rsi_Allocation1DSubElementData(Context *rsc, RsAllocation va, uint32_t x, const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700802 Allocation *a = static_cast<Allocation *>(va);
803 a->subElementData(rsc, x, data, eoff, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700804}
805
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800806void 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 Samsd19f10d2009-05-22 14:03:28 -0700807 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700808 a->subData(rsc, xoff, yoff, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700809}
810
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800811void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700812 Allocation *a = static_cast<Allocation *>(va);
813 a->read(data);
814}
815
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800816void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700817 Allocation *a = static_cast<Allocation *>(va);
818 a->resize1D(rsc, dimX);
819}
820
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800821void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700822 Allocation *a = static_cast<Allocation *>(va);
823 a->resize2D(rsc, dimX, dimY);
824}
825
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700826#endif //ANDROID_RS_BUILD_FOR_HOST
827
828}
829}
830
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800831const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700832 Allocation *a = static_cast<Allocation *>(va);
833 a->getType()->incUserRef();
834
835 return a->getType();
836}
837
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800838RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype) {
Jason Sams31a7e422010-10-26 13:09:17 -0700839 Context *rsc = static_cast<Context *>(con);
840 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype));
841 alloc->incUserRef();
842 return alloc;
843}
844
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800845RsAllocation rsaAllocationCreateFromBitmap(RsContext con, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data) {
Jason Sams31a7e422010-10-26 13:09:17 -0700846 Context *rsc = static_cast<Context *>(con);
847 const Element *src = static_cast<const Element *>(_src);
848 const Element *dst = static_cast<const Element *>(_dst);
849
850 //LOGE("%p rsi_AllocationCreateFromBitmap %i %i %i", rsc, w, h, genMips);
851 RsDimension dims[] = {RS_DIMENSION_X, RS_DIMENSION_Y, RS_DIMENSION_LOD};
852 uint32_t dimValues[] = {w, h, genMips};
853 RsType type = rsaTypeCreate(rsc, _dst, 3, dims, dimValues);
854
855 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, type);
856 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
857 if (texAlloc == NULL) {
858 LOGE("Memory allocation failure");
859 return NULL;
860 }
861
862 ElementConverter_t cvt = pickConverter(dst, src);
863 if (cvt) {
864 cvt(texAlloc->getPtr(), data, w * h);
865 if (genMips) {
866 Adapter2D adapt(rsc, texAlloc);
867 Adapter2D adapt2(rsc, texAlloc);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800868 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
Jason Sams31a7e422010-10-26 13:09:17 -0700869 adapt.setLOD(lod);
870 adapt2.setLOD(lod + 1);
871 mip(adapt2, adapt);
872 }
873 }
874 } else {
875 rsc->setError(RS_ERROR_BAD_VALUE, "Unsupported bitmap format");
Jason Sams06d69de2010-11-09 17:11:40 -0800876 delete texAlloc;
877 return NULL;
Jason Sams31a7e422010-10-26 13:09:17 -0700878 }
879
880 return texAlloc;
881}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800882
883RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data) {
884 Context *rsc = static_cast<Context *>(con);
885 const Element *src = static_cast<const Element *>(_src);
886 const Element *dst = static_cast<const Element *>(_dst);
887
888 // Cubemap allocation's faces should be Width by Width each.
889 // Source data should have 6 * Width by Width pixels
890 // Error checking is done in the java layer
891 RsDimension dims[] = {RS_DIMENSION_X, RS_DIMENSION_Y, RS_DIMENSION_LOD, RS_DIMENSION_FACE};
892 uint32_t dimValues[] = {w, w, genMips, true};
893 RsType type = rsaTypeCreate(rsc, _dst, 4, dims, dimValues);
894
895 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, type);
896 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
897 if (texAlloc == NULL) {
898 LOGE("Memory allocation failure");
899 return NULL;
900 }
901
902 uint8_t *sourcePtr = (uint8_t*)data;
903 ElementConverter_t cvt = pickConverter(dst, src);
904 if (cvt) {
905 for (uint32_t face = 0; face < 6; face ++) {
906 Adapter2D faceAdapter(rsc, texAlloc);
907 faceAdapter.setFace(face);
908
909 cvt(faceAdapter.getElement(0, 0), sourcePtr, w * w);
910
911 // Move the data pointer to the next cube face
912 sourcePtr += w * w * src->getSizeBytes();
913
914 if (genMips) {
915 Adapter2D adapt(rsc, texAlloc);
916 Adapter2D adapt2(rsc, texAlloc);
917 adapt.setFace(face);
918 adapt2.setFace(face);
919 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
920 adapt.setLOD(lod);
921 adapt2.setLOD(lod + 1);
922 mip(adapt2, adapt);
923 }
924 }
925 }
926 } else {
927 rsc->setError(RS_ERROR_BAD_VALUE, "Unsupported bitmap format");
928 delete texAlloc;
929 return NULL;
930 }
931
932 return texAlloc;
933}