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