blob: 172d07d2291a92880048d149ac8cddf27493cce3 [file] [log] [blame]
Jason Sams326e0dd2009-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 Sakhartchoukfb6b6142010-05-21 12:53:13 -070016#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -070017#include "rsContext.h"
18
Jason Sams1aa5a4e2009-06-22 17:15:15 -070019#include <GLES/gl.h>
Jason Sams7fabe1a2010-02-23 17:44:28 -080020#include <GLES2/gl2.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070021#include <GLES/glext.h>
Alex Sakhartchoukfb6b6142010-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 Sams1aa5a4e2009-06-22 17:15:15 -070028
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -070029#include "utils/StopWatch.h"
30
Jason Sams326e0dd2009-05-22 14:03:28 -070031using namespace android;
32using namespace android::renderscript;
33
Jason Samse514b452009-09-25 14:51:22 -070034Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070035{
Jason Samsfa84da22010-03-01 15:31:04 -080036 init(rsc, type);
37
38 mPtr = malloc(mType->getSizeBytes());
Jason Sams10e5e572010-08-12 12:44:02 -070039 if (mType->getElement()->getHasReferences()) {
40 memset(mPtr, 0, mType->getSizeBytes());
41 }
Jason Samsfa84da22010-03-01 15:31:04 -080042 if (!mPtr) {
43 LOGE("Allocation::Allocation, alloc failure");
44 }
45}
46
47Allocation::Allocation(Context *rsc, const Type *type, void *bmp,
48 void *callbackData, RsBitmapCallback_t callback)
49: ObjectBase(rsc)
50{
51 init(rsc, type);
52
53 mPtr = bmp;
54 mUserBitmapCallback = callback;
55 mUserBitmapCallbackData = callbackData;
56}
57
58void Allocation::init(Context *rsc, const Type *type)
59{
Jason Samsf2649a92009-09-25 16:37:33 -070060 mAllocFile = __FILE__;
61 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070062 mPtr = NULL;
63
64 mCpuWrite = false;
65 mCpuRead = false;
66 mGpuWrite = false;
67 mGpuRead = false;
68
69 mReadWriteRatio = 0;
70 mUpdateSize = 0;
71
72 mIsTexture = false;
73 mTextureID = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070074 mIsVertexBuffer = false;
75 mBufferID = 0;
Jason Samscf4c7c92009-12-14 12:57:40 -080076 mUploadDefered = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070077
Jason Samsfa84da22010-03-01 15:31:04 -080078 mUserBitmapCallback = NULL;
79 mUserBitmapCallbackData = NULL;
80
Jason Sams326e0dd2009-05-22 14:03:28 -070081 mType.set(type);
Jason Samse5ffb872009-08-09 17:01:55 -070082 rsAssert(type);
Jason Samsfa84da22010-03-01 15:31:04 -080083
84 mPtr = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070085}
86
87Allocation::~Allocation()
88{
Jason Samsfa84da22010-03-01 15:31:04 -080089 if (mUserBitmapCallback != NULL) {
90 mUserBitmapCallback(mUserBitmapCallbackData);
91 } else {
92 free(mPtr);
93 }
Jason Samsbf3c14e2009-11-02 14:25:10 -080094 mPtr = NULL;
Jason Samse402ed32009-11-03 11:25:42 -080095
96 if (mBufferID) {
97 // Causes a SW crash....
98 //LOGV(" mBufferID %i", mBufferID);
99 //glDeleteBuffers(1, &mBufferID);
100 //mBufferID = 0;
101 }
102 if (mTextureID) {
103 glDeleteTextures(1, &mTextureID);
104 mTextureID = 0;
105 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700106}
107
108void Allocation::setCpuWritable(bool)
109{
110}
111
112void Allocation::setGpuWritable(bool)
113{
114}
115
116void Allocation::setCpuReadable(bool)
117{
118}
119
120void Allocation::setGpuReadable(bool)
121{
122}
123
124bool Allocation::fixAllocation()
125{
126 return false;
127}
128
Jason Sams7fabe1a2010-02-23 17:44:28 -0800129void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset)
Jason Samscf4c7c92009-12-14 12:57:40 -0800130{
131 rsAssert(lodOffset < mType->getLODCount());
132 mIsTexture = true;
133 mTextureLOD = lodOffset;
134 mUploadDefered = true;
Jason Sams7fabe1a2010-02-23 17:44:28 -0800135 mTextureGenMipmap = !mType->getDimLOD() && genMipmap;
Jason Samscf4c7c92009-12-14 12:57:40 -0800136}
137
138void Allocation::uploadToTexture(const Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700139{
140 //rsAssert(!mTextureId);
Jason Samscf4c7c92009-12-14 12:57:40 -0800141
142 mIsTexture = true;
143 if (!rsc->checkDriver()) {
144 mUploadDefered = true;
145 return;
146 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700147
Jason Samsd01d9702009-12-23 14:35:29 -0800148 GLenum type = mType->getElement()->getComponent().getGLType();
149 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Sams565ac362009-06-03 16:04:54 -0700150
151 if (!type || !format) {
152 return;
153 }
154
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700155 bool isFirstUpload = false;
156
Jason Sams326e0dd2009-05-22 14:03:28 -0700157 if (!mTextureID) {
158 glGenTextures(1, &mTextureID);
Jason Sams13e26342009-11-24 12:26:35 -0800159
160 if (!mTextureID) {
161 // This should not happen, however, its likely the cause of the
162 // white sqare bug.
163 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
164 LOGE("Upload to texture failed to gen mTextureID");
165 rsc->dumpDebug();
Jason Samscf4c7c92009-12-14 12:57:40 -0800166 mUploadDefered = true;
167 return;
Jason Sams13e26342009-11-24 12:26:35 -0800168 }
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700169 isFirstUpload = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700170 }
171 glBindTexture(GL_TEXTURE_2D, mTextureID);
Jason Sams5f0b4e12009-11-05 12:44:58 -0800172 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700173
Jason Samse514b452009-09-25 14:51:22 -0700174 Adapter2D adapt(getContext(), this);
Jason Samscf4c7c92009-12-14 12:57:40 -0800175 for(uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
176 adapt.setLOD(lod+mTextureLOD);
Jason Sams326e0dd2009-05-22 14:03:28 -0700177
178 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700179 if(isFirstUpload) {
180 glTexImage2D(GL_TEXTURE_2D, lod, format,
181 adapt.getDimX(), adapt.getDimY(),
182 0, format, type, ptr);
183 } else {
184 glTexSubImage2D(GL_TEXTURE_2D, lod, 0, 0,
185 adapt.getDimX(), adapt.getDimY(),
186 format, type, ptr);
187 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700188 }
Jason Sams7fabe1a2010-02-23 17:44:28 -0800189 if (mTextureGenMipmap) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700190#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams7fabe1a2010-02-23 17:44:28 -0800191 glGenerateMipmap(GL_TEXTURE_2D);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700192#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Sams7fabe1a2010-02-23 17:44:28 -0800193 }
194
Jason Samsc1ed5892010-03-10 17:30:41 -0800195 rsc->checkError("Allocation::uploadToTexture");
Jason Sams326e0dd2009-05-22 14:03:28 -0700196}
197
Jason Samscf4c7c92009-12-14 12:57:40 -0800198void Allocation::deferedUploadToBufferObject(const Context *rsc)
199{
200 mIsVertexBuffer = true;
201 mUploadDefered = true;
202}
203
204void Allocation::uploadToBufferObject(const Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700205{
206 rsAssert(!mType->getDimY());
207 rsAssert(!mType->getDimZ());
208
Jason Samscf4c7c92009-12-14 12:57:40 -0800209 mIsVertexBuffer = true;
210 if (!rsc->checkDriver()) {
211 mUploadDefered = true;
212 return;
213 }
214
Jason Sams326e0dd2009-05-22 14:03:28 -0700215 if (!mBufferID) {
216 glGenBuffers(1, &mBufferID);
217 }
Jason Samscf4c7c92009-12-14 12:57:40 -0800218 if (!mBufferID) {
219 LOGE("Upload to buffer object failed");
220 mUploadDefered = true;
221 return;
222 }
223
Jason Sams326e0dd2009-05-22 14:03:28 -0700224 glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
225 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
226 glBindBuffer(GL_ARRAY_BUFFER, 0);
Jason Samsc1ed5892010-03-10 17:30:41 -0800227 rsc->checkError("Allocation::uploadToBufferObject");
Jason Sams326e0dd2009-05-22 14:03:28 -0700228}
229
Jason Samscf4c7c92009-12-14 12:57:40 -0800230void Allocation::uploadCheck(const Context *rsc)
231{
232 if (mUploadDefered) {
233 mUploadDefered = false;
234 if (mIsVertexBuffer) {
235 uploadToBufferObject(rsc);
236 }
237 if (mIsTexture) {
238 uploadToTexture(rsc);
239 }
240 }
241}
242
Jason Samse5ffb872009-08-09 17:01:55 -0700243
Jason Sams5f0c84c2010-08-31 13:50:42 -0700244void Allocation::data(Context *rsc, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700245{
Jason Sams9397e302009-08-27 20:23:34 -0700246 uint32_t size = mType->getSizeBytes();
247 if (size != sizeBytes) {
248 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
249 return;
250 }
Jason Samse3929c92010-08-09 18:13:33 -0700251
252 if (mType->getElement()->getHasReferences()) {
253 incRefs(data, sizeBytes / mType->getElement()->getSizeBytes());
254 decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes());
255 }
256
Jason Sams9397e302009-08-27 20:23:34 -0700257 memcpy(mPtr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700258 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800259 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700260}
261
Jason Samse579df42009-08-10 14:55:26 -0700262void Allocation::read(void *data)
263{
264 memcpy(data, mPtr, mType->getSizeBytes());
265}
266
Jason Sams5f0c84c2010-08-31 13:50:42 -0700267void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700268{
269 uint32_t eSize = mType->getElementSizeBytes();
270 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
271 ptr += eSize * xoff;
Jason Sams9397e302009-08-27 20:23:34 -0700272 uint32_t size = count * eSize;
273
274 if (size != sizeBytes) {
275 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Samse12c1c52009-09-27 17:50:38 -0700276 mType->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700277 return;
278 }
Jason Samse3929c92010-08-09 18:13:33 -0700279
280 if (mType->getElement()->getHasReferences()) {
281 incRefs(data, count);
282 decRefs(ptr, count);
283 }
284
Jason Sams9397e302009-08-27 20:23:34 -0700285 memcpy(ptr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700286 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800287 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700288}
289
Jason Sams5f0c84c2010-08-31 13:50:42 -0700290void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff,
Jason Sams9397e302009-08-27 20:23:34 -0700291 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700292{
293 uint32_t eSize = mType->getElementSizeBytes();
294 uint32_t lineSize = eSize * w;
295 uint32_t destW = mType->getDimX();
296
297 const uint8_t *src = static_cast<const uint8_t *>(data);
298 uint8_t *dst = static_cast<uint8_t *>(mPtr);
299 dst += eSize * (xoff + yoff * destW);
Jason Sams9397e302009-08-27 20:23:34 -0700300
301 if ((lineSize * eSize * h) != sizeBytes) {
302 rsAssert(!"Allocation::subData called with mismatched size");
303 return;
304 }
305
Jason Sams326e0dd2009-05-22 14:03:28 -0700306 for (uint32_t line=yoff; line < (yoff+h); line++) {
Jason Samse3929c92010-08-09 18:13:33 -0700307 if (mType->getElement()->getHasReferences()) {
308 incRefs(src, w);
309 decRefs(dst, w);
310 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700311 memcpy(dst, src, lineSize);
312 src += lineSize;
313 dst += destW * eSize;
314 }
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700315 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800316 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700317}
318
Jason Sams5f0c84c2010-08-31 13:50:42 -0700319void Allocation::subData(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams9397e302009-08-27 20:23:34 -0700320 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700321{
322}
323
Jason Sams5f0c84c2010-08-31 13:50:42 -0700324void Allocation::subElementData(Context *rsc, uint32_t x, const void *data,
325 uint32_t cIdx, uint32_t sizeBytes)
326{
327 uint32_t eSize = mType->getElementSizeBytes();
328 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
329 ptr += eSize * x;
330
331 if (cIdx >= mType->getElement()->getFieldCount()) {
332 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
333 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
334 return;
335 }
336
337 if (x >= mType->getDimX()) {
338 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
339 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
340 return;
341 }
342
343 const Element * e = mType->getElement()->getField(cIdx);
344 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
345
346 if (sizeBytes != e->getSizeBytes()) {
347 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
348 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
349 return;
350 }
351
352 if (e->getHasReferences()) {
353 e->incRefs(data);
354 e->decRefs(ptr);
355 }
356
357 memcpy(ptr, data, sizeBytes);
358 sendDirty();
359 mUploadDefered = true;
360}
361
362void Allocation::subElementData(Context *rsc, uint32_t x, uint32_t y,
363 const void *data, uint32_t cIdx, uint32_t sizeBytes)
364{
365 uint32_t eSize = mType->getElementSizeBytes();
366 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
367 ptr += eSize * (x + y * mType->getDimX());
368
369 if (x >= mType->getDimX()) {
370 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
371 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
372 return;
373 }
374
375 if (y >= mType->getDimY()) {
376 LOGE("Error Allocation::subElementData X offset %i out of range.", x);
377 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
378 return;
379 }
380
381 if (cIdx >= mType->getElement()->getFieldCount()) {
382 LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
383 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
384 return;
385 }
386
387 const Element * e = mType->getElement()->getField(cIdx);
388 ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
389
390 if (sizeBytes != e->getSizeBytes()) {
391 LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
392 rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
393 return;
394 }
395
396 if (e->getHasReferences()) {
397 e->incRefs(data);
398 e->decRefs(ptr);
399 }
400
401 memcpy(ptr, data, sizeBytes);
402 sendDirty();
403 mUploadDefered = true;
404}
405
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700406void Allocation::addProgramToDirty(const Program *p)
407{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700408 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700409}
Jason Sams326e0dd2009-05-22 14:03:28 -0700410
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700411void Allocation::removeProgramToDirty(const Program *p)
412{
413 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
414 if (mToDirtyList[ct] == p) {
415 mToDirtyList.removeAt(ct);
416 return;
417 }
418 }
419 rsAssert(0);
420}
421
Jason Samsc21cf402009-11-17 17:26:46 -0800422void Allocation::dumpLOGV(const char *prefix) const
423{
424 ObjectBase::dumpLOGV(prefix);
425
426 String8 s(prefix);
427 s.append(" type ");
428 if (mType.get()) {
429 mType->dumpLOGV(s.string());
430 }
431
432 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
433 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
434
Jason Sams43999e72009-11-23 15:27:33 -0800435 LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i",
Jason Samsc21cf402009-11-17 17:26:46 -0800436 prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID);
437
Jason Samsc21cf402009-11-17 17:26:46 -0800438}
Jason Sams326e0dd2009-05-22 14:03:28 -0700439
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700440void Allocation::serialize(OStream *stream) const
441{
442 // Need to identify ourselves
443 stream->addU32((uint32_t)getClassId());
444
445 String8 name(getName());
446 stream->addString(&name);
447
448 // First thing we need to serialize is the type object since it will be needed
449 // to initialize the class
450 mType->serialize(stream);
451
452 uint32_t dataSize = mType->getSizeBytes();
453 // Write how much data we are storing
454 stream->addU32(dataSize);
455 // Now write the data
456 stream->addByteArray(mPtr, dataSize);
457}
458
459Allocation *Allocation::createFromStream(Context *rsc, IStream *stream)
460{
461 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700462 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
463 if(classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700464 LOGE("allocation loading skipped due to invalid class id\n");
465 return NULL;
466 }
467
468 String8 name;
469 stream->loadString(&name);
470
471 Type *type = Type::createFromStream(rsc, stream);
472 if(!type) {
473 return NULL;
474 }
475 type->compute();
476
477 // Number of bytes we wrote out for this allocation
478 uint32_t dataSize = stream->loadU32();
479 if(dataSize != type->getSizeBytes()) {
480 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
481 delete type;
482 return NULL;
483 }
484
485 Allocation *alloc = new Allocation(rsc, type);
486 alloc->setName(name.string(), name.size());
487
488 // Read in all of our allocation data
Jason Sams5f0c84c2010-08-31 13:50:42 -0700489 alloc->data(rsc, stream->getPtr() + stream->getPos(), dataSize);
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700490 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700491
492 return alloc;
493}
494
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700495void Allocation::sendDirty() const
496{
497 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
498 mToDirtyList[ct]->forceDirty();
499 }
500}
Jason Sams326e0dd2009-05-22 14:03:28 -0700501
Jason Sams96abf812010-10-05 13:32:49 -0700502void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const
Jason Samse3929c92010-08-09 18:13:33 -0700503{
504 const uint8_t *p = static_cast<const uint8_t *>(ptr);
505 const Element *e = mType->getElement();
506 uint32_t stride = e->getSizeBytes();
507
Jason Sams96abf812010-10-05 13:32:49 -0700508 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700509 while (ct > 0) {
510 e->incRefs(p);
511 ct --;
512 p += stride;
513 }
514}
515
Jason Sams96abf812010-10-05 13:32:49 -0700516void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const
Jason Samse3929c92010-08-09 18:13:33 -0700517{
518 const uint8_t *p = static_cast<const uint8_t *>(ptr);
519 const Element *e = mType->getElement();
520 uint32_t stride = e->getSizeBytes();
521
Jason Sams96abf812010-10-05 13:32:49 -0700522 p += stride * startOff;
Jason Samse3929c92010-08-09 18:13:33 -0700523 while (ct > 0) {
524 e->decRefs(p);
525 ct --;
526 p += stride;
527 }
528}
529
Jason Sams96abf812010-10-05 13:32:49 -0700530void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len)
531{
532}
533
534void Allocation::resize1D(Context *rsc, uint32_t dimX)
535{
536 Type *t = mType->cloneAndResize1D(rsc, dimX);
537
538 uint32_t oldDimX = mType->getDimX();
539 if (dimX == oldDimX) {
540 return;
541 }
542
543 if (dimX < oldDimX) {
544 decRefs(mPtr, oldDimX - dimX, dimX);
545 }
546 mPtr = realloc(mPtr, t->getSizeBytes());
547
548 if (dimX > oldDimX) {
549 const Element *e = mType->getElement();
550 uint32_t stride = e->getSizeBytes();
551 memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
552 }
553 mType.set(t);
554}
555
556void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY)
557{
558 LOGE("not implemented");
559}
560
Jason Sams326e0dd2009-05-22 14:03:28 -0700561/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700562//
Jason Sams326e0dd2009-05-22 14:03:28 -0700563
564
565namespace android {
566namespace renderscript {
567
568RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
569{
570 const Type * type = static_cast<const Type *>(vtype);
571
Jason Samse514b452009-09-25 14:51:22 -0700572 Allocation * alloc = new Allocation(rsc, type);
Jason Sams9397e302009-08-27 20:23:34 -0700573 alloc->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700574 return alloc;
575}
576
Jason Sams326e0dd2009-05-22 14:03:28 -0700577RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
578{
Jason Samse514b452009-09-25 14:51:22 -0700579 Type * type = new Type(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700580 type->setDimX(count);
581 type->setElement(static_cast<Element *>(e));
582 type->compute();
583 return rsi_AllocationCreateTyped(rsc, type);
584}
585
Jason Sams7fabe1a2010-02-23 17:44:28 -0800586void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel)
Jason Sams326e0dd2009-05-22 14:03:28 -0700587{
588 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams7fabe1a2010-02-23 17:44:28 -0800589 alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel);
Jason Sams326e0dd2009-05-22 14:03:28 -0700590}
591
592void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
593{
594 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samscf4c7c92009-12-14 12:57:40 -0800595 alloc->deferedUploadToBufferObject(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700596}
597
Jason Sams565ac362009-06-03 16:04:54 -0700598static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Sams326e0dd2009-05-22 14:03:28 -0700599{
600 uint32_t w = out.getDimX();
601 uint32_t h = out.getDimY();
602
Jason Samse9f5c532009-07-28 17:20:11 -0700603 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700604 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
605 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
606 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
607
Jason Samse9f5c532009-07-28 17:20:11 -0700608 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700609 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
610 oPtr ++;
611 i1 += 2;
612 i2 += 2;
613 }
614 }
615}
616
617static void mip8888(const Adapter2D &out, const Adapter2D &in)
618{
619 uint32_t w = out.getDimX();
620 uint32_t h = out.getDimY();
621
Jason Samse9f5c532009-07-28 17:20:11 -0700622 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700623 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
624 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
625 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
626
Jason Samse9f5c532009-07-28 17:20:11 -0700627 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700628 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700629 oPtr ++;
630 i1 += 2;
631 i2 += 2;
632 }
633 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700634}
635
Jason Sams2f6d8612010-01-19 17:53:54 -0800636static void mip8(const Adapter2D &out, const Adapter2D &in)
637{
638 uint32_t w = out.getDimX();
639 uint32_t h = out.getDimY();
640
641 for (uint32_t y=0; y < h; y++) {
642 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
643 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
644 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
645
646 for (uint32_t x=0; x < w; x++) {
647 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
648 oPtr ++;
649 i1 += 2;
650 i2 += 2;
651 }
652 }
653}
654
Jason Samse9f5c532009-07-28 17:20:11 -0700655static void mip(const Adapter2D &out, const Adapter2D &in)
656{
657 switch(out.getBaseType()->getElement()->getSizeBits()) {
658 case 32:
659 mip8888(out, in);
660 break;
661 case 16:
662 mip565(out, in);
663 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800664 case 8:
665 mip8(out, in);
666 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700667
668 }
669
670}
Jason Sams326e0dd2009-05-22 14:03:28 -0700671
Jason Sams6678e9b2009-05-27 14:45:32 -0700672typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
673
674static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
675{
676 memcpy(dst, src, count * 2);
677}
678static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
679{
680 memcpy(dst, src, count);
681}
682static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
683{
684 memcpy(dst, src, count * 4);
685}
686
687
688static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
689{
690 uint16_t *d = static_cast<uint16_t *>(dst);
691 const uint8_t *s = static_cast<const uint8_t *>(src);
692
693 while(count--) {
694 *d = rs888to565(s[0], s[1], s[2]);
695 d++;
696 s+= 3;
697 }
698}
699
700static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
701{
702 uint16_t *d = static_cast<uint16_t *>(dst);
703 const uint8_t *s = static_cast<const uint8_t *>(src);
704
705 while(count--) {
706 *d = rs888to565(s[0], s[1], s[2]);
707 d++;
708 s+= 4;
709 }
710}
711
Jason Samsa57c0a72009-09-04 14:42:41 -0700712static ElementConverter_t pickConverter(const Element *dst, const Element *src)
Jason Sams6678e9b2009-05-27 14:45:32 -0700713{
Jason Samsd01d9702009-12-23 14:35:29 -0800714 GLenum srcGLType = src->getComponent().getGLType();
715 GLenum srcGLFmt = src->getComponent().getGLFormat();
716 GLenum dstGLType = dst->getComponent().getGLType();
717 GLenum dstGLFmt = dst->getComponent().getGLFormat();
Jason Samsa57c0a72009-09-04 14:42:41 -0700718
719 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
720 switch(dst->getSizeBytes()) {
721 case 4:
722 return elementConverter_cpy_32;
723 case 2:
724 return elementConverter_cpy_16;
725 case 1:
726 return elementConverter_cpy_8;
727 }
Jason Sams6678e9b2009-05-27 14:45:32 -0700728 }
729
Jason Samsa57c0a72009-09-04 14:42:41 -0700730 if (srcGLType == GL_UNSIGNED_BYTE &&
731 srcGLFmt == GL_RGB &&
732 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
Jason Sams44b28942010-06-22 17:45:34 -0700733 dstGLFmt == GL_RGB) {
Jason Samsa57c0a72009-09-04 14:42:41 -0700734
Jason Sams6678e9b2009-05-27 14:45:32 -0700735 return elementConverter_888_to_565;
736 }
737
Jason Samsa57c0a72009-09-04 14:42:41 -0700738 if (srcGLType == GL_UNSIGNED_BYTE &&
739 srcGLFmt == GL_RGBA &&
740 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
Jason Sams44b28942010-06-22 17:45:34 -0700741 dstGLFmt == GL_RGB) {
Jason Samsa57c0a72009-09-04 14:42:41 -0700742
Jason Sams6678e9b2009-05-27 14:45:32 -0700743 return elementConverter_8888_to_565;
744 }
745
Jason Samsa57c0a72009-09-04 14:42:41 -0700746 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst);
Jason Sams44b28942010-06-22 17:45:34 -0700747 LOGE("pickConverter, srcGLType = %x, srcGLFmt = %x", srcGLType, srcGLFmt);
748 LOGE("pickConverter, dstGLType = %x, dstGLFmt = %x", dstGLType, dstGLFmt);
749 src->dumpLOGV("SRC ");
750 dst->dumpLOGV("DST ");
Jason Sams6678e9b2009-05-27 14:45:32 -0700751 return 0;
752}
753
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700754#ifndef ANDROID_RS_BUILD_FOR_HOST
755
Jason Samsfa84da22010-03-01 15:31:04 -0800756RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype,
757 void *bmp, void *callbackData, RsBitmapCallback_t callback)
758{
759 const Type * type = static_cast<const Type *>(vtype);
760 Allocation * alloc = new Allocation(rsc, type, bmp, callbackData, callback);
761 alloc->incUserRef();
762 return alloc;
763}
Jason Sams6678e9b2009-05-27 14:45:32 -0700764
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700765void rsi_AllocationUpdateFromBitmap(Context *rsc, RsAllocation va, RsElement _src, const void *data)
766{
767 Allocation *texAlloc = static_cast<Allocation *>(va);
768 const Element *src = static_cast<const Element *>(_src);
769 const Element *dst = texAlloc->getType()->getElement();
770 uint32_t w = texAlloc->getType()->getDimX();
771 uint32_t h = texAlloc->getType()->getDimY();
772 bool genMips = texAlloc->getType()->getDimLOD();
773
774 ElementConverter_t cvt = pickConverter(dst, src);
775 if (cvt) {
776 cvt(texAlloc->getPtr(), data, w * h);
777 if (genMips) {
778 Adapter2D adapt(rsc, texAlloc);
779 Adapter2D adapt2(rsc, texAlloc);
780 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
781 adapt.setLOD(lod);
782 adapt2.setLOD(lod + 1);
783 mip(adapt2, adapt);
784 }
785 }
786 } else {
787 rsc->setError(RS_ERROR_BAD_VALUE, "Unsupported bitmap format");
788 }
789}
790
Jason Samsa57c0a72009-09-04 14:42:41 -0700791RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data)
Jason Sams6678e9b2009-05-27 14:45:32 -0700792{
Jason Samsa57c0a72009-09-04 14:42:41 -0700793 const Element *src = static_cast<const Element *>(_src);
794 const Element *dst = static_cast<const Element *>(_dst);
Jason Samsfdcf7db2010-01-06 15:10:29 -0800795
Jason Sams900f1612010-09-16 18:18:29 -0700796 //LOGE("%p rsi_AllocationCreateFromBitmap %i %i %i", rsc, w, h, genMips);
Jason Samsa57c0a72009-09-04 14:42:41 -0700797 rsi_TypeBegin(rsc, _dst);
Jason Sams6678e9b2009-05-27 14:45:32 -0700798 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
799 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
800 if (genMips) {
801 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
802 }
803 RsType type = rsi_TypeCreate(rsc);
804
805 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
806 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
807 if (texAlloc == NULL) {
808 LOGE("Memory allocation failure");
809 return NULL;
810 }
Jason Sams6678e9b2009-05-27 14:45:32 -0700811
Jason Samsa57c0a72009-09-04 14:42:41 -0700812 ElementConverter_t cvt = pickConverter(dst, src);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700813 if (cvt) {
814 cvt(texAlloc->getPtr(), data, w * h);
815 if (genMips) {
816 Adapter2D adapt(rsc, texAlloc);
817 Adapter2D adapt2(rsc, texAlloc);
818 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
819 adapt.setLOD(lod);
820 adapt2.setLOD(lod + 1);
821 mip(adapt2, adapt);
822 }
Jason Sams6678e9b2009-05-27 14:45:32 -0700823 }
Jason Sams5f0c84c2010-08-31 13:50:42 -0700824 } else {
825 rsc->setError(RS_ERROR_BAD_VALUE, "Unsupported bitmap format");
Jason Sams6678e9b2009-05-27 14:45:32 -0700826 }
827
828 return texAlloc;
829}
830
Jason Samsa57c0a72009-09-04 14:42:41 -0700831RsAllocation rsi_AllocationCreateFromBitmapBoxed(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data)
Jason Samsc9d43db2009-07-28 12:02:16 -0700832{
Jason Samsa57c0a72009-09-04 14:42:41 -0700833 const Element *srcE = static_cast<const Element *>(_src);
834 const Element *dstE = static_cast<const Element *>(_dst);
Jason Samsc9d43db2009-07-28 12:02:16 -0700835 uint32_t w2 = rsHigherPow2(w);
836 uint32_t h2 = rsHigherPow2(h);
837
838 if ((w2 == w) && (h2 == h)) {
Jason Samsa57c0a72009-09-04 14:42:41 -0700839 return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data);
Jason Samsc9d43db2009-07-28 12:02:16 -0700840 }
841
Jason Samsa57c0a72009-09-04 14:42:41 -0700842 uint32_t bpp = srcE->getSizeBytes();
Jason Samsc9d43db2009-07-28 12:02:16 -0700843 size_t size = w2 * h2 * bpp;
844 uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
845 memset(tmp, 0, size);
846
847 const uint8_t * src = static_cast<const uint8_t *>(data);
848 for (uint32_t y = 0; y < h; y++) {
Jason Sams50253db2009-07-29 20:55:44 -0700849 uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
Marco Nelissen6cd833d2009-10-28 15:10:56 -0700850 memcpy(&ydst[((w2 - w) >> 1) * bpp], src, w * bpp);
Jason Sams50253db2009-07-29 20:55:44 -0700851 src += w * bpp;
Jason Samsc9d43db2009-07-28 12:02:16 -0700852 }
853
Jason Samsa57c0a72009-09-04 14:42:41 -0700854 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp);
Jason Samsc9d43db2009-07-28 12:02:16 -0700855 free(tmp);
856 return ret;
Jason Samsc9d43db2009-07-28 12:02:16 -0700857}
858
Jason Sams9397e302009-08-27 20:23:34 -0700859void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700860{
861 Allocation *a = static_cast<Allocation *>(va);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700862 a->data(rsc, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700863}
864
Jason Sams9397e302009-08-27 20:23:34 -0700865void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700866{
867 Allocation *a = static_cast<Allocation *>(va);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700868 a->subData(rsc, xoff, count, data, sizeBytes);
869}
870
871void rsi_Allocation2DSubElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, const void *data, uint32_t eoff, uint32_t sizeBytes)
872{
873 Allocation *a = static_cast<Allocation *>(va);
874 a->subElementData(rsc, x, y, data, eoff, sizeBytes);
875}
876
877void rsi_Allocation1DSubElementData(Context *rsc, RsAllocation va, uint32_t x, const void *data, uint32_t eoff, uint32_t sizeBytes)
878{
879 Allocation *a = static_cast<Allocation *>(va);
880 a->subElementData(rsc, x, data, eoff, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700881}
882
Jason Sams9397e302009-08-27 20:23:34 -0700883void 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 Sams326e0dd2009-05-22 14:03:28 -0700884{
885 Allocation *a = static_cast<Allocation *>(va);
Jason Sams5f0c84c2010-08-31 13:50:42 -0700886 a->subData(rsc, xoff, yoff, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700887}
888
Jason Samse579df42009-08-10 14:55:26 -0700889void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data)
890{
891 Allocation *a = static_cast<Allocation *>(va);
892 a->read(data);
893}
894
Jason Sams96abf812010-10-05 13:32:49 -0700895void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX)
896{
897 Allocation *a = static_cast<Allocation *>(va);
898 a->resize1D(rsc, dimX);
899}
900
901void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY)
902{
903 Allocation *a = static_cast<Allocation *>(va);
904 a->resize2D(rsc, dimX, dimY);
905}
906
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700907const void* rsi_AllocationGetType(Context *rsc, RsAllocation va)
908{
909 Allocation *a = static_cast<Allocation *>(va);
910 a->getType()->incUserRef();
911
912 return a->getType();
913}
914
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700915#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -0700916
917}
918}