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