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