blob: 54dcbcbefc3041a9a7ff2ec050389e72a56e811c [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jason Samsd19f10d2009-05-22 14:03:28 -070016
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080017#include "rsContext.h"
18#ifndef ANDROID_RS_SERIALIZE
Jason Sams4b962e52009-06-22 17:15:15 -070019#include <GLES/gl.h>
Jason Samsc2908e62010-02-23 17:44:28 -080020#include <GLES2/gl2.h>
Jason Sams4b962e52009-06-22 17:15:15 -070021#include <GLES/glext.h>
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080022#endif //ANDROID_RS_SERIALIZE
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -070023
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -080024static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va);
25
Jason Samsd19f10d2009-05-22 14:03:28 -070026using namespace android;
27using namespace android::renderscript;
28
Alex Sakhartchouk08571962010-12-15 09:59:58 -080029Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
30 RsAllocationMipmapControl mc)
31 : ObjectBase(rsc) {
Jason Sams8a64743f2010-03-01 15:31:04 -080032 init(rsc, type);
33
Jason Sams5476b452010-12-08 16:14:36 -080034 mUsageFlags = usages;
Alex Sakhartchouk08571962010-12-15 09:59:58 -080035 mMipmapControl = mc;
Jason Sams5476b452010-12-08 16:14:36 -080036
Jason Sams5e0035a2010-12-13 17:11:21 -080037 allocScriptMemory();
Jason Samsee734982010-08-12 12:44:02 -070038 if (mType->getElement()->getHasReferences()) {
39 memset(mPtr, 0, mType->getSizeBytes());
40 }
Jason Sams8a64743f2010-03-01 15:31:04 -080041 if (!mPtr) {
42 LOGE("Allocation::Allocation, alloc failure");
43 }
44}
45
Jason Sams8a64743f2010-03-01 15:31:04 -080046
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080047void Allocation::init(Context *rsc, const Type *type) {
Jason Samsd19f10d2009-05-22 14:03:28 -070048 mPtr = NULL;
49
50 mCpuWrite = false;
51 mCpuRead = false;
52 mGpuWrite = false;
53 mGpuRead = false;
54
55 mReadWriteRatio = 0;
56 mUpdateSize = 0;
Jason Samsd4b23b52010-12-13 15:32:35 -080057 mUsageFlags = 0;
58 mMipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsd19f10d2009-05-22 14:03:28 -070059
Jason Samsd19f10d2009-05-22 14:03:28 -070060 mTextureID = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070061 mBufferID = 0;
Jason Sams3b7d39b2009-12-14 12:57:40 -080062 mUploadDefered = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070063
Jason Sams8a64743f2010-03-01 15:31:04 -080064 mUserBitmapCallback = NULL;
65 mUserBitmapCallbackData = NULL;
66
Jason Samsd19f10d2009-05-22 14:03:28 -070067 mType.set(type);
Jason Sams1bada8c2009-08-09 17:01:55 -070068 rsAssert(type);
Jason Sams8a64743f2010-03-01 15:31:04 -080069
70 mPtr = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -070071}
72
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080073Allocation::~Allocation() {
Jason Sams8a64743f2010-03-01 15:31:04 -080074 if (mUserBitmapCallback != NULL) {
75 mUserBitmapCallback(mUserBitmapCallbackData);
Jason Sams5e0035a2010-12-13 17:11:21 -080076 mPtr = NULL;
Jason Sams8a64743f2010-03-01 15:31:04 -080077 }
Jason Sams5e0035a2010-12-13 17:11:21 -080078 freeScriptMemory();
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080079#ifndef ANDROID_RS_SERIALIZE
Jason Sams9d5e03d2009-11-03 11:25:42 -080080 if (mBufferID) {
81 // Causes a SW crash....
82 //LOGV(" mBufferID %i", mBufferID);
83 //glDeleteBuffers(1, &mBufferID);
84 //mBufferID = 0;
85 }
86 if (mTextureID) {
87 glDeleteTextures(1, &mTextureID);
88 mTextureID = 0;
89 }
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080090#endif //ANDROID_RS_SERIALIZE
Jason Samsd19f10d2009-05-22 14:03:28 -070091}
92
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080093void Allocation::setCpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -070094}
95
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080096void Allocation::setGpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -070097}
98
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080099void Allocation::setCpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700100}
101
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800102void Allocation::setGpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700103}
104
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800105bool Allocation::fixAllocation() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700106 return false;
107}
108
Jason Sams5e0035a2010-12-13 17:11:21 -0800109void Allocation::deferedUploadToTexture(const Context *rsc) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800110 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800111 mUploadDefered = true;
112}
113
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800114uint32_t Allocation::getGLTarget() const {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800115#ifndef ANDROID_RS_SERIALIZE
Jason Samsd4b23b52010-12-13 15:32:35 -0800116 if (getIsTexture()) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800117 if (mType->getDimFaces()) {
118 return GL_TEXTURE_CUBE_MAP;
119 } else {
120 return GL_TEXTURE_2D;
121 }
122 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800123 if (getIsBufferObject()) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800124 return GL_ARRAY_BUFFER;
125 }
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800126#endif //ANDROID_RS_SERIALIZE
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800127 return 0;
128}
129
Jason Sams5e0035a2010-12-13 17:11:21 -0800130void Allocation::allocScriptMemory() {
131 rsAssert(!mPtr);
132 mPtr = malloc(mType->getSizeBytes());
133}
134
135void Allocation::freeScriptMemory() {
Jason Sams5e0035a2010-12-13 17:11:21 -0800136 if (mPtr) {
137 free(mPtr);
138 mPtr = NULL;
139 }
140}
141
142
Jason Sams5476b452010-12-08 16:14:36 -0800143void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
144 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
145
Jason Samsd4b23b52010-12-13 15:32:35 -0800146 if (getIsTexture()) {
Jason Sams5476b452010-12-08 16:14:36 -0800147 uploadToTexture(rsc);
148 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800149 if (getIsBufferObject()) {
Jason Sams5476b452010-12-08 16:14:36 -0800150 uploadToBufferObject(rsc);
151 }
152
153 mUploadDefered = false;
154}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800155
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800156void Allocation::uploadToTexture(const Context *rsc) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800157#ifndef ANDROID_RS_SERIALIZE
Jason Samsd4b23b52010-12-13 15:32:35 -0800158 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Sams718cd1f2009-12-23 14:35:29 -0800159 GLenum type = mType->getElement()->getComponent().getGLType();
160 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Samse2ae85f2009-06-03 16:04:54 -0700161
162 if (!type || !format) {
163 return;
164 }
165
Jason Sams5e0035a2010-12-13 17:11:21 -0800166 if (!mPtr) {
167 return;
168 }
169
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700170 bool isFirstUpload = false;
171
Jason Samsd19f10d2009-05-22 14:03:28 -0700172 if (!mTextureID) {
173 glGenTextures(1, &mTextureID);
Jason Sams9dab6672009-11-24 12:26:35 -0800174
175 if (!mTextureID) {
176 // This should not happen, however, its likely the cause of the
177 // white sqare bug.
178 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
179 LOGE("Upload to texture failed to gen mTextureID");
180 rsc->dumpDebug();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800181 mUploadDefered = true;
182 return;
Jason Sams9dab6672009-11-24 12:26:35 -0800183 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700184 isFirstUpload = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700185 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800186
Jason Samsef70a202011-01-13 17:38:18 -0800187 upload2DTexture(isFirstUpload);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800188
Jason Sams5e0035a2010-12-13 17:11:21 -0800189 if (!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
190 freeScriptMemory();
191 }
192
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800193 rsc->checkError("Allocation::uploadToTexture");
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800194#endif //ANDROID_RS_SERIALIZE
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800195}
196
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800197#ifndef ANDROID_RS_SERIALIZE
Jason Samsef70a202011-01-13 17:38:18 -0800198const static GLenum gFaceOrder[] = {
199 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
200 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
201 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
202 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
203 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
204 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
205};
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800206#endif //ANDROID_RS_SERIALIZE
Jason Samsef70a202011-01-13 17:38:18 -0800207
Jason Samsfb9f82c2011-01-12 14:53:25 -0800208void Allocation::update2DTexture(const void *ptr, uint32_t xoff, uint32_t yoff,
209 uint32_t lod, RsAllocationCubemapFace face,
210 uint32_t w, uint32_t h) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800211#ifndef ANDROID_RS_SERIALIZE
Jason Samsfb9f82c2011-01-12 14:53:25 -0800212 GLenum type = mType->getElement()->getComponent().getGLType();
213 GLenum format = mType->getElement()->getComponent().getGLFormat();
214 GLenum target = (GLenum)getGLTarget();
Jason Sams300406a2011-01-16 14:54:28 -0800215 rsAssert(mTextureID);
Jason Samsfb9f82c2011-01-12 14:53:25 -0800216 glBindTexture(target, mTextureID);
217 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsef70a202011-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);
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800223#endif //ANDROID_RS_SERIALIZE
Jason Samsfb9f82c2011-01-12 14:53:25 -0800224}
225
Jason Samsef70a202011-01-13 17:38:18 -0800226void Allocation::upload2DTexture(bool isFirstUpload) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800227#ifndef ANDROID_RS_SERIALIZE
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800228 GLenum type = mType->getElement()->getComponent().getGLType();
229 GLenum format = mType->getElement()->getComponent().getGLFormat();
230
Jason Sams5e0035a2010-12-13 17:11:21 -0800231 GLenum target = (GLenum)getGLTarget();
232 glBindTexture(target, mTextureID);
233 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -0700234
Jason Samsef70a202011-01-13 17:38:18 -0800235 uint32_t faceCount = 1;
236 if (mType->getDimFaces()) {
237 faceCount = 6;
Jason Samsd19f10d2009-05-22 14:03:28 -0700238 }
Jason Sams6d8eb262010-12-15 01:41:00 -0800239
Jason Samsef70a202011-01-13 17:38:18 -0800240 for (uint32_t face = 0; face < faceCount; face ++) {
Jason Sams5e0035a2010-12-13 17:11:21 -0800241 for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
Jason Samsef70a202011-01-13 17:38:18 -0800242 const uint8_t *p = (const uint8_t *)mPtr;
243 p += mType->getLODFaceOffset(lod, (RsAllocationCubemapFace)face, 0, 0);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800244
Jason Samsef70a202011-01-13 17:38:18 -0800245 GLenum t = GL_TEXTURE_2D;
246 if (mType->getDimFaces()) {
247 t = gFaceOrder[face];
248 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800249
250 if (isFirstUpload) {
Jason Samsef70a202011-01-13 17:38:18 -0800251 glTexImage2D(t, lod, format,
252 mType->getLODDimX(lod), mType->getLODDimY(lod),
253 0, format, type, p);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800254 } else {
Jason Samsef70a202011-01-13 17:38:18 -0800255 glTexSubImage2D(t, lod, 0, 0,
256 mType->getLODDimX(lod), mType->getLODDimY(lod),
257 format, type, p);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800258 }
259 }
260 }
Jason Sams6d8eb262010-12-15 01:41:00 -0800261
262 if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
Jason Sams6d8eb262010-12-15 01:41:00 -0800263 glGenerateMipmap(target);
Jason Sams6d8eb262010-12-15 01:41:00 -0800264 }
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800265#endif //ANDROID_RS_SERIALIZE
Jason Samsd19f10d2009-05-22 14:03:28 -0700266}
267
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800268void Allocation::deferedUploadToBufferObject(const Context *rsc) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800269 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800270 mUploadDefered = true;
271}
272
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800273void Allocation::uploadToBufferObject(const Context *rsc) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800274#ifndef ANDROID_RS_SERIALIZE
Jason Samsd19f10d2009-05-22 14:03:28 -0700275 rsAssert(!mType->getDimY());
276 rsAssert(!mType->getDimZ());
277
Jason Samsd4b23b52010-12-13 15:32:35 -0800278 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800279
Jason Samsd19f10d2009-05-22 14:03:28 -0700280 if (!mBufferID) {
281 glGenBuffers(1, &mBufferID);
282 }
Jason Sams3b7d39b2009-12-14 12:57:40 -0800283 if (!mBufferID) {
284 LOGE("Upload to buffer object failed");
285 mUploadDefered = true;
286 return;
287 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800288 GLenum target = (GLenum)getGLTarget();
289 glBindBuffer(target, mBufferID);
290 glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
291 glBindBuffer(target, 0);
Jason Samsf4687002010-03-10 17:30:41 -0800292 rsc->checkError("Allocation::uploadToBufferObject");
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800293#endif //ANDROID_RS_SERIALIZE
Jason Samsd19f10d2009-05-22 14:03:28 -0700294}
295
Jason Sams5476b452010-12-08 16:14:36 -0800296void Allocation::uploadCheck(Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800297 if (mUploadDefered) {
Jason Sams5476b452010-12-08 16:14:36 -0800298 syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800299 }
300}
301
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800302void Allocation::read(void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700303 memcpy(data, mPtr, mType->getSizeBytes());
304}
305
Jason Sams49a05d72010-12-29 14:31:29 -0800306void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
307 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700308 uint32_t eSize = mType->getElementSizeBytes();
309 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
310 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700311 uint32_t size = count * eSize;
312
313 if (size != sizeBytes) {
314 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Sams3c0dfba2009-09-27 17:50:38 -0700315 mType->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -0700316 return;
317 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700318
319 if (mType->getElement()->getHasReferences()) {
320 incRefs(data, count);
321 decRefs(ptr, count);
322 }
323
Jason Sams07ae4062009-08-27 20:23:34 -0700324 memcpy(ptr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700325 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800326 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700327}
328
Jason Sams49a05d72010-12-29 14:31:29 -0800329void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800330 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700331 uint32_t eSize = mType->getElementSizeBytes();
332 uint32_t lineSize = eSize * w;
333 uint32_t destW = mType->getDimX();
334
Jason Samsf7086092011-01-12 13:28:37 -0800335 //LOGE("data2d %p, %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams07ae4062009-08-27 20:23:34 -0700336
Jason Samsf7086092011-01-12 13:28:37 -0800337 if ((lineSize * h) != sizeBytes) {
338 LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
Jason Sams07ae4062009-08-27 20:23:34 -0700339 rsAssert(!"Allocation::subData called with mismatched size");
340 return;
341 }
342
Jason Samsf7086092011-01-12 13:28:37 -0800343 if (mPtr) {
344 const uint8_t *src = static_cast<const uint8_t *>(data);
345 uint8_t *dst = static_cast<uint8_t *>(mPtr);
Jason Samsef70a202011-01-13 17:38:18 -0800346 dst += mType->getLODFaceOffset(lod, face, xoff, yoff);
Jason Samsf7086092011-01-12 13:28:37 -0800347
348 //LOGE(" %p %p %i ", dst, src, eSize);
349 for (uint32_t line=yoff; line < (yoff+h); line++) {
350 if (mType->getElement()->getHasReferences()) {
351 incRefs(src, w);
352 decRefs(dst, w);
353 }
354 memcpy(dst, src, lineSize);
355 src += lineSize;
356 dst += destW * eSize;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700357 }
Jason Samsf7086092011-01-12 13:28:37 -0800358 sendDirty();
359 mUploadDefered = true;
360 } else {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800361 update2DTexture(data, xoff, yoff, lod, face, w, h);
Jason Samsd19f10d2009-05-22 14:03:28 -0700362 }
363}
364
Jason Samsfb9f82c2011-01-12 14:53:25 -0800365void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
366 uint32_t lod, RsAllocationCubemapFace face,
367 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700368}
369
Jason Sams49a05d72010-12-29 14:31:29 -0800370void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800371 uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700372 uint32_t eSize = mType->getElementSizeBytes();
373 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
374 ptr += eSize * x;
375
376 if (cIdx >= mType->getElement()->getFieldCount()) {
377 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
378 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
379 return;
380 }
381
382 if (x >= mType->getDimX()) {
383 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
384 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
385 return;
386 }
387
388 const Element * e = mType->getElement()->getField(cIdx);
389 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
390
391 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800392 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams49bdaf02010-08-31 13:50:42 -0700393 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
394 return;
395 }
396
397 if (e->getHasReferences()) {
398 e->incRefs(data);
399 e->decRefs(ptr);
400 }
401
402 memcpy(ptr, data, sizeBytes);
403 sendDirty();
404 mUploadDefered = true;
405}
406
Jason Sams49a05d72010-12-29 14:31:29 -0800407void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800408 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700409 uint32_t eSize = mType->getElementSizeBytes();
410 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
411 ptr += eSize * (x + y * mType->getDimX());
412
413 if (x >= mType->getDimX()) {
414 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
415 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
416 return;
417 }
418
419 if (y >= mType->getDimY()) {
420 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
421 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
422 return;
423 }
424
425 if (cIdx >= mType->getElement()->getFieldCount()) {
426 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
427 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
428 return;
429 }
430
431 const Element * e = mType->getElement()->getField(cIdx);
432 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
433
434 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800435 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams49bdaf02010-08-31 13:50:42 -0700436 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
437 return;
438 }
439
440 if (e->getHasReferences()) {
441 e->incRefs(data);
442 e->decRefs(ptr);
443 }
444
445 memcpy(ptr, data, sizeBytes);
446 sendDirty();
447 mUploadDefered = true;
448}
449
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800450void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800451#ifndef ANDROID_RS_SERIALIZE
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700452 mToDirtyList.push(p);
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800453#endif //ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700454}
Jason Samsd19f10d2009-05-22 14:03:28 -0700455
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800456void Allocation::removeProgramToDirty(const Program *p) {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800457#ifndef ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700458 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
459 if (mToDirtyList[ct] == p) {
460 mToDirtyList.removeAt(ct);
461 return;
462 }
463 }
464 rsAssert(0);
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800465#endif //ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700466}
467
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800468void Allocation::dumpLOGV(const char *prefix) const {
Jason Sams715333b2009-11-17 17:26:46 -0800469 ObjectBase::dumpLOGV(prefix);
470
471 String8 s(prefix);
472 s.append(" type ");
473 if (mType.get()) {
474 mType->dumpLOGV(s.string());
475 }
476
477 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
478 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
479
Jason Samsd4b23b52010-12-13 15:32:35 -0800480 LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
481 prefix, mUsageFlags, mMipmapControl, mTextureID, mBufferID);
Jason Sams715333b2009-11-17 17:26:46 -0800482}
Jason Samsd19f10d2009-05-22 14:03:28 -0700483
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800484void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700485 // Need to identify ourselves
486 stream->addU32((uint32_t)getClassId());
487
488 String8 name(getName());
489 stream->addString(&name);
490
491 // First thing we need to serialize is the type object since it will be needed
492 // to initialize the class
493 mType->serialize(stream);
494
495 uint32_t dataSize = mType->getSizeBytes();
496 // Write how much data we are storing
497 stream->addU32(dataSize);
498 // Now write the data
499 stream->addByteArray(mPtr, dataSize);
500}
501
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800502Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700503 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700504 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800505 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700506 LOGE("allocation loading skipped due to invalid class id\n");
507 return NULL;
508 }
509
510 String8 name;
511 stream->loadString(&name);
512
513 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800514 if (!type) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700515 return NULL;
516 }
517 type->compute();
518
519 // Number of bytes we wrote out for this allocation
520 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800521 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700522 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Samsb38d5342010-10-21 14:06:55 -0700523 ObjectBase::checkDelete(type);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700524 return NULL;
525 }
526
Jason Samsd4b23b52010-12-13 15:32:35 -0800527 Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700528 alloc->setName(name.string(), name.size());
529
Jason Sams49a05d72010-12-29 14:31:29 -0800530 uint32_t count = dataSize / type->getElementSizeBytes();
531
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700532 // Read in all of our allocation data
Jason Sams49a05d72010-12-29 14:31:29 -0800533 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2ce0e3f2010-08-11 10:30:44 -0700534 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700535
536 return alloc;
537}
538
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800539void Allocation::sendDirty() const {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800540#ifndef ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700541 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
542 mToDirtyList[ct]->forceDirty();
543 }
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800544#endif //ANDROID_RS_SERIALIZE
Jason Sams83f1c632009-10-26 15:19:28 -0700545}
Jason Samsd19f10d2009-05-22 14:03:28 -0700546
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800547void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700548 const uint8_t *p = static_cast<const uint8_t *>(ptr);
549 const Element *e = mType->getElement();
550 uint32_t stride = e->getSizeBytes();
551
Jason Sams5edc6082010-10-05 13:32:49 -0700552 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700553 while (ct > 0) {
554 e->incRefs(p);
555 ct --;
556 p += stride;
557 }
558}
559
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800560void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700561 const uint8_t *p = static_cast<const uint8_t *>(ptr);
562 const Element *e = mType->getElement();
563 uint32_t stride = e->getSizeBytes();
564
Jason Sams5edc6082010-10-05 13:32:49 -0700565 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700566 while (ct > 0) {
567 e->decRefs(p);
568 ct --;
569 p += stride;
570 }
571}
572
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800573void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams5edc6082010-10-05 13:32:49 -0700574}
575
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800576void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700577 Type *t = mType->cloneAndResize1D(rsc, dimX);
578
579 uint32_t oldDimX = mType->getDimX();
580 if (dimX == oldDimX) {
581 return;
582 }
583
584 if (dimX < oldDimX) {
585 decRefs(mPtr, oldDimX - dimX, dimX);
586 }
587 mPtr = realloc(mPtr, t->getSizeBytes());
588
589 if (dimX > oldDimX) {
590 const Element *e = mType->getElement();
591 uint32_t stride = e->getSizeBytes();
592 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
593 }
594 mType.set(t);
595}
596
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800597void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700598 LOGE("not implemented");
599}
600
Jason Samsd19f10d2009-05-22 14:03:28 -0700601/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700602//
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800603#ifndef ANDROID_RS_SERIALIZE
Jason Samsd19f10d2009-05-22 14:03:28 -0700604
605namespace android {
606namespace renderscript {
607
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800608void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700609 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams5e0035a2010-12-13 17:11:21 -0800610 alloc->deferedUploadToTexture(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700611}
612
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800613void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700614 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800615 alloc->deferedUploadToBufferObject(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700616}
617
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800618static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700619 uint32_t w = out.getDimX();
620 uint32_t h = out.getDimY();
621
Jason Sams6f5c61c2009-07-28 17:20:11 -0700622 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-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 Sams6f5c61c2009-07-28 17:20:11 -0700627 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-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 Sakhartchouked9f2102010-11-09 17:00:54 -0800636static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700637 uint32_t w = out.getDimX();
638 uint32_t h = out.getDimY();
639
Jason Sams6f5c61c2009-07-28 17:20:11 -0700640 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-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 Sams6f5c61c2009-07-28 17:20:11 -0700645 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700646 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700647 oPtr ++;
648 i1 += 2;
649 i2 += 2;
650 }
651 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700652}
653
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800654static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Samse20e3b42010-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 Sakhartchouked9f2102010-11-09 17:00:54 -0800672static void mip(const Adapter2D &out, const Adapter2D &in) {
673 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Sams6f5c61c2009-07-28 17:20:11 -0700674 case 32:
675 mip8888(out, in);
676 break;
677 case 16:
678 mip565(out, in);
679 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800680 case 8:
681 mip8(out, in);
682 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700683 }
Jason Sams6f5c61c2009-07-28 17:20:11 -0700684}
Jason Samsd19f10d2009-05-22 14:03:28 -0700685
Jason Sams5476b452010-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 Sams62f258f2011-01-28 15:49:07 -0800689 a->sendDirty();
Jason Sams5476b452010-12-08 16:14:36 -0800690}
691
Jason Samsf7086092011-01-12 13:28:37 -0800692void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700693 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsf7086092011-01-12 13:28:37 -0800694 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams4ef66502010-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 Sakhartchouk26ae3902010-10-11 12:35:15 -0700708}
709
Jason Sams49a05d72010-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 Samsd19f10d2009-05-22 14:03:28 -0700712 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800713 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700714}
715
Jason Sams49a05d72010-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 Samsd19f10d2009-05-22 14:03:28 -0700718 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800719 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -0700720}
721
Jason Sams49a05d72010-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 Sams49bdaf02010-08-31 13:50:42 -0700724 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800725 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -0700726}
727
Jason Sams49a05d72010-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 Sams49bdaf02010-08-31 13:50:42 -0700730 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800731 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700732}
733
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800734void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700735 Allocation *a = static_cast<Allocation *>(va);
736 a->read(data);
737}
738
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800739void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700740 Allocation *a = static_cast<Allocation *>(va);
741 a->resize1D(rsc, dimX);
742}
743
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800744void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700745 Allocation *a = static_cast<Allocation *>(va);
746 a->resize2D(rsc, dimX, dimY);
747}
748
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700749}
750}
751
Alex Sakhartchoukdcc23192011-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 Sakhartchouked9f2102010-11-09 17:00:54 -0800769const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700770 Allocation *a = static_cast<Allocation *>(va);
771 a->getType()->incUserRef();
772
773 return a->getType();
774}
775
Jason Sams5476b452010-12-08 16:14:36 -0800776RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800777 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800778 uint32_t usages) {
Jason Sams31a7e422010-10-26 13:09:17 -0700779 Context *rsc = static_cast<Context *>(con);
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800780 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages, mips);
Jason Sams31a7e422010-10-26 13:09:17 -0700781 alloc->incUserRef();
782 return alloc;
783}
784
Jason Sams5476b452010-12-08 16:14:36 -0800785
786RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800787 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800788 const void *data, uint32_t usages) {
Jason Sams31a7e422010-10-26 13:09:17 -0700789 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800790 Type *t = static_cast<Type *>(vtype);
Jason Sams31a7e422010-10-26 13:09:17 -0700791
Jason Sams5476b452010-12-08 16:14:36 -0800792 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages);
Jason Sams31a7e422010-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 Sams5476b452010-12-08 16:14:36 -0800799 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsd4b23b52010-12-13 15:32:35 -0800800 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800801 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams31a7e422010-10-26 13:09:17 -0700802 }
803
Jason Sams5e0035a2010-12-13 17:11:21 -0800804 texAlloc->deferedUploadToTexture(rsc);
Jason Sams31a7e422010-10-26 13:09:17 -0700805 return texAlloc;
806}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800807
Jason Sams5476b452010-12-08 16:14:36 -0800808RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800809 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800810 const void *data, uint32_t usages) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800811 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800812 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk67f2e442010-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 Sams5476b452010-12-08 16:14:36 -0800817 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
Alex Sakhartchouk67f2e442010-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 Sakhartchoukfe852e22011-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 Sakhartchouk67f2e442010-11-18 15:22:43 -0800828 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams5476b452010-12-08 16:14:36 -0800829 for (uint32_t face = 0; face < 6; face ++) {
830 Adapter2D faceAdapter(rsc, texAlloc);
831 faceAdapter.setFace(face);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800832
Alex Sakhartchoukfe852e22011-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 Sakhartchouk67f2e442010-11-18 15:22:43 -0800836
Jason Sams5476b452010-12-08 16:14:36 -0800837 // Move the data pointer to the next cube face
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800838 sourcePtr += copySize;
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800839 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800840
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800841 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
842 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800843 }
844
Jason Sams5e0035a2010-12-13 17:11:21 -0800845 texAlloc->deferedUploadToTexture(rsc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800846 return texAlloc;
847}
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800848
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800849#endif //ANDROID_RS_SERIALIZE