blob: 41c9fe2d016c3f3e1617ac18ed89aa4d7db7135e [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
Jason Samsbcac9342011-01-13 17:38:18 -0800191 upload2DTexture(isFirstUpload);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800192
Jason Samsb89b0b72010-12-13 17:11:21 -0800193 if (!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
194 freeScriptMemory();
195 }
196
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800197 rsc->checkError("Allocation::uploadToTexture");
198}
199
Jason Samsbcac9342011-01-13 17:38:18 -0800200const static GLenum gFaceOrder[] = {
201 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
202 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
203 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
204 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
205 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
206 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
207};
208
Jason Sams236385b2011-01-12 14:53:25 -0800209void Allocation::update2DTexture(const void *ptr, uint32_t xoff, uint32_t yoff,
210 uint32_t lod, RsAllocationCubemapFace face,
211 uint32_t w, uint32_t h) {
212 GLenum type = mType->getElement()->getComponent().getGLType();
213 GLenum format = mType->getElement()->getComponent().getGLFormat();
214 GLenum target = (GLenum)getGLTarget();
Jason Sams185b8b02011-01-16 14:54:28 -0800215 rsAssert(mTextureID);
Jason Sams236385b2011-01-12 14:53:25 -0800216 glBindTexture(target, mTextureID);
217 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsbcac9342011-01-13 17:38:18 -0800218 GLenum t = GL_TEXTURE_2D;
219 if (mType->getDimFaces()) {
220 t = gFaceOrder[face];
221 }
222 glTexSubImage2D(t, lod, xoff, yoff, w, h, format, type, ptr);
Jason Sams236385b2011-01-12 14:53:25 -0800223}
224
Jason Samsbcac9342011-01-13 17:38:18 -0800225void Allocation::upload2DTexture(bool isFirstUpload) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800226 GLenum type = mType->getElement()->getComponent().getGLType();
227 GLenum format = mType->getElement()->getComponent().getGLFormat();
228
Jason Samsb89b0b72010-12-13 17:11:21 -0800229 GLenum target = (GLenum)getGLTarget();
230 glBindTexture(target, mTextureID);
231 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700232
Jason Samsbcac9342011-01-13 17:38:18 -0800233 uint32_t faceCount = 1;
234 if (mType->getDimFaces()) {
235 faceCount = 6;
Jason Sams326e0dd2009-05-22 14:03:28 -0700236 }
Jason Samsb7e83bd2010-12-15 01:41:00 -0800237
Jason Samsbcac9342011-01-13 17:38:18 -0800238 for (uint32_t face = 0; face < faceCount; face ++) {
Jason Samsb89b0b72010-12-13 17:11:21 -0800239 for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
Jason Samsbcac9342011-01-13 17:38:18 -0800240 const uint8_t *p = (const uint8_t *)mPtr;
241 p += mType->getLODFaceOffset(lod, (RsAllocationCubemapFace)face, 0, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800242
Jason Samsbcac9342011-01-13 17:38:18 -0800243 GLenum t = GL_TEXTURE_2D;
244 if (mType->getDimFaces()) {
245 t = gFaceOrder[face];
246 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800247
248 if (isFirstUpload) {
Jason Samsbcac9342011-01-13 17:38:18 -0800249 glTexImage2D(t, lod, format,
250 mType->getLODDimX(lod), mType->getLODDimY(lod),
251 0, format, type, p);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800252 } else {
Jason Samsbcac9342011-01-13 17:38:18 -0800253 glTexSubImage2D(t, lod, 0, 0,
254 mType->getLODDimX(lod), mType->getLODDimY(lod),
255 format, type, p);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800256 }
257 }
258 }
Jason Samsb7e83bd2010-12-15 01:41:00 -0800259
260 if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
261#ifndef ANDROID_RS_BUILD_FOR_HOST
262 glGenerateMipmap(target);
263#endif //ANDROID_RS_BUILD_FOR_HOST
264 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700265}
266
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800267void Allocation::deferedUploadToBufferObject(const Context *rsc) {
Jason Samsebc50192010-12-13 15:32:35 -0800268 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Samscf4c7c92009-12-14 12:57:40 -0800269 mUploadDefered = true;
270}
271
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800272void Allocation::uploadToBufferObject(const Context *rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700273 rsAssert(!mType->getDimY());
274 rsAssert(!mType->getDimZ());
275
Jason Samsebc50192010-12-13 15:32:35 -0800276 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Samscf4c7c92009-12-14 12:57:40 -0800277
Jason Sams326e0dd2009-05-22 14:03:28 -0700278 if (!mBufferID) {
279 glGenBuffers(1, &mBufferID);
280 }
Jason Samscf4c7c92009-12-14 12:57:40 -0800281 if (!mBufferID) {
282 LOGE("Upload to buffer object failed");
283 mUploadDefered = true;
284 return;
285 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800286 GLenum target = (GLenum)getGLTarget();
287 glBindBuffer(target, mBufferID);
288 glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
289 glBindBuffer(target, 0);
Jason Samsc1ed5892010-03-10 17:30:41 -0800290 rsc->checkError("Allocation::uploadToBufferObject");
Jason Sams326e0dd2009-05-22 14:03:28 -0700291}
292
Jason Sams366c9c82010-12-08 16:14:36 -0800293void Allocation::uploadCheck(Context *rsc) {
Jason Samscf4c7c92009-12-14 12:57:40 -0800294 if (mUploadDefered) {
Jason Sams366c9c82010-12-08 16:14:36 -0800295 syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
Jason Samscf4c7c92009-12-14 12:57:40 -0800296 }
297}
298
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800299void Allocation::read(void *data) {
Jason Samse579df42009-08-10 14:55:26 -0700300 memcpy(data, mPtr, mType->getSizeBytes());
301}
302
Jason Sams4b45b892010-12-29 14:31:29 -0800303void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
304 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700305 uint32_t eSize = mType->getElementSizeBytes();
306 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
307 ptr += eSize * xoff;
Jason Sams9397e302009-08-27 20:23:34 -0700308 uint32_t size = count * eSize;
309
310 if (size != sizeBytes) {
311 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Samse12c1c52009-09-27 17:50:38 -0700312 mType->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700313 return;
314 }
Jason Samse3929c92010-08-09 18:13:33 -0700315
316 if (mType->getElement()->getHasReferences()) {
317 incRefs(data, count);
318 decRefs(ptr, count);
319 }
320
Jason Sams9397e302009-08-27 20:23:34 -0700321 memcpy(ptr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700322 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800323 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700324}
325
Jason Sams4b45b892010-12-29 14:31:29 -0800326void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800327 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700328 uint32_t eSize = mType->getElementSizeBytes();
329 uint32_t lineSize = eSize * w;
330 uint32_t destW = mType->getDimX();
331
Jason Samsa2371512011-01-12 13:28:37 -0800332 //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 -0700333
Jason Samsa2371512011-01-12 13:28:37 -0800334 if ((lineSize * h) != sizeBytes) {
335 LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -0700336 rsAssert(!"Allocation::subData called with mismatched size");
337 return;
338 }
339
Jason Samsa2371512011-01-12 13:28:37 -0800340 if (mPtr) {
341 const uint8_t *src = static_cast<const uint8_t *>(data);
342 uint8_t *dst = static_cast<uint8_t *>(mPtr);
Jason Samsbcac9342011-01-13 17:38:18 -0800343 dst += mType->getLODFaceOffset(lod, face, xoff, yoff);
Jason Samsa2371512011-01-12 13:28:37 -0800344
345 //LOGE(" %p %p %i ", dst, src, eSize);
346 for (uint32_t line=yoff; line < (yoff+h); line++) {
347 if (mType->getElement()->getHasReferences()) {
348 incRefs(src, w);
349 decRefs(dst, w);
350 }
351 memcpy(dst, src, lineSize);
352 src += lineSize;
353 dst += destW * eSize;
Jason Samse3929c92010-08-09 18:13:33 -0700354 }
Jason Samsa2371512011-01-12 13:28:37 -0800355 sendDirty();
356 mUploadDefered = true;
357 } else {
Jason Sams236385b2011-01-12 14:53:25 -0800358 update2DTexture(data, xoff, yoff, lod, face, w, h);
Jason Sams326e0dd2009-05-22 14:03:28 -0700359 }
360}
361
Jason Sams236385b2011-01-12 14:53:25 -0800362void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
363 uint32_t lod, RsAllocationCubemapFace face,
364 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700365}
366
Jason Sams4b45b892010-12-29 14:31:29 -0800367void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800368 uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700369 uint32_t eSize = mType->getElementSizeBytes();
370 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
371 ptr += eSize * x;
372
373 if (cIdx >= mType->getElement()->getFieldCount()) {
374 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
375 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
376 return;
377 }
378
379 if (x >= mType->getDimX()) {
380 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
381 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset 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 Sams4b45b892010-12-29 14:31:29 -0800404void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800405 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700406 uint32_t eSize = mType->getElementSizeBytes();
407 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
408 ptr += eSize * (x + y * mType->getDimX());
409
410 if (x >= mType->getDimX()) {
411 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
412 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
413 return;
414 }
415
416 if (y >= mType->getDimY()) {
417 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
418 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
419 return;
420 }
421
422 if (cIdx >= mType->getElement()->getFieldCount()) {
423 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
424 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
425 return;
426 }
427
428 const Element * e = mType->getElement()->getField(cIdx);
429 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
430
431 if (sizeBytes != e->getSizeBytes()) {
432 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
433 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
434 return;
435 }
436
437 if (e->getHasReferences()) {
438 e->incRefs(data);
439 e->decRefs(ptr);
440 }
441
442 memcpy(ptr, data, sizeBytes);
443 sendDirty();
444 mUploadDefered = true;
445}
446
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800447void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700448 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700449}
Jason Sams326e0dd2009-05-22 14:03:28 -0700450
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800451void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700452 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
453 if (mToDirtyList[ct] == p) {
454 mToDirtyList.removeAt(ct);
455 return;
456 }
457 }
458 rsAssert(0);
459}
460
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800461void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800462 ObjectBase::dumpLOGV(prefix);
463
464 String8 s(prefix);
465 s.append(" type ");
466 if (mType.get()) {
467 mType->dumpLOGV(s.string());
468 }
469
470 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
471 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
472
Jason Samsebc50192010-12-13 15:32:35 -0800473 LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
474 prefix, mUsageFlags, mMipmapControl, mTextureID, mBufferID);
Jason Samsc21cf402009-11-17 17:26:46 -0800475}
Jason Sams326e0dd2009-05-22 14:03:28 -0700476
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800477void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700478 // Need to identify ourselves
479 stream->addU32((uint32_t)getClassId());
480
481 String8 name(getName());
482 stream->addString(&name);
483
484 // First thing we need to serialize is the type object since it will be needed
485 // to initialize the class
486 mType->serialize(stream);
487
488 uint32_t dataSize = mType->getSizeBytes();
489 // Write how much data we are storing
490 stream->addU32(dataSize);
491 // Now write the data
492 stream->addByteArray(mPtr, dataSize);
493}
494
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800495Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700496 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700497 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800498 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700499 LOGE("allocation loading skipped due to invalid class id\n");
500 return NULL;
501 }
502
503 String8 name;
504 stream->loadString(&name);
505
506 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800507 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700508 return NULL;
509 }
510 type->compute();
511
512 // Number of bytes we wrote out for this allocation
513 uint32_t dataSize = stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800514 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700515 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Sams225afd32010-10-21 14:06:55 -0700516 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700517 return NULL;
518 }
519
Jason Samsebc50192010-12-13 15:32:35 -0800520 Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700521 alloc->setName(name.string(), name.size());
522
Jason Sams4b45b892010-12-29 14:31:29 -0800523 uint32_t count = dataSize / type->getElementSizeBytes();
524
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700525 // Read in all of our allocation data
Jason Sams4b45b892010-12-29 14:31:29 -0800526 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700527 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700528
529 return alloc;
530}
531
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800532void Allocation::sendDirty() const {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700533 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
534 mToDirtyList[ct]->forceDirty();
535 }
536}
Jason Sams326e0dd2009-05-22 14:03:28 -0700537
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800538void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700539 const uint8_t *p = static_cast<const uint8_t *>(ptr);
540 const Element *e = mType->getElement();
541 uint32_t stride = e->getSizeBytes();
542
Jason Sams96abf812010-10-05 13:32:49 -0700543 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700544 while (ct > 0) {
545 e->incRefs(p);
546 ct --;
547 p += stride;
548 }
549}
550
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800551void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700552 const uint8_t *p = static_cast<const uint8_t *>(ptr);
553 const Element *e = mType->getElement();
554 uint32_t stride = e->getSizeBytes();
555
Jason Sams96abf812010-10-05 13:32:49 -0700556 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700557 while (ct > 0) {
558 e->decRefs(p);
559 ct --;
560 p += stride;
561 }
562}
563
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800564void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700565}
566
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800567void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700568 Type *t = mType->cloneAndResize1D(rsc, dimX);
569
570 uint32_t oldDimX = mType->getDimX();
571 if (dimX == oldDimX) {
572 return;
573 }
574
575 if (dimX < oldDimX) {
576 decRefs(mPtr, oldDimX - dimX, dimX);
577 }
578 mPtr = realloc(mPtr, t->getSizeBytes());
579
580 if (dimX > oldDimX) {
581 const Element *e = mType->getElement();
582 uint32_t stride = e->getSizeBytes();
583 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
584 }
585 mType.set(t);
586}
587
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800588void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700589 LOGE("not implemented");
590}
591
Jason Sams326e0dd2009-05-22 14:03:28 -0700592/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700593//
Jason Sams326e0dd2009-05-22 14:03:28 -0700594
595
596namespace android {
597namespace renderscript {
598
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800599void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700600 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samsb89b0b72010-12-13 17:11:21 -0800601 alloc->deferedUploadToTexture(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700602}
603
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800604void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700605 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samscf4c7c92009-12-14 12:57:40 -0800606 alloc->deferedUploadToBufferObject(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700607}
608
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800609static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700610 uint32_t w = out.getDimX();
611 uint32_t h = out.getDimY();
612
Jason Samse9f5c532009-07-28 17:20:11 -0700613 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700614 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
615 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
616 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
617
Jason Samse9f5c532009-07-28 17:20:11 -0700618 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700619 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
620 oPtr ++;
621 i1 += 2;
622 i2 += 2;
623 }
624 }
625}
626
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800627static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Sams565ac362009-06-03 16:04:54 -0700628 uint32_t w = out.getDimX();
629 uint32_t h = out.getDimY();
630
Jason Samse9f5c532009-07-28 17:20:11 -0700631 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700632 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
633 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
634 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
635
Jason Samse9f5c532009-07-28 17:20:11 -0700636 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700637 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700638 oPtr ++;
639 i1 += 2;
640 i2 += 2;
641 }
642 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700643}
644
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800645static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Sams2f6d8612010-01-19 17:53:54 -0800646 uint32_t w = out.getDimX();
647 uint32_t h = out.getDimY();
648
649 for (uint32_t y=0; y < h; y++) {
650 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
651 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
652 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
653
654 for (uint32_t x=0; x < w; x++) {
655 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
656 oPtr ++;
657 i1 += 2;
658 i2 += 2;
659 }
660 }
661}
662
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800663static void mip(const Adapter2D &out, const Adapter2D &in) {
664 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Samse9f5c532009-07-28 17:20:11 -0700665 case 32:
666 mip8888(out, in);
667 break;
668 case 16:
669 mip565(out, in);
670 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800671 case 8:
672 mip8(out, in);
673 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700674 }
Jason Samse9f5c532009-07-28 17:20:11 -0700675}
Jason Sams326e0dd2009-05-22 14:03:28 -0700676
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700677#ifndef ANDROID_RS_BUILD_FOR_HOST
678
Jason Sams366c9c82010-12-08 16:14:36 -0800679void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
680 Allocation *a = static_cast<Allocation *>(va);
681 a->syncAll(rsc, src);
682}
683
Jason Samsa2371512011-01-12 13:28:37 -0800684void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700685 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsa2371512011-01-12 13:28:37 -0800686 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams837e3882010-12-10 16:03:15 -0800687}
688
689void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
690 Allocation *texAlloc = static_cast<Allocation *>(va);
691 const Type * t = texAlloc->getType();
692
693 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
694 if (s != dataLen) {
695 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
696 return;
697 }
698
699 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700700}
701
Jason Sams4b45b892010-12-29 14:31:29 -0800702void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
703 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700704 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800705 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700706}
707
Jason Sams4b45b892010-12-29 14:31:29 -0800708void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
709 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700710 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800711 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700712}
713
Jason Sams4b45b892010-12-29 14:31:29 -0800714void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
715 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700716 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800717 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700718}
719
Jason Sams4b45b892010-12-29 14:31:29 -0800720void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
721 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700722 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800723 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700724}
725
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800726void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
Jason Samse579df42009-08-10 14:55:26 -0700727 Allocation *a = static_cast<Allocation *>(va);
728 a->read(data);
729}
730
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800731void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700732 Allocation *a = static_cast<Allocation *>(va);
733 a->resize1D(rsc, dimX);
734}
735
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800736void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700737 Allocation *a = static_cast<Allocation *>(va);
738 a->resize2D(rsc, dimX, dimY);
739}
740
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700741#endif //ANDROID_RS_BUILD_FOR_HOST
742
743}
744}
745
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800746static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va) {
747 Context *rsc = static_cast<Context *>(con);
748 Allocation *texAlloc = static_cast<Allocation *>(va);
749 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
750 for (uint32_t face = 0; face < numFaces; face ++) {
751 Adapter2D adapt(rsc, texAlloc);
752 Adapter2D adapt2(rsc, texAlloc);
753 adapt.setFace(face);
754 adapt2.setFace(face);
755 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
756 adapt.setLOD(lod);
757 adapt2.setLOD(lod + 1);
758 mip(adapt2, adapt);
759 }
760 }
761}
762
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800763const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700764 Allocation *a = static_cast<Allocation *>(va);
765 a->getType()->incUserRef();
766
767 return a->getType();
768}
769
Jason Sams366c9c82010-12-08 16:14:36 -0800770RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800771 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800772 uint32_t usages) {
Jason Samsf0c1df42010-10-26 13:09:17 -0700773 Context *rsc = static_cast<Context *>(con);
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -0800774 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages, mips);
Jason Samsf0c1df42010-10-26 13:09:17 -0700775 alloc->incUserRef();
776 return alloc;
777}
778
Jason Sams366c9c82010-12-08 16:14:36 -0800779
780RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800781 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800782 const void *data, uint32_t usages) {
Jason Samsf0c1df42010-10-26 13:09:17 -0700783 Context *rsc = static_cast<Context *>(con);
Jason Sams366c9c82010-12-08 16:14:36 -0800784 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700785
Jason Sams366c9c82010-12-08 16:14:36 -0800786 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages);
Jason Samsf0c1df42010-10-26 13:09:17 -0700787 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
788 if (texAlloc == NULL) {
789 LOGE("Memory allocation failure");
790 return NULL;
791 }
792
Jason Sams366c9c82010-12-08 16:14:36 -0800793 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsebc50192010-12-13 15:32:35 -0800794 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800795 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700796 }
797
Jason Samsb89b0b72010-12-13 17:11:21 -0800798 texAlloc->deferedUploadToTexture(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700799 return texAlloc;
800}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800801
Jason Sams366c9c82010-12-08 16:14:36 -0800802RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800803 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800804 const void *data, uint32_t usages) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800805 Context *rsc = static_cast<Context *>(con);
Jason Sams366c9c82010-12-08 16:14:36 -0800806 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800807
808 // Cubemap allocation's faces should be Width by Width each.
809 // Source data should have 6 * Width by Width pixels
810 // Error checking is done in the java layer
Jason Sams366c9c82010-12-08 16:14:36 -0800811 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800812 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
813 if (texAlloc == NULL) {
814 LOGE("Memory allocation failure");
815 return NULL;
816 }
817
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800818 uint32_t faceSize = t->getDimX();
819 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
820 uint32_t copySize = faceSize * t->getElementSizeBytes();
821
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800822 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800823 for (uint32_t face = 0; face < 6; face ++) {
824 Adapter2D faceAdapter(rsc, texAlloc);
825 faceAdapter.setFace(face);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800826
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800827 for (uint32_t dI = 0; dI < faceSize; dI ++) {
828 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
829 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800830
Jason Sams366c9c82010-12-08 16:14:36 -0800831 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800832 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800833 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800834
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800835 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
836 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800837 }
838
Jason Samsb89b0b72010-12-13 17:11:21 -0800839 texAlloc->deferedUploadToTexture(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800840 return texAlloc;
841}