blob: cb0022377bc5479ef59212404ad3f4cf773d8649 [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 Sams5e0035a2010-12-13 17:11:21 -080039 allocScriptMemory();
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);
Jason Sams5e0035a2010-12-13 17:11:21 -080078 mPtr = NULL;
Jason Sams8a64743f2010-03-01 15:31:04 -080079 }
Jason Sams5e0035a2010-12-13 17:11:21 -080080 freeScriptMemory();
Jason Sams9d5e03d2009-11-03 11:25:42 -080081
82 if (mBufferID) {
83 // Causes a SW crash....
84 //LOGV(" mBufferID %i", mBufferID);
85 //glDeleteBuffers(1, &mBufferID);
86 //mBufferID = 0;
87 }
88 if (mTextureID) {
89 glDeleteTextures(1, &mTextureID);
90 mTextureID = 0;
91 }
Jason Samsd19f10d2009-05-22 14:03:28 -070092}
93
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080094void Allocation::setCpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -070095}
96
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080097void Allocation::setGpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -070098}
99
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800100void Allocation::setCpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700101}
102
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800103void Allocation::setGpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700104}
105
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800106bool Allocation::fixAllocation() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700107 return false;
108}
109
Jason Sams5e0035a2010-12-13 17:11:21 -0800110void Allocation::deferedUploadToTexture(const Context *rsc) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800111 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800112 mUploadDefered = true;
113}
114
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800115uint32_t Allocation::getGLTarget() const {
Jason Samsd4b23b52010-12-13 15:32:35 -0800116 if (getIsTexture()) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800117 if (mType->getDimFaces()) {
118 return GL_TEXTURE_CUBE_MAP;
119 } else {
120 return GL_TEXTURE_2D;
121 }
122 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800123 if (getIsBufferObject()) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800124 return GL_ARRAY_BUFFER;
125 }
126 return 0;
127}
128
Jason Sams5e0035a2010-12-13 17:11:21 -0800129void Allocation::allocScriptMemory() {
130 rsAssert(!mPtr);
131 mPtr = malloc(mType->getSizeBytes());
132}
133
134void Allocation::freeScriptMemory() {
135 rsAssert(!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT));
136 if (mPtr) {
137 free(mPtr);
138 mPtr = NULL;
139 }
140}
141
142
Jason Sams5476b452010-12-08 16:14:36 -0800143void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
144 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
145
Jason Samsd4b23b52010-12-13 15:32:35 -0800146 if (getIsTexture()) {
Jason Sams5476b452010-12-08 16:14:36 -0800147 uploadToTexture(rsc);
148 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800149 if (getIsBufferObject()) {
Jason Sams5476b452010-12-08 16:14:36 -0800150 uploadToBufferObject(rsc);
151 }
152
153 mUploadDefered = false;
154}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800155
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800156void Allocation::uploadToTexture(const Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800157
Jason Samsd4b23b52010-12-13 15:32:35 -0800158 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Sams718cd1f2009-12-23 14:35:29 -0800159 GLenum type = mType->getElement()->getComponent().getGLType();
160 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Samse2ae85f2009-06-03 16:04:54 -0700161
162 if (!type || !format) {
163 return;
164 }
165
Jason Sams5e0035a2010-12-13 17:11:21 -0800166 if (!mPtr) {
167 return;
168 }
169
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700170 bool isFirstUpload = false;
171
Jason Samsd19f10d2009-05-22 14:03:28 -0700172 if (!mTextureID) {
173 glGenTextures(1, &mTextureID);
Jason Sams9dab6672009-11-24 12:26:35 -0800174
175 if (!mTextureID) {
176 // This should not happen, however, its likely the cause of the
177 // white sqare bug.
178 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
179 LOGE("Upload to texture failed to gen mTextureID");
180 rsc->dumpDebug();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800181 mUploadDefered = true;
182 return;
Jason Sams9dab6672009-11-24 12:26:35 -0800183 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700184 isFirstUpload = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700185 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800186
187 GLenum target = (GLenum)getGLTarget();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800188 if (target == GL_TEXTURE_2D) {
Jason Sams5e0035a2010-12-13 17:11:21 -0800189 upload2DTexture(isFirstUpload, mPtr);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800190 } else if (target == GL_TEXTURE_CUBE_MAP) {
191 uploadCubeTexture(isFirstUpload);
192 }
193
Jason Sams5e0035a2010-12-13 17:11:21 -0800194 if (!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
195 freeScriptMemory();
196 }
197
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800198 rsc->checkError("Allocation::uploadToTexture");
199}
200
Jason Sams5e0035a2010-12-13 17:11:21 -0800201void Allocation::upload2DTexture(bool isFirstUpload, const void *ptr) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800202 GLenum type = mType->getElement()->getComponent().getGLType();
203 GLenum format = mType->getElement()->getComponent().getGLFormat();
204
Jason Sams5e0035a2010-12-13 17:11:21 -0800205 GLenum target = (GLenum)getGLTarget();
206 glBindTexture(target, mTextureID);
207 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -0700208
Jason Sams5e0035a2010-12-13 17:11:21 -0800209 for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
210 const uint8_t *p = (const uint8_t *)ptr;
211 p += mType->getLODOffset(lod);
212
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800213 if (isFirstUpload) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700214 glTexImage2D(GL_TEXTURE_2D, lod, format,
Jason Sams5e0035a2010-12-13 17:11:21 -0800215 mType->getLODDimX(lod), mType->getLODDimY(lod),
216 0, format, type, p);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700217 } else {
218 glTexSubImage2D(GL_TEXTURE_2D, lod, 0, 0,
Jason Sams5e0035a2010-12-13 17:11:21 -0800219 mType->getLODDimX(lod), mType->getLODDimY(lod),
220 format, type, p);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700221 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700222 }
Jason Sams6d8eb262010-12-15 01:41:00 -0800223
224 if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
225#ifndef ANDROID_RS_BUILD_FOR_HOST
226 glGenerateMipmap(target);
227#endif //ANDROID_RS_BUILD_FOR_HOST
228 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800229}
Jason Samsc2908e62010-02-23 17:44:28 -0800230
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800231void Allocation::uploadCubeTexture(bool isFirstUpload) {
232 GLenum type = mType->getElement()->getComponent().getGLType();
233 GLenum format = mType->getElement()->getComponent().getGLFormat();
234
Jason Sams5e0035a2010-12-13 17:11:21 -0800235 GLenum target = (GLenum)getGLTarget();
236 glBindTexture(target, mTextureID);
237 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
238
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800239 GLenum faceOrder[] = {
240 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
241 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
242 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
243 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
244 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
245 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
246 };
247
248 Adapter2D adapt(getContext(), this);
249 for (uint32_t face = 0; face < 6; face ++) {
250 adapt.setFace(face);
251
Jason Sams5e0035a2010-12-13 17:11:21 -0800252 for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
253 adapt.setLOD(lod);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800254
255 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
256
257 if (isFirstUpload) {
258 glTexImage2D(faceOrder[face], lod, format,
259 adapt.getDimX(), adapt.getDimY(),
260 0, format, type, ptr);
261 } else {
262 glTexSubImage2D(faceOrder[face], lod, 0, 0,
263 adapt.getDimX(), adapt.getDimY(),
264 format, type, ptr);
265 }
266 }
267 }
Jason Sams6d8eb262010-12-15 01:41:00 -0800268
269 if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
270#ifndef ANDROID_RS_BUILD_FOR_HOST
271 glGenerateMipmap(target);
272#endif //ANDROID_RS_BUILD_FOR_HOST
273 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700274}
275
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800276void Allocation::deferedUploadToBufferObject(const Context *rsc) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800277 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800278 mUploadDefered = true;
279}
280
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800281void Allocation::uploadToBufferObject(const Context *rsc) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700282 rsAssert(!mType->getDimY());
283 rsAssert(!mType->getDimZ());
284
Jason Samsd4b23b52010-12-13 15:32:35 -0800285 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800286
Jason Samsd19f10d2009-05-22 14:03:28 -0700287 if (!mBufferID) {
288 glGenBuffers(1, &mBufferID);
289 }
Jason Sams3b7d39b2009-12-14 12:57:40 -0800290 if (!mBufferID) {
291 LOGE("Upload to buffer object failed");
292 mUploadDefered = true;
293 return;
294 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800295 GLenum target = (GLenum)getGLTarget();
296 glBindBuffer(target, mBufferID);
297 glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
298 glBindBuffer(target, 0);
Jason Samsf4687002010-03-10 17:30:41 -0800299 rsc->checkError("Allocation::uploadToBufferObject");
Jason Samsd19f10d2009-05-22 14:03:28 -0700300}
301
Jason Sams5476b452010-12-08 16:14:36 -0800302void Allocation::uploadCheck(Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800303 if (mUploadDefered) {
Jason Sams5476b452010-12-08 16:14:36 -0800304 syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800305 }
306}
307
Jason Sams1bada8c2009-08-09 17:01:55 -0700308
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800309void Allocation::data(Context *rsc, const void *data, uint32_t sizeBytes) {
Jason Sams07ae4062009-08-27 20:23:34 -0700310 uint32_t size = mType->getSizeBytes();
311 if (size != sizeBytes) {
312 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
313 return;
314 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700315
316 if (mType->getElement()->getHasReferences()) {
317 incRefs(data, sizeBytes / mType->getElement()->getSizeBytes());
318 decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes());
319 }
320
Jason Sams07ae4062009-08-27 20:23:34 -0700321 memcpy(mPtr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700322 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800323 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700324}
325
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800326void Allocation::read(void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700327 memcpy(data, mPtr, mType->getSizeBytes());
328}
329
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800330void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700331 uint32_t eSize = mType->getElementSizeBytes();
332 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
333 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700334 uint32_t size = count * eSize;
335
336 if (size != sizeBytes) {
337 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Sams3c0dfba2009-09-27 17:50:38 -0700338 mType->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -0700339 return;
340 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700341
342 if (mType->getElement()->getHasReferences()) {
343 incRefs(data, count);
344 decRefs(ptr, count);
345 }
346
Jason Sams07ae4062009-08-27 20:23:34 -0700347 memcpy(ptr, data, size);
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,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800353 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700354 uint32_t eSize = mType->getElementSizeBytes();
355 uint32_t lineSize = eSize * w;
356 uint32_t destW = mType->getDimX();
357
358 const uint8_t *src = static_cast<const uint8_t *>(data);
359 uint8_t *dst = static_cast<uint8_t *>(mPtr);
360 dst += eSize * (xoff + yoff * destW);
Jason Sams07ae4062009-08-27 20:23:34 -0700361
362 if ((lineSize * eSize * h) != sizeBytes) {
363 rsAssert(!"Allocation::subData called with mismatched size");
364 return;
365 }
366
Jason Samsd19f10d2009-05-22 14:03:28 -0700367 for (uint32_t line=yoff; line < (yoff+h); line++) {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700368 if (mType->getElement()->getHasReferences()) {
369 incRefs(src, w);
370 decRefs(dst, w);
371 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700372 memcpy(dst, src, lineSize);
373 src += lineSize;
374 dst += destW * eSize;
375 }
Jason Sams83f1c632009-10-26 15:19:28 -0700376 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800377 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700378}
379
Jason Sams49bdaf02010-08-31 13:50:42 -0700380void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800381 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700382}
383
Jason Sams49bdaf02010-08-31 13:50:42 -0700384void Allocation::subElementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800385 uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700386 uint32_t eSize = mType->getElementSizeBytes();
387 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
388 ptr += eSize * x;
389
390 if (cIdx >= mType->getElement()->getFieldCount()) {
391 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
392 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
393 return;
394 }
395
396 if (x >= mType->getDimX()) {
397 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
398 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
399 return;
400 }
401
402 const Element * e = mType->getElement()->getField(cIdx);
403 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
404
405 if (sizeBytes != e->getSizeBytes()) {
406 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
407 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
408 return;
409 }
410
411 if (e->getHasReferences()) {
412 e->incRefs(data);
413 e->decRefs(ptr);
414 }
415
416 memcpy(ptr, data, sizeBytes);
417 sendDirty();
418 mUploadDefered = true;
419}
420
421void Allocation::subElementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800422 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700423 uint32_t eSize = mType->getElementSizeBytes();
424 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
425 ptr += eSize * (x + y * mType->getDimX());
426
427 if (x >= mType->getDimX()) {
428 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
429 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
430 return;
431 }
432
433 if (y >= mType->getDimY()) {
434 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
435 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
436 return;
437 }
438
439 if (cIdx >= mType->getElement()->getFieldCount()) {
440 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
441 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
442 return;
443 }
444
445 const Element * e = mType->getElement()->getField(cIdx);
446 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
447
448 if (sizeBytes != e->getSizeBytes()) {
449 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
450 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
451 return;
452 }
453
454 if (e->getHasReferences()) {
455 e->incRefs(data);
456 e->decRefs(ptr);
457 }
458
459 memcpy(ptr, data, sizeBytes);
460 sendDirty();
461 mUploadDefered = true;
462}
463
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800464void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700465 mToDirtyList.push(p);
Jason Sams83f1c632009-10-26 15:19:28 -0700466}
Jason Samsd19f10d2009-05-22 14:03:28 -0700467
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800468void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams83f1c632009-10-26 15:19:28 -0700469 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
470 if (mToDirtyList[ct] == p) {
471 mToDirtyList.removeAt(ct);
472 return;
473 }
474 }
475 rsAssert(0);
476}
477
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800478void Allocation::dumpLOGV(const char *prefix) const {
Jason Sams715333b2009-11-17 17:26:46 -0800479 ObjectBase::dumpLOGV(prefix);
480
481 String8 s(prefix);
482 s.append(" type ");
483 if (mType.get()) {
484 mType->dumpLOGV(s.string());
485 }
486
487 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
488 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
489
Jason Samsd4b23b52010-12-13 15:32:35 -0800490 LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
491 prefix, mUsageFlags, mMipmapControl, mTextureID, mBufferID);
Jason Sams715333b2009-11-17 17:26:46 -0800492}
Jason Samsd19f10d2009-05-22 14:03:28 -0700493
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800494void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700495 // Need to identify ourselves
496 stream->addU32((uint32_t)getClassId());
497
498 String8 name(getName());
499 stream->addString(&name);
500
501 // First thing we need to serialize is the type object since it will be needed
502 // to initialize the class
503 mType->serialize(stream);
504
505 uint32_t dataSize = mType->getSizeBytes();
506 // Write how much data we are storing
507 stream->addU32(dataSize);
508 // Now write the data
509 stream->addByteArray(mPtr, dataSize);
510}
511
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800512Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700513 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700514 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800515 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700516 LOGE("allocation loading skipped due to invalid class id\n");
517 return NULL;
518 }
519
520 String8 name;
521 stream->loadString(&name);
522
523 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800524 if (!type) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700525 return NULL;
526 }
527 type->compute();
528
529 // Number of bytes we wrote out for this allocation
530 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800531 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700532 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Samsb38d5342010-10-21 14:06:55 -0700533 ObjectBase::checkDelete(type);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700534 return NULL;
535 }
536
Jason Samsd4b23b52010-12-13 15:32:35 -0800537 Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700538 alloc->setName(name.string(), name.size());
539
540 // Read in all of our allocation data
Jason Sams49bdaf02010-08-31 13:50:42 -0700541 alloc->data(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2ce0e3f2010-08-11 10:30:44 -0700542 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700543
544 return alloc;
545}
546
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800547void Allocation::sendDirty() const {
Jason Sams83f1c632009-10-26 15:19:28 -0700548 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
549 mToDirtyList[ct]->forceDirty();
550 }
551}
Jason Samsd19f10d2009-05-22 14:03:28 -0700552
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800553void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-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 Sams5edc6082010-10-05 13:32:49 -0700558 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700559 while (ct > 0) {
560 e->incRefs(p);
561 ct --;
562 p += stride;
563 }
564}
565
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800566void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700567 const uint8_t *p = static_cast<const uint8_t *>(ptr);
568 const Element *e = mType->getElement();
569 uint32_t stride = e->getSizeBytes();
570
Jason Sams5edc6082010-10-05 13:32:49 -0700571 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700572 while (ct > 0) {
573 e->decRefs(p);
574 ct --;
575 p += stride;
576 }
577}
578
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800579void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams5edc6082010-10-05 13:32:49 -0700580}
581
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800582void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700583 Type *t = mType->cloneAndResize1D(rsc, dimX);
584
585 uint32_t oldDimX = mType->getDimX();
586 if (dimX == oldDimX) {
587 return;
588 }
589
590 if (dimX < oldDimX) {
591 decRefs(mPtr, oldDimX - dimX, dimX);
592 }
593 mPtr = realloc(mPtr, t->getSizeBytes());
594
595 if (dimX > oldDimX) {
596 const Element *e = mType->getElement();
597 uint32_t stride = e->getSizeBytes();
598 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
599 }
600 mType.set(t);
601}
602
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800603void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700604 LOGE("not implemented");
605}
606
Jason Samsd19f10d2009-05-22 14:03:28 -0700607/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700608//
Jason Samsd19f10d2009-05-22 14:03:28 -0700609
610
611namespace android {
612namespace renderscript {
613
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800614void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700615 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams5e0035a2010-12-13 17:11:21 -0800616 alloc->deferedUploadToTexture(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700617}
618
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800619void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700620 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800621 alloc->deferedUploadToBufferObject(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700622}
623
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800624static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700625 uint32_t w = out.getDimX();
626 uint32_t h = out.getDimY();
627
Jason Sams6f5c61c2009-07-28 17:20:11 -0700628 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700629 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
630 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
631 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
632
Jason Sams6f5c61c2009-07-28 17:20:11 -0700633 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700634 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
635 oPtr ++;
636 i1 += 2;
637 i2 += 2;
638 }
639 }
640}
641
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800642static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700643 uint32_t w = out.getDimX();
644 uint32_t h = out.getDimY();
645
Jason Sams6f5c61c2009-07-28 17:20:11 -0700646 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700647 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
648 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
649 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
650
Jason Sams6f5c61c2009-07-28 17:20:11 -0700651 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700652 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700653 oPtr ++;
654 i1 += 2;
655 i2 += 2;
656 }
657 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700658}
659
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800660static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Samse20e3b42010-01-19 17:53:54 -0800661 uint32_t w = out.getDimX();
662 uint32_t h = out.getDimY();
663
664 for (uint32_t y=0; y < h; y++) {
665 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
666 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
667 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
668
669 for (uint32_t x=0; x < w; x++) {
670 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
671 oPtr ++;
672 i1 += 2;
673 i2 += 2;
674 }
675 }
676}
677
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800678static void mip(const Adapter2D &out, const Adapter2D &in) {
679 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Sams6f5c61c2009-07-28 17:20:11 -0700680 case 32:
681 mip8888(out, in);
682 break;
683 case 16:
684 mip565(out, in);
685 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800686 case 8:
687 mip8(out, in);
688 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700689 }
Jason Sams6f5c61c2009-07-28 17:20:11 -0700690}
Jason Samsd19f10d2009-05-22 14:03:28 -0700691
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700692#ifndef ANDROID_RS_BUILD_FOR_HOST
693
Jason Sams5476b452010-12-08 16:14:36 -0800694void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
695 Allocation *a = static_cast<Allocation *>(va);
696 a->syncAll(rsc, src);
697}
698
Jason Sams4ef66502010-12-10 16:03:15 -0800699void rsi_AllocationCopyFromBitmap(Context *rsc, RsAllocation va, const void *data, size_t dataLen) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700700 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Sams4ef66502010-12-10 16:03:15 -0800701 const Type * t = texAlloc->getType();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700702
Jason Sams4ef66502010-12-10 16:03:15 -0800703 uint32_t w = t->getDimX();
704 uint32_t h = t->getDimY();
705 bool genMips = t->getDimLOD();
706 size_t s = w * h * t->getElementSizeBytes();
707 if (s != dataLen) {
708 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
709 return;
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700710 }
Jason Sams4ef66502010-12-10 16:03:15 -0800711
Jason Sams5e0035a2010-12-13 17:11:21 -0800712 if (texAlloc->getIsScript()) {
713 memcpy(texAlloc->getPtr(), data, s);
714 if (genMips) {
715 Adapter2D adapt(rsc, texAlloc);
716 Adapter2D adapt2(rsc, texAlloc);
717 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
718 adapt.setLOD(lod);
719 adapt2.setLOD(lod + 1);
720 mip(adapt2, adapt);
721 }
Jason Sams4ef66502010-12-10 16:03:15 -0800722 }
Jason Sams5e0035a2010-12-13 17:11:21 -0800723 } else {
724 texAlloc->upload2DTexture(false, data);
Jason Sams4ef66502010-12-10 16:03:15 -0800725 }
Jason Sams5e0035a2010-12-13 17:11:21 -0800726
Jason Sams4ef66502010-12-10 16:03:15 -0800727}
728
729void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
730 Allocation *texAlloc = static_cast<Allocation *>(va);
731 const Type * t = texAlloc->getType();
732
733 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
734 if (s != dataLen) {
735 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
736 return;
737 }
738
739 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700740}
741
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800742void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700743 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700744 a->data(rsc, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700745}
746
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800747void 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 -0700748 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700749 a->subData(rsc, xoff, count, data, sizeBytes);
750}
751
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800752void 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 -0700753 Allocation *a = static_cast<Allocation *>(va);
754 a->subElementData(rsc, x, y, data, eoff, sizeBytes);
755}
756
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800757void 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 -0700758 Allocation *a = static_cast<Allocation *>(va);
759 a->subElementData(rsc, x, data, eoff, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700760}
761
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800762void 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 -0700763 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700764 a->subData(rsc, xoff, yoff, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700765}
766
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800767void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700768 Allocation *a = static_cast<Allocation *>(va);
769 a->read(data);
770}
771
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800772void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700773 Allocation *a = static_cast<Allocation *>(va);
774 a->resize1D(rsc, dimX);
775}
776
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800777void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700778 Allocation *a = static_cast<Allocation *>(va);
779 a->resize2D(rsc, dimX, dimY);
780}
781
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700782#endif //ANDROID_RS_BUILD_FOR_HOST
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);
Jason Sams5476b452010-12-08 16:14:36 -0800798 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages);
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) {
Jason Sams5476b452010-12-08 16:14:36 -0800819 Adapter2D adapt(rsc, texAlloc);
820 Adapter2D adapt2(rsc, texAlloc);
821 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
822 adapt.setLOD(lod);
823 adapt2.setLOD(lod + 1);
824 mip(adapt2, adapt);
Jason Sams31a7e422010-10-26 13:09:17 -0700825 }
Jason Sams31a7e422010-10-26 13:09:17 -0700826 }
827
Jason Sams5e0035a2010-12-13 17:11:21 -0800828 texAlloc->deferedUploadToTexture(rsc);
Jason Sams31a7e422010-10-26 13:09:17 -0700829 return texAlloc;
830}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800831
Jason Sams5476b452010-12-08 16:14:36 -0800832RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800833 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800834 const void *data, uint32_t usages) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800835 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800836 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800837
838 // Cubemap allocation's faces should be Width by Width each.
839 // Source data should have 6 * Width by Width pixels
840 // Error checking is done in the java layer
Jason Sams5476b452010-12-08 16:14:36 -0800841 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800842 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
843 if (texAlloc == NULL) {
844 LOGE("Memory allocation failure");
845 return NULL;
846 }
847
848 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams5476b452010-12-08 16:14:36 -0800849 for (uint32_t face = 0; face < 6; face ++) {
850 Adapter2D faceAdapter(rsc, texAlloc);
851 faceAdapter.setFace(face);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800852
Jason Sams5476b452010-12-08 16:14:36 -0800853 size_t cpySize = t->getDimX() * t->getDimX() * t->getElementSizeBytes();
854 memcpy(faceAdapter.getElement(0, 0), sourcePtr, cpySize);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800855
Jason Sams5476b452010-12-08 16:14:36 -0800856 // Move the data pointer to the next cube face
857 sourcePtr += cpySize;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800858
Jason Samsd4b23b52010-12-13 15:32:35 -0800859 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams5476b452010-12-08 16:14:36 -0800860 Adapter2D adapt(rsc, texAlloc);
861 Adapter2D adapt2(rsc, texAlloc);
862 adapt.setFace(face);
863 adapt2.setFace(face);
864 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
865 adapt.setLOD(lod);
866 adapt2.setLOD(lod + 1);
867 mip(adapt2, adapt);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800868 }
869 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800870 }
871
Jason Sams5e0035a2010-12-13 17:11:21 -0800872 texAlloc->deferedUploadToTexture(rsc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800873 return texAlloc;
874}