rsAllocation.cpp revision 41e373d91a60043afa0f9abd026218b49cbc1201
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
416int32_t Allocation::getSurfaceTextureID(const Context *rsc) {
417    int32_t id = rsc->mHal.funcs.allocation.initSurfaceTexture(rsc, this);
418    mHal.state.surfaceTextureID = id;
419    return id;
420}
421
422/////////////////
423//
424
425namespace android {
426namespace renderscript {
427
428static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
429
430static void mip565(const Adapter2D &out, const Adapter2D &in) {
431    uint32_t w = out.getDimX();
432    uint32_t h = out.getDimY();
433
434    for (uint32_t y=0; y < h; y++) {
435        uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
436        const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
437        const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
438
439        for (uint32_t x=0; x < w; x++) {
440            *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
441            oPtr ++;
442            i1 += 2;
443            i2 += 2;
444        }
445    }
446}
447
448static void mip8888(const Adapter2D &out, const Adapter2D &in) {
449    uint32_t w = out.getDimX();
450    uint32_t h = out.getDimY();
451
452    for (uint32_t y=0; y < h; y++) {
453        uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
454        const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
455        const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
456
457        for (uint32_t x=0; x < w; x++) {
458            *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
459            oPtr ++;
460            i1 += 2;
461            i2 += 2;
462        }
463    }
464}
465
466static void mip8(const Adapter2D &out, const Adapter2D &in) {
467    uint32_t w = out.getDimX();
468    uint32_t h = out.getDimY();
469
470    for (uint32_t y=0; y < h; y++) {
471        uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
472        const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
473        const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
474
475        for (uint32_t x=0; x < w; x++) {
476            *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
477            oPtr ++;
478            i1 += 2;
479            i2 += 2;
480        }
481    }
482}
483
484static void mip(const Adapter2D &out, const Adapter2D &in) {
485    switch (out.getBaseType()->getElement()->getSizeBits()) {
486    case 32:
487        mip8888(out, in);
488        break;
489    case 16:
490        mip565(out, in);
491        break;
492    case 8:
493        mip8(out, in);
494        break;
495    }
496}
497
498void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
499    Allocation *a = static_cast<Allocation *>(va);
500    a->sendDirty(rsc);
501    a->syncAll(rsc, src);
502}
503
504void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
505    Allocation *texAlloc = static_cast<Allocation *>(va);
506    AllocationGenerateScriptMips(rsc, texAlloc);
507}
508
509void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
510    Allocation *texAlloc = static_cast<Allocation *>(va);
511    const Type * t = texAlloc->getType();
512
513    size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
514    if (s != dataLen) {
515        rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
516        return;
517    }
518
519    memcpy(data, texAlloc->getPtr(), s);
520}
521
522void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
523                          uint32_t count, const void *data, size_t sizeBytes) {
524    Allocation *a = static_cast<Allocation *>(va);
525    a->data(rsc, xoff, lod, count, data, sizeBytes);
526}
527
528void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
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, y, data, eoff, sizeBytes);
532}
533
534void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
535                                 const void *data, size_t eoff, uint32_t sizeBytes) { // TODO: this seems wrong, eoff and sizeBytes may be swapped
536    Allocation *a = static_cast<Allocation *>(va);
537    a->elementData(rsc, x, data, eoff, sizeBytes);
538}
539
540void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
541                          uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
542    Allocation *a = static_cast<Allocation *>(va);
543    a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
544}
545
546void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
547    Allocation *a = static_cast<Allocation *>(va);
548    a->read(data);
549}
550
551void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
552    Allocation *a = static_cast<Allocation *>(va);
553    a->resize1D(rsc, dimX);
554}
555
556void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
557    Allocation *a = static_cast<Allocation *>(va);
558    a->resize2D(rsc, dimX, dimY);
559}
560
561static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
562    Context *rsc = static_cast<Context *>(con);
563    Allocation *texAlloc = static_cast<Allocation *>(va);
564    uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
565    for (uint32_t face = 0; face < numFaces; face ++) {
566        Adapter2D adapt(rsc, texAlloc);
567        Adapter2D adapt2(rsc, texAlloc);
568        adapt.setFace(face);
569        adapt2.setFace(face);
570        for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
571            adapt.setLOD(lod);
572            adapt2.setLOD(lod + 1);
573            mip(adapt2, adapt);
574        }
575    }
576}
577
578RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
579                                       RsAllocationMipmapControl mips,
580                                       uint32_t usages, uint32_t ptr) {
581    Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips, (void *)ptr);
582    if (!alloc) {
583        return NULL;
584    }
585    alloc->incUserRef();
586    return alloc;
587}
588
589RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
590                                            RsAllocationMipmapControl mips,
591                                            const void *data, size_t data_length, uint32_t usages) {
592    Type *t = static_cast<Type *>(vtype);
593
594    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
595    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
596    if (texAlloc == NULL) {
597        ALOGE("Memory allocation failure");
598        return NULL;
599    }
600
601    memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
602    if (mips == RS_ALLOCATION_MIPMAP_FULL) {
603        AllocationGenerateScriptMips(rsc, texAlloc);
604    }
605
606    texAlloc->sendDirty(rsc);
607    return texAlloc;
608}
609
610RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
611                                                RsAllocationMipmapControl mips,
612                                                const void *data, size_t data_length, uint32_t usages) {
613    Type *t = static_cast<Type *>(vtype);
614
615    // Cubemap allocation's faces should be Width by Width each.
616    // Source data should have 6 * Width by Width pixels
617    // Error checking is done in the java layer
618    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
619    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
620    if (texAlloc == NULL) {
621        ALOGE("Memory allocation failure");
622        return NULL;
623    }
624
625    uint32_t faceSize = t->getDimX();
626    uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
627    uint32_t copySize = faceSize * t->getElementSizeBytes();
628
629    uint8_t *sourcePtr = (uint8_t*)data;
630    for (uint32_t face = 0; face < 6; face ++) {
631        Adapter2D faceAdapter(rsc, texAlloc);
632        faceAdapter.setFace(face);
633
634        for (uint32_t dI = 0; dI < faceSize; dI ++) {
635            memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
636        }
637
638        // Move the data pointer to the next cube face
639        sourcePtr += copySize;
640    }
641
642    if (mips == RS_ALLOCATION_MIPMAP_FULL) {
643        AllocationGenerateScriptMips(rsc, texAlloc);
644    }
645
646    texAlloc->sendDirty(rsc);
647    return texAlloc;
648}
649
650void rsi_AllocationCopy2DRange(Context *rsc,
651                               RsAllocation dstAlloc,
652                               uint32_t dstXoff, uint32_t dstYoff,
653                               uint32_t dstMip, uint32_t dstFace,
654                               uint32_t width, uint32_t height,
655                               RsAllocation srcAlloc,
656                               uint32_t srcXoff, uint32_t srcYoff,
657                               uint32_t srcMip, uint32_t srcFace) {
658    Allocation *dst = static_cast<Allocation *>(dstAlloc);
659    Allocation *src= static_cast<Allocation *>(srcAlloc);
660    rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
661                                           (RsAllocationCubemapFace)dstFace,
662                                           width, height,
663                                           src, srcXoff, srcYoff,srcMip,
664                                           (RsAllocationCubemapFace)srcFace);
665}
666
667int32_t rsi_AllocationGetSurfaceTextureID(Context *rsc, RsAllocation valloc) {
668    Allocation *alloc = static_cast<Allocation *>(valloc);
669    return alloc->getSurfaceTextureID(rsc);
670}
671
672}
673}
674
675const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
676    Allocation *a = static_cast<Allocation *>(va);
677    a->getType()->incUserRef();
678
679    return a->getType();
680}
681