blob: 1ae231761701eed34c98be47c1ca6d95d9ec803c [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>
20#include <GLES/glext.h>
21
Jason Samsd19f10d2009-05-22 14:03:28 -070022using namespace android;
23using namespace android::renderscript;
24
Jason Samsa9e7a052009-09-25 14:51:22 -070025Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070026{
Jason Sams61f08d62009-09-25 16:37:33 -070027 mAllocFile = __FILE__;
28 mAllocLine = __LINE__;
Jason Samsd19f10d2009-05-22 14:03:28 -070029 mPtr = NULL;
30
31 mCpuWrite = false;
32 mCpuRead = false;
33 mGpuWrite = false;
34 mGpuRead = false;
35
36 mReadWriteRatio = 0;
37 mUpdateSize = 0;
38
39 mIsTexture = false;
40 mTextureID = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070041 mIsVertexBuffer = false;
42 mBufferID = 0;
Jason Sams3b7d39b2009-12-14 12:57:40 -080043 mUploadDefered = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070044
45 mType.set(type);
Jason Sams1bada8c2009-08-09 17:01:55 -070046 rsAssert(type);
Jason Samsd19f10d2009-05-22 14:03:28 -070047 mPtr = malloc(mType->getSizeBytes());
48 if (!mPtr) {
49 LOGE("Allocation::Allocation, alloc failure");
50 }
Jason Samsd19f10d2009-05-22 14:03:28 -070051}
52
53Allocation::~Allocation()
54{
Jason Samsb7a6c432009-11-02 14:25:10 -080055 free(mPtr);
56 mPtr = NULL;
Jason Sams9d5e03d2009-11-03 11:25:42 -080057
58 if (mBufferID) {
59 // Causes a SW crash....
60 //LOGV(" mBufferID %i", mBufferID);
61 //glDeleteBuffers(1, &mBufferID);
62 //mBufferID = 0;
63 }
64 if (mTextureID) {
65 glDeleteTextures(1, &mTextureID);
66 mTextureID = 0;
67 }
Jason Samsd19f10d2009-05-22 14:03:28 -070068}
69
70void Allocation::setCpuWritable(bool)
71{
72}
73
74void Allocation::setGpuWritable(bool)
75{
76}
77
78void Allocation::setCpuReadable(bool)
79{
80}
81
82void Allocation::setGpuReadable(bool)
83{
84}
85
86bool Allocation::fixAllocation()
87{
88 return false;
89}
90
Jason Sams3b7d39b2009-12-14 12:57:40 -080091void Allocation::deferedUploadToTexture(const Context *rsc, uint32_t lodOffset)
92{
93 rsAssert(lodOffset < mType->getLODCount());
94 mIsTexture = true;
95 mTextureLOD = lodOffset;
96 mUploadDefered = true;
97}
98
99void Allocation::uploadToTexture(const Context *rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -0700100{
101 //rsAssert(!mTextureId);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800102
103 mIsTexture = true;
104 if (!rsc->checkDriver()) {
105 mUploadDefered = true;
106 return;
107 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700108
Jason Sams718cd1f2009-12-23 14:35:29 -0800109 GLenum type = mType->getElement()->getComponent().getGLType();
110 GLenum format = mType->getElement()->getComponent().getGLFormat();
Jason Samse2ae85f2009-06-03 16:04:54 -0700111
112 if (!type || !format) {
113 return;
114 }
115
Jason Samsd19f10d2009-05-22 14:03:28 -0700116 if (!mTextureID) {
117 glGenTextures(1, &mTextureID);
Jason Sams9dab6672009-11-24 12:26:35 -0800118
119 if (!mTextureID) {
120 // This should not happen, however, its likely the cause of the
121 // white sqare bug.
122 // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
123 LOGE("Upload to texture failed to gen mTextureID");
124 rsc->dumpDebug();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800125 mUploadDefered = true;
126 return;
Jason Sams9dab6672009-11-24 12:26:35 -0800127 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700128 }
129 glBindTexture(GL_TEXTURE_2D, mTextureID);
Jason Sams0e27b5c2009-11-05 12:44:58 -0800130 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Jason Samsd19f10d2009-05-22 14:03:28 -0700131
Jason Samsa9e7a052009-09-25 14:51:22 -0700132 Adapter2D adapt(getContext(), this);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800133 for(uint32_t lod = 0; (lod + mTextureLOD) < mType->getLODCount(); lod++) {
134 adapt.setLOD(lod+mTextureLOD);
Jason Samsd19f10d2009-05-22 14:03:28 -0700135
136 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Jason Samse2ae85f2009-06-03 16:04:54 -0700137 glTexImage2D(GL_TEXTURE_2D, lod, format,
138 adapt.getDimX(), adapt.getDimY(),
139 0, format, type, ptr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700140 }
141}
142
Jason Sams3b7d39b2009-12-14 12:57:40 -0800143void Allocation::deferedUploadToBufferObject(const Context *rsc)
144{
145 mIsVertexBuffer = true;
146 mUploadDefered = true;
147}
148
149void Allocation::uploadToBufferObject(const Context *rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -0700150{
151 rsAssert(!mType->getDimY());
152 rsAssert(!mType->getDimZ());
153
Jason Sams3b7d39b2009-12-14 12:57:40 -0800154 mIsVertexBuffer = true;
155 if (!rsc->checkDriver()) {
156 mUploadDefered = true;
157 return;
158 }
159
Jason Samsd19f10d2009-05-22 14:03:28 -0700160 if (!mBufferID) {
161 glGenBuffers(1, &mBufferID);
162 }
Jason Sams3b7d39b2009-12-14 12:57:40 -0800163 if (!mBufferID) {
164 LOGE("Upload to buffer object failed");
165 mUploadDefered = true;
166 return;
167 }
168
Jason Samsd19f10d2009-05-22 14:03:28 -0700169 glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
170 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
171 glBindBuffer(GL_ARRAY_BUFFER, 0);
172}
173
Jason Sams3b7d39b2009-12-14 12:57:40 -0800174void Allocation::uploadCheck(const Context *rsc)
175{
176 if (mUploadDefered) {
177 mUploadDefered = false;
178 if (mIsVertexBuffer) {
179 uploadToBufferObject(rsc);
180 }
181 if (mIsTexture) {
182 uploadToTexture(rsc);
183 }
184 }
185}
186
Jason Sams1bada8c2009-08-09 17:01:55 -0700187
Jason Sams07ae4062009-08-27 20:23:34 -0700188void Allocation::data(const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700189{
Jason Sams07ae4062009-08-27 20:23:34 -0700190 uint32_t size = mType->getSizeBytes();
191 if (size != sizeBytes) {
192 LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
193 return;
194 }
195 memcpy(mPtr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700196 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800197 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700198}
199
Jason Sams40a29e82009-08-10 14:55:26 -0700200void Allocation::read(void *data)
201{
202 memcpy(data, mPtr, mType->getSizeBytes());
203}
204
Jason Sams07ae4062009-08-27 20:23:34 -0700205void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700206{
207 uint32_t eSize = mType->getElementSizeBytes();
208 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
209 ptr += eSize * xoff;
Jason Sams07ae4062009-08-27 20:23:34 -0700210 uint32_t size = count * eSize;
211
212 if (size != sizeBytes) {
213 LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
Jason Sams3c0dfba2009-09-27 17:50:38 -0700214 mType->dumpLOGV("type info");
Jason Sams07ae4062009-08-27 20:23:34 -0700215 return;
216 }
217 memcpy(ptr, data, size);
Jason Sams83f1c632009-10-26 15:19:28 -0700218 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800219 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700220}
221
Jason Samse2ae85f2009-06-03 16:04:54 -0700222void Allocation::subData(uint32_t xoff, uint32_t yoff,
Jason Sams07ae4062009-08-27 20:23:34 -0700223 uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700224{
225 uint32_t eSize = mType->getElementSizeBytes();
226 uint32_t lineSize = eSize * w;
227 uint32_t destW = mType->getDimX();
228
229 const uint8_t *src = static_cast<const uint8_t *>(data);
230 uint8_t *dst = static_cast<uint8_t *>(mPtr);
231 dst += eSize * (xoff + yoff * destW);
Jason Sams07ae4062009-08-27 20:23:34 -0700232
233 if ((lineSize * eSize * h) != sizeBytes) {
234 rsAssert(!"Allocation::subData called with mismatched size");
235 return;
236 }
237
Jason Samsd19f10d2009-05-22 14:03:28 -0700238 for (uint32_t line=yoff; line < (yoff+h); line++) {
239 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
240 memcpy(dst, src, lineSize);
241 src += lineSize;
242 dst += destW * eSize;
243 }
Jason Sams83f1c632009-10-26 15:19:28 -0700244 sendDirty();
Jason Sams3b7d39b2009-12-14 12:57:40 -0800245 mUploadDefered = true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700246}
247
248void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams07ae4062009-08-27 20:23:34 -0700249 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700250{
251}
252
Jason Sams83f1c632009-10-26 15:19:28 -0700253void Allocation::addProgramToDirty(const Program *p)
254{
255 mToDirtyList.add(p);
256}
Jason Samsd19f10d2009-05-22 14:03:28 -0700257
Jason Sams83f1c632009-10-26 15:19:28 -0700258void Allocation::removeProgramToDirty(const Program *p)
259{
260 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
261 if (mToDirtyList[ct] == p) {
262 mToDirtyList.removeAt(ct);
263 return;
264 }
265 }
266 rsAssert(0);
267}
268
Jason Sams715333b2009-11-17 17:26:46 -0800269void Allocation::dumpLOGV(const char *prefix) const
270{
271 ObjectBase::dumpLOGV(prefix);
272
273 String8 s(prefix);
274 s.append(" type ");
275 if (mType.get()) {
276 mType->dumpLOGV(s.string());
277 }
278
279 LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
280 prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
281
Jason Sams361765362009-11-23 15:27:33 -0800282 LOGV("%s allocation mIsTexture=%i mTextureID=%i, mIsVertexBuffer=%i, mBufferID=%i",
Jason Sams715333b2009-11-17 17:26:46 -0800283 prefix, mIsTexture, mTextureID, mIsVertexBuffer, mBufferID);
284
Jason Sams715333b2009-11-17 17:26:46 -0800285}
Jason Samsd19f10d2009-05-22 14:03:28 -0700286
Jason Sams83f1c632009-10-26 15:19:28 -0700287void Allocation::sendDirty() const
288{
289 for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
290 mToDirtyList[ct]->forceDirty();
291 }
292}
Jason Samsd19f10d2009-05-22 14:03:28 -0700293
294/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700295//
Jason Samsd19f10d2009-05-22 14:03:28 -0700296
297
298namespace android {
299namespace renderscript {
300
301RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
302{
303 const Type * type = static_cast<const Type *>(vtype);
304
Jason Samsa9e7a052009-09-25 14:51:22 -0700305 Allocation * alloc = new Allocation(rsc, type);
Jason Sams07ae4062009-08-27 20:23:34 -0700306 alloc->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700307 return alloc;
308}
309
Jason Samsd19f10d2009-05-22 14:03:28 -0700310RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
311{
Jason Samsa9e7a052009-09-25 14:51:22 -0700312 Type * type = new Type(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700313 type->setDimX(count);
314 type->setElement(static_cast<Element *>(e));
315 type->compute();
316 return rsi_AllocationCreateTyped(rsc, type);
317}
318
319void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel)
320{
321 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800322 alloc->deferedUploadToTexture(rsc, baseMipLevel);
Jason Samsd19f10d2009-05-22 14:03:28 -0700323}
324
325void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
326{
327 Allocation *alloc = static_cast<Allocation *>(va);
Jason Sams3b7d39b2009-12-14 12:57:40 -0800328 alloc->deferedUploadToBufferObject(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700329}
330
Jason Samse2ae85f2009-06-03 16:04:54 -0700331static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Samsd19f10d2009-05-22 14:03:28 -0700332{
333 uint32_t w = out.getDimX();
334 uint32_t h = out.getDimY();
335
Jason Sams6f5c61c2009-07-28 17:20:11 -0700336 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700337 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
338 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
339 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
340
Jason Sams6f5c61c2009-07-28 17:20:11 -0700341 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700342 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
343 oPtr ++;
344 i1 += 2;
345 i2 += 2;
346 }
347 }
348}
349
350static void mip8888(const Adapter2D &out, const Adapter2D &in)
351{
352 uint32_t w = out.getDimX();
353 uint32_t h = out.getDimY();
354
Jason Sams6f5c61c2009-07-28 17:20:11 -0700355 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700356 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
357 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
358 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
359
Jason Sams6f5c61c2009-07-28 17:20:11 -0700360 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700361 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700362 oPtr ++;
363 i1 += 2;
364 i2 += 2;
365 }
366 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700367}
368
Jason Samse20e3b42010-01-19 17:53:54 -0800369static void mip8(const Adapter2D &out, const Adapter2D &in)
370{
371 uint32_t w = out.getDimX();
372 uint32_t h = out.getDimY();
373
374 for (uint32_t y=0; y < h; y++) {
375 uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
376 const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
377 const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
378
379 for (uint32_t x=0; x < w; x++) {
380 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
381 oPtr ++;
382 i1 += 2;
383 i2 += 2;
384 }
385 }
386}
387
Jason Sams6f5c61c2009-07-28 17:20:11 -0700388static void mip(const Adapter2D &out, const Adapter2D &in)
389{
390 switch(out.getBaseType()->getElement()->getSizeBits()) {
391 case 32:
392 mip8888(out, in);
393 break;
394 case 16:
395 mip565(out, in);
396 break;
Jason Samse20e3b42010-01-19 17:53:54 -0800397 case 8:
398 mip8(out, in);
399 break;
Jason Sams6f5c61c2009-07-28 17:20:11 -0700400
401 }
402
403}
Jason Samsd19f10d2009-05-22 14:03:28 -0700404
Jason Samsfe08d992009-05-27 14:45:32 -0700405typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
406
407static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
408{
409 memcpy(dst, src, count * 2);
410}
411static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
412{
413 memcpy(dst, src, count);
414}
415static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
416{
417 memcpy(dst, src, count * 4);
418}
419
420
421static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
422{
423 uint16_t *d = static_cast<uint16_t *>(dst);
424 const uint8_t *s = static_cast<const uint8_t *>(src);
425
426 while(count--) {
427 *d = rs888to565(s[0], s[1], s[2]);
428 d++;
429 s+= 3;
430 }
431}
432
433static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
434{
435 uint16_t *d = static_cast<uint16_t *>(dst);
436 const uint8_t *s = static_cast<const uint8_t *>(src);
437
438 while(count--) {
439 *d = rs888to565(s[0], s[1], s[2]);
440 d++;
441 s+= 4;
442 }
443}
444
Jason Samsea84a7c2009-09-04 14:42:41 -0700445static ElementConverter_t pickConverter(const Element *dst, const Element *src)
Jason Samsfe08d992009-05-27 14:45:32 -0700446{
Jason Sams718cd1f2009-12-23 14:35:29 -0800447 GLenum srcGLType = src->getComponent().getGLType();
448 GLenum srcGLFmt = src->getComponent().getGLFormat();
449 GLenum dstGLType = dst->getComponent().getGLType();
450 GLenum dstGLFmt = dst->getComponent().getGLFormat();
Jason Samsea84a7c2009-09-04 14:42:41 -0700451
452 if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
453 switch(dst->getSizeBytes()) {
454 case 4:
455 return elementConverter_cpy_32;
456 case 2:
457 return elementConverter_cpy_16;
458 case 1:
459 return elementConverter_cpy_8;
460 }
Jason Samsfe08d992009-05-27 14:45:32 -0700461 }
462
Jason Samsea84a7c2009-09-04 14:42:41 -0700463 if (srcGLType == GL_UNSIGNED_BYTE &&
464 srcGLFmt == GL_RGB &&
465 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
466 dstGLType == GL_RGB) {
467
Jason Samsfe08d992009-05-27 14:45:32 -0700468 return elementConverter_888_to_565;
469 }
470
Jason Samsea84a7c2009-09-04 14:42:41 -0700471 if (srcGLType == GL_UNSIGNED_BYTE &&
472 srcGLFmt == GL_RGBA &&
473 dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
474 dstGLType == GL_RGB) {
475
Jason Samsfe08d992009-05-27 14:45:32 -0700476 return elementConverter_8888_to_565;
477 }
478
Jason Samsea84a7c2009-09-04 14:42:41 -0700479 LOGE("pickConverter, unsuported combo, src %p, dst %p", src, dst);
Jason Samsfe08d992009-05-27 14:45:32 -0700480 return 0;
481}
482
483
Jason Samsea84a7c2009-09-04 14:42:41 -0700484RsAllocation 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 -0700485{
Jason Samsea84a7c2009-09-04 14:42:41 -0700486 const Element *src = static_cast<const Element *>(_src);
487 const Element *dst = static_cast<const Element *>(_dst);
Jason Sams74e02ef2010-01-06 15:10:29 -0800488
489 // Check for pow2 on pre es 2.0 versions.
490 rsAssert(rsc->checkVersion2_0() || (!(w & (w-1)) && !(h & (h-1))));
Jason Samsb0ec1b42009-07-28 12:02:16 -0700491
492 //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
Jason Samsea84a7c2009-09-04 14:42:41 -0700493 rsi_TypeBegin(rsc, _dst);
Jason Samsfe08d992009-05-27 14:45:32 -0700494 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
495 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
496 if (genMips) {
497 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
498 }
499 RsType type = rsi_TypeCreate(rsc);
500
501 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
502 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
503 if (texAlloc == NULL) {
504 LOGE("Memory allocation failure");
505 return NULL;
506 }
Jason Samsfe08d992009-05-27 14:45:32 -0700507
Jason Samsea84a7c2009-09-04 14:42:41 -0700508 ElementConverter_t cvt = pickConverter(dst, src);
Jason Samsfe08d992009-05-27 14:45:32 -0700509 cvt(texAlloc->getPtr(), data, w * h);
510
511 if (genMips) {
Jason Samsa9e7a052009-09-25 14:51:22 -0700512 Adapter2D adapt(rsc, texAlloc);
513 Adapter2D adapt2(rsc, texAlloc);
Jason Samsfe08d992009-05-27 14:45:32 -0700514 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
515 adapt.setLOD(lod);
516 adapt2.setLOD(lod + 1);
Jason Sams6f5c61c2009-07-28 17:20:11 -0700517 mip(adapt2, adapt);
Jason Samsfe08d992009-05-27 14:45:32 -0700518 }
519 }
520
521 return texAlloc;
522}
523
Jason Samsea84a7c2009-09-04 14:42:41 -0700524RsAllocation 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 -0700525{
Jason Samsea84a7c2009-09-04 14:42:41 -0700526 const Element *srcE = static_cast<const Element *>(_src);
527 const Element *dstE = static_cast<const Element *>(_dst);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700528 uint32_t w2 = rsHigherPow2(w);
529 uint32_t h2 = rsHigherPow2(h);
530
531 if ((w2 == w) && (h2 == h)) {
Jason Samsea84a7c2009-09-04 14:42:41 -0700532 return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700533 }
534
Jason Samsea84a7c2009-09-04 14:42:41 -0700535 uint32_t bpp = srcE->getSizeBytes();
Jason Samsb0ec1b42009-07-28 12:02:16 -0700536 size_t size = w2 * h2 * bpp;
537 uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
538 memset(tmp, 0, size);
539
540 const uint8_t * src = static_cast<const uint8_t *>(data);
541 for (uint32_t y = 0; y < h; y++) {
Jason Samsfaf15202009-07-29 20:55:44 -0700542 uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
Marco Nelissen911458a2009-10-28 15:10:56 -0700543 memcpy(&ydst[((w2 - w) >> 1) * bpp], src, w * bpp);
Jason Samsfaf15202009-07-29 20:55:44 -0700544 src += w * bpp;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700545 }
546
Jason Samsea84a7c2009-09-04 14:42:41 -0700547 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700548 free(tmp);
549 return ret;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700550}
551
Jason Sams07ae4062009-08-27 20:23:34 -0700552void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes)
Jason Samsd19f10d2009-05-22 14:03:28 -0700553{
554 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700555 a->data(data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700556}
557
Jason Sams07ae4062009-08-27 20:23:34 -0700558void 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 -0700559{
560 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700561 a->subData(xoff, count, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700562}
563
Jason Sams07ae4062009-08-27 20:23:34 -0700564void 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 -0700565{
566 Allocation *a = static_cast<Allocation *>(va);
Jason Sams07ae4062009-08-27 20:23:34 -0700567 a->subData(xoff, yoff, w, h, data, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -0700568}
569
Jason Sams40a29e82009-08-10 14:55:26 -0700570void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data)
571{
572 Allocation *a = static_cast<Allocation *>(va);
573 a->read(data);
574}
575
Jason Samsd19f10d2009-05-22 14:03:28 -0700576
577}
578}