/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "rsdCore.h"
#include "rsdIntrinsics.h"
#include "rsdAllocation.h"

#include "rsdIntrinsicInlines.h"

using namespace android;
using namespace android::renderscript;

struct ConvolveParams {
    float f[4];
};


enum {
    BLEND_CLEAR = 0,
    BLEND_SRC = 1,
    BLEND_DST = 2,
    BLEND_SRC_OVER = 3,
    BLEND_DST_OVER = 4,
    BLEND_SRC_IN = 5,
    BLEND_DST_IN = 6,
    BLEND_SRC_OUT = 7,
    BLEND_DST_OUT = 8,
    BLEND_SRC_ATOP = 9,
    BLEND_DST_ATOP = 10,
    BLEND_XOR = 11,

    BLEND_NORMAL = 12,
    BLEND_AVERAGE = 13,
    BLEND_MULTIPLY = 14,
    BLEND_SCREEN = 15,
    BLEND_DARKEN = 16,
    BLEND_LIGHTEN = 17,
    BLEND_OVERLAY = 18,
    BLEND_HARDLIGHT = 19,
    BLEND_SOFTLIGHT = 20,
    BLEND_DIFFERENCE = 21,
    BLEND_NEGATION = 22,
    BLEND_EXCLUSION = 23,
    BLEND_COLOR_DODGE = 24,
    BLEND_INVERSE_COLOR_DODGE = 25,
    BLEND_SOFT_DODGE = 26,
    BLEND_COLOR_BURN = 27,
    BLEND_INVERSE_COLOR_BURN = 28,
    BLEND_SOFT_BURN = 29,
    BLEND_REFLECT = 30,
    BLEND_GLOW = 31,
    BLEND_FREEZE = 32,
    BLEND_HEAT = 33,
    BLEND_ADD = 34,
    BLEND_SUBTRACT = 35,
    BLEND_STAMP = 36,
    BLEND_RED = 37,
    BLEND_GREEN = 38,
    BLEND_BLUE = 39,
    BLEND_HUE = 40,
    BLEND_SATURATION = 41,
    BLEND_COLOR = 42,
    BLEND_LUMINOSITY = 43
};

extern "C" void rsdIntrinsicBlendSrcOver_K(void *dst, const void *src, uint32_t count8);
extern "C" void rsdIntrinsicBlendDstOver_K(void *dst, const void *src, uint32_t count8);
extern "C" void rsdIntrinsicBlendSrcIn_K(void *dst, const void *src, uint32_t count8);
extern "C" void rsdIntrinsicBlendDstIn_K(void *dst, const void *src, uint32_t count8);
extern "C" void rsdIntrinsicBlendSrcOut_K(void *dst, const void *src, uint32_t count8);
extern "C" void rsdIntrinsicBlendDstOut_K(void *dst, const void *src, uint32_t count8);
extern "C" void rsdIntrinsicBlendSrcAtop_K(void *dst, const void *src, uint32_t count8);
extern "C" void rsdIntrinsicBlendDstAtop_K(void *dst, const void *src, uint32_t count8);
extern "C" void rsdIntrinsicBlendXor_K(void *dst, const void *src, uint32_t count8);
extern "C" void rsdIntrinsicBlendMultiply_K(void *dst, const void *src, uint32_t count8);
extern "C" void rsdIntrinsicBlendAdd_K(void *dst, const void *src, uint32_t count8);
extern "C" void rsdIntrinsicBlendSub_K(void *dst, const void *src, uint32_t count8);

//#undef ARCH_ARM_HAVE_NEON

