fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 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 "SkImageSource.h" |
| 9 | |
| 10 | #include "SkCanvas.h" |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame^] | 11 | #include "SkColorSpaceXformer.h" |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 12 | #include "SkImage.h" |
| 13 | #include "SkReadBuffer.h" |
robertphillips | 6ac97b7 | 2016-03-09 05:17:10 -0800 | [diff] [blame] | 14 | #include "SkSpecialImage.h" |
| 15 | #include "SkSpecialSurface.h" |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 16 | #include "SkWriteBuffer.h" |
| 17 | #include "SkString.h" |
| 18 | |
robertphillips | 225db44 | 2016-04-17 14:27:05 -0700 | [diff] [blame] | 19 | sk_sp<SkImageFilter> SkImageSource::Make(sk_sp<SkImage> image) { |
| 20 | if (!image) { |
| 21 | return nullptr; |
| 22 | } |
| 23 | |
| 24 | return sk_sp<SkImageFilter>(new SkImageSource(std::move(image))); |
| 25 | } |
| 26 | |
| 27 | sk_sp<SkImageFilter> SkImageSource::Make(sk_sp<SkImage> image, |
| 28 | const SkRect& srcRect, |
| 29 | const SkRect& dstRect, |
| 30 | SkFilterQuality filterQuality) { |
Hal Canary | 27ecd86 | 2017-03-21 14:00:35 -0700 | [diff] [blame] | 31 | if (!image || srcRect.width() <= 0.0f || srcRect.height() <= 0.0f) { |
robertphillips | 225db44 | 2016-04-17 14:27:05 -0700 | [diff] [blame] | 32 | return nullptr; |
| 33 | } |
| 34 | |
| 35 | return sk_sp<SkImageFilter>(new SkImageSource(std::move(image), |
| 36 | srcRect, dstRect, |
| 37 | filterQuality)); |
| 38 | } |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 39 | |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 40 | SkImageSource::SkImageSource(sk_sp<SkImage> image) |
robertphillips | 6b13473 | 2016-04-15 09:58:37 -0700 | [diff] [blame] | 41 | : INHERITED(nullptr, 0, nullptr) |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 42 | , fImage(std::move(image)) |
| 43 | , fSrcRect(SkRect::MakeIWH(fImage->width(), fImage->height())) |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 44 | , fDstRect(fSrcRect) |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 45 | , fFilterQuality(kHigh_SkFilterQuality) { |
| 46 | } |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 47 | |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 48 | SkImageSource::SkImageSource(sk_sp<SkImage> image, |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 49 | const SkRect& srcRect, |
| 50 | const SkRect& dstRect, |
| 51 | SkFilterQuality filterQuality) |
robertphillips | 6b13473 | 2016-04-15 09:58:37 -0700 | [diff] [blame] | 52 | : INHERITED(nullptr, 0, nullptr) |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 53 | , fImage(std::move(image)) |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 54 | , fSrcRect(srcRect) |
| 55 | , fDstRect(dstRect) |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 56 | , fFilterQuality(filterQuality) { |
| 57 | } |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 58 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 59 | sk_sp<SkFlattenable> SkImageSource::CreateProc(SkReadBuffer& buffer) { |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 60 | SkFilterQuality filterQuality = (SkFilterQuality)buffer.readInt(); |
| 61 | |
| 62 | SkRect src, dst; |
| 63 | buffer.readRect(&src); |
| 64 | buffer.readRect(&dst); |
| 65 | |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 66 | sk_sp<SkImage> image(buffer.readImage()); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 67 | if (!image) { |
| 68 | return nullptr; |
| 69 | } |
| 70 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 71 | return SkImageSource::Make(std::move(image), src, dst, filterQuality); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void SkImageSource::flatten(SkWriteBuffer& buffer) const { |
| 75 | buffer.writeInt(fFilterQuality); |
| 76 | buffer.writeRect(fSrcRect); |
| 77 | buffer.writeRect(fDstRect); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 78 | buffer.writeImage(fImage.get()); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 79 | } |
| 80 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 81 | sk_sp<SkSpecialImage> SkImageSource::onFilterImage(SkSpecialImage* source, const Context& ctx, |
| 82 | SkIPoint* offset) const { |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 83 | SkRect dstRect; |
| 84 | ctx.ctm().mapRect(&dstRect, fDstRect); |
robertphillips | 6ac97b7 | 2016-03-09 05:17:10 -0800 | [diff] [blame] | 85 | |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 86 | SkRect bounds = SkRect::MakeIWH(fImage->width(), fImage->height()); |
Robert Phillips | 8bb3b21 | 2017-02-06 12:32:55 -0500 | [diff] [blame] | 87 | if (fSrcRect == bounds) { |
| 88 | int iLeft = dstRect.fLeft; |
| 89 | int iTop = dstRect.fTop; |
| 90 | // TODO: this seems to be a very noise-prone way to determine this (esp. the floating-point |
| 91 | // widths & heights). |
| 92 | if (dstRect.width() == bounds.width() && dstRect.height() == bounds.height() && |
| 93 | iLeft == dstRect.fLeft && iTop == dstRect.fTop) { |
| 94 | // The dest is just an un-scaled integer translation of the entire image; return it |
| 95 | offset->fX = iLeft; |
| 96 | offset->fY = iTop; |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 97 | |
Robert Phillips | 8bb3b21 | 2017-02-06 12:32:55 -0500 | [diff] [blame] | 98 | return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(fImage->width(), fImage->height()), |
| 99 | fImage, ctx.outputProperties().colorSpace(), |
| 100 | &source->props()); |
| 101 | } |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | const SkIRect dstIRect = dstRect.roundOut(); |
robertphillips | 6ac97b7 | 2016-03-09 05:17:10 -0800 | [diff] [blame] | 105 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 106 | sk_sp<SkSpecialSurface> surf(source->makeSurface(ctx.outputProperties(), dstIRect.size())); |
robertphillips | 6ac97b7 | 2016-03-09 05:17:10 -0800 | [diff] [blame] | 107 | if (!surf) { |
| 108 | return nullptr; |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 109 | } |
| 110 | |
robertphillips | 6ac97b7 | 2016-03-09 05:17:10 -0800 | [diff] [blame] | 111 | SkCanvas* canvas = surf->getCanvas(); |
| 112 | SkASSERT(canvas); |
| 113 | |
| 114 | // TODO: it seems like this clear shouldn't be necessary (see skbug.com/5075) |
| 115 | canvas->clear(0x0); |
| 116 | |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 117 | SkPaint paint; |
| 118 | |
robertphillips | 6ac97b7 | 2016-03-09 05:17:10 -0800 | [diff] [blame] | 119 | // Subtract off the integer component of the translation (will be applied in offset, below). |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 120 | dstRect.offset(-SkIntToScalar(dstIRect.fLeft), -SkIntToScalar(dstIRect.fTop)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 121 | paint.setBlendMode(SkBlendMode::kSrc); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 122 | // FIXME: this probably shouldn't be necessary, but drawImageRect asserts |
| 123 | // None filtering when it's translate-only |
| 124 | paint.setFilterQuality( |
| 125 | fSrcRect.width() == dstRect.width() && fSrcRect.height() == dstRect.height() ? |
| 126 | kNone_SkFilterQuality : fFilterQuality); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 127 | canvas->drawImageRect(fImage.get(), fSrcRect, dstRect, &paint, |
| 128 | SkCanvas::kStrict_SrcRectConstraint); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 129 | |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 130 | offset->fX = dstIRect.fLeft; |
| 131 | offset->fY = dstIRect.fTop; |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 132 | return surf->makeImageSnapshot(); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame^] | 135 | sk_sp<SkImageFilter> SkImageSource::onMakeColorSpace(SkColorSpaceXformer* xformer) const { |
| 136 | SkASSERT(0 == this->countInputs()); |
| 137 | |
| 138 | return SkImageSource::Make(xformer->apply(fImage.get()), fSrcRect, fDstRect, fFilterQuality); |
| 139 | } |
| 140 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 141 | SkRect SkImageSource::computeFastBounds(const SkRect& src) const { |
| 142 | return fDstRect; |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | #ifndef SK_IGNORE_TO_STRING |
| 146 | void SkImageSource::toString(SkString* str) const { |
| 147 | str->appendf("SkImageSource: ("); |
| 148 | str->appendf("src: (%f,%f,%f,%f) dst: (%f,%f,%f,%f) ", |
| 149 | fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBottom, |
| 150 | fDstRect.fLeft, fDstRect.fTop, fDstRect.fRight, fDstRect.fBottom); |
| 151 | str->appendf("image: (%d,%d)", |
| 152 | fImage->width(), fImage->height()); |
| 153 | str->append(")"); |
| 154 | } |
| 155 | #endif |