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" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 11 | #include "SkColorMatrixFilter.h" |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 12 | #include "SkDevice.h" |
| 13 | #include "SkColorFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 14 | #include "SkReadBuffer.h" |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 15 | #include "SkTableColorFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 16 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 17 | |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 18 | SkColorFilterImageFilter* SkColorFilterImageFilter::Create(SkColorFilter* cf, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 19 | SkImageFilter* input, const CropRect* cropRect) { |
sugoi | 9bde31e | 2014-10-30 14:05:14 -0700 | [diff] [blame] | 20 | if (NULL == cf) { |
| 21 | return NULL; |
| 22 | } |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 23 | |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 24 | SkColorFilter* inputCF; |
| 25 | if (input && input->isColorFilterNode(&inputCF)) { |
| 26 | // This is an optimization, as it collapses the hierarchy by just combining the two |
| 27 | // colorfilters into a single one, which the new imagefilter will wrap. |
| 28 | SkAutoUnref autoUnref(inputCF); |
| 29 | SkAutoTUnref<SkColorFilter> newCF(SkColorFilter::CreateComposeFilter(cf, inputCF)); |
reed | 8a8d841 | 2015-03-02 13:46:03 -0800 | [diff] [blame] | 30 | if (newCF) { |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 31 | return SkNEW_ARGS(SkColorFilterImageFilter, (newCF, input->getInput(0), cropRect)); |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 32 | } |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 33 | } |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 34 | |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 35 | return SkNEW_ARGS(SkColorFilterImageFilter, (cf, input, cropRect)); |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 36 | } |
| 37 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 38 | SkColorFilterImageFilter::SkColorFilterImageFilter(SkColorFilter* cf, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 39 | SkImageFilter* input, const CropRect* cropRect) |
| 40 | : INHERITED(1, &input, cropRect), fColorFilter(SkRef(cf)) { |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 41 | } |
| 42 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 43 | SkFlattenable* SkColorFilterImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 44 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
reed | f0f6c40 | 2014-08-22 05:08:26 -0700 | [diff] [blame] | 45 | SkAutoTUnref<SkColorFilter> cf(buffer.readColorFilter()); |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 46 | return Create(cf, common.getInput(0), &common.cropRect()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 47 | } |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 48 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 49 | void SkColorFilterImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 50 | this->INHERITED::flatten(buffer); |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 51 | buffer.writeFlattenable(fColorFilter); |
| 52 | } |
| 53 | |
| 54 | SkColorFilterImageFilter::~SkColorFilterImageFilter() { |
sugoi | 9bde31e | 2014-10-30 14:05:14 -0700 | [diff] [blame] | 55 | fColorFilter->unref(); |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool SkColorFilterImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& source, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 59 | const Context& ctx, |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 60 | SkBitmap* result, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 61 | SkIPoint* offset) const { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 62 | SkBitmap src = source; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 63 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 64 | if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctx, &src, &srcOffset)) { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 68 | SkIRect bounds; |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 69 | if (!this->applyCropRect(ctx, src, srcOffset, &bounds)) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 73 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 74 | if (NULL == device.get()) { |
| 75 | return false; |
| 76 | } |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 77 | SkCanvas canvas(device.get()); |
| 78 | SkPaint paint; |
| 79 | |
| 80 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 81 | paint.setColorFilter(fColorFilter); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 82 | canvas.drawSprite(src, srcOffset.fX - bounds.fLeft, srcOffset.fY - bounds.fTop, &paint); |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 83 | |
| 84 | *result = device.get()->accessBitmap(false); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 85 | offset->fX = bounds.fLeft; |
| 86 | offset->fY = bounds.fTop; |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 87 | return true; |
| 88 | } |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 89 | |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 90 | bool SkColorFilterImageFilter::onIsColorFilterNode(SkColorFilter** filter) const { |
| 91 | SkASSERT(1 == this->countInputs()); |
| 92 | if (!this->cropRectIsSet()) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 93 | if (filter) { |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 94 | *filter = SkRef(fColorFilter); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 95 | } |
| 96 | return true; |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 97 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 98 | return false; |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 99 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 100 | |
| 101 | #ifndef SK_IGNORE_TO_STRING |
| 102 | void SkColorFilterImageFilter::toString(SkString* str) const { |
| 103 | str->appendf("SkColorFilterImageFilter: ("); |
| 104 | |
robertphillips | 33cca88 | 2014-12-22 06:52:04 -0800 | [diff] [blame] | 105 | str->appendf("input: ("); |
| 106 | |
| 107 | if (this->getInput(0)) { |
| 108 | this->getInput(0)->toString(str); |
| 109 | } |
| 110 | |
| 111 | str->appendf(") color filter: "); |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 112 | fColorFilter->toString(str); |
| 113 | |
| 114 | str->append(")"); |
| 115 | } |
| 116 | #endif |