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