blob: 5ec73d74ac893b791b9e761a126b2974ce8bba0d [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
25Allocation::Allocation(const Type *type)
26{
27 mPtr = NULL;
28
29 mCpuWrite = false;
30 mCpuRead = false;
31 mGpuWrite = false;
32 mGpuRead = false;
33
34 mReadWriteRatio = 0;
35 mUpdateSize = 0;
36
37 mIsTexture = false;
38 mTextureID = 0;
39
40 mIsVertexBuffer = false;
41 mBufferID = 0;
42
43 mType.set(type);
Jason Sams1bada8c2009-08-09 17:01:55 -070044 rsAssert(type);
Jason Samsd19f10d2009-05-22 14:03:28 -070045 mPtr = malloc(mType->getSizeBytes());
46 if (!mPtr) {
47 LOGE("Allocation::Allocation, alloc failure");
48 }
Jason Samsd19f10d2009-05-22 14:03:28 -070049}
50
51Allocation::~Allocation()
52{
53}
54
55void Allocation::setCpuWritable(bool)
56{
57}
58
59void Allocation::setGpuWritable(bool)
60{
61}
62
63void Allocation::setCpuReadable(bool)
64{
65}
66
67void Allocation::setGpuReadable(bool)
68{
69}
70
71bool Allocation::fixAllocation()
72{
73 return false;
74}
75
76void Allocation::uploadToTexture(uint32_t lodOffset)
77{
78 //rsAssert(!mTextureId);
79 rsAssert(lodOffset < mType->getLODCount());
80
Jason Samse2ae85f2009-06-03 16:04:54 -070081 GLenum type = mType->getElement()->getGLType();
82 GLenum format = mType->getElement()->getGLFormat();
83
84 if (!type || !format) {
85 return;
86 }
87
Jason Samsd19f10d2009-05-22 14:03:28 -070088 if (!mTextureID) {
89 glGenTextures(1, &mTextureID);
90 }
91 glBindTexture(GL_TEXTURE_2D, mTextureID);
92
93 Adapter2D adapt(this);
94 for(uint32_t lod = 0; (lod + lodOffset) < mType->getLODCount(); lod++) {
95 adapt.setLOD(lod+lodOffset);
96
97 uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
Jason Samse2ae85f2009-06-03 16:04:54 -070098 glTexImage2D(GL_TEXTURE_2D, lod, format,
99 adapt.getDimX(), adapt.getDimY(),
100 0, format, type, ptr);
Jason Samsd19f10d2009-05-22 14:03:28 -0700101 }
102}
103
104void Allocation::uploadToBufferObject()
105{
106 rsAssert(!mType->getDimY());
107 rsAssert(!mType->getDimZ());
108
Jason Samsd19f10d2009-05-22 14:03:28 -0700109 if (!mBufferID) {
110 glGenBuffers(1, &mBufferID);
111 }
112 glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
113 glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
114 glBindBuffer(GL_ARRAY_BUFFER, 0);
115}
116
Jason Sams1bada8c2009-08-09 17:01:55 -0700117
Jason Samsd19f10d2009-05-22 14:03:28 -0700118void Allocation::data(const void *data)
119{
120 memcpy(mPtr, data, mType->getSizeBytes());
121}
122
123void Allocation::subData(uint32_t xoff, uint32_t count, const void *data)
124{
125 uint32_t eSize = mType->getElementSizeBytes();
126 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
127 ptr += eSize * xoff;
128 memcpy(ptr, data, count * eSize);
129}
130
Jason Samse2ae85f2009-06-03 16:04:54 -0700131void Allocation::subData(uint32_t xoff, uint32_t yoff,
Jason Samsd19f10d2009-05-22 14:03:28 -0700132 uint32_t w, uint32_t h, const void *data)
133{
134 uint32_t eSize = mType->getElementSizeBytes();
135 uint32_t lineSize = eSize * w;
136 uint32_t destW = mType->getDimX();
137
138 const uint8_t *src = static_cast<const uint8_t *>(data);
139 uint8_t *dst = static_cast<uint8_t *>(mPtr);
140 dst += eSize * (xoff + yoff * destW);
141 for (uint32_t line=yoff; line < (yoff+h); line++) {
142 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
143 memcpy(dst, src, lineSize);
144 src += lineSize;
145 dst += destW * eSize;
146 }
147}
148
149void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
150 uint32_t w, uint32_t h, uint32_t d, const void *data)
151{
152}
153
154
155
156/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700157//
Jason Samsd19f10d2009-05-22 14:03:28 -0700158
159
160namespace android {
161namespace renderscript {
162
163RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
164{
165 const Type * type = static_cast<const Type *>(vtype);
166
167 Allocation * alloc = new Allocation(type);
Jason Sams4244afa2009-07-02 15:09:27 -0700168 alloc->incRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700169 return alloc;
170}
171
172RsAllocation rsi_AllocationCreatePredefSized(Context *rsc, RsElementPredefined t, size_t count)
173{
174 RsElement e = rsi_ElementGetPredefined(rsc, t);
175 return rsi_AllocationCreateSized(rsc, e, count);
176}
177
178RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
179{
180 Type * type = new Type();
181 type->setDimX(count);
182 type->setElement(static_cast<Element *>(e));
183 type->compute();
184 return rsi_AllocationCreateTyped(rsc, type);
185}
186
187void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel)
188{
189 Allocation *alloc = static_cast<Allocation *>(va);
190 alloc->uploadToTexture(baseMipLevel);
191}
192
193void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
194{
195 Allocation *alloc = static_cast<Allocation *>(va);
196 alloc->uploadToBufferObject();
197}
198
199void rsi_AllocationDestroy(Context *rsc, RsAllocation)
200{
201}
202
Jason Samse2ae85f2009-06-03 16:04:54 -0700203static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Samsd19f10d2009-05-22 14:03:28 -0700204{
205 uint32_t w = out.getDimX();
206 uint32_t h = out.getDimY();
207
Jason Sams6f5c61c2009-07-28 17:20:11 -0700208 for (uint32_t y=0; y < h; y++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700209 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
210 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
211 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
212
Jason Sams6f5c61c2009-07-28 17:20:11 -0700213 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700214 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
215 oPtr ++;
216 i1 += 2;
217 i2 += 2;
218 }
219 }
220}
221
222static void mip8888(const Adapter2D &out, const Adapter2D &in)
223{
224 uint32_t w = out.getDimX();
225 uint32_t h = out.getDimY();
226
Jason Sams6f5c61c2009-07-28 17:20:11 -0700227 for (uint32_t y=0; y < h; y++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700228 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
229 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
230 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
231
Jason Sams6f5c61c2009-07-28 17:20:11 -0700232 for (uint32_t x=0; x < w; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700233 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700234 oPtr ++;
235 i1 += 2;
236 i2 += 2;
237 }
238 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700239}
240
Jason Sams6f5c61c2009-07-28 17:20:11 -0700241static void mip(const Adapter2D &out, const Adapter2D &in)
242{
243 switch(out.getBaseType()->getElement()->getSizeBits()) {
244 case 32:
245 mip8888(out, in);
246 break;
247 case 16:
248 mip565(out, in);
249 break;
250
251 }
252
253}
Jason Samsd19f10d2009-05-22 14:03:28 -0700254
Jason Samsfe08d992009-05-27 14:45:32 -0700255typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
256
257static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
258{
259 memcpy(dst, src, count * 2);
260}
261static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
262{
263 memcpy(dst, src, count);
264}
265static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
266{
267 memcpy(dst, src, count * 4);
268}
269
270
271static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
272{
273 uint16_t *d = static_cast<uint16_t *>(dst);
274 const uint8_t *s = static_cast<const uint8_t *>(src);
275
276 while(count--) {
277 *d = rs888to565(s[0], s[1], s[2]);
278 d++;
279 s+= 3;
280 }
281}
282
283static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
284{
285 uint16_t *d = static_cast<uint16_t *>(dst);
286 const uint8_t *s = static_cast<const uint8_t *>(src);
287
288 while(count--) {
289 *d = rs888to565(s[0], s[1], s[2]);
290 d++;
291 s+= 4;
292 }
293}
294
295static ElementConverter_t pickConverter(RsElementPredefined dstFmt, RsElementPredefined srcFmt)
296{
Jason Samse2ae85f2009-06-03 16:04:54 -0700297 if ((dstFmt == RS_ELEMENT_RGB_565) &&
Jason Samsfe08d992009-05-27 14:45:32 -0700298 (srcFmt == RS_ELEMENT_RGB_565)) {
299 return elementConverter_cpy_16;
300 }
301
Jason Samse2ae85f2009-06-03 16:04:54 -0700302 if ((dstFmt == RS_ELEMENT_RGB_565) &&
Jason Samsfe08d992009-05-27 14:45:32 -0700303 (srcFmt == RS_ELEMENT_RGB_888)) {
304 return elementConverter_888_to_565;
305 }
306
Jason Samse2ae85f2009-06-03 16:04:54 -0700307 if ((dstFmt == RS_ELEMENT_RGB_565) &&
Jason Samsfe08d992009-05-27 14:45:32 -0700308 (srcFmt == RS_ELEMENT_RGBA_8888)) {
309 return elementConverter_8888_to_565;
310 }
311
Jason Samse2ae85f2009-06-03 16:04:54 -0700312 if ((dstFmt == RS_ELEMENT_RGBA_8888) &&
313 (srcFmt == RS_ELEMENT_RGBA_8888)) {
314 return elementConverter_cpy_32;
315 }
Jason Samsfe08d992009-05-27 14:45:32 -0700316
Jason Samsb0ec1b42009-07-28 12:02:16 -0700317 LOGE("pickConverter, unsuported combo, src %i, dst %i", srcFmt, dstFmt);
Jason Samsfe08d992009-05-27 14:45:32 -0700318 return 0;
319}
320
321
322RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElementPredefined dstFmt, RsElementPredefined srcFmt, bool genMips, const void *data)
323{
Jason Samsb0ec1b42009-07-28 12:02:16 -0700324 rsAssert(!(w & (w-1)));
325 rsAssert(!(h & (h-1)));
326
327 //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
328 rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, dstFmt));
Jason Samsfe08d992009-05-27 14:45:32 -0700329 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
330 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
331 if (genMips) {
332 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
333 }
334 RsType type = rsi_TypeCreate(rsc);
335
336 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
337 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
338 if (texAlloc == NULL) {
339 LOGE("Memory allocation failure");
340 return NULL;
341 }
342 texAlloc->incRef();
343
344 ElementConverter_t cvt = pickConverter(dstFmt, srcFmt);
345 cvt(texAlloc->getPtr(), data, w * h);
346
347 if (genMips) {
348 Adapter2D adapt(texAlloc);
349 Adapter2D adapt2(texAlloc);
350 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
351 adapt.setLOD(lod);
352 adapt2.setLOD(lod + 1);
Jason Sams6f5c61c2009-07-28 17:20:11 -0700353 mip(adapt2, adapt);
Jason Samsfe08d992009-05-27 14:45:32 -0700354 }
355 }
356
357 return texAlloc;
358}
359
Jason Samsb0ec1b42009-07-28 12:02:16 -0700360static uint32_t fmtToBits(RsElementPredefined fmt)
361{
362 return 16;
363}
364
365RsAllocation rsi_AllocationCreateFromBitmapBoxed(Context *rsc, uint32_t w, uint32_t h, RsElementPredefined dstFmt, RsElementPredefined srcFmt, bool genMips, const void *data)
366{
367 uint32_t w2 = rsHigherPow2(w);
368 uint32_t h2 = rsHigherPow2(h);
369
370 if ((w2 == w) && (h2 == h)) {
371 return rsi_AllocationCreateFromBitmap(rsc, w, h, dstFmt, srcFmt, genMips, data);
372 }
373
374 uint32_t bpp = fmtToBits(srcFmt) >> 3;
375 size_t size = w2 * h2 * bpp;
376 uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
377 memset(tmp, 0, size);
378
379 const uint8_t * src = static_cast<const uint8_t *>(data);
380 for (uint32_t y = 0; y < h; y++) {
Jason Samsfaf15202009-07-29 20:55:44 -0700381 uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
Jason Samsb0ec1b42009-07-28 12:02:16 -0700382 memcpy(&ydst[(w2 - w) >> 1], src, w * bpp);
Jason Samsfaf15202009-07-29 20:55:44 -0700383 src += w * bpp;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700384 }
385
386 RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, dstFmt, srcFmt, genMips, tmp);
387 free(tmp);
388 return ret;
389
390
391
392
393}
394
395
Jason Samsfe08d992009-05-27 14:45:32 -0700396RsAllocation rsi_AllocationCreateFromFile(Context *rsc, const char *file, bool genMips)
Jason Samsd19f10d2009-05-22 14:03:28 -0700397{
Jason Samse2ae85f2009-06-03 16:04:54 -0700398 bool use32bpp = false;
399
Jason Samsd19f10d2009-05-22 14:03:28 -0700400 typedef struct _Win3xBitmapHeader
401 {
402 uint16_t type;
403 uint32_t totalSize;
404 uint32_t reserved;
405 uint32_t offset;
406 int32_t hdrSize; /* Size of this header in bytes */
407 int32_t width; /* Image width in pixels */
408 int32_t height; /* Image height in pixels */
409 int16_t planes; /* Number of color planes */
410 int16_t bpp; /* Number of bits per pixel */
411 /* Fields added for Windows 3.x follow this line */
412 int32_t compression; /* Compression methods used */
413 int32_t sizeOfBitmap; /* Size of bitmap in bytes */
414 int32_t horzResolution; /* Horizontal resolution in pixels per meter */
415 int32_t vertResolution; /* Vertical resolution in pixels per meter */
416 int32_t colorsUsed; /* Number of colors in the image */
417 int32_t colorsImportant; /* Minimum number of important colors */
418 } __attribute__((__packed__)) WIN3XBITMAPHEADER;
419
420 _Win3xBitmapHeader hdr;
421
422 FILE *f = fopen(file, "rb");
423 if (f == NULL) {
424 LOGE("rsAllocationCreateFromBitmap failed to open file %s", file);
425 return NULL;
426 }
427 memset(&hdr, 0, sizeof(hdr));
428 fread(&hdr, sizeof(hdr), 1, f);
429
430 if (hdr.bpp != 24) {
431 LOGE("Unsuported BMP type");
432 fclose(f);
433 return NULL;
434 }
435
436 int32_t texWidth = rsHigherPow2(hdr.width);
437 int32_t texHeight = rsHigherPow2(hdr.height);
438
Jason Samse2ae85f2009-06-03 16:04:54 -0700439 if (use32bpp) {
440 rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGBA_8888));
441 } else {
442 rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGB_565));
443 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700444 rsi_TypeAdd(rsc, RS_DIMENSION_X, texWidth);
445 rsi_TypeAdd(rsc, RS_DIMENSION_Y, texHeight);
446 if (genMips) {
447 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
448 }
449 RsType type = rsi_TypeCreate(rsc);
450
451 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
452 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
453 texAlloc->incRef();
454 if (texAlloc == NULL) {
455 LOGE("Memory allocation failure");
456 fclose(f);
457 return NULL;
458 }
459
460 // offset to letterbox if height is not pow2
461 Adapter2D adapt(texAlloc);
462 uint8_t * fileInBuf = new uint8_t[texWidth * 3];
463 uint32_t yOffset = (hdr.width - hdr.height) / 2;
Jason Samsd19f10d2009-05-22 14:03:28 -0700464
Jason Samse2ae85f2009-06-03 16:04:54 -0700465 if (use32bpp) {
466 uint8_t *tmp = static_cast<uint8_t *>(adapt.getElement(0, yOffset));
467 for (int y=0; y < hdr.height; y++) {
468 fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET);
469 fread(fileInBuf, 1, hdr.width * 3, f);
470 for(int x=0; x < hdr.width; x++) {
471 tmp[0] = fileInBuf[x*3 + 2];
472 tmp[1] = fileInBuf[x*3 + 1];
473 tmp[2] = fileInBuf[x*3];
474 tmp[3] = 0xff;
475 tmp += 4;
476 }
477 }
478 } else {
479 uint16_t *tmp = static_cast<uint16_t *>(adapt.getElement(0, yOffset));
480 for (int y=0; y < hdr.height; y++) {
481 fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET);
482 fread(fileInBuf, 1, hdr.width * 3, f);
483 for(int x=0; x < hdr.width; x++) {
484 *tmp = rs888to565(fileInBuf[x*3 + 2], fileInBuf[x*3 + 1], fileInBuf[x*3]);
485 tmp++;
486 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700487 }
488 }
489
490 fclose(f);
491 delete [] fileInBuf;
492
493 if (genMips) {
494 Adapter2D adapt2(texAlloc);
495 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
496 adapt.setLOD(lod);
497 adapt2.setLOD(lod + 1);
Jason Sams6f5c61c2009-07-28 17:20:11 -0700498 mip(adapt2, adapt);
Jason Samsd19f10d2009-05-22 14:03:28 -0700499 }
500 }
501
502 return texAlloc;
503}
504
Jason Samsd19f10d2009-05-22 14:03:28 -0700505void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data)
506{
507 Allocation *a = static_cast<Allocation *>(va);
508 a->data(data);
Jason Sams9bee51c2009-08-05 13:57:03 -0700509 rsc->allocationCheck(a);
Jason Samsd19f10d2009-05-22 14:03:28 -0700510}
511
512void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data)
513{
514 Allocation *a = static_cast<Allocation *>(va);
515 a->subData(xoff, count, data);
Jason Sams9bee51c2009-08-05 13:57:03 -0700516 rsc->allocationCheck(a);
Jason Samsd19f10d2009-05-22 14:03:28 -0700517}
518
519void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data)
520{
521 Allocation *a = static_cast<Allocation *>(va);
522 a->subData(xoff, yoff, w, h, data);
Jason Sams9bee51c2009-08-05 13:57:03 -0700523 rsc->allocationCheck(a);
Jason Samsd19f10d2009-05-22 14:03:28 -0700524}
525
526
527}
528}