senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 The Android Open Source Project |
| 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 "SkColorFilterImageFilter.h" |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 9 | |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 11 | #include "SkColorFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 13 | #include "SkSpecialImage.h" |
| 14 | #include "SkSpecialSurface.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 15 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 16 | |
reed | f028003 | 2015-10-12 11:10:10 -0700 | [diff] [blame] | 17 | SkImageFilter* SkColorFilterImageFilter::Create(SkColorFilter* cf, SkImageFilter* input, |
| 18 | const CropRect* cropRect) { |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 19 | if (!cf) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 20 | return nullptr; |
sugoi | 9bde31e | 2014-10-30 14:05:14 -0700 | [diff] [blame] | 21 | } |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 22 | |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 23 | SkColorFilter* inputCF; |
| 24 | if (input && input->isColorFilterNode(&inputCF)) { |
| 25 | // This is an optimization, as it collapses the hierarchy by just combining the two |
| 26 | // colorfilters into a single one, which the new imagefilter will wrap. |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 27 | sk_sp<SkColorFilter> newCF(SkColorFilter::MakeComposeFilter(sk_ref_sp(cf), |
| 28 | sk_sp<SkColorFilter>(inputCF))); |
reed | 8a8d841 | 2015-03-02 13:46:03 -0800 | [diff] [blame] | 29 | if (newCF) { |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 30 | return new SkColorFilterImageFilter(newCF.get(), input->getInput(0), cropRect); |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 31 | } |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 32 | } |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 33 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 34 | return new SkColorFilterImageFilter(cf, input, cropRect); |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 35 | } |
| 36 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 37 | SkColorFilterImageFilter::SkColorFilterImageFilter(SkColorFilter* cf, |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 38 | SkImageFilter* input, |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 39 | const CropRect* cropRect) |
| 40 | : INHERITED(1, &input, cropRect) |
| 41 | , fColorFilter(SkRef(cf)) { |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 42 | } |
| 43 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 44 | SkFlattenable* SkColorFilterImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 45 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 46 | sk_sp<SkColorFilter> cf(buffer.readColorFilter()); |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame^] | 47 | return Create(cf.get(), common.getInput(0).get(), &common.cropRect()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 48 | } |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 49 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 50 | void SkColorFilterImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 51 | this->INHERITED::flatten(buffer); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 52 | buffer.writeFlattenable(fColorFilter.get()); |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 53 | } |
| 54 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 55 | sk_sp<SkSpecialImage> SkColorFilterImageFilter::onFilterImage(SkSpecialImage* source, |
| 56 | const Context& ctx, |
| 57 | SkIPoint* offset) const { |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 58 | SkIPoint inputOffset = SkIPoint::Make(0, 0); |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 59 | sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset)); |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 60 | if (!input) { |
| 61 | return nullptr; |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 62 | } |
| 63 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 64 | SkIRect bounds; |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 65 | const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.fX, inputOffset.fY, |
| 66 | input->width(), input->height()); |
| 67 | if (!this->applyCropRect(ctx, inputBounds, &bounds)) { |
| 68 | return nullptr; |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 69 | } |
| 70 | |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 71 | SkImageInfo info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), kPremul_SkAlphaType); |
| 72 | sk_sp<SkSpecialSurface> surf(source->makeSurface(info)); |
| 73 | if (!surf) { |
| 74 | return nullptr; |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 75 | } |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 76 | |
| 77 | SkCanvas* canvas = surf->getCanvas(); |
| 78 | SkASSERT(canvas); |
| 79 | |
| 80 | // TODO: it seems like this clear shouldn't be necessary (see skbug.com/5075) |
| 81 | canvas->clear(0x0); |
| 82 | |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 83 | SkPaint paint; |
| 84 | |
| 85 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 86 | paint.setColorFilter(fColorFilter); |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 87 | |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 88 | input->draw(canvas, |
| 89 | SkIntToScalar(inputOffset.fX - bounds.fLeft), |
| 90 | SkIntToScalar(inputOffset.fY - bounds.fTop), |
| 91 | &paint); |
| 92 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 93 | offset->fX = bounds.fLeft; |
| 94 | offset->fY = bounds.fTop; |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 95 | return surf->makeImageSnapshot(); |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 96 | } |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 97 | |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 98 | bool SkColorFilterImageFilter::onIsColorFilterNode(SkColorFilter** filter) const { |
| 99 | SkASSERT(1 == this->countInputs()); |
| 100 | if (!this->cropRectIsSet()) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 101 | if (filter) { |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 102 | *filter = SkRef(fColorFilter.get()); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 103 | } |
| 104 | return true; |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 105 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 106 | return false; |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 107 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 108 | |
senorblanco | a544eda | 2015-12-07 07:48:34 -0800 | [diff] [blame] | 109 | bool SkColorFilterImageFilter::canComputeFastBounds() const { |
| 110 | if (fColorFilter->affectsTransparentBlack()) { |
| 111 | return false; |
| 112 | } |
| 113 | return INHERITED::canComputeFastBounds(); |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 114 | } |
| 115 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 116 | #ifndef SK_IGNORE_TO_STRING |
| 117 | void SkColorFilterImageFilter::toString(SkString* str) const { |
| 118 | str->appendf("SkColorFilterImageFilter: ("); |
| 119 | |
robertphillips | 33cca88 | 2014-12-22 06:52:04 -0800 | [diff] [blame] | 120 | str->appendf("input: ("); |
| 121 | |
| 122 | if (this->getInput(0)) { |
| 123 | this->getInput(0)->toString(str); |
| 124 | } |
| 125 | |
| 126 | str->appendf(") color filter: "); |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 127 | fColorFilter->toString(str); |
| 128 | |
| 129 | str->append(")"); |
| 130 | } |
| 131 | #endif |