blob: aab789ae94070ed8d91a7ae8828e6dcfa81fd7f9 [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 */
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070016#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsd19f10d2009-05-22 14:03:28 -070017#include "rsContext.h"
18
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 Sakhartchoukaa7d2882010-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 Sams4b962e52009-06-22 17:15:15 -070028
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -070029#include "utils/StopWatch.h"
30
Jason Samsd19f10d2009-05-22 14:03:28 -070031using namespace android;
32using namespace android::renderscript;
33
Jason Sams5476b452010-12-08 16:14:36 -080034Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages) : ObjectBase(rsc) {
Jason Sams8a64743f2010-03-01 15:31:04 -080035 init(rsc, type);
36
Jason Sams5476b452010-12-08 16:14:36 -080037 mUsageFlags = usages;
38
Jason Sams8a64743f2010-03-01 15:31:04 -080039 mPtr = malloc(mType->getSizeBytes());
Jason Samsee734982010-08-12 12:44:02 -070040 if (mType->getElement()->getHasReferences()) {
41 memset(mPtr, 0, mType->getSizeBytes());
42 }
Jason Sams8a64743f2010-03-01 15:31:04 -080043 if (!mPtr) {
44 LOGE("Allocation::Allocation, alloc failure");
45 }
46}
47
Jason Sams8a64743f2010-03-01 15:31:04 -080048
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080049void Allocation::init(Context *rsc, const Type *type) {
Jason Samsd19f10d2009-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 Samsd4b23b52010-12-13 15:32:35 -080059 mUsageFlags = 0;
60 mMipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsd19f10d2009-05-22 14:03:28 -070061
Jason Samsd19f10d2009-05-22 14:03:28 -070062 mTextureID = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070063 mBufferID = 0;
Jason Sams3b7d39b2009-12-14 12:57:40 -080064 mUploadDefered = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070065
Jason Sams8a64743f2010-03-01 15:31:04 -080066 mUserBitmapCallback = NULL;
67 mUserBitmapCallbackData = NULL;
68
Jason Samsd19f10d2009-05-22 14:03:28 -070069 mType.set(type);
Jason Sams1bada8c2009-08-09 17:01:55 -070070 rsAssert(type);
Jason Sams8a64743f2010-03-01 15:31:04 -080071
72 mPtr = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -070073}
74
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080075Allocation::~Allocation() {
Jason Sams8a64743f2010-03-01 15:31:04 -080076 if (mUserBitmapCallback != NULL) {
77 mUserBitmapCallback(mUserBitmapCallbackData);
78 } else {
79 free(mPtr);
80 }
Jason Samsb7a6c432009-11-02 14:25:10 -080081 mPtr = NULL;
Jason Sams9d5e03d2009-11-03 11:25:42 -080082
83 if (mBufferID) {
84 // Causes a SW crash....
85 //LOGV(" mBufferID %i", mBufferID);
86 //glDeleteBuffers(1, &mBufferID);
87 //mBufferID = 0;
88 }
89 if (mTextureID) {
90 glDeleteTextures(1, &mTextureID);
91 mTextureID = 0;
92 }
Jason Samsd19f10d2009-05-22 14:03:28 -070093}
94
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080095void Allocation::setCpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -070096}
97
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080098void Allocation::setGpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -070099}
100
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800101void Allocation::setCpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700102}
103
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800104void Allocation::setGpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700105}
106
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800107bool Allocation::fixAllocation() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700108 return false;
109}
110
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800111void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800112 rsAssert(lodOffset < mType->getLODCount());
Jason Samsd4b23b52010-12-13 15:32:35 -0800113 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800114 mTextureLOD = lodOffset;
115 mUploadDefered = true;
Jason Samsc2908e62010-02-23 17:44:28 -0800116 mTextureGenMipmap = !mType->getDimLOD() && genMipmap;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800117}
118
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800119uint32_t Allocation::getGLTarget() const {
Jason Samsd4b23b52010-12-13 15:32:35 -0800120 if (getIsTexture()) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800121 if (mType->getDimFaces()) {
122 return GL_TEXTURE_CUBE_MAP;
123 } else {
124 return GL_TEXTURE_2D;
125 }
126 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800127 if (getIsBufferObject()) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800128 return GL_ARRAY_BUFFER;
129 }
130 return 0;
131}
132
Jason Sams5476b452010-12-08 16:14:36 -0800133void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
134 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
135
Jason Samsd4b23b52010-12-13 15:32:35 -0800136 if (getIsTexture()) {
Jason Sams5476b452010-12-08 16:14:36 -0800137 uploadToTexture(rsc);
138 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800139 if (getIsBufferObject()) {
Jason Sams5476b452010-12-08 16:14:36 -0800140 uploadToBufferObject(rsc);
141 }
142
143 mUploadDefered = false;
144}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800145
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800146void Allocation::uploadToTexture(const Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800147
Jason Samsd4b23b52010-12-13 15:32:35 -0800148 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Sams718cd1f2009-12-23 14:35:29 -0800149 GLenum type = mType->getElement()->getComponent().getGLType();
150 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Samse2ae85f2009-06-03 16:04:54 -0700151
152 if (!type || !format) {
153 return;
154 }
155
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700156 bool isFirstUpload = false;
157
Jason Samsd19f10d2009-05-22 14:03:28 -0700158 if (!mTextureID) {
159 glGenTextures(1, &mTextureID);
Jason Sams9dab6672009-11-24 12:26:35 -0800160
161 if (!mTextureID) {
162 // This should not happen, however, its likely the cause of the
163 // white sqare bug.
164 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
165 LOGE("Upload to texture failed to gen mTextureID");
166 rsc->dumpDebug();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800167 mUploadDefered = true;
168 return;
Jason Sams9dab6672009-11-24 12:26:35 -0800169 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700170 isFirstUpload = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700171 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800172
173 GLenum target = (GLenum)getGLTarget();
174 glBindTexture(target, mTextureID);
Jason Sams0e27b5c2009-11-05 12:44:58 -0800175 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -0700176
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800177 if (target == GL_TEXTURE_2D) {
178 upload2DTexture(isFirstUpload);
179 } else if (target == GL_TEXTURE_CUBE_MAP) {
180 uploadCubeTexture(isFirstUpload);
181 }
182
183 if (mTextureGenMipmap) {
184#ifndef ANDROID_RS_BUILD_FOR_HOST
185 glGenerateMipmap(target);
186#endif //ANDROID_RS_BUILD_FOR_HOST
187 }
188
189 rsc->checkError("Allocation::uploadToTexture");
190}
191
192void Allocation::upload2DTexture(bool isFirstUpload) {
193 GLenum type = mType->getElement()->getComponent().getGLType();
194 GLenum format = mType->getElement()->getComponent().getGLFormat();
195
Jason Samsa9e7a052009-09-25 14:51:22 -0700196 Adapter2D adapt(getContext(), this);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800197 for (uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800198 adapt.setLOD(lod+mTextureLOD);
Jason Samsd19f10d2009-05-22 14:03:28 -0700199
200 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800201 if (isFirstUpload) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700202 glTexImage2D(GL_TEXTURE_2D, lod, format,
203 adapt.getDimX(), adapt.getDimY(),
204 0, format, type, ptr);
205 } else {
206 glTexSubImage2D(GL_TEXTURE_2D, lod, 0, 0,
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800207 adapt.getDimX(), adapt.getDimY(),
208 format, type, ptr);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700209 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700210 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800211}
Jason Samsc2908e62010-02-23 17:44:28 -0800212
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800213void Allocation::uploadCubeTexture(bool isFirstUpload) {
214 GLenum type = mType->getElement()->getComponent().getGLType();
215 GLenum format = mType->getElement()->getComponent().getGLFormat();
216
217 GLenum faceOrder[] = {
218 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
219 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
220 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
221 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
222 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
223 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
224 };
225
226 Adapter2D adapt(getContext(), this);
227 for (uint32_t face = 0; face < 6; face ++) {
228 adapt.setFace(face);
229
230 for (uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
231 adapt.setLOD(lod+mTextureLOD);
232
233 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
234
235 if (isFirstUpload) {
236 glTexImage2D(faceOrder[face], lod, format,
237 adapt.getDimX(), adapt.getDimY(),
238 0, format, type, ptr);
239 } else {
240 glTexSubImage2D(faceOrder[face], lod, 0, 0,
241 adapt.getDimX(), adapt.getDimY(),
242 format, type, ptr);
243 }
244 }
245 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700246}
247
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800248void Allocation::deferedUploadToBufferObject(const Context *rsc) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800249 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800250 mUploadDefered = true;
251}
252
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800253void Allocation::uploadToBufferObject(const Context *rsc) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700254 rsAssert(!mType->getDimY());
255 rsAssert(!mType->getDimZ());
256
Jason Samsd4b23b52010-12-13 15:32:35 -0800257 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800258
Jason Samsd19f10d2009-05-22 14:03:28 -0700259 if (!mBufferID) {
260 glGenBuffers(1, &mBufferID);
261 }
Jason Sams3b7d39b2009-12-14 12:57:40 -0800262 if (!mBufferID) {
263 LOGE("Upload to buffer object failed");
264 mUploadDefered = true;
265 return;
266 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800267 GLenum target = (GLenum)getGLTarget();
268 glBindBuffer(target, mBufferID);
269 glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
270 glBindBuffer(target, 0);
Jason Samsf4687002010-03-10 17:30:41 -0800271 rsc->checkError("Allocation::uploadToBufferObject");
Jason Samsd19f10d2009-05-22 14:03:28 -0700272}
273
Jason Sams5476b452010-12-08 16:14:36 -0800274void Allocation::uploadCheck(Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800275 if (mUploadDefered) {
Jason Sams5476b452010-12-08 16:14:36 -0800276 syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800277 }
278}
279
Jason Sams1bada8c2009-08-09 17:01:55 -0700280
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800281void Allocation::data(Context *rsc, const void *data, uint32_t sizeBytes) {
Jason Sams07ae4062009-08-27 20:23:34 -0700282 uint32_t size = mType->getSizeBytes();
283 if (size != sizeBytes) {
284 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
285 return;
286 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700287
288 if (mType->getElement()->getHasReferences()) {
289 incRefs(data, sizeBytes / mType->getElement()->getSizeBytes());
290 decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes());
291 }
292
Jason Sams07ae4062009-08-27 20:23:34 -0700293 memcpy(mPtr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700294 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800295 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700296}
297
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800298void Allocation::read(void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700299 memcpy(data, mPtr, mType->getSizeBytes());
300}
301
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800302void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700303 uint32_t eSize = mType->getElementSizeBytes();
304 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
305 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700306 uint32_t size = count * eSize;
307
308 if (size != sizeBytes) {
309 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Sams3c0dfba2009-09-27 17:50:38 -0700310 mType->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -0700311 return;
312 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700313
314 if (mType->getElement()->getHasReferences()) {
315 incRefs(data, count);
316 decRefs(ptr, count);
317 }
318
Jason Sams07ae4062009-08-27 20:23:34 -0700319 memcpy(ptr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700320 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800321 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700322}
323
Jason Sams49bdaf02010-08-31 13:50:42 -0700324void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800325 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700326 uint32_t eSize = mType->getElementSizeBytes();
327 uint32_t lineSize = eSize * w;
328 uint32_t destW = mType->getDimX();
329
330 const uint8_t *src = static_cast<const uint8_t *>(data);
331 uint8_t *dst = static_cast<uint8_t *>(mPtr);
332 dst += eSize * (xoff + yoff * destW);
Jason Sams07ae4062009-08-27 20:23:34 -0700333
334 if ((lineSize * eSize * h) != sizeBytes) {
335 rsAssert(!"Allocation::subData called with mismatched size");
336 return;
337 }
338
Jason Samsd19f10d2009-05-22 14:03:28 -0700339 for (uint32_t line=yoff; line < (yoff+h); line++) {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700340 if (mType->getElement()->getHasReferences()) {
341 incRefs(src, w);
342 decRefs(dst, w);
343 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700344 memcpy(dst, src, lineSize);
345 src += lineSize;
346 dst += destW * eSize;
347 }
Jason Sams83f1c632009-10-26 15:19:28 -0700348 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800349 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700350}
351
Jason Sams49bdaf02010-08-31 13:50:42 -0700352void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800353 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700354}
355
Jason Sams49bdaf02010-08-31 13:50:42 -0700356void Allocation::subElementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800357 uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700358 uint32_t eSize = mType->getElementSizeBytes();
359 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
360 ptr += eSize * x;
361
362 if (cIdx >= mType->getElement()->getFieldCount()) {
363 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
364 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
365 return;
366 }
367
368 if (x >= mType->getDimX()) {
369 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
370 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
371 return;
372 }
373
374 const Element * e = mType->getElement()->getField(cIdx);
375 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
376
377 if (sizeBytes != e->getSizeBytes()) {
378 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
379 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
380 return;
381 }
382
383 if (e->getHasReferences()) {
384 e->incRefs(data);
385 e->decRefs(ptr);
386 }
387
388 memcpy(ptr, data, sizeBytes);
389 sendDirty();
390 mUploadDefered = true;
391}
392
393void Allocation::subElementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800394 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700395 uint32_t eSize = mType->getElementSizeBytes();
396 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
397 ptr += eSize * (x + y * mType->getDimX());
398
399 if (x >= mType->getDimX()) {
400 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
401 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
402 return;
403 }
404
405 if (y >= mType->getDimY()) {
406 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
407 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
408 return;
409 }
410
411 if (cIdx >= mType->getElement()->getFieldCount()) {
412 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
413 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
414 return;
415 }
416
417 const Element * e = mType->getElement()->getField(cIdx);
418 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
419
420 if (sizeBytes != e->getSizeBytes()) {
421 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
422 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
423 return;
424 }
425
426 if (e->getHasReferences()) {
427 e->incRefs(data);
428 e->decRefs(ptr);
429 }
430
431 memcpy(ptr, data, sizeBytes);
432 sendDirty();
433 mUploadDefered = true;
434}
435
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800436void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700437 mToDirtyList.push(p);
Jason Sams83f1c632009-10-26 15:19:28 -0700438}
Jason Samsd19f10d2009-05-22 14:03:28 -0700439
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800440void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams83f1c632009-10-26 15:19:28 -0700441 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
442 if (mToDirtyList[ct] == p) {
443 mToDirtyList.removeAt(ct);
444 return;
445 }
446 }
447 rsAssert(0);
448}
449
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800450void Allocation::dumpLOGV(const char *prefix) const {
Jason Sams715333b2009-11-17 17:26:46 -0800451 ObjectBase::dumpLOGV(prefix);
452
453 String8 s(prefix);
454 s.append(" type ");
455 if (mType.get()) {
456 mType->dumpLOGV(s.string());
457 }
458
459 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
460 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
461
Jason Samsd4b23b52010-12-13 15:32:35 -0800462 LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
463 prefix, mUsageFlags, mMipmapControl, mTextureID, mBufferID);
Jason Sams715333b2009-11-17 17:26:46 -0800464}
Jason Samsd19f10d2009-05-22 14:03:28 -0700465
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800466void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700467 // Need to identify ourselves
468 stream->addU32((uint32_t)getClassId());
469
470 String8 name(getName());
471 stream->addString(&name);
472
473 // First thing we need to serialize is the type object since it will be needed
474 // to initialize the class
475 mType->serialize(stream);
476
477 uint32_t dataSize = mType->getSizeBytes();
478 // Write how much data we are storing
479 stream->addU32(dataSize);
480 // Now write the data
481 stream->addByteArray(mPtr, dataSize);
482}
483
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800484Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700485 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700486 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800487 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700488 LOGE("allocation loading skipped due to invalid class id\n");
489 return NULL;
490 }
491
492 String8 name;
493 stream->loadString(&name);
494
495 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800496 if (!type) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700497 return NULL;
498 }
499 type->compute();
500
501 // Number of bytes we wrote out for this allocation
502 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800503 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700504 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Samsb38d5342010-10-21 14:06:55 -0700505 ObjectBase::checkDelete(type);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700506 return NULL;
507 }
508
Jason Samsd4b23b52010-12-13 15:32:35 -0800509 Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700510 alloc->setName(name.string(), name.size());
511
512 // Read in all of our allocation data
Jason Sams49bdaf02010-08-31 13:50:42 -0700513 alloc->data(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2ce0e3f2010-08-11 10:30:44 -0700514 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700515
516 return alloc;
517}
518
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800519void Allocation::sendDirty() const {
Jason Sams83f1c632009-10-26 15:19:28 -0700520 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
521 mToDirtyList[ct]->forceDirty();
522 }
523}
Jason Samsd19f10d2009-05-22 14:03:28 -0700524
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800525void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700526 const uint8_t *p = static_cast<const uint8_t *>(ptr);
527 const Element *e = mType->getElement();
528 uint32_t stride = e->getSizeBytes();
529
Jason Sams5edc6082010-10-05 13:32:49 -0700530 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700531 while (ct > 0) {
532 e->incRefs(p);
533 ct --;
534 p += stride;
535 }
536}
537
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800538void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700539 const uint8_t *p = static_cast<const uint8_t *>(ptr);
540 const Element *e = mType->getElement();
541 uint32_t stride = e->getSizeBytes();
542
Jason Sams5edc6082010-10-05 13:32:49 -0700543 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700544 while (ct > 0) {
545 e->decRefs(p);
546 ct --;
547 p += stride;
548 }
549}
550
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800551void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams5edc6082010-10-05 13:32:49 -0700552}
553
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800554void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700555 Type *t = mType->cloneAndResize1D(rsc, dimX);
556
557 uint32_t oldDimX = mType->getDimX();
558 if (dimX == oldDimX) {
559 return;
560 }
561
562 if (dimX < oldDimX) {
563 decRefs(mPtr, oldDimX - dimX, dimX);
564 }
565 mPtr = realloc(mPtr, t->getSizeBytes());
566
567 if (dimX > oldDimX) {
568 const Element *e = mType->getElement();
569 uint32_t stride = e->getSizeBytes();
570 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
571 }
572 mType.set(t);
573}
574
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800575void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700576 LOGE("not implemented");
577}
578
Jason Samsd19f10d2009-05-22 14:03:28 -0700579/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700580//
Jason Samsd19f10d2009-05-22 14:03:28 -0700581
582
583namespace android {
584namespace renderscript {
585
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800586void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700587 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samsc2908e62010-02-23 17:44:28 -0800588 alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel);
Jason Samsd19f10d2009-05-22 14:03:28 -0700589}
590
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800591void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700592 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800593 alloc->deferedUploadToBufferObject(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700594}
595
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800596static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700597 uint32_t w = out.getDimX();
598 uint32_t h = out.getDimY();
599
Jason Sams6f5c61c2009-07-28 17:20:11 -0700600 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700601 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
602 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
603 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
604
Jason Sams6f5c61c2009-07-28 17:20:11 -0700605 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700606 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
607 oPtr ++;
608 i1 += 2;
609 i2 += 2;
610 }
611 }
612}
613
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800614static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700615 uint32_t w = out.getDimX();
616 uint32_t h = out.getDimY();
617
Jason Sams6f5c61c2009-07-28 17:20:11 -0700618 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700619 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
620 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
621 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
622
Jason Sams6f5c61c2009-07-28 17:20:11 -0700623 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700624 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700625 oPtr ++;
626 i1 += 2;
627 i2 += 2;
628 }
629 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700630}
631
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800632static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Samse20e3b42010-01-19 17:53:54 -0800633 uint32_t w = out.getDimX();
634 uint32_t h = out.getDimY();
635
636 for (uint32_t y=0; y < h; y++) {
637 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
638 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
639 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
640
641 for (uint32_t x=0; x < w; x++) {
642 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
643 oPtr ++;
644 i1 += 2;
645 i2 += 2;
646 }
647 }
648}
649
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800650static void mip(const Adapter2D &out, const Adapter2D &in) {
651 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Sams6f5c61c2009-07-28 17:20:11 -0700652 case 32:
653 mip8888(out, in);
654 break;
655 case 16:
656 mip565(out, in);
657 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800658 case 8:
659 mip8(out, in);
660 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700661 }
Jason Sams6f5c61c2009-07-28 17:20:11 -0700662}
Jason Samsd19f10d2009-05-22 14:03:28 -0700663
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700664#ifndef ANDROID_RS_BUILD_FOR_HOST
665
Jason Sams5476b452010-12-08 16:14:36 -0800666void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
667 Allocation *a = static_cast<Allocation *>(va);
668 a->syncAll(rsc, src);
669}
670
Jason Sams4ef66502010-12-10 16:03:15 -0800671void rsi_AllocationCopyFromBitmap(Context *rsc, RsAllocation va, const void *data, size_t dataLen) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700672 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Sams4ef66502010-12-10 16:03:15 -0800673 const Type * t = texAlloc->getType();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700674
Jason Sams4ef66502010-12-10 16:03:15 -0800675 uint32_t w = t->getDimX();
676 uint32_t h = t->getDimY();
677 bool genMips = t->getDimLOD();
678 size_t s = w * h * t->getElementSizeBytes();
679 if (s != dataLen) {
680 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
681 return;
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700682 }
Jason Sams4ef66502010-12-10 16:03:15 -0800683
684 memcpy(texAlloc->getPtr(), data, s);
685 if (genMips) {
686 Adapter2D adapt(rsc, texAlloc);
687 Adapter2D adapt2(rsc, texAlloc);
688 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
689 adapt.setLOD(lod);
690 adapt2.setLOD(lod + 1);
691 mip(adapt2, adapt);
692 }
693 }
694}
695
696void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
697 Allocation *texAlloc = static_cast<Allocation *>(va);
698 const Type * t = texAlloc->getType();
699
700 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
701 if (s != dataLen) {
702 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
703 return;
704 }
705
706 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700707}
708
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800709void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700710 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700711 a->data(rsc, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700712}
713
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800714void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700715 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700716 a->subData(rsc, xoff, count, data, sizeBytes);
717}
718
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800719void rsi_Allocation2DSubElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700720 Allocation *a = static_cast<Allocation *>(va);
721 a->subElementData(rsc, x, y, data, eoff, sizeBytes);
722}
723
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800724void rsi_Allocation1DSubElementData(Context *rsc, RsAllocation va, uint32_t x, const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700725 Allocation *a = static_cast<Allocation *>(va);
726 a->subElementData(rsc, x, data, eoff, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700727}
728
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800729void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700730 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700731 a->subData(rsc, xoff, yoff, 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#endif //ANDROID_RS_BUILD_FOR_HOST
750
751}
752}
753
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800754const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700755 Allocation *a = static_cast<Allocation *>(va);
756 a->getType()->incUserRef();
757
758 return a->getType();
759}
760
Jason Sams5476b452010-12-08 16:14:36 -0800761RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800762 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800763 uint32_t usages) {
Jason Sams31a7e422010-10-26 13:09:17 -0700764 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800765 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages);
Jason Sams31a7e422010-10-26 13:09:17 -0700766 alloc->incUserRef();
767 return alloc;
768}
769
Jason Sams5476b452010-12-08 16:14:36 -0800770
771RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800772 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800773 const void *data, uint32_t usages) {
Jason Sams31a7e422010-10-26 13:09:17 -0700774 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800775 Type *t = static_cast<Type *>(vtype);
Jason Sams31a7e422010-10-26 13:09:17 -0700776
Jason Sams5476b452010-12-08 16:14:36 -0800777 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages);
Jason Sams31a7e422010-10-26 13:09:17 -0700778 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
779 if (texAlloc == NULL) {
780 LOGE("Memory allocation failure");
781 return NULL;
782 }
783
Jason Sams5476b452010-12-08 16:14:36 -0800784 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsd4b23b52010-12-13 15:32:35 -0800785 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams5476b452010-12-08 16:14:36 -0800786 Adapter2D adapt(rsc, texAlloc);
787 Adapter2D adapt2(rsc, texAlloc);
788 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
789 adapt.setLOD(lod);
790 adapt2.setLOD(lod + 1);
791 mip(adapt2, adapt);
Jason Sams31a7e422010-10-26 13:09:17 -0700792 }
Jason Sams31a7e422010-10-26 13:09:17 -0700793 }
794
Jason Samsd4b23b52010-12-13 15:32:35 -0800795 texAlloc->deferedUploadToTexture(rsc, false, 0);
Jason Sams31a7e422010-10-26 13:09:17 -0700796 return texAlloc;
797}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800798
Jason Sams5476b452010-12-08 16:14:36 -0800799RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800800 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800801 const void *data, uint32_t usages) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800802 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800803 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800804
805 // Cubemap allocation's faces should be Width by Width each.
806 // Source data should have 6 * Width by Width pixels
807 // Error checking is done in the java layer
Jason Sams5476b452010-12-08 16:14:36 -0800808 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800809 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
810 if (texAlloc == NULL) {
811 LOGE("Memory allocation failure");
812 return NULL;
813 }
814
815 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams5476b452010-12-08 16:14:36 -0800816 for (uint32_t face = 0; face < 6; face ++) {
817 Adapter2D faceAdapter(rsc, texAlloc);
818 faceAdapter.setFace(face);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800819
Jason Sams5476b452010-12-08 16:14:36 -0800820 size_t cpySize = t->getDimX() * t->getDimX() * t->getElementSizeBytes();
821 memcpy(faceAdapter.getElement(0, 0), sourcePtr, cpySize);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800822
Jason Sams5476b452010-12-08 16:14:36 -0800823 // Move the data pointer to the next cube face
824 sourcePtr += cpySize;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800825
Jason Samsd4b23b52010-12-13 15:32:35 -0800826 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams5476b452010-12-08 16:14:36 -0800827 Adapter2D adapt(rsc, texAlloc);
828 Adapter2D adapt2(rsc, texAlloc);
829 adapt.setFace(face);
830 adapt2.setFace(face);
831 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
832 adapt.setLOD(lod);
833 adapt2.setLOD(lod + 1);
834 mip(adapt2, adapt);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800835 }
836 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800837 }
838
Jason Samsd4b23b52010-12-13 15:32:35 -0800839 texAlloc->deferedUploadToTexture(rsc, false, 0);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800840 return texAlloc;
841}