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