gfx/skia/trunk/src/effects/SkLerpXfermode.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/effects/SkLerpXfermode.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,111 @@
     1.4 +/*
     1.5 + * Copyright 2013 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#include "SkLerpXfermode.h"
    1.12 +#include "SkColorPriv.h"
    1.13 +#include "SkReadBuffer.h"
    1.14 +#include "SkWriteBuffer.h"
    1.15 +#include "SkString.h"
    1.16 +
    1.17 +SkXfermode* SkLerpXfermode::Create(SkScalar scale) {
    1.18 +    int scale256 = SkScalarRoundToInt(scale * 256);
    1.19 +    if (scale256 >= 256) {
    1.20 +        return SkXfermode::Create(SkXfermode::kSrc_Mode);
    1.21 +    } else if (scale256 <= 0) {
    1.22 +        return SkXfermode::Create(SkXfermode::kDst_Mode);
    1.23 +    }
    1.24 +    return SkNEW_ARGS(SkLerpXfermode, (scale256));
    1.25 +}
    1.26 +
    1.27 +SkLerpXfermode::SkLerpXfermode(unsigned scale256) : fScale256(scale256) {}
    1.28 +
    1.29 +SkLerpXfermode::SkLerpXfermode(SkReadBuffer& buffer)
    1.30 +    : INHERITED(buffer) {
    1.31 +    fScale256 = buffer.readUInt();
    1.32 +}
    1.33 +
    1.34 +void SkLerpXfermode::flatten(SkWriteBuffer& buffer) const {
    1.35 +    this->INHERITED::flatten(buffer);
    1.36 +    buffer.writeUInt(fScale256);
    1.37 +}
    1.38 +
    1.39 +void SkLerpXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count,
    1.40 +                             const SkAlpha aa[]) const {
    1.41 +    const int scale = fScale256;
    1.42 +
    1.43 +    if (aa) {
    1.44 +        for (int i = 0; i < count; ++i) {
    1.45 +            unsigned a = aa[i];
    1.46 +            if (a) {
    1.47 +                SkPMColor dstC = dst[i];
    1.48 +                SkPMColor resC = SkFastFourByteInterp256(src[i], dstC, scale);
    1.49 +                if (a < 255) {
    1.50 +                    resC = SkFastFourByteInterp256(resC, dstC, a + (a >> 7));
    1.51 +                }
    1.52 +                dst[i] = resC;
    1.53 +            }
    1.54 +        }
    1.55 +    } else {
    1.56 +        for (int i = 0; i < count; ++i) {
    1.57 +            dst[i] = SkFastFourByteInterp256(src[i], dst[i], scale);
    1.58 +        }
    1.59 +    }
    1.60 +}
    1.61 +
    1.62 +void SkLerpXfermode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
    1.63 +                             const SkAlpha aa[]) const {
    1.64 +    const int scale = fScale256;
    1.65 +
    1.66 +    if (aa) {
    1.67 +        for (int i = 0; i < count; ++i) {
    1.68 +            unsigned a = aa[i];
    1.69 +            if (a) {
    1.70 +                SkPMColor dstC = SkPixel16ToPixel32(dst[i]);
    1.71 +                SkPMColor resC = SkFastFourByteInterp256(src[i], dstC, scale);
    1.72 +                if (a < 255) {
    1.73 +                    resC = SkFastFourByteInterp256(resC, dstC, a + (a >> 7));
    1.74 +                }
    1.75 +                dst[i] = SkPixel32ToPixel16(resC);
    1.76 +            }
    1.77 +        }
    1.78 +    } else {
    1.79 +        for (int i = 0; i < count; ++i) {
    1.80 +            SkPMColor dstC = SkPixel16ToPixel32(dst[i]);
    1.81 +            SkPMColor resC = SkFastFourByteInterp256(src[i], dstC, scale);
    1.82 +            dst[i] = SkPixel32ToPixel16(resC);
    1.83 +        }
    1.84 +    }
    1.85 +}
    1.86 +
    1.87 +void SkLerpXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count,
    1.88 +                             const SkAlpha aa[]) const {
    1.89 +    const int scale = fScale256;
    1.90 +
    1.91 +    if (aa) {
    1.92 +        for (int i = 0; i < count; ++i) {
    1.93 +            unsigned a = aa[i];
    1.94 +            if (a) {
    1.95 +                unsigned dstA = dst[i];
    1.96 +                unsigned resA = SkAlphaBlend(SkGetPackedA32(src[i]), dstA, scale);
    1.97 +                if (a < 255) {
    1.98 +                    resA = SkAlphaBlend(resA, dstA, a + (a >> 7));
    1.99 +                }
   1.100 +                dst[i] = resA;
   1.101 +            }
   1.102 +        }
   1.103 +    } else {
   1.104 +        for (int i = 0; i < count; ++i) {
   1.105 +            dst[i] = SkAlphaBlend(SkGetPackedA32(src[i]), dst[i], scale);
   1.106 +        }
   1.107 +    }
   1.108 +}
   1.109 +
   1.110 +#ifndef SK_IGNORE_TO_STRING
   1.111 +void SkLerpXfermode::toString(SkString* str) const {
   1.112 +    str->printf("SkLerpXfermode: scale: %g", fScale256 / 256.0);
   1.113 +}
   1.114 +#endif

mercurial