Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
| 8 | #include "SkColorFilter.h" |
| 9 | #include "SkColorSpaceXformer.h" |
| 10 | #include "SkColorSpaceXform_Base.h" |
| 11 | #include "SkDrawLooper.h" |
| 12 | #include "SkGradientShader.h" |
| 13 | #include "SkImage_Base.h" |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 14 | #include "SkImageFilter.h" |
Matt Sarett | e684483 | 2017-04-03 10:35:42 -0400 | [diff] [blame] | 15 | #include "SkImagePriv.h" |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 16 | #include "SkMakeUnique.h" |
| 17 | |
| 18 | std::unique_ptr<SkColorSpaceXformer> SkColorSpaceXformer::Make(sk_sp<SkColorSpace> dst) { |
| 19 | std::unique_ptr<SkColorSpaceXform> fromSRGB = SkColorSpaceXform_Base::New( |
| 20 | SkColorSpace::MakeSRGB().get(), dst.get(), SkTransferFunctionBehavior::kIgnore); |
| 21 | if (!fromSRGB) { |
| 22 | return nullptr; |
| 23 | } |
| 24 | |
| 25 | auto xformer = std::unique_ptr<SkColorSpaceXformer>(new SkColorSpaceXformer()); |
| 26 | xformer->fDst = std::move(dst); |
| 27 | xformer->fFromSRGB = std::move(fromSRGB); |
| 28 | return xformer; |
| 29 | } |
| 30 | |
| 31 | sk_sp<SkImage> SkColorSpaceXformer::apply(const SkImage* src) { |
Matt Sarett | cb87423 | 2017-04-05 11:41:27 -0400 | [diff] [blame] | 32 | return src->makeColorSpace(fDst, SkTransferFunctionBehavior::kIgnore); |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 33 | } |
| 34 | |
Matt Sarett | e684483 | 2017-04-03 10:35:42 -0400 | [diff] [blame] | 35 | sk_sp<SkImage> SkColorSpaceXformer::apply(const SkBitmap& src) { |
| 36 | sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(src, kNever_SkCopyPixelsMode); |
| 37 | if (!image) { |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
Matt Sarett | cb87423 | 2017-04-05 11:41:27 -0400 | [diff] [blame] | 41 | sk_sp<SkImage> xformed = image->makeColorSpace(fDst, SkTransferFunctionBehavior::kIgnore); |
Matt Sarett | e684483 | 2017-04-03 10:35:42 -0400 | [diff] [blame] | 42 | // We want to be sure we don't let the kNever_SkCopyPixelsMode image escape this stack frame. |
| 43 | SkASSERT(xformed != image); |
| 44 | return xformed; |
| 45 | } |
| 46 | |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame^] | 47 | // As far as I know, SkModeColorFilter is the only color filter that holds a color. |
| 48 | sk_sp<SkColorFilter> SkColorSpaceXformer::apply(const SkColorFilter* colorFilter) { |
| 49 | SkColor color; |
| 50 | SkBlendMode mode; |
| 51 | if (colorFilter->asColorMode(&color, &mode)) { |
| 52 | return SkColorFilter::MakeModeFilter(this->apply(color), mode); |
| 53 | } |
| 54 | |
| 55 | return sk_ref_sp(const_cast<SkColorFilter*>(colorFilter)); |
| 56 | } |
| 57 | |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 58 | void SkColorSpaceXformer::apply(SkColor* xformed, const SkColor* srgb, int n) { |
| 59 | SkAssertResult(fFromSRGB->apply(SkColorSpaceXform::kBGRA_8888_ColorFormat, xformed, |
| 60 | SkColorSpaceXform::kBGRA_8888_ColorFormat, srgb, |
| 61 | n, kUnpremul_SkAlphaType)); |
| 62 | } |
| 63 | |
| 64 | SkColor SkColorSpaceXformer::apply(SkColor srgb) { |
| 65 | SkColor xformed; |
| 66 | this->apply(&xformed, &srgb, 1); |
| 67 | return xformed; |
| 68 | } |
| 69 | |
| 70 | // TODO: Is this introspection going to be enough, or do we need a new SkShader method? |
| 71 | sk_sp<SkShader> SkColorSpaceXformer::apply(const SkShader* shader) { |
| 72 | SkColor color; |
| 73 | if (shader->isConstant() && shader->asLuminanceColor(&color)) { |
| 74 | return SkShader::MakeColorShader(this->apply(color)); |
| 75 | } |
| 76 | |
| 77 | SkShader::TileMode xy[2]; |
| 78 | SkMatrix local; |
| 79 | if (auto img = shader->isAImage(&local, xy)) { |
| 80 | return this->apply(img)->makeShader(xy[0], xy[1], &local); |
| 81 | } |
| 82 | |
| 83 | SkShader::ComposeRec compose; |
| 84 | if (shader->asACompose(&compose)) { |
| 85 | auto A = this->apply(compose.fShaderA), |
| 86 | B = this->apply(compose.fShaderB); |
| 87 | if (A && B) { |
| 88 | return SkShader::MakeComposeShader(std::move(A), std::move(B), compose.fBlendMode); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | SkShader::GradientInfo gradient; |
| 93 | sk_bzero(&gradient, sizeof(gradient)); |
| 94 | if (auto type = shader->asAGradient(&gradient)) { |
| 95 | SkSTArray<8, SkColor> colors(gradient.fColorCount); |
| 96 | SkSTArray<8, SkScalar> pos(gradient.fColorCount); |
| 97 | |
| 98 | gradient.fColors = colors.begin(); |
| 99 | gradient.fColorOffsets = pos.begin(); |
| 100 | shader->asAGradient(&gradient); |
| 101 | |
| 102 | SkSTArray<8, SkColor> xformed(gradient.fColorCount); |
| 103 | this->apply(xformed.begin(), gradient.fColors, gradient.fColorCount); |
| 104 | |
| 105 | switch (type) { |
| 106 | case SkShader::kNone_GradientType: |
| 107 | case SkShader::kColor_GradientType: |
| 108 | SkASSERT(false); // Should be unreachable. |
| 109 | break; |
| 110 | |
| 111 | case SkShader::kLinear_GradientType: |
| 112 | return SkGradientShader::MakeLinear(gradient.fPoint, |
| 113 | xformed.begin(), |
| 114 | gradient.fColorOffsets, |
| 115 | gradient.fColorCount, |
| 116 | gradient.fTileMode, |
| 117 | gradient.fGradientFlags, |
| 118 | &shader->getLocalMatrix()); |
| 119 | case SkShader::kRadial_GradientType: |
| 120 | return SkGradientShader::MakeRadial(gradient.fPoint[0], |
| 121 | gradient.fRadius[0], |
| 122 | xformed.begin(), |
| 123 | gradient.fColorOffsets, |
| 124 | gradient.fColorCount, |
| 125 | gradient.fTileMode, |
| 126 | gradient.fGradientFlags, |
| 127 | &shader->getLocalMatrix()); |
| 128 | case SkShader::kSweep_GradientType: |
| 129 | return SkGradientShader::MakeSweep(gradient.fPoint[0].fX, |
| 130 | gradient.fPoint[0].fY, |
| 131 | xformed.begin(), |
| 132 | gradient.fColorOffsets, |
| 133 | gradient.fColorCount, |
| 134 | gradient.fGradientFlags, |
| 135 | &shader->getLocalMatrix()); |
| 136 | case SkShader::kConical_GradientType: |
| 137 | return SkGradientShader::MakeTwoPointConical(gradient.fPoint[0], |
| 138 | gradient.fRadius[0], |
| 139 | gradient.fPoint[1], |
| 140 | gradient.fRadius[1], |
| 141 | xformed.begin(), |
| 142 | gradient.fColorOffsets, |
| 143 | gradient.fColorCount, |
| 144 | gradient.fTileMode, |
| 145 | gradient.fGradientFlags, |
| 146 | &shader->getLocalMatrix()); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return nullptr; |
| 151 | } |
| 152 | |
| 153 | const SkPaint& SkColorSpaceXformer::apply(const SkPaint& src) { |
| 154 | const SkPaint* result = &src; |
| 155 | auto get_dst = [&] { |
| 156 | if (result == &src) { |
| 157 | fDstPaint = src; |
| 158 | result = &fDstPaint; |
| 159 | } |
| 160 | return &fDstPaint; |
| 161 | }; |
| 162 | |
| 163 | // All SkColorSpaces have the same black point. |
| 164 | if (src.getColor() & 0xffffff) { |
| 165 | get_dst()->setColor(this->apply(src.getColor())); |
| 166 | } |
| 167 | |
| 168 | if (auto shader = src.getShader()) { |
| 169 | if (auto replacement = this->apply(shader)) { |
| 170 | get_dst()->setShader(std::move(replacement)); |
| 171 | } |
| 172 | } |
| 173 | |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 174 | if (auto cf = src.getColorFilter()) { |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame^] | 175 | auto replacement = this->apply(cf); |
| 176 | if (replacement.get() != cf) { |
| 177 | get_dst()->setColorFilter(std::move(replacement)); |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | if (auto looper = src.getDrawLooper()) { |
| 182 | get_dst()->setDrawLooper(looper->makeColorSpace(this)); |
| 183 | } |
| 184 | |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 185 | if (auto imageFilter = src.getImageFilter()) { |
| 186 | get_dst()->setImageFilter(imageFilter->makeColorSpace(this)); |
| 187 | } |
| 188 | |
Matt Sarett | cdc651d | 2017-03-30 12:41:48 -0400 | [diff] [blame] | 189 | return *result; |
| 190 | } |
| 191 | |
| 192 | const SkPaint* SkColorSpaceXformer::apply(const SkPaint* src) { |
| 193 | return src ? &this->apply(*src) : nullptr; |
| 194 | } |