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