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