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