reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 6 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 8 | #include "SkBlitRow.h" |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 9 | #include "SkBlendModePriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkColorFilter.h" |
| 11 | #include "SkColorPriv.h" |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 12 | #include "SkModeColorFilter.h" |
Mike Klein | 96b333a | 2016-10-12 11:05:05 -0400 | [diff] [blame] | 13 | #include "SkRasterPipeline.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 14 | #include "SkReadBuffer.h" |
| 15 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkUtils.h" |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 17 | #include "SkString.h" |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 18 | #include "SkValidationUtils.h" |
reed | dd9ffea | 2016-02-18 12:39:14 -0800 | [diff] [blame] | 19 | #include "SkPM4f.h" |
reed@google.com | 31b3044 | 2014-02-06 20:59:47 +0000 | [diff] [blame] | 20 | |
reed | c7141eb | 2016-01-11 13:08:59 -0800 | [diff] [blame] | 21 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 22 | |
| 23 | #ifndef SK_IGNORE_TO_STRING |
| 24 | void SkModeColorFilter::toString(SkString* str) const { |
| 25 | str->append("SkModeColorFilter: color: 0x"); |
| 26 | str->appendHex(fColor); |
| 27 | str->append(" mode: "); |
| 28 | str->append(SkXfermode::ModeName(fMode)); |
| 29 | } |
| 30 | #endif |
| 31 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 32 | bool SkModeColorFilter::asColorMode(SkColor* color, SK_XFERMODE_MODE_PARAM* mode) const { |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 33 | if (color) { |
| 34 | *color = fColor; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 35 | } |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 36 | if (mode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 37 | *mode = (SK_XFERMODE_MODE_PARAM)fMode; |
reed@google.com | 8b0d0f6 | 2012-06-04 18:10:33 +0000 | [diff] [blame] | 38 | } |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 39 | return true; |
| 40 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 41 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 42 | uint32_t SkModeColorFilter::getFlags() const { |
reed | c5b1228 | 2016-02-19 13:38:53 -0800 | [diff] [blame] | 43 | uint32_t flags = 0; |
reed | 62a320c | 2015-03-24 06:35:23 -0700 | [diff] [blame] | 44 | switch (fMode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 45 | case SkBlendMode::kDst: //!< [Da, Dc] |
| 46 | case SkBlendMode::kSrcATop: //!< [Da, Sc * Da + (1 - Sa) * Dc] |
reed | 3125565 | 2016-02-08 12:56:56 -0800 | [diff] [blame] | 47 | flags |= kAlphaUnchanged_Flag; |
reed | 62a320c | 2015-03-24 06:35:23 -0700 | [diff] [blame] | 48 | default: |
| 49 | break; |
| 50 | } |
reed | 3125565 | 2016-02-08 12:56:56 -0800 | [diff] [blame] | 51 | return flags; |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 52 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 53 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 54 | void SkModeColorFilter::filterSpan(const SkPMColor shader[], int count, SkPMColor result[]) const { |
| 55 | SkPMColor color = fPMColor; |
| 56 | SkXfermodeProc proc = fProc; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 57 | |
reed | 3125565 | 2016-02-08 12:56:56 -0800 | [diff] [blame] | 58 | for (int i = 0; i < count; i++) { |
| 59 | result[i] = proc(color, shader[i]); |
| 60 | } |
| 61 | } |
mtklein | 95cc012 | 2015-04-27 15:11:01 -0700 | [diff] [blame] | 62 | |
reed | 3125565 | 2016-02-08 12:56:56 -0800 | [diff] [blame] | 63 | void SkModeColorFilter::filterSpan4f(const SkPM4f shader[], int count, SkPM4f result[]) const { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 64 | SkXfermodeProc4f proc = SkXfermode::GetProc4f((SkXfermode::Mode)fMode); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 65 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 66 | for (int i = 0; i < count; i++) { |
Mike Klein | 96b333a | 2016-10-12 11:05:05 -0400 | [diff] [blame] | 67 | result[i] = proc(fPM4f, shader[i]); |
reed@google.com | 8b0d0f6 | 2012-06-04 18:10:33 +0000 | [diff] [blame] | 68 | } |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 69 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 70 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 71 | void SkModeColorFilter::flatten(SkWriteBuffer& buffer) const { |
| 72 | buffer.writeColor(fColor); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 73 | buffer.writeUInt((int)fMode); |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 74 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 75 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 76 | void SkModeColorFilter::updateCache() { |
| 77 | fPMColor = SkPreMultiplyColor(fColor); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 78 | fProc = SkXfermode::GetProc((SkXfermode::Mode)fMode); |
Matt Sarett | df3fbd6 | 2016-10-17 13:52:49 -0400 | [diff] [blame] | 79 | fPM4f = SkColor4f::FromColor(fColor).premul(); |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 80 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 82 | sk_sp<SkFlattenable> SkModeColorFilter::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 83 | SkColor color = buffer.readColor(); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 84 | SkBlendMode mode = (SkBlendMode)buffer.readUInt(); |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 85 | return SkColorFilter::MakeModeFilter(color, mode); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Mike Klein | 96b333a | 2016-10-12 11:05:05 -0400 | [diff] [blame] | 88 | bool SkModeColorFilter::onAppendStages(SkRasterPipeline* p) const { |
| 89 | // TODO: For some modes we can cut a stage by loading the fPM4f into dr,dg,db,da |
| 90 | // and applying the opposite xfermode, e.g. dst-in instead of src-in. |
| 91 | p->append(SkRasterPipeline::swap_src_dst); |
| 92 | p->append(SkRasterPipeline::constant_color, &fPM4f); |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 93 | auto mode = (SkBlendMode)fMode; |
| 94 | if (!SkBlendMode_AppendStages(mode, p)) { |
| 95 | return false; |
| 96 | } |
| 97 | if (SkBlendMode_CanOverflow(mode)) { p->append(SkRasterPipeline::clamp_1); } |
| 98 | return true; |
Mike Klein | 96b333a | 2016-10-12 11:05:05 -0400 | [diff] [blame] | 99 | } |
| 100 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 101 | /////////////////////////////////////////////////////////////////////////////// |
| 102 | #if SK_SUPPORT_GPU |
| 103 | #include "GrBlend.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 104 | #include "GrInvariantOutput.h" |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 105 | #include "effects/GrXfermodeFragmentProcessor.h" |
| 106 | #include "effects/GrConstColorProcessor.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 107 | #include "SkGr.h" |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 108 | |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 109 | sk_sp<GrFragmentProcessor> SkModeColorFilter::asFragmentProcessor( |
| 110 | GrContext*, SkColorSpace* dstColorSpace) const { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 111 | if (SkBlendMode::kDst == fMode) { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 112 | return nullptr; |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 113 | } |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 114 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 115 | sk_sp<GrFragmentProcessor> constFP( |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 116 | GrConstColorProcessor::Make(SkColorToPremulGrColor4f(fColor, dstColorSpace), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 117 | GrConstColorProcessor::kIgnore_InputMode)); |
| 118 | sk_sp<GrFragmentProcessor> fp( |
| 119 | GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(constFP), fMode)); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 120 | if (!fp) { |
| 121 | return nullptr; |
| 122 | } |
| 123 | #ifdef SK_DEBUG |
| 124 | // With a solid color input this should always be able to compute the blended color |
| 125 | // (at least for coeff modes) |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 126 | if ((unsigned)fMode <= (unsigned)SkBlendMode::kLastCoeffMode) { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 127 | static SkRandom gRand; |
| 128 | GrInvariantOutput io(GrPremulColor(gRand.nextU()), kRGBA_GrColorComponentFlags, |
| 129 | false); |
| 130 | fp->computeInvariantOutput(&io); |
| 131 | SkASSERT(io.validFlags() == kRGBA_GrColorComponentFlags); |
| 132 | } |
| 133 | #endif |
| 134 | return fp; |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | #endif |
| 138 | |
| 139 | /////////////////////////////////////////////////////////////////////////////// |
| 140 | |
reed | c7141eb | 2016-01-11 13:08:59 -0800 | [diff] [blame] | 141 | class Src_SkModeColorFilter final : public SkModeColorFilter { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | public: |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 143 | Src_SkModeColorFilter(SkColor color) : INHERITED(color, SkBlendMode::kSrc) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 145 | void filterSpan(const SkPMColor shader[], int count, SkPMColor result[]) const override { |
reed@google.com | 8b0d0f6 | 2012-06-04 18:10:33 +0000 | [diff] [blame] | 146 | sk_memset32(result, this->getPMColor(), count); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | } |
| 148 | |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 149 | private: |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 150 | typedef SkModeColorFilter INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
reed | c7141eb | 2016-01-11 13:08:59 -0800 | [diff] [blame] | 153 | class SrcOver_SkModeColorFilter final : public SkModeColorFilter { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | public: |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 155 | SrcOver_SkModeColorFilter(SkColor color) : INHERITED(color, SkBlendMode::kSrcOver) { } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 157 | void filterSpan(const SkPMColor shader[], int count, SkPMColor result[]) const override { |
mtklein | 95cc012 | 2015-04-27 15:11:01 -0700 | [diff] [blame] | 158 | SkBlitRow::Color32(result, shader, count, this->getPMColor()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | } |
| 160 | |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 161 | private: |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 162 | typedef SkModeColorFilter INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 165 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 167 | sk_sp<SkColorFilter> SkColorFilter::MakeModeFilter(SkColor color, SkBlendMode mode) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 168 | if (!SkIsValidMode(mode)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 169 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 170 | } |
| 171 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | unsigned alpha = SkColorGetA(color); |
| 173 | |
| 174 | // first collaps some modes if possible |
| 175 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 176 | if (SkBlendMode::kClear == mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | color = 0; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 178 | mode = SkBlendMode::kSrc; |
| 179 | } else if (SkBlendMode::kSrcOver == mode) { |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 180 | if (0 == alpha) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 181 | mode = SkBlendMode::kDst; |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 182 | } else if (255 == alpha) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 183 | mode = SkBlendMode::kSrc; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | } |
| 185 | // else just stay srcover |
| 186 | } |
| 187 | |
| 188 | // weed out combinations that are noops, and just return null |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 189 | if (SkBlendMode::kDst == mode || |
| 190 | (0 == alpha && (SkBlendMode::kSrcOver == mode || |
| 191 | SkBlendMode::kDstOver == mode || |
| 192 | SkBlendMode::kDstOut == mode || |
| 193 | SkBlendMode::kSrcATop == mode || |
| 194 | SkBlendMode::kXor == mode || |
| 195 | SkBlendMode::kDarken == mode)) || |
| 196 | (0xFF == alpha && SkBlendMode::kDstIn == mode)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 197 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | } |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 199 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | switch (mode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 201 | case SkBlendMode::kSrc: |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 202 | return sk_make_sp<Src_SkModeColorFilter>(color); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame^] | 203 | case SkBlendMode::kSrcOver: |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 204 | return sk_make_sp<SrcOver_SkModeColorFilter>(color); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 205 | default: |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 206 | return SkModeColorFilter::Make(color, mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | } |
| 208 | } |