michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project 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: michael@0: #include "SkXfermode.h" michael@0: #include "SkXfermode_proccoeff.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkReadBuffer.h" michael@0: #include "SkWriteBuffer.h" michael@0: #include "SkMathPriv.h" michael@0: #include "SkString.h" michael@0: #include "SkUtilsArm.h" michael@0: michael@0: #if !SK_ARM_NEON_IS_NONE michael@0: #include "SkXfermode_opts_arm_neon.h" michael@0: #endif michael@0: michael@0: #define SkAlphaMulAlpha(a, b) SkMulDiv255Round(a, b) michael@0: michael@0: #if 0 michael@0: // idea for higher precision blends in xfer procs (and slightly faster) michael@0: // see DstATop as a probable caller michael@0: static U8CPU mulmuldiv255round(U8CPU a, U8CPU b, U8CPU c, U8CPU d) { michael@0: SkASSERT(a <= 255); michael@0: SkASSERT(b <= 255); michael@0: SkASSERT(c <= 255); michael@0: SkASSERT(d <= 255); michael@0: unsigned prod = SkMulS16(a, b) + SkMulS16(c, d) + 128; michael@0: unsigned result = (prod + (prod >> 8)) >> 8; michael@0: SkASSERT(result <= 255); michael@0: return result; michael@0: } michael@0: #endif michael@0: michael@0: static inline unsigned saturated_add(unsigned a, unsigned b) { michael@0: SkASSERT(a <= 255); michael@0: SkASSERT(b <= 255); michael@0: unsigned sum = a + b; michael@0: if (sum > 255) { michael@0: sum = 255; michael@0: } michael@0: return sum; michael@0: } michael@0: michael@0: static inline int clamp_signed_byte(int n) { michael@0: if (n < 0) { michael@0: n = 0; michael@0: } else if (n > 255) { michael@0: n = 255; michael@0: } michael@0: return n; michael@0: } michael@0: michael@0: static inline int clamp_div255round(int prod) { michael@0: if (prod <= 0) { michael@0: return 0; michael@0: } else if (prod >= 255*255) { michael@0: return 255; michael@0: } else { michael@0: return SkDiv255Round(prod); michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // kClear_Mode, //!< [0, 0] michael@0: static SkPMColor clear_modeproc(SkPMColor src, SkPMColor dst) { michael@0: return 0; michael@0: } michael@0: michael@0: // kSrc_Mode, //!< [Sa, Sc] michael@0: static SkPMColor src_modeproc(SkPMColor src, SkPMColor dst) { michael@0: return src; michael@0: } michael@0: michael@0: // kDst_Mode, //!< [Da, Dc] michael@0: static SkPMColor dst_modeproc(SkPMColor src, SkPMColor dst) { michael@0: return dst; michael@0: } michael@0: michael@0: // kSrcOver_Mode, //!< [Sa + Da - Sa*Da, Sc + (1 - Sa)*Dc] michael@0: static SkPMColor srcover_modeproc(SkPMColor src, SkPMColor dst) { michael@0: #if 0 michael@0: // this is the old, more-correct way, but it doesn't guarantee that dst==255 michael@0: // will always stay opaque michael@0: return src + SkAlphaMulQ(dst, SkAlpha255To256(255 - SkGetPackedA32(src))); michael@0: #else michael@0: // this is slightly faster, but more importantly guarantees that dst==255 michael@0: // will always stay opaque michael@0: return src + SkAlphaMulQ(dst, 256 - SkGetPackedA32(src)); michael@0: #endif michael@0: } michael@0: michael@0: // kDstOver_Mode, //!< [Sa + Da - Sa*Da, Dc + (1 - Da)*Sc] michael@0: static SkPMColor dstover_modeproc(SkPMColor src, SkPMColor dst) { michael@0: // this is the reverse of srcover, just flipping src and dst michael@0: // see srcover's comment about the 256 for opaqueness guarantees michael@0: return dst + SkAlphaMulQ(src, 256 - SkGetPackedA32(dst)); michael@0: } michael@0: michael@0: // kSrcIn_Mode, //!< [Sa * Da, Sc * Da] michael@0: static SkPMColor srcin_modeproc(SkPMColor src, SkPMColor dst) { michael@0: return SkAlphaMulQ(src, SkAlpha255To256(SkGetPackedA32(dst))); michael@0: } michael@0: michael@0: // kDstIn_Mode, //!< [Sa * Da, Sa * Dc] michael@0: static SkPMColor dstin_modeproc(SkPMColor src, SkPMColor dst) { michael@0: return SkAlphaMulQ(dst, SkAlpha255To256(SkGetPackedA32(src))); michael@0: } michael@0: michael@0: // kSrcOut_Mode, //!< [Sa * (1 - Da), Sc * (1 - Da)] michael@0: static SkPMColor srcout_modeproc(SkPMColor src, SkPMColor dst) { michael@0: return SkAlphaMulQ(src, SkAlpha255To256(255 - SkGetPackedA32(dst))); michael@0: } michael@0: michael@0: // kDstOut_Mode, //!< [Da * (1 - Sa), Dc * (1 - Sa)] michael@0: static SkPMColor dstout_modeproc(SkPMColor src, SkPMColor dst) { michael@0: return SkAlphaMulQ(dst, SkAlpha255To256(255 - SkGetPackedA32(src))); michael@0: } michael@0: michael@0: // kSrcATop_Mode, //!< [Da, Sc * Da + (1 - Sa) * Dc] michael@0: static SkPMColor srcatop_modeproc(SkPMColor src, SkPMColor dst) { michael@0: unsigned sa = SkGetPackedA32(src); michael@0: unsigned da = SkGetPackedA32(dst); michael@0: unsigned isa = 255 - sa; michael@0: michael@0: return SkPackARGB32(da, michael@0: SkAlphaMulAlpha(da, SkGetPackedR32(src)) + michael@0: SkAlphaMulAlpha(isa, SkGetPackedR32(dst)), michael@0: SkAlphaMulAlpha(da, SkGetPackedG32(src)) + michael@0: SkAlphaMulAlpha(isa, SkGetPackedG32(dst)), michael@0: SkAlphaMulAlpha(da, SkGetPackedB32(src)) + michael@0: SkAlphaMulAlpha(isa, SkGetPackedB32(dst))); michael@0: } michael@0: michael@0: // kDstATop_Mode, //!< [Sa, Sa * Dc + Sc * (1 - Da)] michael@0: static SkPMColor dstatop_modeproc(SkPMColor src, SkPMColor dst) { michael@0: unsigned sa = SkGetPackedA32(src); michael@0: unsigned da = SkGetPackedA32(dst); michael@0: unsigned ida = 255 - da; michael@0: michael@0: return SkPackARGB32(sa, michael@0: SkAlphaMulAlpha(ida, SkGetPackedR32(src)) + michael@0: SkAlphaMulAlpha(sa, SkGetPackedR32(dst)), michael@0: SkAlphaMulAlpha(ida, SkGetPackedG32(src)) + michael@0: SkAlphaMulAlpha(sa, SkGetPackedG32(dst)), michael@0: SkAlphaMulAlpha(ida, SkGetPackedB32(src)) + michael@0: SkAlphaMulAlpha(sa, SkGetPackedB32(dst))); michael@0: } michael@0: michael@0: // kXor_Mode [Sa + Da - 2 * Sa * Da, Sc * (1 - Da) + (1 - Sa) * Dc] michael@0: static SkPMColor xor_modeproc(SkPMColor src, SkPMColor dst) { michael@0: unsigned sa = SkGetPackedA32(src); michael@0: unsigned da = SkGetPackedA32(dst); michael@0: unsigned isa = 255 - sa; michael@0: unsigned ida = 255 - da; michael@0: michael@0: return SkPackARGB32(sa + da - (SkAlphaMulAlpha(sa, da) << 1), michael@0: SkAlphaMulAlpha(ida, SkGetPackedR32(src)) + michael@0: SkAlphaMulAlpha(isa, SkGetPackedR32(dst)), michael@0: SkAlphaMulAlpha(ida, SkGetPackedG32(src)) + michael@0: SkAlphaMulAlpha(isa, SkGetPackedG32(dst)), michael@0: SkAlphaMulAlpha(ida, SkGetPackedB32(src)) + michael@0: SkAlphaMulAlpha(isa, SkGetPackedB32(dst))); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // kPlus_Mode michael@0: static SkPMColor plus_modeproc(SkPMColor src, SkPMColor dst) { michael@0: unsigned b = saturated_add(SkGetPackedB32(src), SkGetPackedB32(dst)); michael@0: unsigned g = saturated_add(SkGetPackedG32(src), SkGetPackedG32(dst)); michael@0: unsigned r = saturated_add(SkGetPackedR32(src), SkGetPackedR32(dst)); michael@0: unsigned a = saturated_add(SkGetPackedA32(src), SkGetPackedA32(dst)); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // kModulate_Mode michael@0: static SkPMColor modulate_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int a = SkAlphaMulAlpha(SkGetPackedA32(src), SkGetPackedA32(dst)); michael@0: int r = SkAlphaMulAlpha(SkGetPackedR32(src), SkGetPackedR32(dst)); michael@0: int g = SkAlphaMulAlpha(SkGetPackedG32(src), SkGetPackedG32(dst)); michael@0: int b = SkAlphaMulAlpha(SkGetPackedB32(src), SkGetPackedB32(dst)); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: static inline int srcover_byte(int a, int b) { michael@0: return a + b - SkAlphaMulAlpha(a, b); michael@0: } michael@0: michael@0: // kMultiply_Mode michael@0: // B(Cb, Cs) = Cb x Cs michael@0: // multiply uses its own version of blendfunc_byte because sa and da are not needed michael@0: static int blendfunc_multiply_byte(int sc, int dc, int sa, int da) { michael@0: return clamp_div255round(sc * (255 - da) + dc * (255 - sa) + sc * dc); michael@0: } michael@0: michael@0: static SkPMColor multiply_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sa = SkGetPackedA32(src); michael@0: int da = SkGetPackedA32(dst); michael@0: int a = srcover_byte(sa, da); michael@0: int r = blendfunc_multiply_byte(SkGetPackedR32(src), SkGetPackedR32(dst), sa, da); michael@0: int g = blendfunc_multiply_byte(SkGetPackedG32(src), SkGetPackedG32(dst), sa, da); michael@0: int b = blendfunc_multiply_byte(SkGetPackedB32(src), SkGetPackedB32(dst), sa, da); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // kScreen_Mode michael@0: static SkPMColor screen_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int a = srcover_byte(SkGetPackedA32(src), SkGetPackedA32(dst)); michael@0: int r = srcover_byte(SkGetPackedR32(src), SkGetPackedR32(dst)); michael@0: int g = srcover_byte(SkGetPackedG32(src), SkGetPackedG32(dst)); michael@0: int b = srcover_byte(SkGetPackedB32(src), SkGetPackedB32(dst)); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // kOverlay_Mode michael@0: static inline int overlay_byte(int sc, int dc, int sa, int da) { michael@0: int tmp = sc * (255 - da) + dc * (255 - sa); michael@0: int rc; michael@0: if (2 * dc <= da) { michael@0: rc = 2 * sc * dc; michael@0: } else { michael@0: rc = sa * da - 2 * (da - dc) * (sa - sc); michael@0: } michael@0: return clamp_div255round(rc + tmp); michael@0: } michael@0: static SkPMColor overlay_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sa = SkGetPackedA32(src); michael@0: int da = SkGetPackedA32(dst); michael@0: int a = srcover_byte(sa, da); michael@0: int r = overlay_byte(SkGetPackedR32(src), SkGetPackedR32(dst), sa, da); michael@0: int g = overlay_byte(SkGetPackedG32(src), SkGetPackedG32(dst), sa, da); michael@0: int b = overlay_byte(SkGetPackedB32(src), SkGetPackedB32(dst), sa, da); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // kDarken_Mode michael@0: static inline int darken_byte(int sc, int dc, int sa, int da) { michael@0: int sd = sc * da; michael@0: int ds = dc * sa; michael@0: if (sd < ds) { michael@0: // srcover michael@0: return sc + dc - SkDiv255Round(ds); michael@0: } else { michael@0: // dstover michael@0: return dc + sc - SkDiv255Round(sd); michael@0: } michael@0: } michael@0: static SkPMColor darken_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sa = SkGetPackedA32(src); michael@0: int da = SkGetPackedA32(dst); michael@0: int a = srcover_byte(sa, da); michael@0: int r = darken_byte(SkGetPackedR32(src), SkGetPackedR32(dst), sa, da); michael@0: int g = darken_byte(SkGetPackedG32(src), SkGetPackedG32(dst), sa, da); michael@0: int b = darken_byte(SkGetPackedB32(src), SkGetPackedB32(dst), sa, da); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // kLighten_Mode michael@0: static inline int lighten_byte(int sc, int dc, int sa, int da) { michael@0: int sd = sc * da; michael@0: int ds = dc * sa; michael@0: if (sd > ds) { michael@0: // srcover michael@0: return sc + dc - SkDiv255Round(ds); michael@0: } else { michael@0: // dstover michael@0: return dc + sc - SkDiv255Round(sd); michael@0: } michael@0: } michael@0: static SkPMColor lighten_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sa = SkGetPackedA32(src); michael@0: int da = SkGetPackedA32(dst); michael@0: int a = srcover_byte(sa, da); michael@0: int r = lighten_byte(SkGetPackedR32(src), SkGetPackedR32(dst), sa, da); michael@0: int g = lighten_byte(SkGetPackedG32(src), SkGetPackedG32(dst), sa, da); michael@0: int b = lighten_byte(SkGetPackedB32(src), SkGetPackedB32(dst), sa, da); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // kColorDodge_Mode michael@0: static inline int colordodge_byte(int sc, int dc, int sa, int da) { michael@0: int diff = sa - sc; michael@0: int rc; michael@0: if (0 == dc) { michael@0: return SkAlphaMulAlpha(sc, 255 - da); michael@0: } else if (0 == diff) { michael@0: rc = sa * da + sc * (255 - da) + dc * (255 - sa); michael@0: } else { michael@0: diff = dc * sa / diff; michael@0: rc = sa * ((da < diff) ? da : diff) + sc * (255 - da) + dc * (255 - sa); michael@0: } michael@0: return clamp_div255round(rc); michael@0: } michael@0: static SkPMColor colordodge_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sa = SkGetPackedA32(src); michael@0: int da = SkGetPackedA32(dst); michael@0: int a = srcover_byte(sa, da); michael@0: int r = colordodge_byte(SkGetPackedR32(src), SkGetPackedR32(dst), sa, da); michael@0: int g = colordodge_byte(SkGetPackedG32(src), SkGetPackedG32(dst), sa, da); michael@0: int b = colordodge_byte(SkGetPackedB32(src), SkGetPackedB32(dst), sa, da); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // kColorBurn_Mode michael@0: static inline int colorburn_byte(int sc, int dc, int sa, int da) { michael@0: int rc; michael@0: if (dc == da) { michael@0: rc = sa * da + sc * (255 - da) + dc * (255 - sa); michael@0: } else if (0 == sc) { michael@0: return SkAlphaMulAlpha(dc, 255 - sa); michael@0: } else { michael@0: int tmp = (da - dc) * sa / sc; michael@0: rc = sa * (da - ((da < tmp) ? da : tmp)) michael@0: + sc * (255 - da) + dc * (255 - sa); michael@0: } michael@0: return clamp_div255round(rc); michael@0: } michael@0: static SkPMColor colorburn_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sa = SkGetPackedA32(src); michael@0: int da = SkGetPackedA32(dst); michael@0: int a = srcover_byte(sa, da); michael@0: int r = colorburn_byte(SkGetPackedR32(src), SkGetPackedR32(dst), sa, da); michael@0: int g = colorburn_byte(SkGetPackedG32(src), SkGetPackedG32(dst), sa, da); michael@0: int b = colorburn_byte(SkGetPackedB32(src), SkGetPackedB32(dst), sa, da); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // kHardLight_Mode michael@0: static inline int hardlight_byte(int sc, int dc, int sa, int da) { michael@0: int rc; michael@0: if (2 * sc <= sa) { michael@0: rc = 2 * sc * dc; michael@0: } else { michael@0: rc = sa * da - 2 * (da - dc) * (sa - sc); michael@0: } michael@0: return clamp_div255round(rc + sc * (255 - da) + dc * (255 - sa)); michael@0: } michael@0: static SkPMColor hardlight_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sa = SkGetPackedA32(src); michael@0: int da = SkGetPackedA32(dst); michael@0: int a = srcover_byte(sa, da); michael@0: int r = hardlight_byte(SkGetPackedR32(src), SkGetPackedR32(dst), sa, da); michael@0: int g = hardlight_byte(SkGetPackedG32(src), SkGetPackedG32(dst), sa, da); michael@0: int b = hardlight_byte(SkGetPackedB32(src), SkGetPackedB32(dst), sa, da); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // returns 255 * sqrt(n/255) michael@0: static U8CPU sqrt_unit_byte(U8CPU n) { michael@0: return SkSqrtBits(n, 15+4); michael@0: } michael@0: michael@0: // kSoftLight_Mode michael@0: static inline int softlight_byte(int sc, int dc, int sa, int da) { michael@0: int m = da ? dc * 256 / da : 0; michael@0: int rc; michael@0: if (2 * sc <= sa) { michael@0: rc = dc * (sa + ((2 * sc - sa) * (256 - m) >> 8)); michael@0: } else if (4 * dc <= da) { michael@0: int tmp = (4 * m * (4 * m + 256) * (m - 256) >> 16) + 7 * m; michael@0: rc = dc * sa + (da * (2 * sc - sa) * tmp >> 8); michael@0: } else { michael@0: int tmp = sqrt_unit_byte(m) - m; michael@0: rc = dc * sa + (da * (2 * sc - sa) * tmp >> 8); michael@0: } michael@0: return clamp_div255round(rc + sc * (255 - da) + dc * (255 - sa)); michael@0: } michael@0: static SkPMColor softlight_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sa = SkGetPackedA32(src); michael@0: int da = SkGetPackedA32(dst); michael@0: int a = srcover_byte(sa, da); michael@0: int r = softlight_byte(SkGetPackedR32(src), SkGetPackedR32(dst), sa, da); michael@0: int g = softlight_byte(SkGetPackedG32(src), SkGetPackedG32(dst), sa, da); michael@0: int b = softlight_byte(SkGetPackedB32(src), SkGetPackedB32(dst), sa, da); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // kDifference_Mode michael@0: static inline int difference_byte(int sc, int dc, int sa, int da) { michael@0: int tmp = SkMin32(sc * da, dc * sa); michael@0: return clamp_signed_byte(sc + dc - 2 * SkDiv255Round(tmp)); michael@0: } michael@0: static SkPMColor difference_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sa = SkGetPackedA32(src); michael@0: int da = SkGetPackedA32(dst); michael@0: int a = srcover_byte(sa, da); michael@0: int r = difference_byte(SkGetPackedR32(src), SkGetPackedR32(dst), sa, da); michael@0: int g = difference_byte(SkGetPackedG32(src), SkGetPackedG32(dst), sa, da); michael@0: int b = difference_byte(SkGetPackedB32(src), SkGetPackedB32(dst), sa, da); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // kExclusion_Mode michael@0: static inline int exclusion_byte(int sc, int dc, int, int) { michael@0: // this equations is wacky, wait for SVG to confirm it michael@0: //int r = sc * da + dc * sa - 2 * sc * dc + sc * (255 - da) + dc * (255 - sa); michael@0: michael@0: // The above equation can be simplified as follows michael@0: int r = 255*(sc + dc) - 2 * sc * dc; michael@0: return clamp_div255round(r); michael@0: } michael@0: static SkPMColor exclusion_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sa = SkGetPackedA32(src); michael@0: int da = SkGetPackedA32(dst); michael@0: int a = srcover_byte(sa, da); michael@0: int r = exclusion_byte(SkGetPackedR32(src), SkGetPackedR32(dst), sa, da); michael@0: int g = exclusion_byte(SkGetPackedG32(src), SkGetPackedG32(dst), sa, da); michael@0: int b = exclusion_byte(SkGetPackedB32(src), SkGetPackedB32(dst), sa, da); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // The CSS compositing spec introduces the following formulas: michael@0: // (See https://dvcs.w3.org/hg/FXTF/rawfile/tip/compositing/index.html#blendingnonseparable) michael@0: // SkComputeLuminance is similar to this formula but it uses the new definition from Rec. 709 michael@0: // while PDF and CG uses the one from Rec. Rec. 601 michael@0: // See http://www.glennchan.info/articles/technical/hd-versus-sd-color-space/hd-versus-sd-color-space.htm michael@0: static inline int Lum(int r, int g, int b) michael@0: { michael@0: return SkDiv255Round(r * 77 + g * 150 + b * 28); michael@0: } michael@0: michael@0: static inline int min2(int a, int b) { return a < b ? a : b; } michael@0: static inline int max2(int a, int b) { return a > b ? a : b; } michael@0: #define minimum(a, b, c) min2(min2(a, b), c) michael@0: #define maximum(a, b, c) max2(max2(a, b), c) michael@0: michael@0: static inline int Sat(int r, int g, int b) { michael@0: return maximum(r, g, b) - minimum(r, g, b); michael@0: } michael@0: michael@0: static inline void setSaturationComponents(int* Cmin, int* Cmid, int* Cmax, int s) { michael@0: if(*Cmax > *Cmin) { michael@0: *Cmid = SkMulDiv(*Cmid - *Cmin, s, *Cmax - *Cmin); michael@0: *Cmax = s; michael@0: } else { michael@0: *Cmax = 0; michael@0: *Cmid = 0; michael@0: } michael@0: michael@0: *Cmin = 0; michael@0: } michael@0: michael@0: static inline void SetSat(int* r, int* g, int* b, int s) { michael@0: if(*r <= *g) { michael@0: if(*g <= *b) { michael@0: setSaturationComponents(r, g, b, s); michael@0: } else if(*r <= *b) { michael@0: setSaturationComponents(r, b, g, s); michael@0: } else { michael@0: setSaturationComponents(b, r, g, s); michael@0: } michael@0: } else if(*r <= *b) { michael@0: setSaturationComponents(g, r, b, s); michael@0: } else if(*g <= *b) { michael@0: setSaturationComponents(g, b, r, s); michael@0: } else { michael@0: setSaturationComponents(b, g, r, s); michael@0: } michael@0: } michael@0: michael@0: static inline void clipColor(int* r, int* g, int* b, int a) { michael@0: int L = Lum(*r, *g, *b); michael@0: int n = minimum(*r, *g, *b); michael@0: int x = maximum(*r, *g, *b); michael@0: int denom; michael@0: if ((n < 0) && (denom = L - n)) { // Compute denom and make sure it's non zero michael@0: *r = L + SkMulDiv(*r - L, L, denom); michael@0: *g = L + SkMulDiv(*g - L, L, denom); michael@0: *b = L + SkMulDiv(*b - L, L, denom); michael@0: } michael@0: michael@0: if ((x > a) && (denom = x - L)) { // Compute denom and make sure it's non zero michael@0: int numer = a - L; michael@0: *r = L + SkMulDiv(*r - L, numer, denom); michael@0: *g = L + SkMulDiv(*g - L, numer, denom); michael@0: *b = L + SkMulDiv(*b - L, numer, denom); michael@0: } michael@0: } michael@0: michael@0: static inline void SetLum(int* r, int* g, int* b, int a, int l) { michael@0: int d = l - Lum(*r, *g, *b); michael@0: *r += d; michael@0: *g += d; michael@0: *b += d; michael@0: michael@0: clipColor(r, g, b, a); michael@0: } michael@0: michael@0: // non-separable blend modes are done in non-premultiplied alpha michael@0: #define blendfunc_nonsep_byte(sc, dc, sa, da, blendval) \ michael@0: clamp_div255round(sc * (255 - da) + dc * (255 - sa) + blendval) michael@0: michael@0: // kHue_Mode michael@0: // B(Cb, Cs) = SetLum(SetSat(Cs, Sat(Cb)), Lum(Cb)) michael@0: // Create a color with the hue of the source color and the saturation and luminosity of the backdrop color. michael@0: static SkPMColor hue_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sr = SkGetPackedR32(src); michael@0: int sg = SkGetPackedG32(src); michael@0: int sb = SkGetPackedB32(src); michael@0: int sa = SkGetPackedA32(src); michael@0: michael@0: int dr = SkGetPackedR32(dst); michael@0: int dg = SkGetPackedG32(dst); michael@0: int db = SkGetPackedB32(dst); michael@0: int da = SkGetPackedA32(dst); michael@0: int Sr, Sg, Sb; michael@0: michael@0: if(sa && da) { michael@0: Sr = sr * sa; michael@0: Sg = sg * sa; michael@0: Sb = sb * sa; michael@0: SetSat(&Sr, &Sg, &Sb, Sat(dr, dg, db) * sa); michael@0: SetLum(&Sr, &Sg, &Sb, sa * da, Lum(dr, dg, db) * sa); michael@0: } else { michael@0: Sr = 0; michael@0: Sg = 0; michael@0: Sb = 0; michael@0: } michael@0: michael@0: int a = srcover_byte(sa, da); michael@0: int r = blendfunc_nonsep_byte(sr, dr, sa, da, Sr); michael@0: int g = blendfunc_nonsep_byte(sg, dg, sa, da, Sg); michael@0: int b = blendfunc_nonsep_byte(sb, db, sa, da, Sb); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // kSaturation_Mode michael@0: // B(Cb, Cs) = SetLum(SetSat(Cb, Sat(Cs)), Lum(Cb)) michael@0: // Create a color with the saturation of the source color and the hue and luminosity of the backdrop color. michael@0: static SkPMColor saturation_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sr = SkGetPackedR32(src); michael@0: int sg = SkGetPackedG32(src); michael@0: int sb = SkGetPackedB32(src); michael@0: int sa = SkGetPackedA32(src); michael@0: michael@0: int dr = SkGetPackedR32(dst); michael@0: int dg = SkGetPackedG32(dst); michael@0: int db = SkGetPackedB32(dst); michael@0: int da = SkGetPackedA32(dst); michael@0: int Dr, Dg, Db; michael@0: michael@0: if(sa && da) { michael@0: Dr = dr * sa; michael@0: Dg = dg * sa; michael@0: Db = db * sa; michael@0: SetSat(&Dr, &Dg, &Db, Sat(sr, sg, sb) * da); michael@0: SetLum(&Dr, &Dg, &Db, sa * da, Lum(dr, dg, db) * sa); michael@0: } else { michael@0: Dr = 0; michael@0: Dg = 0; michael@0: Db = 0; michael@0: } michael@0: michael@0: int a = srcover_byte(sa, da); michael@0: int r = blendfunc_nonsep_byte(sr, dr, sa, da, Dr); michael@0: int g = blendfunc_nonsep_byte(sg, dg, sa, da, Dg); michael@0: int b = blendfunc_nonsep_byte(sb, db, sa, da, Db); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // kColor_Mode michael@0: // B(Cb, Cs) = SetLum(Cs, Lum(Cb)) michael@0: // Create a color with the hue and saturation of the source color and the luminosity of the backdrop color. michael@0: static SkPMColor color_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sr = SkGetPackedR32(src); michael@0: int sg = SkGetPackedG32(src); michael@0: int sb = SkGetPackedB32(src); michael@0: int sa = SkGetPackedA32(src); michael@0: michael@0: int dr = SkGetPackedR32(dst); michael@0: int dg = SkGetPackedG32(dst); michael@0: int db = SkGetPackedB32(dst); michael@0: int da = SkGetPackedA32(dst); michael@0: int Sr, Sg, Sb; michael@0: michael@0: if(sa && da) { michael@0: Sr = sr * da; michael@0: Sg = sg * da; michael@0: Sb = sb * da; michael@0: SetLum(&Sr, &Sg, &Sb, sa * da, Lum(dr, dg, db) * sa); michael@0: } else { michael@0: Sr = 0; michael@0: Sg = 0; michael@0: Sb = 0; michael@0: } michael@0: michael@0: int a = srcover_byte(sa, da); michael@0: int r = blendfunc_nonsep_byte(sr, dr, sa, da, Sr); michael@0: int g = blendfunc_nonsep_byte(sg, dg, sa, da, Sg); michael@0: int b = blendfunc_nonsep_byte(sb, db, sa, da, Sb); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: // kLuminosity_Mode michael@0: // B(Cb, Cs) = SetLum(Cb, Lum(Cs)) michael@0: // Create a color with the luminosity of the source color and the hue and saturation of the backdrop color. michael@0: static SkPMColor luminosity_modeproc(SkPMColor src, SkPMColor dst) { michael@0: int sr = SkGetPackedR32(src); michael@0: int sg = SkGetPackedG32(src); michael@0: int sb = SkGetPackedB32(src); michael@0: int sa = SkGetPackedA32(src); michael@0: michael@0: int dr = SkGetPackedR32(dst); michael@0: int dg = SkGetPackedG32(dst); michael@0: int db = SkGetPackedB32(dst); michael@0: int da = SkGetPackedA32(dst); michael@0: int Dr, Dg, Db; michael@0: michael@0: if(sa && da) { michael@0: Dr = dr * sa; michael@0: Dg = dg * sa; michael@0: Db = db * sa; michael@0: SetLum(&Dr, &Dg, &Db, sa * da, Lum(sr, sg, sb) * da); michael@0: } else { michael@0: Dr = 0; michael@0: Dg = 0; michael@0: Db = 0; michael@0: } michael@0: michael@0: int a = srcover_byte(sa, da); michael@0: int r = blendfunc_nonsep_byte(sr, dr, sa, da, Dr); michael@0: int g = blendfunc_nonsep_byte(sg, dg, sa, da, Dg); michael@0: int b = blendfunc_nonsep_byte(sb, db, sa, da, Db); michael@0: return SkPackARGB32(a, r, g, b); michael@0: } michael@0: michael@0: const ProcCoeff gProcCoeffs[] = { michael@0: { clear_modeproc, SkXfermode::kZero_Coeff, SkXfermode::kZero_Coeff }, michael@0: { src_modeproc, SkXfermode::kOne_Coeff, SkXfermode::kZero_Coeff }, michael@0: { dst_modeproc, SkXfermode::kZero_Coeff, SkXfermode::kOne_Coeff }, michael@0: { srcover_modeproc, SkXfermode::kOne_Coeff, SkXfermode::kISA_Coeff }, michael@0: { dstover_modeproc, SkXfermode::kIDA_Coeff, SkXfermode::kOne_Coeff }, michael@0: { srcin_modeproc, SkXfermode::kDA_Coeff, SkXfermode::kZero_Coeff }, michael@0: { dstin_modeproc, SkXfermode::kZero_Coeff, SkXfermode::kSA_Coeff }, michael@0: { srcout_modeproc, SkXfermode::kIDA_Coeff, SkXfermode::kZero_Coeff }, michael@0: { dstout_modeproc, SkXfermode::kZero_Coeff, SkXfermode::kISA_Coeff }, michael@0: { srcatop_modeproc, SkXfermode::kDA_Coeff, SkXfermode::kISA_Coeff }, michael@0: { dstatop_modeproc, SkXfermode::kIDA_Coeff, SkXfermode::kSA_Coeff }, michael@0: { xor_modeproc, SkXfermode::kIDA_Coeff, SkXfermode::kISA_Coeff }, michael@0: michael@0: { plus_modeproc, SkXfermode::kOne_Coeff, SkXfermode::kOne_Coeff }, michael@0: { modulate_modeproc,SkXfermode::kZero_Coeff, SkXfermode::kSC_Coeff }, michael@0: { screen_modeproc, SkXfermode::kOne_Coeff, SkXfermode::kISC_Coeff }, michael@0: { overlay_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: { darken_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: { lighten_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: { colordodge_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: { colorburn_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: { hardlight_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: { softlight_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: { difference_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: { exclusion_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: { multiply_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: { hue_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: { saturation_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: { color_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: { luminosity_modeproc, CANNOT_USE_COEFF, CANNOT_USE_COEFF }, michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: bool SkXfermode::asCoeff(Coeff* src, Coeff* dst) const { michael@0: return false; michael@0: } michael@0: michael@0: bool SkXfermode::asMode(Mode* mode) const { michael@0: return false; michael@0: } michael@0: michael@0: bool SkXfermode::asNewEffect(GrEffectRef** effect, GrTexture* background) const { michael@0: return false; michael@0: } michael@0: michael@0: bool SkXfermode::AsNewEffectOrCoeff(SkXfermode* xfermode, michael@0: GrEffectRef** effect, michael@0: Coeff* src, michael@0: Coeff* dst, michael@0: GrTexture* background) { michael@0: if (NULL == xfermode) { michael@0: return ModeAsCoeff(kSrcOver_Mode, src, dst); michael@0: } else if (xfermode->asCoeff(src, dst)) { michael@0: return true; michael@0: } else { michael@0: return xfermode->asNewEffect(effect, background); michael@0: } michael@0: } michael@0: michael@0: SkPMColor SkXfermode::xferColor(SkPMColor src, SkPMColor dst) const{ michael@0: // no-op. subclasses should override this michael@0: return dst; michael@0: } michael@0: michael@0: void SkXfermode::xfer32(SkPMColor* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT src, int count, michael@0: const SkAlpha* SK_RESTRICT aa) const { michael@0: SkASSERT(dst && src && count >= 0); michael@0: michael@0: if (NULL == aa) { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: dst[i] = this->xferColor(src[i], dst[i]); michael@0: } michael@0: } else { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: unsigned a = aa[i]; michael@0: if (0 != a) { michael@0: SkPMColor dstC = dst[i]; michael@0: SkPMColor C = this->xferColor(src[i], dstC); michael@0: if (0xFF != a) { michael@0: C = SkFourByteInterp(C, dstC, a); michael@0: } michael@0: dst[i] = C; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkXfermode::xfer16(uint16_t* dst, michael@0: const SkPMColor* SK_RESTRICT src, int count, michael@0: const SkAlpha* SK_RESTRICT aa) const { michael@0: SkASSERT(dst && src && count >= 0); michael@0: michael@0: if (NULL == aa) { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: SkPMColor dstC = SkPixel16ToPixel32(dst[i]); michael@0: dst[i] = SkPixel32ToPixel16_ToU16(this->xferColor(src[i], dstC)); michael@0: } michael@0: } else { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: unsigned a = aa[i]; michael@0: if (0 != a) { michael@0: SkPMColor dstC = SkPixel16ToPixel32(dst[i]); michael@0: SkPMColor C = this->xferColor(src[i], dstC); michael@0: if (0xFF != a) { michael@0: C = SkFourByteInterp(C, dstC, a); michael@0: } michael@0: dst[i] = SkPixel32ToPixel16_ToU16(C); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkXfermode::xferA8(SkAlpha* SK_RESTRICT dst, michael@0: const SkPMColor src[], int count, michael@0: const SkAlpha* SK_RESTRICT aa) const { michael@0: SkASSERT(dst && src && count >= 0); michael@0: michael@0: if (NULL == aa) { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: SkPMColor res = this->xferColor(src[i], (dst[i] << SK_A32_SHIFT)); michael@0: dst[i] = SkToU8(SkGetPackedA32(res)); michael@0: } michael@0: } else { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: unsigned a = aa[i]; michael@0: if (0 != a) { michael@0: SkAlpha dstA = dst[i]; michael@0: unsigned A = SkGetPackedA32(this->xferColor(src[i], michael@0: (SkPMColor)(dstA << SK_A32_SHIFT))); michael@0: if (0xFF != a) { michael@0: A = SkAlphaBlend(A, dstA, SkAlpha255To256(a)); michael@0: } michael@0: dst[i] = SkToU8(A); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkProcXfermode::xfer32(SkPMColor* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT src, int count, michael@0: const SkAlpha* SK_RESTRICT aa) const { michael@0: SkASSERT(dst && src && count >= 0); michael@0: michael@0: SkXfermodeProc proc = fProc; michael@0: michael@0: if (NULL != proc) { michael@0: if (NULL == aa) { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: dst[i] = proc(src[i], dst[i]); michael@0: } michael@0: } else { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: unsigned a = aa[i]; michael@0: if (0 != a) { michael@0: SkPMColor dstC = dst[i]; michael@0: SkPMColor C = proc(src[i], dstC); michael@0: if (a != 0xFF) { michael@0: C = SkFourByteInterp(C, dstC, a); michael@0: } michael@0: dst[i] = C; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkProcXfermode::xfer16(uint16_t* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT src, int count, michael@0: const SkAlpha* SK_RESTRICT aa) const { michael@0: SkASSERT(dst && src && count >= 0); michael@0: michael@0: SkXfermodeProc proc = fProc; michael@0: michael@0: if (NULL != proc) { michael@0: if (NULL == aa) { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: SkPMColor dstC = SkPixel16ToPixel32(dst[i]); michael@0: dst[i] = SkPixel32ToPixel16_ToU16(proc(src[i], dstC)); michael@0: } michael@0: } else { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: unsigned a = aa[i]; michael@0: if (0 != a) { michael@0: SkPMColor dstC = SkPixel16ToPixel32(dst[i]); michael@0: SkPMColor C = proc(src[i], dstC); michael@0: if (0xFF != a) { michael@0: C = SkFourByteInterp(C, dstC, a); michael@0: } michael@0: dst[i] = SkPixel32ToPixel16_ToU16(C); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkProcXfermode::xferA8(SkAlpha* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT src, int count, michael@0: const SkAlpha* SK_RESTRICT aa) const { michael@0: SkASSERT(dst && src && count >= 0); michael@0: michael@0: SkXfermodeProc proc = fProc; michael@0: michael@0: if (NULL != proc) { michael@0: if (NULL == aa) { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: SkPMColor res = proc(src[i], dst[i] << SK_A32_SHIFT); michael@0: dst[i] = SkToU8(SkGetPackedA32(res)); michael@0: } michael@0: } else { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: unsigned a = aa[i]; michael@0: if (0 != a) { michael@0: SkAlpha dstA = dst[i]; michael@0: SkPMColor res = proc(src[i], dstA << SK_A32_SHIFT); michael@0: unsigned A = SkGetPackedA32(res); michael@0: if (0xFF != a) { michael@0: A = SkAlphaBlend(A, dstA, SkAlpha255To256(a)); michael@0: } michael@0: dst[i] = SkToU8(A); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: SkProcXfermode::SkProcXfermode(SkReadBuffer& buffer) michael@0: : SkXfermode(buffer) { michael@0: fProc = NULL; michael@0: if (!buffer.isCrossProcess()) { michael@0: fProc = (SkXfermodeProc)buffer.readFunctionPtr(); michael@0: } michael@0: } michael@0: michael@0: void SkProcXfermode::flatten(SkWriteBuffer& buffer) const { michael@0: this->INHERITED::flatten(buffer); michael@0: if (!buffer.isCrossProcess()) { michael@0: buffer.writeFunctionPtr((void*)fProc); michael@0: } michael@0: } michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkProcXfermode::toString(SkString* str) const { michael@0: str->appendf("SkProcXfermode: %p", fProc); michael@0: } michael@0: #endif michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #if SK_SUPPORT_GPU michael@0: michael@0: #include "GrEffect.h" michael@0: #include "GrCoordTransform.h" michael@0: #include "GrEffectUnitTest.h" michael@0: #include "GrTBackendEffectFactory.h" michael@0: #include "gl/GrGLEffect.h" michael@0: michael@0: /** michael@0: * GrEffect that implements the all the separable xfer modes that cannot be expressed as Coeffs. michael@0: */ michael@0: class XferEffect : public GrEffect { michael@0: public: michael@0: static bool IsSupportedMode(SkXfermode::Mode mode) { michael@0: return mode > SkXfermode::kLastCoeffMode && mode <= SkXfermode::kLastMode; michael@0: } michael@0: michael@0: static GrEffectRef* Create(SkXfermode::Mode mode, GrTexture* background) { michael@0: if (!IsSupportedMode(mode)) { michael@0: return NULL; michael@0: } else { michael@0: AutoEffectUnref effect(SkNEW_ARGS(XferEffect, (mode, background))); michael@0: return CreateEffectRef(effect); michael@0: } michael@0: } michael@0: michael@0: virtual void getConstantColorComponents(GrColor* color, michael@0: uint32_t* validFlags) const SK_OVERRIDE { michael@0: *validFlags = 0; michael@0: } michael@0: michael@0: virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { michael@0: return GrTBackendEffectFactory::getInstance(); michael@0: } michael@0: michael@0: static const char* Name() { return "XferEffect"; } michael@0: michael@0: SkXfermode::Mode mode() const { return fMode; } michael@0: const GrTextureAccess& backgroundAccess() const { return fBackgroundAccess; } michael@0: michael@0: class GLEffect : public GrGLEffect { michael@0: public: michael@0: GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) michael@0: : GrGLEffect(factory) { michael@0: } michael@0: virtual void emitCode(GrGLShaderBuilder* builder, michael@0: const GrDrawEffect& drawEffect, michael@0: EffectKey key, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TransformedCoordsArray& coords, michael@0: const TextureSamplerArray& samplers) SK_OVERRIDE { michael@0: SkXfermode::Mode mode = drawEffect.castEffect().mode(); michael@0: const GrTexture* backgroundTex = drawEffect.castEffect().backgroundAccess().getTexture(); michael@0: const char* dstColor; michael@0: if (backgroundTex) { michael@0: dstColor = "bgColor"; michael@0: builder->fsCodeAppendf("\t\tvec4 %s = ", dstColor); michael@0: builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type()); michael@0: builder->fsCodeAppendf(";\n"); michael@0: } else { michael@0: dstColor = builder->dstColor(); michael@0: } michael@0: SkASSERT(NULL != dstColor); michael@0: michael@0: // We don't try to optimize for this case at all michael@0: if (NULL == inputColor) { michael@0: builder->fsCodeAppendf("\t\tconst vec4 ones = vec4(1);\n"); michael@0: inputColor = "ones"; michael@0: } michael@0: builder->fsCodeAppendf("\t\t// SkXfermode::Mode: %s\n", SkXfermode::ModeName(mode)); michael@0: michael@0: // These all perform src-over on the alpha channel. michael@0: builder->fsCodeAppendf("\t\t%s.a = %s.a + (1.0 - %s.a) * %s.a;\n", michael@0: outputColor, inputColor, inputColor, dstColor); michael@0: michael@0: switch (mode) { michael@0: case SkXfermode::kOverlay_Mode: michael@0: // Overlay is Hard-Light with the src and dst reversed michael@0: HardLight(builder, outputColor, dstColor, inputColor); michael@0: break; michael@0: case SkXfermode::kDarken_Mode: michael@0: builder->fsCodeAppendf("\t\t%s.rgb = min((1.0 - %s.a) * %s.rgb + %s.rgb, " michael@0: "(1.0 - %s.a) * %s.rgb + %s.rgb);\n", michael@0: outputColor, michael@0: inputColor, dstColor, inputColor, michael@0: dstColor, inputColor, dstColor); michael@0: break; michael@0: case SkXfermode::kLighten_Mode: michael@0: builder->fsCodeAppendf("\t\t%s.rgb = max((1.0 - %s.a) * %s.rgb + %s.rgb, " michael@0: "(1.0 - %s.a) * %s.rgb + %s.rgb);\n", michael@0: outputColor, michael@0: inputColor, dstColor, inputColor, michael@0: dstColor, inputColor, dstColor); michael@0: break; michael@0: case SkXfermode::kColorDodge_Mode: michael@0: ColorDodgeComponent(builder, outputColor, inputColor, dstColor, 'r'); michael@0: ColorDodgeComponent(builder, outputColor, inputColor, dstColor, 'g'); michael@0: ColorDodgeComponent(builder, outputColor, inputColor, dstColor, 'b'); michael@0: break; michael@0: case SkXfermode::kColorBurn_Mode: michael@0: ColorBurnComponent(builder, outputColor, inputColor, dstColor, 'r'); michael@0: ColorBurnComponent(builder, outputColor, inputColor, dstColor, 'g'); michael@0: ColorBurnComponent(builder, outputColor, inputColor, dstColor, 'b'); michael@0: break; michael@0: case SkXfermode::kHardLight_Mode: michael@0: HardLight(builder, outputColor, inputColor, dstColor); michael@0: break; michael@0: case SkXfermode::kSoftLight_Mode: michael@0: builder->fsCodeAppendf("\t\tif (0.0 == %s.a) {\n", dstColor); michael@0: builder->fsCodeAppendf("\t\t\t%s.rgba = %s;\n", outputColor, inputColor); michael@0: builder->fsCodeAppendf("\t\t} else {\n"); michael@0: SoftLightComponentPosDstAlpha(builder, outputColor, inputColor, dstColor, 'r'); michael@0: SoftLightComponentPosDstAlpha(builder, outputColor, inputColor, dstColor, 'g'); michael@0: SoftLightComponentPosDstAlpha(builder, outputColor, inputColor, dstColor, 'b'); michael@0: builder->fsCodeAppendf("\t\t}\n"); michael@0: break; michael@0: case SkXfermode::kDifference_Mode: michael@0: builder->fsCodeAppendf("\t\t%s.rgb = %s.rgb + %s.rgb -" michael@0: "2.0 * min(%s.rgb * %s.a, %s.rgb * %s.a);\n", michael@0: outputColor, inputColor, dstColor, inputColor, dstColor, michael@0: dstColor, inputColor); michael@0: break; michael@0: case SkXfermode::kExclusion_Mode: michael@0: builder->fsCodeAppendf("\t\t%s.rgb = %s.rgb + %s.rgb - " michael@0: "2.0 * %s.rgb * %s.rgb;\n", michael@0: outputColor, dstColor, inputColor, dstColor, inputColor); michael@0: break; michael@0: case SkXfermode::kMultiply_Mode: michael@0: builder->fsCodeAppendf("\t\t%s.rgb = (1.0 - %s.a) * %s.rgb + " michael@0: "(1.0 - %s.a) * %s.rgb + " michael@0: "%s.rgb * %s.rgb;\n", michael@0: outputColor, inputColor, dstColor, dstColor, inputColor, michael@0: inputColor, dstColor); michael@0: break; michael@0: case SkXfermode::kHue_Mode: { michael@0: // SetLum(SetSat(S * Da, Sat(D * Sa)), Sa*Da, D*Sa) + (1 - Sa) * D + (1 - Da) * S michael@0: SkString setSat, setLum; michael@0: AddSatFunction(builder, &setSat); michael@0: AddLumFunction(builder, &setLum); michael@0: builder->fsCodeAppendf("\t\tvec4 dstSrcAlpha = %s * %s.a;\n", michael@0: dstColor, inputColor); michael@0: builder->fsCodeAppendf("\t\t%s.rgb = %s(%s(%s.rgb * %s.a, dstSrcAlpha.rgb), dstSrcAlpha.a, dstSrcAlpha.rgb);\n", michael@0: outputColor, setLum.c_str(), setSat.c_str(), inputColor, michael@0: dstColor); michael@0: builder->fsCodeAppendf("\t\t%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;\n", michael@0: outputColor, inputColor, dstColor, dstColor, inputColor); michael@0: break; michael@0: } michael@0: case SkXfermode::kSaturation_Mode: { michael@0: // SetLum(SetSat(D * Sa, Sat(S * Da)), Sa*Da, D*Sa)) + (1 - Sa) * D + (1 - Da) * S michael@0: SkString setSat, setLum; michael@0: AddSatFunction(builder, &setSat); michael@0: AddLumFunction(builder, &setLum); michael@0: builder->fsCodeAppendf("\t\tvec4 dstSrcAlpha = %s * %s.a;\n", michael@0: dstColor, inputColor); michael@0: builder->fsCodeAppendf("\t\t%s.rgb = %s(%s(dstSrcAlpha.rgb, %s.rgb * %s.a), dstSrcAlpha.a, dstSrcAlpha.rgb);\n", michael@0: outputColor, setLum.c_str(), setSat.c_str(), inputColor, michael@0: dstColor); michael@0: builder->fsCodeAppendf("\t\t%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;\n", michael@0: outputColor, inputColor, dstColor, dstColor, inputColor); michael@0: break; michael@0: } michael@0: case SkXfermode::kColor_Mode: { michael@0: // SetLum(S * Da, Sa* Da, D * Sa) + (1 - Sa) * D + (1 - Da) * S michael@0: SkString setLum; michael@0: AddLumFunction(builder, &setLum); michael@0: builder->fsCodeAppendf("\t\tvec4 srcDstAlpha = %s * %s.a;\n", michael@0: inputColor, dstColor); michael@0: builder->fsCodeAppendf("\t\t%s.rgb = %s(srcDstAlpha.rgb, srcDstAlpha.a, %s.rgb * %s.a);\n", michael@0: outputColor, setLum.c_str(), dstColor, inputColor); michael@0: builder->fsCodeAppendf("\t\t%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;\n", michael@0: outputColor, inputColor, dstColor, dstColor, inputColor); michael@0: break; michael@0: } michael@0: case SkXfermode::kLuminosity_Mode: { michael@0: // SetLum(D * Sa, Sa* Da, S * Da) + (1 - Sa) * D + (1 - Da) * S michael@0: SkString setLum; michael@0: AddLumFunction(builder, &setLum); michael@0: builder->fsCodeAppendf("\t\tvec4 srcDstAlpha = %s * %s.a;\n", michael@0: inputColor, dstColor); michael@0: builder->fsCodeAppendf("\t\t%s.rgb = %s(%s.rgb * %s.a, srcDstAlpha.a, srcDstAlpha.rgb);\n", michael@0: outputColor, setLum.c_str(), dstColor, inputColor); michael@0: builder->fsCodeAppendf("\t\t%s.rgb += (1.0 - %s.a) * %s.rgb + (1.0 - %s.a) * %s.rgb;\n", michael@0: outputColor, inputColor, dstColor, dstColor, inputColor); michael@0: break; michael@0: } michael@0: default: michael@0: GrCrash("Unknown XferEffect mode."); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { michael@0: return drawEffect.castEffect().mode(); michael@0: } michael@0: michael@0: private: michael@0: static void HardLight(GrGLShaderBuilder* builder, michael@0: const char* final, michael@0: const char* src, michael@0: const char* dst) { michael@0: static const char kComponents[] = {'r', 'g', 'b'}; michael@0: for (size_t i = 0; i < SK_ARRAY_COUNT(kComponents); ++i) { michael@0: char component = kComponents[i]; michael@0: builder->fsCodeAppendf("\t\tif (2.0 * %s.%c <= %s.a) {\n", src, component, src); michael@0: builder->fsCodeAppendf("\t\t\t%s.%c = 2.0 * %s.%c * %s.%c;\n", final, component, src, component, dst, component); michael@0: builder->fsCodeAppend("\t\t} else {\n"); michael@0: builder->fsCodeAppendf("\t\t\t%s.%c = %s.a * %s.a - 2.0 * (%s.a - %s.%c) * (%s.a - %s.%c);\n", michael@0: final, component, src, dst, dst, dst, component, src, src, component); michael@0: builder->fsCodeAppend("\t\t}\n"); michael@0: } michael@0: builder->fsCodeAppendf("\t\t%s.rgb += %s.rgb * (1.0 - %s.a) + %s.rgb * (1.0 - %s.a);\n", michael@0: final, src, dst, dst, src); michael@0: } michael@0: michael@0: // Does one component of color-dodge michael@0: static void ColorDodgeComponent(GrGLShaderBuilder* builder, michael@0: const char* final, michael@0: const char* src, michael@0: const char* dst, michael@0: const char component) { michael@0: builder->fsCodeAppendf("\t\tif (0.0 == %s.%c) {\n", dst, component); michael@0: builder->fsCodeAppendf("\t\t\t%s.%c = %s.%c * (1.0 - %s.a);\n", michael@0: final, component, src, component, dst); michael@0: builder->fsCodeAppend("\t\t} else {\n"); michael@0: builder->fsCodeAppendf("\t\t\tfloat d = %s.a - %s.%c;\n", src, src, component); michael@0: builder->fsCodeAppend("\t\t\tif (0.0 == d) {\n"); michael@0: builder->fsCodeAppendf("\t\t\t\t%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);\n", michael@0: final, component, src, dst, src, component, dst, dst, component, michael@0: src); michael@0: builder->fsCodeAppend("\t\t\t} else {\n"); michael@0: builder->fsCodeAppendf("\t\t\t\td = min(%s.a, %s.%c * %s.a / d);\n", michael@0: dst, dst, component, src); michael@0: builder->fsCodeAppendf("\t\t\t\t%s.%c = d * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);\n", michael@0: final, component, src, src, component, dst, dst, component, src); michael@0: builder->fsCodeAppend("\t\t\t}\n"); michael@0: builder->fsCodeAppend("\t\t}\n"); michael@0: } michael@0: michael@0: // Does one component of color-burn michael@0: static void ColorBurnComponent(GrGLShaderBuilder* builder, michael@0: const char* final, michael@0: const char* src, michael@0: const char* dst, michael@0: const char component) { michael@0: builder->fsCodeAppendf("\t\tif (%s.a == %s.%c) {\n", dst, dst, component); michael@0: builder->fsCodeAppendf("\t\t\t%s.%c = %s.a * %s.a + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);\n", michael@0: final, component, src, dst, src, component, dst, dst, component, michael@0: src); michael@0: builder->fsCodeAppendf("\t\t} else if (0.0 == %s.%c) {\n", src, component); michael@0: builder->fsCodeAppendf("\t\t\t%s.%c = %s.%c * (1.0 - %s.a);\n", michael@0: final, component, dst, component, src); michael@0: builder->fsCodeAppend("\t\t} else {\n"); michael@0: builder->fsCodeAppendf("\t\t\tfloat d = max(0.0, %s.a - (%s.a - %s.%c) * %s.a / %s.%c);\n", michael@0: dst, dst, dst, component, src, src, component); michael@0: builder->fsCodeAppendf("\t\t\t%s.%c = %s.a * d + %s.%c * (1.0 - %s.a) + %s.%c * (1.0 - %s.a);\n", michael@0: final, component, src, src, component, dst, dst, component, src); michael@0: builder->fsCodeAppend("\t\t}\n"); michael@0: } michael@0: michael@0: // Does one component of soft-light. Caller should have already checked that dst alpha > 0. michael@0: static void SoftLightComponentPosDstAlpha(GrGLShaderBuilder* builder, michael@0: const char* final, michael@0: const char* src, michael@0: const char* dst, michael@0: const char component) { michael@0: // if (2S < Sa) michael@0: builder->fsCodeAppendf("\t\t\tif (2.0 * %s.%c <= %s.a) {\n", src, component, src); michael@0: // (D^2 (Sa-2 S))/Da+(1-Da) S+D (-Sa+2 S+1) michael@0: builder->fsCodeAppendf("\t\t\t\t%s.%c = (%s.%c*%s.%c*(%s.a - 2.0*%s.%c)) / %s.a + (1.0 - %s.a) * %s.%c + %s.%c*(-%s.a + 2.0*%s.%c + 1.0);\n", michael@0: final, component, dst, component, dst, component, src, src, michael@0: component, dst, dst, src, component, dst, component, src, src, michael@0: component); michael@0: // else if (4D < Da) michael@0: builder->fsCodeAppendf("\t\t\t} else if (4.0 * %s.%c <= %s.a) {\n", michael@0: dst, component, dst); michael@0: builder->fsCodeAppendf("\t\t\t\tfloat DSqd = %s.%c * %s.%c;\n", michael@0: dst, component, dst, component); michael@0: builder->fsCodeAppendf("\t\t\t\tfloat DCub = DSqd * %s.%c;\n", dst, component); michael@0: builder->fsCodeAppendf("\t\t\t\tfloat DaSqd = %s.a * %s.a;\n", dst, dst); michael@0: builder->fsCodeAppendf("\t\t\t\tfloat DaCub = DaSqd * %s.a;\n", dst); michael@0: // (Da^3 (-S)+Da^2 (S-D (3 Sa-6 S-1))+12 Da D^2 (Sa-2 S)-16 D^3 (Sa-2 S))/Da^2 michael@0: builder->fsCodeAppendf("\t\t\t\t%s.%c = (-DaCub*%s.%c + DaSqd*(%s.%c - %s.%c * (3.0*%s.a - 6.0*%s.%c - 1.0)) + 12.0*%s.a*DSqd*(%s.a - 2.0*%s.%c) - 16.0*DCub * (%s.a - 2.0*%s.%c)) / DaSqd;\n", michael@0: final, component, src, component, src, component, dst, component, michael@0: src, src, component, dst, src, src, component, src, src, michael@0: component); michael@0: builder->fsCodeAppendf("\t\t\t} else {\n"); michael@0: // -sqrt(Da * D) (Sa-2 S)-Da S+D (Sa-2 S+1)+S michael@0: builder->fsCodeAppendf("\t\t\t\t%s.%c = -sqrt(%s.a*%s.%c)*(%s.a - 2.0*%s.%c) - %s.a*%s.%c + %s.%c*(%s.a - 2.0*%s.%c + 1.0) + %s.%c;\n", michael@0: final, component, dst, dst, component, src, src, component, dst, michael@0: src, component, dst, component, src, src, component, src, michael@0: component); michael@0: builder->fsCodeAppendf("\t\t\t}\n"); michael@0: } michael@0: michael@0: // Adds a function that takes two colors and an alpha as input. It produces a color with the michael@0: // hue and saturation of the first color, the luminosity of the second color, and the input michael@0: // alpha. It has this signature: michael@0: // vec3 set_luminance(vec3 hueSatColor, float alpha, vec3 lumColor). michael@0: static void AddLumFunction(GrGLShaderBuilder* builder, SkString* setLumFunction) { michael@0: // Emit a helper that gets the luminance of a color. michael@0: SkString getFunction; michael@0: GrGLShaderVar getLumArgs[] = { michael@0: GrGLShaderVar("color", kVec3f_GrSLType), michael@0: }; michael@0: SkString getLumBody("\treturn dot(vec3(0.3, 0.59, 0.11), color);\n"); michael@0: builder->fsEmitFunction(kFloat_GrSLType, michael@0: "luminance", michael@0: SK_ARRAY_COUNT(getLumArgs), getLumArgs, michael@0: getLumBody.c_str(), michael@0: &getFunction); michael@0: michael@0: // Emit the set luminance function. michael@0: GrGLShaderVar setLumArgs[] = { michael@0: GrGLShaderVar("hueSat", kVec3f_GrSLType), michael@0: GrGLShaderVar("alpha", kFloat_GrSLType), michael@0: GrGLShaderVar("lumColor", kVec3f_GrSLType), michael@0: }; michael@0: SkString setLumBody; michael@0: setLumBody.printf("\tfloat diff = %s(lumColor - hueSat);\n", getFunction.c_str()); michael@0: setLumBody.append("\tvec3 outColor = hueSat + diff;\n"); michael@0: setLumBody.appendf("\tfloat outLum = %s(outColor);\n", getFunction.c_str()); michael@0: setLumBody.append("\tfloat minComp = min(min(outColor.r, outColor.g), outColor.b);\n" michael@0: "\tfloat maxComp = max(max(outColor.r, outColor.g), outColor.b);\n" michael@0: "\tif (minComp < 0.0) {\n" michael@0: "\t\toutColor = outLum + ((outColor - vec3(outLum, outLum, outLum)) * outLum) / (outLum - minComp);\n" michael@0: "\t}\n" michael@0: "\tif (maxComp > alpha) {\n" michael@0: "\t\toutColor = outLum + ((outColor - vec3(outLum, outLum, outLum)) * (alpha - outLum)) / (maxComp - outLum);\n" michael@0: "\t}\n" michael@0: "\treturn outColor;\n"); michael@0: builder->fsEmitFunction(kVec3f_GrSLType, michael@0: "set_luminance", michael@0: SK_ARRAY_COUNT(setLumArgs), setLumArgs, michael@0: setLumBody.c_str(), michael@0: setLumFunction); michael@0: } michael@0: michael@0: // Adds a function that creates a color with the hue and luminosity of one input color and michael@0: // the saturation of another color. It will have this signature: michael@0: // float set_saturation(vec3 hueLumColor, vec3 satColor) michael@0: static void AddSatFunction(GrGLShaderBuilder* builder, SkString* setSatFunction) { michael@0: // Emit a helper that gets the saturation of a color michael@0: SkString getFunction; michael@0: GrGLShaderVar getSatArgs[] = { GrGLShaderVar("color", kVec3f_GrSLType) }; michael@0: SkString getSatBody; michael@0: getSatBody.printf("\treturn max(max(color.r, color.g), color.b) - " michael@0: "min(min(color.r, color.g), color.b);\n"); michael@0: builder->fsEmitFunction(kFloat_GrSLType, michael@0: "saturation", michael@0: SK_ARRAY_COUNT(getSatArgs), getSatArgs, michael@0: getSatBody.c_str(), michael@0: &getFunction); michael@0: michael@0: // Emit a helper that sets the saturation given sorted input channels. This used michael@0: // to use inout params for min, mid, and max components but that seems to cause michael@0: // problems on PowerVR drivers. So instead it returns a vec3 where r, g ,b are the michael@0: // adjusted min, mid, and max inputs, respectively. michael@0: SkString helperFunction; michael@0: GrGLShaderVar helperArgs[] = { michael@0: GrGLShaderVar("minComp", kFloat_GrSLType), michael@0: GrGLShaderVar("midComp", kFloat_GrSLType), michael@0: GrGLShaderVar("maxComp", kFloat_GrSLType), michael@0: GrGLShaderVar("sat", kFloat_GrSLType), michael@0: }; michael@0: static const char kHelperBody[] = "\tif (minComp < maxComp) {\n" michael@0: "\t\tvec3 result;\n" michael@0: "\t\tresult.r = 0.0;\n" michael@0: "\t\tresult.g = sat * (midComp - minComp) / (maxComp - minComp);\n" michael@0: "\t\tresult.b = sat;\n" michael@0: "\t\treturn result;\n" michael@0: "\t} else {\n" michael@0: "\t\treturn vec3(0, 0, 0);\n" michael@0: "\t}\n"; michael@0: builder->fsEmitFunction(kVec3f_GrSLType, michael@0: "set_saturation_helper", michael@0: SK_ARRAY_COUNT(helperArgs), helperArgs, michael@0: kHelperBody, michael@0: &helperFunction); michael@0: michael@0: GrGLShaderVar setSatArgs[] = { michael@0: GrGLShaderVar("hueLumColor", kVec3f_GrSLType), michael@0: GrGLShaderVar("satColor", kVec3f_GrSLType), michael@0: }; michael@0: const char* helpFunc = helperFunction.c_str(); michael@0: SkString setSatBody; michael@0: setSatBody.appendf("\tfloat sat = %s(satColor);\n" michael@0: "\tif (hueLumColor.r <= hueLumColor.g) {\n" michael@0: "\t\tif (hueLumColor.g <= hueLumColor.b) {\n" michael@0: "\t\t\thueLumColor.rgb = %s(hueLumColor.r, hueLumColor.g, hueLumColor.b, sat);\n" michael@0: "\t\t} else if (hueLumColor.r <= hueLumColor.b) {\n" michael@0: "\t\t\thueLumColor.rbg = %s(hueLumColor.r, hueLumColor.b, hueLumColor.g, sat);\n" michael@0: "\t\t} else {\n" michael@0: "\t\t\thueLumColor.brg = %s(hueLumColor.b, hueLumColor.r, hueLumColor.g, sat);\n" michael@0: "\t\t}\n" michael@0: "\t} else if (hueLumColor.r <= hueLumColor.b) {\n" michael@0: "\t\thueLumColor.grb = %s(hueLumColor.g, hueLumColor.r, hueLumColor.b, sat);\n" michael@0: "\t} else if (hueLumColor.g <= hueLumColor.b) {\n" michael@0: "\t\thueLumColor.gbr = %s(hueLumColor.g, hueLumColor.b, hueLumColor.r, sat);\n" michael@0: "\t} else {\n" michael@0: "\t\thueLumColor.bgr = %s(hueLumColor.b, hueLumColor.g, hueLumColor.r, sat);\n" michael@0: "\t}\n" michael@0: "\treturn hueLumColor;\n", michael@0: getFunction.c_str(), helpFunc, helpFunc, helpFunc, helpFunc, michael@0: helpFunc, helpFunc); michael@0: builder->fsEmitFunction(kVec3f_GrSLType, michael@0: "set_saturation", michael@0: SK_ARRAY_COUNT(setSatArgs), setSatArgs, michael@0: setSatBody.c_str(), michael@0: setSatFunction); michael@0: michael@0: } michael@0: michael@0: typedef GrGLEffect INHERITED; michael@0: }; michael@0: michael@0: GR_DECLARE_EFFECT_TEST; michael@0: michael@0: private: michael@0: XferEffect(SkXfermode::Mode mode, GrTexture* background) michael@0: : fMode(mode) { michael@0: if (background) { michael@0: fBackgroundTransform.reset(kLocal_GrCoordSet, background); michael@0: this->addCoordTransform(&fBackgroundTransform); michael@0: fBackgroundAccess.reset(background); michael@0: this->addTextureAccess(&fBackgroundAccess); michael@0: } else { michael@0: this->setWillReadDstColor(); michael@0: } michael@0: } michael@0: virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { michael@0: const XferEffect& s = CastEffect(other); michael@0: return fMode == s.fMode && michael@0: fBackgroundAccess.getTexture() == s.fBackgroundAccess.getTexture(); michael@0: } michael@0: michael@0: SkXfermode::Mode fMode; michael@0: GrCoordTransform fBackgroundTransform; michael@0: GrTextureAccess fBackgroundAccess; michael@0: michael@0: typedef GrEffect INHERITED; michael@0: }; michael@0: michael@0: GR_DEFINE_EFFECT_TEST(XferEffect); michael@0: GrEffectRef* XferEffect::TestCreate(SkRandom* rand, michael@0: GrContext*, michael@0: const GrDrawTargetCaps&, michael@0: GrTexture*[]) { michael@0: int mode = rand->nextRangeU(SkXfermode::kLastCoeffMode + 1, SkXfermode::kLastSeparableMode); michael@0: michael@0: AutoEffectUnref gEffect(SkNEW_ARGS(XferEffect, (static_cast(mode), NULL))); michael@0: return CreateEffectRef(gEffect); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkProcCoeffXfermode::SkProcCoeffXfermode(SkReadBuffer& buffer) : INHERITED(buffer) { michael@0: uint32_t mode32 = buffer.read32() % SK_ARRAY_COUNT(gProcCoeffs); michael@0: if (mode32 >= SK_ARRAY_COUNT(gProcCoeffs)) { michael@0: // out of range, just set to something harmless michael@0: mode32 = SkXfermode::kSrcOut_Mode; michael@0: } michael@0: fMode = (SkXfermode::Mode)mode32; michael@0: michael@0: const ProcCoeff& rec = gProcCoeffs[fMode]; michael@0: // these may be valid, or may be CANNOT_USE_COEFF michael@0: fSrcCoeff = rec.fSC; michael@0: fDstCoeff = rec.fDC; michael@0: // now update our function-ptr in the super class michael@0: this->INHERITED::setProc(rec.fProc); michael@0: } michael@0: michael@0: bool SkProcCoeffXfermode::asMode(Mode* mode) const { michael@0: if (mode) { michael@0: *mode = fMode; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SkProcCoeffXfermode::asCoeff(Coeff* sc, Coeff* dc) const { michael@0: if (CANNOT_USE_COEFF == fSrcCoeff) { michael@0: return false; michael@0: } michael@0: michael@0: if (sc) { michael@0: *sc = fSrcCoeff; michael@0: } michael@0: if (dc) { michael@0: *dc = fDstCoeff; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: #if SK_SUPPORT_GPU michael@0: bool SkProcCoeffXfermode::asNewEffect(GrEffectRef** effect, michael@0: GrTexture* background) const { michael@0: if (XferEffect::IsSupportedMode(fMode)) { michael@0: if (NULL != effect) { michael@0: *effect = XferEffect::Create(fMode, background); michael@0: SkASSERT(NULL != *effect); michael@0: } michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: #endif michael@0: michael@0: void SkProcCoeffXfermode::flatten(SkWriteBuffer& buffer) const { michael@0: this->INHERITED::flatten(buffer); michael@0: buffer.write32(fMode); michael@0: } michael@0: michael@0: const char* SkXfermode::ModeName(Mode mode) { michael@0: SkASSERT((unsigned) mode <= (unsigned)kLastMode); michael@0: const char* gModeStrings[] = { michael@0: "Clear", "Src", "Dst", "SrcOver", "DstOver", "SrcIn", "DstIn", michael@0: "SrcOut", "DstOut", "SrcATop", "DstATop", "Xor", "Plus", michael@0: "Modulate", "Screen", "Overlay", "Darken", "Lighten", "ColorDodge", michael@0: "ColorBurn", "HardLight", "SoftLight", "Difference", "Exclusion", michael@0: "Multiply", "Hue", "Saturation", "Color", "Luminosity" michael@0: }; michael@0: return gModeStrings[mode]; michael@0: SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gModeStrings) == kLastMode + 1, mode_count); michael@0: } michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkProcCoeffXfermode::toString(SkString* str) const { michael@0: str->append("SkProcCoeffXfermode: "); michael@0: michael@0: str->append("mode: "); michael@0: str->append(ModeName(fMode)); michael@0: michael@0: static const char* gCoeffStrings[kCoeffCount] = { michael@0: "Zero", "One", "SC", "ISC", "DC", "IDC", "SA", "ISA", "DA", "IDA" michael@0: }; michael@0: michael@0: str->append(" src: "); michael@0: if (CANNOT_USE_COEFF == fSrcCoeff) { michael@0: str->append("can't use"); michael@0: } else { michael@0: str->append(gCoeffStrings[fSrcCoeff]); michael@0: } michael@0: michael@0: str->append(" dst: "); michael@0: if (CANNOT_USE_COEFF == fDstCoeff) { michael@0: str->append("can't use"); michael@0: } else { michael@0: str->append(gCoeffStrings[fDstCoeff]); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: class SkClearXfermode : public SkProcCoeffXfermode { michael@0: public: michael@0: static SkClearXfermode* Create(const ProcCoeff& rec) { michael@0: return SkNEW_ARGS(SkClearXfermode, (rec)); michael@0: } michael@0: michael@0: virtual void xfer32(SkPMColor*, const SkPMColor*, int, const SkAlpha*) const SK_OVERRIDE; michael@0: virtual void xferA8(SkAlpha*, const SkPMColor*, int, const SkAlpha*) const SK_OVERRIDE; michael@0: michael@0: SK_TO_STRING_OVERRIDE() michael@0: SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkClearXfermode) michael@0: michael@0: private: michael@0: SkClearXfermode(const ProcCoeff& rec) : SkProcCoeffXfermode(rec, kClear_Mode) {} michael@0: SkClearXfermode(SkReadBuffer& buffer) michael@0: : SkProcCoeffXfermode(buffer) {} michael@0: michael@0: typedef SkProcCoeffXfermode INHERITED; michael@0: }; michael@0: michael@0: void SkClearXfermode::xfer32(SkPMColor* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT, int count, michael@0: const SkAlpha* SK_RESTRICT aa) const { michael@0: SkASSERT(dst && count >= 0); michael@0: michael@0: if (NULL == aa) { michael@0: memset(dst, 0, count << 2); michael@0: } else { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: unsigned a = aa[i]; michael@0: if (0xFF == a) { michael@0: dst[i] = 0; michael@0: } else if (a != 0) { michael@0: dst[i] = SkAlphaMulQ(dst[i], SkAlpha255To256(255 - a)); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: void SkClearXfermode::xferA8(SkAlpha* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT, int count, michael@0: const SkAlpha* SK_RESTRICT aa) const { michael@0: SkASSERT(dst && count >= 0); michael@0: michael@0: if (NULL == aa) { michael@0: memset(dst, 0, count); michael@0: } else { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: unsigned a = aa[i]; michael@0: if (0xFF == a) { michael@0: dst[i] = 0; michael@0: } else if (0 != a) { michael@0: dst[i] = SkAlphaMulAlpha(dst[i], 255 - a); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkClearXfermode::toString(SkString* str) const { michael@0: this->INHERITED::toString(str); michael@0: } michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: class SkSrcXfermode : public SkProcCoeffXfermode { michael@0: public: michael@0: static SkSrcXfermode* Create(const ProcCoeff& rec) { michael@0: return SkNEW_ARGS(SkSrcXfermode, (rec)); michael@0: } michael@0: michael@0: virtual void xfer32(SkPMColor*, const SkPMColor*, int, const SkAlpha*) const SK_OVERRIDE; michael@0: virtual void xferA8(SkAlpha*, const SkPMColor*, int, const SkAlpha*) const SK_OVERRIDE; michael@0: michael@0: SK_TO_STRING_OVERRIDE() michael@0: SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSrcXfermode) michael@0: michael@0: private: michael@0: SkSrcXfermode(const ProcCoeff& rec) : SkProcCoeffXfermode(rec, kSrc_Mode) {} michael@0: SkSrcXfermode(SkReadBuffer& buffer) michael@0: : SkProcCoeffXfermode(buffer) {} michael@0: michael@0: typedef SkProcCoeffXfermode INHERITED; michael@0: }; michael@0: michael@0: void SkSrcXfermode::xfer32(SkPMColor* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT src, int count, michael@0: const SkAlpha* SK_RESTRICT aa) const { michael@0: SkASSERT(dst && src && count >= 0); michael@0: michael@0: if (NULL == aa) { michael@0: memcpy(dst, src, count << 2); michael@0: } else { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: unsigned a = aa[i]; michael@0: if (a == 0xFF) { michael@0: dst[i] = src[i]; michael@0: } else if (a != 0) { michael@0: dst[i] = SkFourByteInterp(src[i], dst[i], a); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkSrcXfermode::xferA8(SkAlpha* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT src, int count, michael@0: const SkAlpha* SK_RESTRICT aa) const { michael@0: SkASSERT(dst && src && count >= 0); michael@0: michael@0: if (NULL == aa) { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: dst[i] = SkToU8(SkGetPackedA32(src[i])); michael@0: } michael@0: } else { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: unsigned a = aa[i]; michael@0: if (0 != a) { michael@0: unsigned srcA = SkGetPackedA32(src[i]); michael@0: if (a == 0xFF) { michael@0: dst[i] = SkToU8(srcA); michael@0: } else { michael@0: dst[i] = SkToU8(SkAlphaBlend(srcA, dst[i], a)); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkSrcXfermode::toString(SkString* str) const { michael@0: this->INHERITED::toString(str); michael@0: } michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: class SkDstInXfermode : public SkProcCoeffXfermode { michael@0: public: michael@0: static SkDstInXfermode* Create(const ProcCoeff& rec) { michael@0: return SkNEW_ARGS(SkDstInXfermode, (rec)); michael@0: } michael@0: michael@0: virtual void xfer32(SkPMColor*, const SkPMColor*, int, const SkAlpha*) const SK_OVERRIDE; michael@0: michael@0: SK_TO_STRING_OVERRIDE() michael@0: SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDstInXfermode) michael@0: michael@0: private: michael@0: SkDstInXfermode(const ProcCoeff& rec) : SkProcCoeffXfermode(rec, kDstIn_Mode) {} michael@0: SkDstInXfermode(SkReadBuffer& buffer) : INHERITED(buffer) {} michael@0: michael@0: typedef SkProcCoeffXfermode INHERITED; michael@0: }; michael@0: michael@0: void SkDstInXfermode::xfer32(SkPMColor* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT src, int count, michael@0: const SkAlpha* SK_RESTRICT aa) const { michael@0: SkASSERT(dst && src); michael@0: michael@0: if (count <= 0) { michael@0: return; michael@0: } michael@0: if (NULL != aa) { michael@0: return this->INHERITED::xfer32(dst, src, count, aa); michael@0: } michael@0: michael@0: do { michael@0: unsigned a = SkGetPackedA32(*src); michael@0: *dst = SkAlphaMulQ(*dst, SkAlpha255To256(a)); michael@0: dst++; michael@0: src++; michael@0: } while (--count != 0); michael@0: } michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkDstInXfermode::toString(SkString* str) const { michael@0: this->INHERITED::toString(str); michael@0: } michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: class SkDstOutXfermode : public SkProcCoeffXfermode { michael@0: public: michael@0: static SkDstOutXfermode* Create(const ProcCoeff& rec) { michael@0: return SkNEW_ARGS(SkDstOutXfermode, (rec)); michael@0: } michael@0: michael@0: virtual void xfer32(SkPMColor*, const SkPMColor*, int, const SkAlpha*) const SK_OVERRIDE; michael@0: michael@0: SK_TO_STRING_OVERRIDE() michael@0: SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDstOutXfermode) michael@0: michael@0: private: michael@0: SkDstOutXfermode(const ProcCoeff& rec) : SkProcCoeffXfermode(rec, kDstOut_Mode) {} michael@0: SkDstOutXfermode(SkReadBuffer& buffer) michael@0: : INHERITED(buffer) {} michael@0: michael@0: typedef SkProcCoeffXfermode INHERITED; michael@0: }; michael@0: michael@0: void SkDstOutXfermode::xfer32(SkPMColor* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT src, int count, michael@0: const SkAlpha* SK_RESTRICT aa) const { michael@0: SkASSERT(dst && src); michael@0: michael@0: if (count <= 0) { michael@0: return; michael@0: } michael@0: if (NULL != aa) { michael@0: return this->INHERITED::xfer32(dst, src, count, aa); michael@0: } michael@0: michael@0: do { michael@0: unsigned a = SkGetPackedA32(*src); michael@0: *dst = SkAlphaMulQ(*dst, SkAlpha255To256(255 - a)); michael@0: dst++; michael@0: src++; michael@0: } while (--count != 0); michael@0: } michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkDstOutXfermode::toString(SkString* str) const { michael@0: this->INHERITED::toString(str); michael@0: } michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SK_DECLARE_STATIC_MUTEX(gCachedXfermodesMutex); michael@0: static SkXfermode* gCachedXfermodes[SkXfermode::kLastMode + 1]; michael@0: michael@0: void SkXfermode::Term() { michael@0: SkAutoMutexAcquire ac(gCachedXfermodesMutex); michael@0: michael@0: for (size_t i = 0; i < SK_ARRAY_COUNT(gCachedXfermodes); ++i) { michael@0: SkSafeUnref(gCachedXfermodes[i]); michael@0: gCachedXfermodes[i] = NULL; michael@0: } michael@0: } michael@0: michael@0: extern SkProcCoeffXfermode* SkPlatformXfermodeFactory(const ProcCoeff& rec, michael@0: SkXfermode::Mode mode); michael@0: extern SkXfermodeProc SkPlatformXfermodeProcFactory(SkXfermode::Mode mode); michael@0: michael@0: SkXfermode* SkXfermode::Create(Mode mode) { michael@0: SkASSERT(SK_ARRAY_COUNT(gProcCoeffs) == kModeCount); michael@0: SkASSERT(SK_ARRAY_COUNT(gCachedXfermodes) == kModeCount); michael@0: michael@0: if ((unsigned)mode >= kModeCount) { michael@0: // report error michael@0: return NULL; michael@0: } michael@0: michael@0: // Skia's "defaut" mode is srcover. NULL in SkPaint is interpreted as srcover michael@0: // so we can just return NULL from the factory. michael@0: if (kSrcOver_Mode == mode) { michael@0: return NULL; michael@0: } michael@0: michael@0: // guard our access to gCachedXfermodes, since we may write into it michael@0: SkAutoMutexAcquire ac(gCachedXfermodesMutex); michael@0: michael@0: SkXfermode* xfer = gCachedXfermodes[mode]; michael@0: if (NULL == xfer) { michael@0: ProcCoeff rec = gProcCoeffs[mode]; michael@0: michael@0: SkXfermodeProc pp = SkPlatformXfermodeProcFactory(mode); michael@0: michael@0: if (pp != NULL) { michael@0: rec.fProc = pp; michael@0: } michael@0: michael@0: // check if we have a platform optim for that michael@0: SkProcCoeffXfermode* xfm = SkPlatformXfermodeFactory(rec, mode); michael@0: if (xfm != NULL) { michael@0: xfer = xfm; michael@0: } else { michael@0: // All modes can in theory be represented by the ProcCoeff rec, since michael@0: // it contains function ptrs. However, a few modes are both simple and michael@0: // commonly used, so we call those out for their own subclasses here. michael@0: switch (mode) { michael@0: case kClear_Mode: michael@0: xfer = SkClearXfermode::Create(rec); michael@0: break; michael@0: case kSrc_Mode: michael@0: xfer = SkSrcXfermode::Create(rec); michael@0: break; michael@0: case kSrcOver_Mode: michael@0: SkASSERT(false); // should not land here michael@0: break; michael@0: case kDstIn_Mode: michael@0: xfer = SkDstInXfermode::Create(rec); michael@0: break; michael@0: case kDstOut_Mode: michael@0: xfer = SkDstOutXfermode::Create(rec); michael@0: break; michael@0: default: michael@0: // no special-case, just rely in the rec and its function-ptrs michael@0: xfer = SkProcCoeffXfermode::Create(rec, mode); michael@0: break; michael@0: } michael@0: } michael@0: gCachedXfermodes[mode] = xfer; michael@0: } michael@0: return SkSafeRef(xfer); michael@0: } michael@0: michael@0: SkXfermodeProc SkXfermode::GetProc(Mode mode) { michael@0: SkXfermodeProc proc = NULL; michael@0: if ((unsigned)mode < kModeCount) { michael@0: proc = gProcCoeffs[mode].fProc; michael@0: } michael@0: return proc; michael@0: } michael@0: michael@0: bool SkXfermode::ModeAsCoeff(Mode mode, Coeff* src, Coeff* dst) { michael@0: SkASSERT(SK_ARRAY_COUNT(gProcCoeffs) == kModeCount); michael@0: michael@0: if ((unsigned)mode >= (unsigned)kModeCount) { michael@0: // illegal mode parameter michael@0: return false; michael@0: } michael@0: michael@0: const ProcCoeff& rec = gProcCoeffs[mode]; michael@0: michael@0: if (CANNOT_USE_COEFF == rec.fSC) { michael@0: return false; michael@0: } michael@0: michael@0: SkASSERT(CANNOT_USE_COEFF != rec.fDC); michael@0: if (src) { michael@0: *src = rec.fSC; michael@0: } michael@0: if (dst) { michael@0: *dst = rec.fDC; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SkXfermode::AsMode(const SkXfermode* xfer, Mode* mode) { michael@0: if (NULL == xfer) { michael@0: if (mode) { michael@0: *mode = kSrcOver_Mode; michael@0: } michael@0: return true; michael@0: } michael@0: return xfer->asMode(mode); michael@0: } michael@0: michael@0: bool SkXfermode::AsCoeff(const SkXfermode* xfer, Coeff* src, Coeff* dst) { michael@0: if (NULL == xfer) { michael@0: return ModeAsCoeff(kSrcOver_Mode, src, dst); michael@0: } michael@0: return xfer->asCoeff(src, dst); michael@0: } michael@0: michael@0: bool SkXfermode::IsMode(const SkXfermode* xfer, Mode mode) { michael@0: // if xfer==null then the mode is srcover michael@0: Mode m = kSrcOver_Mode; michael@0: if (xfer && !xfer->asMode(&m)) { michael@0: return false; michael@0: } michael@0: return mode == m; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: //////////// 16bit xfermode procs michael@0: michael@0: #ifdef SK_DEBUG michael@0: static bool require_255(SkPMColor src) { return SkGetPackedA32(src) == 0xFF; } michael@0: static bool require_0(SkPMColor src) { return SkGetPackedA32(src) == 0; } michael@0: #endif michael@0: michael@0: static uint16_t src_modeproc16_255(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_255(src)); michael@0: return SkPixel32ToPixel16(src); michael@0: } michael@0: michael@0: static uint16_t dst_modeproc16(SkPMColor src, uint16_t dst) { michael@0: return dst; michael@0: } michael@0: michael@0: static uint16_t srcover_modeproc16_0(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_0(src)); michael@0: return dst; michael@0: } michael@0: michael@0: static uint16_t srcover_modeproc16_255(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_255(src)); michael@0: return SkPixel32ToPixel16(src); michael@0: } michael@0: michael@0: static uint16_t dstover_modeproc16_0(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_0(src)); michael@0: return dst; michael@0: } michael@0: michael@0: static uint16_t dstover_modeproc16_255(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_255(src)); michael@0: return dst; michael@0: } michael@0: michael@0: static uint16_t srcin_modeproc16_255(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_255(src)); michael@0: return SkPixel32ToPixel16(src); michael@0: } michael@0: michael@0: static uint16_t dstin_modeproc16_255(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_255(src)); michael@0: return dst; michael@0: } michael@0: michael@0: static uint16_t dstout_modeproc16_0(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_0(src)); michael@0: return dst; michael@0: } michael@0: michael@0: static uint16_t srcatop_modeproc16(SkPMColor src, uint16_t dst) { michael@0: unsigned isa = 255 - SkGetPackedA32(src); michael@0: michael@0: return SkPackRGB16( michael@0: SkPacked32ToR16(src) + SkAlphaMulAlpha(SkGetPackedR16(dst), isa), michael@0: SkPacked32ToG16(src) + SkAlphaMulAlpha(SkGetPackedG16(dst), isa), michael@0: SkPacked32ToB16(src) + SkAlphaMulAlpha(SkGetPackedB16(dst), isa)); michael@0: } michael@0: michael@0: static uint16_t srcatop_modeproc16_0(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_0(src)); michael@0: return dst; michael@0: } michael@0: michael@0: static uint16_t srcatop_modeproc16_255(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_255(src)); michael@0: return SkPixel32ToPixel16(src); michael@0: } michael@0: michael@0: static uint16_t dstatop_modeproc16_255(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_255(src)); michael@0: return dst; michael@0: } michael@0: michael@0: /********* michael@0: darken and lighten boil down to this. michael@0: michael@0: darken = (1 - Sa) * Dc + min(Sc, Dc) michael@0: lighten = (1 - Sa) * Dc + max(Sc, Dc) michael@0: michael@0: if (Sa == 0) these become michael@0: darken = Dc + min(0, Dc) = 0 michael@0: lighten = Dc + max(0, Dc) = Dc michael@0: michael@0: if (Sa == 1) these become michael@0: darken = min(Sc, Dc) michael@0: lighten = max(Sc, Dc) michael@0: */ michael@0: michael@0: static uint16_t darken_modeproc16_0(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_0(src)); michael@0: return 0; michael@0: } michael@0: michael@0: static uint16_t darken_modeproc16_255(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_255(src)); michael@0: unsigned r = SkFastMin32(SkPacked32ToR16(src), SkGetPackedR16(dst)); michael@0: unsigned g = SkFastMin32(SkPacked32ToG16(src), SkGetPackedG16(dst)); michael@0: unsigned b = SkFastMin32(SkPacked32ToB16(src), SkGetPackedB16(dst)); michael@0: return SkPackRGB16(r, g, b); michael@0: } michael@0: michael@0: static uint16_t lighten_modeproc16_0(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_0(src)); michael@0: return dst; michael@0: } michael@0: michael@0: static uint16_t lighten_modeproc16_255(SkPMColor src, uint16_t dst) { michael@0: SkASSERT(require_255(src)); michael@0: unsigned r = SkMax32(SkPacked32ToR16(src), SkGetPackedR16(dst)); michael@0: unsigned g = SkMax32(SkPacked32ToG16(src), SkGetPackedG16(dst)); michael@0: unsigned b = SkMax32(SkPacked32ToB16(src), SkGetPackedB16(dst)); michael@0: return SkPackRGB16(r, g, b); michael@0: } michael@0: michael@0: struct Proc16Rec { michael@0: SkXfermodeProc16 fProc16_0; michael@0: SkXfermodeProc16 fProc16_255; michael@0: SkXfermodeProc16 fProc16_General; michael@0: }; michael@0: michael@0: static const Proc16Rec gModeProcs16[] = { michael@0: { NULL, NULL, NULL }, // CLEAR michael@0: { NULL, src_modeproc16_255, NULL }, michael@0: { dst_modeproc16, dst_modeproc16, dst_modeproc16 }, michael@0: { srcover_modeproc16_0, srcover_modeproc16_255, NULL }, michael@0: { dstover_modeproc16_0, dstover_modeproc16_255, NULL }, michael@0: { NULL, srcin_modeproc16_255, NULL }, michael@0: { NULL, dstin_modeproc16_255, NULL }, michael@0: { NULL, NULL, NULL },// SRC_OUT michael@0: { dstout_modeproc16_0, NULL, NULL }, michael@0: { srcatop_modeproc16_0, srcatop_modeproc16_255, srcatop_modeproc16 }, michael@0: { NULL, dstatop_modeproc16_255, NULL }, michael@0: { NULL, NULL, NULL }, // XOR michael@0: michael@0: { NULL, NULL, NULL }, // plus michael@0: { NULL, NULL, NULL }, // modulate michael@0: { NULL, NULL, NULL }, // screen michael@0: { NULL, NULL, NULL }, // overlay michael@0: { darken_modeproc16_0, darken_modeproc16_255, NULL }, // darken michael@0: { lighten_modeproc16_0, lighten_modeproc16_255, NULL }, // lighten michael@0: { NULL, NULL, NULL }, // colordodge michael@0: { NULL, NULL, NULL }, // colorburn michael@0: { NULL, NULL, NULL }, // hardlight michael@0: { NULL, NULL, NULL }, // softlight michael@0: { NULL, NULL, NULL }, // difference michael@0: { NULL, NULL, NULL }, // exclusion michael@0: { NULL, NULL, NULL }, // multiply michael@0: { NULL, NULL, NULL }, // hue michael@0: { NULL, NULL, NULL }, // saturation michael@0: { NULL, NULL, NULL }, // color michael@0: { NULL, NULL, NULL }, // luminosity michael@0: }; michael@0: michael@0: SkXfermodeProc16 SkXfermode::GetProc16(Mode mode, SkColor srcColor) { michael@0: SkXfermodeProc16 proc16 = NULL; michael@0: if ((unsigned)mode < kModeCount) { michael@0: const Proc16Rec& rec = gModeProcs16[mode]; michael@0: unsigned a = SkColorGetA(srcColor); michael@0: michael@0: if (0 == a) { michael@0: proc16 = rec.fProc16_0; michael@0: } else if (255 == a) { michael@0: proc16 = rec.fProc16_255; michael@0: } else { michael@0: proc16 = rec.fProc16_General; michael@0: } michael@0: } michael@0: return proc16; michael@0: } michael@0: michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkXfermode) michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkProcCoeffXfermode) michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkClearXfermode) michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSrcXfermode) michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDstInXfermode) michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDstOutXfermode) michael@0: #if !SK_ARM_NEON_IS_NONE michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkNEONProcCoeffXfermode) michael@0: #endif michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END