senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +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 "SkXfermodeImageFilter.h" |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 9 | |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 13 | #include "SkSpecialImage.h" |
| 14 | #include "SkSpecialSurface.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 15 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 16 | #include "SkXfermode.h" |
| 17 | #if SK_SUPPORT_GPU |
| 18 | #include "GrContext.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 19 | #include "GrDrawContext.h" |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 20 | #include "effects/GrConstColorProcessor.h" |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 21 | #include "effects/GrTextureDomain.h" |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 22 | #include "effects/GrSimpleTextureEffect.h" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 23 | #include "SkGr.h" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 24 | #endif |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 28 | sk_sp<SkImageFilter> SkXfermodeImageFilter::Make(sk_sp<SkXfermode> mode, |
| 29 | sk_sp<SkImageFilter> background, |
| 30 | sk_sp<SkImageFilter> foreground, |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 31 | const CropRect* cropRect) { |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 32 | sk_sp<SkImageFilter> inputs[2] = { std::move(background), std::move(foreground) }; |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 33 | return sk_sp<SkImageFilter>(new SkXfermodeImageFilter(mode, inputs, cropRect)); |
| 34 | } |
| 35 | |
| 36 | SkXfermodeImageFilter::SkXfermodeImageFilter(sk_sp<SkXfermode> mode, |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 37 | sk_sp<SkImageFilter> inputs[2], |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 38 | const CropRect* cropRect) |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 39 | : INHERITED(inputs, 2, cropRect) |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 40 | , fMode(std::move(mode)) { |
| 41 | } |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 42 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 43 | sk_sp<SkFlattenable> SkXfermodeImageFilter::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 44 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 2); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 45 | sk_sp<SkXfermode> mode(buffer.readXfermode()); |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 46 | return Make(std::move(mode), common.getInput(0), common.getInput(1), &common.cropRect()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 47 | } |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 48 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 49 | void SkXfermodeImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 50 | this->INHERITED::flatten(buffer); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 51 | buffer.writeFlattenable(fMode.get()); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 52 | } |
| 53 | |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 54 | sk_sp<SkSpecialImage> SkXfermodeImageFilter::onFilterImage(SkSpecialImage* source, |
| 55 | const Context& ctx, |
| 56 | SkIPoint* offset) const { |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 57 | SkIPoint backgroundOffset = SkIPoint::Make(0, 0); |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 58 | sk_sp<SkSpecialImage> background(this->filterInput(0, source, ctx, &backgroundOffset)); |
| 59 | |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 60 | SkIPoint foregroundOffset = SkIPoint::Make(0, 0); |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 61 | sk_sp<SkSpecialImage> foreground(this->filterInput(1, source, ctx, &foregroundOffset)); |
| 62 | |
| 63 | SkIRect foregroundBounds = SkIRect::EmptyIRect(); |
| 64 | if (foreground) { |
| 65 | foregroundBounds = SkIRect::MakeXYWH(foregroundOffset.x(), foregroundOffset.y(), |
| 66 | foreground->width(), foreground->height()); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 67 | } |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 68 | |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 69 | SkIRect srcBounds = SkIRect::EmptyIRect(); |
| 70 | if (background) { |
| 71 | srcBounds = SkIRect::MakeXYWH(backgroundOffset.x(), backgroundOffset.y(), |
| 72 | background->width(), background->height()); |
| 73 | } |
| 74 | |
senorblanco | 9db0427 | 2016-03-31 08:24:29 -0700 | [diff] [blame] | 75 | srcBounds.join(foregroundBounds); |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 76 | if (srcBounds.isEmpty()) { |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
senorblanco | 9db0427 | 2016-03-31 08:24:29 -0700 | [diff] [blame] | 80 | SkIRect bounds; |
| 81 | if (!this->applyCropRect(ctx, srcBounds, &bounds)) { |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 82 | return nullptr; |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 83 | } |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 84 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 85 | offset->fX = bounds.left(); |
| 86 | offset->fY = bounds.top(); |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 87 | |
| 88 | #if SK_SUPPORT_GPU |
| 89 | if (source->isTextureBacked()) { |
| 90 | return this->filterImageGPU(source, |
| 91 | background, backgroundOffset, |
| 92 | foreground, foregroundOffset, |
| 93 | bounds); |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | const SkImageInfo info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), |
| 98 | kPremul_SkAlphaType); |
| 99 | sk_sp<SkSpecialSurface> surf(source->makeSurface(info)); |
| 100 | if (!surf) { |
| 101 | return nullptr; |
| 102 | } |
| 103 | |
| 104 | SkCanvas* canvas = surf->getCanvas(); |
| 105 | SkASSERT(canvas); |
| 106 | |
robertphillips | 3806b8f | 2016-05-12 10:46:39 -0700 | [diff] [blame] | 107 | canvas->clear(0x0); // can't count on background to fully clear the background |
| 108 | |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 109 | canvas->translate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())); |
| 110 | |
| 111 | SkPaint paint; |
| 112 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 113 | |
| 114 | if (background) { |
| 115 | background->draw(canvas, |
| 116 | SkIntToScalar(backgroundOffset.fX), SkIntToScalar(backgroundOffset.fY), |
| 117 | &paint); |
| 118 | } |
| 119 | |
| 120 | paint.setXfermode(fMode); |
| 121 | |
| 122 | if (foreground) { |
| 123 | foreground->draw(canvas, |
| 124 | SkIntToScalar(foregroundOffset.fX), SkIntToScalar(foregroundOffset.fY), |
| 125 | &paint); |
| 126 | } |
| 127 | |
| 128 | canvas->clipRect(SkRect::Make(foregroundBounds), SkRegion::kDifference_Op); |
| 129 | paint.setColor(SK_ColorTRANSPARENT); |
| 130 | canvas->drawPaint(paint); |
| 131 | |
| 132 | return surf->makeImageSnapshot(); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 133 | } |
| 134 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 135 | #ifndef SK_IGNORE_TO_STRING |
| 136 | void SkXfermodeImageFilter::toString(SkString* str) const { |
| 137 | str->appendf("SkXfermodeImageFilter: ("); |
| 138 | str->appendf("xfermode: ("); |
| 139 | if (fMode) { |
| 140 | fMode->toString(str); |
| 141 | } |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 142 | str->append(")"); |
| 143 | if (this->getInput(0)) { |
| 144 | str->appendf("foreground: ("); |
| 145 | this->getInput(0)->toString(str); |
| 146 | str->appendf(")"); |
| 147 | } |
| 148 | if (this->getInput(1)) { |
| 149 | str->appendf("background: ("); |
| 150 | this->getInput(1)->toString(str); |
| 151 | str->appendf(")"); |
| 152 | } |
| 153 | str->append(")"); |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 154 | } |
| 155 | #endif |
| 156 | |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 157 | #if SK_SUPPORT_GPU |
| 158 | |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 159 | #include "SkXfermode_proccoeff.h" |
| 160 | |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 161 | sk_sp<SkSpecialImage> SkXfermodeImageFilter::filterImageGPU(SkSpecialImage* source, |
| 162 | sk_sp<SkSpecialImage> background, |
| 163 | const SkIPoint& backgroundOffset, |
| 164 | sk_sp<SkSpecialImage> foreground, |
| 165 | const SkIPoint& foregroundOffset, |
| 166 | const SkIRect& bounds) const { |
| 167 | SkASSERT(source->isTextureBacked()); |
| 168 | |
| 169 | GrContext* context = source->getContext(); |
| 170 | |
| 171 | sk_sp<GrTexture> backgroundTex, foregroundTex; |
| 172 | |
| 173 | if (background) { |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 174 | backgroundTex = background->asTextureRef(context); |
egdaniel | 38cd055 | 2015-01-14 08:05:11 -0800 | [diff] [blame] | 175 | } |
| 176 | |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 177 | if (foreground) { |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 178 | foregroundTex = foreground->asTextureRef(context); |
senorblanco | 06d54ad | 2016-01-13 13:48:54 -0800 | [diff] [blame] | 179 | } |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 180 | |
joshualitt | b245605 | 2015-07-08 09:36:59 -0700 | [diff] [blame] | 181 | GrPaint paint; |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 182 | // SRGBTODO: AllowSRGBInputs? |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 183 | sk_sp<GrFragmentProcessor> bgFP; |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 184 | |
| 185 | if (backgroundTex) { |
| 186 | SkMatrix backgroundMatrix; |
| 187 | backgroundMatrix.setIDiv(backgroundTex->width(), backgroundTex->height()); |
robertphillips | d092ffd | 2016-08-17 09:28:59 -0700 | [diff] [blame] | 188 | backgroundMatrix.preTranslate(-SkIntToScalar(backgroundOffset.fX), |
| 189 | -SkIntToScalar(backgroundOffset.fY)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 190 | bgFP = GrTextureDomainEffect::Make( |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 191 | backgroundTex.get(), nullptr, backgroundMatrix, |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 192 | GrTextureDomain::MakeTexelDomain(backgroundTex.get(), |
| 193 | background->subset()), |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 194 | GrTextureDomain::kDecal_Mode, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 195 | GrTextureParams::kNone_FilterMode); |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 196 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 197 | bgFP = GrConstColorProcessor::Make(GrColor_TRANSPARENT_BLACK, |
| 198 | GrConstColorProcessor::kIgnore_InputMode); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 199 | } |
| 200 | |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 201 | if (foregroundTex) { |
| 202 | SkMatrix foregroundMatrix; |
| 203 | foregroundMatrix.setIDiv(foregroundTex->width(), foregroundTex->height()); |
robertphillips | d092ffd | 2016-08-17 09:28:59 -0700 | [diff] [blame] | 204 | foregroundMatrix.preTranslate(-SkIntToScalar(foregroundOffset.fX), |
| 205 | -SkIntToScalar(foregroundOffset.fY)); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 206 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 207 | sk_sp<GrFragmentProcessor> foregroundFP; |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 208 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 209 | foregroundFP = GrTextureDomainEffect::Make( |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 210 | foregroundTex.get(), nullptr, foregroundMatrix, |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 211 | GrTextureDomain::MakeTexelDomain(foregroundTex.get(), |
| 212 | foreground->subset()), |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 213 | GrTextureDomain::kDecal_Mode, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 214 | GrTextureParams::kNone_FilterMode); |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 215 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 216 | paint.addColorFragmentProcessor(std::move(foregroundFP)); |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 217 | |
| 218 | // A null fMode is interpreted to mean kSrcOver_Mode (to match raster). |
| 219 | SkAutoTUnref<SkXfermode> mode(SkSafeRef(fMode.get())); |
| 220 | if (!mode) { |
| 221 | // It would be awesome to use SkXfermode::Create here but it knows better |
| 222 | // than us and won't return a kSrcOver_Mode SkXfermode. That means we |
| 223 | // have to get one the hard way. |
| 224 | struct ProcCoeff rec; |
| 225 | rec.fProc = SkXfermode::GetProc(SkXfermode::kSrcOver_Mode); |
| 226 | SkXfermode::ModeAsCoeff(SkXfermode::kSrcOver_Mode, &rec.fSC, &rec.fDC); |
| 227 | |
| 228 | mode.reset(new SkProcCoeffXfermode(rec, SkXfermode::kSrcOver_Mode)); |
| 229 | } |
| 230 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 231 | sk_sp<GrFragmentProcessor> xferFP( |
| 232 | mode->makeFragmentProcessorForImageFilter(std::move(bgFP))); |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 233 | |
| 234 | // A null 'xferFP' here means kSrc_Mode was used in which case we can just proceed |
| 235 | if (xferFP) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 236 | paint.addColorFragmentProcessor(std::move(xferFP)); |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 237 | } |
| 238 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 239 | paint.addColorFragmentProcessor(std::move(bgFP)); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 240 | } |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 241 | |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 242 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 243 | |
robertphillips | 6738c70 | 2016-07-27 12:13:51 -0700 | [diff] [blame] | 244 | sk_sp<GrDrawContext> drawContext(context->makeDrawContext(SkBackingFit::kApprox, |
| 245 | bounds.width(), bounds.height(), |
| 246 | kSkia8888_GrPixelConfig, |
| 247 | sk_ref_sp(source->getColorSpace()))); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 248 | if (!drawContext) { |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 249 | return nullptr; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 250 | } |
| 251 | |
senorblanco | 06d54ad | 2016-01-13 13:48:54 -0800 | [diff] [blame] | 252 | SkMatrix matrix; |
| 253 | matrix.setTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())); |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 254 | drawContext->drawRect(GrNoClip(), paint, matrix, SkRect::Make(bounds)); |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 255 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 256 | return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(bounds.width(), bounds.height()), |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 257 | kNeedNewImageUniqueID_SpecialImage, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 258 | drawContext->asTexture(), |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 259 | sk_ref_sp(drawContext->getColorSpace())); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | #endif |