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