rsAllocation.cpp revision 1aa5a4eb81b8b88aeb5d2b6f4c47356fd0a62923
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    LOGE("Allocation %p destryed", this);
54}
55
56void Allocation::setCpuWritable(bool)
57{
58}
59
60void Allocation::setGpuWritable(bool)
61{
62}
63
64void Allocation::setCpuReadable(bool)
65{
66}
67
68void Allocation::setGpuReadable(bool)
69{
70}
71
72bool Allocation::fixAllocation()
73{
74    return false;
75}
76
77void Allocation::uploadToTexture(uint32_t lodOffset)
78{
79    //rsAssert(!mTextureId);
80    rsAssert(lodOffset < mType->getLODCount());
81
82    //LOGE("uploadToTexture  %i,  lod %i", mTextureID, lodOffset);
83
84    GLenum type = mType->getElement()->getGLType();
85    GLenum format = mType->getElement()->getGLFormat();
86
87    if (!type || !format) {
88        return;
89    }
90
91    if (!mTextureID) {
92        glGenTextures(1, &mTextureID);
93    }
94    glBindTexture(GL_TEXTURE_2D, mTextureID);
95
96    Adapter2D adapt(this);
97    for(uint32_t lod = 0; (lod + lodOffset) < mType->getLODCount(); lod++) {
98        adapt.setLOD(lod+lodOffset);
99
100        uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
101        glTexImage2D(GL_TEXTURE_2D, lod, format,
102                     adapt.getDimX(), adapt.getDimY(),
103                     0, format, type, ptr);
104    }
105}
106
107void Allocation::uploadToBufferObject()
108{
109    rsAssert(!mType->getDimY());
110    rsAssert(!mType->getDimZ());
111
112    //LOGE("uploadToTexture  %i,  lod %i", mTextureID, lodOffset);
113
114    if (!mBufferID) {
115        glGenBuffers(1, &mBufferID);
116    }
117    glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
118    glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
119    glBindBuffer(GL_ARRAY_BUFFER, 0);
120}
121
122void Allocation::data(const void *data)
123{
124    memcpy(mPtr, data, mType->getSizeBytes());
125}
126
127void Allocation::subData(uint32_t xoff, uint32_t count, const void *data)
128{
129    uint32_t eSize = mType->getElementSizeBytes();
130    uint8_t * ptr = static_cast<uint8_t *>(mPtr);
131    ptr += eSize * xoff;
132    memcpy(ptr, data, count * eSize);
133}
134
135void Allocation::subData(uint32_t xoff, uint32_t yoff,
136             uint32_t w, uint32_t h, const void *data)
137{
138    uint32_t eSize = mType->getElementSizeBytes();
139    uint32_t lineSize = eSize * w;
140    uint32_t destW = mType->getDimX();
141
142    const uint8_t *src = static_cast<const uint8_t *>(data);
143    uint8_t *dst = static_cast<uint8_t *>(mPtr);
144    dst += eSize * (xoff + yoff * destW);
145    for (uint32_t line=yoff; line < (yoff+h); line++) {
146        uint8_t * ptr = static_cast<uint8_t *>(mPtr);
147        memcpy(dst, src, lineSize);
148        src += lineSize;
149        dst += destW * eSize;
150    }
151}
152
153void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
154             uint32_t w, uint32_t h, uint32_t d, const void *data)
155{
156}
157
158
159
160/////////////////
161//
162
163
164namespace android {
165namespace renderscript {
166
167RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
168{
169    const Type * type = static_cast<const Type *>(vtype);
170
171    Allocation * alloc = new Allocation(type);
172    return alloc;
173}
174
175RsAllocation rsi_AllocationCreatePredefSized(Context *rsc, RsElementPredefined t, size_t count)
176{
177    RsElement e = rsi_ElementGetPredefined(rsc, t);
178    return rsi_AllocationCreateSized(rsc, e, count);
179}
180
181RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
182{
183    Type * type = new Type();
184    type->setDimX(count);
185    type->setElement(static_cast<Element *>(e));
186    type->compute();
187    return rsi_AllocationCreateTyped(rsc, type);
188}
189
190void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel)
191{
192    Allocation *alloc = static_cast<Allocation *>(va);
193    alloc->uploadToTexture(baseMipLevel);
194}
195
196void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
197{
198    Allocation *alloc = static_cast<Allocation *>(va);
199    alloc->uploadToBufferObject();
200}
201
202void rsi_AllocationDestroy(Context *rsc, RsAllocation)
203{
204}
205
206static void mip565(const Adapter2D &out, const Adapter2D &in)
207{
208    uint32_t w = out.getDimX();
209    uint32_t h = out.getDimY();
210
211    for (uint32_t y=0; y < w; y++) {
212        uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
213        const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
214        const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
215
216        for (uint32_t x=0; x < h; x++) {
217            *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
218            oPtr ++;
219            i1 += 2;
220            i2 += 2;
221        }
222    }
223}
224
225static void mip8888(const Adapter2D &out, const Adapter2D &in)
226{
227    uint32_t w = out.getDimX();
228    uint32_t h = out.getDimY();
229
230    for (uint32_t y=0; y < w; y++) {
231        uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
232        const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
233        const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
234
235        for (uint32_t x=0; x < h; x++) {
236            *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
237            oPtr ++;
238            i1 += 2;
239            i2 += 2;
240        }
241    }
242
243}
244
245
246typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
247
248static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
249{
250    memcpy(dst, src, count * 2);
251}
252static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
253{
254    memcpy(dst, src, count);
255}
256static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
257{
258    memcpy(dst, src, count * 4);
259}
260
261
262static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
263{
264    uint16_t *d = static_cast<uint16_t *>(dst);
265    const uint8_t *s = static_cast<const uint8_t *>(src);
266
267    while(count--) {
268        *d = rs888to565(s[0], s[1], s[2]);
269        d++;
270        s+= 3;
271    }
272}
273
274static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
275{
276    uint16_t *d = static_cast<uint16_t *>(dst);
277    const uint8_t *s = static_cast<const uint8_t *>(src);
278
279    while(count--) {
280        *d = rs888to565(s[0], s[1], s[2]);
281        d++;
282        s+= 4;
283    }
284}
285
286static ElementConverter_t pickConverter(RsElementPredefined dstFmt, RsElementPredefined srcFmt)
287{
288    if ((dstFmt == RS_ELEMENT_RGB_565) &&
289        (srcFmt == RS_ELEMENT_RGB_565)) {
290        return elementConverter_cpy_16;
291    }
292
293    if ((dstFmt == RS_ELEMENT_RGB_565) &&
294        (srcFmt == RS_ELEMENT_RGB_888)) {
295        return elementConverter_888_to_565;
296    }
297
298    if ((dstFmt == RS_ELEMENT_RGB_565) &&
299        (srcFmt == RS_ELEMENT_RGBA_8888)) {
300        return elementConverter_8888_to_565;
301    }
302
303    if ((dstFmt == RS_ELEMENT_RGBA_8888) &&
304        (srcFmt == RS_ELEMENT_RGBA_8888)) {
305        return elementConverter_cpy_32;
306    }
307
308    LOGE("pickConverter, unsuported combo");
309    return 0;
310}
311
312
313RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElementPredefined dstFmt, RsElementPredefined srcFmt,  bool genMips, const void *data)
314{
315    rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGB_565));
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
347RsAllocation rsi_AllocationCreateFromFile(Context *rsc, const char *file, bool genMips)
348{
349    bool use32bpp = false;
350
351    typedef struct _Win3xBitmapHeader
352    {
353       uint16_t type;
354       uint32_t totalSize;
355       uint32_t reserved;
356       uint32_t offset;
357       int32_t hdrSize;            /* Size of this header in bytes */
358       int32_t width;           /* Image width in pixels */
359       int32_t height;          /* Image height in pixels */
360       int16_t planes;          /* Number of color planes */
361       int16_t bpp;             /* Number of bits per pixel */
362       /* Fields added for Windows 3.x follow this line */
363       int32_t compression;     /* Compression methods used */
364       int32_t sizeOfBitmap;    /* Size of bitmap in bytes */
365       int32_t horzResolution;  /* Horizontal resolution in pixels per meter */
366       int32_t vertResolution;  /* Vertical resolution in pixels per meter */
367       int32_t colorsUsed;      /* Number of colors in the image */
368       int32_t colorsImportant; /* Minimum number of important colors */
369    } __attribute__((__packed__)) WIN3XBITMAPHEADER;
370
371    _Win3xBitmapHeader hdr;
372
373    FILE *f = fopen(file, "rb");
374    if (f == NULL) {
375        LOGE("rsAllocationCreateFromBitmap failed to open file %s", file);
376        return NULL;
377    }
378    memset(&hdr, 0, sizeof(hdr));
379    fread(&hdr, sizeof(hdr), 1, f);
380
381    if (hdr.bpp != 24) {
382        LOGE("Unsuported BMP type");
383        fclose(f);
384        return NULL;
385    }
386
387    int32_t texWidth = rsHigherPow2(hdr.width);
388    int32_t texHeight = rsHigherPow2(hdr.height);
389
390    if (use32bpp) {
391        rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGBA_8888));
392    } else {
393        rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGB_565));
394    }
395    rsi_TypeAdd(rsc, RS_DIMENSION_X, texWidth);
396    rsi_TypeAdd(rsc, RS_DIMENSION_Y, texHeight);
397    if (genMips) {
398        rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
399    }
400    RsType type = rsi_TypeCreate(rsc);
401
402    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
403    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
404    texAlloc->incRef();
405    if (texAlloc == NULL) {
406        LOGE("Memory allocation failure");
407        fclose(f);
408        return NULL;
409    }
410
411    // offset to letterbox if height is not pow2
412    Adapter2D adapt(texAlloc);
413    uint8_t * fileInBuf = new uint8_t[texWidth * 3];
414    uint32_t yOffset = (hdr.width - hdr.height) / 2;
415
416    if (use32bpp) {
417        uint8_t *tmp = static_cast<uint8_t *>(adapt.getElement(0, yOffset));
418        for (int y=0; y < hdr.height; y++) {
419            fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET);
420            fread(fileInBuf, 1, hdr.width * 3, f);
421            for(int x=0; x < hdr.width; x++) {
422                tmp[0] = fileInBuf[x*3 + 2];
423                tmp[1] = fileInBuf[x*3 + 1];
424                tmp[2] = fileInBuf[x*3];
425                tmp[3] = 0xff;
426                tmp += 4;
427            }
428        }
429    } else {
430        uint16_t *tmp = static_cast<uint16_t *>(adapt.getElement(0, yOffset));
431        for (int y=0; y < hdr.height; y++) {
432            fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET);
433            fread(fileInBuf, 1, hdr.width * 3, f);
434            for(int x=0; x < hdr.width; x++) {
435                *tmp = rs888to565(fileInBuf[x*3 + 2], fileInBuf[x*3 + 1], fileInBuf[x*3]);
436                tmp++;
437            }
438        }
439    }
440
441    fclose(f);
442    delete [] fileInBuf;
443
444    if (genMips) {
445        Adapter2D adapt2(texAlloc);
446        for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
447            adapt.setLOD(lod);
448            adapt2.setLOD(lod + 1);
449            if (use32bpp) {
450                mip8888(adapt2, adapt);
451            } else {
452                mip565(adapt2, adapt);
453            }
454        }
455    }
456
457    return texAlloc;
458}
459
460
461void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data)
462{
463    Allocation *a = static_cast<Allocation *>(va);
464    a->data(data);
465}
466
467void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data)
468{
469    Allocation *a = static_cast<Allocation *>(va);
470    a->subData(xoff, count, data);
471}
472
473void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data)
474{
475    Allocation *a = static_cast<Allocation *>(va);
476    a->subData(xoff, yoff, w, h, data);
477}
478
479
480}
481}
482