rsAllocation.cpp revision 168eecfcdc0b681e2251d443b41f11eb8a6142b8
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(Context *rsc, const Type *type) : ObjectBase(rsc)
26{
27    mAllocFile = __FILE__;
28    mAllocLine = __LINE__;
29    mPtr = NULL;
30
31    mCpuWrite = false;
32    mCpuRead = false;
33    mGpuWrite = false;
34    mGpuRead = false;
35
36    mReadWriteRatio = 0;
37    mUpdateSize = 0;
38
39    mIsTexture = false;
40    mTextureID = 0;
41
42    mIsVertexBuffer = false;
43    mBufferID = 0;
44
45    mType.set(type);
46    rsAssert(type);
47    mPtr = malloc(mType->getSizeBytes());
48    if (!mPtr) {
49        LOGE("Allocation::Allocation, alloc failure");
50    }
51}
52
53Allocation::~Allocation()
54{
55    free(mPtr);
56    mPtr = NULL;
57
58    if (mBufferID) {
59        // Causes a SW crash....
60        //LOGV(" mBufferID %i", mBufferID);
61        //glDeleteBuffers(1, &mBufferID);
62        //mBufferID = 0;
63    }
64    if (mTextureID) {
65        glDeleteTextures(1, &mTextureID);
66        mTextureID = 0;
67    }
68}
69
70void Allocation::setCpuWritable(bool)
71{
72}
73
74void Allocation::setGpuWritable(bool)
75{
76}
77
78void Allocation::setCpuReadable(bool)
79{
80}
81
82void Allocation::setGpuReadable(bool)
83{
84}
85
86bool Allocation::fixAllocation()
87{
88    return false;
89}
90
91void Allocation::uploadToTexture(uint32_t lodOffset)
92{
93    //rsAssert(!mTextureId);
94    rsAssert(lodOffset < mType->getLODCount());
95
96    GLenum type = mType->getElement()->getGLType();
97    GLenum format = mType->getElement()->getGLFormat();
98
99    if (!type || !format) {
100        return;
101    }
102
103    if (!mTextureID) {
104        glGenTextures(1, &mTextureID);
105    }
106    glBindTexture(GL_TEXTURE_2D, mTextureID);
107
108    Adapter2D adapt(getContext(), this);
109    for(uint32_t lod = 0; (lod + lodOffset) < mType->getLODCount(); lod++) {
110        adapt.setLOD(lod+lodOffset);
111
112        uint16_t * ptr = static_cast<uint16_t *>(adapt.getElement(0,0));
113        glTexImage2D(GL_TEXTURE_2D, lod, format,
114                     adapt.getDimX(), adapt.getDimY(),
115                     0, format, type, ptr);
116    }
117}
118
119void Allocation::uploadToBufferObject()
120{
121    rsAssert(!mType->getDimY());
122    rsAssert(!mType->getDimZ());
123
124    if (!mBufferID) {
125        glGenBuffers(1, &mBufferID);
126    }
127    glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
128    glBufferData(GL_ARRAY_BUFFER, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
129    glBindBuffer(GL_ARRAY_BUFFER, 0);
130}
131
132
133void Allocation::data(const void *data, uint32_t sizeBytes)
134{
135    uint32_t size = mType->getSizeBytes();
136    if (size != sizeBytes) {
137        LOGE("Allocation::data called with mismatched size expected %i, got %i", size, sizeBytes);
138        return;
139    }
140    memcpy(mPtr, data, size);
141    sendDirty();
142}
143
144void Allocation::read(void *data)
145{
146    memcpy(data, mPtr, mType->getSizeBytes());
147}
148
149void Allocation::subData(uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
150{
151    uint32_t eSize = mType->getElementSizeBytes();
152    uint8_t * ptr = static_cast<uint8_t *>(mPtr);
153    ptr += eSize * xoff;
154    uint32_t size = count * eSize;
155
156    if (size != sizeBytes) {
157        LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
158        mType->dumpLOGV("type info");
159        return;
160    }
161    memcpy(ptr, data, size);
162    sendDirty();
163}
164
165void Allocation::subData(uint32_t xoff, uint32_t yoff,
166             uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
167{
168    uint32_t eSize = mType->getElementSizeBytes();
169    uint32_t lineSize = eSize * w;
170    uint32_t destW = mType->getDimX();
171
172    const uint8_t *src = static_cast<const uint8_t *>(data);
173    uint8_t *dst = static_cast<uint8_t *>(mPtr);
174    dst += eSize * (xoff + yoff * destW);
175
176    if ((lineSize * eSize * h) != sizeBytes) {
177        rsAssert(!"Allocation::subData called with mismatched size");
178        return;
179    }
180
181    for (uint32_t line=yoff; line < (yoff+h); line++) {
182        uint8_t * ptr = static_cast<uint8_t *>(mPtr);
183        memcpy(dst, src, lineSize);
184        src += lineSize;
185        dst += destW * eSize;
186    }
187    sendDirty();
188}
189
190void Allocation::subData(uint32_t xoff, uint32_t yoff, uint32_t zoff,
191             uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes)
192{
193}
194
195void Allocation::addProgramToDirty(const Program *p)
196{
197    mToDirtyList.add(p);
198}
199
200void Allocation::removeProgramToDirty(const Program *p)
201{
202    for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
203        if (mToDirtyList[ct] == p) {
204            mToDirtyList.removeAt(ct);
205            return;
206        }
207    }
208    rsAssert(0);
209}
210
211void Allocation::sendDirty() const
212{
213    for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
214        mToDirtyList[ct]->forceDirty();
215    }
216}
217
218/////////////////
219//
220
221
222namespace android {
223namespace renderscript {
224
225RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
226{
227    const Type * type = static_cast<const Type *>(vtype);
228
229    Allocation * alloc = new Allocation(rsc, type);
230    alloc->incUserRef();
231    return alloc;
232}
233
234RsAllocation rsi_AllocationCreateSized(Context *rsc, RsElement e, size_t count)
235{
236    Type * type = new Type(rsc);
237    type->setDimX(count);
238    type->setElement(static_cast<Element *>(e));
239    type->compute();
240    return rsi_AllocationCreateTyped(rsc, type);
241}
242
243void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, uint32_t baseMipLevel)
244{
245    Allocation *alloc = static_cast<Allocation *>(va);
246    alloc->uploadToTexture(baseMipLevel);
247}
248
249void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va)
250{
251    Allocation *alloc = static_cast<Allocation *>(va);
252    alloc->uploadToBufferObject();
253}
254
255static void mip565(const Adapter2D &out, const Adapter2D &in)
256{
257    uint32_t w = out.getDimX();
258    uint32_t h = out.getDimY();
259
260    for (uint32_t y=0; y < h; y++) {
261        uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
262        const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
263        const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
264
265        for (uint32_t x=0; x < w; x++) {
266            *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
267            oPtr ++;
268            i1 += 2;
269            i2 += 2;
270        }
271    }
272}
273
274static void mip8888(const Adapter2D &out, const Adapter2D &in)
275{
276    uint32_t w = out.getDimX();
277    uint32_t h = out.getDimY();
278
279    for (uint32_t y=0; y < h; y++) {
280        uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
281        const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
282        const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
283
284        for (uint32_t x=0; x < w; x++) {
285            *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
286            oPtr ++;
287            i1 += 2;
288            i2 += 2;
289        }
290    }
291}
292
293static void mip(const Adapter2D &out, const Adapter2D &in)
294{
295    switch(out.getBaseType()->getElement()->getSizeBits()) {
296    case 32:
297        mip8888(out, in);
298        break;
299    case 16:
300        mip565(out, in);
301        break;
302
303    }
304
305}
306
307typedef void (*ElementConverter_t)(void *dst, const void *src, uint32_t count);
308
309static void elementConverter_cpy_16(void *dst, const void *src, uint32_t count)
310{
311    memcpy(dst, src, count * 2);
312}
313static void elementConverter_cpy_8(void *dst, const void *src, uint32_t count)
314{
315    memcpy(dst, src, count);
316}
317static void elementConverter_cpy_32(void *dst, const void *src, uint32_t count)
318{
319    memcpy(dst, src, count * 4);
320}
321
322
323static void elementConverter_888_to_565(void *dst, const void *src, uint32_t count)
324{
325    uint16_t *d = static_cast<uint16_t *>(dst);
326    const uint8_t *s = static_cast<const uint8_t *>(src);
327
328    while(count--) {
329        *d = rs888to565(s[0], s[1], s[2]);
330        d++;
331        s+= 3;
332    }
333}
334
335static void elementConverter_8888_to_565(void *dst, const void *src, uint32_t count)
336{
337    uint16_t *d = static_cast<uint16_t *>(dst);
338    const uint8_t *s = static_cast<const uint8_t *>(src);
339
340    while(count--) {
341        *d = rs888to565(s[0], s[1], s[2]);
342        d++;
343        s+= 4;
344    }
345}
346
347static ElementConverter_t pickConverter(const Element *dst, const Element *src)
348{
349    GLenum srcGLType = src->getGLType();
350    GLenum srcGLFmt = src->getGLFormat();
351    GLenum dstGLType = dst->getGLType();
352    GLenum dstGLFmt = dst->getGLFormat();
353
354    if (srcGLFmt == dstGLFmt && srcGLType == dstGLType) {
355        switch(dst->getSizeBytes()) {
356        case 4:
357            return elementConverter_cpy_32;
358        case 2:
359            return elementConverter_cpy_16;
360        case 1:
361            return elementConverter_cpy_8;
362        }
363    }
364
365    if (srcGLType == GL_UNSIGNED_BYTE &&
366        srcGLFmt == GL_RGB &&
367        dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
368        dstGLType == GL_RGB) {
369
370        return elementConverter_888_to_565;
371    }
372
373    if (srcGLType == GL_UNSIGNED_BYTE &&
374        srcGLFmt == GL_RGBA &&
375        dstGLType == GL_UNSIGNED_SHORT_5_6_5 &&
376        dstGLType == GL_RGB) {
377
378        return elementConverter_8888_to_565;
379    }
380
381    LOGE("pickConverter, unsuported combo, src %p,  dst %p", src, dst);
382    return 0;
383}
384
385
386RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src,  bool genMips, const void *data)
387{
388    const Element *src = static_cast<const Element *>(_src);
389    const Element *dst = static_cast<const Element *>(_dst);
390    rsAssert(!(w & (w-1)));
391    rsAssert(!(h & (h-1)));
392
393    //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
394    rsi_TypeBegin(rsc, _dst);
395    rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
396    rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
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    if (texAlloc == NULL) {
405        LOGE("Memory allocation failure");
406        return NULL;
407    }
408    texAlloc->incUserRef();
409
410    ElementConverter_t cvt = pickConverter(dst, src);
411    cvt(texAlloc->getPtr(), data, w * h);
412
413    if (genMips) {
414        Adapter2D adapt(rsc, texAlloc);
415        Adapter2D adapt2(rsc, texAlloc);
416        for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
417            adapt.setLOD(lod);
418            adapt2.setLOD(lod + 1);
419            mip(adapt2, adapt);
420        }
421    }
422
423    return texAlloc;
424}
425
426RsAllocation rsi_AllocationCreateFromBitmapBoxed(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data)
427{
428    const Element *srcE = static_cast<const Element *>(_src);
429    const Element *dstE = static_cast<const Element *>(_dst);
430    uint32_t w2 = rsHigherPow2(w);
431    uint32_t h2 = rsHigherPow2(h);
432
433    if ((w2 == w) && (h2 == h)) {
434        return rsi_AllocationCreateFromBitmap(rsc, w, h, _dst, _src, genMips, data);
435    }
436
437    uint32_t bpp = srcE->getSizeBytes();
438    size_t size = w2 * h2 * bpp;
439    uint8_t *tmp = static_cast<uint8_t *>(malloc(size));
440    memset(tmp, 0, size);
441
442    const uint8_t * src = static_cast<const uint8_t *>(data);
443    for (uint32_t y = 0; y < h; y++) {
444        uint8_t * ydst = &tmp[(y + ((h2 - h) >> 1)) * w2 * bpp];
445        memcpy(&ydst[((w2 - w) >> 1) * bpp], src, w * bpp);
446        src += w * bpp;
447    }
448
449    RsAllocation ret = rsi_AllocationCreateFromBitmap(rsc, w2, h2, _dst, _src, genMips, tmp);
450    free(tmp);
451    return ret;
452}
453
454void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes)
455{
456    Allocation *a = static_cast<Allocation *>(va);
457    a->data(data, sizeBytes);
458}
459
460void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data, uint32_t sizeBytes)
461{
462    Allocation *a = static_cast<Allocation *>(va);
463    a->subData(xoff, count, data, sizeBytes);
464}
465
466void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes)
467{
468    Allocation *a = static_cast<Allocation *>(va);
469    a->subData(xoff, yoff, w, h, data, sizeBytes);
470}
471
472void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data)
473{
474    Allocation *a = static_cast<Allocation *>(va);
475    a->read(data);
476}
477
478
479}
480}
481