blob: ec1f684467ef7a69e4760f6f83a98af92959ef50 [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
Alex Sakhartchouk08571962010-12-15 09:59:58 -080034Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
35 RsAllocationMipmapControl mc)
36 : ObjectBase(rsc) {
Jason Sams8a647432010-03-01 15:31:04 -080037 init(rsc, type);
38
Jason Sams5476b452010-12-08 16:14:36 -080039 mUsageFlags = usages;
Alex Sakhartchouk08571962010-12-15 09:59:58 -080040 mMipmapControl = mc;
Jason Sams5476b452010-12-08 16:14:36 -080041
Jason Sams5e0035a2010-12-13 17:11:21 -080042 allocScriptMemory();
Jason Samsee734982010-08-12 12:44:02 -070043 if (mType->getElement()->getHasReferences()) {
44 memset(mPtr, 0, mType->getSizeBytes());
45 }
Jason Sams8a647432010-03-01 15:31:04 -080046 if (!mPtr) {
47 LOGE("Allocation::Allocation, alloc failure");
48 }
49}
50
Jason Sams8a647432010-03-01 15:31:04 -080051
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080052void Allocation::init(Context *rsc, const Type *type) {
Jason Samsd19f10d2009-05-22 14:03:28 -070053 mPtr = NULL;
54
55 mCpuWrite = false;
56 mCpuRead = false;
57 mGpuWrite = false;
58 mGpuRead = false;
59
60 mReadWriteRatio = 0;
61 mUpdateSize = 0;
Jason Samsd4b23b52010-12-13 15:32:35 -080062 mUsageFlags = 0;
63 mMipmapControl = RS_ALLOCATION_MIPMAP_NONE;
Jason Samsd19f10d2009-05-22 14:03:28 -070064
Jason Samsd19f10d2009-05-22 14:03:28 -070065 mTextureID = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070066 mBufferID = 0;
Jason Sams3b7d39b2009-12-14 12:57:40 -080067 mUploadDefered = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070068
Jason Sams8a647432010-03-01 15:31:04 -080069 mUserBitmapCallback = NULL;
70 mUserBitmapCallbackData = NULL;
71
Jason Samsd19f10d2009-05-22 14:03:28 -070072 mType.set(type);
Jason Sams1bada8c2009-08-09 17:01:55 -070073 rsAssert(type);
Jason Sams8a647432010-03-01 15:31:04 -080074
75 mPtr = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -070076}
77
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080078Allocation::~Allocation() {
Jason Sams8a647432010-03-01 15:31:04 -080079 if (mUserBitmapCallback != NULL) {
80 mUserBitmapCallback(mUserBitmapCallbackData);
Jason Sams5e0035a2010-12-13 17:11:21 -080081 mPtr = NULL;
Jason Sams8a647432010-03-01 15:31:04 -080082 }
Jason Sams5e0035a2010-12-13 17:11:21 -080083 freeScriptMemory();
Jason Sams9d5e03d2009-11-03 11:25:42 -080084
85 if (mBufferID) {
86 // Causes a SW crash....
87 //LOGV(" mBufferID %i", mBufferID);
88 //glDeleteBuffers(1, &mBufferID);
89 //mBufferID = 0;
90 }
91 if (mTextureID) {
92 glDeleteTextures(1, &mTextureID);
93 mTextureID = 0;
94 }
Jason Samsd19f10d2009-05-22 14:03:28 -070095}
96
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080097void Allocation::setCpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -070098}
99
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800100void Allocation::setGpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700101}
102
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800103void Allocation::setCpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700104}
105
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800106void Allocation::setGpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700107}
108
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800109bool Allocation::fixAllocation() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700110 return false;
111}
112
Jason Sams5e0035a2010-12-13 17:11:21 -0800113void Allocation::deferedUploadToTexture(const Context *rsc) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800114 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800115 mUploadDefered = true;
116}
117
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800118uint32_t Allocation::getGLTarget() const {
Jason Samsd4b23b52010-12-13 15:32:35 -0800119 if (getIsTexture()) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800120 if (mType->getDimFaces()) {
121 return GL_TEXTURE_CUBE_MAP;
122 } else {
123 return GL_TEXTURE_2D;
124 }
125 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800126 if (getIsBufferObject()) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800127 return GL_ARRAY_BUFFER;
128 }
129 return 0;
130}
131
Jason Sams5e0035a2010-12-13 17:11:21 -0800132void Allocation::allocScriptMemory() {
133 rsAssert(!mPtr);
134 mPtr = malloc(mType->getSizeBytes());
135}
136
137void Allocation::freeScriptMemory() {
Jason Sams5e0035a2010-12-13 17:11:21 -0800138 if (mPtr) {
139 free(mPtr);
140 mPtr = NULL;
141 }
142}
143
144
Jason Sams5476b452010-12-08 16:14:36 -0800145void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
146 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
147
Jason Samsd4b23b52010-12-13 15:32:35 -0800148 if (getIsTexture()) {
Jason Sams5476b452010-12-08 16:14:36 -0800149 uploadToTexture(rsc);
150 }
Jason Samsd4b23b52010-12-13 15:32:35 -0800151 if (getIsBufferObject()) {
Jason Sams5476b452010-12-08 16:14:36 -0800152 uploadToBufferObject(rsc);
153 }
154
155 mUploadDefered = false;
156}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800157
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800158void Allocation::uploadToTexture(const Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800159
Jason Samsd4b23b52010-12-13 15:32:35 -0800160 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
Jason Sams718cd1f2009-12-23 14:35:29 -0800161 GLenum type = mType->getElement()->getComponent().getGLType();
162 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Samse2ae85f2009-06-03 16:04:54 -0700163
164 if (!type || !format) {
165 return;
166 }
167
Jason Sams5e0035a2010-12-13 17:11:21 -0800168 if (!mPtr) {
169 return;
170 }
171
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700172 bool isFirstUpload = false;
173
Jason Samsd19f10d2009-05-22 14:03:28 -0700174 if (!mTextureID) {
175 glGenTextures(1, &mTextureID);
Jason Sams9dab6672009-11-24 12:26:35 -0800176
177 if (!mTextureID) {
178 // This should not happen, however, its likely the cause of the
179 // white sqare bug.
180 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
181 LOGE("Upload to texture failed to gen mTextureID");
182 rsc->dumpDebug();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800183 mUploadDefered = true;
184 return;
Jason Sams9dab6672009-11-24 12:26:35 -0800185 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700186 isFirstUpload = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700187 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800188
189 GLenum target = (GLenum)getGLTarget();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800190 if (target == GL_TEXTURE_2D) {
Jason Sams5e0035a2010-12-13 17:11:21 -0800191 upload2DTexture(isFirstUpload, mPtr);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800192 } else if (target == GL_TEXTURE_CUBE_MAP) {
193 uploadCubeTexture(isFirstUpload);
194 }
195
Jason Sams5e0035a2010-12-13 17:11:21 -0800196 if (!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
197 freeScriptMemory();
198 }
199
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800200 rsc->checkError("Allocation::uploadToTexture");
201}
202
Jason Sams5e0035a2010-12-13 17:11:21 -0800203void Allocation::upload2DTexture(bool isFirstUpload, const void *ptr) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800204 GLenum type = mType->getElement()->getComponent().getGLType();
205 GLenum format = mType->getElement()->getComponent().getGLFormat();
206
Jason Sams5e0035a2010-12-13 17:11:21 -0800207 GLenum target = (GLenum)getGLTarget();
208 glBindTexture(target, mTextureID);
209 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -0700210
Jason Sams5e0035a2010-12-13 17:11:21 -0800211 for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
212 const uint8_t *p = (const uint8_t *)ptr;
213 p += mType->getLODOffset(lod);
214
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800215 if (isFirstUpload) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700216 glTexImage2D(GL_TEXTURE_2D, lod, format,
Jason Sams5e0035a2010-12-13 17:11:21 -0800217 mType->getLODDimX(lod), mType->getLODDimY(lod),
218 0, format, type, p);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700219 } else {
220 glTexSubImage2D(GL_TEXTURE_2D, lod, 0, 0,
Jason Sams5e0035a2010-12-13 17:11:21 -0800221 mType->getLODDimX(lod), mType->getLODDimY(lod),
222 format, type, p);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700223 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700224 }
Jason Sams6d8eb262010-12-15 01:41:00 -0800225
226 if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
227#ifndef ANDROID_RS_BUILD_FOR_HOST
228 glGenerateMipmap(target);
229#endif //ANDROID_RS_BUILD_FOR_HOST
230 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800231}
Jason Samsc2908e62010-02-23 17:44:28 -0800232
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800233void Allocation::uploadCubeTexture(bool isFirstUpload) {
234 GLenum type = mType->getElement()->getComponent().getGLType();
235 GLenum format = mType->getElement()->getComponent().getGLFormat();
236
Jason Sams5e0035a2010-12-13 17:11:21 -0800237 GLenum target = (GLenum)getGLTarget();
238 glBindTexture(target, mTextureID);
239 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
240
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800241 GLenum faceOrder[] = {
242 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
243 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
244 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
245 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
246 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
247 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
248 };
249
250 Adapter2D adapt(getContext(), this);
251 for (uint32_t face = 0; face < 6; face ++) {
252 adapt.setFace(face);
253
Jason Sams5e0035a2010-12-13 17:11:21 -0800254 for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
255 adapt.setLOD(lod);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800256
257 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
258
259 if (isFirstUpload) {
260 glTexImage2D(faceOrder[face], lod, format,
261 adapt.getDimX(), adapt.getDimY(),
262 0, format, type, ptr);
263 } else {
264 glTexSubImage2D(faceOrder[face], lod, 0, 0,
265 adapt.getDimX(), adapt.getDimY(),
266 format, type, ptr);
267 }
268 }
269 }
Jason Sams6d8eb262010-12-15 01:41:00 -0800270
271 if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
272#ifndef ANDROID_RS_BUILD_FOR_HOST
273 glGenerateMipmap(target);
274#endif //ANDROID_RS_BUILD_FOR_HOST
275 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700276}
277
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800278void Allocation::deferedUploadToBufferObject(const Context *rsc) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800279 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800280 mUploadDefered = true;
281}
282
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800283void Allocation::uploadToBufferObject(const Context *rsc) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700284 rsAssert(!mType->getDimY());
285 rsAssert(!mType->getDimZ());
286
Jason Samsd4b23b52010-12-13 15:32:35 -0800287 mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800288
Jason Samsd19f10d2009-05-22 14:03:28 -0700289 if (!mBufferID) {
290 glGenBuffers(1, &mBufferID);
291 }
Jason Sams3b7d39b2009-12-14 12:57:40 -0800292 if (!mBufferID) {
293 LOGE("Upload to buffer object failed");
294 mUploadDefered = true;
295 return;
296 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800297 GLenum target = (GLenum)getGLTarget();
298 glBindBuffer(target, mBufferID);
299 glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
300 glBindBuffer(target, 0);
Jason Samsf4687002010-03-10 17:30:41 -0800301 rsc->checkError("Allocation::uploadToBufferObject");
Jason Samsd19f10d2009-05-22 14:03:28 -0700302}
303
Jason Sams5476b452010-12-08 16:14:36 -0800304void Allocation::uploadCheck(Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800305 if (mUploadDefered) {
Jason Sams5476b452010-12-08 16:14:36 -0800306 syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800307 }
308}
309
Jason Sams1bada8c2009-08-09 17:01:55 -0700310
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800311void Allocation::data(Context *rsc, const void *data, uint32_t sizeBytes) {
Jason Sams07ae4062009-08-27 20:23:34 -0700312 uint32_t size = mType->getSizeBytes();
313 if (size != sizeBytes) {
314 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
315 return;
316 }
Jason Samsb28ca962010-08-09 18:13:33 -0700317
318 if (mType->getElement()->getHasReferences()) {
319 incRefs(data, sizeBytes / mType->getElement()->getSizeBytes());
320 decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes());
321 }
322
Jason Sams07ae4062009-08-27 20:23:34 -0700323 memcpy(mPtr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700324 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800325 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700326}
327
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800328void Allocation::read(void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700329 memcpy(data, mPtr, mType->getSizeBytes());
330}
331
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800332void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700333 uint32_t eSize = mType->getElementSizeBytes();
334 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
335 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700336 uint32_t size = count * eSize;
337
338 if (size != sizeBytes) {
339 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Sams3c0dfba2009-09-27 17:50:38 -0700340 mType->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -0700341 return;
342 }
Jason Samsb28ca962010-08-09 18:13:33 -0700343
344 if (mType->getElement()->getHasReferences()) {
345 incRefs(data, count);
346 decRefs(ptr, count);
347 }
348
Jason Sams07ae4062009-08-27 20:23:34 -0700349 memcpy(ptr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700350 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800351 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700352}
353
Jason Sams49bdaf02010-08-31 13:50:42 -0700354void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800355 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700356 uint32_t eSize = mType->getElementSizeBytes();
357 uint32_t lineSize = eSize * w;
358 uint32_t destW = mType->getDimX();
359
360 const uint8_t *src = static_cast<const uint8_t *>(data);
361 uint8_t *dst = static_cast<uint8_t *>(mPtr);
362 dst += eSize * (xoff + yoff * destW);
Jason Sams07ae4062009-08-27 20:23:34 -0700363
364 if ((lineSize * eSize * h) != sizeBytes) {
365 rsAssert(!"Allocation::subData called with mismatched size");
366 return;
367 }
368
Jason Samsd19f10d2009-05-22 14:03:28 -0700369 for (uint32_t line=yoff; line < (yoff+h); line++) {
Jason Samsb28ca962010-08-09 18:13:33 -0700370 if (mType->getElement()->getHasReferences()) {
371 incRefs(src, w);
372 decRefs(dst, w);
373 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700374 memcpy(dst, src, lineSize);
375 src += lineSize;
376 dst += destW * eSize;
377 }
Jason Sams83f1c632009-10-26 15:19:28 -0700378 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800379 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700380}
381
Jason Sams49bdaf02010-08-31 13:50:42 -0700382void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800383 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700384}
385
Jason Sams49bdaf02010-08-31 13:50:42 -0700386void Allocation::subElementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800387 uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700388 uint32_t eSize = mType->getElementSizeBytes();
389 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
390 ptr += eSize * x;
391
392 if (cIdx >= mType->getElement()->getFieldCount()) {
393 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
394 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
395 return;
396 }
397
398 if (x >= mType->getDimX()) {
399 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
400 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
401 return;
402 }
403
404 const Element * e = mType->getElement()->getField(cIdx);
405 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
406
407 if (sizeBytes != e->getSizeBytes()) {
408 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
409 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
410 return;
411 }
412
413 if (e->getHasReferences()) {
414 e->incRefs(data);
415 e->decRefs(ptr);
416 }
417
418 memcpy(ptr, data, sizeBytes);
419 sendDirty();
420 mUploadDefered = true;
421}
422
423void Allocation::subElementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800424 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700425 uint32_t eSize = mType->getElementSizeBytes();
426 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
427 ptr += eSize * (x + y * mType->getDimX());
428
429 if (x >= mType->getDimX()) {
430 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
431 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
432 return;
433 }
434
435 if (y >= mType->getDimY()) {
436 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
437 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
438 return;
439 }
440
441 if (cIdx >= mType->getElement()->getFieldCount()) {
442 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
443 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
444 return;
445 }
446
447 const Element * e = mType->getElement()->getField(cIdx);
448 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
449
450 if (sizeBytes != e->getSizeBytes()) {
451 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
452 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
453 return;
454 }
455
456 if (e->getHasReferences()) {
457 e->incRefs(data);
458 e->decRefs(ptr);
459 }
460
461 memcpy(ptr, data, sizeBytes);
462 sendDirty();
463 mUploadDefered = true;
464}
465
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800466void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700467 mToDirtyList.push(p);
Jason Sams83f1c632009-10-26 15:19:28 -0700468}
Jason Samsd19f10d2009-05-22 14:03:28 -0700469
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800470void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams83f1c632009-10-26 15:19:28 -0700471 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
472 if (mToDirtyList[ct] == p) {
473 mToDirtyList.removeAt(ct);
474 return;
475 }
476 }
477 rsAssert(0);
478}
479
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800480void Allocation::dumpLOGV(const char *prefix) const {
Jason Sams715333b2009-11-17 17:26:46 -0800481 ObjectBase::dumpLOGV(prefix);
482
483 String8 s(prefix);
484 s.append(" type ");
485 if (mType.get()) {
486 mType->dumpLOGV(s.string());
487 }
488
489 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
490 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
491
Jason Samsd4b23b52010-12-13 15:32:35 -0800492 LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
493 prefix, mUsageFlags, mMipmapControl, mTextureID, mBufferID);
Jason Sams715333b2009-11-17 17:26:46 -0800494}
Jason Samsd19f10d2009-05-22 14:03:28 -0700495
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800496void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700497 // Need to identify ourselves
498 stream->addU32((uint32_t)getClassId());
499
500 String8 name(getName());
501 stream->addString(&name);
502
503 // First thing we need to serialize is the type object since it will be needed
504 // to initialize the class
505 mType->serialize(stream);
506
507 uint32_t dataSize = mType->getSizeBytes();
508 // Write how much data we are storing
509 stream->addU32(dataSize);
510 // Now write the data
511 stream->addByteArray(mPtr, dataSize);
512}
513
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800514Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700515 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700516 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800517 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700518 LOGE("allocation loading skipped due to invalid class id\n");
519 return NULL;
520 }
521
522 String8 name;
523 stream->loadString(&name);
524
525 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800526 if (!type) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700527 return NULL;
528 }
529 type->compute();
530
531 // Number of bytes we wrote out for this allocation
532 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800533 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700534 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Samsb38d5342010-10-21 14:06:55 -0700535 ObjectBase::checkDelete(type);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700536 return NULL;
537 }
538
Jason Samsd4b23b52010-12-13 15:32:35 -0800539 Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700540 alloc->setName(name.string(), name.size());
541
542 // Read in all of our allocation data
Jason Sams49bdaf02010-08-31 13:50:42 -0700543 alloc->data(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2ce0e3f2010-08-11 10:30:44 -0700544 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700545
546 return alloc;
547}
548
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800549void Allocation::sendDirty() const {
Jason Sams83f1c632009-10-26 15:19:28 -0700550 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
551 mToDirtyList[ct]->forceDirty();
552 }
553}
Jason Samsd19f10d2009-05-22 14:03:28 -0700554
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800555void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca962010-08-09 18:13:33 -0700556 const uint8_t *p = static_cast<const uint8_t *>(ptr);
557 const Element *e = mType->getElement();
558 uint32_t stride = e->getSizeBytes();
559
Jason Sams5edc6082010-10-05 13:32:49 -0700560 p += stride * startOff;
Jason Samsb28ca962010-08-09 18:13:33 -0700561 while (ct > 0) {
562 e->incRefs(p);
563 ct --;
564 p += stride;
565 }
566}
567
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800568void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca962010-08-09 18:13:33 -0700569 const uint8_t *p = static_cast<const uint8_t *>(ptr);
570 const Element *e = mType->getElement();
571 uint32_t stride = e->getSizeBytes();
572
Jason Sams5edc6082010-10-05 13:32:49 -0700573 p += stride * startOff;
Jason Samsb28ca962010-08-09 18:13:33 -0700574 while (ct > 0) {
575 e->decRefs(p);
576 ct --;
577 p += stride;
578 }
579}
580
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800581void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams5edc6082010-10-05 13:32:49 -0700582}
583
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800584void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700585 Type *t = mType->cloneAndResize1D(rsc, dimX);
586
587 uint32_t oldDimX = mType->getDimX();
588 if (dimX == oldDimX) {
589 return;
590 }
591
592 if (dimX < oldDimX) {
593 decRefs(mPtr, oldDimX - dimX, dimX);
594 }
595 mPtr = realloc(mPtr, t->getSizeBytes());
596
597 if (dimX > oldDimX) {
598 const Element *e = mType->getElement();
599 uint32_t stride = e->getSizeBytes();
600 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
601 }
602 mType.set(t);
603}
604
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800605void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700606 LOGE("not implemented");
607}
608
Jason Samsd19f10d2009-05-22 14:03:28 -0700609/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700610//
Jason Samsd19f10d2009-05-22 14:03:28 -0700611
612
613namespace android {
614namespace renderscript {
615
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800616void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700617 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams5e0035a2010-12-13 17:11:21 -0800618 alloc->deferedUploadToTexture(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700619}
620
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800621void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700622 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800623 alloc->deferedUploadToBufferObject(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700624}
625
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800626static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700627 uint32_t w = out.getDimX();
628 uint32_t h = out.getDimY();
629
Jason Sams6f5c61c2009-07-28 17:20:11 -0700630 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700631 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
632 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
633 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
634
Jason Sams6f5c61c2009-07-28 17:20:11 -0700635 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700636 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
637 oPtr ++;
638 i1 += 2;
639 i2 += 2;
640 }
641 }
642}
643
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800644static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700645 uint32_t w = out.getDimX();
646 uint32_t h = out.getDimY();
647
Jason Sams6f5c61c2009-07-28 17:20:11 -0700648 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700649 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
650 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
651 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
652
Jason Sams6f5c61c2009-07-28 17:20:11 -0700653 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700654 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700655 oPtr ++;
656 i1 += 2;
657 i2 += 2;
658 }
659 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700660}
661
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800662static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Samse20e3b42010-01-19 17:53:54 -0800663 uint32_t w = out.getDimX();
664 uint32_t h = out.getDimY();
665
666 for (uint32_t y=0; y < h; y++) {
667 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
668 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
669 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
670
671 for (uint32_t x=0; x < w; x++) {
672 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
673 oPtr ++;
674 i1 += 2;
675 i2 += 2;
676 }
677 }
678}
679
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800680static void mip(const Adapter2D &out, const Adapter2D &in) {
681 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Sams6f5c61c2009-07-28 17:20:11 -0700682 case 32:
683 mip8888(out, in);
684 break;
685 case 16:
686 mip565(out, in);
687 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800688 case 8:
689 mip8(out, in);
690 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700691 }
Jason Sams6f5c61c2009-07-28 17:20:11 -0700692}
Jason Samsd19f10d2009-05-22 14:03:28 -0700693
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700694#ifndef ANDROID_RS_BUILD_FOR_HOST
695
Jason Sams5476b452010-12-08 16:14:36 -0800696void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
697 Allocation *a = static_cast<Allocation *>(va);
698 a->syncAll(rsc, src);
699}
700
Jason Sams4ef66502010-12-10 16:03:15 -0800701void rsi_AllocationCopyFromBitmap(Context *rsc, RsAllocation va, const void *data, size_t dataLen) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700702 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Sams4ef66502010-12-10 16:03:15 -0800703 const Type * t = texAlloc->getType();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700704
Jason Sams4ef66502010-12-10 16:03:15 -0800705 uint32_t w = t->getDimX();
706 uint32_t h = t->getDimY();
707 bool genMips = t->getDimLOD();
708 size_t s = w * h * t->getElementSizeBytes();
709 if (s != dataLen) {
710 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
711 return;
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700712 }
Jason Sams4ef66502010-12-10 16:03:15 -0800713
Jason Sams5e0035a2010-12-13 17:11:21 -0800714 if (texAlloc->getIsScript()) {
715 memcpy(texAlloc->getPtr(), data, s);
716 if (genMips) {
717 Adapter2D adapt(rsc, texAlloc);
718 Adapter2D adapt2(rsc, texAlloc);
719 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
720 adapt.setLOD(lod);
721 adapt2.setLOD(lod + 1);
722 mip(adapt2, adapt);
723 }
Jason Sams4ef66502010-12-10 16:03:15 -0800724 }
Jason Sams5e0035a2010-12-13 17:11:21 -0800725 } else {
726 texAlloc->upload2DTexture(false, data);
Jason Sams4ef66502010-12-10 16:03:15 -0800727 }
Jason Sams5e0035a2010-12-13 17:11:21 -0800728
Jason Sams4ef66502010-12-10 16:03:15 -0800729}
730
731void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
732 Allocation *texAlloc = static_cast<Allocation *>(va);
733 const Type * t = texAlloc->getType();
734
735 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
736 if (s != dataLen) {
737 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
738 return;
739 }
740
741 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700742}
743
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800744void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700745 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700746 a->data(rsc, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700747}
748
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800749void 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 -0700750 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700751 a->subData(rsc, xoff, count, data, sizeBytes);
752}
753
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800754void 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 -0700755 Allocation *a = static_cast<Allocation *>(va);
756 a->subElementData(rsc, x, y, data, eoff, sizeBytes);
757}
758
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800759void 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 -0700760 Allocation *a = static_cast<Allocation *>(va);
761 a->subElementData(rsc, x, data, eoff, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700762}
763
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800764void 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 -0700765 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700766 a->subData(rsc, xoff, yoff, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700767}
768
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800769void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700770 Allocation *a = static_cast<Allocation *>(va);
771 a->read(data);
772}
773
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800774void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700775 Allocation *a = static_cast<Allocation *>(va);
776 a->resize1D(rsc, dimX);
777}
778
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800779void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700780 Allocation *a = static_cast<Allocation *>(va);
781 a->resize2D(rsc, dimX, dimY);
782}
783
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700784#endif //ANDROID_RS_BUILD_FOR_HOST
785
786}
787}
788
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800789const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700790 Allocation *a = static_cast<Allocation *>(va);
791 a->getType()->incUserRef();
792
793 return a->getType();
794}
795
Jason Sams5476b452010-12-08 16:14:36 -0800796RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800797 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800798 uint32_t usages) {
Jason Sams31a7e422010-10-26 13:09:17 -0700799 Context *rsc = static_cast<Context *>(con);
Alex Sakhartchouk08571962010-12-15 09:59:58 -0800800 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages, mips);
Jason Sams31a7e422010-10-26 13:09:17 -0700801 alloc->incUserRef();
802 return alloc;
803}
804
Jason Sams5476b452010-12-08 16:14:36 -0800805
806RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800807 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800808 const void *data, uint32_t usages) {
Jason Sams31a7e422010-10-26 13:09:17 -0700809 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800810 Type *t = static_cast<Type *>(vtype);
Jason Sams31a7e422010-10-26 13:09:17 -0700811
Jason Sams5476b452010-12-08 16:14:36 -0800812 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages);
Jason Sams31a7e422010-10-26 13:09:17 -0700813 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
814 if (texAlloc == NULL) {
815 LOGE("Memory allocation failure");
816 return NULL;
817 }
818
Jason Sams5476b452010-12-08 16:14:36 -0800819 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
Jason Samsd4b23b52010-12-13 15:32:35 -0800820 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams5476b452010-12-08 16:14:36 -0800821 Adapter2D adapt(rsc, texAlloc);
822 Adapter2D adapt2(rsc, texAlloc);
823 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
824 adapt.setLOD(lod);
825 adapt2.setLOD(lod + 1);
826 mip(adapt2, adapt);
Jason Sams31a7e422010-10-26 13:09:17 -0700827 }
Jason Sams31a7e422010-10-26 13:09:17 -0700828 }
829
Jason Sams5e0035a2010-12-13 17:11:21 -0800830 texAlloc->deferedUploadToTexture(rsc);
Jason Sams31a7e422010-10-26 13:09:17 -0700831 return texAlloc;
832}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800833
Jason Sams5476b452010-12-08 16:14:36 -0800834RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
Jason Samsd4b23b52010-12-13 15:32:35 -0800835 RsAllocationMipmapControl mips,
Jason Sams5476b452010-12-08 16:14:36 -0800836 const void *data, uint32_t usages) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800837 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800838 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800839
840 // Cubemap allocation's faces should be Width by Width each.
841 // Source data should have 6 * Width by Width pixels
842 // Error checking is done in the java layer
Jason Sams5476b452010-12-08 16:14:36 -0800843 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800844 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
845 if (texAlloc == NULL) {
846 LOGE("Memory allocation failure");
847 return NULL;
848 }
849
850 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams5476b452010-12-08 16:14:36 -0800851 for (uint32_t face = 0; face < 6; face ++) {
852 Adapter2D faceAdapter(rsc, texAlloc);
853 faceAdapter.setFace(face);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800854
Jason Sams5476b452010-12-08 16:14:36 -0800855 size_t cpySize = t->getDimX() * t->getDimX() * t->getElementSizeBytes();
856 memcpy(faceAdapter.getElement(0, 0), sourcePtr, cpySize);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800857
Jason Sams5476b452010-12-08 16:14:36 -0800858 // Move the data pointer to the next cube face
859 sourcePtr += cpySize;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800860
Jason Samsd4b23b52010-12-13 15:32:35 -0800861 if (mips == RS_ALLOCATION_MIPMAP_FULL) {
Jason Sams5476b452010-12-08 16:14:36 -0800862 Adapter2D adapt(rsc, texAlloc);
863 Adapter2D adapt2(rsc, texAlloc);
864 adapt.setFace(face);
865 adapt2.setFace(face);
866 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
867 adapt.setLOD(lod);
868 adapt2.setLOD(lod + 1);
869 mip(adapt2, adapt);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800870 }
871 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800872 }
873
Jason Sams5e0035a2010-12-13 17:11:21 -0800874 texAlloc->deferedUploadToTexture(rsc);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800875 return texAlloc;
876}