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