1eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams/*
2bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams * Copyright (C) 2013 The Android Open Source Project
3eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams *
4eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams * you may not use this file except in compliance with the License.
6eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams * You may obtain a copy of the License at
7eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams *
8eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams *
10eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams * Unless required by applicable law or agreed to in writing, software
11eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams * See the License for the specific language governing permissions and
14eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams * limitations under the License.
15eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams */
16eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
17eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams#include "rsdCore.h"
18eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams#include "rsdAllocation.h"
19eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
20eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams#include "rsAllocation.h"
21eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
22b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
237ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams#include "system/window.h"
247ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams#include "ui/Rect.h"
257ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams#include "ui/GraphicBufferMapper.h"
260b575de8ed0b628d84d256f5846500b0385979bdTim Murray#endif
2793eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams
2810f317038dd53543dc8c7f5afe26a6360adfb5f3Stephen Hines#ifdef RS_COMPATIBILITY_LIB
2910f317038dd53543dc8c7f5afe26a6360adfb5f3Stephen Hines#include "rsCompatibilityLib.h"
3010f317038dd53543dc8c7f5afe26a6360adfb5f3Stephen Hines#else
3193eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#include "rsdFrameBufferObj.h"
3258fd6a5f10480551786739280d56dfa620c80b39Andy McFadden#include "gui/GLConsumer.h"
33733396b67724162844ea2785c7495115dc5ee8d8Jason Sams#include "gui/CpuConsumer.h"
34733396b67724162844ea2785c7495115dc5ee8d8Jason Sams#include "gui/Surface.h"
3593eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#include "hardware/gralloc.h"
367ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
37eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams#include <GLES/gl.h>
38eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams#include <GLES2/gl2.h>
39eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams#include <GLES/glext.h>
4093eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
41eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
420b575de8ed0b628d84d256f5846500b0385979bdTim Murray#ifdef RS_SERVER
430b575de8ed0b628d84d256f5846500b0385979bdTim Murray// server requires malloc.h for memalign
440b575de8ed0b628d84d256f5846500b0385979bdTim Murray#include <malloc.h>
450b575de8ed0b628d84d256f5846500b0385979bdTim Murray#endif
460b575de8ed0b628d84d256f5846500b0385979bdTim Murray
47eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Samsusing namespace android;
48eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Samsusing namespace android::renderscript;
49eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
5093eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
51eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Samsconst static GLenum gFaceOrder[] = {
52eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    GL_TEXTURE_CUBE_MAP_POSITIVE_X,
53eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
54eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
55eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
56eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
57eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
58eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams};
59eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
60a614ae175bbf97201b5e18984d814a1d3e86faa8Jason SamsGLenum rsdTypeToGLType(RsDataType t) {
61a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    switch (t) {
62a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_TYPE_UNSIGNED_5_6_5:    return GL_UNSIGNED_SHORT_5_6_5;
63a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_TYPE_UNSIGNED_5_5_5_1:  return GL_UNSIGNED_SHORT_5_5_5_1;
64a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_TYPE_UNSIGNED_4_4_4_4:  return GL_UNSIGNED_SHORT_4_4_4_4;
65a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams
66a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    //case RS_TYPE_FLOAT_16:      return GL_HALF_FLOAT;
67a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_TYPE_FLOAT_32:      return GL_FLOAT;
68a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_TYPE_UNSIGNED_8:    return GL_UNSIGNED_BYTE;
69a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_TYPE_UNSIGNED_16:   return GL_UNSIGNED_SHORT;
70a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_TYPE_SIGNED_8:      return GL_BYTE;
71a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_TYPE_SIGNED_16:     return GL_SHORT;
72a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    default:    break;
73a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    }
74a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    return 0;
75a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams}
76a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams
77a614ae175bbf97201b5e18984d814a1d3e86faa8Jason SamsGLenum rsdKindToGLFormat(RsDataKind k) {
78a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    switch (k) {
79a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_KIND_PIXEL_L: return GL_LUMINANCE;
80a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_KIND_PIXEL_A: return GL_ALPHA;
81a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_KIND_PIXEL_LA: return GL_LUMINANCE_ALPHA;
82a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_KIND_PIXEL_RGB: return GL_RGB;
83a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_KIND_PIXEL_RGBA: return GL_RGBA;
84a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    case RS_KIND_PIXEL_DEPTH: return GL_DEPTH_COMPONENT16;
85a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    default: break;
86a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    }
87a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    return 0;
88a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams}
8993eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
90a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams
91807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Samsuint8_t *GetOffsetPtr(const android::renderscript::Allocation *alloc,
923bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                      uint32_t xoff, uint32_t yoff, uint32_t zoff,
933bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                      uint32_t lod, RsAllocationCubemapFace face) {
94709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    uint8_t *ptr = (uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
95709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    ptr += face * alloc->mHal.drvState.faceOffset;
963bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams    ptr += zoff * alloc->mHal.drvState.lod[lod].dimY * alloc->mHal.drvState.lod[lod].stride;
97709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    ptr += yoff * alloc->mHal.drvState.lod[lod].stride;
98807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    ptr += xoff * alloc->mHal.state.elementSizeBytes;
99807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    return ptr;
100807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams}
101807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams
102a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams
1032382aba4a55c6ae74789c478eead8fbd96593321Jason Samsstatic void Update2DTexture(const Context *rsc, const Allocation *alloc, const void *ptr,
1042382aba4a55c6ae74789c478eead8fbd96593321Jason Sams                            uint32_t xoff, uint32_t yoff, uint32_t lod,
1052382aba4a55c6ae74789c478eead8fbd96593321Jason Sams                            RsAllocationCubemapFace face, uint32_t w, uint32_t h) {
10693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
107eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
108eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
109eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    rsAssert(drv->textureID);
1102382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
1112382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
112eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    GLenum t = GL_TEXTURE_2D;
113eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    if (alloc->mHal.state.hasFaces) {
114eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        t = gFaceOrder[face];
115eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
1162382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(glTexSubImage2D, t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr);
11793eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
118eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams}
119eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
120eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
12193eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
122eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Samsstatic void Upload2DTexture(const Context *rsc, const Allocation *alloc, bool isFirstUpload) {
123eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
124eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
1252382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
1262382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
127eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
128eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    uint32_t faceCount = 1;
129eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    if (alloc->mHal.state.hasFaces) {
130eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        faceCount = 6;
131eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
132eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
133eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    rsdGLCheckError(rsc, "Upload2DTexture 1 ");
134eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    for (uint32_t face = 0; face < faceCount; face ++) {
135eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        for (uint32_t lod = 0; lod < alloc->mHal.state.type->getLODCount(); lod++) {
1363bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams            const uint8_t *p = GetOffsetPtr(alloc, 0, 0, 0, lod, (RsAllocationCubemapFace)face);
137eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
138eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            GLenum t = GL_TEXTURE_2D;
139eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            if (alloc->mHal.state.hasFaces) {
140eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams                t = gFaceOrder[face];
141eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            }
142eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
143eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            if (isFirstUpload) {
1442382aba4a55c6ae74789c478eead8fbd96593321Jason Sams                RSD_CALL_GL(glTexImage2D, t, lod, drv->glFormat,
145eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams                             alloc->mHal.state.type->getLODDimX(lod),
146eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams                             alloc->mHal.state.type->getLODDimY(lod),
147a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams                             0, drv->glFormat, drv->glType, p);
148eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            } else {
1492382aba4a55c6ae74789c478eead8fbd96593321Jason Sams                RSD_CALL_GL(glTexSubImage2D, t, lod, 0, 0,
150eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams                                alloc->mHal.state.type->getLODDimX(lod),
151eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams                                alloc->mHal.state.type->getLODDimY(lod),
152a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams                                drv->glFormat, drv->glType, p);
153eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            }
154eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        }
155eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
156eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
157eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    if (alloc->mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
1582382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glGenerateMipmap, drv->glTarget);
159eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
160eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    rsdGLCheckError(rsc, "Upload2DTexture");
161eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams}
16293eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
163eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
164eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Samsstatic void UploadToTexture(const Context *rsc, const Allocation *alloc) {
16593eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
166eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
167eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
1683522f40418fdf877f5a136475dbf75e57a3b7c77Jason Sams    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
16941e373d91a60043afa0f9abd026218b49cbc1201Jason Sams        if (!drv->textureID) {
17041e373d91a60043afa0f9abd026218b49cbc1201Jason Sams            RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
17141e373d91a60043afa0f9abd026218b49cbc1201Jason Sams        }
17241e373d91a60043afa0f9abd026218b49cbc1201Jason Sams        return;
17341e373d91a60043afa0f9abd026218b49cbc1201Jason Sams    }
17441e373d91a60043afa0f9abd026218b49cbc1201Jason Sams
175a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    if (!drv->glType || !drv->glFormat) {
176eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        return;
177eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
178eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
179709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    if (!alloc->mHal.drvState.lod[0].mallocPtr) {
180eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        return;
181eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
182eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
183eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    bool isFirstUpload = false;
184eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
185eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    if (!drv->textureID) {
1862382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
187eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        isFirstUpload = true;
188eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
189eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
190eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    Upload2DTexture(rsc, alloc, isFirstUpload);
191eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
192eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
193709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        if (alloc->mHal.drvState.lod[0].mallocPtr) {
194709a0978ae141198018ca9769f8d96292a8928e6Jason Sams            free(alloc->mHal.drvState.lod[0].mallocPtr);
19544bef6fba6244292b751387f3d6c31cca96c28adChris Wailes            alloc->mHal.drvState.lod[0].mallocPtr = nullptr;
196eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        }
197eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
198eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    rsdGLCheckError(rsc, "UploadToTexture");
19993eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
200eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams}
201eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
202eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Samsstatic void AllocateRenderTarget(const Context *rsc, const Allocation *alloc) {
20393eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
204eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
205eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
206a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    if (!drv->glFormat) {
207eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        return;
208eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
209eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
210eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    if (!drv->renderTargetID) {
2112382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glGenRenderbuffers, 1, &drv->renderTargetID);
212eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
213eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        if (!drv->renderTargetID) {
214eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            // This should generally not happen
215af12ac6a08651464f8d823add667c706f993b587Steve Block            ALOGE("allocateRenderTarget failed to gen mRenderTargetID");
216eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            rsc->dumpDebug();
217eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            return;
218eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        }
2192382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glBindRenderbuffer, GL_RENDERBUFFER, drv->renderTargetID);
2202382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glRenderbufferStorage, GL_RENDERBUFFER, drv->glFormat,
221a572aca4eb4ddb32c10baa1f529431cfefd756b8Jason Sams                    alloc->mHal.drvState.lod[0].dimX, alloc->mHal.drvState.lod[0].dimY);
222eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
223eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    rsdGLCheckError(rsc, "AllocateRenderTarget");
22493eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
225eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams}
226eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
227eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Samsstatic void UploadToBufferObject(const Context *rsc, const Allocation *alloc) {
22893eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
229eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
230eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
231eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    rsAssert(!alloc->mHal.state.type->getDimY());
232eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    rsAssert(!alloc->mHal.state.type->getDimZ());
233eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
234eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    //alloc->mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
235eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
236eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    if (!drv->bufferID) {
2372382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        RSD_CALL_GL(glGenBuffers, 1, &drv->bufferID);
238eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
239eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    if (!drv->bufferID) {
240af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("Upload to buffer object failed");
241eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        drv->uploadDeferred = true;
242eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        return;
243eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
2442382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(glBindBuffer, drv->glTarget, drv->bufferID);
24561656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams    RSD_CALL_GL(glBufferData, drv->glTarget,
24661656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams                alloc->mHal.state.type->getPackedSizeBytes(),
24761656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams                alloc->mHal.drvState.lod[0].mallocPtr, GL_DYNAMIC_DRAW);
2482382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    RSD_CALL_GL(glBindBuffer, drv->glTarget, 0);
249eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    rsdGLCheckError(rsc, "UploadToBufferObject");
25093eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
251eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams}
252eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
2530b575de8ed0b628d84d256f5846500b0385979bdTim Murray
2549d8e5af146326250ebc1e9b3c93402bd0e7669bbJason Samsstatic size_t DeriveYUVLayout(int yuv, Allocation::Hal::DrvState *state) {
255b010bc019c6ad11224ff6f73c22bce13cdab06e3Pirama Arumuga Nainar#ifndef RS_COMPATIBILITY_LIB
256b010bc019c6ad11224ff6f73c22bce13cdab06e3Pirama Arumuga Nainar    // For the flexible YCbCr format, layout is initialized during call to
257b010bc019c6ad11224ff6f73c22bce13cdab06e3Pirama Arumuga Nainar    // Allocation::ioReceive.  Return early and avoid clobberring any
258b010bc019c6ad11224ff6f73c22bce13cdab06e3Pirama Arumuga Nainar    // pre-existing layout.
259b010bc019c6ad11224ff6f73c22bce13cdab06e3Pirama Arumuga Nainar    if (yuv == HAL_PIXEL_FORMAT_YCbCr_420_888) {
260b010bc019c6ad11224ff6f73c22bce13cdab06e3Pirama Arumuga Nainar        return 0;
261b010bc019c6ad11224ff6f73c22bce13cdab06e3Pirama Arumuga Nainar    }
262b010bc019c6ad11224ff6f73c22bce13cdab06e3Pirama Arumuga Nainar#endif
263b010bc019c6ad11224ff6f73c22bce13cdab06e3Pirama Arumuga Nainar
264733396b67724162844ea2785c7495115dc5ee8d8Jason Sams    // YUV only supports basic 2d
265733396b67724162844ea2785c7495115dc5ee8d8Jason Sams    // so we can stash the plane pointers in the mipmap levels.
2669d8e5af146326250ebc1e9b3c93402bd0e7669bbJason Sams    size_t uvSize = 0;
26761656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams    state->lod[1].dimX = state->lod[0].dimX / 2;
26861656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams    state->lod[1].dimY = state->lod[0].dimY / 2;
26961656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams    state->lod[2].dimX = state->lod[0].dimX / 2;
27061656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams    state->lod[2].dimY = state->lod[0].dimY / 2;
27161656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams    state->yuv.shift = 1;
27261656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams    state->yuv.step = 1;
27361656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams    state->lodCount = 3;
27461656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams
2750b575de8ed0b628d84d256f5846500b0385979bdTim Murray#ifndef RS_SERVER
276733396b67724162844ea2785c7495115dc5ee8d8Jason Sams    switch(yuv) {
277733396b67724162844ea2785c7495115dc5ee8d8Jason Sams    case HAL_PIXEL_FORMAT_YV12:
2784961cceab2b71bf0ab59e1b66a7559f67ed28781Jason Sams        state->lod[2].stride = rsRound(state->lod[0].stride >> 1, 16);
2794961cceab2b71bf0ab59e1b66a7559f67ed28781Jason Sams        state->lod[2].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) +
280733396b67724162844ea2785c7495115dc5ee8d8Jason Sams                (state->lod[0].stride * state->lod[0].dimY);
2819d8e5af146326250ebc1e9b3c93402bd0e7669bbJason Sams        uvSize += state->lod[2].stride * state->lod[2].dimY;
282733396b67724162844ea2785c7495115dc5ee8d8Jason Sams
2834961cceab2b71bf0ab59e1b66a7559f67ed28781Jason Sams        state->lod[1].stride = state->lod[2].stride;
2844961cceab2b71bf0ab59e1b66a7559f67ed28781Jason Sams        state->lod[1].mallocPtr = ((uint8_t *)state->lod[2].mallocPtr) +
2854961cceab2b71bf0ab59e1b66a7559f67ed28781Jason Sams                (state->lod[2].stride * state->lod[2].dimY);
2864961cceab2b71bf0ab59e1b66a7559f67ed28781Jason Sams        uvSize += state->lod[1].stride * state->lod[2].dimY;
287733396b67724162844ea2785c7495115dc5ee8d8Jason Sams        break;
288733396b67724162844ea2785c7495115dc5ee8d8Jason Sams    case HAL_PIXEL_FORMAT_YCrCb_420_SP:  // NV21
28961656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams        //state->lod[1].dimX = state->lod[0].dimX;
290733396b67724162844ea2785c7495115dc5ee8d8Jason Sams        state->lod[1].stride = state->lod[0].stride;
29161656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams        state->lod[2].stride = state->lod[0].stride;
29261656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams        state->lod[2].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) +
293733396b67724162844ea2785c7495115dc5ee8d8Jason Sams                (state->lod[0].stride * state->lod[0].dimY);
29461656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams        state->lod[1].mallocPtr = ((uint8_t *)state->lod[2].mallocPtr) + 1;
2959d8e5af146326250ebc1e9b3c93402bd0e7669bbJason Sams        uvSize += state->lod[1].stride * state->lod[1].dimY;
29661656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams        state->yuv.step = 2;
29761656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams        break;
298733396b67724162844ea2785c7495115dc5ee8d8Jason Sams    default:
299733396b67724162844ea2785c7495115dc5ee8d8Jason Sams        rsAssert(0);
300733396b67724162844ea2785c7495115dc5ee8d8Jason Sams    }
3010b575de8ed0b628d84d256f5846500b0385979bdTim Murray#endif
3029d8e5af146326250ebc1e9b3c93402bd0e7669bbJason Sams    return uvSize;
303733396b67724162844ea2785c7495115dc5ee8d8Jason Sams}
304733396b67724162844ea2785c7495115dc5ee8d8Jason Sams
305463bfced38d97605dd8403a5e9435d9ad3394e35Jason Samsstatic size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc,
30647a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang                                          const Type *type, uint8_t *ptr, size_t requiredAlignment) {
307709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    alloc->mHal.drvState.lod[0].dimX = type->getDimX();
308709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    alloc->mHal.drvState.lod[0].dimY = type->getDimY();
309f2b611e26db1c83ad5e0e0cdebcfa639b03fba75Jason Sams    alloc->mHal.drvState.lod[0].dimZ = type->getDimZ();
310709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    alloc->mHal.drvState.lod[0].mallocPtr = 0;
31147a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang    // Stride needs to be aligned to a boundary defined by requiredAlignment!
31294999c3c7e3dcb6b6c0a9fe5785a0d3216357b0eStephen Hines    size_t stride = alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes();
31347a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang    alloc->mHal.drvState.lod[0].stride = rsRound(stride, requiredAlignment);
314709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    alloc->mHal.drvState.lodCount = type->getLODCount();
315709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    alloc->mHal.drvState.faceCount = type->getDimFaces();
316807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams
317807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    size_t offsets[Allocation::MAX_LOD];
318807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    memset(offsets, 0, sizeof(offsets));
319807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams
320709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    size_t o = alloc->mHal.drvState.lod[0].stride * rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) *
321709a0978ae141198018ca9769f8d96292a8928e6Jason Sams            rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u);
322d06653c70a67a987a2a1deb1bfb139e927fb7bd4Jason Sams    if (alloc->mHal.state.yuv) {
323d06653c70a67a987a2a1deb1bfb139e927fb7bd4Jason Sams        o += DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState);
324d06653c70a67a987a2a1deb1bfb139e927fb7bd4Jason Sams
325d06653c70a67a987a2a1deb1bfb139e927fb7bd4Jason Sams        for (uint32_t ct = 1; ct < alloc->mHal.drvState.lodCount; ct++) {
326d06653c70a67a987a2a1deb1bfb139e927fb7bd4Jason Sams            offsets[ct] = (size_t)alloc->mHal.drvState.lod[ct].mallocPtr;
327d06653c70a67a987a2a1deb1bfb139e927fb7bd4Jason Sams        }
328d06653c70a67a987a2a1deb1bfb139e927fb7bd4Jason Sams    } else if(alloc->mHal.drvState.lodCount > 1) {
329709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        uint32_t tx = alloc->mHal.drvState.lod[0].dimX;
330709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        uint32_t ty = alloc->mHal.drvState.lod[0].dimY;
331709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        uint32_t tz = alloc->mHal.drvState.lod[0].dimZ;
332709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
333709a0978ae141198018ca9769f8d96292a8928e6Jason Sams            alloc->mHal.drvState.lod[lod].dimX = tx;
334709a0978ae141198018ca9769f8d96292a8928e6Jason Sams            alloc->mHal.drvState.lod[lod].dimY = ty;
335709a0978ae141198018ca9769f8d96292a8928e6Jason Sams            alloc->mHal.drvState.lod[lod].dimZ = tz;
33694999c3c7e3dcb6b6c0a9fe5785a0d3216357b0eStephen Hines            alloc->mHal.drvState.lod[lod].stride =
33747a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang                    rsRound(tx * type->getElementSizeBytes(), requiredAlignment);
338807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams            offsets[lod] = o;
339709a0978ae141198018ca9769f8d96292a8928e6Jason Sams            o += alloc->mHal.drvState.lod[lod].stride * rsMax(ty, 1u) * rsMax(tz, 1u);
340807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams            if (tx > 1) tx >>= 1;
341807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams            if (ty > 1) ty >>= 1;
342807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams            if (tz > 1) tz >>= 1;
343807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams        }
344807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    }
345bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams
346709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    alloc->mHal.drvState.faceOffset = o;
347807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams
348709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    alloc->mHal.drvState.lod[0].mallocPtr = ptr;
349709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
350709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod];
351463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    }
352463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams
353709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    size_t allocSize = alloc->mHal.drvState.faceOffset;
354709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    if(alloc->mHal.drvState.faceCount) {
355463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams        allocSize *= 6;
356463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    }
357463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams
358463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    return allocSize;
359463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams}
360463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams
36147a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wangstatic size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc,
36247a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang                                          const Type *type, uint8_t *ptr) {
36347a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang    return AllocationBuildPointerTable(rsc, alloc, type, ptr, Allocation::kMinimumRSAlignment);
36447a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang}
36547a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang
36647a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wangstatic uint8_t* allocAlignedMemory(size_t allocSize, bool forceZero, size_t requiredAlignment) {
36747a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang    // We align all allocations to a boundary defined by requiredAlignment.
36847a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang    uint8_t* ptr = (uint8_t *)memalign(requiredAlignment, allocSize);
369c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray    if (!ptr) {
37044bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
371c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray    }
372c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray    if (forceZero) {
373c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray        memset(ptr, 0, allocSize);
374c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray    }
375c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray    return ptr;
376c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray}
377c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray
37847a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wangbool rsdAllocationInitStrided(const Context *rsc, Allocation *alloc, bool forceZero, size_t requiredAlignment) {
379463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation));
380463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    if (!drv) {
381463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams        return false;
382463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    }
383463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    alloc->mHal.drv = drv;
384463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams
38547a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang    // Check if requiredAlignment is power of 2, also requiredAlignment should be larger or equal than kMinimumRSAlignment.
38647a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang    if ((requiredAlignment & (requiredAlignment-1)) != 0 || requiredAlignment < Allocation::kMinimumRSAlignment) {
38747a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang        ALOGE("requiredAlignment must be power of 2");
38847a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang        return false;
38947a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang    }
390463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    // Calculate the object size.
39147a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang    size_t allocSize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), nullptr, requiredAlignment);
392463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams
39344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    uint8_t * ptr = nullptr;
3947ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) {
395733396b67724162844ea2785c7495115dc5ee8d8Jason Sams
396733396b67724162844ea2785c7495115dc5ee8d8Jason Sams    } else if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
397733396b67724162844ea2785c7495115dc5ee8d8Jason Sams        // Allocation is allocated when the surface is created
398733396b67724162844ea2785c7495115dc5ee8d8Jason Sams        // in getSurface
39947a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang#ifdef RS_COMPATIBILITY_LIB
40047a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang    } else if (alloc->mHal.state.usageFlags == (RS_ALLOCATION_USAGE_INCREMENTAL_SUPPORT | RS_ALLOCATION_USAGE_SHARED)) {
40147a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang        if (alloc->mHal.state.userProvidedPtr == nullptr) {
40247a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang            ALOGE("User-backed buffer pointer cannot be null");
40347a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang            return false;
40447a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang        }
40547a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang        if (alloc->getType()->getDimLOD() || alloc->getType()->getDimFaces()) {
40647a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang            ALOGE("User-allocated buffers must not have multiple faces or LODs");
40747a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang            return false;
40847a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang        }
40947a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang
41047a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang        drv->useUserProvidedPtr = true;
41147a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang        ptr = (uint8_t*)alloc->mHal.state.userProvidedPtr;
41247a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang#endif
41344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    } else if (alloc->mHal.state.userProvidedPtr != nullptr) {
4142e1a94df812f0caa42ff24eaefeba8f90fbdf1acTim Murray        // user-provided allocation
415ba24d08b4228fd1e35cd79319f15fddc11f24a49Tim Murray        // limitations: no faces, no LOD, USAGE_SCRIPT or SCRIPT+TEXTURE only
416ba24d08b4228fd1e35cd79319f15fddc11f24a49Tim Murray        if (!(alloc->mHal.state.usageFlags == (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED) ||
417ba24d08b4228fd1e35cd79319f15fddc11f24a49Tim Murray              alloc->mHal.state.usageFlags == (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED | RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE))) {
418ba24d08b4228fd1e35cd79319f15fddc11f24a49Tim Murray            ALOGE("Can't use user-allocated buffers if usage is not USAGE_SCRIPT | USAGE_SHARED or USAGE_SCRIPT | USAGE_SHARED | USAGE_GRAPHICS_TEXTURE");
4192e1a94df812f0caa42ff24eaefeba8f90fbdf1acTim Murray            return false;
4202e1a94df812f0caa42ff24eaefeba8f90fbdf1acTim Murray        }
4212e1a94df812f0caa42ff24eaefeba8f90fbdf1acTim Murray        if (alloc->getType()->getDimLOD() || alloc->getType()->getDimFaces()) {
4222e1a94df812f0caa42ff24eaefeba8f90fbdf1acTim Murray            ALOGE("User-allocated buffers must not have multiple faces or LODs");
4232e1a94df812f0caa42ff24eaefeba8f90fbdf1acTim Murray            return false;
4242e1a94df812f0caa42ff24eaefeba8f90fbdf1acTim Murray        }
425c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray
42647a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang        // rows must be aligned based on requiredAlignment.
427c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray        // validate that here, otherwise fall back to not use the user-backed allocation
42847a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang        if (((alloc->getType()->getDimX() * alloc->getType()->getElement()->getSizeBytes()) % requiredAlignment) != 0) {
429c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray            ALOGV("User-backed allocation failed stride requirement, falling back to separate allocation");
430c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray            drv->useUserProvidedPtr = false;
431c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray
43247a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang            ptr = allocAlignedMemory(allocSize, forceZero, requiredAlignment);
433c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray            if (!ptr) {
43444bef6fba6244292b751387f3d6c31cca96c28adChris Wailes                alloc->mHal.drv = nullptr;
435c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray                free(drv);
436c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray                return false;
437c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray            }
438c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray
439c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray        } else {
440c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray            drv->useUserProvidedPtr = true;
441c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray            ptr = (uint8_t*)alloc->mHal.state.userProvidedPtr;
442c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray        }
4437ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    } else {
44447a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang        ptr = allocAlignedMemory(allocSize, forceZero, requiredAlignment);
445179e9a457095fea4c9e6d366c269754b882d05ddJason Sams        if (!ptr) {
44644bef6fba6244292b751387f3d6c31cca96c28adChris Wailes            alloc->mHal.drv = nullptr;
447179e9a457095fea4c9e6d366c269754b882d05ddJason Sams            free(drv);
448179e9a457095fea4c9e6d366c269754b882d05ddJason Sams            return false;
449179e9a457095fea4c9e6d366c269754b882d05ddJason Sams        }
450eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
451463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    // Build the pointer tables
45247a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang    size_t verifySize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), ptr, requiredAlignment);
453463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    if(allocSize != verifySize) {
454463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams        rsAssert(!"Size mismatch");
455807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    }
456eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
4570b575de8ed0b628d84d256f5846500b0385979bdTim Murray#ifndef RS_SERVER
458eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    drv->glTarget = GL_NONE;
459eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
460eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        if (alloc->mHal.state.hasFaces) {
461eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            drv->glTarget = GL_TEXTURE_CUBE_MAP;
462eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        } else {
463eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            drv->glTarget = GL_TEXTURE_2D;
464eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        }
465eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    } else {
466eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
467eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            drv->glTarget = GL_ARRAY_BUFFER;
468eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        }
469eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
4700b575de8ed0b628d84d256f5846500b0385979bdTim Murray#endif
471eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
47293eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
473a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType());
474a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams    drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind());
47593eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#else
47693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams    drv->glType = 0;
47793eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams    drv->glFormat = 0;
47893eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
479a614ae175bbf97201b5e18984d814a1d3e86faa8Jason Sams
480eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) {
481eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        drv->uploadDeferred = true;
482eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
483a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk
484b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams
48544bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    drv->readBackFBO = nullptr;
486a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk
487c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray    // fill out the initial state of the buffer if we couldn't use the user-provided ptr and USAGE_SHARED was accepted
488c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray    if ((alloc->mHal.state.userProvidedPtr != 0) && (drv->useUserProvidedPtr == false)) {
489c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray        rsdAllocationData2D(rsc, alloc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, alloc->getType()->getDimX(), alloc->getType()->getDimY(), alloc->mHal.state.userProvidedPtr, allocSize, 0);
490c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray    }
491c2cfe6abfc74befbaa4d2ca09024a27fbfb1f515Tim Murray
492e3af53b643677c40d228ffd3624cf259f4dc68edTim Murray
493e3af53b643677c40d228ffd3624cf259f4dc68edTim Murray#ifdef RS_FIND_OFFSETS
494e3af53b643677c40d228ffd3624cf259f4dc68edTim Murray    ALOGE("pointer for allocation: %p", alloc);
495e3af53b643677c40d228ffd3624cf259f4dc68edTim Murray    ALOGE("pointer for allocation.drv: %p", &alloc->mHal.drv);
496e3af53b643677c40d228ffd3624cf259f4dc68edTim Murray#endif
497e3af53b643677c40d228ffd3624cf259f4dc68edTim Murray
498e3af53b643677c40d228ffd3624cf259f4dc68edTim Murray
499eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    return true;
500eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams}
501eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
50247a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wangbool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) {
50347a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang    return rsdAllocationInitStrided(rsc, alloc, forceZero, Allocation::kMinimumRSAlignment);
50447a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang}
50547a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang
506bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Samsvoid rsdAllocationAdapterOffset(const Context *rsc, const Allocation *alloc) {
507bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    //ALOGE("rsdAllocationAdapterOffset");
508eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
509bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    // Get a base pointer to the new LOD
510bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    const Allocation *base = alloc->mHal.state.baseAlloc;
511bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    const Type *type = alloc->mHal.state.type;
512bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    if (base == nullptr) {
513bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams        return;
514eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
515bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams
516bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    //ALOGE("rsdAllocationAdapterOffset  %p  %p", ptrA, ptrB);
517bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    //ALOGE("rsdAllocationAdapterOffset  lodCount %i", alloc->mHal.drvState.lodCount);
518bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams
519bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    const int lodBias = alloc->mHal.state.originLOD;
520bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    uint32_t lodCount = rsMax(alloc->mHal.drvState.lodCount, (uint32_t)1);
521bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    for (uint32_t lod=0; lod < lodCount; lod++) {
522bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams        alloc->mHal.drvState.lod[lod] = base->mHal.drvState.lod[lod + lodBias];
5232178d4262c06210b27c51a04379d23d1368b2e8bJason Sams        alloc->mHal.drvState.lod[lod].mallocPtr = GetOffsetPtr(alloc,
5242178d4262c06210b27c51a04379d23d1368b2e8bJason Sams                      alloc->mHal.state.originX, alloc->mHal.state.originY, alloc->mHal.state.originZ,
5252178d4262c06210b27c51a04379d23d1368b2e8bJason Sams                      lodBias, (RsAllocationCubemapFace)alloc->mHal.state.originFace);
526eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
527bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams}
528bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams
529bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Samsbool rsdAllocationAdapterInit(const Context *rsc, Allocation *alloc) {
530bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation));
531bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    if (!drv) {
532bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams        return false;
533eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
534bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    alloc->mHal.drv = drv;
535bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams
536bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    // We need to build an allocation that looks like a subset of the parent allocation
537bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    rsdAllocationAdapterOffset(rsc, alloc);
538bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams
539bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    return true;
540bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams}
541bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams
542bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Samsvoid rsdAllocationDestroy(const Context *rsc, Allocation *alloc) {
543bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
544bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams
545bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    if (alloc->mHal.state.baseAlloc == nullptr) {
546bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams#ifndef RS_COMPATIBILITY_LIB
547bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams        if (drv->bufferID) {
548bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            // Causes a SW crash....
549bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            //ALOGV(" mBufferID %i", mBufferID);
550bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            //glDeleteBuffers(1, &mBufferID);
551bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            //mBufferID = 0;
552bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams        }
553bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams        if (drv->textureID) {
554bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID);
555bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            drv->textureID = 0;
556bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams        }
557bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams        if (drv->renderTargetID) {
558bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID);
559bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            drv->renderTargetID = 0;
560bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams        }
56193eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
562eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
563bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams        if (alloc->mHal.drvState.lod[0].mallocPtr) {
564bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            // don't free user-allocated ptrs or IO_OUTPUT buffers
565bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            if (!(drv->useUserProvidedPtr) &&
566bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams                !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) &&
567bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams                !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
568bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams                    free(alloc->mHal.drvState.lod[0].mallocPtr);
569bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            }
570bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            alloc->mHal.drvState.lod[0].mallocPtr = nullptr;
5712e1a94df812f0caa42ff24eaefeba8f90fbdf1acTim Murray        }
57293eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams
57393eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
574bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams        if (drv->readBackFBO != nullptr) {
575bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            delete drv->readBackFBO;
576bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            drv->readBackFBO = nullptr;
577bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams        }
57810c9dd709f8b52213c6792961afa9c5de807db5cJason Sams
579bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams        if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) &&
580bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
58110c9dd709f8b52213c6792961afa9c5de807db5cJason Sams
582bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
583bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            ANativeWindow *nw = drv->wndSurface;
584bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            if (nw) {
585bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams                GraphicBufferMapper &mapper = GraphicBufferMapper::get();
586bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams                mapper.unlock(drv->wndBuffer->handle);
587abc1f945f6fd3de8d7d8f980f7d62c72d97ee802Jason Sams                int32_t r = nw->cancelBuffer(nw, drv->wndBuffer, -1);
5885537da4b965ce6d0c2043b945757b06688ee803eTong, Bo
589bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams                drv->wndSurface = nullptr;
590bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams                native_window_api_disconnect(nw, NATIVE_WINDOW_API_CPU);
591bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams                nw->decStrong(nullptr);
592bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams            }
5933a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray        }
59493eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
595bc9dc27b84f4e5c72d4dbe8a8e01af87dd780f79Jason Sams    }
59693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams
597eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    free(drv);
59844bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    alloc->mHal.drv = nullptr;
599eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams}
600eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
601eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Samsvoid rsdAllocationResize(const Context *rsc, const Allocation *alloc,
602eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams                         const Type *newType, bool zeroNew) {
603a572aca4eb4ddb32c10baa1f529431cfefd756b8Jason Sams    const uint32_t oldDimX = alloc->mHal.drvState.lod[0].dimX;
604a572aca4eb4ddb32c10baa1f529431cfefd756b8Jason Sams    const uint32_t dimX = newType->getDimX();
605a572aca4eb4ddb32c10baa1f529431cfefd756b8Jason Sams
6069e2bda58db9e6427a1640ea302d4df079ffa0252Tim Murray    // can't resize Allocations with user-allocated buffers
6079e2bda58db9e6427a1640ea302d4df079ffa0252Tim Murray    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
6089e2bda58db9e6427a1640ea302d4df079ffa0252Tim Murray        ALOGE("Resize cannot be called on a USAGE_SHARED allocation");
6099e2bda58db9e6427a1640ea302d4df079ffa0252Tim Murray        return;
6109e2bda58db9e6427a1640ea302d4df079ffa0252Tim Murray    }
611709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr;
612463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    // Calculate the object size
61344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    size_t s = AllocationBuildPointerTable(rsc, alloc, newType, nullptr);
614463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    uint8_t *ptr = (uint8_t *)realloc(oldPtr, s);
615463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    // Build the relative pointer tables.
616463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr);
617463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    if(s != verifySize) {
618463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams        rsAssert(!"Size mismatch");
619463bfced38d97605dd8403a5e9435d9ad3394e35Jason Sams    }
620eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
621eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
622eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    if (dimX > oldDimX) {
623099bc262f862cdeb547cf8a78fe9e0e92560f437Tim Murray        size_t stride = alloc->mHal.state.elementSizeBytes;
624709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX,
6252275e9cf534ff601fe42a72ff50580b230c04fe2Jason Sams                 0, stride * (dimX - oldDimX));
626eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
627eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams}
628eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
629a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchoukstatic void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) {
63093eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
631a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    if (!alloc->getIsScript()) {
632a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk        return; // nothing to sync
633a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    }
634a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk
635a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
636a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer;
637a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk
638a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
639a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    if (!drv->textureID && !drv->renderTargetID) {
640a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk        return; // nothing was rendered here yet, so nothing to sync
641a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    }
64244bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (drv->readBackFBO == nullptr) {
643a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk        drv->readBackFBO = new RsdFrameBufferObj();
644a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk        drv->readBackFBO->setColorTarget(drv, 0);
645a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk        drv->readBackFBO->setDimensions(alloc->getType()->getDimX(),
646a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk                                        alloc->getType()->getDimY());
647a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    }
648a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk
649a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    // Bind the framebuffer object so we can read back from it
650a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    drv->readBackFBO->setActive(rsc);
651a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk
652a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    // Do the readback
653709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX,
654709a0978ae141198018ca9769f8d96292a8928e6Jason Sams                alloc->mHal.drvState.lod[0].dimY,
655709a0978ae141198018ca9769f8d96292a8928e6Jason Sams                drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr);
656a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk
657a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    // Revert framebuffer to its original
658a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    lastFbo->setActive(rsc);
65993eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
660a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk}
661eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
662eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
663eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Samsvoid rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc,
664eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams                         RsAllocationUsageType src) {
665eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
666eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
667a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk    if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
668a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk        if(!alloc->getIsRenderTarget()) {
669a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk            rsc->setError(RS_ERROR_FATAL_DRIVER,
670a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk                          "Attempting to sync allocation from render target, "
671a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk                          "for non-render target allocation");
672a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk        } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) {
673a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk            rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA"
674a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk                                                 "render target");
675a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk        } else {
676a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk            rsdAllocationSyncFromFBO(rsc, alloc);
677a94952436aeb251f587c1bccdf94c7f75285dfe2Alex Sakhartchouk        }
678eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        return;
679eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
680eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
681b8a94e26c0a5e8f58d5b6ed04e46b411e95b77a4Jason Sams    rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT || src == RS_ALLOCATION_USAGE_SHARED);
682eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
683eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
684eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        UploadToTexture(rsc, alloc);
685eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    } else {
686b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) &&
6870dc66936c02f285a242b6a68166d6f03618a0f07Jason Sams            !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
688eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            AllocateRenderTarget(rsc, alloc);
689eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        }
690eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
691eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
692eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        UploadToBufferObject(rsc, alloc);
693eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
694eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
695ba24d08b4228fd1e35cd79319f15fddc11f24a49Tim Murray    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
696b8a94e26c0a5e8f58d5b6ed04e46b411e95b77a4Jason Sams
697b8a94e26c0a5e8f58d5b6ed04e46b411e95b77a4Jason Sams        if (src == RS_ALLOCATION_USAGE_SHARED) {
698b8a94e26c0a5e8f58d5b6ed04e46b411e95b77a4Jason Sams            // just a memory fence for the CPU driver
699b8a94e26c0a5e8f58d5b6ed04e46b411e95b77a4Jason Sams            // vendor drivers probably want to flush any dirty cachelines for
700b8a94e26c0a5e8f58d5b6ed04e46b411e95b77a4Jason Sams            // this particular Allocation
701b8a94e26c0a5e8f58d5b6ed04e46b411e95b77a4Jason Sams            __sync_synchronize();
702b8a94e26c0a5e8f58d5b6ed04e46b411e95b77a4Jason Sams        }
703ba24d08b4228fd1e35cd79319f15fddc11f24a49Tim Murray    }
704ba24d08b4228fd1e35cd79319f15fddc11f24a49Tim Murray
705eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    drv->uploadDeferred = false;
706eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams}
707eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
708eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Samsvoid rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) {
709eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
710eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    drv->uploadDeferred = true;
711eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams}
712eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
7134961cceab2b71bf0ab59e1b66a7559f67ed28781Jason Sams#ifndef RS_COMPATIBILITY_LIB
7147ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Samsstatic bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
7157ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
7167ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
717cd919a177f2b25af1b304dd4d79f0a768fed342fJamie Gennis    int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer);
7187ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    if (r) {
7197ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams        rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer.");
7207ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams        return false;
7217ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    }
7227ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
7237ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    // Must lock the whole surface
7247ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    GraphicBufferMapper &mapper = GraphicBufferMapper::get();
7257ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height);
7267ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
72744bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    void *dst = nullptr;
7287ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    mapper.lock(drv->wndBuffer->handle,
7297ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
7307ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams            bounds, &dst);
731709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    alloc->mHal.drvState.lod[0].mallocPtr = dst;
732709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    alloc->mHal.drvState.lod[0].stride = drv->wndBuffer->stride * alloc->mHal.state.elementSizeBytes;
73394999c3c7e3dcb6b6c0a9fe5785a0d3216357b0eStephen Hines    rsAssert((alloc->mHal.drvState.lod[0].stride & 0xf) == 0);
734b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams
7357ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    return true;
7367ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams}
73793eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
7387ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
739733396b67724162844ea2785c7495115dc5ee8d8Jason Samsvoid rsdAllocationSetSurface(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
74093eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
7417ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
7423a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray    ANativeWindow *old = drv->wndSurface;
7433a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray
7443a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray    if (nw) {
74544bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        nw->incStrong(nullptr);
7463a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray    }
7477ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
748b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
749b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        //TODO finish support for render target + script
750b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        drv->wnd = nw;
751b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        return;
752b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams    }
753b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams
7547ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    // Cleanup old surface if there is one.
7553a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray    if (drv->wndSurface) {
7563a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray        ANativeWindow *old = drv->wndSurface;
7577ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams        GraphicBufferMapper &mapper = GraphicBufferMapper::get();
7587ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams        mapper.unlock(drv->wndBuffer->handle);
7593a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray        old->cancelBuffer(old, drv->wndBuffer, -1);
76044bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        drv->wndSurface = nullptr;
7619dae48ed019a7bc4e1bd31471540cefcc667fab4Jason Sams
7629dae48ed019a7bc4e1bd31471540cefcc667fab4Jason Sams        native_window_api_disconnect(old, NATIVE_WINDOW_API_CPU);
76344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        old->decStrong(nullptr);
7647ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    }
7657ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
76644bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (nw != nullptr) {
7677ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams        int32_t r;
768b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        uint32_t flags = 0;
7693a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray
770b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
771b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams            flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
772b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        }
773b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
774b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams            flags |= GRALLOC_USAGE_HW_RENDER;
775b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        }
776b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams
7779dae48ed019a7bc4e1bd31471540cefcc667fab4Jason Sams        r = native_window_api_connect(nw, NATIVE_WINDOW_API_CPU);
7789dae48ed019a7bc4e1bd31471540cefcc667fab4Jason Sams        if (r) {
7799dae48ed019a7bc4e1bd31471540cefcc667fab4Jason Sams            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
7809dae48ed019a7bc4e1bd31471540cefcc667fab4Jason Sams            goto error;
7819dae48ed019a7bc4e1bd31471540cefcc667fab4Jason Sams        }
7829dae48ed019a7bc4e1bd31471540cefcc667fab4Jason Sams
783b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        r = native_window_set_usage(nw, flags);
7847ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams        if (r) {
7857ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
7863a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray            goto error;
7877ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams        }
7887ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
789a572aca4eb4ddb32c10baa1f529431cfefd756b8Jason Sams        r = native_window_set_buffers_dimensions(nw, alloc->mHal.drvState.lod[0].dimX,
790a572aca4eb4ddb32c10baa1f529431cfefd756b8Jason Sams                                                 alloc->mHal.drvState.lod[0].dimY);
7917ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams        if (r) {
7927ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions.");
7933a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray            goto error;
7947ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams        }
7957ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
796733396b67724162844ea2785c7495115dc5ee8d8Jason Sams        int format = 0;
797733396b67724162844ea2785c7495115dc5ee8d8Jason Sams        const Element *e = alloc->mHal.state.type->getElement();
7983b0efb602f7151a158ce37f6ba02b0e2ad885545Jason Sams        if ((e->getType() != RS_TYPE_UNSIGNED_8) ||
7993b0efb602f7151a158ce37f6ba02b0e2ad885545Jason Sams            (e->getVectorSize() != 4)) {
8003b0efb602f7151a158ce37f6ba02b0e2ad885545Jason Sams            // We do not check for RGBA, RGBx, to allow for interop with U8_4
8013b0efb602f7151a158ce37f6ba02b0e2ad885545Jason Sams
8023b0efb602f7151a158ce37f6ba02b0e2ad885545Jason Sams            rsc->setError(RS_ERROR_DRIVER, "Surface passed to setSurface is not U8_4, RGBA.");
8033b0efb602f7151a158ce37f6ba02b0e2ad885545Jason Sams            goto error;
8043b0efb602f7151a158ce37f6ba02b0e2ad885545Jason Sams        }
8058b9b3aa8e0b92e677c0100ee402a7328fc3ce1c4Stephen Hines        format = PIXEL_FORMAT_RGBA_8888;
806733396b67724162844ea2785c7495115dc5ee8d8Jason Sams
807733396b67724162844ea2785c7495115dc5ee8d8Jason Sams        r = native_window_set_buffers_format(nw, format);
808733396b67724162844ea2785c7495115dc5ee8d8Jason Sams        if (r) {
809733396b67724162844ea2785c7495115dc5ee8d8Jason Sams            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer format.");
8103a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray            goto error;
8117ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams        }
8127ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
8137ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams        IoGetBuffer(rsc, alloc, nw);
8143a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray        drv->wndSurface = nw;
8153a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray    }
8163a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray
8173a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray    return;
8183a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray
8193a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray error:
8203a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray
8213a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray    if (nw) {
82244bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        nw->decStrong(nullptr);
8237ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    }
8243a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray
8253a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray
82693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
8277ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams}
8287ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
8297ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Samsvoid rsdAllocationIoSend(const Context *rsc, Allocation *alloc) {
83093eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
8317ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
8323a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray    ANativeWindow *nw = drv->wndSurface;
833b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
834b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        RsdHal *dc = (RsdHal *)rsc->mHal.drv;
835b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
8367ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams        return;
8377ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    }
8383a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray    if (nw) {
8393a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
8403a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray            GraphicBufferMapper &mapper = GraphicBufferMapper::get();
8413a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray            mapper.unlock(drv->wndBuffer->handle);
8423a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray            int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
8433a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray            if (r) {
8443a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray                rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer.");
8453a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray                return;
8463a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray            }
8477ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
8483a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray            IoGetBuffer(rsc, alloc, nw);
849b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams        }
8503a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray    } else {
8513a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray        rsc->setError(RS_ERROR_DRIVER, "Sent IO buffer with no attached surface.");
8523a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray        return;
853b322033c13487a174bb9c26466e9684d1ff4de8dJason Sams    }
85493eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
8557ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams}
8567ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
8577ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Samsvoid rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) {
85893eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
8593522f40418fdf877f5a136475dbf75e57a3b7c77Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
860ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams    if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
8613a25fdd3786c1a08b783d8a83ef94b756347ff5cTim Murray        drv->surfaceTexture->updateTexImage();
862733396b67724162844ea2785c7495115dc5ee8d8Jason Sams    }
86393eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
8647314cca8575544195e475ecc53d995311ac269c0Jason Sams    if (alloc->mHal.state.yuv) {
8657314cca8575544195e475ecc53d995311ac269c0Jason Sams        DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState);
8667314cca8575544195e475ecc53d995311ac269c0Jason Sams    }
8677ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams}
8687ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
8697ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams
870eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Samsvoid rsdAllocationData1D(const Context *rsc, const Allocation *alloc,
871099bc262f862cdeb547cf8a78fe9e0e92560f437Tim Murray                         uint32_t xoff, uint32_t lod, size_t count,
872c794cd56e46408e08862c42a022090e323684197Alex Sakhartchouk                         const void *data, size_t sizeBytes) {
873eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
874eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
875099bc262f862cdeb547cf8a78fe9e0e92560f437Tim Murray    const size_t eSize = alloc->mHal.state.type->getElementSizeBytes();
8763bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams    uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
877099bc262f862cdeb547cf8a78fe9e0e92560f437Tim Murray    size_t size = count * eSize;
87820c535f54fb41348a96af98079ad377d7a310c8aStephen Hines    if (ptr != data) {
87920c535f54fb41348a96af98079ad377d7a310c8aStephen Hines        // Skip the copy if we are the same allocation. This can arise from
88020c535f54fb41348a96af98079ad377d7a310c8aStephen Hines        // our Bitmap optimization, where we share the same storage.
88120c535f54fb41348a96af98079ad377d7a310c8aStephen Hines        if (alloc->mHal.state.hasReferences) {
88220c535f54fb41348a96af98079ad377d7a310c8aStephen Hines            alloc->incRefs(data, count);
88320c535f54fb41348a96af98079ad377d7a310c8aStephen Hines            alloc->decRefs(ptr, count);
88420c535f54fb41348a96af98079ad377d7a310c8aStephen Hines        }
88520c535f54fb41348a96af98079ad377d7a310c8aStephen Hines        memcpy(ptr, data, size);
886eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
887eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    drv->uploadDeferred = true;
888eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams}
889eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
890eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Samsvoid rsdAllocationData2D(const Context *rsc, const Allocation *alloc,
891eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams                         uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
892358747a3118301c5faeee73c98dd5f839bbfb54aTim Murray                         uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
893eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
894eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
895099bc262f862cdeb547cf8a78fe9e0e92560f437Tim Murray    size_t eSize = alloc->mHal.state.elementSizeBytes;
896099bc262f862cdeb547cf8a78fe9e0e92560f437Tim Murray    size_t lineSize = eSize * w;
897358747a3118301c5faeee73c98dd5f839bbfb54aTim Murray    if (!stride) {
898358747a3118301c5faeee73c98dd5f839bbfb54aTim Murray        stride = lineSize;
899358747a3118301c5faeee73c98dd5f839bbfb54aTim Murray    }
900eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
901709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    if (alloc->mHal.drvState.lod[0].mallocPtr) {
902eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        const uint8_t *src = static_cast<const uint8_t *>(data);
9033bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams        uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
90420c535f54fb41348a96af98079ad377d7a310c8aStephen Hines        if (dst == src) {
90520c535f54fb41348a96af98079ad377d7a310c8aStephen Hines            // Skip the copy if we are the same allocation. This can arise from
90620c535f54fb41348a96af98079ad377d7a310c8aStephen Hines            // our Bitmap optimization, where we share the same storage.
90720c535f54fb41348a96af98079ad377d7a310c8aStephen Hines            drv->uploadDeferred = true;
90820c535f54fb41348a96af98079ad377d7a310c8aStephen Hines            return;
90920c535f54fb41348a96af98079ad377d7a310c8aStephen Hines        }
910eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
911eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        for (uint32_t line=yoff; line < (yoff+h); line++) {
912eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            if (alloc->mHal.state.hasReferences) {
913eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams                alloc->incRefs(src, w);
914eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams                alloc->decRefs(dst, w);
915eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            }
916eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams            memcpy(dst, src, lineSize);
917358747a3118301c5faeee73c98dd5f839bbfb54aTim Murray            src += stride;
918709a0978ae141198018ca9769f8d96292a8928e6Jason Sams            dst += alloc->mHal.drvState.lod[lod].stride;
919eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        }
920bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams        if (alloc->mHal.state.yuv) {
9210052f8dcb5ebb5c9205a1d25445629fb5a772380Jason Sams            size_t clineSize = lineSize;
922bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams            int lod = 1;
92361656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams            int maxLod = 2;
92461656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams            if (alloc->mHal.state.yuv == HAL_PIXEL_FORMAT_YV12) {
92561656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams                maxLod = 3;
9260052f8dcb5ebb5c9205a1d25445629fb5a772380Jason Sams                clineSize >>= 1;
92761656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams            } else if (alloc->mHal.state.yuv == HAL_PIXEL_FORMAT_YCrCb_420_SP) {
92861656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams                lod = 2;
92961656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams                maxLod = 3;
93061656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams            }
93161656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams
93261656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams            while (lod < maxLod) {
9333bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
934bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams
935bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams                for (uint32_t line=(yoff >> 1); line < ((yoff+h)>>1); line++) {
9360052f8dcb5ebb5c9205a1d25445629fb5a772380Jason Sams                    memcpy(dst, src, clineSize);
9376b855bd97cc9294e86abdb5e50733fc32cca25e4Miao Wang                    // When copying from an array to an Allocation, the src pointer
9386b855bd97cc9294e86abdb5e50733fc32cca25e4Miao Wang                    // to the array should just move by the number of bytes copied.
9396b855bd97cc9294e86abdb5e50733fc32cca25e4Miao Wang                    src += clineSize;
940bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams                    dst += alloc->mHal.drvState.lod[lod].stride;
941bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams                }
942bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams                lod++;
943bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams            }
944bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams
945bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams        }
946eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        drv->uploadDeferred = true;
947eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    } else {
9482382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h);
949eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    }
950eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams}
951eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
952eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Samsvoid rsdAllocationData3D(const Context *rsc, const Allocation *alloc,
953eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams                         uint32_t xoff, uint32_t yoff, uint32_t zoff,
9543bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                         uint32_t lod,
9553bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                         uint32_t w, uint32_t h, uint32_t d, const void *data,
9563bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                         size_t sizeBytes, size_t stride) {
9573bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
9583bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams
9593bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
9603bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams    uint32_t lineSize = eSize * w;
9613bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams    if (!stride) {
9623bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams        stride = lineSize;
9633bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams    }
9643bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams
9653bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams    if (alloc->mHal.drvState.lod[0].mallocPtr) {
9663bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams        const uint8_t *src = static_cast<const uint8_t *>(data);
967a814de97f42dc3d9d58a73b282e49dca0951fccbMiao Wang        for (uint32_t z = zoff; z < (d + zoff); z++) {
9683bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams            uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, z, lod,
9693bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                                        RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
9703bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams            if (dst == src) {
9713bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                // Skip the copy if we are the same allocation. This can arise from
9723bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                // our Bitmap optimization, where we share the same storage.
9733bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                drv->uploadDeferred = true;
9743bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                return;
9753bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams            }
976eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
9773bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams            for (uint32_t line=yoff; line < (yoff+h); line++) {
9783bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                if (alloc->mHal.state.hasReferences) {
9793bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                    alloc->incRefs(src, w);
9803bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                    alloc->decRefs(dst, w);
9813bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                }
9823bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                memcpy(dst, src, lineSize);
9833bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                src += stride;
9843bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams                dst += alloc->mHal.drvState.lod[lod].stride;
9853bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams            }
9863bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams        }
9873bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams        drv->uploadDeferred = true;
9883bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams    }
989eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams}
990eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
991807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Samsvoid rsdAllocationRead1D(const Context *rsc, const Allocation *alloc,
992099bc262f862cdeb547cf8a78fe9e0e92560f437Tim Murray                         uint32_t xoff, uint32_t lod, size_t count,
993807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams                         void *data, size_t sizeBytes) {
994099bc262f862cdeb547cf8a78fe9e0e92560f437Tim Murray    const size_t eSize = alloc->mHal.state.type->getElementSizeBytes();
9953bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams    const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
99620c535f54fb41348a96af98079ad377d7a310c8aStephen Hines    if (data != ptr) {
99720c535f54fb41348a96af98079ad377d7a310c8aStephen Hines        // Skip the copy if we are the same allocation. This can arise from
99820c535f54fb41348a96af98079ad377d7a310c8aStephen Hines        // our Bitmap optimization, where we share the same storage.
99920c535f54fb41348a96af98079ad377d7a310c8aStephen Hines        memcpy(data, ptr, count * eSize);
100020c535f54fb41348a96af98079ad377d7a310c8aStephen Hines    }
1001807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams}
1002807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams
1003807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Samsvoid rsdAllocationRead2D(const Context *rsc, const Allocation *alloc,
1004358747a3118301c5faeee73c98dd5f839bbfb54aTim Murray                                uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
1005358747a3118301c5faeee73c98dd5f839bbfb54aTim Murray                                uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
1006099bc262f862cdeb547cf8a78fe9e0e92560f437Tim Murray    size_t eSize = alloc->mHal.state.elementSizeBytes;
1007099bc262f862cdeb547cf8a78fe9e0e92560f437Tim Murray    size_t lineSize = eSize * w;
1008358747a3118301c5faeee73c98dd5f839bbfb54aTim Murray    if (!stride) {
1009358747a3118301c5faeee73c98dd5f839bbfb54aTim Murray        stride = lineSize;
1010358747a3118301c5faeee73c98dd5f839bbfb54aTim Murray    }
1011807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams
1012709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    if (alloc->mHal.drvState.lod[0].mallocPtr) {
1013807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams        uint8_t *dst = static_cast<uint8_t *>(data);
10143bbc0fd40264ddae1592706d9023865b7b3e3195</