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" |
Matt Sarett | e22a6a2 | 2017-04-12 16:26:21 -0400 | [diff] [blame] | 11 | #include "SkColorSpaceXformCanvas.h" |
| 12 | #include "SkColorSpaceXformer.h" |
Mike Reed | 4872315 | 2017-11-10 14:35:54 -0500 | [diff] [blame] | 13 | #include "SkImageSource.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 14 | #include "SkReadBuffer.h" |
robertphillips | fef2860 | 2016-04-11 11:08:52 -0700 | [diff] [blame] | 15 | #include "SkSpecialImage.h" |
| 16 | #include "SkSpecialSurface.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 17 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 18 | #include "SkValidationUtils.h" |
| 19 | |
robertphillips | 225db44 | 2016-04-17 14:27:05 -0700 | [diff] [blame] | 20 | sk_sp<SkImageFilter> SkPictureImageFilter::Make(sk_sp<SkPicture> picture) { |
| 21 | return sk_sp<SkImageFilter>(new SkPictureImageFilter(std::move(picture))); |
| 22 | } |
| 23 | |
| 24 | sk_sp<SkImageFilter> SkPictureImageFilter::Make(sk_sp<SkPicture> picture, |
| 25 | const SkRect& cropRect) { |
Mike Reed | 4872315 | 2017-11-10 14:35:54 -0500 | [diff] [blame] | 26 | return sk_sp<SkImageFilter>(new SkPictureImageFilter(std::move(picture), cropRect, nullptr)); |
robertphillips | 225db44 | 2016-04-17 14:27:05 -0700 | [diff] [blame] | 27 | } |
| 28 | |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame] | 29 | SkPictureImageFilter::SkPictureImageFilter(sk_sp<SkPicture> picture) |
robertphillips | 372177e | 2016-03-30 07:32:28 -0700 | [diff] [blame] | 30 | : INHERITED(nullptr, 0, nullptr) |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame] | 31 | , fPicture(std::move(picture)) |
Mike Reed | 4872315 | 2017-11-10 14:35:54 -0500 | [diff] [blame] | 32 | , fCropRect(fPicture ? fPicture->cullRect() : SkRect::MakeEmpty()) { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 33 | } |
| 34 | |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame] | 35 | SkPictureImageFilter::SkPictureImageFilter(sk_sp<SkPicture> picture, const SkRect& cropRect, |
Matt Sarett | e22a6a2 | 2017-04-12 16:26:21 -0400 | [diff] [blame] | 36 | sk_sp<SkColorSpace> colorSpace) |
robertphillips | 372177e | 2016-03-30 07:32:28 -0700 | [diff] [blame] | 37 | : INHERITED(nullptr, 0, nullptr) |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame] | 38 | , fPicture(std::move(picture)) |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 39 | , fCropRect(cropRect) |
Matt Sarett | e22a6a2 | 2017-04-12 16:26:21 -0400 | [diff] [blame] | 40 | , fColorSpace(std::move(colorSpace)) { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Mike Reed | 4872315 | 2017-11-10 14:35:54 -0500 | [diff] [blame] | 43 | enum PictureResolution { |
| 44 | kDeviceSpace_PictureResolution, |
| 45 | kLocalSpace_PictureResolution |
| 46 | }; |
| 47 | static sk_sp<SkImageFilter> make_localspace_filter(sk_sp<SkPicture> pic, const SkRect& cropRect, |
| 48 | SkFilterQuality fq) { |
| 49 | SkISize dim = { SkScalarRoundToInt(cropRect.width()), SkScalarRoundToInt(cropRect.height()) }; |
| 50 | auto img = SkImage::MakeFromPicture(std::move(pic), dim, nullptr, nullptr, |
| 51 | SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB()); |
| 52 | return SkImageSource::Make(img, cropRect, cropRect, fq); |
| 53 | } |
| 54 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 55 | sk_sp<SkFlattenable> SkPictureImageFilter::CreateProc(SkReadBuffer& buffer) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 56 | sk_sp<SkPicture> picture; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 57 | SkRect cropRect; |
| 58 | |
Mike Reed | 5a0f345 | 2018-01-19 08:25:11 -0500 | [diff] [blame] | 59 | if (buffer.readBool()) { |
| 60 | picture = SkPicture::MakeFromBuffer(buffer); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 61 | } |
| 62 | buffer.readRect(&cropRect); |
| 63 | |
Mike Reed | 77e487d | 2017-11-09 21:50:20 +0000 | [diff] [blame] | 64 | if (buffer.isVersionLT(SkReadBuffer::kRemovePictureImageFilterLocalSpace)) { |
Mike Reed | 4872315 | 2017-11-10 14:35:54 -0500 | [diff] [blame] | 65 | PictureResolution pictureResolution = buffer.checkRange<PictureResolution>( |
| 66 | kDeviceSpace_PictureResolution, kLocalSpace_PictureResolution); |
Mike Reed | 77e487d | 2017-11-09 21:50:20 +0000 | [diff] [blame] | 67 | if (kLocalSpace_PictureResolution == pictureResolution) { |
Mike Reed | 4872315 | 2017-11-10 14:35:54 -0500 | [diff] [blame] | 68 | return make_localspace_filter(std::move(picture), cropRect, |
| 69 | buffer.checkFilterQuality()); |
Mike Reed | 77e487d | 2017-11-09 21:50:20 +0000 | [diff] [blame] | 70 | } |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 71 | } |
Mike Reed | 4872315 | 2017-11-10 14:35:54 -0500 | [diff] [blame] | 72 | return sk_sp<SkImageFilter>(new SkPictureImageFilter(picture, cropRect, nullptr)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 73 | } |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 74 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 75 | void SkPictureImageFilter::flatten(SkWriteBuffer& buffer) const { |
Mike Reed | 5a0f345 | 2018-01-19 08:25:11 -0500 | [diff] [blame] | 76 | bool hasPicture = (fPicture != nullptr); |
| 77 | buffer.writeBool(hasPicture); |
| 78 | if (hasPicture) { |
Mike Klein | 88d9071 | 2018-01-27 17:30:04 +0000 | [diff] [blame] | 79 | fPicture->flatten(buffer); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 80 | } |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 81 | buffer.writeRect(fCropRect); |
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 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 100 | sk_sp<SkSpecialSurface> surf(source->makeSurface(ctx.outputProperties(), bounds.size())); |
robertphillips | fef2860 | 2016-04-11 11:08:52 -0700 | [diff] [blame] | 101 | if (!surf) { |
| 102 | return nullptr; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 103 | } |
| 104 | |
robertphillips | fef2860 | 2016-04-11 11:08:52 -0700 | [diff] [blame] | 105 | SkCanvas* canvas = surf->getCanvas(); |
| 106 | SkASSERT(canvas); |
robertphillips | fef2860 | 2016-04-11 11:08:52 -0700 | [diff] [blame] | 107 | canvas->clear(0x0); |
| 108 | |
Mike Reed | 4872315 | 2017-11-10 14:35:54 -0500 | [diff] [blame] | 109 | std::unique_ptr<SkCanvas> xformCanvas; |
| 110 | if (fColorSpace) { |
| 111 | // Only non-null in the case where onMakeColorSpace() was called. This instructs |
| 112 | // us to do the color space xform on playback. |
| 113 | xformCanvas = SkCreateColorSpaceXformCanvas(canvas, fColorSpace); |
| 114 | canvas = xformCanvas.get(); |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 115 | } |
Mike Reed | 4872315 | 2017-11-10 14:35:54 -0500 | [diff] [blame] | 116 | canvas->translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop)); |
| 117 | canvas->concat(ctx.ctm()); |
| 118 | canvas->drawPicture(fPicture); |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 119 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 120 | offset->fX = bounds.fLeft; |
| 121 | offset->fY = bounds.fTop; |
robertphillips | fef2860 | 2016-04-11 11:08:52 -0700 | [diff] [blame] | 122 | return surf->makeImageSnapshot(); |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 123 | } |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 124 | |
Matt Sarett | e22a6a2 | 2017-04-12 16:26:21 -0400 | [diff] [blame] | 125 | sk_sp<SkImageFilter> SkPictureImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer) const { |
Florin Malita | a252c66 | 2017-07-10 12:27:52 -0400 | [diff] [blame] | 126 | sk_sp<SkColorSpace> dstCS = xformer->dst(); |
Florin Malita | 604f0d5 | 2017-07-13 14:29:12 -0400 | [diff] [blame] | 127 | if (SkColorSpace::Equals(dstCS.get(), fColorSpace.get())) { |
Florin Malita | a252c66 | 2017-07-10 12:27:52 -0400 | [diff] [blame] | 128 | return this->refMe(); |
| 129 | } |
| 130 | |
Mike Reed | 4872315 | 2017-11-10 14:35:54 -0500 | [diff] [blame] | 131 | return sk_sp<SkImageFilter>(new SkPictureImageFilter(fPicture, fCropRect, std::move(dstCS))); |
Justin Novosad | 5234075 | 2014-12-02 14:50:56 -0500 | [diff] [blame] | 132 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 133 | |
| 134 | #ifndef SK_IGNORE_TO_STRING |
| 135 | void SkPictureImageFilter::toString(SkString* str) const { |
| 136 | str->appendf("SkPictureImageFilter: ("); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 137 | str->appendf("crop: (%f,%f,%f,%f) ", |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 138 | fCropRect.fLeft, fCropRect.fTop, fCropRect.fRight, fCropRect.fBottom); |
| 139 | if (fPicture) { |
| 140 | str->appendf("picture: (%f,%f,%f,%f)", |
| 141 | fPicture->cullRect().fLeft, fPicture->cullRect().fTop, |
| 142 | fPicture->cullRect().fRight, fPicture->cullRect().fBottom); |
| 143 | } |
| 144 | str->append(")"); |
| 145 | } |
| 146 | #endif |