blob: bc14eac9b17de885402c32b3cbcaba1c37f61a0f [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);
44 mPtr = malloc(mType->getSizeBytes());
45 if (!mPtr) {
46 LOGE("Allocation::Allocation, alloc failure");
47 }
48
49}
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
117void Allocation::data(const void *data)
118{
119 memcpy(mPtr, data, mType->getSizeBytes());
120}
121
122void Allocation::subData(uint32_t xoff, uint32_t count, const void *data)
123{
124 uint32_t eSize = mType->getElementSizeBytes();
125 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
126 ptr += eSize * xoff;
127 memcpy(ptr, data, count * eSize);
128}
129
Jason Samse2ae85f2009-06-03 16:04:54 -0700130void Allocation::subData(uint32_t xoff, uint32_t yoff,
Jason Samsd19f10d2009-05-22 14:03:28 -0700131 uint32_t w, uint32_t h, const void *data)
132{
133 uint32_t eSize = mType->getElementSizeBytes();
134 uint32_t lineSize = eSize * w;
135 uint32_t destW = mType->getDimX();
136
137 const uint8_t *src = static_cast<const uint8_t *>(data);
138 uint8_t *dst = static_cast<uint8_t *>(mPtr);
139 dst += eSize * (xoff + yoff * destW);
140 for (uint32_t line=yoff; line < (yoff+h); line++) {
141 uint8_t * ptr = static_cast<uint8_t *>(mPtr);
142 memcpy(dst, src, lineSize);
143 src += lineSize;
144 dst += destW * eSize;
145 }
146}
147
148void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
149 uint32_t w, uint32_t h, uint32_t d, const void *data)
150{
151}
152
153
154
155/////////////////
Jason Samse2ae85f2009-06-03 16:04:54 -0700156//
Jason Samsd19f10d2009-05-22 14:03:28 -0700157
158
159namespace android {
160namespace renderscript {
161
162RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
163{
164 const Type * type = static_cast<const Type *>(vtype);
165
166 Allocation * alloc = new Allocation(type);
167 return alloc;
168}
169
170RsAllocation rsi_AllocationCreatePredefSized(Context *rsc, RsElementPredefined t, size_t count)
171{
172 RsElement e = rsi_ElementGetPredefined(rsc, t);
173 return rsi_AllocationCreateSized(rsc, e, count);
174}
175
176RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
177{
178 Type * type = new Type();
179 type->setDimX(count);
180 type->setElement(static_cast<Element *>(e));
181 type->compute();
182 return rsi_AllocationCreateTyped(rsc, type);
183}
184
185void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel)
186{
187 Allocation *alloc = static_cast<Allocation *>(va);
188 alloc->uploadToTexture(baseMipLevel);
189}
190
191void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
192{
193 Allocation *alloc = static_cast<Allocation *>(va);
194 alloc->uploadToBufferObject();
195}
196
197void rsi_AllocationDestroy(Context *rsc, RsAllocation)
198{
199}
200
Jason Samse2ae85f2009-06-03 16:04:54 -0700201static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Samsd19f10d2009-05-22 14:03:28 -0700202{
203 uint32_t w = out.getDimX();
204 uint32_t h = out.getDimY();
205
206 for (uint32_t y=0; y < w; y++) {
207 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
208 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
209 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
210
211 for (uint32_t x=0; x < h; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700212 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
213 oPtr ++;
214 i1 += 2;
215 i2 += 2;
216 }
217 }
218}
219
220static void mip8888(const Adapter2D &out, const Adapter2D &in)
221{
222 uint32_t w = out.getDimX();
223 uint32_t h = out.getDimY();
224
225 for (uint32_t y=0; y < w; y++) {
226 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
227 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
228 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
229
230 for (uint32_t x=0; x < h; x++) {
231 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700232 oPtr ++;
233 i1 += 2;
234 i2 += 2;
235 }
236 }
237
238}
239
240
Jason Samsfe08d992009-05-27 14:45:32 -0700241typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
242
243static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
244{
245 memcpy(dst, src, count * 2);
246}
247static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
248{
249 memcpy(dst, src, count);
250}
251static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
252{
253 memcpy(dst, src, count * 4);
254}
255
256
257static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
258{
259 uint16_t *d = static_cast<uint16_t *>(dst);
260 const uint8_t *s = static_cast<const uint8_t *>(src);
261
262 while(count--) {
263 *d = rs888to565(s[0], s[1], s[2]);
264 d++;
265 s+= 3;
266 }
267}
268
269static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
270{
271 uint16_t *d = static_cast<uint16_t *>(dst);
272 const uint8_t *s = static_cast<const uint8_t *>(src);
273
274 while(count--) {
275 *d = rs888to565(s[0], s[1], s[2]);
276 d++;
277 s+= 4;
278 }
279}
280
281static ElementConverter_t pickConverter(RsElementPredefined dstFmt, RsElementPredefined srcFmt)
282{
Jason Samse2ae85f2009-06-03 16:04:54 -0700283 if ((dstFmt == RS_ELEMENT_RGB_565) &&
Jason Samsfe08d992009-05-27 14:45:32 -0700284 (srcFmt == RS_ELEMENT_RGB_565)) {
285 return elementConverter_cpy_16;
286 }
287
Jason Samse2ae85f2009-06-03 16:04:54 -0700288 if ((dstFmt == RS_ELEMENT_RGB_565) &&
Jason Samsfe08d992009-05-27 14:45:32 -0700289 (srcFmt == RS_ELEMENT_RGB_888)) {
290 return elementConverter_888_to_565;
291 }
292
Jason Samse2ae85f2009-06-03 16:04:54 -0700293 if ((dstFmt == RS_ELEMENT_RGB_565) &&
Jason Samsfe08d992009-05-27 14:45:32 -0700294 (srcFmt == RS_ELEMENT_RGBA_8888)) {
295 return elementConverter_8888_to_565;
296 }
297
Jason Samse2ae85f2009-06-03 16:04:54 -0700298 if ((dstFmt == RS_ELEMENT_RGBA_8888) &&
299 (srcFmt == RS_ELEMENT_RGBA_8888)) {
300 return elementConverter_cpy_32;
301 }
Jason Samsfe08d992009-05-27 14:45:32 -0700302
303 LOGE("pickConverter, unsuported combo");
304 return 0;
305}
306
307
308RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElementPredefined dstFmt, RsElementPredefined srcFmt, bool genMips, const void *data)
309{
310 rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGB_565));
311 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
312 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
313 if (genMips) {
314 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
315 }
316 RsType type = rsi_TypeCreate(rsc);
317
318 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
319 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
320 if (texAlloc == NULL) {
321 LOGE("Memory allocation failure");
322 return NULL;
323 }
324 texAlloc->incRef();
325
326 ElementConverter_t cvt = pickConverter(dstFmt, srcFmt);
327 cvt(texAlloc->getPtr(), data, w * h);
328
329 if (genMips) {
330 Adapter2D adapt(texAlloc);
331 Adapter2D adapt2(texAlloc);
332 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
333 adapt.setLOD(lod);
334 adapt2.setLOD(lod + 1);
Jason Samse2ae85f2009-06-03 16:04:54 -0700335 mip565(adapt2, adapt);
Jason Samsfe08d992009-05-27 14:45:32 -0700336 }
337 }
338
339 return texAlloc;
340}
341
342RsAllocation rsi_AllocationCreateFromFile(Context *rsc, const char *file, bool genMips)
Jason Samsd19f10d2009-05-22 14:03:28 -0700343{
Jason Samse2ae85f2009-06-03 16:04:54 -0700344 bool use32bpp = false;
345
Jason Samsd19f10d2009-05-22 14:03:28 -0700346 typedef struct _Win3xBitmapHeader
347 {
348 uint16_t type;
349 uint32_t totalSize;
350 uint32_t reserved;
351 uint32_t offset;
352 int32_t hdrSize; /* Size of this header in bytes */
353 int32_t width; /* Image width in pixels */
354 int32_t height; /* Image height in pixels */
355 int16_t planes; /* Number of color planes */
356 int16_t bpp; /* Number of bits per pixel */
357 /* Fields added for Windows 3.x follow this line */
358 int32_t compression; /* Compression methods used */
359 int32_t sizeOfBitmap; /* Size of bitmap in bytes */
360 int32_t horzResolution; /* Horizontal resolution in pixels per meter */
361 int32_t vertResolution; /* Vertical resolution in pixels per meter */
362 int32_t colorsUsed; /* Number of colors in the image */
363 int32_t colorsImportant; /* Minimum number of important colors */
364 } __attribute__((__packed__)) WIN3XBITMAPHEADER;
365
366 _Win3xBitmapHeader hdr;
367
368 FILE *f = fopen(file, "rb");
369 if (f == NULL) {
370 LOGE("rsAllocationCreateFromBitmap failed to open file %s", file);
371 return NULL;
372 }
373 memset(&hdr, 0, sizeof(hdr));
374 fread(&hdr, sizeof(hdr), 1, f);
375
376 if (hdr.bpp != 24) {
377 LOGE("Unsuported BMP type");
378 fclose(f);
379 return NULL;
380 }
381
382 int32_t texWidth = rsHigherPow2(hdr.width);
383 int32_t texHeight = rsHigherPow2(hdr.height);
384
Jason Samse2ae85f2009-06-03 16:04:54 -0700385 if (use32bpp) {
386 rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGBA_8888));
387 } else {
388 rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGB_565));
389 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700390 rsi_TypeAdd(rsc, RS_DIMENSION_X, texWidth);
391 rsi_TypeAdd(rsc, RS_DIMENSION_Y, texHeight);
392 if (genMips) {
393 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
394 }
395 RsType type = rsi_TypeCreate(rsc);
396
397 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
398 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
399 texAlloc->incRef();
400 if (texAlloc == NULL) {
401 LOGE("Memory allocation failure");
402 fclose(f);
403 return NULL;
404 }
405
406 // offset to letterbox if height is not pow2
407 Adapter2D adapt(texAlloc);
408 uint8_t * fileInBuf = new uint8_t[texWidth * 3];
409 uint32_t yOffset = (hdr.width - hdr.height) / 2;
Jason Samsd19f10d2009-05-22 14:03:28 -0700410
Jason Samse2ae85f2009-06-03 16:04:54 -0700411 if (use32bpp) {
412 uint8_t *tmp = static_cast<uint8_t *>(adapt.getElement(0, yOffset));
413 for (int y=0; y < hdr.height; y++) {
414 fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET);
415 fread(fileInBuf, 1, hdr.width * 3, f);
416 for(int x=0; x < hdr.width; x++) {
417 tmp[0] = fileInBuf[x*3 + 2];
418 tmp[1] = fileInBuf[x*3 + 1];
419 tmp[2] = fileInBuf[x*3];
420 tmp[3] = 0xff;
421 tmp += 4;
422 }
423 }
424 } else {
425 uint16_t *tmp = static_cast<uint16_t *>(adapt.getElement(0, yOffset));
426 for (int y=0; y < hdr.height; y++) {
427 fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET);
428 fread(fileInBuf, 1, hdr.width * 3, f);
429 for(int x=0; x < hdr.width; x++) {
430 *tmp = rs888to565(fileInBuf[x*3 + 2], fileInBuf[x*3 + 1], fileInBuf[x*3]);
431 tmp++;
432 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700433 }
434 }
435
436 fclose(f);
437 delete [] fileInBuf;
438
439 if (genMips) {
440 Adapter2D adapt2(texAlloc);
441 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
442 adapt.setLOD(lod);
443 adapt2.setLOD(lod + 1);
Jason Samse2ae85f2009-06-03 16:04:54 -0700444 if (use32bpp) {
445 mip8888(adapt2, adapt);
446 } else {
447 mip565(adapt2, adapt);
448 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700449 }
450 }
451
452 return texAlloc;
453}
454
455
456void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data)
457{
458 Allocation *a = static_cast<Allocation *>(va);
459 a->data(data);
460}
461
462void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data)
463{
464 Allocation *a = static_cast<Allocation *>(va);
465 a->subData(xoff, count, data);
466}
467
468void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data)
469{
470 Allocation *a = static_cast<Allocation *>(va);
471 a->subData(xoff, yoff, w, h, data);
472}
473
474
475}
476}