blob: 4e8278d798402b896193d9cdb71c93df67f9bc66 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsContext.h"
18
Jason Sams4b962e52009-06-22 17:15:15 -070019#include <GLES/gl.h>
Jason Samsc2908e62010-02-23 17:44:28 -080020#include <GLES2/gl2.h>
Jason Sams4b962e52009-06-22 17:15:15 -070021#include <GLES/glext.h>
22
Jason Samsd19f10d2009-05-22 14:03:28 -070023using namespace android;
24using namespace android::renderscript;
25
Jason Samsa9e7a052009-09-25 14:51:22 -070026Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070027{
Jason Sams8a647432010-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 Sams61f08d62009-09-25 16:37:33 -070049 mAllocFile = __FILE__;
50 mAllocLine = __LINE__;
Jason Samsd19f10d2009-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 Samsd19f10d2009-05-22 14:03:28 -070063 mIsVertexBuffer = false;
64 mBufferID = 0;
Jason Sams3b7d39b2009-12-14 12:57:40 -080065 mUploadDefered = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070066
Jason Sams8a647432010-03-01 15:31:04 -080067 mUserBitmapCallback = NULL;
68 mUserBitmapCallbackData = NULL;
69
Jason Samsd19f10d2009-05-22 14:03:28 -070070 mType.set(type);
Jason Sams1bada8c2009-08-09 17:01:55 -070071 rsAssert(type);
Jason Sams8a647432010-03-01 15:31:04 -080072
73 mPtr = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -070074}
75
76Allocation::~Allocation()
77{
Jason Sams8a647432010-03-01 15:31:04 -080078 if (mUserBitmapCallback != NULL) {
79 mUserBitmapCallback(mUserBitmapCallbackData);
80 } else {
81 free(mPtr);
82 }
Jason Samsb7a6c432009-11-02 14:25:10 -080083 mPtr = NULL;
Jason Sams9d5e03d2009-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 Samsd19f10d2009-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 Samsc2908e62010-02-23 17:44:28 -0800118void Allocation::deferedUploadToTexture(const Context *rsc, bool genMipmap, uint32_t lodOffset)
Jason Sams3b7d39b2009-12-14 12:57:40 -0800119{
120 rsAssert(lodOffset < mType->getLODCount());
121 mIsTexture = true;
122 mTextureLOD = lodOffset;
123 mUploadDefered = true;
Jason Samsc2908e62010-02-23 17:44:28 -0800124 mTextureGenMipmap = !mType->getDimLOD() && genMipmap;
Jason Sams3b7d39b2009-12-14 12:57:40 -0800125}
126
127void Allocation::uploadToTexture(const Context *rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -0700128{
129 //rsAssert(!mTextureId);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800130
131 mIsTexture = true;
132 if (!rsc->checkDriver()) {
133 mUploadDefered = true;
134 return;
135 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700136
Jason Sams718cd1f2009-12-23 14:35:29 -0800137 GLenum type = mType->getElement()->getComponent().getGLType();
138 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Samse2ae85f2009-06-03 16:04:54 -0700139
140 if (!type || !format) {
141 return;
142 }
143
Jason Samsd19f10d2009-05-22 14:03:28 -0700144 if (!mTextureID) {
145 glGenTextures(1, &mTextureID);
Jason Sams9dab6672009-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 Sams3b7d39b2009-12-14 12:57:40 -0800153 mUploadDefered = true;
154 return;
Jason Sams9dab6672009-11-24 12:26:35 -0800155 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700156 }
157 glBindTexture(GL_TEXTURE_2D, mTextureID);
Jason Sams0e27b5c2009-11-05 12:44:58 -0800158 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -0700159
Jason Samsa9e7a052009-09-25 14:51:22 -0700160 Adapter2D adapt(getContext(), this);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800161 for(uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
162 adapt.setLOD(lod+mTextureLOD);
Jason Samsd19f10d2009-05-22 14:03:28 -0700163
164 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Jason Samse2ae85f2009-06-03 16:04:54 -0700165 glTexImage2D(GL_TEXTURE_2D, lod, format,
166 adapt.getDimX(), adapt.getDimY(),
167 0, format, type, ptr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700168 }
Jason Samsc2908e62010-02-23 17:44:28 -0800169 if (mTextureGenMipmap) {
170 glGenerateMipmap(GL_TEXTURE_2D);
171 }
172
Jason Samsd19f10d2009-05-22 14:03:28 -0700173}
174
Jason Sams3b7d39b2009-12-14 12:57:40 -0800175void Allocation::deferedUploadToBufferObject(const Context *rsc)
176{
177 mIsVertexBuffer = true;
178 mUploadDefered = true;
179}
180
181void Allocation::uploadToBufferObject(const Context *rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -0700182{
183 rsAssert(!mType->getDimY());
184 rsAssert(!mType->getDimZ());
185
Jason Sams3b7d39b2009-12-14 12:57:40 -0800186 mIsVertexBuffer = true;
187 if (!rsc->checkDriver()) {
188 mUploadDefered = true;
189 return;
190 }
191
Jason Samsd19f10d2009-05-22 14:03:28 -0700192 if (!mBufferID) {
193 glGenBuffers(1, &mBufferID);
194 }
Jason Sams3b7d39b2009-12-14 12:57:40 -0800195 if (!mBufferID) {
196 LOGE("Upload to buffer object failed");
197 mUploadDefered = true;
198 return;
199 }
200
Jason Samsd19f10d2009-05-22 14:03:28 -0700201 glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
202 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
203 glBindBuffer(GL_ARRAY_BUFFER, 0);
204}
205
Jason Sams3b7d39b2009-12-14 12:57:40 -0800206void Allocation::uploadCheck(const Context *rsc)
207{
208 if (mUploadDefered) {
209 mUploadDefered = false;
210 if (mIsVertexBuffer) {
211 uploadToBufferObject(rsc);
212 }
213 if (mIsTexture) {
214 uploadToTexture(rsc);
215 }
216 }
217}
218
Jason Sams1bada8c2009-08-09 17:01:55 -0700219
Jason Sams07ae4062009-08-27 20:23:34 -0700220void Allocation::data(const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700221{
Jason Sams07ae4062009-08-27 20:23:34 -0700222 uint32_t size = mType->getSizeBytes();
223 if (size != sizeBytes) {
224 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
225 return;
226 }
227 memcpy(mPtr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700228 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800229 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700230}
231
Jason Sams40a29e82009-08-10 14:55:26 -0700232void Allocation::read(void *data)
233{
234 memcpy(data, mPtr, mType->getSizeBytes());
235}
236
Jason Sams07ae4062009-08-27 20:23:34 -0700237void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700238{
239 uint32_t eSize = mType->getElementSizeBytes();
240 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
241 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700242 uint32_t size = count * eSize;
243
244 if (size != sizeBytes) {
245 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Sams3c0dfba2009-09-27 17:50:38 -0700246 mType->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -0700247 return;
248 }
249 memcpy(ptr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700250 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800251 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700252}
253
Jason Samse2ae85f2009-06-03 16:04:54 -0700254void Allocation::subData(uint32_t xoff, uint32_t yoff,
Jason Sams07ae4062009-08-27 20:23:34 -0700255 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700256{
257 uint32_t eSize = mType->getElementSizeBytes();
258 uint32_t lineSize = eSize * w;
259 uint32_t destW = mType->getDimX();
260
261 const uint8_t *src = static_cast<const uint8_t *>(data);
262 uint8_t *dst = static_cast<uint8_t *>(mPtr);
263 dst += eSize * (xoff + yoff * destW);
Jason Sams07ae4062009-08-27 20:23:34 -0700264
265 if ((lineSize * eSize * h) != sizeBytes) {
266 rsAssert(!"Allocation::subData called with mismatched size");
267 return;
268 }
269
Jason Samsd19f10d2009-05-22 14:03:28 -0700270 for (uint32_t line=yoff; line < (yoff+h); line++) {
271 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
272 memcpy(dst, src, lineSize);
273 src += lineSize;
274 dst += destW * eSize;
275 }
Jason Sams83f1c632009-10-26 15:19:28 -0700276 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800277 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700278}
279
280void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams07ae4062009-08-27 20:23:34 -0700281 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700282{
283}
284
Jason Sams83f1c632009-10-26 15:19:28 -0700285void Allocation::addProgramToDirty(const Program *p)
286{
287 mToDirtyList.add(p);
288}
Jason Samsd19f10d2009-05-22 14:03:28 -0700289
Jason Sams83f1c632009-10-26 15:19:28 -0700290void Allocation::removeProgramToDirty(const Program *p)
291{
292 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
293 if (mToDirtyList[ct] == p) {
294 mToDirtyList.removeAt(ct);
295 return;
296 }
297 }
298 rsAssert(0);
299}
300
Jason Sams715333b2009-11-17 17:26:46 -0800301void Allocation::dumpLOGV(const char *prefix) const
302{
303 ObjectBase::dumpLOGV(prefix);
304
305 String8 s(prefix);
306 s.append(" type ");
307 if (mType.get()) {
308 mType->dumpLOGV(s.string());
309 }
310
311 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
312 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
313
Jason Sams361765362009-11-23 15:27:33 -0800314 LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i",
Jason Sams715333b2009-11-17 17:26:46 -0800315 prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID);
316
Jason Sams715333b2009-11-17 17:26:46 -0800317}
Jason Samsd19f10d2009-05-22 14:03:28 -0700318
Jason Sams83f1c632009-10-26 15:19:28 -0700319void Allocation::sendDirty() const
320{
321 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
322 mToDirtyList[ct]->forceDirty();
323 }
324}
Jason Samsd19f10d2009-05-22 14:03:28 -0700325
326/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700327//
Jason Samsd19f10d2009-05-22 14:03:28 -0700328
329
330namespace android {
331namespace renderscript {
332
333RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
334{
335 const Type * type = static_cast<const Type *>(vtype);
336
Jason Samsa9e7a052009-09-25 14:51:22 -0700337 Allocation * alloc = new Allocation(rsc, type);
Jason Sams07ae4062009-08-27 20:23:34 -0700338 alloc->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700339 return alloc;
340}
341
Jason Samsd19f10d2009-05-22 14:03:28 -0700342RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
343{
Jason Samsa9e7a052009-09-25 14:51:22 -0700344 Type * type = new Type(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700345 type->setDimX(count);
346 type->setElement(static_cast<Element *>(e));
347 type->compute();
348 return rsi_AllocationCreateTyped(rsc, type);
349}
350
Jason Samsc2908e62010-02-23 17:44:28 -0800351void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel)
Jason Samsd19f10d2009-05-22 14:03:28 -0700352{
353 Allocation *alloc = static_cast<Allocation *>(va);
Jason Samsc2908e62010-02-23 17:44:28 -0800354 alloc->deferedUploadToTexture(rsc, genmip, baseMipLevel);
Jason Samsd19f10d2009-05-22 14:03:28 -0700355}
356
357void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
358{
359 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800360 alloc->deferedUploadToBufferObject(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700361}
362
Jason Samse2ae85f2009-06-03 16:04:54 -0700363static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Samsd19f10d2009-05-22 14:03:28 -0700364{
365 uint32_t w = out.getDimX();
366 uint32_t h = out.getDimY();
367
Jason Sams6f5c61c2009-07-28 17:20:11 -0700368 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700369 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
370 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
371 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
372
Jason Sams6f5c61c2009-07-28 17:20:11 -0700373 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700374 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
375 oPtr ++;
376 i1 += 2;
377 i2 += 2;
378 }
379 }
380}
381
382static void mip8888(const Adapter2D &out, const Adapter2D &in)
383{
384 uint32_t w = out.getDimX();
385 uint32_t h = out.getDimY();
386
Jason Sams6f5c61c2009-07-28 17:20:11 -0700387 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700388 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
389 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
390 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
391
Jason Sams6f5c61c2009-07-28 17:20:11 -0700392 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700393 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700394 oPtr ++;
395 i1 += 2;
396 i2 += 2;
397 }
398 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700399}
400
Jason Samse20e3b42010-01-19 17:53:54 -0800401static void mip8(const Adapter2D &out, const Adapter2D &in)
402{
403 uint32_t w = out.getDimX();
404 uint32_t h = out.getDimY();
405
406 for (uint32_t y=0; y < h; y++) {
407 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
408 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
409 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
410
411 for (uint32_t x=0; x < w; x++) {
412 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
413 oPtr ++;
414 i1 += 2;
415 i2 += 2;
416 }
417 }
418}
419
Jason Sams6f5c61c2009-07-28 17:20:11 -0700420static void mip(const Adapter2D &out, const Adapter2D &in)
421{
422 switch(out.getBaseType()->getElement()->getSizeBits()) {
423 case 32:
424 mip8888(out, in);
425 break;
426 case 16:
427 mip565(out, in);
428 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800429 case 8:
430 mip8(out, in);
431 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700432
433 }
434
435}
Jason Samsd19f10d2009-05-22 14:03:28 -0700436
Jason Samsfe08d992009-05-27 14:45:32 -0700437typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
438
439static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
440{
441 memcpy(dst, src, count * 2);
442}
443static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
444{
445 memcpy(dst, src, count);
446}
447static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
448{
449 memcpy(dst, src, count * 4);
450}
451
452
453static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
454{
455 uint16_t *d = static_cast<uint16_t *>(dst);
456 const uint8_t *s = static_cast<const uint8_t *>(src);
457
458 while(count--) {
459 *d = rs888to565(s[0], s[1], s[2]);
460 d++;
461 s+= 3;
462 }
463}
464
465static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
466{
467 uint16_t *d = static_cast<uint16_t *>(dst);
468 const uint8_t *s = static_cast<const uint8_t *>(src);
469
470 while(count--) {
471 *d = rs888to565(s[0], s[1], s[2]);
472 d++;
473 s+= 4;
474 }
475}
476
Jason Samsea84a7c2009-09-04 14:42:41 -0700477static ElementConverter_t pickConverter(const Element *dst, const Element *src)
Jason Samsfe08d992009-05-27 14:45:32 -0700478{
Jason Sams718cd1f2009-12-23 14:35:29 -0800479 GLenum srcGLType = src->getComponent().getGLType();
480 GLenum srcGLFmt = src->getComponent().getGLFormat();
481 GLenum dstGLType = dst->getComponent().getGLType();
482 GLenum dstGLFmt = dst->getComponent().getGLFormat();
Jason Samsea84a7c2009-09-04 14:42:41 -0700483
484 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
485 switch(dst->getSizeBytes()) {
486 case 4:
487 return elementConverter_cpy_32;
488 case 2:
489 return elementConverter_cpy_16;
490 case 1:
491 return elementConverter_cpy_8;
492 }
Jason Samsfe08d992009-05-27 14:45:32 -0700493 }
494
Jason Samsea84a7c2009-09-04 14:42:41 -0700495 if (srcGLType == GL_UNSIGNED_BYTE &&
496 srcGLFmt == GL_RGB &&
497 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
498 dstGLType == GL_RGB) {
499
Jason Samsfe08d992009-05-27 14:45:32 -0700500 return elementConverter_888_to_565;
501 }
502
Jason Samsea84a7c2009-09-04 14:42:41 -0700503 if (srcGLType == GL_UNSIGNED_BYTE &&
504 srcGLFmt == GL_RGBA &&
505 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
506 dstGLType == GL_RGB) {
507
Jason Samsfe08d992009-05-27 14:45:32 -0700508 return elementConverter_8888_to_565;
509 }
510
Jason Samsea84a7c2009-09-04 14:42:41 -0700511 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst);
Jason Samsfe08d992009-05-27 14:45:32 -0700512 return 0;
513}
514
Jason Sams8a647432010-03-01 15:31:04 -0800515RsAllocation rsi_AllocationCreateBitmapRef(Context *rsc, RsType vtype,
516 void *bmp, void *callbackData, RsBitmapCallback_t callback)
517{
518 const Type * type = static_cast<const Type *>(vtype);
519 Allocation * alloc = new Allocation(rsc, type, bmp, callbackData, callback);
520 alloc->incUserRef();
521 return alloc;
522}
Jason Samsfe08d992009-05-27 14:45:32 -0700523
Jason Samsea84a7c2009-09-04 14:42:41 -0700524RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data)
Jason Samsfe08d992009-05-27 14:45:32 -0700525{
Jason Samsea84a7c2009-09-04 14:42:41 -0700526 const Element *src = static_cast<const Element *>(_src);
527 const Element *dst = static_cast<const Element *>(_dst);
Jason Sams74e02ef2010-01-06 15:10:29 -0800528
529 // Check for pow2 on pre es 2.0 versions.
530 rsAssert(rsc->checkVersion2_0() || (!(w & (w-1)) && !(h & (h-1))));
Jason Samsb0ec1b42009-07-28 12:02:16 -0700531
532 //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
Jason Samsea84a7c2009-09-04 14:42:41 -0700533 rsi_TypeBegin(rsc, _dst);
Jason Samsfe08d992009-05-27 14:45:32 -0700534 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
535 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
536 if (genMips) {
537 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
538 }
539 RsType type = rsi_TypeCreate(rsc);
540
541 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
542 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
543 if (texAlloc == NULL) {
544 LOGE("Memory allocation failure");
545 return NULL;
546 }
Jason Samsfe08d992009-05-27 14:45:32 -0700547
Jason Samsea84a7c2009-09-04 14:42:41 -0700548 ElementConverter_t cvt = pickConverter(dst, src);
Jason Samsfe08d992009-05-27 14:45:32 -0700549 cvt(texAlloc->getPtr(), data, w * h);
550
551 if (genMips) {
Jason Samsa9e7a052009-09-25 14:51:22 -0700552 Adapter2D adapt(rsc, texAlloc);
553 Adapter2D adapt2(rsc, texAlloc);
Jason Samsfe08d992009-05-27 14:45:32 -0700554 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
555 adapt.setLOD(lod);
556 adapt2.setLOD(lod + 1);
Jason Sams6f5c61c2009-07-28 17:20:11 -0700557 mip(adapt2, adapt);
Jason Samsfe08d992009-05-27 14:45:32 -0700558 }
559 }
560
561 return texAlloc;
562}
563
Jason Samsea84a7c2009-09-04 14:42:41 -0700564RsAllocation rsi_AllocationCreateFromBitmapBoxed(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data)
Jason Samsb0ec1b42009-07-28 12:02:16 -0700565{
Jason Samsea84a7c2009-09-04 14:42:41 -0700566 const Element *srcE = static_cast<const Element *>(_src);
567 const Element *dstE = static_cast<const Element *>(_dst);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700568 uint32_t w2 = rsHigherPow2(w);
569 uint32_t h2 = rsHigherPow2(h);
570
571 if ((w2 == w) && (h2 == h)) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700572 return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700573 }
574
Jason Samsea84a7c2009-09-04 14:42:41 -0700575 uint32_t bpp = srcE->getSizeBytes();
Jason Samsb0ec1b42009-07-28 12:02:16 -0700576 size_t size = w2 * h2 * bpp;
577 uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
578 memset(tmp, 0, size);
579
580 const uint8_t * src = static_cast<const uint8_t *>(data);
581 for (uint32_t y = 0; y < h; y++) {
Jason Samsfaf15202009-07-29 20:55:44 -0700582 uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
Marco Nelissen911458a2009-10-28 15:10:56 -0700583 memcpy(&ydst[((w2 - w) >> 1) * bpp], src, w * bpp);
Jason Samsfaf15202009-07-29 20:55:44 -0700584 src += w * bpp;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700585 }
586
Jason Samsea84a7c2009-09-04 14:42:41 -0700587 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700588 free(tmp);
589 return ret;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700590}
591
Jason Sams07ae4062009-08-27 20:23:34 -0700592void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700593{
594 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700595 a->data(data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700596}
597
Jason Sams07ae4062009-08-27 20:23:34 -0700598void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700599{
600 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700601 a->subData(xoff, count, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700602}
603
Jason Sams07ae4062009-08-27 20:23:34 -0700604void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700605{
606 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700607 a->subData(xoff, yoff, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700608}
609
Jason Sams40a29e82009-08-10 14:55:26 -0700610void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data)
611{
612 Allocation *a = static_cast<Allocation *>(va);
613 a->read(data);
614}
615
Jason Samsd19f10d2009-05-22 14:03:28 -0700616
617}
618}