rsAllocation.cpp revision 6f9fb0f8de25366969de08cbe665e4b41a032d4f
1/*
2 * Copyright (C) 2009-2012 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#include "rs_hal.h"
19
20
21using namespace android;
22using namespace android::renderscript;
23
24Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
25                       RsAllocationMipmapControl mc, void * ptr)
26    : ObjectBase(rsc) {
27
28    memset(&mHal, 0, sizeof(mHal));
29    mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
30    mHal.state.usageFlags = usages;
31    mHal.state.mipmapControl = mc;
32    mHal.state.usrPtr = ptr;
33
34    setType(type);
35    updateCache();
36}
37
38Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
39                              RsAllocationMipmapControl mc, void * ptr) {
40    Allocation *a = new Allocation(rsc, type, usages, mc, ptr);
41
42    if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) {
43        rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
44        delete a;
45        return NULL;
46    }
47    return a;
48}
49
50void Allocation::updateCache() {
51    const Type *type = mHal.state.type;
52    mHal.state.dimensionX = type->getDimX();
53    mHal.state.dimensionY = type->getDimY();
54    mHal.state.dimensionZ = type->getDimZ();
55    mHal.state.hasFaces = type->getDimFaces();
56    mHal.state.hasMipmaps = type->getDimLOD();
57    mHal.state.elementSizeBytes = type->getElementSizeBytes();
58    mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
59}
60
61Allocation::~Allocation() {
62    freeChildrenUnlocked();
63    mRSC->mHal.funcs.allocation.destroy(mRSC, this);
64}
65
66void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
67    rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
68}
69
70void Allocation::read(void *data) {
71    memcpy(data, getPtr(), mHal.state.type->getSizeBytes());
72}
73
74void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
75                         uint32_t count, const void *data, size_t sizeBytes) {
76    const size_t eSize = mHal.state.type->getElementSizeBytes();
77
78    if ((count * eSize) != sizeBytes) {
79        ALOGE("Allocation::subData called with mismatched size expected %zu, got %zu",
80             (count * eSize), sizeBytes);
81        mHal.state.type->dumpLOGV("type info");
82        return;
83    }
84
85    rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
86    sendDirty(rsc);
87}
88
89void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
90             uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
91    const size_t eSize = mHal.state.elementSizeBytes;
92    const size_t lineSize = eSize * w;
93
94    //ALOGE("data2d %p,  %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes);
95
96    if ((lineSize * h) != sizeBytes) {
97        ALOGE("Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes);
98        rsAssert(!"Allocation::subData called with mismatched size");
99        return;
100    }
101
102    rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes);
103    sendDirty(rsc);
104}
105
106void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
107                      uint32_t lod, RsAllocationCubemapFace face,
108                      uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes) {
109}
110
111void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
112                                uint32_t cIdx, size_t sizeBytes) {
113    size_t eSize = mHal.state.elementSizeBytes;
114
115    if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
116        ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
117        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
118        return;
119    }
120
121    if (x >= mHal.state.dimensionX) {
122        ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
123        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
124        return;
125    }
126
127    const Element * e = mHal.state.type->getElement()->getField(cIdx);
128    uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
129    if (sizeBytes != e->getSizeBytes() * elemArraySize) {
130        ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
131        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
132        return;
133    }
134
135    rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
136    sendDirty(rsc);
137}
138
139void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
140                                const void *data, uint32_t cIdx, size_t sizeBytes) {
141    size_t eSize = mHal.state.elementSizeBytes;
142
143    if (x >= mHal.state.dimensionX) {
144        ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
145        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
146        return;
147    }
148
149    if (y >= mHal.state.dimensionY) {
150        ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
151        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
152        return;
153    }
154
155    if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
156        ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
157        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
158        return;
159    }
160
161    const Element * e = mHal.state.type->getElement()->getField(cIdx);
162    uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
163    if (sizeBytes != e->getSizeBytes() * elemArraySize) {
164        ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
165        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
166        return;
167    }
168
169    rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
170    sendDirty(rsc);
171}
172
173void Allocation::addProgramToDirty(const Program *p) {
174    mToDirtyList.push(p);
175}
176
177void Allocation::removeProgramToDirty(const Program *p) {
178    for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
179        if (mToDirtyList[ct] == p) {
180            mToDirtyList.removeAt(ct);
181            return;
182        }
183    }
184    rsAssert(0);
185}
186
187void Allocation::dumpLOGV(const char *prefix) const {
188    ObjectBase::dumpLOGV(prefix);
189
190    String8 s(prefix);
191    s.append(" type ");
192    if (mHal.state.type) {
193        mHal.state.type->dumpLOGV(s.string());
194    }
195
196    ALOGV("%s allocation ptr=%p  mUsageFlags=0x04%x, mMipmapControl=0x%04x",
197         prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl);
198}
199
200uint32_t Allocation::getPackedSize() const {
201    uint32_t numItems = mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes();
202    return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
203}
204
205void Allocation::writePackedData(const Type *type,
206                                 uint8_t *dst, const uint8_t *src, bool dstPadded) {
207    const Element *elem = type->getElement();
208    uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
209    uint32_t paddedBytes = elem->getSizeBytes();
210    uint32_t numItems = type->getSizeBytes() / paddedBytes;
211
212    uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
213    uint32_t dstInc =  dstPadded ? paddedBytes : unpaddedBytes;
214
215    // no sub-elements
216    uint32_t fieldCount = elem->getFieldCount();
217    if (fieldCount == 0) {
218        for (uint32_t i = 0; i < numItems; i ++) {
219            memcpy(dst, src, unpaddedBytes);
220            src += srcInc;
221            dst += dstInc;
222        }
223        return;
224    }
225
226    // Cache offsets
227    uint32_t *offsetsPadded = new uint32_t[fieldCount];
228    uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
229    uint32_t *sizeUnpadded = new uint32_t[fieldCount];
230
231    for (uint32_t i = 0; i < fieldCount; i++) {
232        offsetsPadded[i] = elem->getFieldOffsetBytes(i);
233        offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
234        sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
235    }
236
237    uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
238    uint32_t *dstOffsets =  dstPadded ? offsetsPadded : offsetsUnpadded;
239
240    // complex elements, need to copy subelem after subelem
241    for (uint32_t i = 0; i < numItems; i ++) {
242        for (uint32_t fI = 0; fI < fieldCount; fI++) {
243            memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
244        }
245        src += srcInc;
246        dst += dstInc;
247    }
248
249    delete[] offsetsPadded;
250    delete[] offsetsUnpadded;
251    delete[] sizeUnpadded;
252}
253
254void Allocation::unpackVec3Allocation(const void *data, size_t dataSize) {
255    const uint8_t *src = (const uint8_t*)data;
256    uint8_t *dst = (uint8_t*)getPtr();
257
258    writePackedData(getType(), dst, src, true);
259}
260
261void Allocation::packVec3Allocation(OStream *stream) const {
262    uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
263    uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
264    uint32_t numItems = mHal.state.type->getSizeBytes() / paddedBytes;
265
266    const uint8_t *src = (const uint8_t*)getPtr();
267    uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
268
269    writePackedData(getType(), dst, src, false);
270    stream->addByteArray(dst, getPackedSize());
271
272    delete[] dst;
273}
274
275void Allocation::serialize(OStream *stream) const {
276    // Need to identify ourselves
277    stream->addU32((uint32_t)getClassId());
278
279    String8 name(getName());
280    stream->addString(&name);
281
282    // First thing we need to serialize is the type object since it will be needed
283    // to initialize the class
284    mHal.state.type->serialize(stream);
285
286    uint32_t dataSize = mHal.state.type->getSizeBytes();
287    // 3 element vectors are padded to 4 in memory, but padding isn't serialized
288    uint32_t packedSize = getPackedSize();
289    // Write how much data we are storing
290    stream->addU32(packedSize);
291    if (dataSize == packedSize) {
292        // Now write the data
293        stream->addByteArray(getPtr(), dataSize);
294    } else {
295        // Now write the data
296        packVec3Allocation(stream);
297    }
298}
299
300Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
301    // First make sure we are reading the correct object
302    RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
303    if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
304        ALOGE("allocation loading skipped due to invalid class id\n");
305        return NULL;
306    }
307
308    String8 name;
309    stream->loadString(&name);
310
311    Type *type = Type::createFromStream(rsc, stream);
312    if (!type) {
313        return NULL;
314    }
315    type->compute();
316
317    Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
318    type->decUserRef();
319
320    // Number of bytes we wrote out for this allocation
321    uint32_t dataSize = stream->loadU32();
322    // 3 element vectors are padded to 4 in memory, but padding isn't serialized
323    uint32_t packedSize = alloc->getPackedSize();
324    if (dataSize != type->getSizeBytes() &&
325        dataSize != packedSize) {
326        ALOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
327        ObjectBase::checkDelete(alloc);
328        ObjectBase::checkDelete(type);
329        return NULL;
330    }
331
332    alloc->setName(name.string(), name.size());
333
334    if (dataSize == type->getSizeBytes()) {
335        uint32_t count = dataSize / type->getElementSizeBytes();
336        // Read in all of our allocation data
337        alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
338    } else {
339        alloc->unpackVec3Allocation(stream->getPtr() + stream->getPos(), dataSize);
340    }
341    stream->reset(stream->getPos() + dataSize);
342
343    return alloc;
344}
345
346void Allocation::sendDirty(const Context *rsc) const {
347    for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
348        mToDirtyList[ct]->forceDirty();
349    }
350    mRSC->mHal.funcs.allocation.markDirty(rsc, this);
351}
352
353void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
354    const uint8_t *p = static_cast<const uint8_t *>(ptr);
355    const Element *e = mHal.state.type->getElement();
356    uint32_t stride = e->getSizeBytes();
357
358    p += stride * startOff;
359    while (ct > 0) {
360        e->incRefs(p);
361        ct --;
362        p += stride;
363    }
364}
365
366void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
367    if (!mHal.state.hasReferences || !getIsScript()) {
368        return;
369    }
370    const uint8_t *p = static_cast<const uint8_t *>(ptr);
371    const Element *e = mHal.state.type->getElement();
372    uint32_t stride = e->getSizeBytes();
373
374    p += stride * startOff;
375    while (ct > 0) {
376        e->decRefs(p);
377        ct --;
378        p += stride;
379    }
380}
381
382void Allocation::freeChildrenUnlocked () {
383    decRefs(getPtr(), mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
384}
385
386bool Allocation::freeChildren() {
387    if (mHal.state.hasReferences) {
388        incSysRef();
389        freeChildrenUnlocked();
390        return decSysRef();
391    }
392    return false;
393}
394
395void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
396}
397
398void Allocation::resize1D(Context *rsc, uint32_t dimX) {
399    uint32_t oldDimX = mHal.state.dimensionX;
400    if (dimX == oldDimX) {
401        return;
402    }
403
404    ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
405    if (dimX < oldDimX) {
406        decRefs(getPtr(), oldDimX - dimX, dimX);
407    }
408    rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
409    setType(t.get());
410    updateCache();
411}
412
413void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
414    ALOGE("not implemented");
415}
416
417int32_t Allocation::getSurfaceTextureID(const Context *rsc) {
418    int32_t id = rsc->mHal.funcs.allocation.initSurfaceTexture(rsc, this);
419    mHal.state.surfaceTextureID = id;
420    return id;
421}
422
423/////////////////
424//
425
426namespace android {
427namespace renderscript {
428
429static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
430
431static void mip565(const Adapter2D &out, const Adapter2D &in) {
432    uint32_t w = out.getDimX();
433    uint32_t h = out.getDimY();
434
435    for (uint32_t y=0; y < h; y++) {
436        uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
437        const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
438        const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
439
440        for (uint32_t x=0; x < w; x++) {
441            *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
442            oPtr ++;
443            i1 += 2;
444            i2 += 2;
445        }
446    }
447}
448
449static void mip8888(const Adapter2D &out, const Adapter2D &in) {
450    uint32_t w = out.getDimX();
451    uint32_t h = out.getDimY();
452
453    for (uint32_t y=0; y < h; y++) {
454        uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
455        const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
456        const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
457
458        for (uint32_t x=0; x < w; x++) {
459            *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
460            oPtr ++;
461            i1 += 2;
462            i2 += 2;
463        }
464    }
465}
466
467static void mip8(const Adapter2D &out, const Adapter2D &in) {
468    uint32_t w = out.getDimX();
469    uint32_t h = out.getDimY();
470
471    for (uint32_t y=0; y < h; y++) {
472        uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
473        const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
474        const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
475
476        for (uint32_t x=0; x < w; x++) {
477            *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
478            oPtr ++;
479            i1 += 2;
480            i2 += 2;
481        }
482    }
483}
484
485static void mip(const Adapter2D &out, const Adapter2D &in) {
486    switch (out.getBaseType()->getElement()->getSizeBits()) {
487    case 32:
488        mip8888(out, in);
489        break;
490    case 16:
491        mip565(out, in);
492        break;
493    case 8:
494        mip8(out, in);
495        break;
496    }
497}
498
499void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
500    Allocation *a = static_cast<Allocation *>(va);
501    a->sendDirty(rsc);
502    a->syncAll(rsc, src);
503}
504
505void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
506    Allocation *texAlloc = static_cast<Allocation *>(va);
507    AllocationGenerateScriptMips(rsc, texAlloc);
508}
509
510void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
511    Allocation *texAlloc = static_cast<Allocation *>(va);
512    const Type * t = texAlloc->getType();
513
514    size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
515    if (s != dataLen) {
516        rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
517        return;
518    }
519
520    memcpy(data, texAlloc->getPtr(), s);
521}
522
523void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
524                          uint32_t count, const void *data, size_t sizeBytes) {
525    Allocation *a = static_cast<Allocation *>(va);
526    a->data(rsc, xoff, lod, count, data, sizeBytes);
527}
528
529void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
530                                 const void *data, size_t sizeBytes, size_t eoff) {
531    Allocation *a = static_cast<Allocation *>(va);
532    a->elementData(rsc, x, y, data, eoff, sizeBytes);
533}
534
535void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
536                                 const void *data, size_t sizeBytes, size_t eoff) {
537    Allocation *a = static_cast<Allocation *>(va);
538    a->elementData(rsc, x, data, eoff, sizeBytes);
539}
540
541void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
542                          uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
543    Allocation *a = static_cast<Allocation *>(va);
544    a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
545}
546
547void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
548    Allocation *a = static_cast<Allocation *>(va);
549    a->read(data);
550}
551
552void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
553    Allocation *a = static_cast<Allocation *>(va);
554    a->resize1D(rsc, dimX);
555}
556
557void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
558    Allocation *a = static_cast<Allocation *>(va);
559    a->resize2D(rsc, dimX, dimY);
560}
561
562static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
563    Context *rsc = static_cast<Context *>(con);
564    Allocation *texAlloc = static_cast<Allocation *>(va);
565    uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
566    for (uint32_t face = 0; face < numFaces; face ++) {
567        Adapter2D adapt(rsc, texAlloc);
568        Adapter2D adapt2(rsc, texAlloc);
569        adapt.setFace(face);
570        adapt2.setFace(face);
571        for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
572            adapt.setLOD(lod);
573            adapt2.setLOD(lod + 1);
574            mip(adapt2, adapt);
575        }
576    }
577}
578
579RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
580                                       RsAllocationMipmapControl mips,
581                                       uint32_t usages, uint32_t ptr) {
582    Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips, (void *)ptr);
583    if (!alloc) {
584        return NULL;
585    }
586    alloc->incUserRef();
587    return alloc;
588}
589
590RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
591                                            RsAllocationMipmapControl mips,
592                                            const void *data, size_t data_length, uint32_t usages) {
593    Type *t = static_cast<Type *>(vtype);
594
595    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
596    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
597    if (texAlloc == NULL) {
598        ALOGE("Memory allocation failure");
599        return NULL;
600    }
601
602    memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
603    if (mips == RS_ALLOCATION_MIPMAP_FULL) {
604        AllocationGenerateScriptMips(rsc, texAlloc);
605    }
606
607    texAlloc->sendDirty(rsc);
608    return texAlloc;
609}
610
611RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
612                                                RsAllocationMipmapControl mips,
613                                                const void *data, size_t data_length, uint32_t usages) {
614    Type *t = static_cast<Type *>(vtype);
615
616    // Cubemap allocation's faces should be Width by Width each.
617    // Source data should have 6 * Width by Width pixels
618    // Error checking is done in the java layer
619    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
620    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
621    if (texAlloc == NULL) {
622        ALOGE("Memory allocation failure");
623        return NULL;
624    }
625
626    uint32_t faceSize = t->getDimX();
627    uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
628    uint32_t copySize = faceSize * t->getElementSizeBytes();
629
630    uint8_t *sourcePtr = (uint8_t*)data;
631    for (uint32_t face = 0; face < 6; face ++) {
632        Adapter2D faceAdapter(rsc, texAlloc);
633        faceAdapter.setFace(face);
634
635        for (uint32_t dI = 0; dI < faceSize; dI ++) {
636            memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
637        }
638
639        // Move the data pointer to the next cube face
640        sourcePtr += copySize;
641    }
642
643    if (mips == RS_ALLOCATION_MIPMAP_FULL) {
644        AllocationGenerateScriptMips(rsc, texAlloc);
645    }
646
647    texAlloc->sendDirty(rsc);
648    return texAlloc;
649}
650
651void rsi_AllocationCopy2DRange(Context *rsc,
652                               RsAllocation dstAlloc,
653                               uint32_t dstXoff, uint32_t dstYoff,
654                               uint32_t dstMip, uint32_t dstFace,
655                               uint32_t width, uint32_t height,
656                               RsAllocation srcAlloc,
657                               uint32_t srcXoff, uint32_t srcYoff,
658                               uint32_t srcMip, uint32_t srcFace) {
659    Allocation *dst = static_cast<Allocation *>(dstAlloc);
660    Allocation *src= static_cast<Allocation *>(srcAlloc);
661    rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
662                                           (RsAllocationCubemapFace)dstFace,
663                                           width, height,
664                                           src, srcXoff, srcYoff,srcMip,
665                                           (RsAllocationCubemapFace)srcFace);
666}
667
668int32_t rsi_AllocationGetSurfaceTextureID(Context *rsc, RsAllocation valloc) {
669    Allocation *alloc = static_cast<Allocation *>(valloc);
670    return alloc->getSurfaceTextureID(rsc);
671}
672
673}
674}
675
676const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
677    Allocation *a = static_cast<Allocation *>(va);
678    a->getType()->incUserRef();
679
680    return a->getType();
681}
682