blob: 10a5caf560198e186cb68ff2c426416b4ef63559 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070016#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsd19f10d2009-05-22 14:03:28 -070017#include "rsContext.h"
18
Jason Sams4b962e52009-06-22 17:15:15 -070019#include <GLES/gl.h>
Jason Samsc2908e62010-02-23 17:44:28 -080020#include <GLES2/gl2.h>
Jason Sams4b962e52009-06-22 17:15:15 -070021#include <GLES/glext.h>
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070022#else
23#include "rsContextHostStub.h"
24
25#include <OpenGL/gl.h>
26#include <OpenGl/glext.h>
27#endif
Jason Sams4b962e52009-06-22 17:15:15 -070028
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -070029#include "utils/StopWatch.h"
30
Jason Samsd19f10d2009-05-22 14:03:28 -070031using namespace android;
32using namespace android::renderscript;
33
Jason Sams5476b452010-12-08 16:14:36 -080034Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages) : ObjectBase(rsc) {
Jason Sams8a64743f2010-03-01 15:31:04 -080035 init(rsc, type);
36
Jason Sams5476b452010-12-08 16:14:36 -080037 mUsageFlags = usages;
38
Jason Sams8a64743f2010-03-01 15:31:04 -080039 mPtr = malloc(mType->getSizeBytes());
Jason Samsee734982010-08-12 12:44:02 -070040 if (mType->getElement()->getHasReferences()) {
41 memset(mPtr, 0, mType->getSizeBytes());
42 }
Jason Sams8a64743f2010-03-01 15:31:04 -080043 if (!mPtr) {
44 LOGE("Allocation::Allocation, alloc failure");
45 }
46}
47
48Allocation::Allocation(Context *rsc, const Type *type, void *bmp,
49 void *callbackData, RsBitmapCallback_t callback)
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080050 : ObjectBase(rsc) {
Jason Sams8a64743f2010-03-01 15:31:04 -080051 init(rsc, type);
52
Jason Sams5476b452010-12-08 16:14:36 -080053 mUsageFlags = RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
54
Jason Sams8a64743f2010-03-01 15:31:04 -080055 mPtr = bmp;
56 mUserBitmapCallback = callback;
57 mUserBitmapCallbackData = callbackData;
58}
59
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080060void Allocation::init(Context *rsc, const Type *type) {
Jason Samsd19f10d2009-05-22 14:03:28 -070061 mPtr = NULL;
62
63 mCpuWrite = false;
64 mCpuRead = false;
65 mGpuWrite = false;
66 mGpuRead = false;
67
68 mReadWriteRatio = 0;
69 mUpdateSize = 0;
70
71 mIsTexture = false;
72 mTextureID = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070073 mIsVertexBuffer = false;
74 mBufferID = 0;
Jason Sams3b7d39b2009-12-14 12:57:40 -080075 mUploadDefered = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070076
Jason Sams8a64743f2010-03-01 15:31:04 -080077 mUserBitmapCallback = NULL;
78 mUserBitmapCallbackData = NULL;
79
Jason Samsd19f10d2009-05-22 14:03:28 -070080 mType.set(type);
Jason Sams1bada8c2009-08-09 17:01:55 -070081 rsAssert(type);
Jason Sams8a64743f2010-03-01 15:31:04 -080082
83 mPtr = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -070084}
85
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080086Allocation::~Allocation() {
Jason Sams8a64743f2010-03-01 15:31:04 -080087 if (mUserBitmapCallback != NULL) {
88 mUserBitmapCallback(mUserBitmapCallbackData);
89 } else {
90 free(mPtr);
91 }
Jason Samsb7a6c432009-11-02 14:25:10 -080092 mPtr = NULL;
Jason Sams9d5e03d2009-11-03 11:25:42 -080093
94 if (mBufferID) {
95 // Causes a SW crash....
96 //LOGV(" mBufferID %i", mBufferID);
97 //glDeleteBuffers(1, &mBufferID);
98 //mBufferID = 0;
99 }
100 if (mTextureID) {
101 glDeleteTextures(1, &mTextureID);
102 mTextureID = 0;
103 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700104}
105
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800106void Allocation::setCpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700107}
108
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800109void Allocation::setGpuWritable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700110}
111
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800112void Allocation::setCpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700113}
114
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800115void Allocation::setGpuReadable(bool) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700116}
117
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800118bool Allocation::fixAllocation() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700119 return false;
120}
121
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800122void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800123 rsAssert(lodOffset < mType->getLODCount());
124 mIsTexture = true;
125 mTextureLOD = lodOffset;
126 mUploadDefered = true;
Jason Samsc2908e62010-02-23 17:44:28 -0800127 mTextureGenMipmap = !mType->getDimLOD() && genMipmap;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800128}
129
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800130uint32_t Allocation::getGLTarget() const {
131 if (mIsTexture) {
132 if (mType->getDimFaces()) {
133 return GL_TEXTURE_CUBE_MAP;
134 } else {
135 return GL_TEXTURE_2D;
136 }
137 }
138 if (mIsVertexBuffer) {
139 return GL_ARRAY_BUFFER;
140 }
141 return 0;
142}
143
Jason Sams5476b452010-12-08 16:14:36 -0800144void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
145 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
146
147 if (mIsTexture) {
148 uploadToTexture(rsc);
149 }
150 if (mIsVertexBuffer) {
151 uploadToBufferObject(rsc);
152 }
153
154 mUploadDefered = false;
155}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800156
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800157void Allocation::uploadToTexture(const Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800158
159 mIsTexture = true;
Jason Sams718cd1f2009-12-23 14:35:29 -0800160 GLenum type = mType->getElement()->getComponent().getGLType();
161 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Samse2ae85f2009-06-03 16:04:54 -0700162
163 if (!type || !format) {
164 return;
165 }
166
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700167 bool isFirstUpload = false;
168
Jason Samsd19f10d2009-05-22 14:03:28 -0700169 if (!mTextureID) {
170 glGenTextures(1, &mTextureID);
Jason Sams9dab6672009-11-24 12:26:35 -0800171
172 if (!mTextureID) {
173 // This should not happen, however, its likely the cause of the
174 // white sqare bug.
175 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
176 LOGE("Upload to texture failed to gen mTextureID");
177 rsc->dumpDebug();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800178 mUploadDefered = true;
179 return;
Jason Sams9dab6672009-11-24 12:26:35 -0800180 }
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700181 isFirstUpload = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700182 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800183
184 GLenum target = (GLenum)getGLTarget();
185 glBindTexture(target, mTextureID);
Jason Sams0e27b5c2009-11-05 12:44:58 -0800186 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -0700187
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800188 if (target == GL_TEXTURE_2D) {
189 upload2DTexture(isFirstUpload);
190 } else if (target == GL_TEXTURE_CUBE_MAP) {
191 uploadCubeTexture(isFirstUpload);
192 }
193
194 if (mTextureGenMipmap) {
195#ifndef ANDROID_RS_BUILD_FOR_HOST
196 glGenerateMipmap(target);
197#endif //ANDROID_RS_BUILD_FOR_HOST
198 }
199
200 rsc->checkError("Allocation::uploadToTexture");
201}
202
203void Allocation::upload2DTexture(bool isFirstUpload) {
204 GLenum type = mType->getElement()->getComponent().getGLType();
205 GLenum format = mType->getElement()->getComponent().getGLFormat();
206
Jason Samsa9e7a052009-09-25 14:51:22 -0700207 Adapter2D adapt(getContext(), this);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800208 for (uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800209 adapt.setLOD(lod+mTextureLOD);
Jason Samsd19f10d2009-05-22 14:03:28 -0700210
211 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800212 if (isFirstUpload) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700213 glTexImage2D(GL_TEXTURE_2D, lod, format,
214 adapt.getDimX(), adapt.getDimY(),
215 0, format, type, ptr);
216 } else {
217 glTexSubImage2D(GL_TEXTURE_2D, lod, 0, 0,
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800218 adapt.getDimX(), adapt.getDimY(),
219 format, type, ptr);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700220 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700221 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800222}
Jason Samsc2908e62010-02-23 17:44:28 -0800223
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800224void Allocation::uploadCubeTexture(bool isFirstUpload) {
225 GLenum type = mType->getElement()->getComponent().getGLType();
226 GLenum format = mType->getElement()->getComponent().getGLFormat();
227
228 GLenum faceOrder[] = {
229 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
230 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
231 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
232 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
233 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
234 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
235 };
236
237 Adapter2D adapt(getContext(), this);
238 for (uint32_t face = 0; face < 6; face ++) {
239 adapt.setFace(face);
240
241 for (uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
242 adapt.setLOD(lod+mTextureLOD);
243
244 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
245
246 if (isFirstUpload) {
247 glTexImage2D(faceOrder[face], lod, format,
248 adapt.getDimX(), adapt.getDimY(),
249 0, format, type, ptr);
250 } else {
251 glTexSubImage2D(faceOrder[face], lod, 0, 0,
252 adapt.getDimX(), adapt.getDimY(),
253 format, type, ptr);
254 }
255 }
256 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700257}
258
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800259void Allocation::deferedUploadToBufferObject(const Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800260 mIsVertexBuffer = true;
261 mUploadDefered = true;
262}
263
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800264void Allocation::uploadToBufferObject(const Context *rsc) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700265 rsAssert(!mType->getDimY());
266 rsAssert(!mType->getDimZ());
267
Jason Sams3b7d39b2009-12-14 12:57:40 -0800268 mIsVertexBuffer = true;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800269
Jason Samsd19f10d2009-05-22 14:03:28 -0700270 if (!mBufferID) {
271 glGenBuffers(1, &mBufferID);
272 }
Jason Sams3b7d39b2009-12-14 12:57:40 -0800273 if (!mBufferID) {
274 LOGE("Upload to buffer object failed");
275 mUploadDefered = true;
276 return;
277 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800278 GLenum target = (GLenum)getGLTarget();
279 glBindBuffer(target, mBufferID);
280 glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
281 glBindBuffer(target, 0);
Jason Samsf4687002010-03-10 17:30:41 -0800282 rsc->checkError("Allocation::uploadToBufferObject");
Jason Samsd19f10d2009-05-22 14:03:28 -0700283}
284
Jason Sams5476b452010-12-08 16:14:36 -0800285void Allocation::uploadCheck(Context *rsc) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800286 if (mUploadDefered) {
Jason Sams5476b452010-12-08 16:14:36 -0800287 syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800288 }
289}
290
Jason Sams1bada8c2009-08-09 17:01:55 -0700291
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800292void Allocation::data(Context *rsc, const void *data, uint32_t sizeBytes) {
Jason Sams07ae4062009-08-27 20:23:34 -0700293 uint32_t size = mType->getSizeBytes();
294 if (size != sizeBytes) {
295 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
296 return;
297 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700298
299 if (mType->getElement()->getHasReferences()) {
300 incRefs(data, sizeBytes / mType->getElement()->getSizeBytes());
301 decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes());
302 }
303
Jason Sams07ae4062009-08-27 20:23:34 -0700304 memcpy(mPtr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700305 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800306 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700307}
308
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800309void Allocation::read(void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700310 memcpy(data, mPtr, mType->getSizeBytes());
311}
312
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800313void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700314 uint32_t eSize = mType->getElementSizeBytes();
315 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
316 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700317 uint32_t size = count * eSize;
318
319 if (size != sizeBytes) {
320 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Sams3c0dfba2009-09-27 17:50:38 -0700321 mType->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -0700322 return;
323 }
Jason Samsb28ca96f2010-08-09 18:13:33 -0700324
325 if (mType->getElement()->getHasReferences()) {
326 incRefs(data, count);
327 decRefs(ptr, count);
328 }
329
Jason Sams07ae4062009-08-27 20:23:34 -0700330 memcpy(ptr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700331 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800332 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700333}
334
Jason Sams49bdaf02010-08-31 13:50:42 -0700335void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800336 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700337 uint32_t eSize = mType->getElementSizeBytes();
338 uint32_t lineSize = eSize * w;
339 uint32_t destW = mType->getDimX();
340
341 const uint8_t *src = static_cast<const uint8_t *>(data);
342 uint8_t *dst = static_cast<uint8_t *>(mPtr);
343 dst += eSize * (xoff + yoff * destW);
Jason Sams07ae4062009-08-27 20:23:34 -0700344
345 if ((lineSize * eSize * h) != sizeBytes) {
346 rsAssert(!"Allocation::subData called with mismatched size");
347 return;
348 }
349
Jason Samsd19f10d2009-05-22 14:03:28 -0700350 for (uint32_t line=yoff; line < (yoff+h); line++) {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700351 if (mType->getElement()->getHasReferences()) {
352 incRefs(src, w);
353 decRefs(dst, w);
354 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700355 memcpy(dst, src, lineSize);
356 src += lineSize;
357 dst += destW * eSize;
358 }
Jason Sams83f1c632009-10-26 15:19:28 -0700359 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800360 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700361}
362
Jason Sams49bdaf02010-08-31 13:50:42 -0700363void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800364 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700365}
366
Jason Sams49bdaf02010-08-31 13:50:42 -0700367void Allocation::subElementData(Context *rsc, uint32_t x, const void *data,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800368 uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700369 uint32_t eSize = mType->getElementSizeBytes();
370 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
371 ptr += eSize * x;
372
373 if (cIdx >= mType->getElement()->getFieldCount()) {
374 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
375 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
376 return;
377 }
378
379 if (x >= mType->getDimX()) {
380 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
381 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
382 return;
383 }
384
385 const Element * e = mType->getElement()->getField(cIdx);
386 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
387
388 if (sizeBytes != e->getSizeBytes()) {
389 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
390 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
391 return;
392 }
393
394 if (e->getHasReferences()) {
395 e->incRefs(data);
396 e->decRefs(ptr);
397 }
398
399 memcpy(ptr, data, sizeBytes);
400 sendDirty();
401 mUploadDefered = true;
402}
403
404void Allocation::subElementData(Context *rsc, uint32_t x, uint32_t y,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800405 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
Jason Sams49bdaf02010-08-31 13:50:42 -0700406 uint32_t eSize = mType->getElementSizeBytes();
407 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
408 ptr += eSize * (x + y * mType->getDimX());
409
410 if (x >= mType->getDimX()) {
411 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
412 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
413 return;
414 }
415
416 if (y >= mType->getDimY()) {
417 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
418 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
419 return;
420 }
421
422 if (cIdx >= mType->getElement()->getFieldCount()) {
423 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
424 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
425 return;
426 }
427
428 const Element * e = mType->getElement()->getField(cIdx);
429 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
430
431 if (sizeBytes != e->getSizeBytes()) {
432 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
433 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
434 return;
435 }
436
437 if (e->getHasReferences()) {
438 e->incRefs(data);
439 e->decRefs(ptr);
440 }
441
442 memcpy(ptr, data, sizeBytes);
443 sendDirty();
444 mUploadDefered = true;
445}
446
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800447void Allocation::addProgramToDirty(const Program *p) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700448 mToDirtyList.push(p);
Jason Sams83f1c632009-10-26 15:19:28 -0700449}
Jason Samsd19f10d2009-05-22 14:03:28 -0700450
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800451void Allocation::removeProgramToDirty(const Program *p) {
Jason Sams83f1c632009-10-26 15:19:28 -0700452 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
453 if (mToDirtyList[ct] == p) {
454 mToDirtyList.removeAt(ct);
455 return;
456 }
457 }
458 rsAssert(0);
459}
460
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800461void Allocation::dumpLOGV(const char *prefix) const {
Jason Sams715333b2009-11-17 17:26:46 -0800462 ObjectBase::dumpLOGV(prefix);
463
464 String8 s(prefix);
465 s.append(" type ");
466 if (mType.get()) {
467 mType->dumpLOGV(s.string());
468 }
469
470 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
471 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
472
Jason Sams361765362009-11-23 15:27:33 -0800473 LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i",
Jason Sams715333b2009-11-17 17:26:46 -0800474 prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID);
Jason Sams715333b2009-11-17 17:26:46 -0800475}
Jason Samsd19f10d2009-05-22 14:03:28 -0700476
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800477void Allocation::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700478 // Need to identify ourselves
479 stream->addU32((uint32_t)getClassId());
480
481 String8 name(getName());
482 stream->addString(&name);
483
484 // First thing we need to serialize is the type object since it will be needed
485 // to initialize the class
486 mType->serialize(stream);
487
488 uint32_t dataSize = mType->getSizeBytes();
489 // Write how much data we are storing
490 stream->addU32(dataSize);
491 // Now write the data
492 stream->addByteArray(mPtr, dataSize);
493}
494
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800495Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700496 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700497 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800498 if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700499 LOGE("allocation loading skipped due to invalid class id\n");
500 return NULL;
501 }
502
503 String8 name;
504 stream->loadString(&name);
505
506 Type *type = Type::createFromStream(rsc, stream);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800507 if (!type) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700508 return NULL;
509 }
510 type->compute();
511
512 // Number of bytes we wrote out for this allocation
513 uint32_t dataSize = stream->loadU32();
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800514 if (dataSize != type->getSizeBytes()) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700515 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
Jason Samsb38d5342010-10-21 14:06:55 -0700516 ObjectBase::checkDelete(type);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700517 return NULL;
518 }
519
Jason Sams5476b452010-12-08 16:14:36 -0800520 Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_ALL);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700521 alloc->setName(name.string(), name.size());
522
523 // Read in all of our allocation data
Jason Sams49bdaf02010-08-31 13:50:42 -0700524 alloc->data(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouk2ce0e3f2010-08-11 10:30:44 -0700525 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700526
527 return alloc;
528}
529
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800530void Allocation::sendDirty() const {
Jason Sams83f1c632009-10-26 15:19:28 -0700531 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
532 mToDirtyList[ct]->forceDirty();
533 }
534}
Jason Samsd19f10d2009-05-22 14:03:28 -0700535
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800536void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700537 const uint8_t *p = static_cast<const uint8_t *>(ptr);
538 const Element *e = mType->getElement();
539 uint32_t stride = e->getSizeBytes();
540
Jason Sams5edc6082010-10-05 13:32:49 -0700541 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700542 while (ct > 0) {
543 e->incRefs(p);
544 ct --;
545 p += stride;
546 }
547}
548
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800549void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
Jason Samsb28ca96f2010-08-09 18:13:33 -0700550 const uint8_t *p = static_cast<const uint8_t *>(ptr);
551 const Element *e = mType->getElement();
552 uint32_t stride = e->getSizeBytes();
553
Jason Sams5edc6082010-10-05 13:32:49 -0700554 p += stride * startOff;
Jason Samsb28ca96f2010-08-09 18:13:33 -0700555 while (ct > 0) {
556 e->decRefs(p);
557 ct --;
558 p += stride;
559 }
560}
561
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800562void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
Jason Sams5edc6082010-10-05 13:32:49 -0700563}
564
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800565void Allocation::resize1D(Context *rsc, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700566 Type *t = mType->cloneAndResize1D(rsc, dimX);
567
568 uint32_t oldDimX = mType->getDimX();
569 if (dimX == oldDimX) {
570 return;
571 }
572
573 if (dimX < oldDimX) {
574 decRefs(mPtr, oldDimX - dimX, dimX);
575 }
576 mPtr = realloc(mPtr, t->getSizeBytes());
577
578 if (dimX > oldDimX) {
579 const Element *e = mType->getElement();
580 uint32_t stride = e->getSizeBytes();
581 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
582 }
583 mType.set(t);
584}
585
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800586void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700587 LOGE("not implemented");
588}
589
Jason Samsd19f10d2009-05-22 14:03:28 -0700590/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700591//
Jason Samsd19f10d2009-05-22 14:03:28 -0700592
593
594namespace android {
595namespace renderscript {
596
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800597void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700598 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samsc2908e62010-02-23 17:44:28 -0800599 alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel);
Jason Samsd19f10d2009-05-22 14:03:28 -0700600}
601
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800602void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700603 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800604 alloc->deferedUploadToBufferObject(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700605}
606
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800607static void mip565(const Adapter2D &out, const Adapter2D &in) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700608 uint32_t w = out.getDimX();
609 uint32_t h = out.getDimY();
610
Jason Sams6f5c61c2009-07-28 17:20:11 -0700611 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700612 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
613 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
614 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
615
Jason Sams6f5c61c2009-07-28 17:20:11 -0700616 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700617 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
618 oPtr ++;
619 i1 += 2;
620 i2 += 2;
621 }
622 }
623}
624
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800625static void mip8888(const Adapter2D &out, const Adapter2D &in) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700626 uint32_t w = out.getDimX();
627 uint32_t h = out.getDimY();
628
Jason Sams6f5c61c2009-07-28 17:20:11 -0700629 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700630 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
631 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
632 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
633
Jason Sams6f5c61c2009-07-28 17:20:11 -0700634 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700635 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700636 oPtr ++;
637 i1 += 2;
638 i2 += 2;
639 }
640 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700641}
642
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800643static void mip8(const Adapter2D &out, const Adapter2D &in) {
Jason Samse20e3b42010-01-19 17:53:54 -0800644 uint32_t w = out.getDimX();
645 uint32_t h = out.getDimY();
646
647 for (uint32_t y=0; y < h; y++) {
648 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
649 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
650 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
651
652 for (uint32_t x=0; x < w; x++) {
653 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
654 oPtr ++;
655 i1 += 2;
656 i2 += 2;
657 }
658 }
659}
660
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800661static void mip(const Adapter2D &out, const Adapter2D &in) {
662 switch (out.getBaseType()->getElement()->getSizeBits()) {
Jason Sams6f5c61c2009-07-28 17:20:11 -0700663 case 32:
664 mip8888(out, in);
665 break;
666 case 16:
667 mip565(out, in);
668 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800669 case 8:
670 mip8(out, in);
671 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700672 }
Jason Sams6f5c61c2009-07-28 17:20:11 -0700673}
Jason Samsd19f10d2009-05-22 14:03:28 -0700674
Jason Samsfe08d992009-05-27 14:45:32 -0700675typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
676
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800677static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count) {
Jason Samsfe08d992009-05-27 14:45:32 -0700678 memcpy(dst, src, count * 2);
679}
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800680static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count) {
Jason Samsfe08d992009-05-27 14:45:32 -0700681 memcpy(dst, src, count);
682}
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800683static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count) {
Jason Samsfe08d992009-05-27 14:45:32 -0700684 memcpy(dst, src, count * 4);
685}
686
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800687static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count) {
Jason Samsfe08d992009-05-27 14:45:32 -0700688 uint16_t *d = static_cast<uint16_t *>(dst);
689 const uint8_t *s = static_cast<const uint8_t *>(src);
690
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800691 while (count--) {
Jason Samsfe08d992009-05-27 14:45:32 -0700692 *d = rs888to565(s[0], s[1], s[2]);
693 d++;
694 s+= 3;
695 }
696}
697
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800698static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count) {
Jason Samsfe08d992009-05-27 14:45:32 -0700699 uint16_t *d = static_cast<uint16_t *>(dst);
700 const uint8_t *s = static_cast<const uint8_t *>(src);
701
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800702 while (count--) {
Jason Samsfe08d992009-05-27 14:45:32 -0700703 *d = rs888to565(s[0], s[1], s[2]);
704 d++;
705 s+= 4;
706 }
707}
708
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800709static ElementConverter_t pickConverter(const Element *dst, const Element *src) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800710 GLenum srcGLType = src->getComponent().getGLType();
711 GLenum srcGLFmt = src->getComponent().getGLFormat();
712 GLenum dstGLType = dst->getComponent().getGLType();
713 GLenum dstGLFmt = dst->getComponent().getGLFormat();
Jason Samsea84a7c2009-09-04 14:42:41 -0700714
715 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800716 switch (dst->getSizeBytes()) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700717 case 4:
718 return elementConverter_cpy_32;
719 case 2:
720 return elementConverter_cpy_16;
721 case 1:
722 return elementConverter_cpy_8;
723 }
Jason Samsfe08d992009-05-27 14:45:32 -0700724 }
725
Jason Samsea84a7c2009-09-04 14:42:41 -0700726 if (srcGLType == GL_UNSIGNED_BYTE &&
727 srcGLFmt == GL_RGB &&
728 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
Jason Sams0ebd5692010-06-22 17:45:34 -0700729 dstGLFmt == GL_RGB) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700730
Jason Samsfe08d992009-05-27 14:45:32 -0700731 return elementConverter_888_to_565;
732 }
733
Jason Samsea84a7c2009-09-04 14:42:41 -0700734 if (srcGLType == GL_UNSIGNED_BYTE &&
735 srcGLFmt == GL_RGBA &&
736 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
Jason Sams0ebd5692010-06-22 17:45:34 -0700737 dstGLFmt == GL_RGB) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700738
Jason Samsfe08d992009-05-27 14:45:32 -0700739 return elementConverter_8888_to_565;
740 }
741
Jason Samsea84a7c2009-09-04 14:42:41 -0700742 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst);
Jason Sams0ebd5692010-06-22 17:45:34 -0700743 LOGE("pickConverter, srcGLType = %x, srcGLFmt = %x", srcGLType, srcGLFmt);
744 LOGE("pickConverter, dstGLType = %x, dstGLFmt = %x", dstGLType, dstGLFmt);
745 src->dumpLOGV("SRC ");
746 dst->dumpLOGV("DST ");
Jason Samsfe08d992009-05-27 14:45:32 -0700747 return 0;
748}
749
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700750#ifndef ANDROID_RS_BUILD_FOR_HOST
751
Jason Sams5476b452010-12-08 16:14:36 -0800752void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
753 Allocation *a = static_cast<Allocation *>(va);
754 a->syncAll(rsc, src);
755}
756
Jason Sams8a64743f2010-03-01 15:31:04 -0800757RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800758 void *bmp, void *callbackData,
759 RsBitmapCallback_t callback) {
Jason Sams8a64743f2010-03-01 15:31:04 -0800760 const Type * type = static_cast<const Type *>(vtype);
761 Allocation * alloc = new Allocation(rsc, type, bmp, callbackData, callback);
762 alloc->incUserRef();
763 return alloc;
764}
Jason Samsfe08d992009-05-27 14:45:32 -0700765
Jason Sams4ef66502010-12-10 16:03:15 -0800766void rsi_AllocationCopyFromBitmap(Context *rsc, RsAllocation va, const void *data, size_t dataLen) {
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700767 Allocation *texAlloc = static_cast<Allocation *>(va);
Jason Sams4ef66502010-12-10 16:03:15 -0800768 const Type * t = texAlloc->getType();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700769
Jason Sams4ef66502010-12-10 16:03:15 -0800770 uint32_t w = t->getDimX();
771 uint32_t h = t->getDimY();
772 bool genMips = t->getDimLOD();
773 size_t s = w * h * t->getElementSizeBytes();
774 if (s != dataLen) {
775 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
776 return;
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700777 }
Jason Sams4ef66502010-12-10 16:03:15 -0800778
779 memcpy(texAlloc->getPtr(), data, s);
780 if (genMips) {
781 Adapter2D adapt(rsc, texAlloc);
782 Adapter2D adapt2(rsc, texAlloc);
783 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
784 adapt.setLOD(lod);
785 adapt2.setLOD(lod + 1);
786 mip(adapt2, adapt);
787 }
788 }
789}
790
791void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
792 Allocation *texAlloc = static_cast<Allocation *>(va);
793 const Type * t = texAlloc->getType();
794
795 size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
796 if (s != dataLen) {
797 rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
798 return;
799 }
800
801 memcpy(data, texAlloc->getPtr(), s);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700802}
803
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800804void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700805 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700806 a->data(rsc, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700807}
808
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800809void 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 -0700810 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700811 a->subData(rsc, xoff, count, data, sizeBytes);
812}
813
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800814void 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 -0700815 Allocation *a = static_cast<Allocation *>(va);
816 a->subElementData(rsc, x, y, data, eoff, sizeBytes);
817}
818
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800819void 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 -0700820 Allocation *a = static_cast<Allocation *>(va);
821 a->subElementData(rsc, x, data, eoff, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700822}
823
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800824void 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 -0700825 Allocation *a = static_cast<Allocation *>(va);
Jason Sams49bdaf02010-08-31 13:50:42 -0700826 a->subData(rsc, xoff, yoff, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700827}
828
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800829void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
Jason Sams40a29e82009-08-10 14:55:26 -0700830 Allocation *a = static_cast<Allocation *>(va);
831 a->read(data);
832}
833
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800834void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
Jason Sams5edc6082010-10-05 13:32:49 -0700835 Allocation *a = static_cast<Allocation *>(va);
836 a->resize1D(rsc, dimX);
837}
838
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800839void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
Jason Sams5edc6082010-10-05 13:32:49 -0700840 Allocation *a = static_cast<Allocation *>(va);
841 a->resize2D(rsc, dimX, dimY);
842}
843
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700844#endif //ANDROID_RS_BUILD_FOR_HOST
845
846}
847}
848
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800849const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700850 Allocation *a = static_cast<Allocation *>(va);
851 a->getType()->incUserRef();
852
853 return a->getType();
854}
855
Jason Sams5476b452010-12-08 16:14:36 -0800856RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
857 RsAllocationMipmapGenerationControl mips,
858 uint32_t usages) {
Jason Sams31a7e422010-10-26 13:09:17 -0700859 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800860 Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages);
Jason Sams31a7e422010-10-26 13:09:17 -0700861 alloc->incUserRef();
862 return alloc;
863}
864
Jason Sams5476b452010-12-08 16:14:36 -0800865
866RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
867 RsAllocationMipmapGenerationControl mips,
868 const void *data, uint32_t usages) {
Jason Sams31a7e422010-10-26 13:09:17 -0700869 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800870 Type *t = static_cast<Type *>(vtype);
Jason Sams31a7e422010-10-26 13:09:17 -0700871
Jason Sams5476b452010-12-08 16:14:36 -0800872 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages);
Jason Sams31a7e422010-10-26 13:09:17 -0700873 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
874 if (texAlloc == NULL) {
875 LOGE("Memory allocation failure");
876 return NULL;
877 }
878
Jason Sams5476b452010-12-08 16:14:36 -0800879 memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
880 if (mips == RS_MIPMAP_FULL) {
881 Adapter2D adapt(rsc, texAlloc);
882 Adapter2D adapt2(rsc, texAlloc);
883 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
884 adapt.setLOD(lod);
885 adapt2.setLOD(lod + 1);
886 mip(adapt2, adapt);
Jason Sams31a7e422010-10-26 13:09:17 -0700887 }
Jason Sams31a7e422010-10-26 13:09:17 -0700888 }
889
890 return texAlloc;
891}
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800892
Jason Sams5476b452010-12-08 16:14:36 -0800893RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
894 RsAllocationMipmapGenerationControl mips,
895 const void *data, uint32_t usages) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800896 Context *rsc = static_cast<Context *>(con);
Jason Sams5476b452010-12-08 16:14:36 -0800897 Type *t = static_cast<Type *>(vtype);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800898
899 // Cubemap allocation's faces should be Width by Width each.
900 // Source data should have 6 * Width by Width pixels
901 // Error checking is done in the java layer
Jason Sams5476b452010-12-08 16:14:36 -0800902 RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800903 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
904 if (texAlloc == NULL) {
905 LOGE("Memory allocation failure");
906 return NULL;
907 }
908
909 uint8_t *sourcePtr = (uint8_t*)data;
Jason Sams5476b452010-12-08 16:14:36 -0800910 for (uint32_t face = 0; face < 6; face ++) {
911 Adapter2D faceAdapter(rsc, texAlloc);
912 faceAdapter.setFace(face);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800913
Jason Sams5476b452010-12-08 16:14:36 -0800914 size_t cpySize = t->getDimX() * t->getDimX() * t->getElementSizeBytes();
915 memcpy(faceAdapter.getElement(0, 0), sourcePtr, cpySize);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800916
Jason Sams5476b452010-12-08 16:14:36 -0800917 // Move the data pointer to the next cube face
918 sourcePtr += cpySize;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800919
Jason Sams5476b452010-12-08 16:14:36 -0800920 if (mips == RS_MIPMAP_FULL) {
921 Adapter2D adapt(rsc, texAlloc);
922 Adapter2D adapt2(rsc, texAlloc);
923 adapt.setFace(face);
924 adapt2.setFace(face);
925 for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
926 adapt.setLOD(lod);
927 adapt2.setLOD(lod + 1);
928 mip(adapt2, adapt);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800929 }
930 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800931 }
932
933 return texAlloc;
934}