148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams/*
248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams * Copyright (C) 2011 The Android Open Source Project
348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams *
448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams * you may not use this file except in compliance with the License.
648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams * You may obtain a copy of the License at
748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams *
848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams *
1048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams * Unless required by applicable law or agreed to in writing, software
1148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
1248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams * See the License for the specific language governing permissions and
1448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams * limitations under the License.
1548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams */
1648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
1748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
1848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams#include "rsdCore.h"
1948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams#include "rsdProgramStore.h"
2048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
2148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams#include "rsContext.h"
2248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams#include "rsProgramStore.h"
2348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
2448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams#include <GLES/gl.h>
2548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams#include <GLES/glext.h>
2648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
2748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
2848f505657adba4d9156856e7d5593f23af5d5d5aJason Samsusing namespace android;
2948f505657adba4d9156856e7d5593f23af5d5d5aJason Samsusing namespace android::renderscript;
3048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
3148f505657adba4d9156856e7d5593f23af5d5d5aJason Samsstruct DrvProgramStore {
3248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    GLenum blendSrc;
3348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    GLenum blendDst;
3448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    bool blendEnable;
3548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
3648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    GLenum depthFunc;
3748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    bool depthTestEnable;
3848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams};
3948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
4048f505657adba4d9156856e7d5593f23af5d5d5aJason Samsbool rsdProgramStoreInit(const Context *rsc, const ProgramStore *ps) {
4148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    DrvProgramStore *drv = (DrvProgramStore *)calloc(1, sizeof(DrvProgramStore));
4248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    if (drv == NULL) {
4348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        return false;
4448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    }
4548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
4648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    ps->mHal.drv = drv;
4748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    drv->depthTestEnable = true;
4848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
4948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    switch (ps->mHal.state.depthFunc) {
5048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_DEPTH_FUNC_ALWAYS:
5148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->depthTestEnable = false;
5248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->depthFunc = GL_ALWAYS;
5348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
5448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_DEPTH_FUNC_LESS:
5548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->depthFunc = GL_LESS;
5648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
5748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_DEPTH_FUNC_LEQUAL:
5848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->depthFunc = GL_LEQUAL;
5948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
6048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_DEPTH_FUNC_GREATER:
6148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->depthFunc = GL_GREATER;
6248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
6348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_DEPTH_FUNC_GEQUAL:
6448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->depthFunc = GL_GEQUAL;
6548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
6648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_DEPTH_FUNC_EQUAL:
6748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->depthFunc = GL_EQUAL;
6848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
6948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_DEPTH_FUNC_NOTEQUAL:
7048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->depthFunc = GL_NOTEQUAL;
7148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
7248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    default:
7348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        LOGE("Unknown depth function.");
7448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        goto error;
7548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    }
7648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
7748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
7848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
7948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    drv->blendEnable = true;
8048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    if ((ps->mHal.state.blendSrc == RS_BLEND_SRC_ONE) &&
8148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        (ps->mHal.state.blendDst == RS_BLEND_DST_ZERO)) {
8248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendEnable = false;
8348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    }
8448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
8548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    switch (ps->mHal.state.blendSrc) {
8648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_SRC_ZERO:
8748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendSrc = GL_ZERO;
8848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
8948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_SRC_ONE:
9048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendSrc = GL_ONE;
9148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
9248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_SRC_DST_COLOR:
9348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendSrc = GL_DST_COLOR;
9448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
9548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_SRC_ONE_MINUS_DST_COLOR:
9648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendSrc = GL_ONE_MINUS_DST_COLOR;
9748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
9848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_SRC_SRC_ALPHA:
9948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendSrc = GL_SRC_ALPHA;
10048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
10148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA:
10248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendSrc = GL_ONE_MINUS_SRC_ALPHA;
10348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
10448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_SRC_DST_ALPHA:
10548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendSrc = GL_DST_ALPHA;
10648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
10748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_SRC_ONE_MINUS_DST_ALPHA:
10848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendSrc = GL_ONE_MINUS_DST_ALPHA;
10948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
11048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_SRC_SRC_ALPHA_SATURATE:
11148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendSrc = GL_SRC_ALPHA_SATURATE;
11248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
11348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    default:
11448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        LOGE("Unknown blend src mode.");
11548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        goto error;
11648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    }
11748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
11848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    switch (ps->mHal.state.blendDst) {
11948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_DST_ZERO:
12048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendDst = GL_ZERO;
12148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
12248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_DST_ONE:
12348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendDst = GL_ONE;
12448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
12548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_DST_SRC_COLOR:
12648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendDst = GL_SRC_COLOR;
12748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
12848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_DST_ONE_MINUS_SRC_COLOR:
12948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendDst = GL_ONE_MINUS_SRC_COLOR;
13048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
13148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_DST_SRC_ALPHA:
13248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendDst = GL_SRC_ALPHA;
13348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
13448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_DST_ONE_MINUS_SRC_ALPHA:
13548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendDst = GL_ONE_MINUS_SRC_ALPHA;
13648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
13748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_DST_DST_ALPHA:
13848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendDst = GL_DST_ALPHA;
13948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
14048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    case RS_BLEND_DST_ONE_MINUS_DST_ALPHA:
14148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        drv->blendDst = GL_ONE_MINUS_DST_ALPHA;
14248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        break;
14348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    default:
14448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        LOGE("Unknown blend dst mode.");
14548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        goto error;
14648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    }
14748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
14848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    return true;
14948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
15048f505657adba4d9156856e7d5593f23af5d5d5aJason Samserror:
15148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    free(drv);
15248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    ps->mHal.drv = NULL;
15348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    return false;
15448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams}
15548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
15648f505657adba4d9156856e7d5593f23af5d5d5aJason Samsvoid rsdProgramStoreSetActive(const Context *rsc, const ProgramStore *ps) {
15748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    DrvProgramStore *drv = (DrvProgramStore *)ps->mHal.drv;
15848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
1595316b9eead0c29df36f46d29c17914bc3c61ed9fJason Sams    RSD_CALL_GL(glColorMask, ps->mHal.state.colorRWriteEnable,
16048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams                ps->mHal.state.colorGWriteEnable,
16148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams                ps->mHal.state.colorBWriteEnable,
16248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams                ps->mHal.state.colorAWriteEnable);
16348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
16448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    if (drv->blendEnable) {
1655316b9eead0c29df36f46d29c17914bc3c61ed9fJason Sams        RSD_CALL_GL(glEnable, GL_BLEND);
1665316b9eead0c29df36f46d29c17914bc3c61ed9fJason Sams        RSD_CALL_GL(glBlendFunc, drv->blendSrc, drv->blendDst);
16748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    } else {
1685316b9eead0c29df36f46d29c17914bc3c61ed9fJason Sams        RSD_CALL_GL(glDisable, GL_BLEND);
16948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    }
17048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
17148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    if (rsc->mUserSurfaceConfig.depthMin > 0) {
1725316b9eead0c29df36f46d29c17914bc3c61ed9fJason Sams        RSD_CALL_GL(glDepthMask, ps->mHal.state.depthWriteEnable);
17348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        if (drv->depthTestEnable || ps->mHal.state.depthWriteEnable) {
1745316b9eead0c29df36f46d29c17914bc3c61ed9fJason Sams            RSD_CALL_GL(glEnable, GL_DEPTH_TEST);
1755316b9eead0c29df36f46d29c17914bc3c61ed9fJason Sams            RSD_CALL_GL(glDepthFunc, drv->depthFunc);
17648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        } else {
1775316b9eead0c29df36f46d29c17914bc3c61ed9fJason Sams            RSD_CALL_GL(glDisable, GL_DEPTH_TEST);
17848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        }
17948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    } else {
1805316b9eead0c29df36f46d29c17914bc3c61ed9fJason Sams        RSD_CALL_GL(glDepthMask, false);
1815316b9eead0c29df36f46d29c17914bc3c61ed9fJason Sams        RSD_CALL_GL(glDisable, GL_DEPTH_TEST);
18248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    }
18348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
18448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    /*
18548f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    if (rsc->mUserSurfaceConfig.stencilMin > 0) {
18648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    } else {
18748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        glStencilMask(0);
18848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams        glDisable(GL_STENCIL_TEST);
18948f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    }
19048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    */
19148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
19248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    if (ps->mHal.state.ditherEnable) {
1935316b9eead0c29df36f46d29c17914bc3c61ed9fJason Sams        RSD_CALL_GL(glEnable, GL_DITHER);
19448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    } else {
1955316b9eead0c29df36f46d29c17914bc3c61ed9fJason Sams        RSD_CALL_GL(glDisable, GL_DITHER);
19648f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    }
19748f505657adba4d9156856e7d5593f23af5d5d5aJason Sams}
19848f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
19948f505657adba4d9156856e7d5593f23af5d5d5aJason Samsvoid rsdProgramStoreDestroy(const Context *rsc, const ProgramStore *ps) {
20048f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    free(ps->mHal.drv);
20148f505657adba4d9156856e7d5593f23af5d5d5aJason Sams    ps->mHal.drv = NULL;
20248f505657adba4d9156856e7d5593f23af5d5d5aJason Sams}
20348f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
20448f505657adba4d9156856e7d5593f23af5d5d5aJason Sams
205