static void ColorMatrix_uchar4(const RsForEachStubParamStruct *p,
                               uint32_t xstart, uint32_t xend,
                               uint32_t instep, uint32_t outstep) {
    ConvolveParams *cp = (ConvolveParams *)p->usr;

    // instep/outstep can be ignored--sizeof(uchar4) known at compile time
    uchar4 *out = (uchar4 *)p->out;
    uchar4 *in = (uchar4 *)p->in;
    uint32_t x1 = xstart;
    uint32_t x2 = xend;

    switch (p->slot) {
    case BLEND_CLEAR:
        for (;x1 < x2; x1++, out++) {
            *out = 0;
        }
        break;
    case BLEND_SRC:
        for (;x1 < x2; x1++, out++, in++) {
          *out = *in;
        }
        break;
    //BLEND_DST is a NOP
    case BLEND_DST:
        break;
    case BLEND_SRC_OVER:
#if defined(ARCH_ARM_HAVE_NEON)
        if((x1 + 8) < x2) {
            uint32_t len = (x2 - x1) >> 3;
            rsdIntrinsicBlendSrcOver_K(out, in, len);
            x1 += len << 3;
            out += len << 3;
            in += len << 3;
        }
#endif
        for (;x1 < x2; x1++, out++, in++) {
            short4 in_s = convert_short4(*in);
            short4 out_s = convert_short4(*out);
            in_s = in_s + ((out_s * (short4)(255 - in_s.a)) >> (short4)8);
            *out = convert_uchar4(in_s);
        }
        break;
    case BLEND_DST_OVER:
#if defined(ARCH_ARM_HAVE_NEON)
        if((x1 + 8) < x2) {
            uint32_t len = (x2 - x1) >> 3;
            rsdIntrinsicBlendDstOver_K(out, in, len);
            x1 += len << 3;
            out += len << 3;
            in += len << 3;
        }
#endif
        for (;x1 < x2; x1++, out++, in++) {
            short4 in_s = convert_short4(*in);
            short4 out_s = convert_short4(*out);
            in_s = out_s + ((in_s * (short4)(255 - out_s.a)) >> (short4)8);
            *out = convert_uchar4(in_s);
        }
        break;
    case BLEND_SRC_IN:
#if defined(ARCH_ARM_HAVE_NEON)
        if((x1 + 8) < x2) {
            uint32_t len = (x2 - x1) >> 3;
            rsdIntrinsicBlendSrcIn_K(out, in, len);
            x1 += len << 3;
            out += len << 3;
            in += len << 3;
        }
#endif
        for (;x1 < x2; x1++, out++, in++) {
            short4 in_s = convert_short4(*in);
            in_s = (in_s * out->a) >> (short4)8;
            *out = convert_uchar4(in_s);
        }
        break;
    case BLEND_DST_IN:
#if defined(ARCH_ARM_HAVE_NEON)
        if((x1 + 8) < x2) {
            uint32_t len = (x2 - x1) >> 3;
            rsdIntrinsicBlendDstIn_K(out, in, len);
            x1 += len << 3;
            out += len << 3;
            in += len << 3;
        }
#endif
        for (;x1 < x2; x1++, out++, in++) {
            short4 out_s = convert_short4(*out);
            out_s = (out_s * in->a) >> (short4)8;
            *out = convert_uchar4(out_s);
        }
        break;
    case BLEND_SRC_OUT:
#if defined(ARCH_ARM_HAVE_NEON)
        if((x1 + 8) < x2) {
            uint32_t len = (x2 - x1) >> 3;
            rsdIntrinsicBlendSrcOut_K(out, in, len);
            x1 += len << 3;
            out += len << 3;
            in += len << 3;
        }
#endif
        for (;x1 < x2; x1++, out++, in++) {
            short4 in_s = convert_short4(*in);
            in_s = (in_s * (short4)(255 - out->a)) >> (short4)8;
            *out = convert_uchar4(in_s);
        }
        break;
    case BLEND_DST_OUT:
#if defined(ARCH_ARM_HAVE_NEON)
        if((x1 + 8) < x2) {
            uint32_t len = (x2 - x1) >> 3;
            rsdIntrinsicBlendDstOut_K(out, in, len);
            x1 += len << 3;
            out += len << 3;
            in += len << 3;
        }
#endif
        for (;x1 < x2; x1++, out++, in++) {
            short4 out_s = convert_short4(*out);
            out_s = (out_s * (short4)(255 - in->a)) >> (short4)8;
            *out = convert_uchar4(out_s);
        }
        break;
    case BLEND_SRC_ATOP:
#if defined(ARCH_ARM_HAVE_NEON)
        if((x1 + 8) < x2) {
            uint32_t len = (x2 - x1) >> 3;
            rsdIntrinsicBlendSrcAtop_K(out, in, len);
            x1 += len << 3;
            out += len << 3;
            in += len << 3;
        }
#endif
        for (;x1 < x2; x1++, out++, in++) {
            short4 in_s = convert_short4(*in);
            short4 out_s = convert_short4(*out);
            out_s.rgb = ((in_s.rgb * out_s.a) +
              (out_s.rgb * ((short3)255 - (short3)in_s.a))) >> (short3)8;
            *out = convert_uchar4(out_s);
        }
        break;
    case BLEND_DST_ATOP:
#if defined(ARCH_ARM_HAVE_NEON)
        if((x1 + 8) < x2) {
            uint32_t len = (x2 - x1) >> 3;
            rsdIntrinsicBlendDstAtop_K(out, in, len);
            x1 += len << 3;
            out += len << 3;
            in += len << 3;
        }
#endif
        for (;x1 < x2; x1++, out++, in++) {
            short4 in_s = convert_short4(*in);
            short4 out_s = convert_short4(*out);
            out_s.rgb = ((out_s.rgb * in_s.a) +
              (in_s.rgb * ((short3)255 - (short3)out_s.a))) >> (short3)8;
            *out = convert_uchar4(out_s);
        }
        break;
    case BLEND_XOR:
#if defined(ARCH_ARM_HAVE_NEON)
        if((x1 + 8) < x2) {
            uint32_t len = (x2 - x1) >> 3;
            rsdIntrinsicBlendXor_K(out, in, len);
            x1 += len << 3;
            out += len << 3;
            in += len << 3;
        }
#endif
        for (;x1 < x2; x1++, out++, in++) {
            *out = *in ^ *out;
        }
        break;
    case BLEND_NORMAL:
        ALOGE("Called unimplemented blend intrinsic BLEND_NORMAL");
        rsAssert(false);
        break;
    case BLEND_AVERAGE:
        ALOGE("Called unimplemented blend intrinsic BLEND_AVERAGE");
        rsAssert(false);
        break;
    case BLEND_MULTIPLY:
#if defined(ARCH_ARM_HAVE_NEON)
        if((x1 + 8) < x2) {
            uint32_t len = (x2 - x1) >> 3;
            rsdIntrinsicBlendMultiply_K(out, in, len);
            x1 += len << 3;
            out += len << 3;
            in += len << 3;
        }
#endif
        for (;x1 < x2; x1++, out++, in++) {
          *out = convert_uchar4((convert_short4(*in) * convert_short4(*out))
                                >> (short4)8);
        }
        break;
    case BLEND_SCREEN:
        ALOGE("Called unimplemented blend intrinsic BLEND_SCREEN");
        rsAssert(false);
        break;
    case BLEND_DARKEN:
        ALOGE("Called unimplemented blend intrinsic BLEND_DARKEN");
        rsAssert(false);
        break;
    case BLEND_LIGHTEN:
        ALOGE("Called unimplemented blend intrinsic BLEND_LIGHTEN");
        rsAssert(false);
        break;
    case BLEND_OVERLAY:
        ALOGE("Called unimplemented blend intrinsic BLEND_OVERLAY");
        rsAssert(false);
        break;
    case BLEND_HARDLIGHT:
        ALOGE("Called unimplemented blend intrinsic BLEND_HARDLIGHT");
        rsAssert(false);
        break;
    case BLEND_SOFTLIGHT:
        ALOGE("Called unimplemented blend intrinsic BLEND_SOFTLIGHT");
        rsAssert(false);
        break;
    case BLEND_DIFFERENCE:
        ALOGE("Called unimplemented blend intrinsic BLEND_DIFFERENCE");
        rsAssert(false);
        break;
    case BLEND_NEGATION:
        ALOGE("Called unimplemented blend intrinsic BLEND_NEGATION");
        rsAssert(false);
        break;
    case BLEND_EXCLUSION:
        ALOGE("Called unimplemented blend intrinsic BLEND_EXCLUSION");
        rsAssert(false);
        break;
    case BLEND_COLOR_DODGE:
        ALOGE("Called unimplemented blend intrinsic BLEND_COLOR_DODGE");
        rsAssert(false);
        break;
    case BLEND_INVERSE_COLOR_DODGE:
        ALOGE("Called unimplemented blend intrinsic BLEND_INVERSE_COLOR_DODGE");
        rsAssert(false);
        break;
    case BLEND_SOFT_DODGE:
        ALOGE("Called unimplemented blend intrinsic BLEND_SOFT_DODGE");
        rsAssert(false);
        break;
    case BLEND_COLOR_BURN:
        ALOGE("Called unimplemented blend intrinsic BLEND_COLOR_BURN");
        rsAssert(false);
        break;
    case BLEND_INVERSE_COLOR_BURN:
        ALOGE("Called unimplemented blend intrinsic BLEND_INVERSE_COLOR_BURN");
        rsAssert(false);
        break;
    case BLEND_SOFT_BURN:
        ALOGE("Called unimplemented blend intrinsic BLEND_SOFT_BURN");
        rsAssert(false);
        break;
    case BLEND_REFLECT:
        ALOGE("Called unimplemented blend intrinsic BLEND_REFLECT");
        rsAssert(false);
        break;
    case BLEND_GLOW:
        ALOGE("Called unimplemented blend intrinsic BLEND_GLOW");
        rsAssert(false);
        break;
    case BLEND_FREEZE:
        ALOGE("Called unimplemented blend intrinsic BLEND_FREEZE");
        rsAssert(false);
        break;
    case BLEND_HEAT:
        ALOGE("Called unimplemented blend intrinsic BLEND_HEAT");
        rsAssert(false);
        break;
    case BLEND_ADD:
#if defined(ARCH_ARM_HAVE_NEON)
        if((x1 + 8) < x2) {
            uint32_t len = (x2 - x1) >> 3;
            rsdIntrinsicBlendAdd_K(out, in, len);
            x1 += len << 3;
            out += len << 3;
            in += len << 3;
        }
#endif
        for (;x1 < x2; x1++, out++, in++) {
            uint32_t iR = in->r, iG = in->g, iB = in->b, iA = in->a,
                oR = out->r, oG = out->g, oB = out->b, oA = out->a;
            out->r = (oR + iR) > 255 ? 255 : oR + iR;
            out->g = (oG + iG) > 255 ? 255 : oG + iG;
            out->b = (oB + iB) > 255 ? 255 : oB + iB;
            out->a = (oA + iA) > 255 ? 255 : oA + iA;
        }
        break;
    case BLEND_SUBTRACT:
#if defined(ARCH_ARM_HAVE_NEON)
        if((x1 + 8) < x2) {
            uint32_t len = (x2 - x1) >> 3;
            rsdIntrinsicBlendSub_K(out, in, len);
            x1 += len << 3;
            out += len << 3;
            in += len << 3;
        }
#endif
        for (;x1 < x2; x1++, out++, in++) {
            int32_t iR = in->r, iG = in->g, iB = in->b, iA = in->a,
                oR = out->r, oG = out->g, oB = out->b, oA = out->a;
            out->r = (oR - iR) < 0 ? 0 : oR - iR;
            out->g = (oG - iG) < 0 ? 0 : oG - iG;
            out->b = (oB - iB) < 0 ? 0 : oB - iB;
            out->a = (oA - iA) < 0 ? 0 : oA - iA;
        }
        break;
    case BLEND_STAMP:
        ALOGE("Called unimplemented blend intrinsic BLEND_STAMP");
        rsAssert(false);
        break;
    case BLEND_RED:
        ALOGE("Called unimplemented blend intrinsic BLEND_RED");
        rsAssert(false);
        break;
    case BLEND_GREEN:
        ALOGE("Called unimplemented blend intrinsic BLEND_GREEN");
        rsAssert(false);
        break;
    case BLEND_BLUE:
        ALOGE("Called unimplemented blend intrinsic BLEND_BLUE");
        rsAssert(false);
        break;
    case BLEND_HUE:
        ALOGE("Called unimplemented blend intrinsic BLEND_HUE");
        rsAssert(false);
        break;
    case BLEND_SATURATION:
        ALOGE("Called unimplemented blend intrinsic BLEND_SATURATION");
        rsAssert(false);
        break;
    case BLEND_COLOR:
        ALOGE("Called unimplemented blend intrinsic BLEND_COLOR");
        rsAssert(false);
        break;
    case BLEND_LUMINOSITY:
        ALOGE("Called unimplemented blend intrinsic BLEND_LUMINOSITY");
        rsAssert(false);
        break;

    default:
        ALOGE("Called unimplemented value %d", p->slot);
        rsAssert(false);

    }
}

void * rsdIntrinsic_InitBlend(const android::renderscript::Context *dc,
                              android::renderscript::Script *script,
                              RsdIntriniscFuncs_t *funcs) {

    script->mHal.info.exportedVariableCount = 0;
    funcs->root = ColorMatrix_uchar4;

    ConvolveParams *cp = (ConvolveParams *)calloc(1, sizeof(ConvolveParams));
    return cp;
}