rsdAllocation.cpp revision a43823a68a27c8ee059610905ea6a11a6599239b
14b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes/*
24b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * Copyright (C) 2013 The Android Open Source Project
34b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *
44b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
54b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * you may not use this file except in compliance with the License.
64b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * You may obtain a copy of the License at
74b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *
84b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
94b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *
104b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * Unless required by applicable law or agreed to in writing, software
114b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
124b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * See the License for the specific language governing permissions and
144b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * limitations under the License.
154b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes */
164b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "rsdCore.h"
184b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "rsdAllocation.h"
194b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
204b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "rsAllocation.h"
214b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
234b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "system/window.h"
244b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "ui/Rect.h"
254b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "ui/GraphicBufferMapper.h"
264b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#endif
274b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
284b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#ifdef RS_COMPATIBILITY_LIB
294b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "rsCompatibilityLib.h"
304b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#else
314b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "rsdFrameBufferObj.h"
324b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "gui/GLConsumer.h"
334b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "gui/CpuConsumer.h"
344b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "gui/Surface.h"
354b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "hardware/gralloc.h"
364b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
374b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include <GLES/gl.h>
388f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#include <GLES2/gl2.h>
394b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include <GLES/glext.h>
404b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#endif
414b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
424b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#ifdef RS_SERVER
4340eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes// server requires malloc.h for memalign
444b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include <malloc.h>
454b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#endif
464b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
474b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesusing namespace android;
484b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesusing namespace android::renderscript;
494b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
504b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#ifndef RS_COMPATIBILITY_LIB
514b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesconst static GLenum gFaceOrder[] = {
524b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    GL_TEXTURE_CUBE_MAP_POSITIVE_X,
534b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
544b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
554b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
564b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
574b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
584b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes};
5940eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes
604b4a8824289c48c823cd38bc63289d121aae3d67Elliott HughesGLenum rsdTypeToGLType(RsDataType t) {
614b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    switch (t) {
6240eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    case RS_TYPE_UNSIGNED_5_6_5:    return GL_UNSIGNED_SHORT_5_6_5;
634b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    case RS_TYPE_UNSIGNED_5_5_5_1:  return GL_UNSIGNED_SHORT_5_5_5_1;
644b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    case RS_TYPE_UNSIGNED_4_4_4_4:  return GL_UNSIGNED_SHORT_4_4_4_4;
654b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
6640eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    //case RS_TYPE_FLOAT_16:      return GL_HALF_FLOAT;
6740eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    case RS_TYPE_FLOAT_32:      return GL_FLOAT;
684b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    case RS_TYPE_UNSIGNED_8:    return GL_UNSIGNED_BYTE;
6940eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    case RS_TYPE_UNSIGNED_16:   return GL_UNSIGNED_SHORT;
704b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    case RS_TYPE_SIGNED_8:      return GL_BYTE;
7140eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    case RS_TYPE_SIGNED_16:     return GL_SHORT;
724b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    default:    break;
734b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
744b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return 0;
7540eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes}
764b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
774b4a8824289c48c823cd38bc63289d121aae3d67Elliott HughesGLenum rsdKindToGLFormat(RsDataKind k) {
784b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    switch (k) {
794b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    case RS_KIND_PIXEL_L: return GL_LUMINANCE;
804b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    case RS_KIND_PIXEL_A: return GL_ALPHA;
814b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    case RS_KIND_PIXEL_LA: return GL_LUMINANCE_ALPHA;
824b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    case RS_KIND_PIXEL_RGB: return GL_RGB;
834b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    case RS_KIND_PIXEL_RGBA: return GL_RGBA;
844b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    case RS_KIND_PIXEL_DEPTH: return GL_DEPTH_COMPONENT16;
8540eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    default: break;
8640eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    }
874b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return 0;
884b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
894b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#endif
904b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
914b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesuint8_t *GetOffsetPtr(const android::renderscript::Allocation *alloc,
9240eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes                      uint32_t xoff, uint32_t yoff, uint32_t zoff,
9340eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes                      uint32_t lod, RsAllocationCubemapFace face) {
944b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    uint8_t *ptr = (uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
954b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    ptr += face * alloc->mHal.drvState.faceOffset;
964b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    ptr += zoff * alloc->mHal.drvState.lod[lod].dimY * alloc->mHal.drvState.lod[lod].stride;
9740eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    ptr += yoff * alloc->mHal.drvState.lod[lod].stride;
984b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    ptr += xoff * alloc->mHal.state.elementSizeBytes;
994b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return ptr;
1004b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
1014b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1024b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1034b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesstatic void Update2DTexture(const Context *rsc, const Allocation *alloc, const void *ptr,
10440eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes                            uint32_t xoff, uint32_t yoff, uint32_t lod,
1054b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                            RsAllocationCubemapFace face, uint32_t w, uint32_t h) {
1064b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#ifndef RS_COMPATIBILITY_LIB
1074b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
1084b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1094b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    rsAssert(drv->textureID);
1104b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
1114b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
1124b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    GLenum t = GL_TEXTURE_2D;
1134b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    if (alloc->mHal.state.hasFaces) {
1144b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        t = gFaceOrder[face];
1154b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
1164b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    RSD_CALL_GL(glTexSubImage2D, t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr);
1174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#endif
1184b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
1194b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1204b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1214b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#ifndef RS_COMPATIBILITY_LIB
1224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesstatic void Upload2DTexture(const Context *rsc, const Allocation *alloc, bool isFirstUpload) {
1234b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
1244b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1254b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
1264b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
1274b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1284b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    uint32_t faceCount = 1;
1294b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    if (alloc->mHal.state.hasFaces) {
1304b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        faceCount = 6;
1314b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
1324b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1334b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    rsdGLCheckError(rsc, "Upload2DTexture 1 ");
1344b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    for (uint32_t face = 0; face < faceCount; face ++) {
1354b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        for (uint32_t lod = 0; lod < alloc->mHal.state.type->getLODCount(); lod++) {
1364b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes            const uint8_t *p = GetOffsetPtr(alloc, 0, 0, 0, lod, (RsAllocationCubemapFace)face);
1374b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1384b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes            GLenum t = GL_TEXTURE_2D;
1394b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes            if (alloc->mHal.state.hasFaces) {
1404b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                t = gFaceOrder[face];
1414b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes            }
1424b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1434b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes            if (isFirstUpload) {
1444b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                RSD_CALL_GL(glTexImage2D, t, lod, drv->glFormat,
1454b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                             alloc->mHal.state.type->getLODDimX(lod),
1464b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                             alloc->mHal.state.type->getLODDimY(lod),
1474b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                             0, drv->glFormat, drv->glType, p);
1484b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes            } else {
1494b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                RSD_CALL_GL(glTexSubImage2D, t, lod, 0, 0,
1504b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                                alloc->mHal.state.type->getLODDimX(lod),
1514b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                                alloc->mHal.state.type->getLODDimY(lod),
1524b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                                drv->glFormat, drv->glType, p);
1534b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes            }
1544b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        }
1554b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
156cfa089df23ff50fcd5ed3854c54991d30be5fc7eElliott Hughes
1574b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    if (alloc->mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
1584b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        RSD_CALL_GL(glGenerateMipmap, drv->glTarget);
1594b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
1604b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    rsdGLCheckError(rsc, "Upload2DTexture");
1614b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
1624b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#endif
1634b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1644b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesstatic void UploadToTexture(const Context *rsc, const Allocation *alloc) {
1654b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#ifndef RS_COMPATIBILITY_LIB
1664b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
1674b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1684b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
1694b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        if (!drv->textureID) {
1704b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes            RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
1714b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        }
1724b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        return;
1734b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
1744b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1754b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    if (!drv->glType || !drv->glFormat) {
176cfa089df23ff50fcd5ed3854c54991d30be5fc7eElliott Hughes        return;
1774b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
1784b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1794b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    if (!alloc->mHal.drvState.lod[0].mallocPtr) {
1804b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        return;
1814b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
1824b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1834b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    bool isFirstUpload = false;
1844b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1854b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    if (!drv->textureID) {
1864b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
1874b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        isFirstUpload = true;
1884b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
1894b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1904b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    Upload2DTexture(rsc, alloc, isFirstUpload);
1914b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1924b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
1934b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        if (alloc->mHal.drvState.lod[0].mallocPtr) {
1944b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes            free(alloc->mHal.drvState.lod[0].mallocPtr);
1954b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes            alloc->mHal.drvState.lod[0].mallocPtr = nullptr;
1964b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        }
1974b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
1984b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    rsdGLCheckError(rsc, "UploadToTexture");
19940eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes#endif
2004b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
20140eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes
2024b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesstatic void AllocateRenderTarget(const Context *rsc, const Allocation *alloc) {
2034b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#ifndef RS_COMPATIBILITY_LIB
2044b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
2054b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2064b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    if (!drv->glFormat) {
2074b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        return;
208cfa089df23ff50fcd5ed3854c54991d30be5fc7eElliott Hughes    }
2094b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2104b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    if (!drv->renderTargetID) {
2114b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        RSD_CALL_GL(glGenRenderbuffers, 1, &drv->renderTargetID);
21240eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes
21340eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes        if (!drv->renderTargetID) {
21440eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes            // This should generally not happen
2154b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes            ALOGE("allocateRenderTarget failed to gen mRenderTargetID");
2164b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes            rsc->dumpDebug();
2174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes            return;
2184b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        }
2194b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        RSD_CALL_GL(glBindRenderbuffer, GL_RENDERBUFFER, drv->renderTargetID);
2204b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes        RSD_CALL_GL(glRenderbufferStorage, GL_RENDERBUFFER, drv->glFormat,
2214b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                    alloc->mHal.drvState.lod[0].dimX, alloc->mHal.drvState.lod[0].dimY);
2224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
2234b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    rsdGLCheckError(rsc, "AllocateRenderTarget");
2244b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#endif
2254b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
22640eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes
2274b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesstatic void UploadToBufferObject(const Context *rsc, const Allocation *alloc) {
2284b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#ifndef RS_COMPATIBILITY_LIB
2294b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
2304b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2314b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    rsAssert(!alloc->mHal.state.type->getDimY());
2324b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    rsAssert(!alloc->mHal.state.type->getDimZ());
2334b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
234    //alloc->mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
235
236    if (!drv->bufferID) {
237        RSD_CALL_GL(glGenBuffers, 1, &drv->bufferID);
238    }
239    if (!drv->bufferID) {
240        ALOGE("Upload to buffer object failed");
241        drv->uploadDeferred = true;
242        return;
243    }
244    RSD_CALL_GL(glBindBuffer, drv->glTarget, drv->bufferID);
245    RSD_CALL_GL(glBufferData, drv->glTarget,
246                alloc->mHal.state.type->getPackedSizeBytes(),
247                alloc->mHal.drvState.lod[0].mallocPtr, GL_DYNAMIC_DRAW);
248    RSD_CALL_GL(glBindBuffer, drv->glTarget, 0);
249    rsdGLCheckError(rsc, "UploadToBufferObject");
250#endif
251}
252
253
254static size_t DeriveYUVLayout(int yuv, Allocation::Hal::DrvState *state) {
255    // YUV only supports basic 2d
256    // so we can stash the plane pointers in the mipmap levels.
257    size_t uvSize = 0;
258    state->lod[1].dimX = state->lod[0].dimX / 2;
259    state->lod[1].dimY = state->lod[0].dimY / 2;
260    state->lod[2].dimX = state->lod[0].dimX / 2;
261    state->lod[2].dimY = state->lod[0].dimY / 2;
262    state->yuv.shift = 1;
263    state->yuv.step = 1;
264    state->lodCount = 3;
265
266#ifndef RS_SERVER
267    switch(yuv) {
268    case HAL_PIXEL_FORMAT_YV12:
269        state->lod[2].stride = rsRound(state->lod[0].stride >> 1, 16);
270        state->lod[2].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) +
271                (state->lod[0].stride * state->lod[0].dimY);
272        uvSize += state->lod[2].stride * state->lod[2].dimY;
273
274        state->lod[1].stride = state->lod[2].stride;
275        state->lod[1].mallocPtr = ((uint8_t *)state->lod[2].mallocPtr) +
276                (state->lod[2].stride * state->lod[2].dimY);
277        uvSize += state->lod[1].stride * state->lod[2].dimY;
278        break;
279    case HAL_PIXEL_FORMAT_YCrCb_420_SP:  // NV21
280        //state->lod[1].dimX = state->lod[0].dimX;
281        state->lod[1].stride = state->lod[0].stride;
282        state->lod[2].stride = state->lod[0].stride;
283        state->lod[2].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) +
284                (state->lod[0].stride * state->lod[0].dimY);
285        state->lod[1].mallocPtr = ((uint8_t *)state->lod[2].mallocPtr) + 1;
286        uvSize += state->lod[1].stride * state->lod[1].dimY;
287        state->yuv.step = 2;
288        break;
289#ifndef RS_COMPATIBILITY_LIB
290    case HAL_PIXEL_FORMAT_YCbCr_420_888:
291        // This will be filled in by ioReceive()
292        break;
293#endif
294    default:
295        rsAssert(0);
296    }
297#endif
298    return uvSize;
299}
300
301
302static size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc,
303        const Type *type, uint8_t *ptr) {
304    alloc->mHal.drvState.lod[0].dimX = type->getDimX();
305    alloc->mHal.drvState.lod[0].dimY = type->getDimY();
306    alloc->mHal.drvState.lod[0].dimZ = type->getDimZ();
307    alloc->mHal.drvState.lod[0].mallocPtr = 0;
308    // Stride needs to be 16-byte aligned too!
309    size_t stride = alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes();
310    alloc->mHal.drvState.lod[0].stride = rsRound(stride, 16);
311    alloc->mHal.drvState.lodCount = type->getLODCount();
312    alloc->mHal.drvState.faceCount = type->getDimFaces();
313
314    size_t offsets[Allocation::MAX_LOD];
315    memset(offsets, 0, sizeof(offsets));
316
317    size_t o = alloc->mHal.drvState.lod[0].stride * rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) *
318            rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u);
319    if (alloc->mHal.state.yuv) {
320        o += DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState);
321
322        for (uint32_t ct = 1; ct < alloc->mHal.drvState.lodCount; ct++) {
323            offsets[ct] = (size_t)alloc->mHal.drvState.lod[ct].mallocPtr;
324        }
325    } else if(alloc->mHal.drvState.lodCount > 1) {
326        uint32_t tx = alloc->mHal.drvState.lod[0].dimX;
327        uint32_t ty = alloc->mHal.drvState.lod[0].dimY;
328        uint32_t tz = alloc->mHal.drvState.lod[0].dimZ;
329        for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
330            alloc->mHal.drvState.lod[lod].dimX = tx;
331            alloc->mHal.drvState.lod[lod].dimY = ty;
332            alloc->mHal.drvState.lod[lod].dimZ = tz;
333            alloc->mHal.drvState.lod[lod].stride =
334                    rsRound(tx * type->getElementSizeBytes(), 16);
335            offsets[lod] = o;
336            o += alloc->mHal.drvState.lod[lod].stride * rsMax(ty, 1u) * rsMax(tz, 1u);
337            if (tx > 1) tx >>= 1;
338            if (ty > 1) ty >>= 1;
339            if (tz > 1) tz >>= 1;
340        }
341    }
342
343    alloc->mHal.drvState.faceOffset = o;
344
345    alloc->mHal.drvState.lod[0].mallocPtr = ptr;
346    for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
347        alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod];
348    }
349
350    size_t allocSize = alloc->mHal.drvState.faceOffset;
351    if(alloc->mHal.drvState.faceCount) {
352        allocSize *= 6;
353    }
354
355    return allocSize;
356}
357
358static uint8_t* allocAlignedMemory(size_t allocSize, bool forceZero) {
359    // We align all allocations to a 16-byte boundary.
360    uint8_t* ptr = (uint8_t *)memalign(16, allocSize);
361    if (!ptr) {
362        return nullptr;
363    }
364    if (forceZero) {
365        memset(ptr, 0, allocSize);
366    }
367    return ptr;
368}
369
370bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) {
371    DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation));
372    if (!drv) {
373        return false;
374    }
375    alloc->mHal.drv = drv;
376
377    // Calculate the object size.
378    size_t allocSize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), nullptr);
379
380    uint8_t * ptr = nullptr;
381    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) {
382
383    } else if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
384        // Allocation is allocated when the surface is created
385        // in getSurface
386    } else if (alloc->mHal.state.userProvidedPtr != nullptr) {
387        // user-provided allocation
388        // limitations: no faces, no LOD, USAGE_SCRIPT or SCRIPT+TEXTURE only
389        if (!(alloc->mHal.state.usageFlags == (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED) ||
390              alloc->mHal.state.usageFlags == (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED | RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE))) {
391            ALOGE("Can't use user-allocated buffers if usage is not USAGE_SCRIPT | USAGE_SHARED or USAGE_SCRIPT | USAGE_SHARED | USAGE_GRAPHICS_TEXTURE");
392            return false;
393        }
394        if (alloc->getType()->getDimLOD() || alloc->getType()->getDimFaces()) {
395            ALOGE("User-allocated buffers must not have multiple faces or LODs");
396            return false;
397        }
398
399        // rows must be 16-byte aligned
400        // validate that here, otherwise fall back to not use the user-backed allocation
401        if (((alloc->getType()->getDimX() * alloc->getType()->getElement()->getSizeBytes()) % 16) != 0) {
402            ALOGV("User-backed allocation failed stride requirement, falling back to separate allocation");
403            drv->useUserProvidedPtr = false;
404
405            ptr = allocAlignedMemory(allocSize, forceZero);
406            if (!ptr) {
407                alloc->mHal.drv = nullptr;
408                free(drv);
409                return false;
410            }
411
412        } else {
413            drv->useUserProvidedPtr = true;
414            ptr = (uint8_t*)alloc->mHal.state.userProvidedPtr;
415        }
416    } else {
417        ptr = allocAlignedMemory(allocSize, forceZero);
418        if (!ptr) {
419            alloc->mHal.drv = nullptr;
420            free(drv);
421            return false;
422        }
423    }
424    // Build the pointer tables
425    size_t verifySize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), ptr);
426    if(allocSize != verifySize) {
427        rsAssert(!"Size mismatch");
428    }
429
430#ifndef RS_SERVER
431    drv->glTarget = GL_NONE;
432    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
433        if (alloc->mHal.state.hasFaces) {
434            drv->glTarget = GL_TEXTURE_CUBE_MAP;
435        } else {
436            drv->glTarget = GL_TEXTURE_2D;
437        }
438    } else {
439        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
440            drv->glTarget = GL_ARRAY_BUFFER;
441        }
442    }
443#endif
444
445#ifndef RS_COMPATIBILITY_LIB
446    drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType());
447    drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind());
448#else
449    drv->glType = 0;
450    drv->glFormat = 0;
451#endif
452
453    if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) {
454        drv->uploadDeferred = true;
455    }
456
457
458    drv->readBackFBO = nullptr;
459
460    // fill out the initial state of the buffer if we couldn't use the user-provided ptr and USAGE_SHARED was accepted
461    if ((alloc->mHal.state.userProvidedPtr != 0) && (drv->useUserProvidedPtr == false)) {
462        rsdAllocationData2D(rsc, alloc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, alloc->getType()->getDimX(), alloc->getType()->getDimY(), alloc->mHal.state.userProvidedPtr, allocSize, 0);
463    }
464
465
466#ifdef RS_FIND_OFFSETS
467    ALOGE("pointer for allocation: %p", alloc);
468    ALOGE("pointer for allocation.drv: %p", &alloc->mHal.drv);
469#endif
470
471
472    return true;
473}
474
475void rsdAllocationAdapterOffset(const Context *rsc, const Allocation *alloc) {
476    //ALOGE("rsdAllocationAdapterOffset");
477
478    // Get a base pointer to the new LOD
479    const Allocation *base = alloc->mHal.state.baseAlloc;
480    const Type *type = alloc->mHal.state.type;
481    if (base == nullptr) {
482        return;
483    }
484
485    uint8_t * ptrA = (uint8_t *)base->getPointerUnchecked(alloc->mHal.state.originX, alloc->mHal.state.originY);
486    uint8_t * ptrB = (uint8_t *)base->getPointerUnchecked(0, 0);
487
488    //ALOGE("rsdAllocationAdapterOffset  %p  %p", ptrA, ptrB);
489    //ALOGE("rsdAllocationAdapterOffset  lodCount %i", alloc->mHal.drvState.lodCount);
490
491    const int lodBias = alloc->mHal.state.originLOD;
492    uint32_t lodCount = rsMax(alloc->mHal.drvState.lodCount, (uint32_t)1);
493    for (uint32_t lod=0; lod < lodCount; lod++) {
494        alloc->mHal.drvState.lod[lod] = base->mHal.drvState.lod[lod + lodBias];
495        alloc->mHal.drvState.lod[lod].mallocPtr =
496                ((uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr + (ptrA - ptrB));
497        //ALOGE("rsdAllocationAdapterOffset  lod  %p  %i %i", alloc->mHal.drvState.lod[lod].mallocPtr, alloc->mHal.drvState.lod[lod].dimX, alloc->mHal.drvState.lod[lod].dimY);
498    }
499}
500
501bool rsdAllocationAdapterInit(const Context *rsc, Allocation *alloc) {
502    DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation));
503    if (!drv) {
504        return false;
505    }
506    alloc->mHal.drv = drv;
507
508    // We need to build an allocation that looks like a subset of the parent allocation
509    rsdAllocationAdapterOffset(rsc, alloc);
510
511    return true;
512}
513
514void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) {
515    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
516
517    if (alloc->mHal.state.baseAlloc == nullptr) {
518#ifndef RS_COMPATIBILITY_LIB
519        if (drv->bufferID) {
520            // Causes a SW crash....
521            //ALOGV(" mBufferID %i", mBufferID);
522            //glDeleteBuffers(1, &mBufferID);
523            //mBufferID = 0;
524        }
525        if (drv->textureID) {
526            RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID);
527            drv->textureID = 0;
528        }
529        if (drv->renderTargetID) {
530            RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID);
531            drv->renderTargetID = 0;
532        }
533#endif
534
535        if (alloc->mHal.drvState.lod[0].mallocPtr) {
536            // don't free user-allocated ptrs or IO_OUTPUT buffers
537            if (!(drv->useUserProvidedPtr) &&
538                !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) &&
539                !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
540                    free(alloc->mHal.drvState.lod[0].mallocPtr);
541            }
542            alloc->mHal.drvState.lod[0].mallocPtr = nullptr;
543        }
544
545#ifndef RS_COMPATIBILITY_LIB
546        if (drv->readBackFBO != nullptr) {
547            delete drv->readBackFBO;
548            drv->readBackFBO = nullptr;
549        }
550
551        if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) &&
552            (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
553
554            DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
555            ANativeWindow *nw = drv->wndSurface;
556            if (nw) {
557                GraphicBufferMapper &mapper = GraphicBufferMapper::get();
558                mapper.unlock(drv->wndBuffer->handle);
559                int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
560
561                drv->wndSurface = nullptr;
562                native_window_api_disconnect(nw, NATIVE_WINDOW_API_CPU);
563                nw->decStrong(nullptr);
564            }
565        }
566#endif
567    }
568
569    free(drv);
570    alloc->mHal.drv = nullptr;
571}
572
573void rsdAllocationResize(const Context *rsc, const Allocation *alloc,
574                         const Type *newType, bool zeroNew) {
575    const uint32_t oldDimX = alloc->mHal.drvState.lod[0].dimX;
576    const uint32_t dimX = newType->getDimX();
577
578    // can't resize Allocations with user-allocated buffers
579    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
580        ALOGE("Resize cannot be called on a USAGE_SHARED allocation");
581        return;
582    }
583    void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr;
584    // Calculate the object size
585    size_t s = AllocationBuildPointerTable(rsc, alloc, newType, nullptr);
586    uint8_t *ptr = (uint8_t *)realloc(oldPtr, s);
587    // Build the relative pointer tables.
588    size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr);
589    if(s != verifySize) {
590        rsAssert(!"Size mismatch");
591    }
592
593
594    if (dimX > oldDimX) {
595        size_t stride = alloc->mHal.state.elementSizeBytes;
596        memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX,
597                 0, stride * (dimX - oldDimX));
598    }
599}
600
601static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) {
602#ifndef RS_COMPATIBILITY_LIB
603    if (!alloc->getIsScript()) {
604        return; // nothing to sync
605    }
606
607    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
608    RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer;
609
610    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
611    if (!drv->textureID && !drv->renderTargetID) {
612        return; // nothing was rendered here yet, so nothing to sync
613    }
614    if (drv->readBackFBO == nullptr) {
615        drv->readBackFBO = new RsdFrameBufferObj();
616        drv->readBackFBO->setColorTarget(drv, 0);
617        drv->readBackFBO->setDimensions(alloc->getType()->getDimX(),
618                                        alloc->getType()->getDimY());
619    }
620
621    // Bind the framebuffer object so we can read back from it
622    drv->readBackFBO->setActive(rsc);
623
624    // Do the readback
625    RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX,
626                alloc->mHal.drvState.lod[0].dimY,
627                drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr);
628
629    // Revert framebuffer to its original
630    lastFbo->setActive(rsc);
631#endif
632}
633
634
635void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc,
636                         RsAllocationUsageType src) {
637    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
638
639    if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
640        if(!alloc->getIsRenderTarget()) {
641            rsc->setError(RS_ERROR_FATAL_DRIVER,
642                          "Attempting to sync allocation from render target, "
643                          "for non-render target allocation");
644        } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) {
645            rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA"
646                                                 "render target");
647        } else {
648            rsdAllocationSyncFromFBO(rsc, alloc);
649        }
650        return;
651    }
652
653    rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT || src == RS_ALLOCATION_USAGE_SHARED);
654
655    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
656        UploadToTexture(rsc, alloc);
657    } else {
658        if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) &&
659            !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
660            AllocateRenderTarget(rsc, alloc);
661        }
662    }
663    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
664        UploadToBufferObject(rsc, alloc);
665    }
666
667    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
668
669        if (src == RS_ALLOCATION_USAGE_SHARED) {
670            // just a memory fence for the CPU driver
671            // vendor drivers probably want to flush any dirty cachelines for
672            // this particular Allocation
673            __sync_synchronize();
674        }
675    }
676
677    drv->uploadDeferred = false;
678}
679
680void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) {
681    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
682    drv->uploadDeferred = true;
683}
684
685#ifndef RS_COMPATIBILITY_LIB
686static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
687    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
688
689    int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer);
690    if (r) {
691        rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer.");
692        return false;
693    }
694
695    // Must lock the whole surface
696    GraphicBufferMapper &mapper = GraphicBufferMapper::get();
697    Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height);
698
699    void *dst = nullptr;
700    mapper.lock(drv->wndBuffer->handle,
701            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
702            bounds, &dst);
703    alloc->mHal.drvState.lod[0].mallocPtr = dst;
704    alloc->mHal.drvState.lod[0].stride = drv->wndBuffer->stride * alloc->mHal.state.elementSizeBytes;
705    rsAssert((alloc->mHal.drvState.lod[0].stride & 0xf) == 0);
706
707    return true;
708}
709#endif
710
711void rsdAllocationSetSurface(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
712#ifndef RS_COMPATIBILITY_LIB
713    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
714    ANativeWindow *old = drv->wndSurface;
715
716    if (nw) {
717        nw->incStrong(nullptr);
718    }
719
720    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
721        //TODO finish support for render target + script
722        drv->wnd = nw;
723        return;
724    }
725
726    // Cleanup old surface if there is one.
727    if (drv->wndSurface) {
728        ANativeWindow *old = drv->wndSurface;
729        GraphicBufferMapper &mapper = GraphicBufferMapper::get();
730        mapper.unlock(drv->wndBuffer->handle);
731        old->cancelBuffer(old, drv->wndBuffer, -1);
732        drv->wndSurface = nullptr;
733
734        native_window_api_disconnect(old, NATIVE_WINDOW_API_CPU);
735        old->decStrong(nullptr);
736    }
737
738    if (nw != nullptr) {
739        int32_t r;
740        uint32_t flags = 0;
741
742        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
743            flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
744        }
745        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
746            flags |= GRALLOC_USAGE_HW_RENDER;
747        }
748
749        r = native_window_api_connect(nw, NATIVE_WINDOW_API_CPU);
750        if (r) {
751            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
752            goto error;
753        }
754
755        r = native_window_set_usage(nw, flags);
756        if (r) {
757            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
758            goto error;
759        }
760
761        r = native_window_set_buffers_dimensions(nw, alloc->mHal.drvState.lod[0].dimX,
762                                                 alloc->mHal.drvState.lod[0].dimY);
763        if (r) {
764            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions.");
765            goto error;
766        }
767
768        int format = 0;
769        const Element *e = alloc->mHal.state.type->getElement();
770        rsAssert(e->getType() == RS_TYPE_UNSIGNED_8);
771        rsAssert(e->getVectorSize() == 4);
772        rsAssert(e->getKind() == RS_KIND_PIXEL_RGBA);
773        format = PIXEL_FORMAT_RGBA_8888;
774
775        r = native_window_set_buffers_format(nw, format);
776        if (r) {
777            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer format.");
778            goto error;
779        }
780
781        IoGetBuffer(rsc, alloc, nw);
782        drv->wndSurface = nw;
783    }
784
785    return;
786
787 error:
788
789    if (nw) {
790        nw->decStrong(nullptr);
791    }
792
793
794#endif
795}
796
797void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) {
798#ifndef RS_COMPATIBILITY_LIB
799    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
800    ANativeWindow *nw = drv->wndSurface;
801    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
802        RsdHal *dc = (RsdHal *)rsc->mHal.drv;
803        RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
804        return;
805    }
806    if (nw) {
807        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
808            GraphicBufferMapper &mapper = GraphicBufferMapper::get();
809            mapper.unlock(drv->wndBuffer->handle);
810            int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
811            if (r) {
812                rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer.");
813                return;
814            }
815
816            IoGetBuffer(rsc, alloc, nw);
817        }
818    } else {
819        rsc->setError(RS_ERROR_DRIVER, "Sent IO buffer with no attached surface.");
820        return;
821    }
822#endif
823}
824
825void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) {
826#ifndef RS_COMPATIBILITY_LIB
827    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
828    if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
829        drv->surfaceTexture->updateTexImage();
830    }
831#endif
832    if (alloc->mHal.state.yuv) {
833        DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState);
834    }
835}
836
837
838void rsdAllocationData1D(const Context *rsc, const Allocation *alloc,
839                         uint32_t xoff, uint32_t lod, size_t count,
840                         const void *data, size_t sizeBytes) {
841    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
842
843    const size_t eSize = alloc->mHal.state.type->getElementSizeBytes();
844    uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
845    size_t size = count * eSize;
846    if (ptr != data) {
847        // Skip the copy if we are the same allocation. This can arise from
848        // our Bitmap optimization, where we share the same storage.
849        if (alloc->mHal.state.hasReferences) {
850            alloc->incRefs(data, count);
851            alloc->decRefs(ptr, count);
852        }
853        memcpy(ptr, data, size);
854    }
855    drv->uploadDeferred = true;
856}
857
858void rsdAllocationData2D(const Context *rsc, const Allocation *alloc,
859                         uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
860                         uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
861    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
862
863    size_t eSize = alloc->mHal.state.elementSizeBytes;
864    size_t lineSize = eSize * w;
865    if (!stride) {
866        stride = lineSize;
867    }
868
869    if (alloc->mHal.drvState.lod[0].mallocPtr) {
870        const uint8_t *src = static_cast<const uint8_t *>(data);
871        uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
872        if (dst == src) {
873            // Skip the copy if we are the same allocation. This can arise from
874            // our Bitmap optimization, where we share the same storage.
875            drv->uploadDeferred = true;
876            return;
877        }
878
879        for (uint32_t line=yoff; line < (yoff+h); line++) {
880            if (alloc->mHal.state.hasReferences) {
881                alloc->incRefs(src, w);
882                alloc->decRefs(dst, w);
883            }
884            memcpy(dst, src, lineSize);
885            src += stride;
886            dst += alloc->mHal.drvState.lod[lod].stride;
887        }
888        if (alloc->mHal.state.yuv) {
889            size_t clineSize = lineSize;
890            int lod = 1;
891            int maxLod = 2;
892            if (alloc->mHal.state.yuv == HAL_PIXEL_FORMAT_YV12) {
893                maxLod = 3;
894                clineSize >>= 1;
895            } else if (alloc->mHal.state.yuv == HAL_PIXEL_FORMAT_YCrCb_420_SP) {
896                lod = 2;
897                maxLod = 3;
898            }
899
900            while (lod < maxLod) {
901                uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
902
903                for (uint32_t line=(yoff >> 1); line < ((yoff+h)>>1); line++) {
904                    memcpy(dst, src, clineSize);
905                    src += alloc->mHal.drvState.lod[lod].stride;
906                    dst += alloc->mHal.drvState.lod[lod].stride;
907                }
908                lod++;
909            }
910
911        }
912        drv->uploadDeferred = true;
913    } else {
914        Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h);
915    }
916}
917
918void rsdAllocationData3D(const Context *rsc, const Allocation *alloc,
919                         uint32_t xoff, uint32_t yoff, uint32_t zoff,
920                         uint32_t lod,
921                         uint32_t w, uint32_t h, uint32_t d, const void *data,
922                         size_t sizeBytes, size_t stride) {
923    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
924
925    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
926    uint32_t lineSize = eSize * w;
927    if (!stride) {
928        stride = lineSize;
929    }
930
931    if (alloc->mHal.drvState.lod[0].mallocPtr) {
932        const uint8_t *src = static_cast<const uint8_t *>(data);
933        for (uint32_t z = zoff; z < d; z++) {
934            uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, z, lod,
935                                        RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
936            if (dst == src) {
937                // Skip the copy if we are the same allocation. This can arise from
938                // our Bitmap optimization, where we share the same storage.
939                drv->uploadDeferred = true;
940                return;
941            }
942
943            for (uint32_t line=yoff; line < (yoff+h); line++) {
944                if (alloc->mHal.state.hasReferences) {
945                    alloc->incRefs(src, w);
946                    alloc->decRefs(dst, w);
947                }
948                memcpy(dst, src, lineSize);
949                src += stride;
950                dst += alloc->mHal.drvState.lod[lod].stride;
951            }
952        }
953        drv->uploadDeferred = true;
954    }
955}
956
957void rsdAllocationRead1D(const Context *rsc, const Allocation *alloc,
958                         uint32_t xoff, uint32_t lod, size_t count,
959                         void *data, size_t sizeBytes) {
960    const size_t eSize = alloc->mHal.state.type->getElementSizeBytes();
961    const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
962    if (data != ptr) {
963        // Skip the copy if we are the same allocation. This can arise from
964        // our Bitmap optimization, where we share the same storage.
965        memcpy(data, ptr, count * eSize);
966    }
967}
968
969void rsdAllocationRead2D(const Context *rsc, const Allocation *alloc,
970                                uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
971                                uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
972    size_t eSize = alloc->mHal.state.elementSizeBytes;
973    size_t lineSize = eSize * w;
974    if (!stride) {
975        stride = lineSize;
976    }
977
978    if (alloc->mHal.drvState.lod[0].mallocPtr) {
979        uint8_t *dst = static_cast<uint8_t *>(data);
980        const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
981        if (dst == src) {
982            // Skip the copy if we are the same allocation. This can arise from
983            // our Bitmap optimization, where we share the same storage.
984            return;
985        }
986
987        for (uint32_t line=yoff; line < (yoff+h); line++) {
988            memcpy(dst, src, lineSize);
989            dst += stride;
990            src += alloc->mHal.drvState.lod[lod].stride;
991        }
992    } else {
993        ALOGE("Add code to readback from non-script memory");
994    }
995}
996
997
998void rsdAllocationRead3D(const Context *rsc, const Allocation *alloc,
999                         uint32_t xoff, uint32_t yoff, uint32_t zoff,
1000                         uint32_t lod,
1001                         uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride) {
1002    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
1003    uint32_t lineSize = eSize * w;
1004    if (!stride) {
1005        stride = lineSize;
1006    }
1007
1008    if (alloc->mHal.drvState.lod[0].mallocPtr) {
1009        uint8_t *dst = static_cast<uint8_t *>(data);
1010        for (uint32_t z = zoff; z < d; z++) {
1011            const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, z, lod,
1012                                              RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1013            if (dst == src) {
1014                // Skip the copy if we are the same allocation. This can arise from
1015                // our Bitmap optimization, where we share the same storage.
1016                return;
1017            }
1018
1019            for (uint32_t line=yoff; line < (yoff+h); line++) {
1020                memcpy(dst, src, lineSize);
1021                dst += stride;
1022                src += alloc->mHal.drvState.lod[lod].stride;
1023            }
1024        }
1025    }
1026}
1027
1028void * rsdAllocationLock1D(const android::renderscript::Context *rsc,
1029                          const android::renderscript::Allocation *alloc) {
1030    return alloc->mHal.drvState.lod[0].mallocPtr;
1031}
1032
1033void rsdAllocationUnlock1D(const android::renderscript::Context *rsc,
1034                          const android::renderscript::Allocation *alloc) {
1035
1036}
1037
1038void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc,
1039                               const android::renderscript::Allocation *dstAlloc,
1040                               uint32_t dstXoff, uint32_t dstLod, size_t count,
1041                               const android::renderscript::Allocation *srcAlloc,
1042                               uint32_t srcXoff, uint32_t srcLod) {
1043}
1044
1045
1046void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc,
1047                                      const android::renderscript::Allocation *dstAlloc,
1048                                      uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
1049                                      RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
1050                                      const android::renderscript::Allocation *srcAlloc,
1051                                      uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
1052                                      RsAllocationCubemapFace srcFace) {
1053    size_t elementSize = dstAlloc->getType()->getElementSizeBytes();
1054    for (uint32_t i = 0; i < h; i ++) {
1055        uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, 0, dstLod, dstFace);
1056        uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, 0, srcLod, srcFace);
1057        memcpy(dstPtr, srcPtr, w * elementSize);
1058
1059        //ALOGE("COPIED dstXoff(%u), dstYoff(%u), dstLod(%u), dstFace(%u), w(%u), h(%u), srcXoff(%u), srcYoff(%u), srcLod(%u), srcFace(%u)",
1060        //     dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
1061    }
1062}
1063
1064void rsdAllocationData3D_alloc_script(const android::renderscript::Context *rsc,
1065                                      const android::renderscript::Allocation *dstAlloc,
1066                                      uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff, uint32_t dstLod,
1067                                      uint32_t w, uint32_t h, uint32_t d,
1068                                      const android::renderscript::Allocation *srcAlloc,
1069                                      uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff, uint32_t srcLod) {
1070    uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes();
1071    for (uint32_t j = 0; j < d; j++) {
1072        for (uint32_t i = 0; i < h; i ++) {
1073            uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstZoff + j,
1074                                           dstLod, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1075            uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcZoff + j,
1076                                           srcLod, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1077            memcpy(dstPtr, srcPtr, w * elementSize);
1078
1079            //ALOGE("COPIED dstXoff(%u), dstYoff(%u), dstLod(%u), dstFace(%u), w(%u), h(%u), srcXoff(%u), srcYoff(%u), srcLod(%u), srcFace(%u)",
1080            //     dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
1081        }
1082    }
1083}
1084
1085void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc,
1086                               const android::renderscript::Allocation *dstAlloc,
1087                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
1088                               RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
1089                               const android::renderscript::Allocation *srcAlloc,
1090                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
1091                               RsAllocationCubemapFace srcFace) {
1092    if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
1093        rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
1094                                             "yet implemented.");
1095        return;
1096    }
1097    rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff,
1098                                     dstLod, dstFace, w, h, srcAlloc,
1099                                     srcXoff, srcYoff, srcLod, srcFace);
1100}
1101
1102void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc,
1103                               const android::renderscript::Allocation *dstAlloc,
1104                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
1105                               uint32_t dstLod,
1106                               uint32_t w, uint32_t h, uint32_t d,
1107                               const android::renderscript::Allocation *srcAlloc,
1108                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
1109                               uint32_t srcLod) {
1110    if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
1111        rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
1112                                             "yet implemented.");
1113        return;
1114    }
1115    rsdAllocationData3D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff, dstZoff,
1116                                     dstLod, w, h, d, srcAlloc,
1117                                     srcXoff, srcYoff, srcZoff, srcLod);
1118}
1119
1120void rsdAllocationElementData(const Context *rsc, const Allocation *alloc,
1121                              uint32_t x, uint32_t y, uint32_t z,
1122                              const void *data, uint32_t cIdx, size_t sizeBytes) {
1123    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
1124
1125    uint8_t * ptr = GetOffsetPtr(alloc, x, y, z, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1126
1127    const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
1128    ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
1129
1130    if (alloc->mHal.state.hasReferences) {
1131        e->incRefs(data);
1132        e->decRefs(ptr);
1133    }
1134
1135    memcpy(ptr, data, sizeBytes);
1136    drv->uploadDeferred = true;
1137}
1138
1139void rsdAllocationElementRead(const Context *rsc, const Allocation *alloc,
1140                              uint32_t x, uint32_t y, uint32_t z,
1141                              void *data, uint32_t cIdx, size_t sizeBytes) {
1142    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
1143
1144    uint8_t * ptr = GetOffsetPtr(alloc, x, y, z, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1145
1146    const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
1147    ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
1148
1149    memcpy(data, ptr, sizeBytes);
1150}
1151
1152static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
1153    uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1154    uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
1155
1156    for (uint32_t y=0; y < h; y++) {
1157        uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, 0, lod + 1, face);
1158        const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, 0, y*2, lod, face);
1159        const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, 0, y*2+1, lod, face);
1160
1161        for (uint32_t x=0; x < w; x++) {
1162            *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
1163            oPtr ++;
1164            i1 += 2;
1165            i2 += 2;
1166        }
1167    }
1168}
1169
1170static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
1171    uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1172    uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
1173
1174    for (uint32_t y=0; y < h; y++) {
1175        uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, 0, lod + 1, face);
1176        const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, 0, lod, face);
1177        const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, 0, lod, face);
1178
1179        for (uint32_t x=0; x < w; x++) {
1180            *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
1181            oPtr ++;
1182            i1 += 2;
1183            i2 += 2;
1184        }
1185    }
1186}
1187
1188static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
1189    uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1190    uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
1191
1192    for (uint32_t y=0; y < h; y++) {
1193        uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, 0, lod + 1, face);
1194        const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, 0, lod, face);
1195        const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, 0, lod, face);
1196
1197        for (uint32_t x=0; x < w; x++) {
1198            *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
1199            oPtr ++;
1200            i1 += 2;
1201            i2 += 2;
1202        }
1203    }
1204}
1205
1206void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) {
1207    if(!alloc->mHal.drvState.lod[0].mallocPtr) {
1208        return;
1209    }
1210    uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1;
1211    for (uint32_t face = 0; face < numFaces; face ++) {
1212        for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) {
1213            switch (alloc->getType()->getElement()->getSizeBits()) {
1214            case 32:
1215                mip8888(alloc, lod, (RsAllocationCubemapFace)face);
1216                break;
1217            case 16:
1218                mip565(alloc, lod, (RsAllocationCubemapFace)face);
1219                break;
1220            case 8:
1221                mip8(alloc, lod, (RsAllocationCubemapFace)face);
1222                break;
1223            }
1224        }
1225    }
1226}
1227
1228uint32_t rsdAllocationGrallocBits(const android::renderscript::Context *rsc,
1229                                  android::renderscript::Allocation *alloc)
1230{
1231    return 0;
1232}
1233
1234void rsdAllocationUpdateCachedObject(const Context *rsc,
1235                                     const Allocation *alloc,
1236                                     rs_allocation *obj)
1237{
1238    obj->p = alloc;
1239#ifdef __LP64__
1240    if (alloc != nullptr) {
1241        obj->r = alloc->mHal.drvState.lod[0].mallocPtr;
1242        obj->v1 = alloc->mHal.drv;
1243        obj->v2 = (void *)alloc->mHal.drvState.lod[0].stride;
1244    } else {
1245        obj->r = nullptr;
1246        obj->v1 = nullptr;
1247        obj->v2 = nullptr;
1248    }
1249#endif
1250}
1251