blob: c143307bd54934e76a825f683cfd2a3d29cfbabd [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);
Jason Sams4244afa2009-07-02 15:09:27 -0700167 alloc->incRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700168 return alloc;
169}
170
171RsAllocation rsi_AllocationCreatePredefSized(Context *rsc, RsElementPredefined t, size_t count)
172{
173 RsElement e = rsi_ElementGetPredefined(rsc, t);
174 return rsi_AllocationCreateSized(rsc, e, count);
175}
176
177RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
178{
179 Type * type = new Type();
180 type->setDimX(count);
181 type->setElement(static_cast<Element *>(e));
182 type->compute();
183 return rsi_AllocationCreateTyped(rsc, type);
184}
185
186void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel)
187{
188 Allocation *alloc = static_cast<Allocation *>(va);
189 alloc->uploadToTexture(baseMipLevel);
190}
191
192void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
193{
194 Allocation *alloc = static_cast<Allocation *>(va);
195 alloc->uploadToBufferObject();
196}
197
198void rsi_AllocationDestroy(Context *rsc, RsAllocation)
199{
200}
201
Jason Samse2ae85f2009-06-03 16:04:54 -0700202static void mip565(const Adapter2D &out, const Adapter2D &in)
Jason Samsd19f10d2009-05-22 14:03:28 -0700203{
204 uint32_t w = out.getDimX();
205 uint32_t h = out.getDimY();
206
207 for (uint32_t y=0; y < w; y++) {
208 uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
209 const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
210 const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
211
212 for (uint32_t x=0; x < h; x++) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700213 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
214 oPtr ++;
215 i1 += 2;
216 i2 += 2;
217 }
218 }
219}
220
221static void mip8888(const Adapter2D &out, const Adapter2D &in)
222{
223 uint32_t w = out.getDimX();
224 uint32_t h = out.getDimY();
225
226 for (uint32_t y=0; y < w; y++) {
227 uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
228 const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
229 const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
230
231 for (uint32_t x=0; x < h; x++) {
232 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
Jason Samsd19f10d2009-05-22 14:03:28 -0700233 oPtr ++;
234 i1 += 2;
235 i2 += 2;
236 }
237 }
238
239}
240
241
Jason Samsfe08d992009-05-27 14:45:32 -0700242typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
243
244static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
245{
246 memcpy(dst, src, count * 2);
247}
248static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
249{
250 memcpy(dst, src, count);
251}
252static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
253{
254 memcpy(dst, src, count * 4);
255}
256
257
258static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
259{
260 uint16_t *d = static_cast<uint16_t *>(dst);
261 const uint8_t *s = static_cast<const uint8_t *>(src);
262
263 while(count--) {
264 *d = rs888to565(s[0], s[1], s[2]);
265 d++;
266 s+= 3;
267 }
268}
269
270static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
271{
272 uint16_t *d = static_cast<uint16_t *>(dst);
273 const uint8_t *s = static_cast<const uint8_t *>(src);
274
275 while(count--) {
276 *d = rs888to565(s[0], s[1], s[2]);
277 d++;
278 s+= 4;
279 }
280}
281
282static ElementConverter_t pickConverter(RsElementPredefined dstFmt, RsElementPredefined srcFmt)
283{
Jason Samse2ae85f2009-06-03 16:04:54 -0700284 if ((dstFmt == RS_ELEMENT_RGB_565) &&
Jason Samsfe08d992009-05-27 14:45:32 -0700285 (srcFmt == RS_ELEMENT_RGB_565)) {
286 return elementConverter_cpy_16;
287 }
288
Jason Samse2ae85f2009-06-03 16:04:54 -0700289 if ((dstFmt == RS_ELEMENT_RGB_565) &&
Jason Samsfe08d992009-05-27 14:45:32 -0700290 (srcFmt == RS_ELEMENT_RGB_888)) {
291 return elementConverter_888_to_565;
292 }
293
Jason Samse2ae85f2009-06-03 16:04:54 -0700294 if ((dstFmt == RS_ELEMENT_RGB_565) &&
Jason Samsfe08d992009-05-27 14:45:32 -0700295 (srcFmt == RS_ELEMENT_RGBA_8888)) {
296 return elementConverter_8888_to_565;
297 }
298
Jason Samse2ae85f2009-06-03 16:04:54 -0700299 if ((dstFmt == RS_ELEMENT_RGBA_8888) &&
300 (srcFmt == RS_ELEMENT_RGBA_8888)) {
301 return elementConverter_cpy_32;
302 }
Jason Samsfe08d992009-05-27 14:45:32 -0700303
304 LOGE("pickConverter, unsuported combo");
305 return 0;
306}
307
308
309RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElementPredefined dstFmt, RsElementPredefined srcFmt, bool genMips, const void *data)
310{
311 rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGB_565));
312 rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
313 rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
314 if (genMips) {
315 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
316 }
317 RsType type = rsi_TypeCreate(rsc);
318
319 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
320 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
321 if (texAlloc == NULL) {
322 LOGE("Memory allocation failure");
323 return NULL;
324 }
325 texAlloc->incRef();
326
327 ElementConverter_t cvt = pickConverter(dstFmt, srcFmt);
328 cvt(texAlloc->getPtr(), data, w * h);
329
330 if (genMips) {
331 Adapter2D adapt(texAlloc);
332 Adapter2D adapt2(texAlloc);
333 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
334 adapt.setLOD(lod);
335 adapt2.setLOD(lod + 1);
Jason Samse2ae85f2009-06-03 16:04:54 -0700336 mip565(adapt2, adapt);
Jason Samsfe08d992009-05-27 14:45:32 -0700337 }
338 }
339
340 return texAlloc;
341}
342
343RsAllocation rsi_AllocationCreateFromFile(Context *rsc, const char *file, bool genMips)
Jason Samsd19f10d2009-05-22 14:03:28 -0700344{
Jason Samse2ae85f2009-06-03 16:04:54 -0700345 bool use32bpp = false;
346
Jason Samsd19f10d2009-05-22 14:03:28 -0700347 typedef struct _Win3xBitmapHeader
348 {
349 uint16_t type;
350 uint32_t totalSize;
351 uint32_t reserved;
352 uint32_t offset;
353 int32_t hdrSize; /* Size of this header in bytes */
354 int32_t width; /* Image width in pixels */
355 int32_t height; /* Image height in pixels */
356 int16_t planes; /* Number of color planes */
357 int16_t bpp; /* Number of bits per pixel */
358 /* Fields added for Windows 3.x follow this line */
359 int32_t compression; /* Compression methods used */
360 int32_t sizeOfBitmap; /* Size of bitmap in bytes */
361 int32_t horzResolution; /* Horizontal resolution in pixels per meter */
362 int32_t vertResolution; /* Vertical resolution in pixels per meter */
363 int32_t colorsUsed; /* Number of colors in the image */
364 int32_t colorsImportant; /* Minimum number of important colors */
365 } __attribute__((__packed__)) WIN3XBITMAPHEADER;
366
367 _Win3xBitmapHeader hdr;
368
369 FILE *f = fopen(file, "rb");
370 if (f == NULL) {
371 LOGE("rsAllocationCreateFromBitmap failed to open file %s", file);
372 return NULL;
373 }
374 memset(&hdr, 0, sizeof(hdr));
375 fread(&hdr, sizeof(hdr), 1, f);
376
377 if (hdr.bpp != 24) {
378 LOGE("Unsuported BMP type");
379 fclose(f);
380 return NULL;
381 }
382
383 int32_t texWidth = rsHigherPow2(hdr.width);
384 int32_t texHeight = rsHigherPow2(hdr.height);
385
Jason Samse2ae85f2009-06-03 16:04:54 -0700386 if (use32bpp) {
387 rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGBA_8888));
388 } else {
389 rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGB_565));
390 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700391 rsi_TypeAdd(rsc, RS_DIMENSION_X, texWidth);
392 rsi_TypeAdd(rsc, RS_DIMENSION_Y, texHeight);
393 if (genMips) {
394 rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
395 }
396 RsType type = rsi_TypeCreate(rsc);
397
398 RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
399 Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
400 texAlloc->incRef();
401 if (texAlloc == NULL) {
402 LOGE("Memory allocation failure");
403 fclose(f);
404 return NULL;
405 }
406
407 // offset to letterbox if height is not pow2
408 Adapter2D adapt(texAlloc);
409 uint8_t * fileInBuf = new uint8_t[texWidth * 3];
410 uint32_t yOffset = (hdr.width - hdr.height) / 2;
Jason Samsd19f10d2009-05-22 14:03:28 -0700411
Jason Samse2ae85f2009-06-03 16:04:54 -0700412 if (use32bpp) {
413 uint8_t *tmp = static_cast<uint8_t *>(adapt.getElement(0, yOffset));
414 for (int y=0; y < hdr.height; y++) {
415 fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET);
416 fread(fileInBuf, 1, hdr.width * 3, f);
417 for(int x=0; x < hdr.width; x++) {
418 tmp[0] = fileInBuf[x*3 + 2];
419 tmp[1] = fileInBuf[x*3 + 1];
420 tmp[2] = fileInBuf[x*3];
421 tmp[3] = 0xff;
422 tmp += 4;
423 }
424 }
425 } else {
426 uint16_t *tmp = static_cast<uint16_t *>(adapt.getElement(0, yOffset));
427 for (int y=0; y < hdr.height; y++) {
428 fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET);
429 fread(fileInBuf, 1, hdr.width * 3, f);
430 for(int x=0; x < hdr.width; x++) {
431 *tmp = rs888to565(fileInBuf[x*3 + 2], fileInBuf[x*3 + 1], fileInBuf[x*3]);
432 tmp++;
433 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700434 }
435 }
436
437 fclose(f);
438 delete [] fileInBuf;
439
440 if (genMips) {
441 Adapter2D adapt2(texAlloc);
442 for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
443 adapt.setLOD(lod);
444 adapt2.setLOD(lod + 1);
Jason Samse2ae85f2009-06-03 16:04:54 -0700445 if (use32bpp) {
446 mip8888(adapt2, adapt);
447 } else {
448 mip565(adapt2, adapt);
449 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700450 }
451 }
452
453 return texAlloc;
454}
455
456
457void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data)
458{
459 Allocation *a = static_cast<Allocation *>(va);
460 a->data(data);
461}
462
463void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data)
464{
465 Allocation *a = static_cast<Allocation *>(va);
466 a->subData(xoff, count, data);
467}
468
469void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data)
470{
471 Allocation *a = static_cast<Allocation *>(va);
472 a->subData(xoff, yoff, w, h, data);
473}
474
475
476}
477}