blob: 2839b76cdeb2871496b98b97b6a26b287594896a [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"
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -080024#endif //ANDROID_RS_BUILD_FOR_HOST
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070025
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -080026static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va);
27
Jason Sams326e0dd2009-05-22 14:03:28 -070028using namespace android;
29using namespace android::renderscript;
30
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080031Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
32 RsAllocationMipmapControl mc)
33 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080034 init(rsc, type);
35
Jason Sams366c9c82010-12-08 16:14:36 -080036 mUsageFlags = usages;
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080037 mMipmapControl = mc;
Jason Sams366c9c82010-12-08 16:14:36 -080038
Jason Samsb89b0b72010-12-13 17:11:21 -080039 allocScriptMemory();
Jason Sams10e5e572010-08-12 12:44:02 -070040 if (mType->getElement()->getHasReferences()) {
41 memset(mPtr, 0, mType->getSizeBytes());
42 }
Jason Samsfa84da22010-03-01 15:31:04 -080043 if (!mPtr) {
44 LOGE("Allocation::Allocation, alloc failure");
45 }
46}
47
Jason Samsfa84da22010-03-01 15:31:04 -080048
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080049void Allocation::init(Context *rsc, const Type *type) {
Jason Sams326e0dd2009-05-22 14:03:28 -070050 mPtr = NULL;
51
52 mCpuWrite = false;
53 mCpuRead = false;
54 mGpuWrite = false;
55 mGpuRead = false;
56
57 mReadWriteRatio = 0;
58 mUpdateSize = 0;
Jason Samsebc50192010-12-13 15:32:35 -080059 mUsageFlags = 0;
60 mMipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Sams326e0dd2009-05-22 14:03:28 -070061
Jason Sams326e0dd2009-05-22 14:03:28 -070062 mTextureID = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070063 mBufferID = 0;
Jason Samscf4c7c92009-12-14 12:57:40 -080064 mUploadDefered = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070065
Jason Samsfa84da22010-03-01 15:31:04 -080066 mUserBitmapCallback = NULL;
67 mUserBitmapCallbackData = NULL;
68
Jason Sams326e0dd2009-05-22 14:03:28 -070069 mType.set(type);
Jason Samse5ffb872009-08-09 17:01:55 -070070 rsAssert(type);
Jason Samsfa84da22010-03-01 15:31:04 -080071
72 mPtr = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070073}
74
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080075Allocation::~Allocation() {
Jason Samsfa84da22010-03-01 15:31:04 -080076 if (mUserBitmapCallback != NULL) {
77 mUserBitmapCallback(mUserBitmapCallbackData);
Jason Samsb89b0b72010-12-13 17:11:21 -080078 mPtr = NULL;
Jason Samsfa84da22010-03-01 15:31:04 -080079 }
Jason Samsb89b0b72010-12-13 17:11:21 -080080 freeScriptMemory();
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -080081#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samse402ed32009-11-03 11:25:42 -080082 if (mBufferID) {
83 // Causes a SW crash....
84 //LOGV(" mBufferID %i", mBufferID);
85 //glDeleteBuffers(1, &mBufferID);
86 //mBufferID = 0;
87 }
88 if (mTextureID) {
89 glDeleteTextures(1, &mTextureID);
90 mTextureID = 0;
91 }
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -080092#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -070093}
94
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080095void Allocation::setCpuWritable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -070096}
97
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080098void Allocation::setGpuWritable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -070099}
100
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800101void Allocation::setCpuReadable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700102}
103
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800104void Allocation::setGpuReadable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700105}
106
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800107bool Allocation::fixAllocation() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700108 return false;
109}
110
Jason Samsb89b0b72010-12-13 17:11:21 -0800111void Allocation::deferedUploadToTexture(const Context *rsc) {
Jason Samsebc50192010-12-13 15:32:35 -0800112 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Samscf4c7c92009-12-14 12:57:40 -0800113 mUploadDefered = true;
114}
115
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800116uint32_t Allocation::getGLTarget() const {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800117#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsebc50192010-12-13 15:32:35 -0800118 if (getIsTexture()) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800119 if (mType->getDimFaces()) {
120 return GL_TEXTURE_CUBE_MAP;
121 } else {
122 return GL_TEXTURE_2D;
123 }
124 }
Jason Samsebc50192010-12-13 15:32:35 -0800125 if (getIsBufferObject()) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800126 return GL_ARRAY_BUFFER;
127 }
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800128#endif //ANDROID_RS_BUILD_FOR_HOST
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800129 return 0;
130}
131
Jason Samsb89b0b72010-12-13 17:11:21 -0800132void Allocation::allocScriptMemory() {
133 rsAssert(!mPtr);
134 mPtr = malloc(mType->getSizeBytes());
135}
136
137void Allocation::freeScriptMemory() {
Jason Samsb89b0b72010-12-13 17:11:21 -0800138 if (mPtr) {
139 free(mPtr);
140 mPtr = NULL;
141 }
142}
143
144
Jason Sams366c9c82010-12-08 16:14:36 -0800145void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
146 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
147
Jason Samsebc50192010-12-13 15:32:35 -0800148 if (getIsTexture()) {
Jason Sams366c9c82010-12-08 16:14:36 -0800149 uploadToTexture(rsc);
150 }
Jason Samsebc50192010-12-13 15:32:35 -0800151 if (getIsBufferObject()) {
Jason Sams366c9c82010-12-08 16:14:36 -0800152 uploadToBufferObject(rsc);
153 }
154
155 mUploadDefered = false;
156}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800157
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800158void Allocation::uploadToTexture(const Context *rsc) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800159#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsebc50192010-12-13 15:32:35 -0800160 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Samsd01d9702009-12-23 14:35:29 -0800161 GLenum type = mType->getElement()->getComponent().getGLType();
162 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Sams565ac362009-06-03 16:04:54 -0700163
164 if (!type || !format) {
165 return;
166 }
167
Jason Samsb89b0b72010-12-13 17:11:21 -0800168 if (!mPtr) {
169 return;
170 }
171
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700172 bool isFirstUpload = false;
173
Jason Sams326e0dd2009-05-22 14:03:28 -0700174 if (!mTextureID) {
175 glGenTextures(1, &mTextureID);
Jason Sams13e26342009-11-24 12:26:35 -0800176
177 if (!mTextureID) {
178 // This should not happen, however, its likely the cause of the
179 // white sqare bug.
180 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
181 LOGE("Upload to texture failed to gen mTextureID");
182 rsc->dumpDebug();
Jason Samscf4c7c92009-12-14 12:57:40 -0800183 mUploadDefered = true;
184 return;
Jason Sams13e26342009-11-24 12:26:35 -0800185 }
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700186 isFirstUpload = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700187 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800188
Jason Samsbcac9342011-01-13 17:38:18 -0800189 upload2DTexture(isFirstUpload);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800190
Jason Samsb89b0b72010-12-13 17:11:21 -0800191 if (!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
192 freeScriptMemory();
193 }
194
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800195 rsc->checkError("Allocation::uploadToTexture");
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800196#endif //ANDROID_RS_BUILD_FOR_HOST
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800197}
198
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800199#ifndef ANDROID_RS_BUILD_FOR_HOST
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};
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800208#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Samsbcac9342011-01-13 17:38:18 -0800209
Jason Sams236385b2011-01-12 14:53:25 -0800210void Allocation::update2DTexture(const void *ptr, uint32_t xoff, uint32_t yoff,
211 uint32_t lod, RsAllocationCubemapFace face,
212 uint32_t w, uint32_t h) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800213#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams236385b2011-01-12 14:53:25 -0800214 GLenum type = mType->getElement()->getComponent().getGLType();
215 GLenum format = mType->getElement()->getComponent().getGLFormat();
216 GLenum target = (GLenum)getGLTarget();
Jason Sams185b8b02011-01-16 14:54:28 -0800217 rsAssert(mTextureID);
Jason Sams236385b2011-01-12 14:53:25 -0800218 glBindTexture(target, mTextureID);
219 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsbcac9342011-01-13 17:38:18 -0800220 GLenum t = GL_TEXTURE_2D;
221 if (mType->getDimFaces()) {
222 t = gFaceOrder[face];
223 }
224 glTexSubImage2D(t, lod, xoff, yoff, w, h, format, type, ptr);
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800225#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Sams236385b2011-01-12 14:53:25 -0800226}
227
Jason Samsbcac9342011-01-13 17:38:18 -0800228void Allocation::upload2DTexture(bool isFirstUpload) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800229#ifndef ANDROID_RS_BUILD_FOR_HOST
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800230 GLenum type = mType->getElement()->getComponent().getGLType();
231 GLenum format = mType->getElement()->getComponent().getGLFormat();
232
Jason Samsb89b0b72010-12-13 17:11:21 -0800233 GLenum target = (GLenum)getGLTarget();
234 glBindTexture(target, mTextureID);
235 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700236
Jason Samsbcac9342011-01-13 17:38:18 -0800237 uint32_t faceCount = 1;
238 if (mType->getDimFaces()) {
239 faceCount = 6;
Jason Sams326e0dd2009-05-22 14:03:28 -0700240 }
Jason Samsb7e83bd2010-12-15 01:41:00 -0800241
Jason Samsbcac9342011-01-13 17:38:18 -0800242 for (uint32_t face = 0; face < faceCount; face ++) {
Jason Samsb89b0b72010-12-13 17:11:21 -0800243 for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
Jason Samsbcac9342011-01-13 17:38:18 -0800244 const uint8_t *p = (const uint8_t *)mPtr;
245 p += mType->getLODFaceOffset(lod, (RsAllocationCubemapFace)face, 0, 0);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800246
Jason Samsbcac9342011-01-13 17:38:18 -0800247 GLenum t = GL_TEXTURE_2D;
248 if (mType->getDimFaces()) {
249 t = gFaceOrder[face];
250 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800251
252 if (isFirstUpload) {
Jason Samsbcac9342011-01-13 17:38:18 -0800253 glTexImage2D(t, lod, format,
254 mType->getLODDimX(lod), mType->getLODDimY(lod),
255 0, format, type, p);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800256 } else {
Jason Samsbcac9342011-01-13 17:38:18 -0800257 glTexSubImage2D(t, lod, 0, 0,
258 mType->getLODDimX(lod), mType->getLODDimY(lod),
259 format, type, p);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800260 }
261 }
262 }
Jason Samsb7e83bd2010-12-15 01:41:00 -0800263
264 if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
Jason Samsb7e83bd2010-12-15 01:41:00 -0800265 glGenerateMipmap(target);
Jason Samsb7e83bd2010-12-15 01:41:00 -0800266 }
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800267#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -0700268}
269
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800270void Allocation::deferedUploadToBufferObject(const Context *rsc) {
Jason Samsebc50192010-12-13 15:32:35 -0800271 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Samscf4c7c92009-12-14 12:57:40 -0800272 mUploadDefered = true;
273}
274
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800275void Allocation::uploadToBufferObject(const Context *rsc) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800276#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -0700277 rsAssert(!mType->getDimY());
278 rsAssert(!mType->getDimZ());
279
Jason Samsebc50192010-12-13 15:32:35 -0800280 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Samscf4c7c92009-12-14 12:57:40 -0800281
Jason Sams326e0dd2009-05-22 14:03:28 -0700282 if (!mBufferID) {
283 glGenBuffers(1, &mBufferID);
284 }
Jason Samscf4c7c92009-12-14 12:57:40 -0800285 if (!mBufferID) {
286 LOGE("Upload to buffer object failed");
287 mUploadDefered = true;
288 return;
289 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800290 GLenum target = (GLenum)getGLTarget();
291 glBindBuffer(target, mBufferID);
292 glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
293 glBindBuffer(target, 0);
Jason Samsc1ed5892010-03-10 17:30:41 -0800294 rsc->checkError("Allocation::uploadToBufferObject");
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800295#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -0700296}
297
Jason Sams366c9c82010-12-08 16:14:36 -0800298void Allocation::uploadCheck(Context *rsc) {
Jason Samscf4c7c92009-12-14 12:57:40 -0800299 if (mUploadDefered) {
Jason Sams366c9c82010-12-08 16:14:36 -0800300 syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
Jason Samscf4c7c92009-12-14 12:57:40 -0800301 }
302}
303
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800304void Allocation::read(void *data) {
Jason Samse579df42009-08-10 14:55:26 -0700305 memcpy(data, mPtr, mType->getSizeBytes());
306}
307
Jason Sams4b45b892010-12-29 14:31:29 -0800308void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
309 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700310 uint32_t eSize = mType->getElementSizeBytes();
311 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
312 ptr += eSize * xoff;
Jason Sams9397e302009-08-27 20:23:34 -0700313 uint32_t size = count * eSize;
314
315 if (size != sizeBytes) {
316 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Samse12c1c52009-09-27 17:50:38 -0700317 mType->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700318 return;
319 }
Jason Samse3929c92010-08-09 18:13:33 -0700320
321 if (mType->getElement()->getHasReferences()) {
322 incRefs(data, count);
323 decRefs(ptr, count);
324 }
325
Jason Sams9397e302009-08-27 20:23:34 -0700326 memcpy(ptr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700327 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800328 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700329}
330
Jason Sams4b45b892010-12-29 14:31:29 -0800331void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800332 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700333 uint32_t eSize = mType->getElementSizeBytes();
334 uint32_t lineSize = eSize * w;
335 uint32_t destW = mType->getDimX();
336
Jason Samsa2371512011-01-12 13:28:37 -0800337 //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 -0700338
Jason Samsa2371512011-01-12 13:28:37 -0800339 if ((lineSize * h) != sizeBytes) {
340 LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
Jason Sams9397e302009-08-27 20:23:34 -0700341 rsAssert(!"Allocation::subData called with mismatched size");
342 return;
343 }
344
Jason Samsa2371512011-01-12 13:28:37 -0800345 if (mPtr) {
346 const uint8_t *src = static_cast<const uint8_t *>(data);
347 uint8_t *dst = static_cast<uint8_t *>(mPtr);
Jason Samsbcac9342011-01-13 17:38:18 -0800348 dst += mType->getLODFaceOffset(lod, face, xoff, yoff);
Jason Samsa2371512011-01-12 13:28:37 -0800349
350 //LOGE(" %p %p %i ", dst, src, eSize);
351 for (uint32_t line=yoff; line < (yoff+h); line++) {
352 if (mType->getElement()->getHasReferences()) {
353 incRefs(src, w);
354 decRefs(dst, w);
355 }
356 memcpy(dst, src, lineSize);
357 src += lineSize;
358 dst += destW * eSize;
Jason Samse3929c92010-08-09 18:13:33 -0700359 }
Jason Samsa2371512011-01-12 13:28:37 -0800360 sendDirty();
361 mUploadDefered = true;
362 } else {
Jason Sams236385b2011-01-12 14:53:25 -0800363 update2DTexture(data, xoff, yoff, lod, face, w, h);
Jason Sams326e0dd2009-05-22 14:03:28 -0700364 }
365}
366
Jason Sams236385b2011-01-12 14:53:25 -0800367void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
368 uint32_t lod, RsAllocationCubemapFace face,
369 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700370}
371
Jason Sams4b45b892010-12-29 14:31:29 -0800372void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800373 uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700374 uint32_t eSize = mType->getElementSizeBytes();
375 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
376 ptr += eSize * x;
377
378 if (cIdx >= mType->getElement()->getFieldCount()) {
379 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
380 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
381 return;
382 }
383
384 if (x >= mType->getDimX()) {
385 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
386 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
387 return;
388 }
389
390 const Element * e = mType->getElement()->getField(cIdx);
391 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
392
393 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800394 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700395 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
396 return;
397 }
398
399 if (e->getHasReferences()) {
400 e->incRefs(data);
401 e->decRefs(ptr);
402 }
403
404 memcpy(ptr, data, sizeBytes);
405 sendDirty();
406 mUploadDefered = true;
407}
408
Jason Sams4b45b892010-12-29 14:31:29 -0800409void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800410 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700411 uint32_t eSize = mType->getElementSizeBytes();
412 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
413 ptr += eSize * (x + y * mType->getDimX());
414
415 if (x >= mType->getDimX()) {
416 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
417 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
418 return;
419 }
420
421 if (y >= mType->getDimY()) {
422 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
423 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
424 return;
425 }
426
427 if (cIdx >= mType->getElement()->getFieldCount()) {
428 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
429 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
430 return;
431 }
432
433 const Element * e = mType->getElement()->getField(cIdx);
434 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
435
436 if (sizeBytes != e->getSizeBytes()) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800437 LOGE("Error Allocation::subElementData data size %i does not match field size %zu.", sizeBytes, e->getSizeBytes());
Jason Sams5f0c84c2010-08-31 13:50:42 -0700438 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
439 return;
440 }
441
442 if (e->getHasReferences()) {
443 e->incRefs(data);
444 e->decRefs(ptr);
445 }
446
447 memcpy(ptr, data, sizeBytes);
448 sendDirty();
449 mUploadDefered = true;
450}
451
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800452void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800453#ifndef ANDROID_RS_BUILD_FOR_HOST
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700454 mToDirtyList.push(p);
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800455#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700456}
Jason Sams326e0dd2009-05-22 14:03:28 -0700457
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800458void Allocation::removeProgramToDirty(const Program *p) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800459#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700460 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
461 if (mToDirtyList[ct] == p) {
462 mToDirtyList.removeAt(ct);
463 return;
464 }
465 }
466 rsAssert(0);
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800467#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700468}
469
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800470void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800471 ObjectBase::dumpLOGV(prefix);
472
473 String8 s(prefix);
474 s.append(" type ");
475 if (mType.get()) {
476 mType->dumpLOGV(s.string());
477 }
478
479 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
480 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
481
Jason Samsebc50192010-12-13 15:32:35 -0800482 LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
483 prefix, mUsageFlags, mMipmapControl, mTextureID, mBufferID);
Jason Samsc21cf402009-11-17 17:26:46 -0800484}
Jason Sams326e0dd2009-05-22 14:03:28 -0700485
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800486void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700487 // Need to identify ourselves
488 stream->addU32((uint32_t)getClassId());
489
490 String8 name(getName());
491 stream->addString(&name);
492
493 // First thing we need to serialize is the type object since it will be needed
494 // to initialize the class
495 mType->serialize(stream);
496
497 uint32_t dataSize = mType->getSizeBytes();
498 // Write how much data we are storing
499 stream->addU32(dataSize);
500 // Now write the data
501 stream->addByteArray(mPtr, dataSize);
502}
503
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800504Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700505 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700506 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800507 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700508 LOGE("allocation loading skipped due to invalid class id\n");
509 return NULL;
510 }
511
512 String8 name;
513 stream->loadString(&name);
514
515 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800516 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700517 return NULL;
518 }
519 type->compute();
520
521 // Number of bytes we wrote out for this allocation
522 uint32_t dataSize = stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800523 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700524 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Sams225afd32010-10-21 14:06:55 -0700525 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700526 return NULL;
527 }
528
Jason Samsebc50192010-12-13 15:32:35 -0800529 Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700530 alloc->setName(name.string(), name.size());
531
Jason Sams4b45b892010-12-29 14:31:29 -0800532 uint32_t count = dataSize / type->getElementSizeBytes();
533
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700534 // Read in all of our allocation data
Jason Sams4b45b892010-12-29 14:31:29 -0800535 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700536 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700537
538 return alloc;
539}
540
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800541void Allocation::sendDirty() const {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800542#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700543 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
544 mToDirtyList[ct]->forceDirty();
545 }
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800546#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700547}
Jason Sams326e0dd2009-05-22 14:03:28 -0700548
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800549void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700550 const uint8_t *p = static_cast<const uint8_t *>(ptr);
551 const Element *e = mType->getElement();
552 uint32_t stride = e->getSizeBytes();
553
Jason Sams96abf812010-10-05 13:32:49 -0700554 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700555 while (ct > 0) {
556 e->incRefs(p);
557 ct --;
558 p += stride;
559 }
560}
561
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800562void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700563 const uint8_t *p = static_cast<const uint8_t *>(ptr);
564 const Element *e = mType->getElement();
565 uint32_t stride = e->getSizeBytes();
566
Jason Sams96abf812010-10-05 13:32:49 -0700567 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700568 while (ct > 0) {
569 e->decRefs(p);
570 ct --;
571 p += stride;
572 }
573}
574
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800575void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700576}
577
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800578void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700579 Type *t = mType->cloneAndResize1D(rsc, dimX);
580
581 uint32_t oldDimX = mType->getDimX();
582 if (dimX == oldDimX) {
583 return;
584 }
585
586 if (dimX < oldDimX) {
587 decRefs(mPtr, oldDimX - dimX, dimX);
588 }
589 mPtr = realloc(mPtr, t->getSizeBytes());
590
591 if (dimX > oldDimX) {
592 const Element *e = mType->getElement();
593 uint32_t stride = e->getSizeBytes();
594 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
595 }
596 mType.set(t);
597}
598
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800599void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700600 LOGE("not implemented");
601}
602
Jason Sams326e0dd2009-05-22 14:03:28 -0700603/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700604//
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800605#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -0700606
607namespace android {
608namespace renderscript {
609
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800610void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700611 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samsb89b0b72010-12-13 17:11:21 -0800612 alloc->deferedUploadToTexture(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700613}
614
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800615void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700616 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samscf4c7c92009-12-14 12:57:40 -0800617 alloc->deferedUploadToBufferObject(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700618}
619
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800620static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700621 uint32_t w = out.getDimX();
622 uint32_t h = out.getDimY();
623
Jason Samse9f5c532009-07-28 17:20:11 -0700624 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700625 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
626 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
627 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
628
Jason Samse9f5c532009-07-28 17:20:11 -0700629 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700630 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
631 oPtr ++;
632 i1 += 2;
633 i2 += 2;
634 }
635 }
636}
637
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800638static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Sams565ac362009-06-03 16:04:54 -0700639 uint32_t w = out.getDimX();
640 uint32_t h = out.getDimY();
641
Jason Samse9f5c532009-07-28 17:20:11 -0700642 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700643 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
644 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
645 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
646
Jason Samse9f5c532009-07-28 17:20:11 -0700647 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700648 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700649 oPtr ++;
650 i1 += 2;
651 i2 += 2;
652 }
653 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700654}
655
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800656static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Sams2f6d8612010-01-19 17:53:54 -0800657 uint32_t w = out.getDimX();
658 uint32_t h = out.getDimY();
659
660 for (uint32_t y=0; y < h; y++) {
661 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
662 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
663 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
664
665 for (uint32_t x=0; x < w; x++) {
666 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
667 oPtr ++;
668 i1 += 2;
669 i2 += 2;
670 }
671 }
672}
673
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800674static void mip(const Adapter2D &out, const Adapter2D &in) {
675 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Samse9f5c532009-07-28 17:20:11 -0700676 case 32:
677 mip8888(out, in);
678 break;
679 case 16:
680 mip565(out, in);
681 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800682 case 8:
683 mip8(out, in);
684 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700685 }
Jason Samse9f5c532009-07-28 17:20:11 -0700686}
Jason Sams326e0dd2009-05-22 14:03:28 -0700687
Jason Sams366c9c82010-12-08 16:14:36 -0800688void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
689 Allocation *a = static_cast<Allocation *>(va);
690 a->syncAll(rsc, src);
Jason Sams09aeb8a2011-01-28 15:49:07 -0800691 a->sendDirty();
Jason Sams366c9c82010-12-08 16:14:36 -0800692}
693
Jason Samsa2371512011-01-12 13:28:37 -0800694void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700695 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsa2371512011-01-12 13:28:37 -0800696 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams837e3882010-12-10 16:03:15 -0800697}
698
699void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
700 Allocation *texAlloc = static_cast<Allocation *>(va);
701 const Type * t = texAlloc->getType();
702
703 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
704 if (s != dataLen) {
705 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
706 return;
707 }
708
709 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700710}
711
Jason Sams4b45b892010-12-29 14:31:29 -0800712void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
713 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700714 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800715 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700716}
717
Jason Sams4b45b892010-12-29 14:31:29 -0800718void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
719 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700720 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800721 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700722}
723
Jason Sams4b45b892010-12-29 14:31:29 -0800724void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
725 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700726 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800727 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700728}
729
Jason Sams4b45b892010-12-29 14:31:29 -0800730void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
731 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700732 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800733 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700734}
735
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800736void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
Jason Samse579df42009-08-10 14:55:26 -0700737 Allocation *a = static_cast<Allocation *>(va);
738 a->read(data);
739}
740
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800741void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700742 Allocation *a = static_cast<Allocation *>(va);
743 a->resize1D(rsc, dimX);
744}
745
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800746void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700747 Allocation *a = static_cast<Allocation *>(va);
748 a->resize2D(rsc, dimX, dimY);
749}
750
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700751}
752}
753
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800754static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va) {
755 Context *rsc = static_cast<Context *>(con);
756 Allocation *texAlloc = static_cast<Allocation *>(va);
757 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
758 for (uint32_t face = 0; face < numFaces; face ++) {
759 Adapter2D adapt(rsc, texAlloc);
760 Adapter2D adapt2(rsc, texAlloc);
761 adapt.setFace(face);
762 adapt2.setFace(face);
763 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
764 adapt.setLOD(lod);
765 adapt2.setLOD(lod + 1);
766 mip(adapt2, adapt);
767 }
768 }
769}
770
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800771const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700772 Allocation *a = static_cast<Allocation *>(va);
773 a->getType()->incUserRef();
774
775 return a->getType();
776}
777
Jason Sams366c9c82010-12-08 16:14:36 -0800778RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800779 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800780 uint32_t usages) {
Jason Samsf0c1df42010-10-26 13:09:17 -0700781 Context *rsc = static_cast<Context *>(con);
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -0800782 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages, mips);
Jason Samsf0c1df42010-10-26 13:09:17 -0700783 alloc->incUserRef();
784 return alloc;
785}
786
Jason Sams366c9c82010-12-08 16:14:36 -0800787
788RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800789 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800790 const void *data, uint32_t usages) {
Jason Samsf0c1df42010-10-26 13:09:17 -0700791 Context *rsc = static_cast<Context *>(con);
Jason Sams366c9c82010-12-08 16:14:36 -0800792 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700793
Jason Sams366c9c82010-12-08 16:14:36 -0800794 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages);
Jason Samsf0c1df42010-10-26 13:09:17 -0700795 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
796 if (texAlloc == NULL) {
797 LOGE("Memory allocation failure");
798 return NULL;
799 }
800
Jason Sams366c9c82010-12-08 16:14:36 -0800801 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsebc50192010-12-13 15:32:35 -0800802 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800803 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700804 }
805
Jason Samsb89b0b72010-12-13 17:11:21 -0800806 texAlloc->deferedUploadToTexture(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700807 return texAlloc;
808}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800809
Jason Sams366c9c82010-12-08 16:14:36 -0800810RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800811 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800812 const void *data, uint32_t usages) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800813 Context *rsc = static_cast<Context *>(con);
Jason Sams366c9c82010-12-08 16:14:36 -0800814 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800815
816 // Cubemap allocation's faces should be Width by Width each.
817 // Source data should have 6 * Width by Width pixels
818 // Error checking is done in the java layer
Jason Sams366c9c82010-12-08 16:14:36 -0800819 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800820 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
821 if (texAlloc == NULL) {
822 LOGE("Memory allocation failure");
823 return NULL;
824 }
825
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800826 uint32_t faceSize = t->getDimX();
827 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
828 uint32_t copySize = faceSize * t->getElementSizeBytes();
829
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800830 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800831 for (uint32_t face = 0; face < 6; face ++) {
832 Adapter2D faceAdapter(rsc, texAlloc);
833 faceAdapter.setFace(face);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800834
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800835 for (uint32_t dI = 0; dI < faceSize; dI ++) {
836 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
837 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800838
Jason Sams366c9c82010-12-08 16:14:36 -0800839 // Move the data pointer to the next cube face
Alex Sakhartchouk9f8bc4f2011-01-10 15:57:57 -0800840 sourcePtr += copySize;
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800841 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800842
Alex Sakhartchoukf8aafcf2011-01-11 14:47:44 -0800843 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
844 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800845 }
846
Jason Samsb89b0b72010-12-13 17:11:21 -0800847 texAlloc->deferedUploadToTexture(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800848 return texAlloc;
849}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800850
851#endif //ANDROID_RS_BUILD_FOR_HOST