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 | |
Mike Klein | 4e92b82 | 2017-04-26 13:45:36 -0400 | [diff] [blame] | 8 | #include "SkArenaAlloc.h" |
Mike Klein | e902f8d | 2016-10-26 15:32:26 -0400 | [diff] [blame] | 9 | #include "SkBlendModePriv.h" |
Mike Klein | 4e92b82 | 2017-04-26 13:45:36 -0400 | [diff] [blame] | 10 | #include "SkBlitRow.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkColorFilter.h" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 12 | #include "SkColorData.h" |
Mike Klein | 4e92b82 | 2017-04-26 13:45:36 -0400 | [diff] [blame] | 13 | #include "SkColorSpaceXformer.h" |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 14 | #include "SkModeColorFilter.h" |
Mike Klein | 4e92b82 | 2017-04-26 13:45:36 -0400 | [diff] [blame] | 15 | #include "SkPM4f.h" |
Mike Klein | 744908e | 2016-11-11 12:51:36 -0500 | [diff] [blame] | 16 | #include "SkPM4fPriv.h" |
Mike Klein | 4e92b82 | 2017-04-26 13:45:36 -0400 | [diff] [blame] | 17 | #include "SkRandom.h" |
Mike Klein | 96b333a | 2016-10-12 11:05:05 -0400 | [diff] [blame] | 18 | #include "SkRasterPipeline.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 19 | #include "SkReadBuffer.h" |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 20 | #include "SkString.h" |
Mike Klein | 4e92b82 | 2017-04-26 13:45:36 -0400 | [diff] [blame] | 21 | #include "SkUtils.h" |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 22 | #include "SkValidationUtils.h" |
Mike Klein | 4e92b82 | 2017-04-26 13:45:36 -0400 | [diff] [blame] | 23 | #include "SkWriteBuffer.h" |
reed@google.com | 31b3044 | 2014-02-06 20:59:47 +0000 | [diff] [blame] | 24 | |
reed | c7141eb | 2016-01-11 13:08:59 -0800 | [diff] [blame] | 25 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 26 | |
Mike Reed | ce9514c | 2017-06-11 22:14:14 -0400 | [diff] [blame] | 27 | SkModeColorFilter::SkModeColorFilter(SkColor color, SkBlendMode mode) { |
| 28 | fColor = color; |
| 29 | fMode = mode; |
| 30 | // cache |
| 31 | fPMColor = SkPreMultiplyColor(fColor); |
| 32 | } |
| 33 | |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 34 | bool SkModeColorFilter::asColorMode(SkColor* color, SkBlendMode* mode) const { |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 35 | if (color) { |
| 36 | *color = fColor; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 37 | } |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 38 | if (mode) { |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 39 | *mode = fMode; |
reed@google.com | 8b0d0f6 | 2012-06-04 18:10:33 +0000 | [diff] [blame] | 40 | } |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 41 | return true; |
| 42 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 43 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 44 | uint32_t SkModeColorFilter::getFlags() const { |
reed | c5b1228 | 2016-02-19 13:38:53 -0800 | [diff] [blame] | 45 | uint32_t flags = 0; |
reed | 62a320c | 2015-03-24 06:35:23 -0700 | [diff] [blame] | 46 | switch (fMode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 47 | case SkBlendMode::kDst: //!< [Da, Dc] |
| 48 | case SkBlendMode::kSrcATop: //!< [Da, Sc * Da + (1 - Sa) * Dc] |
reed | 3125565 | 2016-02-08 12:56:56 -0800 | [diff] [blame] | 49 | flags |= kAlphaUnchanged_Flag; |
reed | 62a320c | 2015-03-24 06:35:23 -0700 | [diff] [blame] | 50 | default: |
| 51 | break; |
| 52 | } |
reed | 3125565 | 2016-02-08 12:56:56 -0800 | [diff] [blame] | 53 | return flags; |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 54 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 55 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 56 | void SkModeColorFilter::flatten(SkWriteBuffer& buffer) const { |
| 57 | buffer.writeColor(fColor); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 58 | buffer.writeUInt((int)fMode); |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 59 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 60 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 61 | sk_sp<SkFlattenable> SkModeColorFilter::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 62 | SkColor color = buffer.readColor(); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 63 | SkBlendMode mode = (SkBlendMode)buffer.readUInt(); |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 64 | return SkColorFilter::MakeModeFilter(color, mode); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Mike Klein | fdf3103 | 2017-05-09 14:57:58 -0400 | [diff] [blame] | 67 | void SkModeColorFilter::onAppendStages(SkRasterPipeline* p, |
Mike Klein | 744908e | 2016-11-11 12:51:36 -0500 | [diff] [blame] | 68 | SkColorSpace* dst, |
Herb Derby | ac04fef | 2017-01-13 17:34:33 -0500 | [diff] [blame] | 69 | SkArenaAlloc* scratch, |
Mike Klein | 744908e | 2016-11-11 12:51:36 -0500 | [diff] [blame] | 70 | bool shaderIsOpaque) const { |
Mike Klein | c509341 | 2016-11-04 16:36:39 -0400 | [diff] [blame] | 71 | p->append(SkRasterPipeline::move_src_dst); |
Mike Klein | 073073e | 2017-08-03 09:42:53 -0400 | [diff] [blame] | 72 | p->append_constant_color(scratch, SkPM4f_from_SkColor(fColor, dst)); |
Mike Klein | cb3ceb7 | 2017-06-09 15:58:37 -0400 | [diff] [blame] | 73 | SkBlendMode_AppendStages(fMode, p); |
Mike Klein | 96b333a | 2016-10-12 11:05:05 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Mike Klein | 4e92b82 | 2017-04-26 13:45:36 -0400 | [diff] [blame] | 76 | sk_sp<SkColorFilter> SkModeColorFilter::onMakeColorSpace(SkColorSpaceXformer* xformer) const { |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame] | 77 | SkColor color = xformer->apply(fColor); |
| 78 | if (color != fColor) { |
| 79 | return SkColorFilter::MakeModeFilter(color, fMode); |
| 80 | } |
| 81 | return this->INHERITED::onMakeColorSpace(xformer); |
Mike Klein | 4e92b82 | 2017-04-26 13:45:36 -0400 | [diff] [blame] | 82 | } |
| 83 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 84 | /////////////////////////////////////////////////////////////////////////////// |
| 85 | #if SK_SUPPORT_GPU |
| 86 | #include "GrBlend.h" |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 87 | #include "effects/GrXfermodeFragmentProcessor.h" |
| 88 | #include "effects/GrConstColorProcessor.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 89 | #include "SkGr.h" |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 90 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 91 | std::unique_ptr<GrFragmentProcessor> SkModeColorFilter::asFragmentProcessor( |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 92 | GrContext*, const GrColorSpaceInfo& dstColorSpaceInfo) const { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 93 | if (SkBlendMode::kDst == fMode) { |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 94 | return nullptr; |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 95 | } |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 96 | |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 97 | auto constFP = GrConstColorProcessor::Make(SkColorToPremulGrColor4f(fColor, dstColorSpaceInfo), |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 98 | GrConstColorProcessor::InputMode::kIgnore); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 99 | auto fp = GrXfermodeFragmentProcessor::MakeFromSrcProcessor(std::move(constFP), fMode); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 100 | if (!fp) { |
| 101 | return nullptr; |
| 102 | } |
| 103 | #ifdef SK_DEBUG |
| 104 | // With a solid color input this should always be able to compute the blended color |
| 105 | // (at least for coeff modes) |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 106 | if ((unsigned)fMode <= (unsigned)SkBlendMode::kLastCoeffMode) { |
Brian Salomon | cb30bb2 | 2017-02-12 09:28:54 -0500 | [diff] [blame] | 107 | SkASSERT(fp->hasConstantOutputForConstantInput()); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 108 | } |
| 109 | #endif |
| 110 | return fp; |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | #endif |
| 114 | |
| 115 | /////////////////////////////////////////////////////////////////////////////// |
| 116 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 117 | sk_sp<SkColorFilter> SkColorFilter::MakeModeFilter(SkColor color, SkBlendMode mode) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 118 | if (!SkIsValidMode(mode)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 119 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 120 | } |
| 121 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | unsigned alpha = SkColorGetA(color); |
| 123 | |
| 124 | // first collaps some modes if possible |
| 125 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 126 | if (SkBlendMode::kClear == mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | color = 0; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 128 | mode = SkBlendMode::kSrc; |
| 129 | } else if (SkBlendMode::kSrcOver == mode) { |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 130 | if (0 == alpha) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 131 | mode = SkBlendMode::kDst; |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 132 | } else if (255 == alpha) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 133 | mode = SkBlendMode::kSrc; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | } |
| 135 | // else just stay srcover |
| 136 | } |
| 137 | |
| 138 | // weed out combinations that are noops, and just return null |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 139 | if (SkBlendMode::kDst == mode || |
| 140 | (0 == alpha && (SkBlendMode::kSrcOver == mode || |
| 141 | SkBlendMode::kDstOver == mode || |
| 142 | SkBlendMode::kDstOut == mode || |
| 143 | SkBlendMode::kSrcATop == mode || |
| 144 | SkBlendMode::kXor == mode || |
| 145 | SkBlendMode::kDarken == mode)) || |
| 146 | (0xFF == alpha && SkBlendMode::kDstIn == mode)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 147 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | } |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 149 | |
Mike Reed | e8f2881 | 2017-06-12 21:58:03 -0400 | [diff] [blame] | 150 | return SkModeColorFilter::Make(color, mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | } |