michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkLerpXfermode.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkReadBuffer.h" michael@0: #include "SkWriteBuffer.h" michael@0: #include "SkString.h" michael@0: michael@0: SkXfermode* SkLerpXfermode::Create(SkScalar scale) { michael@0: int scale256 = SkScalarRoundToInt(scale * 256); michael@0: if (scale256 >= 256) { michael@0: return SkXfermode::Create(SkXfermode::kSrc_Mode); michael@0: } else if (scale256 <= 0) { michael@0: return SkXfermode::Create(SkXfermode::kDst_Mode); michael@0: } michael@0: return SkNEW_ARGS(SkLerpXfermode, (scale256)); michael@0: } michael@0: michael@0: SkLerpXfermode::SkLerpXfermode(unsigned scale256) : fScale256(scale256) {} michael@0: michael@0: SkLerpXfermode::SkLerpXfermode(SkReadBuffer& buffer) michael@0: : INHERITED(buffer) { michael@0: fScale256 = buffer.readUInt(); michael@0: } michael@0: michael@0: void SkLerpXfermode::flatten(SkWriteBuffer& buffer) const { michael@0: this->INHERITED::flatten(buffer); michael@0: buffer.writeUInt(fScale256); michael@0: } michael@0: michael@0: void SkLerpXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count, michael@0: const SkAlpha aa[]) const { michael@0: const int scale = fScale256; michael@0: michael@0: if (aa) { michael@0: for (int i = 0; i < count; ++i) { michael@0: unsigned a = aa[i]; michael@0: if (a) { michael@0: SkPMColor dstC = dst[i]; michael@0: SkPMColor resC = SkFastFourByteInterp256(src[i], dstC, scale); michael@0: if (a < 255) { michael@0: resC = SkFastFourByteInterp256(resC, dstC, a + (a >> 7)); michael@0: } michael@0: dst[i] = resC; michael@0: } michael@0: } michael@0: } else { michael@0: for (int i = 0; i < count; ++i) { michael@0: dst[i] = SkFastFourByteInterp256(src[i], dst[i], scale); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkLerpXfermode::xfer16(uint16_t dst[], const SkPMColor src[], int count, michael@0: const SkAlpha aa[]) const { michael@0: const int scale = fScale256; michael@0: michael@0: if (aa) { michael@0: for (int i = 0; i < count; ++i) { michael@0: unsigned a = aa[i]; michael@0: if (a) { michael@0: SkPMColor dstC = SkPixel16ToPixel32(dst[i]); michael@0: SkPMColor resC = SkFastFourByteInterp256(src[i], dstC, scale); michael@0: if (a < 255) { michael@0: resC = SkFastFourByteInterp256(resC, dstC, a + (a >> 7)); michael@0: } michael@0: dst[i] = SkPixel32ToPixel16(resC); michael@0: } michael@0: } michael@0: } else { michael@0: for (int i = 0; i < count; ++i) { michael@0: SkPMColor dstC = SkPixel16ToPixel32(dst[i]); michael@0: SkPMColor resC = SkFastFourByteInterp256(src[i], dstC, scale); michael@0: dst[i] = SkPixel32ToPixel16(resC); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkLerpXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count, michael@0: const SkAlpha aa[]) const { michael@0: const int scale = fScale256; michael@0: michael@0: if (aa) { michael@0: for (int i = 0; i < count; ++i) { michael@0: unsigned a = aa[i]; michael@0: if (a) { michael@0: unsigned dstA = dst[i]; michael@0: unsigned resA = SkAlphaBlend(SkGetPackedA32(src[i]), dstA, scale); michael@0: if (a < 255) { michael@0: resA = SkAlphaBlend(resA, dstA, a + (a >> 7)); michael@0: } michael@0: dst[i] = resA; michael@0: } michael@0: } michael@0: } else { michael@0: for (int i = 0; i < count; ++i) { michael@0: dst[i] = SkAlphaBlend(SkGetPackedA32(src[i]), dst[i], scale); michael@0: } michael@0: } michael@0: } michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkLerpXfermode::toString(SkString* str) const { michael@0: str->printf("SkLerpXfermode: scale: %g", fScale256 / 256.0); michael@0: } michael@0: #endif