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