blob: e5ff1d743db6309cc112b79c00776481493d1c75 [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 */
16
17#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>
22
Jason Sams326e0dd2009-05-22 14:03:28 -070023using namespace android;
24using namespace android::renderscript;
25
Jason Samse514b452009-09-25 14:51:22 -070026Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070027{
Jason Samsfa84da22010-03-01 15:31:04 -080028 init(rsc, type);
29
30 mPtr = malloc(mType->getSizeBytes());
31 if (!mPtr) {
32 LOGE("Allocation::Allocation, alloc failure");
33 }
34}
35
36Allocation::Allocation(Context *rsc, const Type *type, void *bmp,
37 void *callbackData, RsBitmapCallback_t callback)
38: ObjectBase(rsc)
39{
40 init(rsc, type);
41
42 mPtr = bmp;
43 mUserBitmapCallback = callback;
44 mUserBitmapCallbackData = callbackData;
45}
46
47void Allocation::init(Context *rsc, const Type *type)
48{
Jason Samsf2649a92009-09-25 16:37:33 -070049 mAllocFile = __FILE__;
50 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070051 mPtr = NULL;
52
53 mCpuWrite = false;
54 mCpuRead = false;
55 mGpuWrite = false;
56 mGpuRead = false;
57
58 mReadWriteRatio = 0;
59 mUpdateSize = 0;
60
61 mIsTexture = false;
62 mTextureID = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070063 mIsVertexBuffer = false;
64 mBufferID = 0;
Jason Samscf4c7c92009-12-14 12:57:40 -080065 mUploadDefered = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070066
Jason Samsfa84da22010-03-01 15:31:04 -080067 mUserBitmapCallback = NULL;
68 mUserBitmapCallbackData = NULL;
69
Jason Sams326e0dd2009-05-22 14:03:28 -070070 mType.set(type);
Jason Samse5ffb872009-08-09 17:01:55 -070071 rsAssert(type);
Jason Samsfa84da22010-03-01 15:31:04 -080072
73 mPtr = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070074}
75
76Allocation::~Allocation()
77{
Jason Samsfa84da22010-03-01 15:31:04 -080078 if (mUserBitmapCallback != NULL) {
79 mUserBitmapCallback(mUserBitmapCallbackData);
80 } else {
81 free(mPtr);
82 }
Jason Samsbf3c14e2009-11-02 14:25:10 -080083 mPtr = NULL;
Jason Samse402ed32009-11-03 11:25:42 -080084
85 if (mBufferID) {
86 // Causes a SW crash....
87 //LOGV(" mBufferID %i", mBufferID);
88 //glDeleteBuffers(1, &mBufferID);
89 //mBufferID = 0;
90 }
91 if (mTextureID) {
92 glDeleteTextures(1, &mTextureID);
93 mTextureID = 0;
94 }
Jason Sams326e0dd2009-05-22 14:03:28 -070095}
96
97void Allocation::setCpuWritable(bool)
98{
99}
100
101void Allocation::setGpuWritable(bool)
102{
103}
104
105void Allocation::setCpuReadable(bool)
106{
107}
108
109void Allocation::setGpuReadable(bool)
110{
111}
112
113bool Allocation::fixAllocation()
114{
115 return false;
116}
117
Jason Sams7fabe1a2010-02-23 17:44:28 -0800118void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset)
Jason Samscf4c7c92009-12-14 12:57:40 -0800119{
120 rsAssert(lodOffset < mType->getLODCount());
121 mIsTexture = true;
122 mTextureLOD = lodOffset;
123 mUploadDefered = true;
Jason Sams7fabe1a2010-02-23 17:44:28 -0800124 mTextureGenMipmap = !mType->getDimLOD() && genMipmap;
Jason Samscf4c7c92009-12-14 12:57:40 -0800125}
126
127void Allocation::uploadToTexture(const Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700128{
129 //rsAssert(!mTextureId);
Jason Samscf4c7c92009-12-14 12:57:40 -0800130
131 mIsTexture = true;
132 if (!rsc->checkDriver()) {
133 mUploadDefered = true;
134 return;
135 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700136
Jason Samsd01d9702009-12-23 14:35:29 -0800137 GLenum type = mType->getElement()->getComponent().getGLType();
138 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Sams565ac362009-06-03 16:04:54 -0700139
140 if (!type || !format) {
141 return;
142 }
143
Jason Sams326e0dd2009-05-22 14:03:28 -0700144 if (!mTextureID) {
145 glGenTextures(1, &mTextureID);
Jason Sams13e26342009-11-24 12:26:35 -0800146
147 if (!mTextureID) {
148 // This should not happen, however, its likely the cause of the
149 // white sqare bug.
150 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
151 LOGE("Upload to texture failed to gen mTextureID");
152 rsc->dumpDebug();
Jason Samscf4c7c92009-12-14 12:57:40 -0800153 mUploadDefered = true;
154 return;
Jason Sams13e26342009-11-24 12:26:35 -0800155 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700156 }
157 glBindTexture(GL_TEXTURE_2D, mTextureID);
Jason Sams5f0b4e12009-11-05 12:44:58 -0800158 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700159
Jason Samse514b452009-09-25 14:51:22 -0700160 Adapter2D adapt(getContext(), this);
Jason Samscf4c7c92009-12-14 12:57:40 -0800161 for(uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
162 adapt.setLOD(lod+mTextureLOD);
Jason Sams326e0dd2009-05-22 14:03:28 -0700163
164 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Jason Sams565ac362009-06-03 16:04:54 -0700165 glTexImage2D(GL_TEXTURE_2D, lod, format,
166 adapt.getDimX(), adapt.getDimY(),
167 0, format, type, ptr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700168 }
Jason Sams7fabe1a2010-02-23 17:44:28 -0800169 if (mTextureGenMipmap) {
170 glGenerateMipmap(GL_TEXTURE_2D);
171 }
172
Jason Samsc1ed5892010-03-10 17:30:41 -0800173 rsc->checkError("Allocation::uploadToTexture");
Jason Sams326e0dd2009-05-22 14:03:28 -0700174}
175
Jason Samscf4c7c92009-12-14 12:57:40 -0800176void Allocation::deferedUploadToBufferObject(const Context *rsc)
177{
178 mIsVertexBuffer = true;
179 mUploadDefered = true;
180}
181
182void Allocation::uploadToBufferObject(const Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700183{
184 rsAssert(!mType->getDimY());
185 rsAssert(!mType->getDimZ());
186
Jason Samscf4c7c92009-12-14 12:57:40 -0800187 mIsVertexBuffer = true;
188 if (!rsc->checkDriver()) {
189 mUploadDefered = true;
190 return;
191 }
192
Jason Sams326e0dd2009-05-22 14:03:28 -0700193 if (!mBufferID) {
194 glGenBuffers(1, &mBufferID);
195 }
Jason Samscf4c7c92009-12-14 12:57:40 -0800196 if (!mBufferID) {
197 LOGE("Upload to buffer object failed");
198 mUploadDefered = true;
199 return;
200 }
201
Jason Sams326e0dd2009-05-22 14:03:28 -0700202 glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
203 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
204 glBindBuffer(GL_ARRAY_BUFFER, 0);
Jason Samsc1ed5892010-03-10 17:30:41 -0800205 rsc->checkError("Allocation::uploadToBufferObject");
Jason Sams326e0dd2009-05-22 14:03:28 -0700206}
207
Jason Samscf4c7c92009-12-14 12:57:40 -0800208void Allocation::uploadCheck(const Context *rsc)
209{
210 if (mUploadDefered) {
211 mUploadDefered = false;
212 if (mIsVertexBuffer) {
213 uploadToBufferObject(rsc);
214 }
215 if (mIsTexture) {
216 uploadToTexture(rsc);
217 }
218 }
219}
220
Jason Samse5ffb872009-08-09 17:01:55 -0700221
Jason Sams9397e302009-08-27 20:23:34 -0700222void Allocation::data(const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700223{
Jason Sams9397e302009-08-27 20:23:34 -0700224 uint32_t size = mType->getSizeBytes();
225 if (size != sizeBytes) {
226 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
227 return;
228 }
229 memcpy(mPtr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700230 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800231 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700232}
233
Jason Samse579df42009-08-10 14:55:26 -0700234void Allocation::read(void *data)
235{
236 memcpy(data, mPtr, mType->getSizeBytes());
237}
238
Jason Sams9397e302009-08-27 20:23:34 -0700239void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700240{
241 uint32_t eSize = mType->getElementSizeBytes();
242 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
243 ptr += eSize * xoff;
Jason Sams9397e302009-08-27 20:23:34 -0700244 uint32_t size = count * eSize;
245
246 if (size != sizeBytes) {
247 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Samse12c1c52009-09-27 17:50:38 -0700248 mType->dumpLOGV("type info");
Jason Sams9397e302009-08-27 20:23:34 -0700249 return;
250 }
251 memcpy(ptr, data, size);
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700252 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800253 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700254}
255
Jason Sams565ac362009-06-03 16:04:54 -0700256void Allocation::subData(uint32_t xoff, uint32_t yoff,
Jason Sams9397e302009-08-27 20:23:34 -0700257 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700258{
259 uint32_t eSize = mType->getElementSizeBytes();
260 uint32_t lineSize = eSize * w;
261 uint32_t destW = mType->getDimX();
262
263 const uint8_t *src = static_cast<const uint8_t *>(data);
264 uint8_t *dst = static_cast<uint8_t *>(mPtr);
265 dst += eSize * (xoff + yoff * destW);
Jason Sams9397e302009-08-27 20:23:34 -0700266
267 if ((lineSize * eSize * h) != sizeBytes) {
268 rsAssert(!"Allocation::subData called with mismatched size");
269 return;
270 }
271
Jason Sams326e0dd2009-05-22 14:03:28 -0700272 for (uint32_t line=yoff; line < (yoff+h); line++) {
273 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
274 memcpy(dst, src, lineSize);
275 src += lineSize;
276 dst += destW * eSize;
277 }
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700278 sendDirty();
Jason Samscf4c7c92009-12-14 12:57:40 -0800279 mUploadDefered = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700280}
281
282void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams9397e302009-08-27 20:23:34 -0700283 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700284{
285}
286
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700287void Allocation::addProgramToDirty(const Program *p)
288{
289 mToDirtyList.add(p);
290}
Jason Sams326e0dd2009-05-22 14:03:28 -0700291
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700292void Allocation::removeProgramToDirty(const Program *p)
293{
294 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
295 if (mToDirtyList[ct] == p) {
296 mToDirtyList.removeAt(ct);
297 return;
298 }
299 }
300 rsAssert(0);
301}
302
Jason Samsc21cf402009-11-17 17:26:46 -0800303void Allocation::dumpLOGV(const char *prefix) const
304{
305 ObjectBase::dumpLOGV(prefix);
306
307 String8 s(prefix);
308 s.append(" type ");
309 if (mType.get()) {
310 mType->dumpLOGV(s.string());
311 }
312
313 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
314 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
315
Jason Sams43999e72009-11-23 15:27:33 -0800316 LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i",
Jason Samsc21cf402009-11-17 17:26:46 -0800317 prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID);
318
Jason Samsc21cf402009-11-17 17:26:46 -0800319}
Jason Sams326e0dd2009-05-22 14:03:28 -0700320
Jason Sams5c3e3bc2009-10-26 15:19:28 -0700321void Allocation::sendDirty() const
322{
323 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
324 mToDirtyList[ct]->forceDirty();
325 }
326}
Jason Sams326e0dd2009-05-22 14:03:28 -0700327
328/////////////////
Jason Sams565ac362009-06-03 16:04:54 -0700329//
Jason Sams326e0dd2009-05-22 14:03:28 -0700330
331
332namespace android {
333namespace renderscript {
334
335RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
336{
337 const Type * type = static_cast<const Type *>(vtype);
338
Jason Samse514b452009-09-25 14:51:22 -0700339 Allocation * alloc = new Allocation(rsc, type);
Jason Sams9397e302009-08-27 20:23:34 -0700340 alloc->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700341 return alloc;
342}
343
Jason Sams326e0dd2009-05-22 14:03:28 -0700344RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
345{
Jason Samse514b452009-09-25 14:51:22 -0700346 Type * type = new Type(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700347 type->setDimX(count);
348 type->setElement(static_cast<Element *>(e));
349 type->compute();
350 return rsi_AllocationCreateTyped(rsc, type);
351}
352
Jason Sams7fabe1a2010-02-23 17:44:28 -0800353void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel)
Jason Sams326e0dd2009-05-22 14:03:28 -0700354{
355 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams7fabe1a2010-02-23 17:44:28 -0800356 alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel);
Jason Sams326e0dd2009-05-22 14:03:28 -0700357}
358
359void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
360{
361 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samscf4c7c92009-12-14 12:57:40 -0800362 alloc->deferedUploadToBufferObject(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700363}
364
Jason Sams565ac362009-06-03 16:04:54 -0700365static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Sams326e0dd2009-05-22 14:03:28 -0700366{
367 uint32_t w = out.getDimX();
368 uint32_t h = out.getDimY();
369
Jason Samse9f5c532009-07-28 17:20:11 -0700370 for (uint32_t y=0; y < h; y++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700371 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
372 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
373 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
374
Jason Samse9f5c532009-07-28 17:20:11 -0700375 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700376 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
377 oPtr ++;
378 i1 += 2;
379 i2 += 2;
380 }
381 }
382}
383
384static void mip8888(const Adapter2D &out, const Adapter2D &in)
385{
386 uint32_t w = out.getDimX();
387 uint32_t h = out.getDimY();
388
Jason Samse9f5c532009-07-28 17:20:11 -0700389 for (uint32_t y=0; y < h; y++) {
Jason Sams565ac362009-06-03 16:04:54 -0700390 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
391 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
392 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
393
Jason Samse9f5c532009-07-28 17:20:11 -0700394 for (uint32_t x=0; x < w; x++) {
Jason Sams565ac362009-06-03 16:04:54 -0700395 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700396 oPtr ++;
397 i1 += 2;
398 i2 += 2;
399 }
400 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700401}
402
Jason Sams2f6d8612010-01-19 17:53:54 -0800403static void mip8(const Adapter2D &out, const Adapter2D &in)
404{
405 uint32_t w = out.getDimX();
406 uint32_t h = out.getDimY();
407
408 for (uint32_t y=0; y < h; y++) {
409 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
410 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
411 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
412
413 for (uint32_t x=0; x < w; x++) {
414 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
415 oPtr ++;
416 i1 += 2;
417 i2 += 2;
418 }
419 }
420}
421
Jason Samse9f5c532009-07-28 17:20:11 -0700422static void mip(const Adapter2D &out, const Adapter2D &in)
423{
424 switch(out.getBaseType()->getElement()->getSizeBits()) {
425 case 32:
426 mip8888(out, in);
427 break;
428 case 16:
429 mip565(out, in);
430 break;
Jason Sams2f6d8612010-01-19 17:53:54 -0800431 case 8:
432 mip8(out, in);
433 break;
Jason Samse9f5c532009-07-28 17:20:11 -0700434
435 }
436
437}
Jason Sams326e0dd2009-05-22 14:03:28 -0700438
Jason Sams6678e9b2009-05-27 14:45:32 -0700439typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
440
441static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
442{
443 memcpy(dst, src, count * 2);
444}
445static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
446{
447 memcpy(dst, src, count);
448}
449static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
450{
451 memcpy(dst, src, count * 4);
452}
453
454
455static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
456{
457 uint16_t *d = static_cast<uint16_t *>(dst);
458 const uint8_t *s = static_cast<const uint8_t *>(src);
459
460 while(count--) {
461 *d = rs888to565(s[0], s[1], s[2]);
462 d++;
463 s+= 3;
464 }
465}
466
467static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
468{
469 uint16_t *d = static_cast<uint16_t *>(dst);
470 const uint8_t *s = static_cast<const uint8_t *>(src);
471
472 while(count--) {
473 *d = rs888to565(s[0], s[1], s[2]);
474 d++;
475 s+= 4;
476 }
477}
478
Jason Samsa57c0a72009-09-04 14:42:41 -0700479static ElementConverter_t pickConverter(const Element *dst, const Element *src)
Jason Sams6678e9b2009-05-27 14:45:32 -0700480{
Jason Samsd01d9702009-12-23 14:35:29 -0800481 GLenum srcGLType = src->getComponent().getGLType();
482 GLenum srcGLFmt = src->getComponent().getGLFormat();
483 GLenum dstGLType = dst->getComponent().getGLType();
484 GLenum dstGLFmt = dst->getComponent().getGLFormat();
Jason Samsa57c0a72009-09-04 14:42:41 -0700485
486 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
487 switch(dst->getSizeBytes()) {
488 case 4:
489 return elementConverter_cpy_32;
490 case 2:
491 return elementConverter_cpy_16;
492 case 1:
493 return elementConverter_cpy_8;
494 }
Jason Sams6678e9b2009-05-27 14:45:32 -0700495 }
496
Jason Samsa57c0a72009-09-04 14:42:41 -0700497 if (srcGLType == GL_UNSIGNED_BYTE &&
498 srcGLFmt == GL_RGB &&
499 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
500 dstGLType == GL_RGB) {
501
Jason Sams6678e9b2009-05-27 14:45:32 -0700502 return elementConverter_888_to_565;
503 }
504
Jason Samsa57c0a72009-09-04 14:42:41 -0700505 if (srcGLType == GL_UNSIGNED_BYTE &&
506 srcGLFmt == GL_RGBA &&
507 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
508 dstGLType == GL_RGB) {
509
Jason Sams6678e9b2009-05-27 14:45:32 -0700510 return elementConverter_8888_to_565;
511 }
512
Jason Samsa57c0a72009-09-04 14:42:41 -0700513 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst);
Jason Sams6678e9b2009-05-27 14:45:32 -0700514 return 0;
515}
516
Jason Samsfa84da22010-03-01 15:31:04 -0800517RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype,
518 void *bmp, void *callbackData, RsBitmapCallback_t callback)
519{
520 const Type * type = static_cast<const Type *>(vtype);
521 Allocation * alloc = new Allocation(rsc, type, bmp, callbackData, callback);
522 alloc->incUserRef();
523 return alloc;
524}
Jason Sams6678e9b2009-05-27 14:45:32 -0700525
Jason Samsa57c0a72009-09-04 14:42:41 -0700526RsAllocation 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 -0700527{
Jason Samsa57c0a72009-09-04 14:42:41 -0700528 const Element *src = static_cast<const Element *>(_src);
529 const Element *dst = static_cast<const Element *>(_dst);
Jason Samsfdcf7db2010-01-06 15:10:29 -0800530
531 // Check for pow2 on pre es 2.0 versions.
532 rsAssert(rsc->checkVersion2_0() || (!(w & (w-1)) && !(h & (h-1))));
Jason Samsc9d43db2009-07-28 12:02:16 -0700533
534 //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
Jason Samsa57c0a72009-09-04 14:42:41 -0700535 rsi_TypeBegin(rsc, _dst);
Jason Sams6678e9b2009-05-27 14:45:32 -0700536 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
537 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
538 if (genMips) {
539 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
540 }
541 RsType type = rsi_TypeCreate(rsc);
542
543 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
544 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
545 if (texAlloc == NULL) {
546 LOGE("Memory allocation failure");
547 return NULL;
548 }
Jason Sams6678e9b2009-05-27 14:45:32 -0700549
Jason Samsa57c0a72009-09-04 14:42:41 -0700550 ElementConverter_t cvt = pickConverter(dst, src);
Jason Sams6678e9b2009-05-27 14:45:32 -0700551 cvt(texAlloc->getPtr(), data, w * h);
552
553 if (genMips) {
Jason Samse514b452009-09-25 14:51:22 -0700554 Adapter2D adapt(rsc, texAlloc);
555 Adapter2D adapt2(rsc, texAlloc);
Jason Sams6678e9b2009-05-27 14:45:32 -0700556 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
557 adapt.setLOD(lod);
558 adapt2.setLOD(lod + 1);
Jason Samse9f5c532009-07-28 17:20:11 -0700559 mip(adapt2, adapt);
Jason Sams6678e9b2009-05-27 14:45:32 -0700560 }
561 }
562
563 return texAlloc;
564}
565
Jason Samsa57c0a72009-09-04 14:42:41 -0700566RsAllocation 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 -0700567{
Jason Samsa57c0a72009-09-04 14:42:41 -0700568 const Element *srcE = static_cast<const Element *>(_src);
569 const Element *dstE = static_cast<const Element *>(_dst);
Jason Samsc9d43db2009-07-28 12:02:16 -0700570 uint32_t w2 = rsHigherPow2(w);
571 uint32_t h2 = rsHigherPow2(h);
572
573 if ((w2 == w) && (h2 == h)) {
Jason Samsa57c0a72009-09-04 14:42:41 -0700574 return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data);
Jason Samsc9d43db2009-07-28 12:02:16 -0700575 }
576
Jason Samsa57c0a72009-09-04 14:42:41 -0700577 uint32_t bpp = srcE->getSizeBytes();
Jason Samsc9d43db2009-07-28 12:02:16 -0700578 size_t size = w2 * h2 * bpp;
579 uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
580 memset(tmp, 0, size);
581
582 const uint8_t * src = static_cast<const uint8_t *>(data);
583 for (uint32_t y = 0; y < h; y++) {
Jason Sams50253db2009-07-29 20:55:44 -0700584 uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
Marco Nelissen6cd833d2009-10-28 15:10:56 -0700585 memcpy(&ydst[((w2 - w) >> 1) * bpp], src, w * bpp);
Jason Sams50253db2009-07-29 20:55:44 -0700586 src += w * bpp;
Jason Samsc9d43db2009-07-28 12:02:16 -0700587 }
588
Jason Samsa57c0a72009-09-04 14:42:41 -0700589 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp);
Jason Samsc9d43db2009-07-28 12:02:16 -0700590 free(tmp);
591 return ret;
Jason Samsc9d43db2009-07-28 12:02:16 -0700592}
593
Jason Sams9397e302009-08-27 20:23:34 -0700594void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes)
Jason Sams326e0dd2009-05-22 14:03:28 -0700595{
596 Allocation *a = static_cast<Allocation *>(va);
Jason Sams9397e302009-08-27 20:23:34 -0700597 a->data(data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700598}
599
Jason Sams9397e302009-08-27 20:23:34 -0700600void 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 -0700601{
602 Allocation *a = static_cast<Allocation *>(va);
Jason Sams9397e302009-08-27 20:23:34 -0700603 a->subData(xoff, count, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700604}
605
Jason Sams9397e302009-08-27 20:23:34 -0700606void 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 -0700607{
608 Allocation *a = static_cast<Allocation *>(va);
Jason Sams9397e302009-08-27 20:23:34 -0700609 a->subData(xoff, yoff, w, h, data, sizeBytes);
Jason Sams326e0dd2009-05-22 14:03:28 -0700610}
611
Jason Samse579df42009-08-10 14:55:26 -0700612void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data)
613{
614 Allocation *a = static_cast<Allocation *>(va);
615 a->read(data);
616}
617
Jason Sams326e0dd2009-05-22 14:03:28 -0700618
619}
620}