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