rsAllocation.cpp revision bc0ca6ba4e31239bf77060578d0bdf1a10e04168
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
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, GLConsumer *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, size_t stride) {
530    Allocation *a = static_cast<Allocation *>(va);
531    a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
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, 0);
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, 0);
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<GLConsumer *>(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
675void rsi_Allocation2DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff,
676                          uint32_t lod, RsAllocationCubemapFace face, uint32_t w,
677                          uint32_t h, void *data, size_t sizeBytes, size_t stride) {
678    Allocation *a = static_cast<Allocation *>(va);
679    a->read(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
680}
681
682}
683}
684
685const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
686    Allocation *a = static_cast<Allocation *>(va);
687    a->getType()->incUserRef();
688
689    return a->getType();
690}
691