rsAllocation.cpp revision a572aca4eb4ddb32c10baa1f529431cfefd756b8
1/*
2 * Copyright (C) 2009-2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsContext.h"
18#include "rsAllocation.h"
19#include "rsAdapter.h"
20#include "rs_hal.h"
21
22#include "system/window.h"
23#include "gui/GLConsumer.h"
24
25using namespace android;
26using namespace android::renderscript;
27
28Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
29                       RsAllocationMipmapControl mc, void * ptr)
30    : ObjectBase(rsc) {
31
32    memset(&mHal, 0, sizeof(mHal));
33    mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE;
34    mHal.state.usageFlags = usages;
35    mHal.state.mipmapControl = mc;
36    mHal.state.userProvidedPtr = ptr;
37
38    setType(type);
39    updateCache();
40}
41
42Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
43                              RsAllocationMipmapControl mc, void * ptr) {
44    Allocation *a = new Allocation(rsc, type, usages, mc, ptr);
45
46    if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) {
47        rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
48        delete a;
49        return NULL;
50    }
51
52    return a;
53}
54
55void Allocation::updateCache() {
56    const Type *type = mHal.state.type;
57    mHal.state.yuv = type->getDimYuv();
58    mHal.state.hasFaces = type->getDimFaces();
59    mHal.state.hasMipmaps = type->getDimLOD();
60    mHal.state.elementSizeBytes = type->getElementSizeBytes();
61    mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences();
62}
63
64Allocation::~Allocation() {
65    freeChildrenUnlocked();
66    setSurfaceTexture(mRSC, NULL);
67    mRSC->mHal.funcs.allocation.destroy(mRSC, this);
68}
69
70void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
71    rsc->mHal.funcs.allocation.syncAll(rsc, this, src);
72}
73
74void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
75                         uint32_t count, const void *data, size_t sizeBytes) {
76    const size_t eSize = mHal.state.type->getElementSizeBytes();
77
78    if ((count * eSize) != sizeBytes) {
79        ALOGE("Allocation::subData called with mismatched size expected %zu, got %zu",
80             (count * eSize), sizeBytes);
81        mHal.state.type->dumpLOGV("type info");
82        return;
83    }
84
85    rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes);
86    sendDirty(rsc);
87}
88
89void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
90                      uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
91
92    const size_t eSize = mHal.state.elementSizeBytes;
93    const size_t lineSize = eSize * w;
94
95    if ((lineSize * h) != sizeBytes) {
96        ALOGE("Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes);
97        rsAssert(!"Allocation::subData called with mismatched size");
98        return;
99    }
100
101    this->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, lineSize);
102}
103
104void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
105                      uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
106    const size_t eSize = mHal.state.elementSizeBytes;
107    const size_t lineSize = eSize * w;
108
109    //ALOGE("data2d %p,  %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes);
110
111    rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
112    sendDirty(rsc);
113}
114
115void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
116                      uint32_t lod, RsAllocationCubemapFace face,
117                      uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes) {
118}
119
120void Allocation::read(Context *rsc, uint32_t xoff, uint32_t lod,
121                      uint32_t count, void *data, size_t sizeBytes) {
122    const size_t eSize = mHal.state.type->getElementSizeBytes();
123
124    if ((count * eSize) != sizeBytes) {
125        ALOGE("Allocation::read called with mismatched size expected %zu, got %zu",
126             (count * eSize), sizeBytes);
127        mHal.state.type->dumpLOGV("type info");
128        return;
129    }
130
131    rsc->mHal.funcs.allocation.read1D(rsc, this, xoff, lod, count, data, sizeBytes);
132}
133
134void Allocation::readUnchecked(Context *rsc, uint32_t xoff, uint32_t lod,
135                         uint32_t count, void *data, size_t sizeBytes) {
136    rsc->mHal.funcs.allocation.read1D(rsc, this, xoff, lod, count, data, sizeBytes);
137}
138
139
140void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
141                      uint32_t w, uint32_t h, void *data, size_t sizeBytes) {
142    const size_t eSize = mHal.state.elementSizeBytes;
143    const size_t lineSize = eSize * w;
144
145    if ((lineSize * h) != sizeBytes) {
146        ALOGE("Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes);
147        rsAssert(!"Allocation::read called with mismatched size");
148        return;
149    }
150
151    read(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, lineSize);
152}
153
154void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
155                      uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
156    const size_t eSize = mHal.state.elementSizeBytes;
157    const size_t lineSize = eSize * w;
158    if (!stride) {
159        stride = lineSize;
160    }
161
162    rsc->mHal.funcs.allocation.read2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
163}
164
165void Allocation::read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
166                      uint32_t lod, RsAllocationCubemapFace face,
167                      uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes) {
168}
169
170void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
171                                uint32_t cIdx, size_t sizeBytes) {
172    size_t eSize = mHal.state.elementSizeBytes;
173
174    if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
175        ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
176        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
177        return;
178    }
179
180    if (x >= mHal.drvState.lod[0].dimX) {
181        ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
182        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
183        return;
184    }
185
186    const Element * e = mHal.state.type->getElement()->getField(cIdx);
187    uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
188    if (sizeBytes != e->getSizeBytes() * elemArraySize) {
189        ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
190        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
191        return;
192    }
193
194    rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes);
195    sendDirty(rsc);
196}
197
198void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
199                                const void *data, uint32_t cIdx, size_t sizeBytes) {
200    size_t eSize = mHal.state.elementSizeBytes;
201
202    if (x >= mHal.drvState.lod[0].dimX) {
203        ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
204        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
205        return;
206    }
207
208    if (y >= mHal.drvState.lod[0].dimY) {
209        ALOGE("Error Allocation::subElementData X offset %i out of range.", x);
210        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
211        return;
212    }
213
214    if (cIdx >= mHal.state.type->getElement()->getFieldCount()) {
215        ALOGE("Error Allocation::subElementData component %i out of range.", cIdx);
216        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
217        return;
218    }
219
220    const Element * e = mHal.state.type->getElement()->getField(cIdx);
221    uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx);
222    if (sizeBytes != e->getSizeBytes() * elemArraySize) {
223        ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes());
224        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
225        return;
226    }
227
228    rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes);
229    sendDirty(rsc);
230}
231
232void Allocation::addProgramToDirty(const Program *p) {
233    mToDirtyList.push(p);
234}
235
236void Allocation::removeProgramToDirty(const Program *p) {
237    for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
238        if (mToDirtyList[ct] == p) {
239            mToDirtyList.removeAt(ct);
240            return;
241        }
242    }
243    rsAssert(0);
244}
245
246void Allocation::dumpLOGV(const char *prefix) const {
247    ObjectBase::dumpLOGV(prefix);
248
249    String8 s(prefix);
250    s.append(" type ");
251    if (mHal.state.type) {
252        mHal.state.type->dumpLOGV(s.string());
253    }
254
255    ALOGV("%s allocation ptr=%p  mUsageFlags=0x04%x, mMipmapControl=0x%04x",
256         prefix, mHal.drvState.lod[0].mallocPtr, mHal.state.usageFlags, mHal.state.mipmapControl);
257}
258
259uint32_t Allocation::getPackedSize() const {
260    uint32_t numItems = mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes();
261    return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded();
262}
263
264void Allocation::writePackedData(Context *rsc, const Type *type,
265                                 uint8_t *dst, const uint8_t *src, bool dstPadded) {
266    const Element *elem = type->getElement();
267    uint32_t unpaddedBytes = elem->getSizeBytesUnpadded();
268    uint32_t paddedBytes = elem->getSizeBytes();
269    uint32_t numItems = type->getSizeBytes() / paddedBytes;
270
271    uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes;
272    uint32_t dstInc =  dstPadded ? paddedBytes : unpaddedBytes;
273
274    // no sub-elements
275    uint32_t fieldCount = elem->getFieldCount();
276    if (fieldCount == 0) {
277        for (uint32_t i = 0; i < numItems; i ++) {
278            memcpy(dst, src, unpaddedBytes);
279            src += srcInc;
280            dst += dstInc;
281        }
282        return;
283    }
284
285    // Cache offsets
286    uint32_t *offsetsPadded = new uint32_t[fieldCount];
287    uint32_t *offsetsUnpadded = new uint32_t[fieldCount];
288    uint32_t *sizeUnpadded = new uint32_t[fieldCount];
289
290    for (uint32_t i = 0; i < fieldCount; i++) {
291        offsetsPadded[i] = elem->getFieldOffsetBytes(i);
292        offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i);
293        sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded();
294    }
295
296    uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded;
297    uint32_t *dstOffsets =  dstPadded ? offsetsPadded : offsetsUnpadded;
298
299    // complex elements, need to copy subelem after subelem
300    for (uint32_t i = 0; i < numItems; i ++) {
301        for (uint32_t fI = 0; fI < fieldCount; fI++) {
302            memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]);
303        }
304        src += srcInc;
305        dst += dstInc;
306    }
307
308    delete[] offsetsPadded;
309    delete[] offsetsUnpadded;
310    delete[] sizeUnpadded;
311}
312
313void Allocation::unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize) {
314    const uint8_t *src = (const uint8_t*)data;
315    uint8_t *dst = (uint8_t *)rsc->mHal.funcs.allocation.lock1D(rsc, this);
316
317    writePackedData(rsc, getType(), dst, src, true);
318    rsc->mHal.funcs.allocation.unlock1D(rsc, this);
319}
320
321void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const {
322    uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
323    uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
324    uint32_t numItems = mHal.state.type->getSizeBytes() / paddedBytes;
325
326    const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this);
327    uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
328
329    writePackedData(rsc, getType(), dst, src, false);
330    stream->addByteArray(dst, getPackedSize());
331
332    delete[] dst;
333    rsc->mHal.funcs.allocation.unlock1D(rsc, this);
334}
335
336void Allocation::serialize(Context *rsc, OStream *stream) const {
337    // Need to identify ourselves
338    stream->addU32((uint32_t)getClassId());
339
340    String8 name(getName());
341    stream->addString(&name);
342
343    // First thing we need to serialize is the type object since it will be needed
344    // to initialize the class
345    mHal.state.type->serialize(rsc, stream);
346
347    uint32_t dataSize = mHal.state.type->getSizeBytes();
348    // 3 element vectors are padded to 4 in memory, but padding isn't serialized
349    uint32_t packedSize = getPackedSize();
350    // Write how much data we are storing
351    stream->addU32(packedSize);
352    if (dataSize == packedSize) {
353        // Now write the data
354        stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize);
355        rsc->mHal.funcs.allocation.unlock1D(rsc, this);
356    } else {
357        // Now write the data
358        packVec3Allocation(rsc, stream);
359    }
360}
361
362Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
363    // First make sure we are reading the correct object
364    RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
365    if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
366        ALOGE("allocation loading skipped due to invalid class id\n");
367        return NULL;
368    }
369
370    String8 name;
371    stream->loadString(&name);
372
373    Type *type = Type::createFromStream(rsc, stream);
374    if (!type) {
375        return NULL;
376    }
377    type->compute();
378
379    Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
380    type->decUserRef();
381
382    // Number of bytes we wrote out for this allocation
383    uint32_t dataSize = stream->loadU32();
384    // 3 element vectors are padded to 4 in memory, but padding isn't serialized
385    uint32_t packedSize = alloc->getPackedSize();
386    if (dataSize != type->getSizeBytes() &&
387        dataSize != packedSize) {
388        ALOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
389        ObjectBase::checkDelete(alloc);
390        ObjectBase::checkDelete(type);
391        return NULL;
392    }
393
394    alloc->setName(name.string(), name.size());
395
396    if (dataSize == type->getSizeBytes()) {
397        uint32_t count = dataSize / type->getElementSizeBytes();
398        // Read in all of our allocation data
399        alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
400    } else {
401        alloc->unpackVec3Allocation(rsc, stream->getPtr() + stream->getPos(), dataSize);
402    }
403    stream->reset(stream->getPos() + dataSize);
404
405    return alloc;
406}
407
408void Allocation::sendDirty(const Context *rsc) const {
409#ifndef RS_COMPATIBILITY_LIB
410    for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
411        mToDirtyList[ct]->forceDirty();
412    }
413#endif
414    mRSC->mHal.funcs.allocation.markDirty(rsc, this);
415}
416
417void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
418    mHal.state.type->incRefs(ptr, ct, startOff);
419}
420
421void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
422    if (!mHal.state.hasReferences || !getIsScript()) {
423        return;
424    }
425    mHal.state.type->decRefs(ptr, ct, startOff);
426}
427
428void Allocation::freeChildrenUnlocked () {
429    void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this);
430    decRefs(ptr, mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
431    mRSC->mHal.funcs.allocation.unlock1D(mRSC, this);
432}
433
434bool Allocation::freeChildren() {
435    if (mHal.state.hasReferences) {
436        incSysRef();
437        freeChildrenUnlocked();
438        return decSysRef();
439    }
440    return false;
441}
442
443void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
444}
445
446void Allocation::resize1D(Context *rsc, uint32_t dimX) {
447    uint32_t oldDimX = mHal.drvState.lod[0].dimX;
448    if (dimX == oldDimX) {
449        return;
450    }
451
452    ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
453    if (dimX < oldDimX) {
454        decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX);
455        rsc->mHal.funcs.allocation.unlock1D(rsc, this);
456    }
457    rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
458    setType(t.get());
459    updateCache();
460}
461
462void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
463    ALOGE("not implemented");
464}
465
466int32_t Allocation::getSurfaceTextureID(const Context *rsc) {
467    int32_t id = rsc->mHal.funcs.allocation.initSurfaceTexture(rsc, this);
468    mHal.state.surfaceTextureID = id;
469    return id;
470}
471
472void Allocation::setSurfaceTexture(const Context *rsc, GLConsumer *st) {
473    if(st != mHal.state.surfaceTexture) {
474        if(mHal.state.surfaceTexture != NULL) {
475            mHal.state.surfaceTexture->decStrong(NULL);
476        }
477        mHal.state.surfaceTexture = st;
478        if(mHal.state.surfaceTexture != NULL) {
479            mHal.state.surfaceTexture->incStrong(NULL);
480        }
481    }
482}
483
484void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
485    ANativeWindow *nw = (ANativeWindow *)sur;
486    ANativeWindow *old = mHal.state.wndSurface;
487    if (nw) {
488        nw->incStrong(NULL);
489    }
490    rsc->mHal.funcs.allocation.setSurfaceTexture(rsc, this, nw);
491    mHal.state.wndSurface = nw;
492    if (old) {
493        old->decStrong(NULL);
494    }
495}
496
497void Allocation::ioSend(const Context *rsc) {
498    rsc->mHal.funcs.allocation.ioSend(rsc, this);
499}
500
501void Allocation::ioReceive(const Context *rsc) {
502    rsc->mHal.funcs.allocation.ioReceive(rsc, this);
503}
504
505
506/////////////////
507//
508
509namespace android {
510namespace renderscript {
511
512void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
513    Allocation *a = static_cast<Allocation *>(va);
514    a->sendDirty(rsc);
515    a->syncAll(rsc, src);
516}
517
518void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
519    Allocation *alloc = static_cast<Allocation *>(va);
520    rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc);
521}
522
523void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
524    Allocation *a = static_cast<Allocation *>(va);
525    const Type * t = a->getType();
526    a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
527            t->getDimX(), t->getDimY(), data, sizeBytes);
528}
529
530void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
531                          uint32_t count, const void *data, size_t sizeBytes) {
532    Allocation *a = static_cast<Allocation *>(va);
533    a->data(rsc, xoff, lod, count, data, sizeBytes);
534}
535
536void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
537                                 const void *data, size_t sizeBytes, size_t eoff) {
538    Allocation *a = static_cast<Allocation *>(va);
539    a->elementData(rsc, x, y, data, eoff, sizeBytes);
540}
541
542void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
543                                 const void *data, size_t sizeBytes, size_t eoff) {
544    Allocation *a = static_cast<Allocation *>(va);
545    a->elementData(rsc, x, data, eoff, sizeBytes);
546}
547
548void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
549                          uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
550    Allocation *a = static_cast<Allocation *>(va);
551    a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
552}
553
554void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
555    Allocation *a = static_cast<Allocation *>(va);
556    const Type * t = a->getType();
557    if(t->getDimY()) {
558        a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
559                t->getDimX(), t->getDimY(), data, sizeBytes);
560    } else {
561        a->read(rsc, 0, 0, t->getDimX(), data, sizeBytes);
562    }
563
564}
565
566void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
567    Allocation *a = static_cast<Allocation *>(va);
568    a->resize1D(rsc, dimX);
569}
570
571void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
572    Allocation *a = static_cast<Allocation *>(va);
573    a->resize2D(rsc, dimX, dimY);
574}
575
576RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
577                                       RsAllocationMipmapControl mips,
578                                       uint32_t usages, uint32_t ptr) {
579    Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips, (void *)ptr);
580    if (!alloc) {
581        return NULL;
582    }
583    alloc->incUserRef();
584    return alloc;
585}
586
587RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
588                                            RsAllocationMipmapControl mips,
589                                            const void *data, size_t sizeBytes, uint32_t usages) {
590    Type *t = static_cast<Type *>(vtype);
591
592    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
593    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
594    if (texAlloc == NULL) {
595        ALOGE("Memory allocation failure");
596        return NULL;
597    }
598
599    texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
600                   t->getDimX(), t->getDimY(), data, sizeBytes);
601    if (mips == RS_ALLOCATION_MIPMAP_FULL) {
602        rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
603    }
604
605    texAlloc->sendDirty(rsc);
606    return texAlloc;
607}
608
609RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
610                                                RsAllocationMipmapControl mips,
611                                                const void *data, size_t sizeBytes, uint32_t usages) {
612    Type *t = static_cast<Type *>(vtype);
613
614    // Cubemap allocation's faces should be Width by Width each.
615    // Source data should have 6 * Width by Width pixels
616    // Error checking is done in the java layer
617    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
618    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
619    if (texAlloc == NULL) {
620        ALOGE("Memory allocation failure");
621        return NULL;
622    }
623
624    uint32_t faceSize = t->getDimX();
625    uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
626    uint32_t copySize = faceSize * t->getElementSizeBytes();
627
628    uint8_t *sourcePtr = (uint8_t*)data;
629    for (uint32_t face = 0; face < 6; face ++) {
630        for (uint32_t dI = 0; dI < faceSize; dI ++) {
631            texAlloc->data(rsc, 0, dI, 0, (RsAllocationCubemapFace)face,
632                           t->getDimX(), 1, sourcePtr + strideBytes * dI, copySize);
633        }
634
635        // Move the data pointer to the next cube face
636        sourcePtr += copySize;
637    }
638
639    if (mips == RS_ALLOCATION_MIPMAP_FULL) {
640        rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
641    }
642
643    texAlloc->sendDirty(rsc);
644    return texAlloc;
645}
646
647void rsi_AllocationCopy2DRange(Context *rsc,
648                               RsAllocation dstAlloc,
649                               uint32_t dstXoff, uint32_t dstYoff,
650                               uint32_t dstMip, uint32_t dstFace,
651                               uint32_t width, uint32_t height,
652                               RsAllocation srcAlloc,
653                               uint32_t srcXoff, uint32_t srcYoff,
654                               uint32_t srcMip, uint32_t srcFace) {
655    Allocation *dst = static_cast<Allocation *>(dstAlloc);
656    Allocation *src= static_cast<Allocation *>(srcAlloc);
657    rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
658                                           (RsAllocationCubemapFace)dstFace,
659                                           width, height,
660                                           src, srcXoff, srcYoff,srcMip,
661                                           (RsAllocationCubemapFace)srcFace);
662}
663
664int32_t rsi_AllocationGetSurfaceTextureID(Context *rsc, RsAllocation valloc) {
665    Allocation *alloc = static_cast<Allocation *>(valloc);
666    return alloc->getSurfaceTextureID(rsc);
667}
668
669void rsi_AllocationGetSurfaceTextureID2(Context *rsc, RsAllocation valloc, void *vst, size_t len) {
670    Allocation *alloc = static_cast<Allocation *>(valloc);
671    alloc->setSurfaceTexture(rsc, static_cast<GLConsumer *>(vst));
672}
673
674void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) {
675    Allocation *alloc = static_cast<Allocation *>(valloc);
676    alloc->setSurface(rsc, sur);
677}
678
679void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) {
680    Allocation *alloc = static_cast<Allocation *>(valloc);
681    alloc->ioSend(rsc);
682}
683
684void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) {
685    Allocation *alloc = static_cast<Allocation *>(valloc);
686    alloc->ioReceive(rsc);
687}
688
689void rsi_Allocation1DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
690                          uint32_t count, void *data, size_t sizeBytes) {
691    Allocation *a = static_cast<Allocation *>(va);
692    a->readUnchecked(rsc, xoff, lod, count, data, sizeBytes);
693}
694
695void rsi_Allocation2DRead(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff,
696                          uint32_t lod, RsAllocationCubemapFace face, uint32_t w,
697                          uint32_t h, void *data, size_t sizeBytes, size_t stride) {
698    Allocation *a = static_cast<Allocation *>(va);
699    a->read(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes, stride);
700}
701
702}
703}
704
705const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
706    Allocation *a = static_cast<Allocation *>(va);
707    a->getType()->incUserRef();
708
709    return a->getType();
710}
711