rsAllocation.cpp revision 9f70a4e63825afe9f786483722e1669b3625f5e9
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    mHal.state.type->incRefs(ptr, ct, startOff);
361}
362
363void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
364    if (!mHal.state.hasReferences || !getIsScript()) {
365        return;
366    }
367    mHal.state.type->decRefs(ptr, ct, startOff);
368}
369
370void Allocation::freeChildrenUnlocked () {
371    decRefs(getPtr(), mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
372}
373
374bool Allocation::freeChildren() {
375    if (mHal.state.hasReferences) {
376        incSysRef();
377        freeChildrenUnlocked();
378        return decSysRef();
379    }
380    return false;
381}
382
383void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
384}
385
386void Allocation::resize1D(Context *rsc, uint32_t dimX) {
387    uint32_t oldDimX = mHal.state.dimensionX;
388    if (dimX == oldDimX) {
389        return;
390    }
391
392    ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
393    if (dimX < oldDimX) {
394        decRefs(getPtr(), oldDimX - dimX, dimX);
395    }
396    rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
397    setType(t.get());
398    updateCache();
399}
400
401void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
402    ALOGE("not implemented");
403}
404
405int32_t Allocation::getSurfaceTextureID(const Context *rsc) {
406    int32_t id = rsc->mHal.funcs.allocation.initSurfaceTexture(rsc, this);
407    mHal.state.surfaceTextureID = id;
408    return id;
409}
410
411void Allocation::setSurfaceTexture(const Context *rsc, SurfaceTexture *st) {
412    if(st != mHal.state.surfaceTexture) {
413        if(mHal.state.surfaceTexture != NULL) {
414            mHal.state.surfaceTexture->decStrong(NULL);
415        }
416        mHal.state.surfaceTexture = st;
417        if(mHal.state.surfaceTexture != NULL) {
418            mHal.state.surfaceTexture->incStrong(NULL);
419        }
420    }
421}
422
423void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
424    ANativeWindow *nw = (ANativeWindow *)sur;
425    ANativeWindow *old = mHal.state.wndSurface;
426    if (nw) {
427        nw->incStrong(NULL);
428    }
429    rsc->mHal.funcs.allocation.setSurfaceTexture(rsc, this, nw);
430    mHal.state.wndSurface = nw;
431    if (old) {
432        old->decStrong(NULL);
433    }
434}
435
436void Allocation::ioSend(const Context *rsc) {
437    rsc->mHal.funcs.allocation.ioSend(rsc, this);
438}
439
440void Allocation::ioReceive(const Context *rsc) {
441    rsc->mHal.funcs.allocation.ioReceive(rsc, this);
442}
443
444
445/////////////////
446//
447
448namespace android {
449namespace renderscript {
450
451static void AllocationGenerateScriptMips(RsContext con, RsAllocation va);
452
453static void mip565(const Adapter2D &out, const Adapter2D &in) {
454    uint32_t w = out.getDimX();
455    uint32_t h = out.getDimY();
456
457    for (uint32_t y=0; y < h; y++) {
458        uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
459        const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
460        const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
461
462        for (uint32_t x=0; x < w; x++) {
463            *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
464            oPtr ++;
465            i1 += 2;
466            i2 += 2;
467        }
468    }
469}
470
471static void mip8888(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        uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
477        const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
478        const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
479
480        for (uint32_t x=0; x < w; x++) {
481            *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
482            oPtr ++;
483            i1 += 2;
484            i2 += 2;
485        }
486    }
487}
488
489static void mip8(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        uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
495        const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
496        const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
497
498        for (uint32_t x=0; x < w; x++) {
499            *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
500            oPtr ++;
501            i1 += 2;
502            i2 += 2;
503        }
504    }
505}
506
507static void mip(const Adapter2D &out, const Adapter2D &in) {
508    switch (out.getBaseType()->getElement()->getSizeBits()) {
509    case 32:
510        mip8888(out, in);
511        break;
512    case 16:
513        mip565(out, in);
514        break;
515    case 8:
516        mip8(out, in);
517        break;
518    }
519}
520
521void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
522    Allocation *a = static_cast<Allocation *>(va);
523    a->sendDirty(rsc);
524    a->syncAll(rsc, src);
525}
526
527void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
528    Allocation *texAlloc = static_cast<Allocation *>(va);
529    AllocationGenerateScriptMips(rsc, texAlloc);
530}
531
532void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
533    Allocation *texAlloc = static_cast<Allocation *>(va);
534    const Type * t = texAlloc->getType();
535
536    size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
537    if (s != dataLen) {
538        rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
539        return;
540    }
541
542    memcpy(data, texAlloc->getPtr(), s);
543}
544
545void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
546                          uint32_t count, const void *data, size_t sizeBytes) {
547    Allocation *a = static_cast<Allocation *>(va);
548    a->data(rsc, xoff, lod, count, data, sizeBytes);
549}
550
551void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
552                                 const void *data, size_t sizeBytes, size_t eoff) {
553    Allocation *a = static_cast<Allocation *>(va);
554    a->elementData(rsc, x, y, data, eoff, sizeBytes);
555}
556
557void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
558                                 const void *data, size_t sizeBytes, size_t eoff) {
559    Allocation *a = static_cast<Allocation *>(va);
560    a->elementData(rsc, x, data, eoff, sizeBytes);
561}
562
563void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
564                          uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
565    Allocation *a = static_cast<Allocation *>(va);
566    a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
567}
568
569void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) {
570    Allocation *a = static_cast<Allocation *>(va);
571    a->read(data);
572}
573
574void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
575    Allocation *a = static_cast<Allocation *>(va);
576    a->resize1D(rsc, dimX);
577}
578
579void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
580    Allocation *a = static_cast<Allocation *>(va);
581    a->resize2D(rsc, dimX, dimY);
582}
583
584static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) {
585    Context *rsc = static_cast<Context *>(con);
586    Allocation *texAlloc = static_cast<Allocation *>(va);
587    uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
588    for (uint32_t face = 0; face < numFaces; face ++) {
589        Adapter2D adapt(rsc, texAlloc);
590        Adapter2D adapt2(rsc, texAlloc);
591        adapt.setFace(face);
592        adapt2.setFace(face);
593        for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
594            adapt.setLOD(lod);
595            adapt2.setLOD(lod + 1);
596            mip(adapt2, adapt);
597        }
598    }
599}
600
601RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
602                                       RsAllocationMipmapControl mips,
603                                       uint32_t usages, uint32_t ptr) {
604    Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips, (void *)ptr);
605    if (!alloc) {
606        return NULL;
607    }
608    alloc->incUserRef();
609    return alloc;
610}
611
612RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
613                                            RsAllocationMipmapControl mips,
614                                            const void *data, size_t data_length, uint32_t usages) {
615    Type *t = static_cast<Type *>(vtype);
616
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    memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
625    if (mips == RS_ALLOCATION_MIPMAP_FULL) {
626        AllocationGenerateScriptMips(rsc, texAlloc);
627    }
628
629    texAlloc->sendDirty(rsc);
630    return texAlloc;
631}
632
633RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
634                                                RsAllocationMipmapControl mips,
635                                                const void *data, size_t data_length, uint32_t usages) {
636    Type *t = static_cast<Type *>(vtype);
637
638    // Cubemap allocation's faces should be Width by Width each.
639    // Source data should have 6 * Width by Width pixels
640    // Error checking is done in the java layer
641    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
642    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
643    if (texAlloc == NULL) {
644        ALOGE("Memory allocation failure");
645        return NULL;
646    }
647
648    uint32_t faceSize = t->getDimX();
649    uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
650    uint32_t copySize = faceSize * t->getElementSizeBytes();
651
652    uint8_t *sourcePtr = (uint8_t*)data;
653    for (uint32_t face = 0; face < 6; face ++) {
654        Adapter2D faceAdapter(rsc, texAlloc);
655        faceAdapter.setFace(face);
656
657        for (uint32_t dI = 0; dI < faceSize; dI ++) {
658            memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
659        }
660
661        // Move the data pointer to the next cube face
662        sourcePtr += copySize;
663    }
664
665    if (mips == RS_ALLOCATION_MIPMAP_FULL) {
666        AllocationGenerateScriptMips(rsc, texAlloc);
667    }
668
669    texAlloc->sendDirty(rsc);
670    return texAlloc;
671}
672
673void rsi_AllocationCopy2DRange(Context *rsc,
674                               RsAllocation dstAlloc,
675                               uint32_t dstXoff, uint32_t dstYoff,
676                               uint32_t dstMip, uint32_t dstFace,
677                               uint32_t width, uint32_t height,
678                               RsAllocation srcAlloc,
679                               uint32_t srcXoff, uint32_t srcYoff,
680                               uint32_t srcMip, uint32_t srcFace) {
681    Allocation *dst = static_cast<Allocation *>(dstAlloc);
682    Allocation *src= static_cast<Allocation *>(srcAlloc);
683    rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
684                                           (RsAllocationCubemapFace)dstFace,
685                                           width, height,
686                                           src, srcXoff, srcYoff,srcMip,
687                                           (RsAllocationCubemapFace)srcFace);
688}
689
690int32_t rsi_AllocationGetSurfaceTextureID(Context *rsc, RsAllocation valloc) {
691    Allocation *alloc = static_cast<Allocation *>(valloc);
692    return alloc->getSurfaceTextureID(rsc);
693}
694
695void rsi_AllocationGetSurfaceTextureID2(Context *rsc, RsAllocation valloc, void *vst, size_t len) {
696    Allocation *alloc = static_cast<Allocation *>(valloc);
697    alloc->setSurfaceTexture(rsc, static_cast<SurfaceTexture *>(vst));
698}
699
700void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) {
701    Allocation *alloc = static_cast<Allocation *>(valloc);
702    alloc->setSurface(rsc, sur);
703}
704
705void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) {
706    Allocation *alloc = static_cast<Allocation *>(valloc);
707    alloc->ioSend(rsc);
708}
709
710void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) {
711    Allocation *alloc = static_cast<Allocation *>(valloc);
712    alloc->ioReceive(rsc);
713}
714
715}
716}
717
718const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
719    Allocation *a = static_cast<Allocation *>(va);
720    a->getType()->incUserRef();
721
722    return a->getType();
723}
724