senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "SkPictureImageFilter.h" |
| 9 | #include "SkDevice.h" |
| 10 | #include "SkCanvas.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
fmalita | 2d97bc1 | 2014-11-20 10:44:58 -0800 | [diff] [blame] | 12 | #include "SkSurfaceProps.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 13 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 14 | #include "SkValidationUtils.h" |
| 15 | |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame^] | 16 | SkPictureImageFilter::SkPictureImageFilter(sk_sp<SkPicture> picture) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 17 | : INHERITED(0, 0, nullptr) |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame^] | 18 | , fPicture(std::move(picture)) |
| 19 | , fCropRect(fPicture ? fPicture->cullRect() : SkRect::MakeEmpty()) |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 20 | , fPictureResolution(kDeviceSpace_PictureResolution) |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 21 | , fFilterQuality(kLow_SkFilterQuality) { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 22 | } |
| 23 | |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame^] | 24 | SkPictureImageFilter::SkPictureImageFilter(sk_sp<SkPicture> picture, const SkRect& cropRect, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 25 | PictureResolution pictureResolution, |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 26 | SkFilterQuality filterQuality) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 27 | : INHERITED(0, 0, nullptr) |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame^] | 28 | , fPicture(std::move(picture)) |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 29 | , fCropRect(cropRect) |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 30 | , fPictureResolution(pictureResolution) |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 31 | , fFilterQuality(filterQuality) { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 32 | } |
| 33 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 34 | SkFlattenable* SkPictureImageFilter::CreateProc(SkReadBuffer& buffer) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 35 | sk_sp<SkPicture> picture; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 36 | SkRect cropRect; |
| 37 | |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 38 | if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) { |
robertphillips | c4bd39c | 2015-01-06 09:17:02 -0800 | [diff] [blame] | 39 | buffer.validate(!buffer.readBool()); |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 40 | } else { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 41 | if (buffer.readBool()) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 42 | picture = SkPicture::MakeFromBuffer(buffer); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 43 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 44 | } |
| 45 | buffer.readRect(&cropRect); |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 46 | PictureResolution pictureResolution; |
| 47 | if (buffer.isVersionLT(SkReadBuffer::kPictureImageFilterResolution_Version)) { |
| 48 | pictureResolution = kDeviceSpace_PictureResolution; |
| 49 | } else { |
| 50 | pictureResolution = (PictureResolution)buffer.readInt(); |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 51 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 52 | |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 53 | if (kLocalSpace_PictureResolution == pictureResolution) { |
| 54 | //filterLevel is only serialized if pictureResolution is LocalSpace |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 55 | SkFilterQuality filterQuality; |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 56 | if (buffer.isVersionLT(SkReadBuffer::kPictureImageFilterLevel_Version)) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 57 | filterQuality = kLow_SkFilterQuality; |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 58 | } else { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 59 | filterQuality = (SkFilterQuality)buffer.readInt(); |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 60 | } |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame^] | 61 | return MakeForLocalSpace(picture, cropRect, filterQuality).release(); |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 62 | } |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame^] | 63 | return Make(picture, cropRect).release(); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 64 | } |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 65 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 66 | void SkPictureImageFilter::flatten(SkWriteBuffer& buffer) const { |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 67 | if (buffer.isCrossProcess() && SkPicture::PictureIOSecurityPrecautionsEnabled()) { |
robertphillips | c4bd39c | 2015-01-06 09:17:02 -0800 | [diff] [blame] | 68 | buffer.writeBool(false); |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 69 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 70 | bool hasPicture = (fPicture != nullptr); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 71 | buffer.writeBool(hasPicture); |
| 72 | if (hasPicture) { |
| 73 | fPicture->flatten(buffer); |
| 74 | } |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 75 | } |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 76 | buffer.writeRect(fCropRect); |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 77 | buffer.writeInt(fPictureResolution); |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 78 | if (kLocalSpace_PictureResolution == fPictureResolution) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 79 | buffer.writeInt(fFilterQuality); |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 80 | } |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 81 | } |
| 82 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 83 | bool SkPictureImageFilter::onFilterImageDeprecated(Proxy* proxy, const SkBitmap&, |
| 84 | const Context& ctx, |
| 85 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 86 | if (!fPicture) { |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 87 | offset->fX = offset->fY = 0; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | |
| 91 | SkRect floatBounds; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 92 | ctx.ctm().mapRect(&floatBounds, fCropRect); |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 93 | SkIRect bounds = floatBounds.roundOut(); |
senorblanco | 3d822c2 | 2014-07-30 14:49:31 -0700 | [diff] [blame] | 94 | if (!bounds.intersect(ctx.clipBounds())) { |
| 95 | return false; |
| 96 | } |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 97 | |
| 98 | if (bounds.isEmpty()) { |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 99 | offset->fX = offset->fY = 0; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 100 | return true; |
| 101 | } |
| 102 | |
| 103 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 104 | if (nullptr == device.get()) { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 108 | if (kDeviceSpace_PictureResolution == fPictureResolution || |
| 109 | 0 == (ctx.ctm().getType() & ~SkMatrix::kTranslate_Mask)) { |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 110 | this->drawPictureAtDeviceResolution(device.get(), bounds, ctx); |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 111 | } else { |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 112 | this->drawPictureAtLocalResolution(proxy, device.get(), bounds, ctx); |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 113 | } |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 114 | |
| 115 | *result = device.get()->accessBitmap(false); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 116 | offset->fX = bounds.fLeft; |
| 117 | offset->fY = bounds.fTop; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 118 | return true; |
| 119 | } |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 120 | |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 121 | void SkPictureImageFilter::drawPictureAtDeviceResolution(SkBaseDevice* device, |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 122 | const SkIRect& deviceBounds, |
| 123 | const Context& ctx) const { |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 124 | SkCanvas canvas(device); |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 125 | |
| 126 | canvas.translate(-SkIntToScalar(deviceBounds.fLeft), -SkIntToScalar(deviceBounds.fTop)); |
| 127 | canvas.concat(ctx.ctm()); |
| 128 | canvas.drawPicture(fPicture); |
| 129 | } |
| 130 | |
| 131 | void SkPictureImageFilter::drawPictureAtLocalResolution(Proxy* proxy, SkBaseDevice* device, |
| 132 | const SkIRect& deviceBounds, |
| 133 | const Context& ctx) const { |
| 134 | SkMatrix inverseCtm; |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 135 | if (!ctx.ctm().invert(&inverseCtm)) { |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 136 | return; |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 139 | SkRect localBounds = SkRect::Make(ctx.clipBounds()); |
| 140 | inverseCtm.mapRect(&localBounds); |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 141 | if (!localBounds.intersect(fCropRect)) { |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 142 | return; |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 143 | } |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 144 | SkIRect localIBounds = localBounds.roundOut(); |
| 145 | SkAutoTUnref<SkBaseDevice> localDevice(proxy->createDevice(localIBounds.width(), localIBounds.height())); |
| 146 | |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 147 | SkCanvas localCanvas(localDevice); |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 148 | localCanvas.translate(-SkIntToScalar(localIBounds.fLeft), -SkIntToScalar(localIBounds.fTop)); |
| 149 | localCanvas.drawPicture(fPicture); |
| 150 | |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 151 | SkCanvas canvas(device); |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 152 | |
| 153 | canvas.translate(-SkIntToScalar(deviceBounds.fLeft), -SkIntToScalar(deviceBounds.fTop)); |
| 154 | canvas.concat(ctx.ctm()); |
| 155 | SkPaint paint; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 156 | paint.setFilterQuality(fFilterQuality); |
junov | f3c78cc | 2014-12-09 13:07:22 -0800 | [diff] [blame] | 157 | canvas.drawBitmap(localDevice.get()->accessBitmap(false), SkIntToScalar(localIBounds.fLeft), |
| 158 | SkIntToScalar(localIBounds.fTop), &paint); |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 159 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 160 | |
| 161 | #ifndef SK_IGNORE_TO_STRING |
| 162 | void SkPictureImageFilter::toString(SkString* str) const { |
| 163 | str->appendf("SkPictureImageFilter: ("); |
| 164 | str->appendf("crop: (%f,%f,%f,%f) ", |
| 165 | fCropRect.fLeft, fCropRect.fTop, fCropRect.fRight, fCropRect.fBottom); |
| 166 | if (fPicture) { |
| 167 | str->appendf("picture: (%f,%f,%f,%f)", |
| 168 | fPicture->cullRect().fLeft, fPicture->cullRect().fTop, |
| 169 | fPicture->cullRect().fRight, fPicture->cullRect().fBottom); |
| 170 | } |
| 171 | str->append(")"); |
| 172 | } |
| 173 | #endif |