rsAllocation.cpp revision 326e0ddf89e8df2837752fbfd7a014814b32082c
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
19using namespace android;
20using namespace android::renderscript;
21
22Allocation::Allocation(const Type *type)
23{
24    mPtr = NULL;
25
26    mCpuWrite = false;
27    mCpuRead = false;
28    mGpuWrite = false;
29    mGpuRead = false;
30
31    mReadWriteRatio = 0;
32    mUpdateSize = 0;
33
34    mIsTexture = false;
35    mTextureID = 0;
36
37    mIsVertexBuffer = false;
38    mBufferID = 0;
39
40    mType.set(type);
41    mPtr = malloc(mType->getSizeBytes());
42    if (!mPtr) {
43        LOGE("Allocation::Allocation, alloc failure");
44    }
45
46}
47
48Allocation::~Allocation()
49{
50}
51
52void Allocation::setCpuWritable(bool)
53{
54}
55
56void Allocation::setGpuWritable(bool)
57{
58}
59
60void Allocation::setCpuReadable(bool)
61{
62}
63
64void Allocation::setGpuReadable(bool)
65{
66}
67
68bool Allocation::fixAllocation()
69{
70    return false;
71}
72
73void Allocation::uploadToTexture(uint32_t lodOffset)
74{
75    //rsAssert(!mTextureId);
76    rsAssert(lodOffset < mType->getLODCount());
77
78    //LOGE("uploadToTexture  %i,  lod %i", mTextureID, lodOffset);
79
80    if (!mTextureID) {
81        glGenTextures(1, &mTextureID);
82    }
83    glBindTexture(GL_TEXTURE_2D, mTextureID);
84
85    Adapter2D adapt(this);
86    for(uint32_t lod = 0; (lod + lodOffset) < mType->getLODCount(); lod++) {
87        adapt.setLOD(lod+lodOffset);
88
89        uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
90        glTexImage2D(GL_TEXTURE_2D, lod, GL_RGB,
91                     adapt.getDimX(), adapt.getDimY(),
92                     0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, ptr);
93    }
94}
95
96void Allocation::uploadToBufferObject()
97{
98    rsAssert(!mType->getDimY());
99    rsAssert(!mType->getDimZ());
100
101    //LOGE("uploadToTexture  %i,  lod %i", mTextureID, lodOffset);
102
103    if (!mBufferID) {
104        glGenBuffers(1, &mBufferID);
105    }
106    glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
107    glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
108    glBindBuffer(GL_ARRAY_BUFFER, 0);
109}
110
111void Allocation::data(const void *data)
112{
113    memcpy(mPtr, data, mType->getSizeBytes());
114}
115
116void Allocation::subData(uint32_t xoff, uint32_t count, const void *data)
117{
118    uint32_t eSize = mType->getElementSizeBytes();
119    uint8_t * ptr = static_cast<uint8_t *>(mPtr);
120    ptr += eSize * xoff;
121    memcpy(ptr, data, count * eSize);
122}
123
124void Allocation::subData(uint32_t xoff, uint32_t yoff,
125             uint32_t w, uint32_t h, const void *data)
126{
127    uint32_t eSize = mType->getElementSizeBytes();
128    uint32_t lineSize = eSize * w;
129    uint32_t destW = mType->getDimX();
130
131    const uint8_t *src = static_cast<const uint8_t *>(data);
132    uint8_t *dst = static_cast<uint8_t *>(mPtr);
133    dst += eSize * (xoff + yoff * destW);
134    for (uint32_t line=yoff; line < (yoff+h); line++) {
135        uint8_t * ptr = static_cast<uint8_t *>(mPtr);
136        memcpy(dst, src, lineSize);
137        src += lineSize;
138        dst += destW * eSize;
139    }
140}
141
142void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
143             uint32_t w, uint32_t h, uint32_t d, const void *data)
144{
145}
146
147
148
149/////////////////
150//
151
152
153namespace android {
154namespace renderscript {
155
156RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
157{
158    const Type * type = static_cast<const Type *>(vtype);
159
160    Allocation * alloc = new Allocation(type);
161    return alloc;
162}
163
164RsAllocation rsi_AllocationCreatePredefSized(Context *rsc, RsElementPredefined t, size_t count)
165{
166    RsElement e = rsi_ElementGetPredefined(rsc, t);
167    return rsi_AllocationCreateSized(rsc, e, count);
168}
169
170RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
171{
172    Type * type = new Type();
173    type->setDimX(count);
174    type->setElement(static_cast<Element *>(e));
175    type->compute();
176    return rsi_AllocationCreateTyped(rsc, type);
177}
178
179void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel)
180{
181    Allocation *alloc = static_cast<Allocation *>(va);
182    alloc->uploadToTexture(baseMipLevel);
183}
184
185void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
186{
187    Allocation *alloc = static_cast<Allocation *>(va);
188    alloc->uploadToBufferObject();
189}
190
191void rsi_AllocationDestroy(Context *rsc, RsAllocation)
192{
193}
194
195static void mip(const Adapter2D &out, const Adapter2D &in)
196{
197    uint32_t w = out.getDimX();
198    uint32_t h = out.getDimY();
199
200    for (uint32_t y=0; y < w; y++) {
201        uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
202        const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
203        const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
204
205        for (uint32_t x=0; x < h; x++) {
206            *oPtr = rsBoxFilter565(i1[0], i1[2], i2[0], i2[1]);
207            oPtr ++;
208            i1 += 2;
209            i2 += 2;
210        }
211    }
212
213}
214
215
216RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, const char *file, bool genMips)
217{
218    typedef struct _Win3xBitmapHeader
219    {
220       uint16_t type;
221       uint32_t totalSize;
222       uint32_t reserved;
223       uint32_t offset;
224       int32_t hdrSize;            /* Size of this header in bytes */
225       int32_t width;           /* Image width in pixels */
226       int32_t height;          /* Image height in pixels */
227       int16_t planes;          /* Number of color planes */
228       int16_t bpp;             /* Number of bits per pixel */
229       /* Fields added for Windows 3.x follow this line */
230       int32_t compression;     /* Compression methods used */
231       int32_t sizeOfBitmap;    /* Size of bitmap in bytes */
232       int32_t horzResolution;  /* Horizontal resolution in pixels per meter */
233       int32_t vertResolution;  /* Vertical resolution in pixels per meter */
234       int32_t colorsUsed;      /* Number of colors in the image */
235       int32_t colorsImportant; /* Minimum number of important colors */
236    } __attribute__((__packed__)) WIN3XBITMAPHEADER;
237
238    _Win3xBitmapHeader hdr;
239
240    FILE *f = fopen(file, "rb");
241    if (f == NULL) {
242        LOGE("rsAllocationCreateFromBitmap failed to open file %s", file);
243        return NULL;
244    }
245    memset(&hdr, 0, sizeof(hdr));
246    fread(&hdr, sizeof(hdr), 1, f);
247
248    if (hdr.bpp != 24) {
249        LOGE("Unsuported BMP type");
250        fclose(f);
251        return NULL;
252    }
253
254    int32_t texWidth = rsHigherPow2(hdr.width);
255    int32_t texHeight = rsHigherPow2(hdr.height);
256
257    rsi_TypeBegin(rsc, rsi_ElementGetPredefined(rsc, RS_ELEMENT_RGB_565));
258    rsi_TypeAdd(rsc, RS_DIMENSION_X, texWidth);
259    rsi_TypeAdd(rsc, RS_DIMENSION_Y, texHeight);
260    if (genMips) {
261        rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
262    }
263    RsType type = rsi_TypeCreate(rsc);
264
265    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
266    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
267    texAlloc->incRef();
268    if (texAlloc == NULL) {
269        LOGE("Memory allocation failure");
270        fclose(f);
271        return NULL;
272    }
273
274    // offset to letterbox if height is not pow2
275    Adapter2D adapt(texAlloc);
276    uint8_t * fileInBuf = new uint8_t[texWidth * 3];
277    uint32_t yOffset = (hdr.width - hdr.height) / 2;
278    uint16_t *tmp = static_cast<uint16_t *>(adapt.getElement(0, yOffset));
279
280    for (int y=0; y < hdr.height; y++) {
281        fseek(f, hdr.offset + (y*hdr.width*3), SEEK_SET);
282        fread(fileInBuf, 1, hdr.width * 3, f);
283        for(int x=0; x < hdr.width; x++) {
284            *tmp = rs888to565(fileInBuf[x*3], fileInBuf[x*3 + 1], fileInBuf[x*3 + 2]);
285            tmp++;
286        }
287    }
288
289    fclose(f);
290    delete [] fileInBuf;
291
292    if (genMips) {
293        Adapter2D adapt2(texAlloc);
294        for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
295            adapt.setLOD(lod);
296            adapt2.setLOD(lod + 1);
297            mip(adapt2, adapt);
298        }
299    }
300
301    return texAlloc;
302}
303
304
305void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data)
306{
307    Allocation *a = static_cast<Allocation *>(va);
308    a->data(data);
309}
310
311void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data)
312{
313    Allocation *a = static_cast<Allocation *>(va);
314    a->subData(xoff, count, data);
315}
316
317void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data)
318{
319    Allocation *a = static_cast<Allocation *>(va);
320    a->subData(xoff, yoff, w, h, data);
321}
322
323
324}
325}
326