blob: 2ed77749a31148355390ffe6591e3d19a23d889d [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
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -080031static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va);
32
Jason Samsd19f10d2009-05-22 14:03:28 -070033using namespace android;
34using namespace android::renderscript;
35
Alex Sakhartchouk08571962010-12-15 09:59:58 -080036Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
37 RsAllocationMipmapControl mc)
38 : ObjectBase(rsc) {
Jason Sams8a64743f2010-03-01 15:31:04 -080039 init(rsc, type);
40
Jason Sams5476b452010-12-08 16:14:36 -080041 mUsageFlags = usages;
Alex Sakhartchouk08571962010-12-15 09:59:58 -080042 mMipmapControl = mc;
Jason Sams5476b452010-12-08 16:14:36 -080043
Jason Sams5e0035a2010-12-13 17:11:21 -080044 allocScriptMemory();
Jason Samsee734982010-08-12 12:44:02 -070045 if (mType->getElement()->getHasReferences()) {
46 memset(mPtr, 0, mType->getSizeBytes());
47 }
Jason Sams8a64743f2010-03-01 15:31:04 -080048 if (!mPtr) {
49 LOGE("Allocation::Allocation, alloc failure");
50 }
51}
52
Jason Sams8a64743f2010-03-01 15:31:04 -080053
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080054void Allocation::init(Context *rsc, const Type *type) {
Jason Samsd19f10d2009-05-22 14:03:28 -070055 mPtr = NULL;
56
57 mCpuWrite = false;
58 mCpuRead = false;
59 mGpuWrite = false;
60 mGpuRead = false;
61
62 mReadWriteRatio = 0;
63 mUpdateSize = 0;
Jason Samsd4b23b52010-12-13 15:32:35 -080064 mUsageFlags = 0;
65 mMipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsd19f10d2009-05-22 14:03:28 -070066
Jason Samsd19f10d2009-05-22 14:03:28 -070067 mTextureID = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070068 mBufferID = 0;
Jason Sams3b7d39b2009-12-14 12:57:40 -080069 mUploadDefered = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070070
Jason Sams8a64743f2010-03-01 15:31:04 -080071 mUserBitmapCallback = NULL;
72 mUserBitmapCallbackData = NULL;
73
Jason Samsd19f10d2009-05-22 14:03:28 -070074 mType.set(type);
Jason Sams1bada8c2009-08-09 17:01:55 -070075 rsAssert(type);
Jason Sams8a64743f2010-03-01 15:31:04 -080076
77 mPtr = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -070078}
79
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080080Allocation::~Allocation() {
Jason Sams8a64743f2010-03-01 15:31:04 -080081 if (mUserBitmapCallback != NULL) {
82 mUserBitmapCallback(mUserBitmapCallbackData);
Jason Sams5e0035a2010-12-13 17:11:21 -080083 mPtr = NULL;
Jason Sams8a64743f2010-03-01 15:31:04 -080084 }
Jason Sams5e0035a2010-12-13 17:11:21 -080085 freeScriptMemory();
Jason Sams9d5e03d2009-11-03 11:25:42 -080086
87 if (mBufferID) {
88 // Causes a SW crash....
89 //LOGV(" mBufferID %i", mBufferID);
90 //glDeleteBuffers(1, &mBufferID);
91 //mBufferID = 0;
92 }
93 if (mTextureID) {
94 glDeleteTextures(1, &mTextureID);
95 mTextureID = 0;
96 }
Jason Samsd19f10d2009-05-22 14:03:28 -070097}
98
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080099void Allocation::setCpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700100}
101
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800102void Allocation::setGpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700103}
104
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800105void Allocation::setCpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700106}
107
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800108void Allocation::setGpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700109}
110
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800111bool Allocation::fixAllocation() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700112 return false;
113}
114
Jason Sams5e0035a2010-12-13 17:11:21 -0800115void Allocation::deferedUploadToTexture(const Context *rsc) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800116 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800117 mUploadDefered = true;
118}
119
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800120uint32_t Allocation::getGLTarget() const {
Jason Samsd4b23b52010-12-13 15:32:35 -0800121 if (getIsTexture()) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800122 if (mType->getDimFaces()) {
123 return GL_TEXTURE_CUBE_MAP;
124 } else {
125 return GL_TEXTURE_2D;
126 }
127 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800128 if (getIsBufferObject()) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800129 return GL_ARRAY_BUFFER;
130 }
131 return 0;
132}
133
Jason Sams5e0035a2010-12-13 17:11:21 -0800134void Allocation::allocScriptMemory() {
135 rsAssert(!mPtr);
136 mPtr = malloc(mType->getSizeBytes());
137}
138
139void Allocation::freeScriptMemory() {
Jason Sams5e0035a2010-12-13 17:11:21 -0800140 if (mPtr) {
141 free(mPtr);
142 mPtr = NULL;
143 }
144}
145
146
Jason Sams5476b452010-12-08 16:14:36 -0800147void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
148 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
149
Jason Samsd4b23b52010-12-13 15:32:35 -0800150 if (getIsTexture()) {
Jason Sams5476b452010-12-08 16:14:36 -0800151 uploadToTexture(rsc);
152 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800153 if (getIsBufferObject()) {
Jason Sams5476b452010-12-08 16:14:36 -0800154 uploadToBufferObject(rsc);
155 }
156
157 mUploadDefered = false;
158}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800159
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800160void Allocation::uploadToTexture(const Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800161
Jason Samsd4b23b52010-12-13 15:32:35 -0800162 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Sams718cd1f2009-12-23 14:35:29 -0800163 GLenum type = mType->getElement()->getComponent().getGLType();
164 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Samse2ae85f2009-06-03 16:04:54 -0700165
166 if (!type || !format) {
167 return;
168 }
169
Jason Sams5e0035a2010-12-13 17:11:21 -0800170 if (!mPtr) {
171 return;
172 }
173
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700174 bool isFirstUpload = false;
175
Jason Samsd19f10d2009-05-22 14:03:28 -0700176 if (!mTextureID) {
177 glGenTextures(1, &mTextureID);
Jason Sams9dab6672009-11-24 12:26:35 -0800178
179 if (!mTextureID) {
180 // This should not happen, however, its likely the cause of the
181 // white sqare bug.
182 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
183 LOGE("Upload to texture failed to gen mTextureID");
184 rsc->dumpDebug();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800185 mUploadDefered = true;
186 return;
Jason Sams9dab6672009-11-24 12:26:35 -0800187 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700188 isFirstUpload = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700189 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800190
191 GLenum target = (GLenum)getGLTarget();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800192 if (target == GL_TEXTURE_2D) {
Jason Sams5e0035a2010-12-13 17:11:21 -0800193 upload2DTexture(isFirstUpload, mPtr);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800194 } else if (target == GL_TEXTURE_CUBE_MAP) {
195 uploadCubeTexture(isFirstUpload);
196 }
197
Jason Sams5e0035a2010-12-13 17:11:21 -0800198 if (!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
199 freeScriptMemory();
200 }
201
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800202 rsc->checkError("Allocation::uploadToTexture");
203}
204
Jason Samsfb9f82c2011-01-12 14:53:25 -0800205void Allocation::update2DTexture(const void *ptr, uint32_t xoff, uint32_t yoff,
206 uint32_t lod, RsAllocationCubemapFace face,
207 uint32_t w, uint32_t h) {
208 GLenum type = mType->getElement()->getComponent().getGLType();
209 GLenum format = mType->getElement()->getComponent().getGLFormat();
210 GLenum target = (GLenum)getGLTarget();
211 glBindTexture(target, mTextureID);
212 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
213 glTexSubImage2D(GL_TEXTURE_2D, lod, xoff, yoff, w, h, format, type, ptr);
214}
215
Jason Sams5e0035a2010-12-13 17:11:21 -0800216void Allocation::upload2DTexture(bool isFirstUpload, const void *ptr) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800217 GLenum type = mType->getElement()->getComponent().getGLType();
218 GLenum format = mType->getElement()->getComponent().getGLFormat();
219
Jason Sams5e0035a2010-12-13 17:11:21 -0800220 GLenum target = (GLenum)getGLTarget();
221 glBindTexture(target, mTextureID);
222 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -0700223
Jason Sams5e0035a2010-12-13 17:11:21 -0800224 for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
225 const uint8_t *p = (const uint8_t *)ptr;
226 p += mType->getLODOffset(lod);
227
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800228 if (isFirstUpload) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700229 glTexImage2D(GL_TEXTURE_2D, lod, format,
Jason Sams5e0035a2010-12-13 17:11:21 -0800230 mType->getLODDimX(lod), mType->getLODDimY(lod),
231 0, format, type, p);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700232 } else {
233 glTexSubImage2D(GL_TEXTURE_2D, lod, 0, 0,
Jason Sams5e0035a2010-12-13 17:11:21 -0800234 mType->getLODDimX(lod), mType->getLODDimY(lod),
235 format, type, p);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700236 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700237 }
Jason Sams6d8eb262010-12-15 01:41:00 -0800238
239 if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
240#ifndef ANDROID_RS_BUILD_FOR_HOST
241 glGenerateMipmap(target);
242#endif //ANDROID_RS_BUILD_FOR_HOST
243 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800244}
Jason Samsc2908e62010-02-23 17:44:28 -0800245
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800246void Allocation::uploadCubeTexture(bool isFirstUpload) {
247 GLenum type = mType->getElement()->getComponent().getGLType();
248 GLenum format = mType->getElement()->getComponent().getGLFormat();
249
Jason Sams5e0035a2010-12-13 17:11:21 -0800250 GLenum target = (GLenum)getGLTarget();
251 glBindTexture(target, mTextureID);
252 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
253
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800254 GLenum faceOrder[] = {
255 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
256 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
257 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
258 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
259 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
260 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
261 };
262
263 Adapter2D adapt(getContext(), this);
264 for (uint32_t face = 0; face < 6; face ++) {
265 adapt.setFace(face);
266
Jason Sams5e0035a2010-12-13 17:11:21 -0800267 for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
268 adapt.setLOD(lod);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800269
270 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
271
272 if (isFirstUpload) {
273 glTexImage2D(faceOrder[face], lod, format,
274 adapt.getDimX(), adapt.getDimY(),
275 0, format, type, ptr);
276 } else {
277 glTexSubImage2D(faceOrder[face], lod, 0, 0,
278 adapt.getDimX(), adapt.getDimY(),
279 format, type, ptr);
280 }
281 }
282 }
Jason Sams6d8eb262010-12-15 01:41:00 -0800283
284 if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
285#ifndef ANDROID_RS_BUILD_FOR_HOST
286 glGenerateMipmap(target);
287#endif //ANDROID_RS_BUILD_FOR_HOST
288 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700289}
290
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800291void Allocation::deferedUploadToBufferObject(const Context *rsc) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800292 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800293 mUploadDefered = true;
294}
295
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800296void Allocation::uploadToBufferObject(const Context *rsc) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700297 rsAssert(!mType->getDimY());
298 rsAssert(!mType->getDimZ());
299
Jason Samsd4b23b52010-12-13 15:32:35 -0800300 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800301
Jason Samsd19f10d2009-05-22 14:03:28 -0700302 if (!mBufferID) {
303 glGenBuffers(1, &mBufferID);
304 }
Jason Sams3b7d39b2009-12-14 12:57:40 -0800305 if (!mBufferID) {
306 LOGE("Upload to buffer object failed");
307 mUploadDefered = true;
308 return;
309 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800310 GLenum target = (GLenum)getGLTarget();
311 glBindBuffer(target, mBufferID);
312 glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
313 glBindBuffer(target, 0);
Jason Samsf4687002010-03-10 17:30:41 -0800314 rsc->checkError("Allocation::uploadToBufferObject");
Jason Samsd19f10d2009-05-22 14:03:28 -0700315}
316
Jason Sams5476b452010-12-08 16:14:36 -0800317void Allocation::uploadCheck(Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800318 if (mUploadDefered) {
Jason Sams5476b452010-12-08 16:14:36 -0800319 syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800320 }
321}
322
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800323void Allocation::read(void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700324 memcpy(data, mPtr, mType->getSizeBytes());
325}
326
Jason Sams49a05d72010-12-29 14:31:29 -0800327void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
328 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700329 uint32_t eSize = mType->getElementSizeBytes();
330 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
331 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700332 uint32_t size = count * eSize;
333
334 if (size != sizeBytes) {
335 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Sams3c0dfba2009-09-27 17:50:38 -0700336 mType->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -0700337 return;
338 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700339
340 if (mType->getElement()->getHasReferences()) {
341 incRefs(data, count);
342 decRefs(ptr, count);
343 }
344
Jason Sams07ae4062009-08-27 20:23:34 -0700345 memcpy(ptr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700346 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800347 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700348}
349
Jason Sams49a05d72010-12-29 14:31:29 -0800350void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800351 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700352 uint32_t eSize = mType->getElementSizeBytes();
353 uint32_t lineSize = eSize * w;
354 uint32_t destW = mType->getDimX();
355
Jason Samsf7086092011-01-12 13:28:37 -0800356 //LOGE("data2d %p, %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Sams07ae4062009-08-27 20:23:34 -0700357
Jason Samsf7086092011-01-12 13:28:37 -0800358 if ((lineSize * h) != sizeBytes) {
359 LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
Jason Sams07ae4062009-08-27 20:23:34 -0700360 rsAssert(!"Allocation::subData called with mismatched size");
361 return;
362 }
363
Jason Samsf7086092011-01-12 13:28:37 -0800364 if (mPtr) {
365 const uint8_t *src = static_cast<const uint8_t *>(data);
366 uint8_t *dst = static_cast<uint8_t *>(mPtr);
367 dst += mType->getLODOffset(lod, xoff, yoff);
368
369 //LOGE(" %p %p %i ", dst, src, eSize);
370 for (uint32_t line=yoff; line < (yoff+h); line++) {
371 if (mType->getElement()->getHasReferences()) {
372 incRefs(src, w);
373 decRefs(dst, w);
374 }
375 memcpy(dst, src, lineSize);
376 src += lineSize;
377 dst += destW * eSize;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700378 }
Jason Samsf7086092011-01-12 13:28:37 -0800379 sendDirty();
380 mUploadDefered = true;
381 } else {
Jason Samsfb9f82c2011-01-12 14:53:25 -0800382 update2DTexture(data, xoff, yoff, lod, face, w, h);
Jason Samsd19f10d2009-05-22 14:03:28 -0700383 }
384}
385
Jason Samsfb9f82c2011-01-12 14:53:25 -0800386void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
387 uint32_t lod, RsAllocationCubemapFace face,
388 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700389}
390
Jason Sams49a05d72010-12-29 14:31:29 -0800391void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800392 uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700393 uint32_t eSize = mType->getElementSizeBytes();
394 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
395 ptr += eSize * x;
396
397 if (cIdx >= mType->getElement()->getFieldCount()) {
398 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
399 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
400 return;
401 }
402
403 if (x >= mType->getDimX()) {
404 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
405 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
406 return;
407 }
408
409 const Element * e = mType->getElement()->getField(cIdx);
410 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
411
412 if (sizeBytes != e->getSizeBytes()) {
413 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
414 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
415 return;
416 }
417
418 if (e->getHasReferences()) {
419 e->incRefs(data);
420 e->decRefs(ptr);
421 }
422
423 memcpy(ptr, data, sizeBytes);
424 sendDirty();
425 mUploadDefered = true;
426}
427
Jason Sams49a05d72010-12-29 14:31:29 -0800428void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800429 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700430 uint32_t eSize = mType->getElementSizeBytes();
431 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
432 ptr += eSize * (x + y * mType->getDimX());
433
434 if (x >= mType->getDimX()) {
435 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
436 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
437 return;
438 }
439
440 if (y >= mType->getDimY()) {
441 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
442 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
443 return;
444 }
445
446 if (cIdx >= mType->getElement()->getFieldCount()) {
447 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
448 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
449 return;
450 }
451
452 const Element * e = mType->getElement()->getField(cIdx);
453 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
454
455 if (sizeBytes != e->getSizeBytes()) {
456 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
457 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
458 return;
459 }
460
461 if (e->getHasReferences()) {
462 e->incRefs(data);
463 e->decRefs(ptr);
464 }
465
466 memcpy(ptr, data, sizeBytes);
467 sendDirty();
468 mUploadDefered = true;
469}
470
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800471void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700472 mToDirtyList.push(p);
Jason Sams83f1c632009-10-26 15:19:28 -0700473}
Jason Samsd19f10d2009-05-22 14:03:28 -0700474
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800475void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams83f1c632009-10-26 15:19:28 -0700476 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
477 if (mToDirtyList[ct] == p) {
478 mToDirtyList.removeAt(ct);
479 return;
480 }
481 }
482 rsAssert(0);
483}
484
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800485void Allocation::dumpLOGV(const char *prefix) const {
Jason Sams715333b2009-11-17 17:26:46 -0800486 ObjectBase::dumpLOGV(prefix);
487
488 String8 s(prefix);
489 s.append(" type ");
490 if (mType.get()) {
491 mType->dumpLOGV(s.string());
492 }
493
494 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
495 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
496
Jason Samsd4b23b52010-12-13 15:32:35 -0800497 LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
498 prefix, mUsageFlags, mMipmapControl, mTextureID, mBufferID);
Jason Sams715333b2009-11-17 17:26:46 -0800499}
Jason Samsd19f10d2009-05-22 14:03:28 -0700500
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800501void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700502 // Need to identify ourselves
503 stream->addU32((uint32_t)getClassId());
504
505 String8 name(getName());
506 stream->addString(&name);
507
508 // First thing we need to serialize is the type object since it will be needed
509 // to initialize the class
510 mType->serialize(stream);
511
512 uint32_t dataSize = mType->getSizeBytes();
513 // Write how much data we are storing
514 stream->addU32(dataSize);
515 // Now write the data
516 stream->addByteArray(mPtr, dataSize);
517}
518
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800519Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700520 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700521 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800522 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700523 LOGE("allocation loading skipped due to invalid class id\n");
524 return NULL;
525 }
526
527 String8 name;
528 stream->loadString(&name);
529
530 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800531 if (!type) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700532 return NULL;
533 }
534 type->compute();
535
536 // Number of bytes we wrote out for this allocation
537 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800538 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700539 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Samsb38d5342010-10-21 14:06:55 -0700540 ObjectBase::checkDelete(type);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700541 return NULL;
542 }
543
Jason Samsd4b23b52010-12-13 15:32:35 -0800544 Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700545 alloc->setName(name.string(), name.size());
546
Jason Sams49a05d72010-12-29 14:31:29 -0800547 uint32_t count = dataSize / type->getElementSizeBytes();
548
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700549 // Read in all of our allocation data
Jason Sams49a05d72010-12-29 14:31:29 -0800550 alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2ce0e3f2010-08-11 10:30:44 -0700551 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700552
553 return alloc;
554}
555
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800556void Allocation::sendDirty() const {
Jason Sams83f1c632009-10-26 15:19:28 -0700557 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
558 mToDirtyList[ct]->forceDirty();
559 }
560}
Jason Samsd19f10d2009-05-22 14:03:28 -0700561
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800562void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-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 Sams5edc6082010-10-05 13:32:49 -0700567 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700568 while (ct > 0) {
569 e->incRefs(p);
570 ct --;
571 p += stride;
572 }
573}
574
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800575void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700576 const uint8_t *p = static_cast<const uint8_t *>(ptr);
577 const Element *e = mType->getElement();
578 uint32_t stride = e->getSizeBytes();
579
Jason Sams5edc6082010-10-05 13:32:49 -0700580 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700581 while (ct > 0) {
582 e->decRefs(p);
583 ct --;
584 p += stride;
585 }
586}
587
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800588void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams5edc6082010-10-05 13:32:49 -0700589}
590
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800591void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700592 Type *t = mType->cloneAndResize1D(rsc, dimX);
593
594 uint32_t oldDimX = mType->getDimX();
595 if (dimX == oldDimX) {
596 return;
597 }
598
599 if (dimX < oldDimX) {
600 decRefs(mPtr, oldDimX - dimX, dimX);
601 }
602 mPtr = realloc(mPtr, t->getSizeBytes());
603
604 if (dimX > oldDimX) {
605 const Element *e = mType->getElement();
606 uint32_t stride = e->getSizeBytes();
607 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
608 }
609 mType.set(t);
610}
611
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800612void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700613 LOGE("not implemented");
614}
615
Jason Samsd19f10d2009-05-22 14:03:28 -0700616/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700617//
Jason Samsd19f10d2009-05-22 14:03:28 -0700618
619
620namespace android {
621namespace renderscript {
622
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800623void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700624 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams5e0035a2010-12-13 17:11:21 -0800625 alloc->deferedUploadToTexture(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700626}
627
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800628void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700629 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800630 alloc->deferedUploadToBufferObject(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700631}
632
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800633static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700634 uint32_t w = out.getDimX();
635 uint32_t h = out.getDimY();
636
Jason Sams6f5c61c2009-07-28 17:20:11 -0700637 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700638 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
639 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
640 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
641
Jason Sams6f5c61c2009-07-28 17:20:11 -0700642 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700643 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
644 oPtr ++;
645 i1 += 2;
646 i2 += 2;
647 }
648 }
649}
650
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800651static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700652 uint32_t w = out.getDimX();
653 uint32_t h = out.getDimY();
654
Jason Sams6f5c61c2009-07-28 17:20:11 -0700655 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700656 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
657 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
658 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
659
Jason Sams6f5c61c2009-07-28 17:20:11 -0700660 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700661 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700662 oPtr ++;
663 i1 += 2;
664 i2 += 2;
665 }
666 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700667}
668
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800669static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Samse20e3b42010-01-19 17:53:54 -0800670 uint32_t w = out.getDimX();
671 uint32_t h = out.getDimY();
672
673 for (uint32_t y=0; y < h; y++) {
674 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
675 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
676 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
677
678 for (uint32_t x=0; x < w; x++) {
679 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
680 oPtr ++;
681 i1 += 2;
682 i2 += 2;
683 }
684 }
685}
686
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800687static void mip(const Adapter2D &out, const Adapter2D &in) {
688 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Sams6f5c61c2009-07-28 17:20:11 -0700689 case 32:
690 mip8888(out, in);
691 break;
692 case 16:
693 mip565(out, in);
694 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800695 case 8:
696 mip8(out, in);
697 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700698 }
Jason Sams6f5c61c2009-07-28 17:20:11 -0700699}
Jason Samsd19f10d2009-05-22 14:03:28 -0700700
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700701#ifndef ANDROID_RS_BUILD_FOR_HOST
702
Jason Sams5476b452010-12-08 16:14:36 -0800703void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
704 Allocation *a = static_cast<Allocation *>(va);
705 a->syncAll(rsc, src);
706}
707
Jason Samsf7086092011-01-12 13:28:37 -0800708void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700709 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Samsf7086092011-01-12 13:28:37 -0800710 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams4ef66502010-12-10 16:03:15 -0800711}
712
713void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
714 Allocation *texAlloc = static_cast<Allocation *>(va);
715 const Type * t = texAlloc->getType();
716
717 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
718 if (s != dataLen) {
719 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
720 return;
721 }
722
723 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700724}
725
Jason Sams49a05d72010-12-29 14:31:29 -0800726void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
727 uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700728 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800729 a->data(rsc, xoff, lod, count, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700730}
731
Jason Sams49a05d72010-12-29 14:31:29 -0800732void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
733 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700734 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800735 a->elementData(rsc, x, y, data, eoff, sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -0700736}
737
Jason Sams49a05d72010-12-29 14:31:29 -0800738void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
739 const void *data, uint32_t eoff, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700740 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800741 a->elementData(rsc, x, data, eoff, sizeBytes);
Jason Sams49bdaf02010-08-31 13:50:42 -0700742}
743
Jason Sams49a05d72010-12-29 14:31:29 -0800744void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
745 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700746 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49a05d72010-12-29 14:31:29 -0800747 a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700748}
749
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800750void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700751 Allocation *a = static_cast<Allocation *>(va);
752 a->read(data);
753}
754
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800755void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700756 Allocation *a = static_cast<Allocation *>(va);
757 a->resize1D(rsc, dimX);
758}
759
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800760void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700761 Allocation *a = static_cast<Allocation *>(va);
762 a->resize2D(rsc, dimX, dimY);
763}
764
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700765#endif //ANDROID_RS_BUILD_FOR_HOST
766
767}
768}
769
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800770static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va) {
771 Context *rsc = static_cast<Context *>(con);
772 Allocation *texAlloc = static_cast<Allocation *>(va);
773 uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
774 for (uint32_t face = 0; face < numFaces; face ++) {
775 Adapter2D adapt(rsc, texAlloc);
776 Adapter2D adapt2(rsc, texAlloc);
777 adapt.setFace(face);
778 adapt2.setFace(face);
779 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
780 adapt.setLOD(lod);
781 adapt2.setLOD(lod + 1);
782 mip(adapt2, adapt);
783 }
784 }
785}
786
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800787const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700788 Allocation *a = static_cast<Allocation *>(va);
789 a->getType()->incUserRef();
790
791 return a->getType();
792}
793
Jason Sams5476b452010-12-08 16:14:36 -0800794RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800795 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800796 uint32_t usages) {
Jason Sams31a7e422010-10-26 13:09:17 -0700797 Context *rsc = static_cast<Context *>(con);
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800798 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages, mips);
Jason Sams31a7e422010-10-26 13:09:17 -0700799 alloc->incUserRef();
800 return alloc;
801}
802
Jason Sams5476b452010-12-08 16:14:36 -0800803
804RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800805 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800806 const void *data, uint32_t usages) {
Jason Sams31a7e422010-10-26 13:09:17 -0700807 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800808 Type *t = static_cast<Type *>(vtype);
Jason Sams31a7e422010-10-26 13:09:17 -0700809
Jason Sams5476b452010-12-08 16:14:36 -0800810 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages);
Jason Sams31a7e422010-10-26 13:09:17 -0700811 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
812 if (texAlloc == NULL) {
813 LOGE("Memory allocation failure");
814 return NULL;
815 }
816
Jason Sams5476b452010-12-08 16:14:36 -0800817 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsd4b23b52010-12-13 15:32:35 -0800818 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800819 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Jason Sams31a7e422010-10-26 13:09:17 -0700820 }
821
Jason Sams5e0035a2010-12-13 17:11:21 -0800822 texAlloc->deferedUploadToTexture(rsc);
Jason Sams31a7e422010-10-26 13:09:17 -0700823 return texAlloc;
824}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800825
Jason Sams5476b452010-12-08 16:14:36 -0800826RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800827 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800828 const void *data, uint32_t usages) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800829 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800830 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800831
832 // Cubemap allocation's faces should be Width by Width each.
833 // Source data should have 6 * Width by Width pixels
834 // Error checking is done in the java layer
Jason Sams5476b452010-12-08 16:14:36 -0800835 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800836 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
837 if (texAlloc == NULL) {
838 LOGE("Memory allocation failure");
839 return NULL;
840 }
841
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800842 uint32_t faceSize = t->getDimX();
843 uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
844 uint32_t copySize = faceSize * t->getElementSizeBytes();
845
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800846 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams5476b452010-12-08 16:14:36 -0800847 for (uint32_t face = 0; face < 6; face ++) {
848 Adapter2D faceAdapter(rsc, texAlloc);
849 faceAdapter.setFace(face);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800850
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800851 for (uint32_t dI = 0; dI < faceSize; dI ++) {
852 memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
853 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800854
Jason Sams5476b452010-12-08 16:14:36 -0800855 // Move the data pointer to the next cube face
Alex Sakhartchoukfe852e22011-01-10 15:57:57 -0800856 sourcePtr += copySize;
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800857 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800858
Alex Sakhartchoukdcc23192011-01-11 14:47:44 -0800859 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
860 rsaAllocationGenerateScriptMips(rsc, texAlloc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800861 }
862
Jason Sams5e0035a2010-12-13 17:11:21 -0800863 texAlloc->deferedUploadToTexture(rsc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800864 return texAlloc;
865}