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