senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "SkMatrixImageFilter.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkDevice.h" |
| 12 | #include "SkColorPriv.h" |
| 13 | #include "SkReadBuffer.h" |
| 14 | #include "SkWriteBuffer.h" |
| 15 | #include "SkMatrix.h" |
| 16 | #include "SkRect.h" |
| 17 | |
| 18 | SkMatrixImageFilter::SkMatrixImageFilter(const SkMatrix& transform, |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 19 | SkFilterQuality filterQuality, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 20 | SkImageFilter* input) |
| 21 | : INHERITED(1, &input), |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 22 | fTransform(transform), |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 23 | fFilterQuality(filterQuality) { |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | SkMatrixImageFilter* SkMatrixImageFilter::Create(const SkMatrix& transform, |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 27 | SkFilterQuality filterQuality, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 28 | SkImageFilter* input) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 29 | return new SkMatrixImageFilter(transform, filterQuality, input); |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 30 | } |
| 31 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 32 | SkFlattenable* SkMatrixImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 33 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 34 | SkMatrix matrix; |
| 35 | buffer.readMatrix(&matrix); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 36 | SkFilterQuality quality = static_cast<SkFilterQuality>(buffer.readInt()); |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 37 | return Create(matrix, quality, common.getInput(0)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 38 | } |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 39 | |
| 40 | void SkMatrixImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 41 | this->INHERITED::flatten(buffer); |
| 42 | buffer.writeMatrix(fTransform); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 43 | buffer.writeInt(fFilterQuality); |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | SkMatrixImageFilter::~SkMatrixImageFilter() { |
| 47 | } |
| 48 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 49 | bool SkMatrixImageFilter::onFilterImageDeprecated(Proxy* proxy, |
| 50 | const SkBitmap& source, |
| 51 | const Context& ctx, |
| 52 | SkBitmap* result, |
| 53 | SkIPoint* offset) const { |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 54 | SkBitmap src = source; |
| 55 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 56 | if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset)) { |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
| 60 | SkRect dstRect; |
| 61 | SkIRect srcBounds, dstBounds; |
| 62 | src.getBounds(&srcBounds); |
| 63 | srcBounds.offset(srcOffset); |
| 64 | SkRect srcRect = SkRect::Make(srcBounds); |
| 65 | SkMatrix matrix; |
| 66 | if (!ctx.ctm().invert(&matrix)) { |
| 67 | return false; |
| 68 | } |
| 69 | matrix.postConcat(fTransform); |
| 70 | matrix.postConcat(ctx.ctm()); |
| 71 | matrix.mapRect(&dstRect, srcRect); |
| 72 | dstRect.roundOut(&dstBounds); |
| 73 | |
| 74 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstBounds.width(), dstBounds.height())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | if (nullptr == device.get()) { |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 76 | return false; |
| 77 | } |
| 78 | |
| 79 | SkCanvas canvas(device.get()); |
| 80 | canvas.translate(-SkIntToScalar(dstBounds.x()), -SkIntToScalar(dstBounds.y())); |
| 81 | canvas.concat(matrix); |
reed | daa57bf | 2015-05-15 10:39:17 -0700 | [diff] [blame] | 82 | SkPaint paint; |
| 83 | |
| 84 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 85 | paint.setFilterQuality(fFilterQuality); |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 86 | canvas.drawBitmap(src, srcRect.x(), srcRect.y(), &paint); |
| 87 | |
| 88 | *result = device.get()->accessBitmap(false); |
| 89 | offset->fX = dstBounds.fLeft; |
| 90 | offset->fY = dstBounds.fTop; |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | void SkMatrixImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const { |
| 95 | SkRect bounds = src; |
| 96 | if (getInput(0)) { |
| 97 | getInput(0)->computeFastBounds(src, &bounds); |
| 98 | } |
robertphillips | 8b7b9c2 | 2014-12-09 05:28:24 -0800 | [diff] [blame] | 99 | fTransform.mapRect(dst, bounds); |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 100 | } |
| 101 | |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 102 | void SkMatrixImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm, |
| 103 | SkIRect* dst, MapDirection direction) const { |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 104 | SkMatrix matrix; |
| 105 | if (!ctm.invert(&matrix)) { |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 106 | *dst = src; |
| 107 | return; |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 108 | } |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 109 | if (kForward_MapDirection == direction) { |
| 110 | matrix.postConcat(fTransform); |
| 111 | } else { |
| 112 | SkMatrix transformInverse; |
| 113 | if (!fTransform.invert(&transformInverse)) { |
| 114 | *dst = src; |
| 115 | return; |
| 116 | } |
| 117 | matrix.postConcat(transformInverse); |
| 118 | } |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 119 | matrix.postConcat(ctm); |
| 120 | SkRect floatBounds; |
| 121 | matrix.mapRect(&floatBounds, SkRect::Make(src)); |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 122 | SkIRect bounds = floatBounds.roundOut(); |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 123 | *dst = bounds; |
senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +0000 | [diff] [blame] | 124 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 125 | |
| 126 | #ifndef SK_IGNORE_TO_STRING |
| 127 | void SkMatrixImageFilter::toString(SkString* str) const { |
| 128 | str->appendf("SkMatrixImageFilter: ("); |
| 129 | |
| 130 | str->appendf("transform: (%f %f %f %f %f %f %f %f %f)", |
| 131 | fTransform[SkMatrix::kMScaleX], |
| 132 | fTransform[SkMatrix::kMSkewX], |
| 133 | fTransform[SkMatrix::kMTransX], |
| 134 | fTransform[SkMatrix::kMSkewY], |
| 135 | fTransform[SkMatrix::kMScaleY], |
| 136 | fTransform[SkMatrix::kMTransY], |
| 137 | fTransform[SkMatrix::kMPersp0], |
| 138 | fTransform[SkMatrix::kMPersp1], |
| 139 | fTransform[SkMatrix::kMPersp2]); |
| 140 | |
| 141 | str->append("<dt>FilterLevel:</dt><dd>"); |
| 142 | static const char* gFilterLevelStrings[] = { "None", "Low", "Medium", "High" }; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 143 | str->append(gFilterLevelStrings[fFilterQuality]); |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 144 | str->append("</dd>"); |
| 145 | |
| 146 | str->appendf(")"); |
| 147 | } |
| 148 | #endif |