blob: 7e44fea058a0f683773b9e99abe04d3dbca64c3a [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
Jason Sams326e0dd2009-05-22 14:03:28 -070029using namespace android;
30using namespace android::renderscript;
31
Jason Samse514b452009-09-25 14:51:22 -070032Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070033{
Jason Samsfa84da22010-03-01 15:31:04 -080034 init(rsc, type);
35
36 mPtr = malloc(mType->getSizeBytes());
Jason Sams10e5e572010-08-12 12:44:02 -070037 if (mType->getElement()->getHasReferences()) {
38 memset(mPtr, 0, mType->getSizeBytes());
39 }
Jason Samsfa84da22010-03-01 15:31:04 -080040 if (!mPtr) {
41 LOGE("Allocation::Allocation, alloc failure");
42 }
43}
44
45Allocation::Allocation(Context *rsc, const Type *type, void *bmp,
46 void *callbackData, RsBitmapCallback_t callback)
47: ObjectBase(rsc)
48{
49 init(rsc, type);
50
51 mPtr = bmp;
52 mUserBitmapCallback = callback;
53 mUserBitmapCallbackData = callbackData;
54}
55
56void Allocation::init(Context *rsc, const Type *type)
57{
Jason Samsf2649a92009-09-25 16:37:33 -070058 mAllocFile = __FILE__;
59 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070060 mPtr = NULL;
61
62 mCpuWrite = false;
63 mCpuRead = false;
64 mGpuWrite = false;
65 mGpuRead = false;
66
67 mReadWriteRatio = 0;
68 mUpdateSize = 0;
69
70 mIsTexture = false;
71 mTextureID = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070072 mIsVertexBuffer = false;
73 mBufferID = 0;
Jason Samscf4c7c92009-12-14 12:57:40 -080074 mUploadDefered = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070075
Jason Samsfa84da22010-03-01 15:31:04 -080076 mUserBitmapCallback = NULL;
77 mUserBitmapCallbackData = NULL;
78
Jason Sams326e0dd2009-05-22 14:03:28 -070079 mType.set(type);
Jason Samse5ffb872009-08-09 17:01:55 -070080 rsAssert(type);
Jason Samsfa84da22010-03-01 15:31:04 -080081
82 mPtr = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070083}
84
85Allocation::~Allocation()
86{
Jason Samsfa84da22010-03-01 15:31:04 -080087 if (mUserBitmapCallback != NULL) {
88 mUserBitmapCallback(mUserBitmapCallbackData);
89 } else {
90 free(mPtr);
91 }
Jason Samsbf3c14e2009-11-02 14:25:10 -080092 mPtr = NULL;
Jason Samse402ed32009-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 Sams326e0dd2009-05-22 14:03:28 -0700104}
105
106void Allocation::setCpuWritable(bool)
107{
108}
109
110void Allocation::setGpuWritable(bool)
111{
112}
113
114void Allocation::setCpuReadable(bool)
115{
116}
117
118void Allocation::setGpuReadable(bool)
119{
120}
121
122bool Allocation::fixAllocation()
123{
124 return false;
125}
126
Jason Sams7fabe1a2010-02-23 17:44:28 -0800127void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset)
Jason Samscf4c7c92009-12-14 12:57:40 -0800128{
129 rsAssert(lodOffset < mType->getLODCount());
130 mIsTexture = true;
131 mTextureLOD = lodOffset;
132 mUploadDefered = true;
Jason Sams7fabe1a2010-02-23 17:44:28 -0800133 mTextureGenMipmap = !mType->getDimLOD() && genMipmap;
Jason Samscf4c7c92009-12-14 12:57:40 -0800134}
135
136void Allocation::uploadToTexture(const Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700137{
138 //rsAssert(!mTextureId);
Jason Samscf4c7c92009-12-14 12:57:40 -0800139
140 mIsTexture = true;
141 if (!rsc->checkDriver()) {
142 mUploadDefered = true;
143 return;
144 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700145
Jason Samsd01d9702009-12-23 14:35:29 -0800146 GLenum type = mType->getElement()->getComponent().getGLType();
147 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Sams565ac362009-06-03 16:04:54 -0700148
149 if (!type || !format) {
150 return;
151 }
152
Jason Sams326e0dd2009-05-22 14:03:28 -0700153 if (!mTextureID) {
154 glGenTextures(1, &mTextureID);
Jason Sams13e26342009-11-24 12:26:35 -0800155
156 if (!mTextureID) {
157 // This should not happen, however, its likely the cause of the
158 // white sqare bug.
159 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
160 LOGE("Upload to texture failed to gen mTextureID");
161 rsc->dumpDebug();
Jason Samscf4c7c92009-12-14 12:57:40 -0800162 mUploadDefered = true;
163 return;
Jason Sams13e26342009-11-24 12:26:35 -0800164 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700165 }
166 glBindTexture(GL_TEXTURE_2D, mTextureID);
Jason Sams5f0b4e12009-11-05 12:44:58 -0800167 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700168
Jason Samse514b452009-09-25 14:51:22 -0700169 Adapter2D adapt(getContext(), this);
Jason Samscf4c7c92009-12-14 12:57:40 -0800170 for(uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
171 adapt.setLOD(lod+mTextureLOD);
Jason Sams326e0dd2009-05-22 14:03:28 -0700172
173 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Jason Sams565ac362009-06-03 16:04:54 -0700174 glTexImage2D(GL_TEXTURE_2D, lod, format,
175 adapt.getDimX(), adapt.getDimY(),
176 0, format, type, ptr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700177 }
Jason Sams7fabe1a2010-02-23 17:44:28 -0800178 if (mTextureGenMipmap) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700179#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams7fabe1a2010-02-23 17:44:28 -0800180 glGenerateMipmap(GL_TEXTURE_2D);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700181#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Sams7fabe1a2010-02-23 17:44:28 -0800182 }
183
Jason Samsc1ed5892010-03-10 17:30:41 -0800184 rsc->checkError("Allocation::uploadToTexture");
Jason Sams326e0dd2009-05-22 14:03:28 -0700185}
186
Jason Samscf4c7c92009-12-14 12:57:40 -0800187void Allocation::deferedUploadToBufferObject(const Context *rsc)
188{
189 mIsVertexBuffer = true;
190 mUploadDefered = true;
191}
192
193void Allocation::uploadToBufferObject(const Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700194{
195 rsAssert(!mType->getDimY());
196 rsAssert(!mType->getDimZ());
197
Jason Samscf4c7c92009-12-14 12:57:40 -0800198 mIsVertexBuffer = true;
199 if (!rsc->checkDriver()) {
200 mUploadDefered = true;
201 return;
202 }
203
Jason Sams326e0dd2009-05-22 14:03:28 -0700204 if (!mBufferID) {
205 glGenBuffers(1, &mBufferID);
206 }
Jason Samscf4c7c92009-12-14 12:57:40 -0800207 if (!mBufferID) {
208 LOGE("Upload to buffer object failed");
209 mUploadDefered = true;
210 return;
211 }
212
Jason Sams326e0dd2009-05-22 14:03:28 -0700213 glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
214 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
215 glBindBuffer(GL_ARRAY_BUFFER, 0);
Jason Samsc1ed5892010-03-10 17:30:41 -0800216 rsc->checkError("Allocation::uploadToBufferObject");
Jason Sams326e0dd2009-05-22 14:03:28 -0700217}
218
Jason Samscf4c7c92009-12-14 12:57:40 -0800219void Allocation::uploadCheck(const Context *rsc)
220{
221 if (mUploadDefered) {
222 mUploadDefered = false;
223 if (mIsVertexBuffer) {
224 uploadToBufferObject(rsc);
225 }
226 if (mIsTexture) {
227 uploadToTexture(rsc);
228 }
229 }
230}
231
Jason Samse5ffb872009-08-09 17:01:55 -0700232
Jason Sams9397e302009-08-27 20:23:34 -0700233void Allocation::data(const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700234{
Jason Sams9397e302009-08-27 20:23:34 -0700235 uint32_t size = mType->getSizeBytes();
236 if (size != sizeBytes) {
237 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
238 return;
239 }
Jason Samse3929c92010-08-09 18:13:33 -0700240
241 if (mType->getElement()->getHasReferences()) {
242 incRefs(data, sizeBytes / mType->getElement()->getSizeBytes());
243 decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes());
244 }
245
Jason Sams9397e302009-08-27 20:23:34 -0700246 memcpy(mPtr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700247 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800248 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700249}
250
Jason Samse579df42009-08-10 14:55:26 -0700251void Allocation::read(void *data)
252{
253 memcpy(data, mPtr, mType->getSizeBytes());
254}
255
Jason Sams9397e302009-08-27 20:23:34 -0700256void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700257{
258 uint32_t eSize = mType->getElementSizeBytes();
259 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
260 ptr += eSize * xoff;
Jason Sams9397e302009-08-27 20:23:34 -0700261 uint32_t size = count * eSize;
262
263 if (size != sizeBytes) {
264 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Samse12c1c52009-09-27 17:50:38 -0700265 mType->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700266 return;
267 }
Jason Samse3929c92010-08-09 18:13:33 -0700268
269 if (mType->getElement()->getHasReferences()) {
270 incRefs(data, count);
271 decRefs(ptr, count);
272 }
273
Jason Sams9397e302009-08-27 20:23:34 -0700274 memcpy(ptr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700275 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800276 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700277}
278
Jason Sams565ac362009-06-03 16:04:54 -0700279void Allocation::subData(uint32_t xoff, uint32_t yoff,
Jason Sams9397e302009-08-27 20:23:34 -0700280 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700281{
282 uint32_t eSize = mType->getElementSizeBytes();
283 uint32_t lineSize = eSize * w;
284 uint32_t destW = mType->getDimX();
285
286 const uint8_t *src = static_cast<const uint8_t *>(data);
287 uint8_t *dst = static_cast<uint8_t *>(mPtr);
288 dst += eSize * (xoff + yoff * destW);
Jason Sams9397e302009-08-27 20:23:34 -0700289
290 if ((lineSize * eSize * h) != sizeBytes) {
291 rsAssert(!"Allocation::subData called with mismatched size");
292 return;
293 }
294
Jason Sams326e0dd2009-05-22 14:03:28 -0700295 for (uint32_t line=yoff; line < (yoff+h); line++) {
296 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
Jason Samse3929c92010-08-09 18:13:33 -0700297 if (mType->getElement()->getHasReferences()) {
298 incRefs(src, w);
299 decRefs(dst, w);
300 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700301 memcpy(dst, src, lineSize);
302 src += lineSize;
303 dst += destW * eSize;
304 }
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700305 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800306 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700307}
308
309void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams9397e302009-08-27 20:23:34 -0700310 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700311{
312}
313
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700314void Allocation::addProgramToDirty(const Program *p)
315{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700316 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700317}
Jason Sams326e0dd2009-05-22 14:03:28 -0700318
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700319void Allocation::removeProgramToDirty(const Program *p)
320{
321 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
322 if (mToDirtyList[ct] == p) {
323 mToDirtyList.removeAt(ct);
324 return;
325 }
326 }
327 rsAssert(0);
328}
329
Jason Samsc21cf402009-11-17 17:26:46 -0800330void Allocation::dumpLOGV(const char *prefix) const
331{
332 ObjectBase::dumpLOGV(prefix);
333
334 String8 s(prefix);
335 s.append(" type ");
336 if (mType.get()) {
337 mType->dumpLOGV(s.string());
338 }
339
340 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
341 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
342
Jason Sams43999e72009-11-23 15:27:33 -0800343 LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i",
Jason Samsc21cf402009-11-17 17:26:46 -0800344 prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID);
345
Jason Samsc21cf402009-11-17 17:26:46 -0800346}
Jason Sams326e0dd2009-05-22 14:03:28 -0700347
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700348void Allocation::serialize(OStream *stream) const
349{
350 // Need to identify ourselves
351 stream->addU32((uint32_t)getClassId());
352
353 String8 name(getName());
354 stream->addString(&name);
355
356 // First thing we need to serialize is the type object since it will be needed
357 // to initialize the class
358 mType->serialize(stream);
359
360 uint32_t dataSize = mType->getSizeBytes();
361 // Write how much data we are storing
362 stream->addU32(dataSize);
363 // Now write the data
364 stream->addByteArray(mPtr, dataSize);
365}
366
367Allocation *Allocation::createFromStream(Context *rsc, IStream *stream)
368{
369 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700370 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
371 if(classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700372 LOGE("allocation loading skipped due to invalid class id\n");
373 return NULL;
374 }
375
376 String8 name;
377 stream->loadString(&name);
378
379 Type *type = Type::createFromStream(rsc, stream);
380 if(!type) {
381 return NULL;
382 }
383 type->compute();
384
385 // Number of bytes we wrote out for this allocation
386 uint32_t dataSize = stream->loadU32();
387 if(dataSize != type->getSizeBytes()) {
388 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
389 delete type;
390 return NULL;
391 }
392
393 Allocation *alloc = new Allocation(rsc, type);
394 alloc->setName(name.string(), name.size());
395
396 // Read in all of our allocation data
Alex Sakhartchouke6d9fbc2010-08-11 10:30:44 -0700397 alloc->data(stream->getPtr() + stream->getPos(), dataSize);
398 stream->reset(stream->getPos() + dataSize);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700399
400 return alloc;
401}
402
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700403void Allocation::sendDirty() const
404{
405 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
406 mToDirtyList[ct]->forceDirty();
407 }
408}
Jason Sams326e0dd2009-05-22 14:03:28 -0700409
Jason Samse3929c92010-08-09 18:13:33 -0700410void Allocation::incRefs(const void *ptr, size_t ct) const
411{
412 const uint8_t *p = static_cast<const uint8_t *>(ptr);
413 const Element *e = mType->getElement();
414 uint32_t stride = e->getSizeBytes();
415
416 while (ct > 0) {
417 e->incRefs(p);
418 ct --;
419 p += stride;
420 }
421}
422
423void Allocation::decRefs(const void *ptr, size_t ct) const
424{
425 const uint8_t *p = static_cast<const uint8_t *>(ptr);
426 const Element *e = mType->getElement();
427 uint32_t stride = e->getSizeBytes();
428
429 while (ct > 0) {
430 e->decRefs(p);
431 ct --;
432 p += stride;
433 }
434}
435
Jason Sams326e0dd2009-05-22 14:03:28 -0700436/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700437//
Jason Sams326e0dd2009-05-22 14:03:28 -0700438
439
440namespace android {
441namespace renderscript {
442
443RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
444{
445 const Type * type = static_cast<const Type *>(vtype);
446
Jason Samse514b452009-09-25 14:51:22 -0700447 Allocation * alloc = new Allocation(rsc, type);
Jason Sams9397e302009-08-27 20:23:34 -0700448 alloc->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700449 return alloc;
450}
451
Jason Sams326e0dd2009-05-22 14:03:28 -0700452RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
453{
Jason Samse514b452009-09-25 14:51:22 -0700454 Type * type = new Type(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700455 type->setDimX(count);
456 type->setElement(static_cast<Element *>(e));
457 type->compute();
458 return rsi_AllocationCreateTyped(rsc, type);
459}
460
Jason Sams7fabe1a2010-02-23 17:44:28 -0800461void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel)
Jason Sams326e0dd2009-05-22 14:03:28 -0700462{
463 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams7fabe1a2010-02-23 17:44:28 -0800464 alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel);
Jason Sams326e0dd2009-05-22 14:03:28 -0700465}
466
467void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
468{
469 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samscf4c7c92009-12-14 12:57:40 -0800470 alloc->deferedUploadToBufferObject(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700471}
472
Jason Sams565ac362009-06-03 16:04:54 -0700473static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Sams326e0dd2009-05-22 14:03:28 -0700474{
475 uint32_t w = out.getDimX();
476 uint32_t h = out.getDimY();
477
Jason Samse9f5c532009-07-28 17:20:11 -0700478 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700479 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
480 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
481 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
482
Jason Samse9f5c532009-07-28 17:20:11 -0700483 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700484 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
485 oPtr ++;
486 i1 += 2;
487 i2 += 2;
488 }
489 }
490}
491
492static void mip8888(const Adapter2D &out, const Adapter2D &in)
493{
494 uint32_t w = out.getDimX();
495 uint32_t h = out.getDimY();
496
Jason Samse9f5c532009-07-28 17:20:11 -0700497 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700498 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
499 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
500 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
501
Jason Samse9f5c532009-07-28 17:20:11 -0700502 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700503 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700504 oPtr ++;
505 i1 += 2;
506 i2 += 2;
507 }
508 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700509}
510
Jason Sams2f6d8612010-01-19 17:53:54 -0800511static void mip8(const Adapter2D &out, const Adapter2D &in)
512{
513 uint32_t w = out.getDimX();
514 uint32_t h = out.getDimY();
515
516 for (uint32_t y=0; y < h; y++) {
517 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
518 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
519 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
520
521 for (uint32_t x=0; x < w; x++) {
522 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
523 oPtr ++;
524 i1 += 2;
525 i2 += 2;
526 }
527 }
528}
529
Jason Samse9f5c532009-07-28 17:20:11 -0700530static void mip(const Adapter2D &out, const Adapter2D &in)
531{
532 switch(out.getBaseType()->getElement()->getSizeBits()) {
533 case 32:
534 mip8888(out, in);
535 break;
536 case 16:
537 mip565(out, in);
538 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800539 case 8:
540 mip8(out, in);
541 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700542
543 }
544
545}
Jason Sams326e0dd2009-05-22 14:03:28 -0700546
Jason Sams6678e9b2009-05-27 14:45:32 -0700547typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
548
549static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
550{
551 memcpy(dst, src, count * 2);
552}
553static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
554{
555 memcpy(dst, src, count);
556}
557static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
558{
559 memcpy(dst, src, count * 4);
560}
561
562
563static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
564{
565 uint16_t *d = static_cast<uint16_t *>(dst);
566 const uint8_t *s = static_cast<const uint8_t *>(src);
567
568 while(count--) {
569 *d = rs888to565(s[0], s[1], s[2]);
570 d++;
571 s+= 3;
572 }
573}
574
575static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
576{
577 uint16_t *d = static_cast<uint16_t *>(dst);
578 const uint8_t *s = static_cast<const uint8_t *>(src);
579
580 while(count--) {
581 *d = rs888to565(s[0], s[1], s[2]);
582 d++;
583 s+= 4;
584 }
585}
586
Jason Samsa57c0a72009-09-04 14:42:41 -0700587static ElementConverter_t pickConverter(const Element *dst, const Element *src)
Jason Sams6678e9b2009-05-27 14:45:32 -0700588{
Jason Samsd01d9702009-12-23 14:35:29 -0800589 GLenum srcGLType = src->getComponent().getGLType();
590 GLenum srcGLFmt = src->getComponent().getGLFormat();
591 GLenum dstGLType = dst->getComponent().getGLType();
592 GLenum dstGLFmt = dst->getComponent().getGLFormat();
Jason Samsa57c0a72009-09-04 14:42:41 -0700593
594 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
595 switch(dst->getSizeBytes()) {
596 case 4:
597 return elementConverter_cpy_32;
598 case 2:
599 return elementConverter_cpy_16;
600 case 1:
601 return elementConverter_cpy_8;
602 }
Jason Sams6678e9b2009-05-27 14:45:32 -0700603 }
604
Jason Samsa57c0a72009-09-04 14:42:41 -0700605 if (srcGLType == GL_UNSIGNED_BYTE &&
606 srcGLFmt == GL_RGB &&
607 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
Jason Sams44b28942010-06-22 17:45:34 -0700608 dstGLFmt == GL_RGB) {
Jason Samsa57c0a72009-09-04 14:42:41 -0700609
Jason Sams6678e9b2009-05-27 14:45:32 -0700610 return elementConverter_888_to_565;
611 }
612
Jason Samsa57c0a72009-09-04 14:42:41 -0700613 if (srcGLType == GL_UNSIGNED_BYTE &&
614 srcGLFmt == GL_RGBA &&
615 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
Jason Sams44b28942010-06-22 17:45:34 -0700616 dstGLFmt == GL_RGB) {
Jason Samsa57c0a72009-09-04 14:42:41 -0700617
Jason Sams6678e9b2009-05-27 14:45:32 -0700618 return elementConverter_8888_to_565;
619 }
620
Jason Samsa57c0a72009-09-04 14:42:41 -0700621 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst);
Jason Sams44b28942010-06-22 17:45:34 -0700622 LOGE("pickConverter, srcGLType = %x, srcGLFmt = %x", srcGLType, srcGLFmt);
623 LOGE("pickConverter, dstGLType = %x, dstGLFmt = %x", dstGLType, dstGLFmt);
624 src->dumpLOGV("SRC ");
625 dst->dumpLOGV("DST ");
Jason Sams6678e9b2009-05-27 14:45:32 -0700626 return 0;
627}
628
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700629#ifndef ANDROID_RS_BUILD_FOR_HOST
630
Jason Samsfa84da22010-03-01 15:31:04 -0800631RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype,
632 void *bmp, void *callbackData, RsBitmapCallback_t callback)
633{
634 const Type * type = static_cast<const Type *>(vtype);
635 Allocation * alloc = new Allocation(rsc, type, bmp, callbackData, callback);
636 alloc->incUserRef();
637 return alloc;
638}
Jason Sams6678e9b2009-05-27 14:45:32 -0700639
Jason Samsa57c0a72009-09-04 14:42:41 -0700640RsAllocation 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 -0700641{
Jason Samsa57c0a72009-09-04 14:42:41 -0700642 const Element *src = static_cast<const Element *>(_src);
643 const Element *dst = static_cast<const Element *>(_dst);
Jason Samsfdcf7db2010-01-06 15:10:29 -0800644
645 // Check for pow2 on pre es 2.0 versions.
646 rsAssert(rsc->checkVersion2_0() || (!(w & (w-1)) && !(h & (h-1))));
Jason Samsc9d43db2009-07-28 12:02:16 -0700647
648 //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
Jason Samsa57c0a72009-09-04 14:42:41 -0700649 rsi_TypeBegin(rsc, _dst);
Jason Sams6678e9b2009-05-27 14:45:32 -0700650 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
651 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
652 if (genMips) {
653 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
654 }
655 RsType type = rsi_TypeCreate(rsc);
656
657 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
658 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
659 if (texAlloc == NULL) {
660 LOGE("Memory allocation failure");
661 return NULL;
662 }
Jason Sams6678e9b2009-05-27 14:45:32 -0700663
Jason Samsa57c0a72009-09-04 14:42:41 -0700664 ElementConverter_t cvt = pickConverter(dst, src);
Jason Sams6678e9b2009-05-27 14:45:32 -0700665 cvt(texAlloc->getPtr(), data, w * h);
666
667 if (genMips) {
Jason Samse514b452009-09-25 14:51:22 -0700668 Adapter2D adapt(rsc, texAlloc);
669 Adapter2D adapt2(rsc, texAlloc);
Jason Sams6678e9b2009-05-27 14:45:32 -0700670 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
671 adapt.setLOD(lod);
672 adapt2.setLOD(lod + 1);
Jason Samse9f5c532009-07-28 17:20:11 -0700673 mip(adapt2, adapt);
Jason Sams6678e9b2009-05-27 14:45:32 -0700674 }
675 }
676
677 return texAlloc;
678}
679
Jason Samsa57c0a72009-09-04 14:42:41 -0700680RsAllocation 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 -0700681{
Jason Samsa57c0a72009-09-04 14:42:41 -0700682 const Element *srcE = static_cast<const Element *>(_src);
683 const Element *dstE = static_cast<const Element *>(_dst);
Jason Samsc9d43db2009-07-28 12:02:16 -0700684 uint32_t w2 = rsHigherPow2(w);
685 uint32_t h2 = rsHigherPow2(h);
686
687 if ((w2 == w) && (h2 == h)) {
Jason Samsa57c0a72009-09-04 14:42:41 -0700688 return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data);
Jason Samsc9d43db2009-07-28 12:02:16 -0700689 }
690
Jason Samsa57c0a72009-09-04 14:42:41 -0700691 uint32_t bpp = srcE->getSizeBytes();
Jason Samsc9d43db2009-07-28 12:02:16 -0700692 size_t size = w2 * h2 * bpp;
693 uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
694 memset(tmp, 0, size);
695
696 const uint8_t * src = static_cast<const uint8_t *>(data);
697 for (uint32_t y = 0; y < h; y++) {
Jason Sams50253db2009-07-29 20:55:44 -0700698 uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
Marco Nelissen6cd833d2009-10-28 15:10:56 -0700699 memcpy(&ydst[((w2 - w) >> 1) * bpp], src, w * bpp);
Jason Sams50253db2009-07-29 20:55:44 -0700700 src += w * bpp;
Jason Samsc9d43db2009-07-28 12:02:16 -0700701 }
702
Jason Samsa57c0a72009-09-04 14:42:41 -0700703 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp);
Jason Samsc9d43db2009-07-28 12:02:16 -0700704 free(tmp);
705 return ret;
Jason Samsc9d43db2009-07-28 12:02:16 -0700706}
707
Jason Sams9397e302009-08-27 20:23:34 -0700708void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700709{
710 Allocation *a = static_cast<Allocation *>(va);
Jason Sams9397e302009-08-27 20:23:34 -0700711 a->data(data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700712}
713
Jason Sams9397e302009-08-27 20:23:34 -0700714void 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 -0700715{
716 Allocation *a = static_cast<Allocation *>(va);
Jason Sams9397e302009-08-27 20:23:34 -0700717 a->subData(xoff, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700718}
719
Jason Sams9397e302009-08-27 20:23:34 -0700720void 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 -0700721{
722 Allocation *a = static_cast<Allocation *>(va);
Jason Sams9397e302009-08-27 20:23:34 -0700723 a->subData(xoff, yoff, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700724}
725
Jason Samse579df42009-08-10 14:55:26 -0700726void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data)
727{
728 Allocation *a = static_cast<Allocation *>(va);
729 a->read(data);
730}
731
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700732const void* rsi_AllocationGetType(Context *rsc, RsAllocation va)
733{
734 Allocation *a = static_cast<Allocation *>(va);
735 a->getType()->incUserRef();
736
737 return a->getType();
738}
739
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700740#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -0700741
742}
743}