blob: 3608e43927c9439a2b9bbc3b401a90910b7ec104 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070016#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -070017#include "rsContext.h"
18
Jason Sams1aa5a4e2009-06-22 17:15:15 -070019#include <GLES/gl.h>
Jason Sams7fabe1a2010-02-23 17:44:28 -080020#include <GLES2/gl2.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070021#include <GLES/glext.h>
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070022#else
23#include "rsContextHostStub.h"
24
25#include <OpenGL/gl.h>
26#include <OpenGl/glext.h>
27#endif
Jason Sams1aa5a4e2009-06-22 17:15:15 -070028
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070029#include "utils/StopWatch.h"
30
Jason Sams326e0dd2009-05-22 14:03:28 -070031using namespace android;
32using namespace android::renderscript;
33
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080034Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
35 RsAllocationMipmapControl mc)
36 : ObjectBase(rsc) {
Jason Samsfa84da22010-03-01 15:31:04 -080037 init(rsc, type);
38
Jason Sams366c9c82010-12-08 16:14:36 -080039 mUsageFlags = usages;
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080040 mMipmapControl = mc;
Jason Sams366c9c82010-12-08 16:14:36 -080041
Jason Samsb89b0b72010-12-13 17:11:21 -080042 allocScriptMemory();
Jason Sams10e5e572010-08-12 12:44:02 -070043 if (mType->getElement()->getHasReferences()) {
44 memset(mPtr, 0, mType->getSizeBytes());
45 }
Jason Samsfa84da22010-03-01 15:31:04 -080046 if (!mPtr) {
47 LOGE("Allocation::Allocation, alloc failure");
48 }
49}
50
Jason Samsfa84da22010-03-01 15:31:04 -080051
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080052void Allocation::init(Context *rsc, const Type *type) {
Jason Sams326e0dd2009-05-22 14:03:28 -070053 mPtr = NULL;
54
55 mCpuWrite = false;
56 mCpuRead = false;
57 mGpuWrite = false;
58 mGpuRead = false;
59
60 mReadWriteRatio = 0;
61 mUpdateSize = 0;
Jason Samsebc50192010-12-13 15:32:35 -080062 mUsageFlags = 0;
63 mMipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Sams326e0dd2009-05-22 14:03:28 -070064
Jason Sams326e0dd2009-05-22 14:03:28 -070065 mTextureID = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070066 mBufferID = 0;
Jason Samscf4c7c92009-12-14 12:57:40 -080067 mUploadDefered = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070068
Jason Samsfa84da22010-03-01 15:31:04 -080069 mUserBitmapCallback = NULL;
70 mUserBitmapCallbackData = NULL;
71
Jason Sams326e0dd2009-05-22 14:03:28 -070072 mType.set(type);
Jason Samse5ffb872009-08-09 17:01:55 -070073 rsAssert(type);
Jason Samsfa84da22010-03-01 15:31:04 -080074
75 mPtr = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070076}
77
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080078Allocation::~Allocation() {
Jason Samsfa84da22010-03-01 15:31:04 -080079 if (mUserBitmapCallback != NULL) {
80 mUserBitmapCallback(mUserBitmapCallbackData);
Jason Samsb89b0b72010-12-13 17:11:21 -080081 mPtr = NULL;
Jason Samsfa84da22010-03-01 15:31:04 -080082 }
Jason Samsb89b0b72010-12-13 17:11:21 -080083 freeScriptMemory();
Jason Samse402ed32009-11-03 11:25:42 -080084
85 if (mBufferID) {
86 // Causes a SW crash....
87 //LOGV(" mBufferID %i", mBufferID);
88 //glDeleteBuffers(1, &mBufferID);
89 //mBufferID = 0;
90 }
91 if (mTextureID) {
92 glDeleteTextures(1, &mTextureID);
93 mTextureID = 0;
94 }
Jason Sams326e0dd2009-05-22 14:03:28 -070095}
96
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080097void Allocation::setCpuWritable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -070098}
99
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800100void Allocation::setGpuWritable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700101}
102
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800103void Allocation::setCpuReadable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700104}
105
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800106void Allocation::setGpuReadable(bool) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700107}
108
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800109bool Allocation::fixAllocation() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700110 return false;
111}
112
Jason Samsb89b0b72010-12-13 17:11:21 -0800113void Allocation::deferedUploadToTexture(const Context *rsc) {
Jason Samsebc50192010-12-13 15:32:35 -0800114 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Samscf4c7c92009-12-14 12:57:40 -0800115 mUploadDefered = true;
116}
117
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800118uint32_t Allocation::getGLTarget() const {
Jason Samsebc50192010-12-13 15:32:35 -0800119 if (getIsTexture()) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800120 if (mType->getDimFaces()) {
121 return GL_TEXTURE_CUBE_MAP;
122 } else {
123 return GL_TEXTURE_2D;
124 }
125 }
Jason Samsebc50192010-12-13 15:32:35 -0800126 if (getIsBufferObject()) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800127 return GL_ARRAY_BUFFER;
128 }
129 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) {
Jason Samscf4c7c92009-12-14 12:57:40 -0800159
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
189 GLenum target = (GLenum)getGLTarget();
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800190 if (target == GL_TEXTURE_2D) {
Jason Samsb89b0b72010-12-13 17:11:21 -0800191 upload2DTexture(isFirstUpload, mPtr);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800192 } else if (target == GL_TEXTURE_CUBE_MAP) {
193 uploadCubeTexture(isFirstUpload);
194 }
195
Jason Samsb89b0b72010-12-13 17:11:21 -0800196 if (!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
197 freeScriptMemory();
198 }
199
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800200 rsc->checkError("Allocation::uploadToTexture");
201}
202
Jason Samsb89b0b72010-12-13 17:11:21 -0800203void Allocation::upload2DTexture(bool isFirstUpload, const void *ptr) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800204 GLenum type = mType->getElement()->getComponent().getGLType();
205 GLenum format = mType->getElement()->getComponent().getGLFormat();
206
Jason Samsb89b0b72010-12-13 17:11:21 -0800207 GLenum target = (GLenum)getGLTarget();
208 glBindTexture(target, mTextureID);
209 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700210
Jason Samsb89b0b72010-12-13 17:11:21 -0800211 for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
212 const uint8_t *p = (const uint8_t *)ptr;
213 p += mType->getLODOffset(lod);
214
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800215 if (isFirstUpload) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700216 glTexImage2D(GL_TEXTURE_2D, lod, format,
Jason Samsb89b0b72010-12-13 17:11:21 -0800217 mType->getLODDimX(lod), mType->getLODDimY(lod),
218 0, format, type, p);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700219 } else {
220 glTexSubImage2D(GL_TEXTURE_2D, lod, 0, 0,
Jason Samsb89b0b72010-12-13 17:11:21 -0800221 mType->getLODDimX(lod), mType->getLODDimY(lod),
222 format, type, p);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700223 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700224 }
Jason Samsb7e83bd2010-12-15 01:41:00 -0800225
226 if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
227#ifndef ANDROID_RS_BUILD_FOR_HOST
228 glGenerateMipmap(target);
229#endif //ANDROID_RS_BUILD_FOR_HOST
230 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800231}
Jason Sams7fabe1a2010-02-23 17:44:28 -0800232
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800233void Allocation::uploadCubeTexture(bool isFirstUpload) {
234 GLenum type = mType->getElement()->getComponent().getGLType();
235 GLenum format = mType->getElement()->getComponent().getGLFormat();
236
Jason Samsb89b0b72010-12-13 17:11:21 -0800237 GLenum target = (GLenum)getGLTarget();
238 glBindTexture(target, mTextureID);
239 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
240
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800241 GLenum faceOrder[] = {
242 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
243 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
244 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
245 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
246 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
247 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
248 };
249
250 Adapter2D adapt(getContext(), this);
251 for (uint32_t face = 0; face < 6; face ++) {
252 adapt.setFace(face);
253
Jason Samsb89b0b72010-12-13 17:11:21 -0800254 for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
255 adapt.setLOD(lod);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800256
257 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
258
259 if (isFirstUpload) {
260 glTexImage2D(faceOrder[face], lod, format,
261 adapt.getDimX(), adapt.getDimY(),
262 0, format, type, ptr);
263 } else {
264 glTexSubImage2D(faceOrder[face], lod, 0, 0,
265 adapt.getDimX(), adapt.getDimY(),
266 format, type, ptr);
267 }
268 }
269 }
Jason Samsb7e83bd2010-12-15 01:41:00 -0800270
271 if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
272#ifndef ANDROID_RS_BUILD_FOR_HOST
273 glGenerateMipmap(target);
274#endif //ANDROID_RS_BUILD_FOR_HOST
275 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700276}
277
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800278void Allocation::deferedUploadToBufferObject(const Context *rsc) {
Jason Samsebc50192010-12-13 15:32:35 -0800279 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Samscf4c7c92009-12-14 12:57:40 -0800280 mUploadDefered = true;
281}
282
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800283void Allocation::uploadToBufferObject(const Context *rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700284 rsAssert(!mType->getDimY());
285 rsAssert(!mType->getDimZ());
286
Jason Samsebc50192010-12-13 15:32:35 -0800287 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Samscf4c7c92009-12-14 12:57:40 -0800288
Jason Sams326e0dd2009-05-22 14:03:28 -0700289 if (!mBufferID) {
290 glGenBuffers(1, &mBufferID);
291 }
Jason Samscf4c7c92009-12-14 12:57:40 -0800292 if (!mBufferID) {
293 LOGE("Upload to buffer object failed");
294 mUploadDefered = true;
295 return;
296 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800297 GLenum target = (GLenum)getGLTarget();
298 glBindBuffer(target, mBufferID);
299 glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
300 glBindBuffer(target, 0);
Jason Samsc1ed5892010-03-10 17:30:41 -0800301 rsc->checkError("Allocation::uploadToBufferObject");
Jason Sams326e0dd2009-05-22 14:03:28 -0700302}
303
Jason Sams366c9c82010-12-08 16:14:36 -0800304void Allocation::uploadCheck(Context *rsc) {
Jason Samscf4c7c92009-12-14 12:57:40 -0800305 if (mUploadDefered) {
Jason Sams366c9c82010-12-08 16:14:36 -0800306 syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
Jason Samscf4c7c92009-12-14 12:57:40 -0800307 }
308}
309
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800310void Allocation::read(void *data) {
Jason Samse579df42009-08-10 14:55:26 -0700311 memcpy(data, mPtr, mType->getSizeBytes());
312}
313
Jason Sams4b45b892010-12-29 14:31:29 -0800314void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
315 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700316 uint32_t eSize = mType->getElementSizeBytes();
317 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
318 ptr += eSize * xoff;
Jason Sams9397e302009-08-27 20:23:34 -0700319 uint32_t size = count * eSize;
320
321 if (size != sizeBytes) {
322 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Samse12c1c52009-09-27 17:50:38 -0700323 mType->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700324 return;
325 }
Jason Samse3929c92010-08-09 18:13:33 -0700326
327 if (mType->getElement()->getHasReferences()) {
328 incRefs(data, count);
329 decRefs(ptr, count);
330 }
331
Jason Sams9397e302009-08-27 20:23:34 -0700332 memcpy(ptr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700333 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800334 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700335}
336
Jason Sams4b45b892010-12-29 14:31:29 -0800337void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800338 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700339 uint32_t eSize = mType->getElementSizeBytes();
340 uint32_t lineSize = eSize * w;
341 uint32_t destW = mType->getDimX();
342
343 const uint8_t *src = static_cast<const uint8_t *>(data);
344 uint8_t *dst = static_cast<uint8_t *>(mPtr);
345 dst += eSize * (xoff + yoff * destW);
Jason Sams9397e302009-08-27 20:23:34 -0700346
347 if ((lineSize * eSize * h) != sizeBytes) {
348 rsAssert(!"Allocation::subData called with mismatched size");
349 return;
350 }
351
Jason Sams326e0dd2009-05-22 14:03:28 -0700352 for (uint32_t line=yoff; line < (yoff+h); line++) {
Jason Samse3929c92010-08-09 18:13:33 -0700353 if (mType->getElement()->getHasReferences()) {
354 incRefs(src, w);
355 decRefs(dst, w);
356 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700357 memcpy(dst, src, lineSize);
358 src += lineSize;
359 dst += destW * eSize;
360 }
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700361 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800362 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700363}
364
Jason Sams4b45b892010-12-29 14:31:29 -0800365void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800366 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700367}
368
Jason Sams4b45b892010-12-29 14:31:29 -0800369void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800370 uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700371 uint32_t eSize = mType->getElementSizeBytes();
372 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
373 ptr += eSize * x;
374
375 if (cIdx >= mType->getElement()->getFieldCount()) {
376 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
377 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
378 return;
379 }
380
381 if (x >= mType->getDimX()) {
382 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
383 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
384 return;
385 }
386
387 const Element * e = mType->getElement()->getField(cIdx);
388 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
389
390 if (sizeBytes != e->getSizeBytes()) {
391 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
392 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
393 return;
394 }
395
396 if (e->getHasReferences()) {
397 e->incRefs(data);
398 e->decRefs(ptr);
399 }
400
401 memcpy(ptr, data, sizeBytes);
402 sendDirty();
403 mUploadDefered = true;
404}
405
Jason Sams4b45b892010-12-29 14:31:29 -0800406void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800407 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700408 uint32_t eSize = mType->getElementSizeBytes();
409 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
410 ptr += eSize * (x + y * mType->getDimX());
411
412 if (x >= mType->getDimX()) {
413 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
414 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
415 return;
416 }
417
418 if (y >= mType->getDimY()) {
419 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
420 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
421 return;
422 }
423
424 if (cIdx >= mType->getElement()->getFieldCount()) {
425 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
426 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
427 return;
428 }
429
430 const Element * e = mType->getElement()->getField(cIdx);
431 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
432
433 if (sizeBytes != e->getSizeBytes()) {
434 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
435 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
436 return;
437 }
438
439 if (e->getHasReferences()) {
440 e->incRefs(data);
441 e->decRefs(ptr);
442 }
443
444 memcpy(ptr, data, sizeBytes);
445 sendDirty();
446 mUploadDefered = true;
447}
448
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800449void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700450 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700451}
Jason Sams326e0dd2009-05-22 14:03:28 -0700452
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800453void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700454 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
455 if (mToDirtyList[ct] == p) {
456 mToDirtyList.removeAt(ct);
457 return;
458 }
459 }
460 rsAssert(0);
461}
462
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800463void Allocation::dumpLOGV(const char *prefix) const {
Jason Samsc21cf402009-11-17 17:26:46 -0800464 ObjectBase::dumpLOGV(prefix);
465
466 String8 s(prefix);
467 s.append(" type ");
468 if (mType.get()) {
469 mType->dumpLOGV(s.string());
470 }
471
472 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
473 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
474
Jason Samsebc50192010-12-13 15:32:35 -0800475 LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
476 prefix, mUsageFlags, mMipmapControl, mTextureID, mBufferID);
Jason Samsc21cf402009-11-17 17:26:46 -0800477}
Jason Sams326e0dd2009-05-22 14:03:28 -0700478
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800479void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700480 // Need to identify ourselves
481 stream->addU32((uint32_t)getClassId());
482
483 String8 name(getName());
484 stream->addString(&name);
485
486 // First thing we need to serialize is the type object since it will be needed
487 // to initialize the class
488 mType->serialize(stream);
489
490 uint32_t dataSize = mType->getSizeBytes();
491 // Write how much data we are storing
492 stream->addU32(dataSize);
493 // Now write the data
494 stream->addByteArray(mPtr, dataSize);
495}
496
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800497Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700498 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700499 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800500 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700501 LOGE("allocation loading skipped due to invalid class id\n");
502 return NULL;
503 }
504
505 String8 name;
506 stream->loadString(&name);
507
508 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800509 if (!type) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700510 return NULL;
511 }
512 type->compute();
513
514 // Number of bytes we wrote out for this allocation
515 uint32_t dataSize = stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800516 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700517 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Sams225afd32010-10-21 14:06:55 -0700518 ObjectBase::checkDelete(type);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700519 return NULL;
520 }
521
Jason Samsebc50192010-12-13 15:32:35 -0800522 Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700523 alloc->setName(name.string(), name.size());
524
Jason Sams4b45b892010-12-29 14:31:29 -0800525 uint32_t count = dataSize / type->getElementSizeBytes();
526
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700527 // Read in all of our allocation data
Jason Sams4b45b892010-12-29 14:31:29 -0800528 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700529 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700530
531 return alloc;
532}
533
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800534void Allocation::sendDirty() const {
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700535 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
536 mToDirtyList[ct]->forceDirty();
537 }
538}
Jason Sams326e0dd2009-05-22 14:03:28 -0700539
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800540void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700541 const uint8_t *p = static_cast<const uint8_t *>(ptr);
542 const Element *e = mType->getElement();
543 uint32_t stride = e->getSizeBytes();
544
Jason Sams96abf812010-10-05 13:32:49 -0700545 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700546 while (ct > 0) {
547 e->incRefs(p);
548 ct --;
549 p += stride;
550 }
551}
552
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800553void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samse3929c92010-08-09 18:13:33 -0700554 const uint8_t *p = static_cast<const uint8_t *>(ptr);
555 const Element *e = mType->getElement();
556 uint32_t stride = e->getSizeBytes();
557
Jason Sams96abf812010-10-05 13:32:49 -0700558 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700559 while (ct > 0) {
560 e->decRefs(p);
561 ct --;
562 p += stride;
563 }
564}
565
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800566void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams96abf812010-10-05 13:32:49 -0700567}
568
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800569void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700570 Type *t = mType->cloneAndResize1D(rsc, dimX);
571
572 uint32_t oldDimX = mType->getDimX();
573 if (dimX == oldDimX) {
574 return;
575 }
576
577 if (dimX < oldDimX) {
578 decRefs(mPtr, oldDimX - dimX, dimX);
579 }
580 mPtr = realloc(mPtr, t->getSizeBytes());
581
582 if (dimX > oldDimX) {
583 const Element *e = mType->getElement();
584 uint32_t stride = e->getSizeBytes();
585 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
586 }
587 mType.set(t);
588}
589
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800590void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700591 LOGE("not implemented");
592}
593
Jason Sams326e0dd2009-05-22 14:03:28 -0700594/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700595//
Jason Sams326e0dd2009-05-22 14:03:28 -0700596
597
598namespace android {
599namespace renderscript {
600
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800601void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700602 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samsb89b0b72010-12-13 17:11:21 -0800603 alloc->deferedUploadToTexture(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700604}
605
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800606void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700607 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samscf4c7c92009-12-14 12:57:40 -0800608 alloc->deferedUploadToBufferObject(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700609}
610
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800611static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700612 uint32_t w = out.getDimX();
613 uint32_t h = out.getDimY();
614
Jason Samse9f5c532009-07-28 17:20:11 -0700615 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700616 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
617 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
618 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
619
Jason Samse9f5c532009-07-28 17:20:11 -0700620 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700621 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
622 oPtr ++;
623 i1 += 2;
624 i2 += 2;
625 }
626 }
627}
628
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800629static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Sams565ac362009-06-03 16:04:54 -0700630 uint32_t w = out.getDimX();
631 uint32_t h = out.getDimY();
632
Jason Samse9f5c532009-07-28 17:20:11 -0700633 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700634 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
635 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
636 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
637
Jason Samse9f5c532009-07-28 17:20:11 -0700638 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700639 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700640 oPtr ++;
641 i1 += 2;
642 i2 += 2;
643 }
644 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700645}
646
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800647static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Sams2f6d8612010-01-19 17:53:54 -0800648 uint32_t w = out.getDimX();
649 uint32_t h = out.getDimY();
650
651 for (uint32_t y=0; y < h; y++) {
652 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
653 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
654 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
655
656 for (uint32_t x=0; x < w; x++) {
657 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
658 oPtr ++;
659 i1 += 2;
660 i2 += 2;
661 }
662 }
663}
664
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800665static void mip(const Adapter2D &out, const Adapter2D &in) {
666 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Samse9f5c532009-07-28 17:20:11 -0700667 case 32:
668 mip8888(out, in);
669 break;
670 case 16:
671 mip565(out, in);
672 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800673 case 8:
674 mip8(out, in);
675 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700676 }
Jason Samse9f5c532009-07-28 17:20:11 -0700677}
Jason Sams326e0dd2009-05-22 14:03:28 -0700678
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700679#ifndef ANDROID_RS_BUILD_FOR_HOST
680
Jason Sams366c9c82010-12-08 16:14:36 -0800681void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
682 Allocation *a = static_cast<Allocation *>(va);
683 a->syncAll(rsc, src);
684}
685
Jason Sams837e3882010-12-10 16:03:15 -0800686void rsi_AllocationCopyFromBitmap(Context *rsc, RsAllocation va, const void *data, size_t dataLen) {
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700687 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Sams837e3882010-12-10 16:03:15 -0800688 const Type * t = texAlloc->getType();
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700689
Jason Sams837e3882010-12-10 16:03:15 -0800690 uint32_t w = t->getDimX();
691 uint32_t h = t->getDimY();
692 bool genMips = t->getDimLOD();
693 size_t s = w * h * t->getElementSizeBytes();
694 if (s != dataLen) {
695 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
696 return;
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700697 }
Jason Sams837e3882010-12-10 16:03:15 -0800698
Jason Samsb89b0b72010-12-13 17:11:21 -0800699 if (texAlloc->getIsScript()) {
700 memcpy(texAlloc->getPtr(), data, s);
701 if (genMips) {
702 Adapter2D adapt(rsc, texAlloc);
703 Adapter2D adapt2(rsc, texAlloc);
704 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
705 adapt.setLOD(lod);
706 adapt2.setLOD(lod + 1);
707 mip(adapt2, adapt);
708 }
Jason Sams837e3882010-12-10 16:03:15 -0800709 }
Jason Samsb89b0b72010-12-13 17:11:21 -0800710 } else {
711 texAlloc->upload2DTexture(false, data);
Jason Sams837e3882010-12-10 16:03:15 -0800712 }
Jason Samsb89b0b72010-12-13 17:11:21 -0800713
Jason Sams837e3882010-12-10 16:03:15 -0800714}
715
716void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
717 Allocation *texAlloc = static_cast<Allocation *>(va);
718 const Type * t = texAlloc->getType();
719
720 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
721 if (s != dataLen) {
722 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
723 return;
724 }
725
726 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700727}
728
Jason Sams4b45b892010-12-29 14:31:29 -0800729void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
730 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700731 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800732 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700733}
734
Jason Sams4b45b892010-12-29 14:31:29 -0800735void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
736 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700737 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800738 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700739}
740
Jason Sams4b45b892010-12-29 14:31:29 -0800741void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
742 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700743 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800744 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700745}
746
Jason Sams4b45b892010-12-29 14:31:29 -0800747void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
748 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams5f0c84c2010-08-31 13:50:42 -0700749 Allocation *a = static_cast<Allocation *>(va);
Jason Sams4b45b892010-12-29 14:31:29 -0800750 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700751}
752
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800753void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
Jason Samse579df42009-08-10 14:55:26 -0700754 Allocation *a = static_cast<Allocation *>(va);
755 a->read(data);
756}
757
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800758void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams96abf812010-10-05 13:32:49 -0700759 Allocation *a = static_cast<Allocation *>(va);
760 a->resize1D(rsc, dimX);
761}
762
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800763void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams96abf812010-10-05 13:32:49 -0700764 Allocation *a = static_cast<Allocation *>(va);
765 a->resize2D(rsc, dimX, dimY);
766}
767
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700768#endif //ANDROID_RS_BUILD_FOR_HOST
769
770}
771}
772
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800773const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700774 Allocation *a = static_cast<Allocation *>(va);
775 a->getType()->incUserRef();
776
777 return a->getType();
778}
779
Jason Sams366c9c82010-12-08 16:14:36 -0800780RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800781 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800782 uint32_t usages) {
Jason Samsf0c1df42010-10-26 13:09:17 -0700783 Context *rsc = static_cast<Context *>(con);
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -0800784 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages, mips);
Jason Samsf0c1df42010-10-26 13:09:17 -0700785 alloc->incUserRef();
786 return alloc;
787}
788
Jason Sams366c9c82010-12-08 16:14:36 -0800789
790RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800791 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800792 const void *data, uint32_t usages) {
Jason Samsf0c1df42010-10-26 13:09:17 -0700793 Context *rsc = static_cast<Context *>(con);
Jason Sams366c9c82010-12-08 16:14:36 -0800794 Type *t = static_cast<Type *>(vtype);
Jason Samsf0c1df42010-10-26 13:09:17 -0700795
Jason Sams366c9c82010-12-08 16:14:36 -0800796 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages);
Jason Samsf0c1df42010-10-26 13:09:17 -0700797 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
798 if (texAlloc == NULL) {
799 LOGE("Memory allocation failure");
800 return NULL;
801 }
802
Jason Sams366c9c82010-12-08 16:14:36 -0800803 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsebc50192010-12-13 15:32:35 -0800804 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams366c9c82010-12-08 16:14:36 -0800805 Adapter2D adapt(rsc, texAlloc);
806 Adapter2D adapt2(rsc, texAlloc);
807 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
808 adapt.setLOD(lod);
809 adapt2.setLOD(lod + 1);
810 mip(adapt2, adapt);
Jason Samsf0c1df42010-10-26 13:09:17 -0700811 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700812 }
813
Jason Samsb89b0b72010-12-13 17:11:21 -0800814 texAlloc->deferedUploadToTexture(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700815 return texAlloc;
816}
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800817
Jason Sams366c9c82010-12-08 16:14:36 -0800818RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsebc50192010-12-13 15:32:35 -0800819 RsAllocationMipmapControl mips,
Jason Sams366c9c82010-12-08 16:14:36 -0800820 const void *data, uint32_t usages) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800821 Context *rsc = static_cast<Context *>(con);
Jason Sams366c9c82010-12-08 16:14:36 -0800822 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800823
824 // Cubemap allocation's faces should be Width by Width each.
825 // Source data should have 6 * Width by Width pixels
826 // Error checking is done in the java layer
Jason Sams366c9c82010-12-08 16:14:36 -0800827 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800828 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
829 if (texAlloc == NULL) {
830 LOGE("Memory allocation failure");
831 return NULL;
832 }
833
834 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams366c9c82010-12-08 16:14:36 -0800835 for (uint32_t face = 0; face < 6; face ++) {
836 Adapter2D faceAdapter(rsc, texAlloc);
837 faceAdapter.setFace(face);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800838
Jason Sams366c9c82010-12-08 16:14:36 -0800839 size_t cpySize = t->getDimX() * t->getDimX() * t->getElementSizeBytes();
840 memcpy(faceAdapter.getElement(0, 0), sourcePtr, cpySize);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800841
Jason Sams366c9c82010-12-08 16:14:36 -0800842 // Move the data pointer to the next cube face
843 sourcePtr += cpySize;
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800844
Jason Samsebc50192010-12-13 15:32:35 -0800845 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams366c9c82010-12-08 16:14:36 -0800846 Adapter2D adapt(rsc, texAlloc);
847 Adapter2D adapt2(rsc, texAlloc);
848 adapt.setFace(face);
849 adapt2.setFace(face);
850 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
851 adapt.setLOD(lod);
852 adapt2.setLOD(lod + 1);
853 mip(adapt2, adapt);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800854 }
855 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800856 }
857
Jason Samsb89b0b72010-12-13 17:11:21 -0800858 texAlloc->deferedUploadToTexture(rsc);
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800859 return texAlloc;
860}