robertphillips@google.com | b83b6b4 | 2013-01-22 14:32:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 8 | #include "SkArithmeticModePriv.h" |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 9 | #include "SkColorPriv.h" |
mtklein | d58f840 | 2016-04-10 06:23:28 -0700 | [diff] [blame] | 10 | #include "SkNx.h" |
mtklein | 05e3c39 | 2016-07-25 05:56:16 -0700 | [diff] [blame] | 11 | #include "SkRasterPipeline.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
robertphillips@google.com | b83b6b4 | 2013-01-22 14:32:09 +0000 | [diff] [blame] | 13 | #include "SkString.h" |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 14 | #include "SkUnPreMultiply.h" |
mtklein | d58f840 | 2016-04-10 06:23:28 -0700 | [diff] [blame] | 15 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | 9a6eb0e | 2013-05-29 20:55:09 +0000 | [diff] [blame] | 16 | #if SK_SUPPORT_GPU |
egdaniel | 16a04b8 | 2015-01-14 10:49:18 -0800 | [diff] [blame] | 17 | #include "SkArithmeticMode_gpu.h" |
senorblanco@chromium.org | 9a6eb0e | 2013-05-29 20:55:09 +0000 | [diff] [blame] | 18 | #endif |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 19 | |
| 20 | class SkArithmeticMode_scalar : public SkXfermode { |
| 21 | public: |
reed | 8781123 | 2016-02-22 06:59:37 -0800 | [diff] [blame] | 22 | SkArithmeticMode_scalar(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4, |
| 23 | bool enforcePMColor) { |
| 24 | fK[0] = k1; |
| 25 | fK[1] = k2; |
| 26 | fK[2] = k3; |
| 27 | fK[3] = k4; |
| 28 | fEnforcePMColor = enforcePMColor; |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 29 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 30 | |
reed | 8781123 | 2016-02-22 06:59:37 -0800 | [diff] [blame] | 31 | void xfer32(SkPMColor[], const SkPMColor[], int count, const SkAlpha[]) const override; |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 32 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 33 | SK_TO_STRING_OVERRIDE() |
senorblanco@chromium.org | 9a6eb0e | 2013-05-29 20:55:09 +0000 | [diff] [blame] | 34 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkArithmeticMode_scalar) |
| 35 | |
| 36 | #if SK_SUPPORT_GPU |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 37 | sk_sp<GrFragmentProcessor> makeFragmentProcessorForImageFilter( |
| 38 | sk_sp<GrFragmentProcessor> dst) const override; |
| 39 | sk_sp<GrXPFactory> asXPFactory() const override; |
senorblanco@chromium.org | 9a6eb0e | 2013-05-29 20:55:09 +0000 | [diff] [blame] | 40 | #endif |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 41 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 42 | bool isArithmetic(SkArithmeticParams* params) const override { |
| 43 | if (params) { |
| 44 | memcpy(params->fK, fK, 4 * sizeof(float)); |
| 45 | params->fEnforcePMColor = fEnforcePMColor; |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 50 | private: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 51 | void flatten(SkWriteBuffer& buffer) const override { |
senorblanco@chromium.org | 9a6eb0e | 2013-05-29 20:55:09 +0000 | [diff] [blame] | 52 | buffer.writeScalar(fK[0]); |
| 53 | buffer.writeScalar(fK[1]); |
| 54 | buffer.writeScalar(fK[2]); |
| 55 | buffer.writeScalar(fK[3]); |
commit-bot@chromium.org | 51a11b7 | 2014-04-02 19:32:15 +0000 | [diff] [blame] | 56 | buffer.writeBool(fEnforcePMColor); |
senorblanco@chromium.org | 9a6eb0e | 2013-05-29 20:55:09 +0000 | [diff] [blame] | 57 | } |
halcanary | 27a6e86 | 2016-01-21 14:15:10 -0800 | [diff] [blame] | 58 | |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 59 | SkScalar fK[4]; |
commit-bot@chromium.org | 51a11b7 | 2014-04-02 19:32:15 +0000 | [diff] [blame] | 60 | bool fEnforcePMColor; |
robertphillips@google.com | b83b6b4 | 2013-01-22 14:32:09 +0000 | [diff] [blame] | 61 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 62 | friend class SkArithmeticMode; |
| 63 | |
skia.committer@gmail.com | 98ded84 | 2013-01-23 07:06:17 +0000 | [diff] [blame] | 64 | typedef SkXfermode INHERITED; |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 67 | sk_sp<SkFlattenable> SkArithmeticMode_scalar::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 68 | const SkScalar k1 = buffer.readScalar(); |
| 69 | const SkScalar k2 = buffer.readScalar(); |
| 70 | const SkScalar k3 = buffer.readScalar(); |
| 71 | const SkScalar k4 = buffer.readScalar(); |
| 72 | const bool enforcePMColor = buffer.readBool(); |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 73 | return SkArithmeticMode::Make(k1, k2, k3, k4, enforcePMColor); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 74 | } |
| 75 | |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 76 | void SkArithmeticMode_scalar::xfer32(SkPMColor dst[], const SkPMColor src[], |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 77 | int count, const SkAlpha aaCoverage[]) const { |
mtklein | d58f840 | 2016-04-10 06:23:28 -0700 | [diff] [blame] | 78 | const Sk4f k1 = fK[0] * (1/255.0f), |
| 79 | k2 = fK[1], |
| 80 | k3 = fK[2], |
| 81 | k4 = fK[3] * 255.0f + 0.5f; |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 82 | |
mtklein | d58f840 | 2016-04-10 06:23:28 -0700 | [diff] [blame] | 83 | auto pin = [](float min, const Sk4f& val, float max) { |
| 84 | return Sk4f::Max(min, Sk4f::Min(val, max)); |
| 85 | }; |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 86 | |
mtklein | d58f840 | 2016-04-10 06:23:28 -0700 | [diff] [blame] | 87 | for (int i = 0; i < count; i++) { |
| 88 | if (aaCoverage && aaCoverage[i] == 0) { |
| 89 | continue; |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 90 | } |
mtklein | d58f840 | 2016-04-10 06:23:28 -0700 | [diff] [blame] | 91 | |
| 92 | Sk4f s = SkNx_cast<float>(Sk4b::Load(src+i)), |
| 93 | d = SkNx_cast<float>(Sk4b::Load(dst+i)), |
| 94 | r = pin(0, k1*s*d + k2*s + k3*d + k4, 255); |
| 95 | |
| 96 | if (fEnforcePMColor) { |
| 97 | Sk4f a = SkNx_shuffle<3,3,3,3>(r); |
| 98 | r = Sk4f::Min(a, r); |
| 99 | } |
| 100 | |
| 101 | if (aaCoverage && aaCoverage[i] != 255) { |
| 102 | Sk4f c = aaCoverage[i] * (1/255.0f); |
| 103 | r = d + (r-d)*c; |
| 104 | } |
| 105 | |
| 106 | SkNx_cast<uint8_t>(r).store(dst+i); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 110 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | b83b6b4 | 2013-01-22 14:32:09 +0000 | [diff] [blame] | 111 | void SkArithmeticMode_scalar::toString(SkString* str) const { |
| 112 | str->append("SkArithmeticMode_scalar: "); |
| 113 | for (int i = 0; i < 4; ++i) { |
| 114 | str->appendScalar(fK[i]); |
commit-bot@chromium.org | 51a11b7 | 2014-04-02 19:32:15 +0000 | [diff] [blame] | 115 | str->append(" "); |
robertphillips@google.com | b83b6b4 | 2013-01-22 14:32:09 +0000 | [diff] [blame] | 116 | } |
commit-bot@chromium.org | 51a11b7 | 2014-04-02 19:32:15 +0000 | [diff] [blame] | 117 | str->appendS32(fEnforcePMColor ? 1 : 0); |
robertphillips@google.com | b83b6b4 | 2013-01-22 14:32:09 +0000 | [diff] [blame] | 118 | } |
| 119 | #endif |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 120 | |
| 121 | /////////////////////////////////////////////////////////////////////////////// |
| 122 | |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 123 | sk_sp<SkXfermode> SkArithmeticMode::Make(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4, |
| 124 | bool enforcePMColor) { |
reed | 8781123 | 2016-02-22 06:59:37 -0800 | [diff] [blame] | 125 | if (SkScalarNearlyZero(k1) && SkScalarNearlyEqual(k2, SK_Scalar1) && |
| 126 | SkScalarNearlyZero(k3) && SkScalarNearlyZero(k4)) { |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 127 | return SkXfermode::Make(SkXfermode::kSrc_Mode); |
reed | 8781123 | 2016-02-22 06:59:37 -0800 | [diff] [blame] | 128 | } else if (SkScalarNearlyZero(k1) && SkScalarNearlyZero(k2) && |
| 129 | SkScalarNearlyEqual(k3, SK_Scalar1) && SkScalarNearlyZero(k4)) { |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 130 | return SkXfermode::Make(SkXfermode::kDst_Mode); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 131 | } |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 132 | return sk_make_sp<SkArithmeticMode_scalar>(k1, k2, k3, k4, enforcePMColor); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 133 | } |
senorblanco@chromium.org | 9a6eb0e | 2013-05-29 20:55:09 +0000 | [diff] [blame] | 134 | |
| 135 | |
| 136 | ////////////////////////////////////////////////////////////////////////////// |
| 137 | |
| 138 | #if SK_SUPPORT_GPU |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 139 | sk_sp<GrFragmentProcessor> SkArithmeticMode_scalar::makeFragmentProcessorForImageFilter( |
| 140 | sk_sp<GrFragmentProcessor> dst) const { |
| 141 | return GrArithmeticFP::Make(SkScalarToFloat(fK[0]), |
| 142 | SkScalarToFloat(fK[1]), |
| 143 | SkScalarToFloat(fK[2]), |
| 144 | SkScalarToFloat(fK[3]), |
| 145 | fEnforcePMColor, |
| 146 | std::move(dst)); |
senorblanco@chromium.org | 9a6eb0e | 2013-05-29 20:55:09 +0000 | [diff] [blame] | 147 | } |
| 148 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 149 | sk_sp<GrXPFactory> SkArithmeticMode_scalar::asXPFactory() const { |
| 150 | return GrArithmeticXPFactory::Make(SkScalarToFloat(fK[0]), |
| 151 | SkScalarToFloat(fK[1]), |
| 152 | SkScalarToFloat(fK[2]), |
| 153 | SkScalarToFloat(fK[3]), |
| 154 | fEnforcePMColor); |
egdaniel | f351aa3 | 2015-01-14 12:53:01 -0800 | [diff] [blame] | 155 | } |
| 156 | |
senorblanco@chromium.org | 9a6eb0e | 2013-05-29 20:55:09 +0000 | [diff] [blame] | 157 | #endif |
| 158 | |
| 159 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkArithmeticMode) |
| 160 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkArithmeticMode_scalar) |
| 161 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |