rsAllocation.cpp revision c9d43db4d216b01b13aebfdb31d5615909591b33
1/*
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
19#include <GLES/gl.h>
20#include <GLES/glext.h>
21
22using 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
81    GLenum type = mType->getElement()->getGLType();
82    GLenum format = mType->getElement()->getGLFormat();
83
84    if (!type || !format) {
85        return;
86    }
87
88    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));
98        glTexImage2D(GL_TEXTURE_2D, lod, format,
99                     adapt.getDimX(), adapt.getDimY(),
100                     0, format, type, ptr);
101    }
102}
103
104void Allocation::uploadToBufferObject()
105{
106    rsAssert(!mType->getDimY());
107    rsAssert(!mType->getDimZ());
108
109    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
130void Allocation::subData(uint32_t xoff, uint32_t yoff,
131             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/////////////////
156//
157
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    alloc->incRef();
168    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
202static void mip565(const Adapter2D &out, const Adapter2D &in)
203{
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++) {
213            *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]);
233            oPtr ++;
234            i1 += 2;
235            i2 += 2;
236        }
237    }
238
239}
240
241
242typedef 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{
284    if ((dstFmt == RS_ELEMENT_RGB_565) &&
285        (srcFmt == RS_ELEMENT_RGB_565)) {
286        return elementConverter_cpy_16;
287    }
288
289    if ((dstFmt == RS_ELEMENT_RGB_565) &&
290        (srcFmt == RS_ELEMENT_RGB_888)) {
291        return elementConverter_888_to_565;
292    }
293
294    if ((dstFmt == RS_ELEMENT_RGB_565) &&
295        (srcFmt == RS_ELEMENT_RGBA_8888)) {
296        return elementConverter_8888_to_565;
297    }
298
299    if ((dstFmt == RS_ELEMENT_RGBA_8888) &&
300        (srcFmt == RS_ELEMENT_RGBA_8888)) {
301        return elementConverter_cpy_32;
302    }
303
304    LOGE("pickConverter, unsuported combo, src %i,  dst %i", srcFmt, dstFmt);
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    rsAssert(!(w & (w-1)));
312    rsAssert(!(h & (h-1)));
313
314    //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
315    rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, dstFmt));
316    rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
317    rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
318    if (genMips) {
319        rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
320    }
321    RsType type = rsi_TypeCreate(rsc);
322
323    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
324    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
325    if (texAlloc == NULL) {
326        LOGE("Memory allocation failure");
327        return NULL;
328    }
329    texAlloc->incRef();
330
331    ElementConverter_t cvt = pickConverter(dstFmt, srcFmt);
332    cvt(texAlloc->getPtr(), data, w * h);
333
334    if (genMips) {
335        Adapter2D adapt(texAlloc);
336        Adapter2D adapt2(texAlloc);
337        for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
338            adapt.setLOD(lod);
339            adapt2.setLOD(lod + 1);
340            mip565(adapt2, adapt);
341        }
342    }
343
344    return texAlloc;
345}
346
347static uint32_t fmtToBits(RsElementPredefined fmt)
348{
349    return 16;
350}
351
352RsAllocation rsi_AllocationCreateFromBitmapBoxed(Context *rsc, uint32_t w, uint32_t h, RsElementPredefined dstFmt, RsElementPredefined srcFmt, bool genMips, const void *data)
353{
354    uint32_t w2 = rsHigherPow2(w);
355    uint32_t h2 = rsHigherPow2(h);
356
357    if ((w2 == w) && (h2 == h)) {
358        return rsi_AllocationCreateFromBitmap(rsc, w, h, dstFmt, srcFmt, genMips, data);
359    }
360
361    uint32_t bpp = fmtToBits(srcFmt) >> 3;
362    size_t size = w2 * h2 * bpp;
363    uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
364    memset(tmp, 0, size);
365
366    const uint8_t * src = static_cast<const uint8_t *>(data);
367    for (uint32_t y = 0; y < h; y++) {
368        uint8_t * ydst = &tmp[y + ((h2 - h) >> 1)];
369        memcpy(&ydst[(w2 - w) >> 1], src, w * bpp);
370        src += h * bpp;
371    }
372
373    RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, dstFmt, srcFmt, genMips, tmp);
374    free(tmp);
375    return ret;
376
377
378
379
380}
381
382
383RsAllocation rsi_AllocationCreateFromFile(Context *rsc, const char *file, bool genMips)
384{
385    bool use32bpp = false;
386
387    typedef struct _Win3xBitmapHeader
388    {
389       uint16_t type;
390       uint32_t totalSize;
391       uint32_t reserved;
392       uint32_t offset;
393       int32_t hdrSize;            /* Size of this header in bytes */
394       int32_t width;           /* Image width in pixels */
395       int32_t height;          /* Image height in pixels */
396       int16_t planes;          /* Number of color planes */
397       int16_t bpp;             /* Number of bits per pixel */
398       /* Fields added for Windows 3.x follow this line */
399       int32_t compression;     /* Compression methods used */
400       int32_t sizeOfBitmap;    /* Size of bitmap in bytes */
401       int32_t horzResolution;  /* Horizontal resolution in pixels per meter */
402       int32_t vertResolution;  /* Vertical resolution in pixels per meter */
403       int32_t colorsUsed;      /* Number of colors in the image */
404       int32_t colorsImportant; /* Minimum number of important colors */
405    } __attribute__((__packed__)) WIN3XBITMAPHEADER;
406
407    _Win3xBitmapHeader hdr;
408
409    FILE *f = fopen(file, "rb");
410    if (f == NULL) {
411        LOGE("rsAllocationCreateFromBitmap failed to open file %s", file);
412        return NULL;
413    }
414    memset(&hdr, 0, sizeof(hdr));
415    fread(&hdr, sizeof(hdr), 1, f);
416
417    if (hdr.bpp != 24) {
418        LOGE("Unsuported BMP type");
419        fclose(f);
420        return NULL;
421    }
422
423    int32_t texWidth = rsHigherPow2(hdr.width);
424    int32_t texHeight = rsHigherPow2(hdr.height);
425
426    if (use32bpp) {
427        rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGBA_8888));
428    } else {
429        rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGB_565));
430    }
431    rsi_TypeAdd(rsc, RS_DIMENSION_X, texWidth);
432    rsi_TypeAdd(rsc, RS_DIMENSION_Y, texHeight);
433    if (genMips) {
434        rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
435    }
436    RsType type = rsi_TypeCreate(rsc);
437
438    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
439    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
440    texAlloc->incRef();
441    if (texAlloc == NULL) {
442        LOGE("Memory allocation failure");
443        fclose(f);
444        return NULL;
445    }
446
447    // offset to letterbox if height is not pow2
448    Adapter2D adapt(texAlloc);
449    uint8_t * fileInBuf = new uint8_t[texWidth * 3];
450    uint32_t yOffset = (hdr.width - hdr.height) / 2;
451
452    if (use32bpp) {
453        uint8_t *tmp = static_cast<uint8_t *>(adapt.getElement(0, yOffset));
454        for (int y=0; y < hdr.height; y++) {
455            fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET);
456            fread(fileInBuf, 1, hdr.width * 3, f);
457            for(int x=0; x < hdr.width; x++) {
458                tmp[0] = fileInBuf[x*3 + 2];
459                tmp[1] = fileInBuf[x*3 + 1];
460                tmp[2] = fileInBuf[x*3];
461                tmp[3] = 0xff;
462                tmp += 4;
463            }
464        }
465    } else {
466        uint16_t *tmp = static_cast<uint16_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 = rs888to565(fileInBuf[x*3 + 2], fileInBuf[x*3 + 1], fileInBuf[x*3]);
472                tmp++;
473            }
474        }
475    }
476
477    fclose(f);
478    delete [] fileInBuf;
479
480    if (genMips) {
481        Adapter2D adapt2(texAlloc);
482        for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
483            adapt.setLOD(lod);
484            adapt2.setLOD(lod + 1);
485            if (use32bpp) {
486                mip8888(adapt2, adapt);
487            } else {
488                mip565(adapt2, adapt);
489            }
490        }
491    }
492
493    return texAlloc;
494}
495
496
497void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data)
498{
499    Allocation *a = static_cast<Allocation *>(va);
500    a->data(data);
501}
502
503void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data)
504{
505    Allocation *a = static_cast<Allocation *>(va);
506    a->subData(xoff, count, data);
507}
508
509void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data)
510{
511    Allocation *a = static_cast<Allocation *>(va);
512    a->subData(xoff, yoff, w, h, data);
513}
514
515
516}
517}
518