rsAllocation.cpp revision 6e127d6c76f02024ef906c9a450a9b2d80657566
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, mHal.drvState.mallocPtrLOD0, 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 *)rsc->mHal.funcs.allocation.lock1D(rsc, this);
291
292    writePackedData(rsc, getType(), dst, src, true);
293    rsc->mHal.funcs.allocation.unlock1D(rsc, this);
294}
295
296void Allocation::packVec3Allocation(Context *rsc, OStream *stream) const {
297    uint32_t paddedBytes = getType()->getElement()->getSizeBytes();
298    uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded();
299    uint32_t numItems = mHal.state.type->getSizeBytes() / paddedBytes;
300
301    const uint8_t *src = (const uint8_t*)rsc->mHal.funcs.allocation.lock1D(rsc, this);
302    uint8_t *dst = new uint8_t[numItems * unpaddedBytes];
303
304    writePackedData(rsc, getType(), dst, src, false);
305    stream->addByteArray(dst, getPackedSize());
306
307    delete[] dst;
308    rsc->mHal.funcs.allocation.unlock1D(rsc, this);
309}
310
311void Allocation::serialize(Context *rsc, OStream *stream) const {
312    // Need to identify ourselves
313    stream->addU32((uint32_t)getClassId());
314
315    String8 name(getName());
316    stream->addString(&name);
317
318    // First thing we need to serialize is the type object since it will be needed
319    // to initialize the class
320    mHal.state.type->serialize(rsc, stream);
321
322    uint32_t dataSize = mHal.state.type->getSizeBytes();
323    // 3 element vectors are padded to 4 in memory, but padding isn't serialized
324    uint32_t packedSize = getPackedSize();
325    // Write how much data we are storing
326    stream->addU32(packedSize);
327    if (dataSize == packedSize) {
328        // Now write the data
329        stream->addByteArray(rsc->mHal.funcs.allocation.lock1D(rsc, this), dataSize);
330        rsc->mHal.funcs.allocation.unlock1D(rsc, this);
331    } else {
332        // Now write the data
333        packVec3Allocation(rsc, stream);
334    }
335}
336
337Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
338    // First make sure we are reading the correct object
339    RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
340    if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
341        ALOGE("allocation loading skipped due to invalid class id\n");
342        return NULL;
343    }
344
345    String8 name;
346    stream->loadString(&name);
347
348    Type *type = Type::createFromStream(rsc, stream);
349    if (!type) {
350        return NULL;
351    }
352    type->compute();
353
354    Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
355    type->decUserRef();
356
357    // Number of bytes we wrote out for this allocation
358    uint32_t dataSize = stream->loadU32();
359    // 3 element vectors are padded to 4 in memory, but padding isn't serialized
360    uint32_t packedSize = alloc->getPackedSize();
361    if (dataSize != type->getSizeBytes() &&
362        dataSize != packedSize) {
363        ALOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
364        ObjectBase::checkDelete(alloc);
365        ObjectBase::checkDelete(type);
366        return NULL;
367    }
368
369    alloc->setName(name.string(), name.size());
370
371    if (dataSize == type->getSizeBytes()) {
372        uint32_t count = dataSize / type->getElementSizeBytes();
373        // Read in all of our allocation data
374        alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
375    } else {
376        alloc->unpackVec3Allocation(rsc, stream->getPtr() + stream->getPos(), dataSize);
377    }
378    stream->reset(stream->getPos() + dataSize);
379
380    return alloc;
381}
382
383void Allocation::sendDirty(const Context *rsc) const {
384    for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
385        mToDirtyList[ct]->forceDirty();
386    }
387    mRSC->mHal.funcs.allocation.markDirty(rsc, this);
388}
389
390void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
391    mHal.state.type->incRefs(ptr, ct, startOff);
392}
393
394void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
395    if (!mHal.state.hasReferences || !getIsScript()) {
396        return;
397    }
398    mHal.state.type->decRefs(ptr, ct, startOff);
399}
400
401void Allocation::freeChildrenUnlocked () {
402    void *ptr = mRSC->mHal.funcs.allocation.lock1D(mRSC, this);
403    decRefs(ptr, mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0);
404    mRSC->mHal.funcs.allocation.unlock1D(mRSC, this);
405}
406
407bool Allocation::freeChildren() {
408    if (mHal.state.hasReferences) {
409        incSysRef();
410        freeChildrenUnlocked();
411        return decSysRef();
412    }
413    return false;
414}
415
416void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
417}
418
419void Allocation::resize1D(Context *rsc, uint32_t dimX) {
420    uint32_t oldDimX = mHal.state.dimensionX;
421    if (dimX == oldDimX) {
422        return;
423    }
424
425    ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
426    if (dimX < oldDimX) {
427        decRefs(rsc->mHal.funcs.allocation.lock1D(rsc, this), oldDimX - dimX, dimX);
428        rsc->mHal.funcs.allocation.unlock1D(rsc, this);
429    }
430    rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
431    setType(t.get());
432    updateCache();
433}
434
435void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
436    ALOGE("not implemented");
437}
438
439int32_t Allocation::getSurfaceTextureID(const Context *rsc) {
440    int32_t id = rsc->mHal.funcs.allocation.initSurfaceTexture(rsc, this);
441    mHal.state.surfaceTextureID = id;
442    return id;
443}
444
445void Allocation::setSurfaceTexture(const Context *rsc, SurfaceTexture *st) {
446    if(st != mHal.state.surfaceTexture) {
447        if(mHal.state.surfaceTexture != NULL) {
448            mHal.state.surfaceTexture->decStrong(NULL);
449        }
450        mHal.state.surfaceTexture = st;
451        if(mHal.state.surfaceTexture != NULL) {
452            mHal.state.surfaceTexture->incStrong(NULL);
453        }
454    }
455}
456
457void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) {
458    ANativeWindow *nw = (ANativeWindow *)sur;
459    ANativeWindow *old = mHal.state.wndSurface;
460    if (nw) {
461        nw->incStrong(NULL);
462    }
463    rsc->mHal.funcs.allocation.setSurfaceTexture(rsc, this, nw);
464    mHal.state.wndSurface = nw;
465    if (old) {
466        old->decStrong(NULL);
467    }
468}
469
470void Allocation::ioSend(const Context *rsc) {
471    rsc->mHal.funcs.allocation.ioSend(rsc, this);
472}
473
474void Allocation::ioReceive(const Context *rsc) {
475    rsc->mHal.funcs.allocation.ioReceive(rsc, this);
476}
477
478
479/////////////////
480//
481
482namespace android {
483namespace renderscript {
484
485void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
486    Allocation *a = static_cast<Allocation *>(va);
487    a->sendDirty(rsc);
488    a->syncAll(rsc, src);
489}
490
491void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
492    Allocation *alloc = static_cast<Allocation *>(va);
493    rsc->mHal.funcs.allocation.generateMipmaps(rsc, alloc);
494}
495
496void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
497    Allocation *a = static_cast<Allocation *>(va);
498    const Type * t = a->getType();
499    a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
500            t->getDimX(), t->getDimY(), data, sizeBytes);
501}
502
503void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
504                          uint32_t count, const void *data, size_t sizeBytes) {
505    Allocation *a = static_cast<Allocation *>(va);
506    a->data(rsc, xoff, lod, count, data, sizeBytes);
507}
508
509void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
510                                 const void *data, size_t sizeBytes, size_t eoff) {
511    Allocation *a = static_cast<Allocation *>(va);
512    a->elementData(rsc, x, y, data, eoff, sizeBytes);
513}
514
515void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
516                                 const void *data, size_t sizeBytes, size_t eoff) {
517    Allocation *a = static_cast<Allocation *>(va);
518    a->elementData(rsc, x, data, eoff, sizeBytes);
519}
520
521void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
522                          uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
523    Allocation *a = static_cast<Allocation *>(va);
524    a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
525}
526
527void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t sizeBytes) {
528    Allocation *a = static_cast<Allocation *>(va);
529    const Type * t = a->getType();
530    if(t->getDimY()) {
531        a->read(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
532                t->getDimX(), t->getDimY(), data, sizeBytes);
533    } else {
534        a->read(rsc, 0, 0, t->getDimX(), data, sizeBytes);
535    }
536
537}
538
539void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
540    Allocation *a = static_cast<Allocation *>(va);
541    a->resize1D(rsc, dimX);
542}
543
544void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
545    Allocation *a = static_cast<Allocation *>(va);
546    a->resize2D(rsc, dimX, dimY);
547}
548
549RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype,
550                                       RsAllocationMipmapControl mips,
551                                       uint32_t usages, uint32_t ptr) {
552    Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips, (void *)ptr);
553    if (!alloc) {
554        return NULL;
555    }
556    alloc->incUserRef();
557    return alloc;
558}
559
560RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype,
561                                            RsAllocationMipmapControl mips,
562                                            const void *data, size_t sizeBytes, uint32_t usages) {
563    Type *t = static_cast<Type *>(vtype);
564
565    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
566    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
567    if (texAlloc == NULL) {
568        ALOGE("Memory allocation failure");
569        return NULL;
570    }
571
572    texAlloc->data(rsc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
573                   t->getDimX(), t->getDimY(), data, sizeBytes);
574    if (mips == RS_ALLOCATION_MIPMAP_FULL) {
575        rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
576    }
577
578    texAlloc->sendDirty(rsc);
579    return texAlloc;
580}
581
582RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype,
583                                                RsAllocationMipmapControl mips,
584                                                const void *data, size_t sizeBytes, uint32_t usages) {
585    Type *t = static_cast<Type *>(vtype);
586
587    // Cubemap allocation's faces should be Width by Width each.
588    // Source data should have 6 * Width by Width pixels
589    // Error checking is done in the java layer
590    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0);
591    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
592    if (texAlloc == NULL) {
593        ALOGE("Memory allocation failure");
594        return NULL;
595    }
596
597    uint32_t faceSize = t->getDimX();
598    uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
599    uint32_t copySize = faceSize * t->getElementSizeBytes();
600
601    uint8_t *sourcePtr = (uint8_t*)data;
602    for (uint32_t face = 0; face < 6; face ++) {
603        for (uint32_t dI = 0; dI < faceSize; dI ++) {
604            texAlloc->data(rsc, 0, dI, 0, (RsAllocationCubemapFace)face,
605                           t->getDimX(), 1, sourcePtr + strideBytes * dI, copySize);
606        }
607
608        // Move the data pointer to the next cube face
609        sourcePtr += copySize;
610    }
611
612    if (mips == RS_ALLOCATION_MIPMAP_FULL) {
613        rsc->mHal.funcs.allocation.generateMipmaps(rsc, texAlloc);
614    }
615
616    texAlloc->sendDirty(rsc);
617    return texAlloc;
618}
619
620void rsi_AllocationCopy2DRange(Context *rsc,
621                               RsAllocation dstAlloc,
622                               uint32_t dstXoff, uint32_t dstYoff,
623                               uint32_t dstMip, uint32_t dstFace,
624                               uint32_t width, uint32_t height,
625                               RsAllocation srcAlloc,
626                               uint32_t srcXoff, uint32_t srcYoff,
627                               uint32_t srcMip, uint32_t srcFace) {
628    Allocation *dst = static_cast<Allocation *>(dstAlloc);
629    Allocation *src= static_cast<Allocation *>(srcAlloc);
630    rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip,
631                                           (RsAllocationCubemapFace)dstFace,
632                                           width, height,
633                                           src, srcXoff, srcYoff,srcMip,
634                                           (RsAllocationCubemapFace)srcFace);
635}
636
637int32_t rsi_AllocationGetSurfaceTextureID(Context *rsc, RsAllocation valloc) {
638    Allocation *alloc = static_cast<Allocation *>(valloc);
639    return alloc->getSurfaceTextureID(rsc);
640}
641
642void rsi_AllocationGetSurfaceTextureID2(Context *rsc, RsAllocation valloc, void *vst, size_t len) {
643    Allocation *alloc = static_cast<Allocation *>(valloc);
644    alloc->setSurfaceTexture(rsc, static_cast<SurfaceTexture *>(vst));
645}
646
647void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) {
648    Allocation *alloc = static_cast<Allocation *>(valloc);
649    alloc->setSurface(rsc, sur);
650}
651
652void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) {
653    Allocation *alloc = static_cast<Allocation *>(valloc);
654    alloc->ioSend(rsc);
655}
656
657void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) {
658    Allocation *alloc = static_cast<Allocation *>(valloc);
659    alloc->ioReceive(rsc);
660}
661
662}
663}
664
665const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
666    Allocation *a = static_cast<Allocation *>(va);
667    a->getType()->incUserRef();
668
669    return a->getType();
670}
671