rsAllocation.cpp revision a57c0a72c3b3babc2757d081ff8146ebaa2caf4c
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    rsAssert(type);
45    mPtr = malloc(mType->getSizeBytes());
46    if (!mPtr) {
47        LOGE("Allocation::Allocation, alloc failure");
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
117
118void Allocation::data(const void *data, uint32_t sizeBytes)
119{
120    uint32_t size = mType->getSizeBytes();
121    if (size != sizeBytes) {
122        LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
123        return;
124    }
125    memcpy(mPtr, data, size);
126}
127
128void Allocation::read(void *data)
129{
130    memcpy(data, mPtr, mType->getSizeBytes());
131}
132
133void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
134{
135    uint32_t eSize = mType->getElementSizeBytes();
136    uint8_t * ptr = static_cast<uint8_t *>(mPtr);
137    ptr += eSize * xoff;
138    uint32_t size = count * eSize;
139
140    if (size != sizeBytes) {
141        LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
142        return;
143    }
144    memcpy(ptr, data, size);
145}
146
147void Allocation::subData(uint32_t xoff, uint32_t yoff,
148             uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
149{
150    uint32_t eSize = mType->getElementSizeBytes();
151    uint32_t lineSize = eSize * w;
152    uint32_t destW = mType->getDimX();
153
154    const uint8_t *src = static_cast<const uint8_t *>(data);
155    uint8_t *dst = static_cast<uint8_t *>(mPtr);
156    dst += eSize * (xoff + yoff * destW);
157
158    if ((lineSize * eSize * h) != sizeBytes) {
159        rsAssert(!"Allocation::subData called with mismatched size");
160        return;
161    }
162
163    for (uint32_t line=yoff; line < (yoff+h); line++) {
164        uint8_t * ptr = static_cast<uint8_t *>(mPtr);
165        memcpy(dst, src, lineSize);
166        src += lineSize;
167        dst += destW * eSize;
168    }
169}
170
171void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
172             uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes)
173{
174}
175
176
177
178/////////////////
179//
180
181
182namespace android {
183namespace renderscript {
184
185RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
186{
187    const Type * type = static_cast<const Type *>(vtype);
188
189    Allocation * alloc = new Allocation(type);
190    alloc->incUserRef();
191    return alloc;
192}
193
194RsAllocation rsi_AllocationCreatePredefSized(Context *rsc, RsElementPredefined t, size_t count)
195{
196    RsElement e = rsi_ElementGetPredefined(rsc, t);
197    return rsi_AllocationCreateSized(rsc, e, count);
198}
199
200RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
201{
202    Type * type = new Type();
203    type->setDimX(count);
204    type->setElement(static_cast<Element *>(e));
205    type->compute();
206    return rsi_AllocationCreateTyped(rsc, type);
207}
208
209void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel)
210{
211    Allocation *alloc = static_cast<Allocation *>(va);
212    alloc->uploadToTexture(baseMipLevel);
213}
214
215void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
216{
217    Allocation *alloc = static_cast<Allocation *>(va);
218    alloc->uploadToBufferObject();
219}
220
221static void mip565(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 < h; y++) {
227        uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
228        const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
229        const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
230
231        for (uint32_t x=0; x < w; x++) {
232            *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
233            oPtr ++;
234            i1 += 2;
235            i2 += 2;
236        }
237    }
238}
239
240static void mip8888(const Adapter2D &out, const Adapter2D &in)
241{
242    uint32_t w = out.getDimX();
243    uint32_t h = out.getDimY();
244
245    for (uint32_t y=0; y < h; y++) {
246        uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
247        const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
248        const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
249
250        for (uint32_t x=0; x < w; x++) {
251            *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
252            oPtr ++;
253            i1 += 2;
254            i2 += 2;
255        }
256    }
257}
258
259static void mip(const Adapter2D &out, const Adapter2D &in)
260{
261    switch(out.getBaseType()->getElement()->getSizeBits()) {
262    case 32:
263        mip8888(out, in);
264        break;
265    case 16:
266        mip565(out, in);
267        break;
268
269    }
270
271}
272
273typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
274
275static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
276{
277    memcpy(dst, src, count * 2);
278}
279static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
280{
281    memcpy(dst, src, count);
282}
283static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
284{
285    memcpy(dst, src, count * 4);
286}
287
288
289static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
290{
291    uint16_t *d = static_cast<uint16_t *>(dst);
292    const uint8_t *s = static_cast<const uint8_t *>(src);
293
294    while(count--) {
295        *d = rs888to565(s[0], s[1], s[2]);
296        d++;
297        s+= 3;
298    }
299}
300
301static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
302{
303    uint16_t *d = static_cast<uint16_t *>(dst);
304    const uint8_t *s = static_cast<const uint8_t *>(src);
305
306    while(count--) {
307        *d = rs888to565(s[0], s[1], s[2]);
308        d++;
309        s+= 4;
310    }
311}
312
313static ElementConverter_t pickConverter(const Element *dst, const Element *src)
314{
315    GLenum srcGLType = src->getGLType();
316    GLenum srcGLFmt = src->getGLFormat();
317    GLenum dstGLType = dst->getGLType();
318    GLenum dstGLFmt = dst->getGLFormat();
319
320    if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
321        switch(dst->getSizeBytes()) {
322        case 4:
323            return elementConverter_cpy_32;
324        case 2:
325            return elementConverter_cpy_16;
326        case 1:
327            return elementConverter_cpy_8;
328        }
329    }
330
331    if (srcGLType == GL_UNSIGNED_BYTE &&
332        srcGLFmt == GL_RGB &&
333        dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
334        dstGLType == GL_RGB) {
335
336        return elementConverter_888_to_565;
337    }
338
339    if (srcGLType == GL_UNSIGNED_BYTE &&
340        srcGLFmt == GL_RGBA &&
341        dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
342        dstGLType == GL_RGB) {
343
344        return elementConverter_8888_to_565;
345    }
346
347    LOGE("pickConverter, unsuported combo, src %p,  dst %p", src, dst);
348    return 0;
349}
350
351
352RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src,  bool genMips, const void *data)
353{
354    const Element *src = static_cast<const Element *>(_src);
355    const Element *dst = static_cast<const Element *>(_dst);
356    rsAssert(!(w & (w-1)));
357    rsAssert(!(h & (h-1)));
358
359    //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
360    rsi_TypeBegin(rsc, _dst);
361    rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
362    rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
363    if (genMips) {
364        rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
365    }
366    RsType type = rsi_TypeCreate(rsc);
367
368    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
369    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
370    if (texAlloc == NULL) {
371        LOGE("Memory allocation failure");
372        return NULL;
373    }
374    texAlloc->incUserRef();
375
376    ElementConverter_t cvt = pickConverter(dst, src);
377    cvt(texAlloc->getPtr(), data, w * h);
378
379    if (genMips) {
380        Adapter2D adapt(texAlloc);
381        Adapter2D adapt2(texAlloc);
382        for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
383            adapt.setLOD(lod);
384            adapt2.setLOD(lod + 1);
385            mip(adapt2, adapt);
386        }
387    }
388
389    return texAlloc;
390}
391
392RsAllocation rsi_AllocationCreateFromBitmapBoxed(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data)
393{
394    const Element *srcE = static_cast<const Element *>(_src);
395    const Element *dstE = static_cast<const Element *>(_dst);
396    uint32_t w2 = rsHigherPow2(w);
397    uint32_t h2 = rsHigherPow2(h);
398
399    if ((w2 == w) && (h2 == h)) {
400        return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data);
401    }
402
403    uint32_t bpp = srcE->getSizeBytes();
404    size_t size = w2 * h2 * bpp;
405    uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
406    memset(tmp, 0, size);
407
408    const uint8_t * src = static_cast<const uint8_t *>(data);
409    for (uint32_t y = 0; y < h; y++) {
410        uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
411        memcpy(&ydst[(w2 - w) >> 1], src, w * bpp);
412        src += w * bpp;
413    }
414
415    RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp);
416    free(tmp);
417    return ret;
418
419
420
421
422}
423
424
425RsAllocation rsi_AllocationCreateFromFile(Context *rsc, const char *file, bool genMips)
426{
427    bool use32bpp = false;
428
429    typedef struct _Win3xBitmapHeader
430    {
431       uint16_t type;
432       uint32_t totalSize;
433       uint32_t reserved;
434       uint32_t offset;
435       int32_t hdrSize;            /* Size of this header in bytes */
436       int32_t width;           /* Image width in pixels */
437       int32_t height;          /* Image height in pixels */
438       int16_t planes;          /* Number of color planes */
439       int16_t bpp;             /* Number of bits per pixel */
440       /* Fields added for Windows 3.x follow this line */
441       int32_t compression;     /* Compression methods used */
442       int32_t sizeOfBitmap;    /* Size of bitmap in bytes */
443       int32_t horzResolution;  /* Horizontal resolution in pixels per meter */
444       int32_t vertResolution;  /* Vertical resolution in pixels per meter */
445       int32_t colorsUsed;      /* Number of colors in the image */
446       int32_t colorsImportant; /* Minimum number of important colors */
447    } __attribute__((__packed__)) WIN3XBITMAPHEADER;
448
449    _Win3xBitmapHeader hdr;
450
451    FILE *f = fopen(file, "rb");
452    if (f == NULL) {
453        LOGE("rsAllocationCreateFromBitmap failed to open file %s", file);
454        return NULL;
455    }
456    memset(&hdr, 0, sizeof(hdr));
457    fread(&hdr, sizeof(hdr), 1, f);
458
459    if (hdr.bpp != 24) {
460        LOGE("Unsuported BMP type");
461        fclose(f);
462        return NULL;
463    }
464
465    int32_t texWidth = rsHigherPow2(hdr.width);
466    int32_t texHeight = rsHigherPow2(hdr.height);
467
468    if (use32bpp) {
469        rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGBA_8888));
470    } else {
471        rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGB_565));
472    }
473    rsi_TypeAdd(rsc, RS_DIMENSION_X, texWidth);
474    rsi_TypeAdd(rsc, RS_DIMENSION_Y, texHeight);
475    if (genMips) {
476        rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
477    }
478    RsType type = rsi_TypeCreate(rsc);
479
480    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
481    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
482    texAlloc->incUserRef();
483    if (texAlloc == NULL) {
484        LOGE("Memory allocation failure");
485        fclose(f);
486        return NULL;
487    }
488
489    // offset to letterbox if height is not pow2
490    Adapter2D adapt(texAlloc);
491    uint8_t * fileInBuf = new uint8_t[texWidth * 3];
492    uint32_t yOffset = (hdr.width - hdr.height) / 2;
493
494    if (use32bpp) {
495        uint8_t *tmp = static_cast<uint8_t *>(adapt.getElement(0, yOffset));
496        for (int y=0; y < hdr.height; y++) {
497            fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET);
498            fread(fileInBuf, 1, hdr.width * 3, f);
499            for(int x=0; x < hdr.width; x++) {
500                tmp[0] = fileInBuf[x*3 + 2];
501                tmp[1] = fileInBuf[x*3 + 1];
502                tmp[2] = fileInBuf[x*3];
503                tmp[3] = 0xff;
504                tmp += 4;
505            }
506        }
507    } else {
508        uint16_t *tmp = static_cast<uint16_t *>(adapt.getElement(0, yOffset));
509        for (int y=0; y < hdr.height; y++) {
510            fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET);
511            fread(fileInBuf, 1, hdr.width * 3, f);
512            for(int x=0; x < hdr.width; x++) {
513                *tmp = rs888to565(fileInBuf[x*3 + 2], fileInBuf[x*3 + 1], fileInBuf[x*3]);
514                tmp++;
515            }
516        }
517    }
518
519    fclose(f);
520    delete [] fileInBuf;
521
522    if (genMips) {
523        Adapter2D adapt2(texAlloc);
524        for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
525            adapt.setLOD(lod);
526            adapt2.setLOD(lod + 1);
527            mip(adapt2, adapt);
528        }
529    }
530
531    return texAlloc;
532}
533
534void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes)
535{
536    Allocation *a = static_cast<Allocation *>(va);
537    a->data(data, sizeBytes);
538    rsc->allocationCheck(a);
539}
540
541void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
542{
543    Allocation *a = static_cast<Allocation *>(va);
544    a->subData(xoff, count, data, sizeBytes);
545    rsc->allocationCheck(a);
546}
547
548void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
549{
550    Allocation *a = static_cast<Allocation *>(va);
551    a->subData(xoff, yoff, w, h, data, sizeBytes);
552    rsc->allocationCheck(a);
553}
554
555void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data)
556{
557    Allocation *a = static_cast<Allocation *>(va);
558    a->read(data);
559}
560
561
562}
563}
564