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