blob: d62fa554093de52c93584eebccbe15c683874d67 [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());
37 if (!mPtr) {
38 LOGE("Allocation::Allocation, alloc failure");
39 }
40}
41
42Allocation::Allocation(Context *rsc, const Type *type, void *bmp,
43 void *callbackData, RsBitmapCallback_t callback)
44: ObjectBase(rsc)
45{
46 init(rsc, type);
47
48 mPtr = bmp;
49 mUserBitmapCallback = callback;
50 mUserBitmapCallbackData = callbackData;
51}
52
53void Allocation::init(Context *rsc, const Type *type)
54{
Jason Samsf2649a92009-09-25 16:37:33 -070055 mAllocFile = __FILE__;
56 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070057 mPtr = NULL;
58
59 mCpuWrite = false;
60 mCpuRead = false;
61 mGpuWrite = false;
62 mGpuRead = false;
63
64 mReadWriteRatio = 0;
65 mUpdateSize = 0;
66
67 mIsTexture = false;
68 mTextureID = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070069 mIsVertexBuffer = false;
70 mBufferID = 0;
Jason Samscf4c7c92009-12-14 12:57:40 -080071 mUploadDefered = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070072
Jason Samsfa84da22010-03-01 15:31:04 -080073 mUserBitmapCallback = NULL;
74 mUserBitmapCallbackData = NULL;
75
Jason Sams326e0dd2009-05-22 14:03:28 -070076 mType.set(type);
Jason Samse5ffb872009-08-09 17:01:55 -070077 rsAssert(type);
Jason Samsfa84da22010-03-01 15:31:04 -080078
79 mPtr = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070080}
81
82Allocation::~Allocation()
83{
Jason Samsfa84da22010-03-01 15:31:04 -080084 if (mUserBitmapCallback != NULL) {
85 mUserBitmapCallback(mUserBitmapCallbackData);
86 } else {
87 free(mPtr);
88 }
Jason Samsbf3c14e2009-11-02 14:25:10 -080089 mPtr = NULL;
Jason Samse402ed32009-11-03 11:25:42 -080090
91 if (mBufferID) {
92 // Causes a SW crash....
93 //LOGV(" mBufferID %i", mBufferID);
94 //glDeleteBuffers(1, &mBufferID);
95 //mBufferID = 0;
96 }
97 if (mTextureID) {
98 glDeleteTextures(1, &mTextureID);
99 mTextureID = 0;
100 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700101}
102
103void Allocation::setCpuWritable(bool)
104{
105}
106
107void Allocation::setGpuWritable(bool)
108{
109}
110
111void Allocation::setCpuReadable(bool)
112{
113}
114
115void Allocation::setGpuReadable(bool)
116{
117}
118
119bool Allocation::fixAllocation()
120{
121 return false;
122}
123
Jason Sams7fabe1a2010-02-23 17:44:28 -0800124void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset)
Jason Samscf4c7c92009-12-14 12:57:40 -0800125{
126 rsAssert(lodOffset < mType->getLODCount());
127 mIsTexture = true;
128 mTextureLOD = lodOffset;
129 mUploadDefered = true;
Jason Sams7fabe1a2010-02-23 17:44:28 -0800130 mTextureGenMipmap = !mType->getDimLOD() && genMipmap;
Jason Samscf4c7c92009-12-14 12:57:40 -0800131}
132
133void Allocation::uploadToTexture(const Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700134{
135 //rsAssert(!mTextureId);
Jason Samscf4c7c92009-12-14 12:57:40 -0800136
137 mIsTexture = true;
138 if (!rsc->checkDriver()) {
139 mUploadDefered = true;
140 return;
141 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700142
Jason Samsd01d9702009-12-23 14:35:29 -0800143 GLenum type = mType->getElement()->getComponent().getGLType();
144 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Sams565ac362009-06-03 16:04:54 -0700145
146 if (!type || !format) {
147 return;
148 }
149
Jason Sams326e0dd2009-05-22 14:03:28 -0700150 if (!mTextureID) {
151 glGenTextures(1, &mTextureID);
Jason Sams13e26342009-11-24 12:26:35 -0800152
153 if (!mTextureID) {
154 // This should not happen, however, its likely the cause of the
155 // white sqare bug.
156 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
157 LOGE("Upload to texture failed to gen mTextureID");
158 rsc->dumpDebug();
Jason Samscf4c7c92009-12-14 12:57:40 -0800159 mUploadDefered = true;
160 return;
Jason Sams13e26342009-11-24 12:26:35 -0800161 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700162 }
163 glBindTexture(GL_TEXTURE_2D, mTextureID);
Jason Sams5f0b4e12009-11-05 12:44:58 -0800164 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700165
Jason Samse514b452009-09-25 14:51:22 -0700166 Adapter2D adapt(getContext(), this);
Jason Samscf4c7c92009-12-14 12:57:40 -0800167 for(uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
168 adapt.setLOD(lod+mTextureLOD);
Jason Sams326e0dd2009-05-22 14:03:28 -0700169
170 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Jason Sams565ac362009-06-03 16:04:54 -0700171 glTexImage2D(GL_TEXTURE_2D, lod, format,
172 adapt.getDimX(), adapt.getDimY(),
173 0, format, type, ptr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700174 }
Jason Sams7fabe1a2010-02-23 17:44:28 -0800175 if (mTextureGenMipmap) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700176#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams7fabe1a2010-02-23 17:44:28 -0800177 glGenerateMipmap(GL_TEXTURE_2D);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700178#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Sams7fabe1a2010-02-23 17:44:28 -0800179 }
180
Jason Samsc1ed5892010-03-10 17:30:41 -0800181 rsc->checkError("Allocation::uploadToTexture");
Jason Sams326e0dd2009-05-22 14:03:28 -0700182}
183
Jason Samscf4c7c92009-12-14 12:57:40 -0800184void Allocation::deferedUploadToBufferObject(const Context *rsc)
185{
186 mIsVertexBuffer = true;
187 mUploadDefered = true;
188}
189
190void Allocation::uploadToBufferObject(const Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700191{
192 rsAssert(!mType->getDimY());
193 rsAssert(!mType->getDimZ());
194
Jason Samscf4c7c92009-12-14 12:57:40 -0800195 mIsVertexBuffer = true;
196 if (!rsc->checkDriver()) {
197 mUploadDefered = true;
198 return;
199 }
200
Jason Sams326e0dd2009-05-22 14:03:28 -0700201 if (!mBufferID) {
202 glGenBuffers(1, &mBufferID);
203 }
Jason Samscf4c7c92009-12-14 12:57:40 -0800204 if (!mBufferID) {
205 LOGE("Upload to buffer object failed");
206 mUploadDefered = true;
207 return;
208 }
209
Jason Sams326e0dd2009-05-22 14:03:28 -0700210 glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
211 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
212 glBindBuffer(GL_ARRAY_BUFFER, 0);
Jason Samsc1ed5892010-03-10 17:30:41 -0800213 rsc->checkError("Allocation::uploadToBufferObject");
Jason Sams326e0dd2009-05-22 14:03:28 -0700214}
215
Jason Samscf4c7c92009-12-14 12:57:40 -0800216void Allocation::uploadCheck(const Context *rsc)
217{
218 if (mUploadDefered) {
219 mUploadDefered = false;
220 if (mIsVertexBuffer) {
221 uploadToBufferObject(rsc);
222 }
223 if (mIsTexture) {
224 uploadToTexture(rsc);
225 }
226 }
227}
228
Jason Samse5ffb872009-08-09 17:01:55 -0700229
Jason Sams9397e302009-08-27 20:23:34 -0700230void Allocation::data(const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700231{
Jason Sams9397e302009-08-27 20:23:34 -0700232 uint32_t size = mType->getSizeBytes();
233 if (size != sizeBytes) {
234 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
235 return;
236 }
Jason Samse3929c92010-08-09 18:13:33 -0700237
238 if (mType->getElement()->getHasReferences()) {
239 incRefs(data, sizeBytes / mType->getElement()->getSizeBytes());
240 decRefs(mPtr, sizeBytes / mType->getElement()->getSizeBytes());
241 }
242
Jason Sams9397e302009-08-27 20:23:34 -0700243 memcpy(mPtr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700244 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800245 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700246}
247
Jason Samse579df42009-08-10 14:55:26 -0700248void Allocation::read(void *data)
249{
250 memcpy(data, mPtr, mType->getSizeBytes());
251}
252
Jason Sams9397e302009-08-27 20:23:34 -0700253void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700254{
255 uint32_t eSize = mType->getElementSizeBytes();
256 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
257 ptr += eSize * xoff;
Jason Sams9397e302009-08-27 20:23:34 -0700258 uint32_t size = count * eSize;
259
260 if (size != sizeBytes) {
261 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Samse12c1c52009-09-27 17:50:38 -0700262 mType->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700263 return;
264 }
Jason Samse3929c92010-08-09 18:13:33 -0700265
266 if (mType->getElement()->getHasReferences()) {
267 incRefs(data, count);
268 decRefs(ptr, count);
269 }
270
Jason Sams9397e302009-08-27 20:23:34 -0700271 memcpy(ptr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700272 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800273 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700274}
275
Jason Sams565ac362009-06-03 16:04:54 -0700276void Allocation::subData(uint32_t xoff, uint32_t yoff,
Jason Sams9397e302009-08-27 20:23:34 -0700277 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700278{
279 uint32_t eSize = mType->getElementSizeBytes();
280 uint32_t lineSize = eSize * w;
281 uint32_t destW = mType->getDimX();
282
283 const uint8_t *src = static_cast<const uint8_t *>(data);
284 uint8_t *dst = static_cast<uint8_t *>(mPtr);
285 dst += eSize * (xoff + yoff * destW);
Jason Sams9397e302009-08-27 20:23:34 -0700286
287 if ((lineSize * eSize * h) != sizeBytes) {
288 rsAssert(!"Allocation::subData called with mismatched size");
289 return;
290 }
291
Jason Sams326e0dd2009-05-22 14:03:28 -0700292 for (uint32_t line=yoff; line < (yoff+h); line++) {
293 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
Jason Samse3929c92010-08-09 18:13:33 -0700294 if (mType->getElement()->getHasReferences()) {
295 incRefs(src, w);
296 decRefs(dst, w);
297 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700298 memcpy(dst, src, lineSize);
299 src += lineSize;
300 dst += destW * eSize;
301 }
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700302 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800303 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700304}
305
306void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams9397e302009-08-27 20:23:34 -0700307 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700308{
309}
310
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700311void Allocation::addProgramToDirty(const Program *p)
312{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700313 mToDirtyList.push(p);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700314}
Jason Sams326e0dd2009-05-22 14:03:28 -0700315
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700316void Allocation::removeProgramToDirty(const Program *p)
317{
318 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
319 if (mToDirtyList[ct] == p) {
320 mToDirtyList.removeAt(ct);
321 return;
322 }
323 }
324 rsAssert(0);
325}
326
Jason Samsc21cf402009-11-17 17:26:46 -0800327void Allocation::dumpLOGV(const char *prefix) const
328{
329 ObjectBase::dumpLOGV(prefix);
330
331 String8 s(prefix);
332 s.append(" type ");
333 if (mType.get()) {
334 mType->dumpLOGV(s.string());
335 }
336
337 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
338 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
339
Jason Sams43999e72009-11-23 15:27:33 -0800340 LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i",
Jason Samsc21cf402009-11-17 17:26:46 -0800341 prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID);
342
Jason Samsc21cf402009-11-17 17:26:46 -0800343}
Jason Sams326e0dd2009-05-22 14:03:28 -0700344
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700345void Allocation::serialize(OStream *stream) const
346{
347 // Need to identify ourselves
348 stream->addU32((uint32_t)getClassId());
349
350 String8 name(getName());
351 stream->addString(&name);
352
353 // First thing we need to serialize is the type object since it will be needed
354 // to initialize the class
355 mType->serialize(stream);
356
357 uint32_t dataSize = mType->getSizeBytes();
358 // Write how much data we are storing
359 stream->addU32(dataSize);
360 // Now write the data
361 stream->addByteArray(mPtr, dataSize);
362}
363
364Allocation *Allocation::createFromStream(Context *rsc, IStream *stream)
365{
366 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700367 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
368 if(classID != RS_A3D_CLASS_ID_ALLOCATION) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700369 LOGE("allocation loading skipped due to invalid class id\n");
370 return NULL;
371 }
372
373 String8 name;
374 stream->loadString(&name);
375
376 Type *type = Type::createFromStream(rsc, stream);
377 if(!type) {
378 return NULL;
379 }
380 type->compute();
381
382 // Number of bytes we wrote out for this allocation
383 uint32_t dataSize = stream->loadU32();
384 if(dataSize != type->getSizeBytes()) {
385 LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
386 delete type;
387 return NULL;
388 }
389
390 Allocation *alloc = new Allocation(rsc, type);
391 alloc->setName(name.string(), name.size());
392
393 // Read in all of our allocation data
394 stream->loadByteArray(alloc->getPtr(), dataSize);
395
396 return alloc;
397}
398
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700399void Allocation::sendDirty() const
400{
401 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
402 mToDirtyList[ct]->forceDirty();
403 }
404}
Jason Sams326e0dd2009-05-22 14:03:28 -0700405
Jason Samse3929c92010-08-09 18:13:33 -0700406void Allocation::incRefs(const void *ptr, size_t ct) const
407{
408 const uint8_t *p = static_cast<const uint8_t *>(ptr);
409 const Element *e = mType->getElement();
410 uint32_t stride = e->getSizeBytes();
411
412 while (ct > 0) {
413 e->incRefs(p);
414 ct --;
415 p += stride;
416 }
417}
418
419void Allocation::decRefs(const void *ptr, size_t ct) const
420{
421 const uint8_t *p = static_cast<const uint8_t *>(ptr);
422 const Element *e = mType->getElement();
423 uint32_t stride = e->getSizeBytes();
424
425 while (ct > 0) {
426 e->decRefs(p);
427 ct --;
428 p += stride;
429 }
430}
431
Jason Sams326e0dd2009-05-22 14:03:28 -0700432/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700433//
Jason Sams326e0dd2009-05-22 14:03:28 -0700434
435
436namespace android {
437namespace renderscript {
438
439RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
440{
441 const Type * type = static_cast<const Type *>(vtype);
442
Jason Samse514b452009-09-25 14:51:22 -0700443 Allocation * alloc = new Allocation(rsc, type);
Jason Sams9397e302009-08-27 20:23:34 -0700444 alloc->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700445 return alloc;
446}
447
Jason Sams326e0dd2009-05-22 14:03:28 -0700448RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
449{
Jason Samse514b452009-09-25 14:51:22 -0700450 Type * type = new Type(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700451 type->setDimX(count);
452 type->setElement(static_cast<Element *>(e));
453 type->compute();
454 return rsi_AllocationCreateTyped(rsc, type);
455}
456
Jason Sams7fabe1a2010-02-23 17:44:28 -0800457void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel)
Jason Sams326e0dd2009-05-22 14:03:28 -0700458{
459 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams7fabe1a2010-02-23 17:44:28 -0800460 alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel);
Jason Sams326e0dd2009-05-22 14:03:28 -0700461}
462
463void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
464{
465 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samscf4c7c92009-12-14 12:57:40 -0800466 alloc->deferedUploadToBufferObject(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700467}
468
Jason Sams565ac362009-06-03 16:04:54 -0700469static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Sams326e0dd2009-05-22 14:03:28 -0700470{
471 uint32_t w = out.getDimX();
472 uint32_t h = out.getDimY();
473
Jason Samse9f5c532009-07-28 17:20:11 -0700474 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700475 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
476 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
477 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
478
Jason Samse9f5c532009-07-28 17:20:11 -0700479 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700480 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
481 oPtr ++;
482 i1 += 2;
483 i2 += 2;
484 }
485 }
486}
487
488static void mip8888(const Adapter2D &out, const Adapter2D &in)
489{
490 uint32_t w = out.getDimX();
491 uint32_t h = out.getDimY();
492
Jason Samse9f5c532009-07-28 17:20:11 -0700493 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700494 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
495 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
496 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
497
Jason Samse9f5c532009-07-28 17:20:11 -0700498 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700499 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700500 oPtr ++;
501 i1 += 2;
502 i2 += 2;
503 }
504 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700505}
506
Jason Sams2f6d8612010-01-19 17:53:54 -0800507static void mip8(const Adapter2D &out, const Adapter2D &in)
508{
509 uint32_t w = out.getDimX();
510 uint32_t h = out.getDimY();
511
512 for (uint32_t y=0; y < h; y++) {
513 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
514 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
515 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
516
517 for (uint32_t x=0; x < w; x++) {
518 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
519 oPtr ++;
520 i1 += 2;
521 i2 += 2;
522 }
523 }
524}
525
Jason Samse9f5c532009-07-28 17:20:11 -0700526static void mip(const Adapter2D &out, const Adapter2D &in)
527{
528 switch(out.getBaseType()->getElement()->getSizeBits()) {
529 case 32:
530 mip8888(out, in);
531 break;
532 case 16:
533 mip565(out, in);
534 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800535 case 8:
536 mip8(out, in);
537 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700538
539 }
540
541}
Jason Sams326e0dd2009-05-22 14:03:28 -0700542
Jason Sams6678e9b2009-05-27 14:45:32 -0700543typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
544
545static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
546{
547 memcpy(dst, src, count * 2);
548}
549static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
550{
551 memcpy(dst, src, count);
552}
553static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
554{
555 memcpy(dst, src, count * 4);
556}
557
558
559static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
560{
561 uint16_t *d = static_cast<uint16_t *>(dst);
562 const uint8_t *s = static_cast<const uint8_t *>(src);
563
564 while(count--) {
565 *d = rs888to565(s[0], s[1], s[2]);
566 d++;
567 s+= 3;
568 }
569}
570
571static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
572{
573 uint16_t *d = static_cast<uint16_t *>(dst);
574 const uint8_t *s = static_cast<const uint8_t *>(src);
575
576 while(count--) {
577 *d = rs888to565(s[0], s[1], s[2]);
578 d++;
579 s+= 4;
580 }
581}
582
Jason Samsa57c0a72009-09-04 14:42:41 -0700583static ElementConverter_t pickConverter(const Element *dst, const Element *src)
Jason Sams6678e9b2009-05-27 14:45:32 -0700584{
Jason Samsd01d9702009-12-23 14:35:29 -0800585 GLenum srcGLType = src->getComponent().getGLType();
586 GLenum srcGLFmt = src->getComponent().getGLFormat();
587 GLenum dstGLType = dst->getComponent().getGLType();
588 GLenum dstGLFmt = dst->getComponent().getGLFormat();
Jason Samsa57c0a72009-09-04 14:42:41 -0700589
590 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
591 switch(dst->getSizeBytes()) {
592 case 4:
593 return elementConverter_cpy_32;
594 case 2:
595 return elementConverter_cpy_16;
596 case 1:
597 return elementConverter_cpy_8;
598 }
Jason Sams6678e9b2009-05-27 14:45:32 -0700599 }
600
Jason Samsa57c0a72009-09-04 14:42:41 -0700601 if (srcGLType == GL_UNSIGNED_BYTE &&
602 srcGLFmt == GL_RGB &&
603 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
Jason Sams44b28942010-06-22 17:45:34 -0700604 dstGLFmt == GL_RGB) {
Jason Samsa57c0a72009-09-04 14:42:41 -0700605
Jason Sams6678e9b2009-05-27 14:45:32 -0700606 return elementConverter_888_to_565;
607 }
608
Jason Samsa57c0a72009-09-04 14:42:41 -0700609 if (srcGLType == GL_UNSIGNED_BYTE &&
610 srcGLFmt == GL_RGBA &&
611 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
Jason Sams44b28942010-06-22 17:45:34 -0700612 dstGLFmt == GL_RGB) {
Jason Samsa57c0a72009-09-04 14:42:41 -0700613
Jason Sams6678e9b2009-05-27 14:45:32 -0700614 return elementConverter_8888_to_565;
615 }
616
Jason Samsa57c0a72009-09-04 14:42:41 -0700617 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst);
Jason Sams44b28942010-06-22 17:45:34 -0700618 LOGE("pickConverter, srcGLType = %x, srcGLFmt = %x", srcGLType, srcGLFmt);
619 LOGE("pickConverter, dstGLType = %x, dstGLFmt = %x", dstGLType, dstGLFmt);
620 src->dumpLOGV("SRC ");
621 dst->dumpLOGV("DST ");
Jason Sams6678e9b2009-05-27 14:45:32 -0700622 return 0;
623}
624
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700625#ifndef ANDROID_RS_BUILD_FOR_HOST
626
Jason Samsfa84da22010-03-01 15:31:04 -0800627RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype,
628 void *bmp, void *callbackData, RsBitmapCallback_t callback)
629{
630 const Type * type = static_cast<const Type *>(vtype);
631 Allocation * alloc = new Allocation(rsc, type, bmp, callbackData, callback);
632 alloc->incUserRef();
633 return alloc;
634}
Jason Sams6678e9b2009-05-27 14:45:32 -0700635
Jason Samsa57c0a72009-09-04 14:42:41 -0700636RsAllocation 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 -0700637{
Jason Samsa57c0a72009-09-04 14:42:41 -0700638 const Element *src = static_cast<const Element *>(_src);
639 const Element *dst = static_cast<const Element *>(_dst);
Jason Samsfdcf7db2010-01-06 15:10:29 -0800640
641 // Check for pow2 on pre es 2.0 versions.
642 rsAssert(rsc->checkVersion2_0() || (!(w & (w-1)) && !(h & (h-1))));
Jason Samsc9d43db2009-07-28 12:02:16 -0700643
644 //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
Jason Samsa57c0a72009-09-04 14:42:41 -0700645 rsi_TypeBegin(rsc, _dst);
Jason Sams6678e9b2009-05-27 14:45:32 -0700646 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
647 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
648 if (genMips) {
649 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
650 }
651 RsType type = rsi_TypeCreate(rsc);
652
653 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
654 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
655 if (texAlloc == NULL) {
656 LOGE("Memory allocation failure");
657 return NULL;
658 }
Jason Sams6678e9b2009-05-27 14:45:32 -0700659
Jason Samsa57c0a72009-09-04 14:42:41 -0700660 ElementConverter_t cvt = pickConverter(dst, src);
Jason Sams6678e9b2009-05-27 14:45:32 -0700661 cvt(texAlloc->getPtr(), data, w * h);
662
663 if (genMips) {
Jason Samse514b452009-09-25 14:51:22 -0700664 Adapter2D adapt(rsc, texAlloc);
665 Adapter2D adapt2(rsc, texAlloc);
Jason Sams6678e9b2009-05-27 14:45:32 -0700666 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
667 adapt.setLOD(lod);
668 adapt2.setLOD(lod + 1);
Jason Samse9f5c532009-07-28 17:20:11 -0700669 mip(adapt2, adapt);
Jason Sams6678e9b2009-05-27 14:45:32 -0700670 }
671 }
672
673 return texAlloc;
674}
675
Jason Samsa57c0a72009-09-04 14:42:41 -0700676RsAllocation 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 -0700677{
Jason Samsa57c0a72009-09-04 14:42:41 -0700678 const Element *srcE = static_cast<const Element *>(_src);
679 const Element *dstE = static_cast<const Element *>(_dst);
Jason Samsc9d43db2009-07-28 12:02:16 -0700680 uint32_t w2 = rsHigherPow2(w);
681 uint32_t h2 = rsHigherPow2(h);
682
683 if ((w2 == w) && (h2 == h)) {
Jason Samsa57c0a72009-09-04 14:42:41 -0700684 return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data);
Jason Samsc9d43db2009-07-28 12:02:16 -0700685 }
686
Jason Samsa57c0a72009-09-04 14:42:41 -0700687 uint32_t bpp = srcE->getSizeBytes();
Jason Samsc9d43db2009-07-28 12:02:16 -0700688 size_t size = w2 * h2 * bpp;
689 uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
690 memset(tmp, 0, size);
691
692 const uint8_t * src = static_cast<const uint8_t *>(data);
693 for (uint32_t y = 0; y < h; y++) {
Jason Sams50253db2009-07-29 20:55:44 -0700694 uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
Marco Nelissen6cd833d2009-10-28 15:10:56 -0700695 memcpy(&ydst[((w2 - w) >> 1) * bpp], src, w * bpp);
Jason Sams50253db2009-07-29 20:55:44 -0700696 src += w * bpp;
Jason Samsc9d43db2009-07-28 12:02:16 -0700697 }
698
Jason Samsa57c0a72009-09-04 14:42:41 -0700699 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp);
Jason Samsc9d43db2009-07-28 12:02:16 -0700700 free(tmp);
701 return ret;
Jason Samsc9d43db2009-07-28 12:02:16 -0700702}
703
Jason Sams9397e302009-08-27 20:23:34 -0700704void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700705{
706 Allocation *a = static_cast<Allocation *>(va);
Jason Sams9397e302009-08-27 20:23:34 -0700707 a->data(data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700708}
709
Jason Sams9397e302009-08-27 20:23:34 -0700710void 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 -0700711{
712 Allocation *a = static_cast<Allocation *>(va);
Jason Sams9397e302009-08-27 20:23:34 -0700713 a->subData(xoff, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700714}
715
Jason Sams9397e302009-08-27 20:23:34 -0700716void 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 -0700717{
718 Allocation *a = static_cast<Allocation *>(va);
Jason Sams9397e302009-08-27 20:23:34 -0700719 a->subData(xoff, yoff, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700720}
721
Jason Samse579df42009-08-10 14:55:26 -0700722void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data)
723{
724 Allocation *a = static_cast<Allocation *>(va);
725 a->read(data);
726}
727
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700728const void* rsi_AllocationGetType(Context *rsc, RsAllocation va)
729{
730 Allocation *a = static_cast<Allocation *>(va);
731 a->getType()->incUserRef();
732
733 return a->getType();
734}
735
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700736#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -0700737
738}
739}