ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "SkPaintImageFilter.h" |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 9 | #include "SkCanvas.h" |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 10 | #include "SkColorSpaceXformer.h" |
Cary Clark | 60aaeb2 | 2017-11-03 08:06:09 -0400 | [diff] [blame] | 11 | #include "SkImageFilterPriv.h" |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
robertphillips | e6163bf | 2016-03-09 10:17:41 -0800 | [diff] [blame] | 13 | #include "SkSpecialImage.h" |
| 14 | #include "SkSpecialSurface.h" |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 15 | #include "SkWriteBuffer.h" |
| 16 | |
robertphillips | 225db44 | 2016-04-17 14:27:05 -0700 | [diff] [blame] | 17 | sk_sp<SkImageFilter> SkPaintImageFilter::Make(const SkPaint& paint, |
| 18 | const CropRect* cropRect) { |
| 19 | return sk_sp<SkImageFilter>(new SkPaintImageFilter(paint, cropRect)); |
| 20 | } |
| 21 | |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 22 | SkPaintImageFilter::SkPaintImageFilter(const SkPaint& paint, const CropRect* cropRect) |
robertphillips | 372177e | 2016-03-30 07:32:28 -0700 | [diff] [blame] | 23 | : INHERITED(nullptr, 0, cropRect) |
| 24 | , fPaint(paint) { |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 25 | } |
| 26 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 27 | sk_sp<SkFlattenable> SkPaintImageFilter::CreateProc(SkReadBuffer& buffer) { |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 28 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); |
| 29 | SkPaint paint; |
| 30 | buffer.readPaint(&paint); |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 31 | return SkPaintImageFilter::Make(paint, &common.cropRect()); |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void SkPaintImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 35 | this->INHERITED::flatten(buffer); |
| 36 | buffer.writePaint(fPaint); |
| 37 | } |
| 38 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 39 | sk_sp<SkSpecialImage> SkPaintImageFilter::onFilterImage(SkSpecialImage* source, |
| 40 | const Context& ctx, |
| 41 | SkIPoint* offset) const { |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 42 | SkIRect bounds; |
robertphillips | e6163bf | 2016-03-09 10:17:41 -0800 | [diff] [blame] | 43 | const SkIRect srcBounds = SkIRect::MakeWH(source->width(), source->height()); |
| 44 | if (!this->applyCropRect(ctx, srcBounds, &bounds)) { |
| 45 | return nullptr; |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 46 | } |
| 47 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 48 | sk_sp<SkSpecialSurface> surf(source->makeSurface(ctx.outputProperties(), bounds.size())); |
robertphillips | e6163bf | 2016-03-09 10:17:41 -0800 | [diff] [blame] | 49 | if (!surf) { |
| 50 | return nullptr; |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 51 | } |
robertphillips | e6163bf | 2016-03-09 10:17:41 -0800 | [diff] [blame] | 52 | |
| 53 | SkCanvas* canvas = surf->getCanvas(); |
| 54 | SkASSERT(canvas); |
| 55 | |
| 56 | canvas->clear(0x0); |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 57 | |
| 58 | SkMatrix matrix(ctx.ctm()); |
| 59 | matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())); |
robertphillips | e6163bf | 2016-03-09 10:17:41 -0800 | [diff] [blame] | 60 | SkRect rect = SkRect::MakeIWH(bounds.width(), bounds.height()); |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 61 | SkMatrix inverse; |
| 62 | if (matrix.invert(&inverse)) { |
| 63 | inverse.mapRect(&rect); |
| 64 | } |
robertphillips | e6163bf | 2016-03-09 10:17:41 -0800 | [diff] [blame] | 65 | canvas->setMatrix(matrix); |
| 66 | canvas->drawRect(rect, fPaint); |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 67 | |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 68 | offset->fX = bounds.fLeft; |
| 69 | offset->fY = bounds.fTop; |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 70 | return surf->makeImageSnapshot(); |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 73 | sk_sp<SkImageFilter> SkPaintImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer) const { |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame] | 74 | SkPaint paint = xformer->apply(fPaint); |
| 75 | if (paint != fPaint) { |
| 76 | return SkPaintImageFilter::Make(paint, this->getCropRectIfSet()); |
| 77 | } |
| 78 | return this->refMe(); |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 79 | } |
| 80 | |
senorblanco | 6db0a7b | 2016-04-01 16:41:10 -0700 | [diff] [blame] | 81 | bool SkPaintImageFilter::affectsTransparentBlack() const { |
| 82 | return true; |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | #ifndef SK_IGNORE_TO_STRING |
| 86 | void SkPaintImageFilter::toString(SkString* str) const { |
| 87 | str->appendf("SkPaintImageFilter: ("); |
robertphillips | 372177e | 2016-03-30 07:32:28 -0700 | [diff] [blame] | 88 | fPaint.toString(str); |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 89 | str->append(")"); |
| 90 | } |
| 91 | #endif |