blob: cf394c0dc1c74d0b8f53ef5b75c16050c2d349fe [file] [log] [blame]
Jason Sams326e0dd2009-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 Sakhartchoukfb6b6142010-05-21 12:53:13 -070016#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -070017#include "rsContext.h"
18
Jason Sams1aa5a4e2009-06-22 17:15:15 -070019#include <GLES/gl.h>
Jason Sams7fabe1a2010-02-23 17:44:28 -080020#include <GLES2/gl2.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070021#include <GLES/glext.h>
Alex Sakhartchoukfb6b6142010-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 Sams1aa5a4e2009-06-22 17:15:15 -070028
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070029#include "utils/StopWatch.h"
30
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -080031static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va);
32
Jason Sams326e0dd2009-05-22 14:03:28 -070033using namespace android;
34using namespace android::renderscript;
35
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080036Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
37 RsAllocationMipmapControl mc)
38 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080039 init(rsc, type);
40
Jason Sams366c9c82010-12-08 16:14:36 -080041 mUsageFlags = usages;
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080042 mMipmapControl = mc;
Jason Sams366c9c82010-12-08 16:14:36 -080043
Jason Samsb89b0b72010-12-13 17:11:21 -080044 allocScriptMemory();
Jason Sams10e5e572010-08-12 12:44:02 -070045 if (mType->getElement()->getHasReferences()) {
46 memset(mPtr, 0, mType->getSizeBytes());
47 }
Jason Samsfa84da22010-03-01 15:31:04 -080048 if (!mPtr) {
49 LOGE("Allocation::Allocation, alloc failure");
50 }
51}
52
Jason Samsfa84da22010-03-01 15:31:04 -080053
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080054void Allocation::init(Context *rsc, const Type *type) {
Jason Sams326e0dd2009-05-22 14:03:28 -070055 mPtr = NULL;
56
57 mCpuWrite = false;
58 mCpuRead = false;
59 mGpuWrite = false;
60 mGpuRead = false;
61
62 mReadWriteRatio = 0;
63 mUpdateSize = 0;
Jason Samsebc50192010-12-13 15:32:35 -080064 mUsageFlags = 0;
65 mMipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Sams326e0dd2009-05-22 14:03:28 -070066
Jason Sams326e0dd2009-05-22 14:03:28 -070067 mTextureID = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070068 mBufferID = 0;
Jason Samscf4c7c92009-12-14 12:57:40 -080069 mUploadDefered = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070070
Jason Samsfa84da22010-03-01 15:31:04 -080071 mUserBitmapCallback = NULL;
72 mUserBitmapCallbackData = NULL;
73
Jason Sams326e0dd2009-05-22 14:03:28 -070074 mType.set(type);
Jason Samse5ffb872009-08-09 17:01:55 -070075 rsAssert(type);
Jason Samsfa84da22010-03-01 15:31:04 -080076
77 mPtr = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070078}
79
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080080Allocation::~Allocation() {
Jason Samsfa84da22010-03-01 15:31:04 -080081 if (mUserBitmapCallback != NULL) {
82 mUserBitmapCallback(mUserBitmapCallbackData);
Jason Samsb89b0b72010-12-13 17:11:21 -080083 mPtr = NULL;
Jason Samsfa84da22010-03-01 15:31:04 -080084 }
Jason Samsb89b0b72010-12-13 17:11:21 -080085 freeScriptMemory();
Jason Samse402ed32009-11-03 11:25:42 -080086
87 if (mBufferID) {
88 // Causes a SW crash....
89 //LOGV(" mBufferID %i", mBufferID);
90 //glDeleteBuffers(1, &mBufferID);
91 //mBufferID = 0;
92 }
93 if (mTextureID) {
94 glDeleteTextures(1, &mTextureID);
95 mTextureID = 0;
96 }
Jason Sams326e0dd2009-05-22 14:03:28 -070097}
98
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080099void Allocation::setCpuWritable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700100}
101
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800102void Allocation::setGpuWritable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700103}
104
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800105void Allocation::setCpuReadable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700106}
107
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800108void Allocation::setGpuReadable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700109}
110
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800111bool Allocation::fixAllocation() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700112 return false;
113}
114
Jason Samsb89b0b72010-12-13 17:11:21 -0800115void Allocation::deferedUploadToTexture(const Context *rsc) {
Jason Samsebc50192010-12-13 15:32:35 -0800116 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Samscf4c7c92009-12-14 12:57:40 -0800117 mUploadDefered = true;
118}
119
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800120uint32_t Allocation::getGLTarget() const {
Jason Samsebc50192010-12-13 15:32:35 -0800121 if (getIsTexture()) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800122 if (mType->getDimFaces()) {
123 return GL_TEXTURE_CUBE_MAP;
124 } else {
125 return GL_TEXTURE_2D;
126 }
127 }
Jason Samsebc50192010-12-13 15:32:35 -0800128 if (getIsBufferObject()) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800129 return GL_ARRAY_BUFFER;
130 }
131 return 0;
132}
133
Jason Samsb89b0b72010-12-13 17:11:21 -0800134void Allocation::allocScriptMemory() {
135 rsAssert(!mPtr);
136 mPtr = malloc(mType->getSizeBytes());
137}
138
139void Allocation::freeScriptMemory() {
Jason Samsb89b0b72010-12-13 17:11:21 -0800140 if (mPtr) {
141 free(mPtr);
142 mPtr = NULL;
143 }
144}
145
146
Jason Sams366c9c82010-12-08 16:14:36 -0800147void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
148 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
149
Jason Samsebc50192010-12-13 15:32:35 -0800150 if (getIsTexture()) {
Jason Sams366c9c82010-12-08 16:14:36 -0800151 uploadToTexture(rsc);
152 }
Jason Samsebc50192010-12-13 15:32:35 -0800153 if (getIsBufferObject()) {
Jason Sams366c9c82010-12-08 16:14:36 -0800154 uploadToBufferObject(rsc);
155 }
156
157 mUploadDefered = false;
158}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800159
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800160void Allocation::uploadToTexture(const Context *rsc) {
Jason Samscf4c7c92009-12-14 12:57:40 -0800161
Jason Samsebc50192010-12-13 15:32:35 -0800162 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Samsd01d9702009-12-23 14:35:29 -0800163 GLenum type = mType->getElement()->getComponent().getGLType();
164 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Sams565ac362009-06-03 16:04:54 -0700165
166 if (!type || !format) {
167 return;
168 }
169
Jason Samsb89b0b72010-12-13 17:11:21 -0800170 if (!mPtr) {
171 return;
172 }
173
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700174 bool isFirstUpload = false;
175
Jason Sams326e0dd2009-05-22 14:03:28 -0700176 if (!mTextureID) {
177 glGenTextures(1, &mTextureID);
Jason Sams13e26342009-11-24 12:26:35 -0800178
179 if (!mTextureID) {
180 // This should not happen, however, its likely the cause of the
181 // white sqare bug.
182 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
183 LOGE("Upload to texture failed to gen mTextureID");
184 rsc->dumpDebug();
Jason Samscf4c7c92009-12-14 12:57:40 -0800185 mUploadDefered = true;
186 return;
Jason Sams13e26342009-11-24 12:26:35 -0800187 }
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700188 isFirstUpload = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700189 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800190
191 GLenum target = (GLenum)getGLTarget();
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800192 if (target == GL_TEXTURE_2D) {
Jason Samsb89b0b72010-12-13 17:11:21 -0800193 upload2DTexture(isFirstUpload, mPtr);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800194 } else if (target == GL_TEXTURE_CUBE_MAP) {
195 uploadCubeTexture(isFirstUpload);
196 }
197
Jason Samsb89b0b72010-12-13 17:11:21 -0800198 if (!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
199 freeScriptMemory();
200 }
201
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800202 rsc->checkError("Allocation::uploadToTexture");
203}
204
Jason Samsb89b0b72010-12-13 17:11:21 -0800205void Allocation::upload2DTexture(bool isFirstUpload, const void *ptr) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800206 GLenum type = mType->getElement()->getComponent().getGLType();
207 GLenum format = mType->getElement()->getComponent().getGLFormat();
208
Jason Samsb89b0b72010-12-13 17:11:21 -0800209 GLenum target = (GLenum)getGLTarget();
210 glBindTexture(target, mTextureID);
211 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700212
Jason Samsb89b0b72010-12-13 17:11:21 -0800213 for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
214 const uint8_t *p = (const uint8_t *)ptr;
215 p += mType->getLODOffset(lod);
216
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800217 if (isFirstUpload) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700218 glTexImage2D(GL_TEXTURE_2D, lod, format,
Jason Samsb89b0b72010-12-13 17:11:21 -0800219 mType->getLODDimX(lod), mType->getLODDimY(lod),
220 0, format, type, p);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700221 } else {
222 glTexSubImage2D(GL_TEXTURE_2D, lod, 0, 0,
Jason Samsb89b0b72010-12-13 17:11:21 -0800223 mType->getLODDimX(lod), mType->getLODDimY(lod),
224 format, type, p);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700225 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700226 }
Jason Samsb7e83bd2010-12-15 01:41:00 -0800227
228 if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
229#ifndef ANDROID_RS_BUILD_FOR_HOST
230 glGenerateMipmap(target);
231#endif //ANDROID_RS_BUILD_FOR_HOST
232 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800233}
Jason Sams7fabe1a2010-02-23 17:44:28 -0800234
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800235void Allocation::uploadCubeTexture(bool isFirstUpload) {
236 GLenum type = mType->getElement()->getComponent().getGLType();
237 GLenum format = mType->getElement()->getComponent().getGLFormat();
238
Jason Samsb89b0b72010-12-13 17:11:21 -0800239 GLenum target = (GLenum)getGLTarget();
240 glBindTexture(target, mTextureID);
241 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
242
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800243 GLenum faceOrder[] = {
244 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
245 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
246 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
247 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
248 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
249 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
250 };
251
252 Adapter2D adapt(getContext(), this);
253 for (uint32_t face = 0; face < 6; face ++) {
254 adapt.setFace(face);
255
Jason Samsb89b0b72010-12-13 17:11:21 -0800256 for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
257 adapt.setLOD(lod);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800258
259 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
260
261 if (isFirstUpload) {
262 glTexImage2D(faceOrder[face], lod, format,
263 adapt.getDimX(), adapt.getDimY(),
264 0, format, type, ptr);
265 } else {
266 glTexSubImage2D(faceOrder[face], lod, 0, 0,
267 adapt.getDimX(), adapt.getDimY(),
268 format, type, ptr);
269 }
270 }
271 }
Jason Samsb7e83bd2010-12-15 01:41:00 -0800272
273 if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
274#ifndef ANDROID_RS_BUILD_FOR_HOST
275 glGenerateMipmap(target);
276#endif //ANDROID_RS_BUILD_FOR_HOST
277 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700278}
279
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800280void Allocation::deferedUploadToBufferObject(const Context *rsc) {
Jason Samsebc50192010-12-13 15:32:35 -0800281 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Samscf4c7c92009-12-14 12:57:40 -0800282 mUploadDefered = true;
283}
284
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800285void Allocation::uploadToBufferObject(const Context *rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700286 rsAssert(!mType->getDimY());
287 rsAssert(!mType->getDimZ());
288
Jason Samsebc50192010-12-13 15:32:35 -0800289 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Samscf4c7c92009-12-14 12:57:40 -0800290
Jason Sams326e0dd2009-05-22 14:03:28 -0700291 if (!mBufferID) {
292 glGenBuffers(1, &mBufferID);
293 }
Jason Samscf4c7c92009-12-14 12:57:40 -0800294 if (!mBufferID) {
295 LOGE("Upload to buffer object failed");
296 mUploadDefered = true;
297 return;
298 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800299 GLenum target = (GLenum)getGLTarget();
300 glBindBuffer(target, mBufferID);
301 glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
302 glBindBuffer(target, 0);
Jason Samsc1ed5892010-03-10 17:30:41 -0800303 rsc->checkError("Allocation::uploadToBufferObject");
Jason Sams326e0dd2009-05-22 14:03:28 -0700304}
305
Jason Sams366c9c82010-12-08 16:14:36 -0800306void Allocation::uploadCheck(Context *rsc) {
Jason Samscf4c7c92009-12-14 12:57:40 -0800307 if (mUploadDefered) {
Jason Sams366c9c82010-12-08 16:14:36 -0800308 syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
Jason Samscf4c7c92009-12-14 12:57:40 -0800309 }
310}
311
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800312void Allocation::read(void *data) {
Jason Samse579df42009-08-10 14:55:26 -0700313 memcpy(data, mPtr, mType->getSizeBytes());
314}
315
Jason Sams4b45b892010-12-29 14:31:29 -0800316void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
317 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700318 uint32_t eSize = mType->getElementSizeBytes();
319 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
320 ptr += eSize * xoff;
Jason Sams9397e302009-08-27 20:23:34 -0700321 uint32_t size = count * eSize;
322
323 if (size != sizeBytes) {
324 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Samse12c1c52009-09-27 17:50:38 -0700325 mType->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700326 return;
327 }
Jason Samse3929c92010-08-09 18:13:33 -0700328
329 if (mType->getElement()->getHasReferences()) {
330 incRefs(data, count);
331 decRefs(ptr, count);
332 }
333
Jason Sams9397e302009-08-27 20:23:34 -0700334 memcpy(ptr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700335 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800336 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700337}
338
Jason Sams4b45b892010-12-29 14:31:29 -0800339void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800340 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700341 uint32_t eSize = mType->getElementSizeBytes();
342 uint32_t lineSize = eSize * w;
343 uint32_t destW = mType->getDimX();
344
Jason Samsa2371512011-01-12 13:28:37 -0800345 //LOGE("data2d %p, %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -0700346
Jason Samsa2371512011-01-12 13:28:37 -0800347 if ((lineSize * h) != sizeBytes) {
348 LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -0700349 rsAssert(!"Allocation::subData called with mismatched size");
350 return;
351 }
352
Jason Samsa2371512011-01-12 13:28:37 -0800353 if (mPtr) {
354 const uint8_t *src = static_cast<const uint8_t *>(data);
355 uint8_t *dst = static_cast<uint8_t *>(mPtr);
356 dst += mType->getLODOffset(lod, xoff, yoff);
357
358 //LOGE(" %p %p %i ", dst, src, eSize);
359 for (uint32_t line=yoff; line < (yoff+h); line++) {
360 if (mType->getElement()->getHasReferences()) {
361 incRefs(src, w);
362 decRefs(dst, w);
363 }
364 memcpy(dst, src, lineSize);
365 src += lineSize;
366 dst += destW * eSize;
Jason Samse3929c92010-08-09 18:13:33 -0700367 }
Jason Samsa2371512011-01-12 13:28:37 -0800368 sendDirty();
369 mUploadDefered = true;
370 } else {
371 upload2DTexture(false, data);
Jason Sams326e0dd2009-05-22 14:03:28 -0700372 }
373}
374
Jason Sams4b45b892010-12-29 14:31:29 -0800375void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800376 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700377}
378
Jason Sams4b45b892010-12-29 14:31:29 -0800379void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800380 uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700381 uint32_t eSize = mType->getElementSizeBytes();
382 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
383 ptr += eSize * x;
384
385 if (cIdx >= mType->getElement()->getFieldCount()) {
386 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
387 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
388 return;
389 }
390
391 if (x >= mType->getDimX()) {
392 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
393 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
394 return;
395 }
396
397 const Element * e = mType->getElement()->getField(cIdx);
398 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
399
400 if (sizeBytes != e->getSizeBytes()) {
401 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
402 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
403 return;
404 }
405
406 if (e->getHasReferences()) {
407 e->incRefs(data);
408 e->decRefs(ptr);
409 }
410
411 memcpy(ptr, data, sizeBytes);
412 sendDirty();
413 mUploadDefered = true;
414}
415
Jason Sams4b45b892010-12-29 14:31:29 -0800416void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800417 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700418 uint32_t eSize = mType->getElementSizeBytes();
419 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
420 ptr += eSize * (x + y * mType->getDimX());
421
422 if (x >= mType->getDimX()) {
423 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
424 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
425 return;
426 }
427
428 if (y >= mType->getDimY()) {
429 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
430 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
431 return;
432 }
433
434 if (cIdx >= mType->getElement()->getFieldCount()) {
435 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
436 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
437 return;
438 }
439
440 const Element * e = mType->getElement()->getField(cIdx);
441 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
442
443 if (sizeBytes != e->getSizeBytes()) {
444 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
445 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
446 return;
447 }
448
449 if (e->getHasReferences()) {
450 e->incRefs(data);
451 e->decRefs(ptr);
452 }
453
454 memcpy(ptr, data, sizeBytes);
455 sendDirty();
456 mUploadDefered = true;
457}
458
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800459void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700460 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700461}
Jason Sams326e0dd2009-05-22 14:03:28 -0700462
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800463void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700464 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
465 if (mToDirtyList[ct] == p) {
466 mToDirtyList.removeAt(ct);
467 return;
468 }
469 }
470 rsAssert(0);
471}
472
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800473void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800474 ObjectBase::dumpLOGV(prefix);
475
476 String8 s(prefix);
477 s.append(" type ");
478 if (mType.get()) {
479 mType->dumpLOGV(s.string());
480 }
481
482 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
483 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
484
Jason Samsebc50192010-12-13 15:32:35 -0800485 LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
486 prefix, mUsageFlags, mMipmapControl, mTextureID, mBufferID);
Jason Samsc21cf402009-11-17 17:26:46 -0800487}
Jason Sams326e0dd2009-05-22 14:03:28 -0700488
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800489void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700490 // Need to identify ourselves
491 stream->addU32((uint32_t)getClassId());
492
493 String8 name(getName());
494 stream->addString(&name);
495
496 // First thing we need to serialize is the type object since it will be needed
497 // to initialize the class
498 mType->serialize(stream);
499
500 uint32_t dataSize = mType->getSizeBytes();
501 // Write how much data we are storing
502 stream->addU32(dataSize);
503 // Now write the data
504 stream->addByteArray(mPtr, dataSize);
505}
506
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800507Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700508 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700509 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800510 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700511 LOGE("allocation loading skipped due to invalid class id\n");
512 return NULL;
513 }
514
515 String8 name;
516 stream->loadString(&name);
517
518 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800519 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700520 return NULL;
521 }
522 type->compute();
523
524 // Number of bytes we wrote out for this allocation
525 uint32_t dataSize = stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800526 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700527 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Sams225afd32010-10-21 14:06:55 -0700528 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700529 return NULL;
530 }
531
Jason Samsebc50192010-12-13 15:32:35 -0800532 Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700533 alloc->setName(name.string(), name.size());
534
Jason Sams4b45b892010-12-29 14:31:29 -0800535 uint32_t count = dataSize / type->getElementSizeBytes();
536
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700537 // Read in all of our allocation data
Jason Sams4b45b892010-12-29 14:31:29 -0800538 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700539 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700540
541 return alloc;
542}
543
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800544void Allocation::sendDirty() const {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700545 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
546 mToDirtyList[ct]->forceDirty();
547 }
548}
Jason Sams326e0dd2009-05-22 14:03:28 -0700549
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800550void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700551 const uint8_t *p = static_cast<const uint8_t *>(ptr);
552 const Element *e = mType->getElement();
553 uint32_t stride = e->getSizeBytes();
554
Jason Sams96abf812010-10-05 13:32:49 -0700555 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700556 while (ct > 0) {
557 e->incRefs(p);
558 ct --;
559 p += stride;
560 }
561}
562
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800563void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700564 const uint8_t *p = static_cast<const uint8_t *>(ptr);
565 const Element *e = mType->getElement();
566 uint32_t stride = e->getSizeBytes();
567
Jason Sams96abf812010-10-05 13:32:49 -0700568 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700569 while (ct > 0) {
570 e->decRefs(p);
571 ct --;
572 p += stride;
573 }
574}
575
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800576void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700577}
578
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800579void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700580 Type *t = mType->cloneAndResize1D(rsc, dimX);
581
582 uint32_t oldDimX = mType->getDimX();
583 if (dimX == oldDimX) {
584 return;
585 }
586
587 if (dimX < oldDimX) {
588 decRefs(mPtr, oldDimX - dimX, dimX);
589 }
590 mPtr = realloc(mPtr, t->getSizeBytes());
591
592 if (dimX > oldDimX) {
593 const Element *e = mType->getElement();
594 uint32_t stride = e->getSizeBytes();
595 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
596 }
597 mType.set(t);
598}
599
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800600void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700601 LOGE("not implemented");
602}
603
Jason Sams326e0dd2009-05-22 14:03:28 -0700604/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700605//
Jason Sams326e0dd2009-05-22 14:03:28 -0700606
607
608namespace android {
609namespace renderscript {
610
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800611void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700612 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samsb89b0b72010-12-13 17:11:21 -0800613 alloc->deferedUploadToTexture(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700614}
615
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800616void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700617 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samscf4c7c92009-12-14 12:57:40 -0800618 alloc->deferedUploadToBufferObject(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700619}
620
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800621static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700622 uint32_t w = out.getDimX();
623 uint32_t h = out.getDimY();
624
Jason Samse9f5c532009-07-28 17:20:11 -0700625 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700626 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
627 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
628 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
629
Jason Samse9f5c532009-07-28 17:20:11 -0700630 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700631 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
632 oPtr ++;
633 i1 += 2;
634 i2 += 2;
635 }
636 }
637}
638
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800639static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Sams565ac362009-06-03 16:04:54 -0700640 uint32_t w = out.getDimX();
641 uint32_t h = out.getDimY();
642
Jason Samse9f5c532009-07-28 17:20:11 -0700643 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700644 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
645 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
646 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
647
Jason Samse9f5c532009-07-28 17:20:11 -0700648 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700649 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700650 oPtr ++;
651 i1 += 2;
652 i2 += 2;
653 }
654 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700655}
656
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800657static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Sams2f6d8612010-01-19 17:53:54 -0800658 uint32_t w = out.getDimX();
659 uint32_t h = out.getDimY();
660
661 for (uint32_t y=0; y < h; y++) {
662 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
663 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
664 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
665
666 for (uint32_t x=0; x < w; x++) {
667 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
668 oPtr ++;
669 i1 += 2;
670 i2 += 2;
671 }
672 }
673}
674
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800675static void mip(const Adapter2D &out, const Adapter2D &in) {
676 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Samse9f5c532009-07-28 17:20:11 -0700677 case 32:
678 mip8888(out, in);
679 break;
680 case 16:
681 mip565(out, in);
682 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800683 case 8:
684 mip8(out, in);
685 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700686 }
Jason Samse9f5c532009-07-28 17:20:11 -0700687}
Jason Sams326e0dd2009-05-22 14:03:28 -0700688
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700689#ifndef ANDROID_RS_BUILD_FOR_HOST
690
Jason Sams366c9c82010-12-08 16:14:36 -0800691void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
692 Allocation *a = static_cast<Allocation *>(va);
693 a->syncAll(rsc, src);
694}
695
Jason Samsa2371512011-01-12 13:28:37 -0800696void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700697 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsa2371512011-01-12 13:28:37 -0800698 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams837e3882010-12-10 16:03:15 -0800699}
700
701void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
702 Allocation *texAlloc = static_cast<Allocation *>(va);
703 const Type * t = texAlloc->getType();
704
705 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
706 if (s != dataLen) {
707 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
708 return;
709 }
710
711 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700712}
713
Jason Sams4b45b892010-12-29 14:31:29 -0800714void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
715 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700716 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800717 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700718}
719
Jason Sams4b45b892010-12-29 14:31:29 -0800720void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
721 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700722 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800723 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700724}
725
Jason Sams4b45b892010-12-29 14:31:29 -0800726void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
727 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700728 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800729 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700730}
731
Jason Sams4b45b892010-12-29 14:31:29 -0800732void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
733 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700734 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800735 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700736}
737
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800738void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
Jason Samse579df42009-08-10 14:55:26 -0700739 Allocation *a = static_cast<Allocation *>(va);
740 a->read(data);
741}
742
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800743void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700744 Allocation *a = static_cast<Allocation *>(va);
745 a->resize1D(rsc, dimX);
746}
747
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800748void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700749 Allocation *a = static_cast<Allocation *>(va);
750 a->resize2D(rsc, dimX, dimY);
751}
752
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700753#endif //ANDROID_RS_BUILD_FOR_HOST
754
755}
756}
757
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800758static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va) {
759 Context *rsc = static_cast<Context *>(con);
760 Allocation *texAlloc = static_cast<Allocation *>(va);
761 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
762 for (uint32_t face = 0; face < numFaces; face ++) {
763 Adapter2D adapt(rsc, texAlloc);
764 Adapter2D adapt2(rsc, texAlloc);
765 adapt.setFace(face);
766 adapt2.setFace(face);
767 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
768 adapt.setLOD(lod);
769 adapt2.setLOD(lod + 1);
770 mip(adapt2, adapt);
771 }
772 }
773}
774
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800775const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700776 Allocation *a = static_cast<Allocation *>(va);
777 a->getType()->incUserRef();
778
779 return a->getType();
780}
781
Jason Sams366c9c82010-12-08 16:14:36 -0800782RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800783 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800784 uint32_t usages) {
Jason Samsf0c1df42010-10-26 13:09:17 -0700785 Context *rsc = static_cast<Context *>(con);
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -0800786 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages, mips);
Jason Samsf0c1df42010-10-26 13:09:17 -0700787 alloc->incUserRef();
788 return alloc;
789}
790
Jason Sams366c9c82010-12-08 16:14:36 -0800791
792RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800793 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800794 const void *data, uint32_t usages) {
Jason Samsf0c1df42010-10-26 13:09:17 -0700795 Context *rsc = static_cast<Context *>(con);
Jason Sams366c9c82010-12-08 16:14:36 -0800796 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700797
Jason Sams366c9c82010-12-08 16:14:36 -0800798 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages);
Jason Samsf0c1df42010-10-26 13:09:17 -0700799 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
800 if (texAlloc == NULL) {
801 LOGE("Memory allocation failure");
802 return NULL;
803 }
804
Jason Sams366c9c82010-12-08 16:14:36 -0800805 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsebc50192010-12-13 15:32:35 -0800806 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800807 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700808 }
809
Jason Samsb89b0b72010-12-13 17:11:21 -0800810 texAlloc->deferedUploadToTexture(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700811 return texAlloc;
812}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800813
Jason Sams366c9c82010-12-08 16:14:36 -0800814RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800815 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800816 const void *data, uint32_t usages) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800817 Context *rsc = static_cast<Context *>(con);
Jason Sams366c9c82010-12-08 16:14:36 -0800818 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800819
820 // Cubemap allocation's faces should be Width by Width each.
821 // Source data should have 6 * Width by Width pixels
822 // Error checking is done in the java layer
Jason Sams366c9c82010-12-08 16:14:36 -0800823 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800824 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
825 if (texAlloc == NULL) {
826 LOGE("Memory allocation failure");
827 return NULL;
828 }
829
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800830 uint32_t faceSize = t->getDimX();
831 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
832 uint32_t copySize = faceSize * t->getElementSizeBytes();
833
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800834 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800835 for (uint32_t face = 0; face < 6; face ++) {
836 Adapter2D faceAdapter(rsc, texAlloc);
837 faceAdapter.setFace(face);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800838
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800839 for (uint32_t dI = 0; dI < faceSize; dI ++) {
840 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
841 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800842
Jason Sams366c9c82010-12-08 16:14:36 -0800843 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800844 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800845 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800846
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800847 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
848 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800849 }
850
Jason Samsb89b0b72010-12-13 17:11:21 -0800851 texAlloc->deferedUploadToTexture(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800852 return texAlloc;
853}