blob: ec03a158871fa7f306158db3b5e2c9ccc5a2cfb0 [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 */
Jason Sams326e0dd2009-05-22 14:03:28 -070016
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080017#include "rsContext.h"
18#ifndef ANDROID_RS_SERIALIZE
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 Sakhartchouk77d9f4b2011-01-31 14:53:24 -080022#endif //ANDROID_RS_SERIALIZE
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070023
Jason Sams326e0dd2009-05-22 14:03:28 -070024using namespace android;
25using namespace android::renderscript;
26
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080027Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
28 RsAllocationMipmapControl mc)
29 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080030 init(rsc, type);
31
Jason Sams366c9c82010-12-08 16:14:36 -080032 mUsageFlags = usages;
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080033 mMipmapControl = mc;
Jason Sams366c9c82010-12-08 16:14:36 -080034
Jason Samsb89b0b72010-12-13 17:11:21 -080035 allocScriptMemory();
Jason Sams10e5e572010-08-12 12:44:02 -070036 if (mType->getElement()->getHasReferences()) {
37 memset(mPtr, 0, mType->getSizeBytes());
38 }
Jason Samsfa84da22010-03-01 15:31:04 -080039 if (!mPtr) {
40 LOGE("Allocation::Allocation, alloc failure");
41 }
42}
43
Jason Samsfa84da22010-03-01 15:31:04 -080044
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080045void Allocation::init(Context *rsc, const Type *type) {
Jason Sams326e0dd2009-05-22 14:03:28 -070046 mPtr = NULL;
47
48 mCpuWrite = false;
49 mCpuRead = false;
50 mGpuWrite = false;
51 mGpuRead = false;
52
53 mReadWriteRatio = 0;
54 mUpdateSize = 0;
Jason Samsebc50192010-12-13 15:32:35 -080055 mUsageFlags = 0;
56 mMipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Sams326e0dd2009-05-22 14:03:28 -070057
Jason Sams326e0dd2009-05-22 14:03:28 -070058 mTextureID = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070059 mBufferID = 0;
Jason Samscf4c7c92009-12-14 12:57:40 -080060 mUploadDefered = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070061
Jason Samsfa84da22010-03-01 15:31:04 -080062 mUserBitmapCallback = NULL;
63 mUserBitmapCallbackData = NULL;
64
Jason Sams326e0dd2009-05-22 14:03:28 -070065 mType.set(type);
Jason Samse5ffb872009-08-09 17:01:55 -070066 rsAssert(type);
Jason Samsfa84da22010-03-01 15:31:04 -080067
68 mPtr = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070069}
70
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080071Allocation::~Allocation() {
Jason Samsfa84da22010-03-01 15:31:04 -080072 if (mUserBitmapCallback != NULL) {
73 mUserBitmapCallback(mUserBitmapCallbackData);
Jason Samsb89b0b72010-12-13 17:11:21 -080074 mPtr = NULL;
Jason Samsfa84da22010-03-01 15:31:04 -080075 }
Jason Samsb89b0b72010-12-13 17:11:21 -080076 freeScriptMemory();
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080077#ifndef ANDROID_RS_SERIALIZE
Jason Samse402ed32009-11-03 11:25:42 -080078 if (mBufferID) {
79 // Causes a SW crash....
80 //LOGV(" mBufferID %i", mBufferID);
81 //glDeleteBuffers(1, &mBufferID);
82 //mBufferID = 0;
83 }
84 if (mTextureID) {
85 glDeleteTextures(1, &mTextureID);
86 mTextureID = 0;
87 }
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080088#endif //ANDROID_RS_SERIALIZE
Jason Sams326e0dd2009-05-22 14:03:28 -070089}
90
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080091void Allocation::setCpuWritable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -070092}
93
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080094void Allocation::setGpuWritable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -070095}
96
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080097void Allocation::setCpuReadable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -070098}
99
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800100void Allocation::setGpuReadable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700101}
102
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800103bool Allocation::fixAllocation() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700104 return false;
105}
106
Jason Samsb89b0b72010-12-13 17:11:21 -0800107void Allocation::deferedUploadToTexture(const Context *rsc) {
Jason Samsebc50192010-12-13 15:32:35 -0800108 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Samscf4c7c92009-12-14 12:57:40 -0800109 mUploadDefered = true;
110}
111
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800112uint32_t Allocation::getGLTarget() const {
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800113#ifndef ANDROID_RS_SERIALIZE
Jason Samsebc50192010-12-13 15:32:35 -0800114 if (getIsTexture()) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800115 if (mType->getDimFaces()) {
116 return GL_TEXTURE_CUBE_MAP;
117 } else {
118 return GL_TEXTURE_2D;
119 }
120 }
Jason Samsebc50192010-12-13 15:32:35 -0800121 if (getIsBufferObject()) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800122 return GL_ARRAY_BUFFER;
123 }
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800124#endif //ANDROID_RS_SERIALIZE
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800125 return 0;
126}
127
Jason Samsb89b0b72010-12-13 17:11:21 -0800128void Allocation::allocScriptMemory() {
129 rsAssert(!mPtr);
130 mPtr = malloc(mType->getSizeBytes());
131}
132
133void Allocation::freeScriptMemory() {
Jason Samsb89b0b72010-12-13 17:11:21 -0800134 if (mPtr) {
135 free(mPtr);
136 mPtr = NULL;
137 }
138}
139
140
Jason Sams366c9c82010-12-08 16:14:36 -0800141void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
142 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
143
Jason Samsebc50192010-12-13 15:32:35 -0800144 if (getIsTexture()) {
Jason Sams366c9c82010-12-08 16:14:36 -0800145 uploadToTexture(rsc);
146 }
Jason Samsebc50192010-12-13 15:32:35 -0800147 if (getIsBufferObject()) {
Jason Sams366c9c82010-12-08 16:14:36 -0800148 uploadToBufferObject(rsc);
149 }
150
151 mUploadDefered = false;
152}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800153
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800154void Allocation::uploadToTexture(const Context *rsc) {
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800155#ifndef ANDROID_RS_SERIALIZE
Jason Samsebc50192010-12-13 15:32:35 -0800156 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Samsd01d9702009-12-23 14:35:29 -0800157 GLenum type = mType->getElement()->getComponent().getGLType();
158 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Sams565ac362009-06-03 16:04:54 -0700159
160 if (!type || !format) {
161 return;
162 }
163
Jason Samsb89b0b72010-12-13 17:11:21 -0800164 if (!mPtr) {
165 return;
166 }
167
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700168 bool isFirstUpload = false;
169
Jason Sams326e0dd2009-05-22 14:03:28 -0700170 if (!mTextureID) {
171 glGenTextures(1, &mTextureID);
Jason Sams13e26342009-11-24 12:26:35 -0800172
173 if (!mTextureID) {
174 // This should not happen, however, its likely the cause of the
175 // white sqare bug.
176 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
177 LOGE("Upload to texture failed to gen mTextureID");
178 rsc->dumpDebug();
Jason Samscf4c7c92009-12-14 12:57:40 -0800179 mUploadDefered = true;
180 return;
Jason Sams13e26342009-11-24 12:26:35 -0800181 }
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700182 isFirstUpload = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700183 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800184
Jason Samsbcac9342011-01-13 17:38:18 -0800185 upload2DTexture(isFirstUpload);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800186
Jason Samsb89b0b72010-12-13 17:11:21 -0800187 if (!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
188 freeScriptMemory();
189 }
190
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800191 rsc->checkError("Allocation::uploadToTexture");
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800192#endif //ANDROID_RS_SERIALIZE
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800193}
194
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800195#ifndef ANDROID_RS_SERIALIZE
Jason Samsbcac9342011-01-13 17:38:18 -0800196const static GLenum gFaceOrder[] = {
197 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
198 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
199 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
200 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
201 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
202 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
203};
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800204#endif //ANDROID_RS_SERIALIZE
Jason Samsbcac9342011-01-13 17:38:18 -0800205
Jason Sams236385b2011-01-12 14:53:25 -0800206void Allocation::update2DTexture(const void *ptr, uint32_t xoff, uint32_t yoff,
207 uint32_t lod, RsAllocationCubemapFace face,
208 uint32_t w, uint32_t h) {
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800209#ifndef ANDROID_RS_SERIALIZE
Jason Sams236385b2011-01-12 14:53:25 -0800210 GLenum type = mType->getElement()->getComponent().getGLType();
211 GLenum format = mType->getElement()->getComponent().getGLFormat();
212 GLenum target = (GLenum)getGLTarget();
Jason Sams185b8b02011-01-16 14:54:28 -0800213 rsAssert(mTextureID);
Jason Sams236385b2011-01-12 14:53:25 -0800214 glBindTexture(target, mTextureID);
215 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsbcac9342011-01-13 17:38:18 -0800216 GLenum t = GL_TEXTURE_2D;
217 if (mType->getDimFaces()) {
218 t = gFaceOrder[face];
219 }
220 glTexSubImage2D(t, lod, xoff, yoff, w, h, format, type, ptr);
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800221#endif //ANDROID_RS_SERIALIZE
Jason Sams236385b2011-01-12 14:53:25 -0800222}
223
Jason Samsbcac9342011-01-13 17:38:18 -0800224void Allocation::upload2DTexture(bool isFirstUpload) {
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800225#ifndef ANDROID_RS_SERIALIZE
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) {
Jason Samsb7e83bd2010-12-15 01:41:00 -0800261 glGenerateMipmap(target);
Jason Samsb7e83bd2010-12-15 01:41:00 -0800262 }
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800263#endif //ANDROID_RS_SERIALIZE
Jason Sams326e0dd2009-05-22 14:03:28 -0700264}
265
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800266void Allocation::deferedUploadToBufferObject(const Context *rsc) {
Jason Samsebc50192010-12-13 15:32:35 -0800267 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Samscf4c7c92009-12-14 12:57:40 -0800268 mUploadDefered = true;
269}
270
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800271void Allocation::uploadToBufferObject(const Context *rsc) {
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800272#ifndef ANDROID_RS_SERIALIZE
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");
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800291#endif //ANDROID_RS_SERIALIZE
Jason Sams326e0dd2009-05-22 14:03:28 -0700292}
293
Jason Sams366c9c82010-12-08 16:14:36 -0800294void Allocation::uploadCheck(Context *rsc) {
Jason Samscf4c7c92009-12-14 12:57:40 -0800295 if (mUploadDefered) {
Jason Sams366c9c82010-12-08 16:14:36 -0800296 syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
Jason Samscf4c7c92009-12-14 12:57:40 -0800297 }
298}
299
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800300void Allocation::read(void *data) {
Jason Samse579df42009-08-10 14:55:26 -0700301 memcpy(data, mPtr, mType->getSizeBytes());
302}
303
Jason Sams4b45b892010-12-29 14:31:29 -0800304void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
305 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700306 uint32_t eSize = mType->getElementSizeBytes();
307 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
308 ptr += eSize * xoff;
Jason Sams9397e302009-08-27 20:23:34 -0700309 uint32_t size = count * eSize;
310
311 if (size != sizeBytes) {
312 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Samse12c1c52009-09-27 17:50:38 -0700313 mType->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700314 return;
315 }
Jason Samse3929c92010-08-09 18:13:33 -0700316
317 if (mType->getElement()->getHasReferences()) {
318 incRefs(data, count);
319 decRefs(ptr, count);
320 }
321
Jason Sams9397e302009-08-27 20:23:34 -0700322 memcpy(ptr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700323 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800324 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700325}
326
Jason Sams4b45b892010-12-29 14:31:29 -0800327void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800328 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700329 uint32_t eSize = mType->getElementSizeBytes();
330 uint32_t lineSize = eSize * w;
331 uint32_t destW = mType->getDimX();
332
Jason Samsa2371512011-01-12 13:28:37 -0800333 //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 -0700334
Jason Samsa2371512011-01-12 13:28:37 -0800335 if ((lineSize * h) != sizeBytes) {
336 LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -0700337 rsAssert(!"Allocation::subData called with mismatched size");
338 return;
339 }
340
Jason Samsa2371512011-01-12 13:28:37 -0800341 if (mPtr) {
342 const uint8_t *src = static_cast<const uint8_t *>(data);
343 uint8_t *dst = static_cast<uint8_t *>(mPtr);
Jason Samsbcac9342011-01-13 17:38:18 -0800344 dst += mType->getLODFaceOffset(lod, face, xoff, yoff);
Jason Samsa2371512011-01-12 13:28:37 -0800345
346 //LOGE(" %p %p %i ", dst, src, eSize);
347 for (uint32_t line=yoff; line < (yoff+h); line++) {
348 if (mType->getElement()->getHasReferences()) {
349 incRefs(src, w);
350 decRefs(dst, w);
351 }
352 memcpy(dst, src, lineSize);
353 src += lineSize;
354 dst += destW * eSize;
Jason Samse3929c92010-08-09 18:13:33 -0700355 }
Jason Samsa2371512011-01-12 13:28:37 -0800356 sendDirty();
357 mUploadDefered = true;
358 } else {
Jason Sams236385b2011-01-12 14:53:25 -0800359 update2DTexture(data, xoff, yoff, lod, face, w, h);
Jason Sams326e0dd2009-05-22 14:03:28 -0700360 }
361}
362
Jason Sams236385b2011-01-12 14:53:25 -0800363void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
364 uint32_t lod, RsAllocationCubemapFace face,
365 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700366}
367
Jason Sams4b45b892010-12-29 14:31:29 -0800368void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800369 uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700370 uint32_t eSize = mType->getElementSizeBytes();
371 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
372 ptr += eSize * x;
373
374 if (cIdx >= mType->getElement()->getFieldCount()) {
375 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
376 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
377 return;
378 }
379
380 if (x >= mType->getDimX()) {
381 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
382 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
383 return;
384 }
385
386 const Element * e = mType->getElement()->getField(cIdx);
387 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
388
389 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800390 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700391 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
392 return;
393 }
394
395 if (e->getHasReferences()) {
396 e->incRefs(data);
397 e->decRefs(ptr);
398 }
399
400 memcpy(ptr, data, sizeBytes);
401 sendDirty();
402 mUploadDefered = true;
403}
404
Jason Sams4b45b892010-12-29 14:31:29 -0800405void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800406 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700407 uint32_t eSize = mType->getElementSizeBytes();
408 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
409 ptr += eSize * (x + y * mType->getDimX());
410
411 if (x >= mType->getDimX()) {
412 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
413 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
414 return;
415 }
416
417 if (y >= mType->getDimY()) {
418 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
419 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
420 return;
421 }
422
423 if (cIdx >= mType->getElement()->getFieldCount()) {
424 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
425 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
426 return;
427 }
428
429 const Element * e = mType->getElement()->getField(cIdx);
430 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
431
432 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800433 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700434 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
435 return;
436 }
437
438 if (e->getHasReferences()) {
439 e->incRefs(data);
440 e->decRefs(ptr);
441 }
442
443 memcpy(ptr, data, sizeBytes);
444 sendDirty();
445 mUploadDefered = true;
446}
447
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800448void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800449#ifndef ANDROID_RS_SERIALIZE
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700450 mToDirtyList.push(p);
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800451#endif //ANDROID_RS_SERIALIZE
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700452}
Jason Sams326e0dd2009-05-22 14:03:28 -0700453
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800454void Allocation::removeProgramToDirty(const Program *p) {
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800455#ifndef ANDROID_RS_SERIALIZE
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700456 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
457 if (mToDirtyList[ct] == p) {
458 mToDirtyList.removeAt(ct);
459 return;
460 }
461 }
462 rsAssert(0);
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800463#endif //ANDROID_RS_SERIALIZE
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700464}
465
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800466void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800467 ObjectBase::dumpLOGV(prefix);
468
469 String8 s(prefix);
470 s.append(" type ");
471 if (mType.get()) {
472 mType->dumpLOGV(s.string());
473 }
474
475 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
476 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
477
Jason Samsebc50192010-12-13 15:32:35 -0800478 LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
479 prefix, mUsageFlags, mMipmapControl, mTextureID, mBufferID);
Jason Samsc21cf402009-11-17 17:26:46 -0800480}
Jason Sams326e0dd2009-05-22 14:03:28 -0700481
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800482void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700483 // Need to identify ourselves
484 stream->addU32((uint32_t)getClassId());
485
486 String8 name(getName());
487 stream->addString(&name);
488
489 // First thing we need to serialize is the type object since it will be needed
490 // to initialize the class
491 mType->serialize(stream);
492
493 uint32_t dataSize = mType->getSizeBytes();
494 // Write how much data we are storing
495 stream->addU32(dataSize);
496 // Now write the data
497 stream->addByteArray(mPtr, dataSize);
498}
499
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800500Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700501 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700502 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800503 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700504 LOGE("allocation loading skipped due to invalid class id\n");
505 return NULL;
506 }
507
508 String8 name;
509 stream->loadString(&name);
510
511 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800512 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700513 return NULL;
514 }
515 type->compute();
516
517 // Number of bytes we wrote out for this allocation
518 uint32_t dataSize = stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800519 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700520 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Sams225afd32010-10-21 14:06:55 -0700521 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700522 return NULL;
523 }
524
Jason Samsebc50192010-12-13 15:32:35 -0800525 Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700526 alloc->setName(name.string(), name.size());
527
Jason Sams4b45b892010-12-29 14:31:29 -0800528 uint32_t count = dataSize / type->getElementSizeBytes();
529
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700530 // Read in all of our allocation data
Jason Sams4b45b892010-12-29 14:31:29 -0800531 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700532 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700533
534 return alloc;
535}
536
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800537void Allocation::sendDirty() const {
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800538#ifndef ANDROID_RS_SERIALIZE
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700539 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
540 mToDirtyList[ct]->forceDirty();
541 }
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800542#endif //ANDROID_RS_SERIALIZE
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700543}
Jason Sams326e0dd2009-05-22 14:03:28 -0700544
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800545void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700546 const uint8_t *p = static_cast<const uint8_t *>(ptr);
547 const Element *e = mType->getElement();
548 uint32_t stride = e->getSizeBytes();
549
Jason Sams96abf812010-10-05 13:32:49 -0700550 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700551 while (ct > 0) {
552 e->incRefs(p);
553 ct --;
554 p += stride;
555 }
556}
557
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800558void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700559 const uint8_t *p = static_cast<const uint8_t *>(ptr);
560 const Element *e = mType->getElement();
561 uint32_t stride = e->getSizeBytes();
562
Jason Sams96abf812010-10-05 13:32:49 -0700563 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700564 while (ct > 0) {
565 e->decRefs(p);
566 ct --;
567 p += stride;
568 }
569}
570
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800571void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700572}
573
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800574void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700575 Type *t = mType->cloneAndResize1D(rsc, dimX);
576
577 uint32_t oldDimX = mType->getDimX();
578 if (dimX == oldDimX) {
579 return;
580 }
581
582 if (dimX < oldDimX) {
583 decRefs(mPtr, oldDimX - dimX, dimX);
584 }
585 mPtr = realloc(mPtr, t->getSizeBytes());
586
587 if (dimX > oldDimX) {
588 const Element *e = mType->getElement();
589 uint32_t stride = e->getSizeBytes();
590 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
591 }
592 mType.set(t);
593}
594
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800595void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700596 LOGE("not implemented");
597}
598
Jason Sams326e0dd2009-05-22 14:03:28 -0700599/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700600//
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800601#ifndef ANDROID_RS_SERIALIZE
Jason Sams326e0dd2009-05-22 14:03:28 -0700602
Stephen Hines6a121812011-03-01 17:34:59 -0800603static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va);
604
Jason Sams326e0dd2009-05-22 14:03:28 -0700605namespace android {
606namespace renderscript {
607
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800608void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700609 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samsb89b0b72010-12-13 17:11:21 -0800610 alloc->deferedUploadToTexture(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700611}
612
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800613void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700614 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samscf4c7c92009-12-14 12:57:40 -0800615 alloc->deferedUploadToBufferObject(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700616}
617
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800618static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700619 uint32_t w = out.getDimX();
620 uint32_t h = out.getDimY();
621
Jason Samse9f5c532009-07-28 17:20:11 -0700622 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700623 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
624 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
625 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
626
Jason Samse9f5c532009-07-28 17:20:11 -0700627 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700628 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
629 oPtr ++;
630 i1 += 2;
631 i2 += 2;
632 }
633 }
634}
635
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800636static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Sams565ac362009-06-03 16:04:54 -0700637 uint32_t w = out.getDimX();
638 uint32_t h = out.getDimY();
639
Jason Samse9f5c532009-07-28 17:20:11 -0700640 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700641 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
642 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
643 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
644
Jason Samse9f5c532009-07-28 17:20:11 -0700645 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700646 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700647 oPtr ++;
648 i1 += 2;
649 i2 += 2;
650 }
651 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700652}
653
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800654static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Sams2f6d8612010-01-19 17:53:54 -0800655 uint32_t w = out.getDimX();
656 uint32_t h = out.getDimY();
657
658 for (uint32_t y=0; y < h; y++) {
659 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
660 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
661 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
662
663 for (uint32_t x=0; x < w; x++) {
664 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
665 oPtr ++;
666 i1 += 2;
667 i2 += 2;
668 }
669 }
670}
671
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800672static void mip(const Adapter2D &out, const Adapter2D &in) {
673 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Samse9f5c532009-07-28 17:20:11 -0700674 case 32:
675 mip8888(out, in);
676 break;
677 case 16:
678 mip565(out, in);
679 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800680 case 8:
681 mip8(out, in);
682 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700683 }
Jason Samse9f5c532009-07-28 17:20:11 -0700684}
Jason Sams326e0dd2009-05-22 14:03:28 -0700685
Jason Sams366c9c82010-12-08 16:14:36 -0800686void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
687 Allocation *a = static_cast<Allocation *>(va);
688 a->syncAll(rsc, src);
Jason Sams09aeb8a2011-01-28 15:49:07 -0800689 a->sendDirty();
Jason Sams366c9c82010-12-08 16:14:36 -0800690}
691
Jason Samsa2371512011-01-12 13:28:37 -0800692void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700693 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsa2371512011-01-12 13:28:37 -0800694 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams837e3882010-12-10 16:03:15 -0800695}
696
697void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
698 Allocation *texAlloc = static_cast<Allocation *>(va);
699 const Type * t = texAlloc->getType();
700
701 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
702 if (s != dataLen) {
703 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
704 return;
705 }
706
707 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700708}
709
Jason Sams4b45b892010-12-29 14:31:29 -0800710void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
711 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700712 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800713 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700714}
715
Jason Sams4b45b892010-12-29 14:31:29 -0800716void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
717 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700718 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800719 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700720}
721
Jason Sams4b45b892010-12-29 14:31:29 -0800722void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
723 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700724 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800725 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700726}
727
Jason Sams4b45b892010-12-29 14:31:29 -0800728void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
729 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700730 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800731 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700732}
733
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800734void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
Jason Samse579df42009-08-10 14:55:26 -0700735 Allocation *a = static_cast<Allocation *>(va);
736 a->read(data);
737}
738
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800739void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700740 Allocation *a = static_cast<Allocation *>(va);
741 a->resize1D(rsc, dimX);
742}
743
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800744void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700745 Allocation *a = static_cast<Allocation *>(va);
746 a->resize2D(rsc, dimX, dimY);
747}
748
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700749}
750}
751
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800752static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va) {
753 Context *rsc = static_cast<Context *>(con);
754 Allocation *texAlloc = static_cast<Allocation *>(va);
755 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
756 for (uint32_t face = 0; face < numFaces; face ++) {
757 Adapter2D adapt(rsc, texAlloc);
758 Adapter2D adapt2(rsc, texAlloc);
759 adapt.setFace(face);
760 adapt2.setFace(face);
761 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
762 adapt.setLOD(lod);
763 adapt2.setLOD(lod + 1);
764 mip(adapt2, adapt);
765 }
766 }
767}
768
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800769const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700770 Allocation *a = static_cast<Allocation *>(va);
771 a->getType()->incUserRef();
772
773 return a->getType();
774}
775
Jason Sams366c9c82010-12-08 16:14:36 -0800776RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800777 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800778 uint32_t usages) {
Jason Samsf0c1df42010-10-26 13:09:17 -0700779 Context *rsc = static_cast<Context *>(con);
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -0800780 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages, mips);
Jason Samsf0c1df42010-10-26 13:09:17 -0700781 alloc->incUserRef();
782 return alloc;
783}
784
Jason Sams366c9c82010-12-08 16:14:36 -0800785
786RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800787 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800788 const void *data, uint32_t usages) {
Jason Samsf0c1df42010-10-26 13:09:17 -0700789 Context *rsc = static_cast<Context *>(con);
Jason Sams366c9c82010-12-08 16:14:36 -0800790 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700791
Jason Sams366c9c82010-12-08 16:14:36 -0800792 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages);
Jason Samsf0c1df42010-10-26 13:09:17 -0700793 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
794 if (texAlloc == NULL) {
795 LOGE("Memory allocation failure");
796 return NULL;
797 }
798
Jason Sams366c9c82010-12-08 16:14:36 -0800799 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsebc50192010-12-13 15:32:35 -0800800 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800801 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700802 }
803
Jason Samsb89b0b72010-12-13 17:11:21 -0800804 texAlloc->deferedUploadToTexture(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700805 return texAlloc;
806}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800807
Jason Sams366c9c82010-12-08 16:14:36 -0800808RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800809 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800810 const void *data, uint32_t usages) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800811 Context *rsc = static_cast<Context *>(con);
Jason Sams366c9c82010-12-08 16:14:36 -0800812 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800813
814 // Cubemap allocation's faces should be Width by Width each.
815 // Source data should have 6 * Width by Width pixels
816 // Error checking is done in the java layer
Jason Sams366c9c82010-12-08 16:14:36 -0800817 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800818 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
819 if (texAlloc == NULL) {
820 LOGE("Memory allocation failure");
821 return NULL;
822 }
823
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800824 uint32_t faceSize = t->getDimX();
825 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
826 uint32_t copySize = faceSize * t->getElementSizeBytes();
827
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800828 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800829 for (uint32_t face = 0; face < 6; face ++) {
830 Adapter2D faceAdapter(rsc, texAlloc);
831 faceAdapter.setFace(face);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800832
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800833 for (uint32_t dI = 0; dI < faceSize; dI ++) {
834 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
835 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800836
Jason Sams366c9c82010-12-08 16:14:36 -0800837 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800838 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800839 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800840
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800841 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
842 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800843 }
844
Jason Samsb89b0b72010-12-13 17:11:21 -0800845 texAlloc->deferedUploadToTexture(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800846 return texAlloc;
847}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800848
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800849#endif //ANDROID_RS_SERIALIZE