rsAllocation.cpp revision 09aeb8ac1b0b976c2de40dd00da0c6841e4e882a
1/*
2 * Copyright (C) 2009 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#ifndef ANDROID_RS_BUILD_FOR_HOST
17#include "rsContext.h"
18
19#include <GLES/gl.h>
20#include <GLES2/gl2.h>
21#include <GLES/glext.h>
22#else
23#include "rsContextHostStub.h"
24
25#include <OpenGL/gl.h>
26#include <OpenGl/glext.h>
27#endif
28
29#include "utils/StopWatch.h"
30
31static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va);
32
33using namespace android;
34using namespace android::renderscript;
35
36Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages,
37                       RsAllocationMipmapControl mc)
38    : ObjectBase(rsc) {
39    init(rsc, type);
40
41    mUsageFlags = usages;
42    mMipmapControl = mc;
43
44    allocScriptMemory();
45    if (mType->getElement()->getHasReferences()) {
46        memset(mPtr, 0, mType->getSizeBytes());
47    }
48    if (!mPtr) {
49        LOGE("Allocation::Allocation, alloc failure");
50    }
51}
52
53
54void Allocation::init(Context *rsc, const Type *type) {
55    mPtr = NULL;
56
57    mCpuWrite = false;
58    mCpuRead = false;
59    mGpuWrite = false;
60    mGpuRead = false;
61
62    mReadWriteRatio = 0;
63    mUpdateSize = 0;
64    mUsageFlags = 0;
65    mMipmapControl = RS_ALLOCATION_MIPMAP_NONE;
66
67    mTextureID = 0;
68    mBufferID = 0;
69    mUploadDefered = false;
70
71    mUserBitmapCallback = NULL;
72    mUserBitmapCallbackData = NULL;
73
74    mType.set(type);
75    rsAssert(type);
76
77    mPtr = NULL;
78}
79
80Allocation::~Allocation() {
81    if (mUserBitmapCallback != NULL) {
82        mUserBitmapCallback(mUserBitmapCallbackData);
83        mPtr = NULL;
84    }
85    freeScriptMemory();
86
87    if (mBufferID) {
88        // Causes a SW crash....
89        //LOGV(" mBufferID %i", mBufferID);
90        //glDeleteBuffers(1, &mBufferID);
91        //mBufferID = 0;
92    }
93    if (mTextureID) {
94        glDeleteTextures(1, &mTextureID);
95        mTextureID = 0;
96    }
97}
98
99void Allocation::setCpuWritable(bool) {
100}
101
102void Allocation::setGpuWritable(bool) {
103}
104
105void Allocation::setCpuReadable(bool) {
106}
107
108void Allocation::setGpuReadable(bool) {
109}
110
111bool Allocation::fixAllocation() {
112    return false;
113}
114
115void Allocation::deferedUploadToTexture(const Context *rsc) {
116    mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
117    mUploadDefered = true;
118}
119
120uint32_t Allocation::getGLTarget() const {
121    if (getIsTexture()) {
122        if (mType->getDimFaces()) {
123            return GL_TEXTURE_CUBE_MAP;
124        } else {
125            return GL_TEXTURE_2D;
126        }
127    }
128    if (getIsBufferObject()) {
129        return GL_ARRAY_BUFFER;
130    }
131    return 0;
132}
133
134void Allocation::allocScriptMemory() {
135    rsAssert(!mPtr);
136    mPtr = malloc(mType->getSizeBytes());
137}
138
139void Allocation::freeScriptMemory() {
140    if (mPtr) {
141        free(mPtr);
142        mPtr = NULL;
143    }
144}
145
146
147void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) {
148    rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
149
150    if (getIsTexture()) {
151        uploadToTexture(rsc);
152    }
153    if (getIsBufferObject()) {
154        uploadToBufferObject(rsc);
155    }
156
157    mUploadDefered = false;
158}
159
160void Allocation::uploadToTexture(const Context *rsc) {
161
162    mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE;
163    GLenum type = mType->getElement()->getComponent().getGLType();
164    GLenum format = mType->getElement()->getComponent().getGLFormat();
165
166    if (!type || !format) {
167        return;
168    }
169
170    if (!mPtr) {
171        return;
172    }
173
174    bool isFirstUpload = false;
175
176    if (!mTextureID) {
177        glGenTextures(1, &mTextureID);
178
179        if (!mTextureID) {
180            // This should not happen, however, its likely the cause of the
181            // white sqare bug.
182            // Force a crash to 1: restart the app, 2: make sure we get a bugreport.
183            LOGE("Upload to texture failed to gen mTextureID");
184            rsc->dumpDebug();
185            mUploadDefered = true;
186            return;
187        }
188        isFirstUpload = true;
189    }
190
191    upload2DTexture(isFirstUpload);
192
193    if (!(mUsageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
194        freeScriptMemory();
195    }
196
197    rsc->checkError("Allocation::uploadToTexture");
198}
199
200const static GLenum gFaceOrder[] = {
201    GL_TEXTURE_CUBE_MAP_POSITIVE_X,
202    GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
203    GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
204    GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
205    GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
206    GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
207};
208
209void Allocation::update2DTexture(const void *ptr, uint32_t xoff, uint32_t yoff,
210                                 uint32_t lod, RsAllocationCubemapFace face,
211                                 uint32_t w, uint32_t h) {
212    GLenum type = mType->getElement()->getComponent().getGLType();
213    GLenum format = mType->getElement()->getComponent().getGLFormat();
214    GLenum target = (GLenum)getGLTarget();
215    rsAssert(mTextureID);
216    glBindTexture(target, mTextureID);
217    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
218    GLenum t = GL_TEXTURE_2D;
219    if (mType->getDimFaces()) {
220        t = gFaceOrder[face];
221    }
222    glTexSubImage2D(t, lod, xoff, yoff, w, h, format, type, ptr);
223}
224
225void Allocation::upload2DTexture(bool isFirstUpload) {
226    GLenum type = mType->getElement()->getComponent().getGLType();
227    GLenum format = mType->getElement()->getComponent().getGLFormat();
228
229    GLenum target = (GLenum)getGLTarget();
230    glBindTexture(target, mTextureID);
231    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
232
233    uint32_t faceCount = 1;
234    if (mType->getDimFaces()) {
235        faceCount = 6;
236    }
237
238    for (uint32_t face = 0; face < faceCount; face ++) {
239        for (uint32_t lod = 0; lod < mType->getLODCount(); lod++) {
240            const uint8_t *p = (const uint8_t *)mPtr;
241            p += mType->getLODFaceOffset(lod, (RsAllocationCubemapFace)face, 0, 0);
242
243            GLenum t = GL_TEXTURE_2D;
244            if (mType->getDimFaces()) {
245                t = gFaceOrder[face];
246            }
247
248            if (isFirstUpload) {
249                glTexImage2D(t, lod, format,
250                             mType->getLODDimX(lod), mType->getLODDimY(lod),
251                             0, format, type, p);
252            } else {
253                glTexSubImage2D(t, lod, 0, 0,
254                                mType->getLODDimX(lod), mType->getLODDimY(lod),
255                                format, type, p);
256            }
257        }
258    }
259
260    if (mMipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
261#ifndef ANDROID_RS_BUILD_FOR_HOST
262        glGenerateMipmap(target);
263#endif //ANDROID_RS_BUILD_FOR_HOST
264    }
265}
266
267void Allocation::deferedUploadToBufferObject(const Context *rsc) {
268    mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
269    mUploadDefered = true;
270}
271
272void Allocation::uploadToBufferObject(const Context *rsc) {
273    rsAssert(!mType->getDimY());
274    rsAssert(!mType->getDimZ());
275
276    mUsageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
277
278    if (!mBufferID) {
279        glGenBuffers(1, &mBufferID);
280    }
281    if (!mBufferID) {
282        LOGE("Upload to buffer object failed");
283        mUploadDefered = true;
284        return;
285    }
286    GLenum target = (GLenum)getGLTarget();
287    glBindBuffer(target, mBufferID);
288    glBufferData(target, mType->getSizeBytes(), getPtr(), GL_DYNAMIC_DRAW);
289    glBindBuffer(target, 0);
290    rsc->checkError("Allocation::uploadToBufferObject");
291}
292
293void Allocation::uploadCheck(Context *rsc) {
294    if (mUploadDefered) {
295        syncAll(rsc, RS_ALLOCATION_USAGE_SCRIPT);
296    }
297}
298
299void Allocation::read(void *data) {
300    memcpy(data, mPtr, mType->getSizeBytes());
301}
302
303void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod,
304                         uint32_t count, const void *data, uint32_t sizeBytes) {
305    uint32_t eSize = mType->getElementSizeBytes();
306    uint8_t * ptr = static_cast<uint8_t *>(mPtr);
307    ptr += eSize * xoff;
308    uint32_t size = count * eSize;
309
310    if (size != sizeBytes) {
311        LOGE("Allocation::subData called with mismatched size expected %i, got %i", size, sizeBytes);
312        mType->dumpLOGV("type info");
313        return;
314    }
315
316    if (mType->getElement()->getHasReferences()) {
317        incRefs(data, count);
318        decRefs(ptr, count);
319    }
320
321    memcpy(ptr, data, size);
322    sendDirty();
323    mUploadDefered = true;
324}
325
326void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
327             uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
328    uint32_t eSize = mType->getElementSizeBytes();
329    uint32_t lineSize = eSize * w;
330    uint32_t destW = mType->getDimX();
331
332    //LOGE("data2d %p,  %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes);
333
334    if ((lineSize * h) != sizeBytes) {
335        LOGE("Allocation size mismatch, expected %i, got %i", (lineSize * h), sizeBytes);
336        rsAssert(!"Allocation::subData called with mismatched size");
337        return;
338    }
339
340    if (mPtr) {
341        const uint8_t *src = static_cast<const uint8_t *>(data);
342        uint8_t *dst = static_cast<uint8_t *>(mPtr);
343        dst += mType->getLODFaceOffset(lod, face, xoff, yoff);
344
345        //LOGE("            %p  %p  %i  ", dst, src, eSize);
346        for (uint32_t line=yoff; line < (yoff+h); line++) {
347            if (mType->getElement()->getHasReferences()) {
348                incRefs(src, w);
349                decRefs(dst, w);
350            }
351            memcpy(dst, src, lineSize);
352            src += lineSize;
353            dst += destW * eSize;
354        }
355        sendDirty();
356        mUploadDefered = true;
357    } else {
358        update2DTexture(data, xoff, yoff, lod, face, w, h);
359    }
360}
361
362void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff,
363                      uint32_t lod, RsAllocationCubemapFace face,
364                      uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
365}
366
367void Allocation::elementData(Context *rsc, uint32_t x, const void *data,
368                                uint32_t cIdx, uint32_t sizeBytes) {
369    uint32_t eSize = mType->getElementSizeBytes();
370    uint8_t * ptr = static_cast<uint8_t *>(mPtr);
371    ptr += eSize * x;
372
373    if (cIdx >= mType->getElement()->getFieldCount()) {
374        LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
375        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
376        return;
377    }
378
379    if (x >= mType->getDimX()) {
380        LOGE("Error Allocation::subElementData X offset %i out of range.", x);
381        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
382        return;
383    }
384
385    const Element * e = mType->getElement()->getField(cIdx);
386    ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
387
388    if (sizeBytes != e->getSizeBytes()) {
389        LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
390        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
391        return;
392    }
393
394    if (e->getHasReferences()) {
395        e->incRefs(data);
396        e->decRefs(ptr);
397    }
398
399    memcpy(ptr, data, sizeBytes);
400    sendDirty();
401    mUploadDefered = true;
402}
403
404void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y,
405                                const void *data, uint32_t cIdx, uint32_t sizeBytes) {
406    uint32_t eSize = mType->getElementSizeBytes();
407    uint8_t * ptr = static_cast<uint8_t *>(mPtr);
408    ptr += eSize * (x + y * mType->getDimX());
409
410    if (x >= mType->getDimX()) {
411        LOGE("Error Allocation::subElementData X offset %i out of range.", x);
412        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
413        return;
414    }
415
416    if (y >= mType->getDimY()) {
417        LOGE("Error Allocation::subElementData X offset %i out of range.", x);
418        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range.");
419        return;
420    }
421
422    if (cIdx >= mType->getElement()->getFieldCount()) {
423        LOGE("Error Allocation::subElementData component %i out of range.", cIdx);
424        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range.");
425        return;
426    }
427
428    const Element * e = mType->getElement()->getField(cIdx);
429    ptr += mType->getElement()->getFieldOffsetBytes(cIdx);
430
431    if (sizeBytes != e->getSizeBytes()) {
432        LOGE("Error Allocation::subElementData data size %i does not match field size %i.", sizeBytes, e->getSizeBytes());
433        rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size.");
434        return;
435    }
436
437    if (e->getHasReferences()) {
438        e->incRefs(data);
439        e->decRefs(ptr);
440    }
441
442    memcpy(ptr, data, sizeBytes);
443    sendDirty();
444    mUploadDefered = true;
445}
446
447void Allocation::addProgramToDirty(const Program *p) {
448    mToDirtyList.push(p);
449}
450
451void Allocation::removeProgramToDirty(const Program *p) {
452    for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
453        if (mToDirtyList[ct] == p) {
454            mToDirtyList.removeAt(ct);
455            return;
456        }
457    }
458    rsAssert(0);
459}
460
461void Allocation::dumpLOGV(const char *prefix) const {
462    ObjectBase::dumpLOGV(prefix);
463
464    String8 s(prefix);
465    s.append(" type ");
466    if (mType.get()) {
467        mType->dumpLOGV(s.string());
468    }
469
470    LOGV("%s allocation ptr=%p mCpuWrite=%i, mCpuRead=%i, mGpuWrite=%i, mGpuRead=%i",
471          prefix, mPtr, mCpuWrite, mCpuRead, mGpuWrite, mGpuRead);
472
473    LOGV("%s allocation mUsageFlags=0x04%x, mMipmapControl=0x%04x, mTextureID=%i, mBufferID=%i",
474          prefix, mUsageFlags, mMipmapControl, mTextureID, mBufferID);
475}
476
477void Allocation::serialize(OStream *stream) const {
478    // Need to identify ourselves
479    stream->addU32((uint32_t)getClassId());
480
481    String8 name(getName());
482    stream->addString(&name);
483
484    // First thing we need to serialize is the type object since it will be needed
485    // to initialize the class
486    mType->serialize(stream);
487
488    uint32_t dataSize = mType->getSizeBytes();
489    // Write how much data we are storing
490    stream->addU32(dataSize);
491    // Now write the data
492    stream->addByteArray(mPtr, dataSize);
493}
494
495Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) {
496    // First make sure we are reading the correct object
497    RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
498    if (classID != RS_A3D_CLASS_ID_ALLOCATION) {
499        LOGE("allocation loading skipped due to invalid class id\n");
500        return NULL;
501    }
502
503    String8 name;
504    stream->loadString(&name);
505
506    Type *type = Type::createFromStream(rsc, stream);
507    if (!type) {
508        return NULL;
509    }
510    type->compute();
511
512    // Number of bytes we wrote out for this allocation
513    uint32_t dataSize = stream->loadU32();
514    if (dataSize != type->getSizeBytes()) {
515        LOGE("failed to read allocation because numbytes written is not the same loaded type wants\n");
516        ObjectBase::checkDelete(type);
517        return NULL;
518    }
519
520    Allocation *alloc = new Allocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT);
521    alloc->setName(name.string(), name.size());
522
523    uint32_t count = dataSize / type->getElementSizeBytes();
524
525    // Read in all of our allocation data
526    alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize);
527    stream->reset(stream->getPos() + dataSize);
528
529    return alloc;
530}
531
532void Allocation::sendDirty() const {
533    for (size_t ct=0; ct < mToDirtyList.size(); ct++) {
534        mToDirtyList[ct]->forceDirty();
535    }
536}
537
538void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const {
539    const uint8_t *p = static_cast<const uint8_t *>(ptr);
540    const Element *e = mType->getElement();
541    uint32_t stride = e->getSizeBytes();
542
543    p += stride * startOff;
544    while (ct > 0) {
545        e->incRefs(p);
546        ct --;
547        p += stride;
548    }
549}
550
551void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const {
552    const uint8_t *p = static_cast<const uint8_t *>(ptr);
553    const Element *e = mType->getElement();
554    uint32_t stride = e->getSizeBytes();
555
556    p += stride * startOff;
557    while (ct > 0) {
558        e->decRefs(p);
559        ct --;
560        p += stride;
561    }
562}
563
564void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) {
565}
566
567void Allocation::resize1D(Context *rsc, uint32_t dimX) {
568    Type *t = mType->cloneAndResize1D(rsc, dimX);
569
570    uint32_t oldDimX = mType->getDimX();
571    if (dimX == oldDimX) {
572        return;
573    }
574
575    if (dimX < oldDimX) {
576        decRefs(mPtr, oldDimX - dimX, dimX);
577    }
578    mPtr = realloc(mPtr, t->getSizeBytes());
579
580    if (dimX > oldDimX) {
581        const Element *e = mType->getElement();
582        uint32_t stride = e->getSizeBytes();
583        memset(((uint8_t *)mPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
584    }
585    mType.set(t);
586}
587
588void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) {
589    LOGE("not implemented");
590}
591
592/////////////////
593//
594
595
596namespace android {
597namespace renderscript {
598
599void rsi_AllocationUploadToTexture(Context *rsc, RsAllocation va, bool genmip, uint32_t baseMipLevel) {
600    Allocation *alloc = static_cast<Allocation *>(va);
601    alloc->deferedUploadToTexture(rsc);
602}
603
604void rsi_AllocationUploadToBufferObject(Context *rsc, RsAllocation va) {
605    Allocation *alloc = static_cast<Allocation *>(va);
606    alloc->deferedUploadToBufferObject(rsc);
607}
608
609static void mip565(const Adapter2D &out, const Adapter2D &in) {
610    uint32_t w = out.getDimX();
611    uint32_t h = out.getDimY();
612
613    for (uint32_t y=0; y < h; y++) {
614        uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y));
615        const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2));
616        const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1));
617
618        for (uint32_t x=0; x < w; x++) {
619            *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
620            oPtr ++;
621            i1 += 2;
622            i2 += 2;
623        }
624    }
625}
626
627static void mip8888(const Adapter2D &out, const Adapter2D &in) {
628    uint32_t w = out.getDimX();
629    uint32_t h = out.getDimY();
630
631    for (uint32_t y=0; y < h; y++) {
632        uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y));
633        const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2));
634        const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1));
635
636        for (uint32_t x=0; x < w; x++) {
637            *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
638            oPtr ++;
639            i1 += 2;
640            i2 += 2;
641        }
642    }
643}
644
645static void mip8(const Adapter2D &out, const Adapter2D &in) {
646    uint32_t w = out.getDimX();
647    uint32_t h = out.getDimY();
648
649    for (uint32_t y=0; y < h; y++) {
650        uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y));
651        const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2));
652        const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1));
653
654        for (uint32_t x=0; x < w; x++) {
655            *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
656            oPtr ++;
657            i1 += 2;
658            i2 += 2;
659        }
660    }
661}
662
663static void mip(const Adapter2D &out, const Adapter2D &in) {
664    switch (out.getBaseType()->getElement()->getSizeBits()) {
665    case 32:
666        mip8888(out, in);
667        break;
668    case 16:
669        mip565(out, in);
670        break;
671    case 8:
672        mip8(out, in);
673        break;
674    }
675}
676
677#ifndef ANDROID_RS_BUILD_FOR_HOST
678
679void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) {
680    Allocation *a = static_cast<Allocation *>(va);
681    a->syncAll(rsc, src);
682    a->sendDirty();
683}
684
685void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) {
686    Allocation *texAlloc = static_cast<Allocation *>(va);
687    rsaAllocationGenerateScriptMips(rsc, texAlloc);
688}
689
690void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
691    Allocation *texAlloc = static_cast<Allocation *>(va);
692    const Type * t = texAlloc->getType();
693
694    size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
695    if (s != dataLen) {
696        rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
697        return;
698    }
699
700    memcpy(data, texAlloc->getPtr(), s);
701}
702
703void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod,
704                          uint32_t count, const void *data, uint32_t sizeBytes) {
705    Allocation *a = static_cast<Allocation *>(va);
706    a->data(rsc, xoff, lod, count, data, sizeBytes);
707}
708
709void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face,
710                                 const void *data, uint32_t eoff, uint32_t sizeBytes) {
711    Allocation *a = static_cast<Allocation *>(va);
712    a->elementData(rsc, x, y, data, eoff, sizeBytes);
713}
714
715void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod,
716                                 const void *data, uint32_t eoff, uint32_t sizeBytes) {
717    Allocation *a = static_cast<Allocation *>(va);
718    a->elementData(rsc, x, data, eoff, sizeBytes);
719}
720
721void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
722                          uint32_t w, uint32_t h, const void *data, uint32_t sizeBytes) {
723    Allocation *a = static_cast<Allocation *>(va);
724    a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes);
725}
726
727void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data) {
728    Allocation *a = static_cast<Allocation *>(va);
729    a->read(data);
730}
731
732void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) {
733    Allocation *a = static_cast<Allocation *>(va);
734    a->resize1D(rsc, dimX);
735}
736
737void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) {
738    Allocation *a = static_cast<Allocation *>(va);
739    a->resize2D(rsc, dimX, dimY);
740}
741
742#endif //ANDROID_RS_BUILD_FOR_HOST
743
744}
745}
746
747static void rsaAllocationGenerateScriptMips(RsContext con, RsAllocation va) {
748    Context *rsc = static_cast<Context *>(con);
749    Allocation *texAlloc = static_cast<Allocation *>(va);
750    uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1;
751    for (uint32_t face = 0; face < numFaces; face ++) {
752        Adapter2D adapt(rsc, texAlloc);
753        Adapter2D adapt2(rsc, texAlloc);
754        adapt.setFace(face);
755        adapt2.setFace(face);
756        for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
757            adapt.setLOD(lod);
758            adapt2.setLOD(lod + 1);
759            mip(adapt2, adapt);
760        }
761    }
762}
763
764const void * rsaAllocationGetType(RsContext con, RsAllocation va) {
765    Allocation *a = static_cast<Allocation *>(va);
766    a->getType()->incUserRef();
767
768    return a->getType();
769}
770
771RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype,
772                                      RsAllocationMipmapControl mips,
773                                      uint32_t usages) {
774    Context *rsc = static_cast<Context *>(con);
775    Allocation * alloc = new Allocation(rsc, static_cast<Type *>(vtype), usages, mips);
776    alloc->incUserRef();
777    return alloc;
778}
779
780
781RsAllocation rsaAllocationCreateFromBitmap(RsContext con, RsType vtype,
782                                           RsAllocationMipmapControl mips,
783                                           const void *data, uint32_t usages) {
784    Context *rsc = static_cast<Context *>(con);
785    Type *t = static_cast<Type *>(vtype);
786
787    RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, vtype, mips, usages);
788    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
789    if (texAlloc == NULL) {
790        LOGE("Memory allocation failure");
791        return NULL;
792    }
793
794    memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes());
795    if (mips == RS_ALLOCATION_MIPMAP_FULL) {
796        rsaAllocationGenerateScriptMips(rsc, texAlloc);
797    }
798
799    texAlloc->deferedUploadToTexture(rsc);
800    return texAlloc;
801}
802
803RsAllocation rsaAllocationCubeCreateFromBitmap(RsContext con, RsType vtype,
804                                               RsAllocationMipmapControl mips,
805                                               const void *data, uint32_t usages) {
806    Context *rsc = static_cast<Context *>(con);
807    Type *t = static_cast<Type *>(vtype);
808
809    // Cubemap allocation's faces should be Width by Width each.
810    // Source data should have 6 * Width by Width pixels
811    // Error checking is done in the java layer
812    RsAllocation vTexAlloc = rsaAllocationCreateTyped(rsc, t, mips, usages);
813    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
814    if (texAlloc == NULL) {
815        LOGE("Memory allocation failure");
816        return NULL;
817    }
818
819    uint32_t faceSize = t->getDimX();
820    uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes();
821    uint32_t copySize = faceSize * t->getElementSizeBytes();
822
823    uint8_t *sourcePtr = (uint8_t*)data;
824    for (uint32_t face = 0; face < 6; face ++) {
825        Adapter2D faceAdapter(rsc, texAlloc);
826        faceAdapter.setFace(face);
827
828        for (uint32_t dI = 0; dI < faceSize; dI ++) {
829            memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize);
830        }
831
832        // Move the data pointer to the next cube face
833        sourcePtr += copySize;
834    }
835
836    if (mips == RS_ALLOCATION_MIPMAP_FULL) {
837        rsaAllocationGenerateScriptMips(rsc, texAlloc);
838    }
839
840    texAlloc->deferedUploadToTexture(rsc);
841    return texAlloc;
842}
843