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