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" |
Brian Salomon | 89cb821 | 2017-01-09 10:48:23 -0500 | [diff] [blame] | 9 | #include "SkArithmeticImageFilter.h" |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 10 | #include "SkArithmeticModePriv.h" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 13 | #include "SkReadBuffer.h" |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 14 | #include "SkSpecialImage.h" |
| 15 | #include "SkSpecialSurface.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 16 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 17 | #if SK_SUPPORT_GPU |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 18 | #include "GrClip.h" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 19 | #include "GrContext.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 20 | #include "GrRenderTargetContext.h" |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 21 | #include "GrTextureProxy.h" |
| 22 | |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 23 | #include "effects/GrConstColorProcessor.h" |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 24 | #include "effects/GrTextureDomain.h" |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 25 | #include "effects/GrSimpleTextureEffect.h" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 26 | #include "SkGr.h" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 27 | #endif |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 28 | #include "SkClipOpPriv.h" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 29 | |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 30 | class SkXfermodeImageFilter_Base : public SkImageFilter { |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 31 | public: |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 32 | SkXfermodeImageFilter_Base(SkBlendMode mode, sk_sp<SkImageFilter> inputs[2], |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 33 | const CropRect* cropRect); |
| 34 | |
| 35 | SK_TO_STRING_OVERRIDE() |
| 36 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkXfermodeImageFilter_Base) |
| 37 | |
| 38 | protected: |
| 39 | sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&, |
| 40 | SkIPoint* offset) const override; |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame^] | 41 | sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override; |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 42 | |
| 43 | #if SK_SUPPORT_GPU |
| 44 | sk_sp<SkSpecialImage> filterImageGPU(SkSpecialImage* source, |
| 45 | sk_sp<SkSpecialImage> background, |
| 46 | const SkIPoint& backgroundOffset, |
| 47 | sk_sp<SkSpecialImage> foreground, |
| 48 | const SkIPoint& foregroundOffset, |
| 49 | const SkIRect& bounds, |
| 50 | const OutputProperties& outputProperties) const; |
| 51 | #endif |
| 52 | |
| 53 | void flatten(SkWriteBuffer&) const override; |
| 54 | |
Brian Salomon | 89cb821 | 2017-01-09 10:48:23 -0500 | [diff] [blame] | 55 | void drawForeground(SkCanvas* canvas, SkSpecialImage*, const SkIRect&) const; |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 56 | #if SK_SUPPORT_GPU |
Brian Salomon | 89cb821 | 2017-01-09 10:48:23 -0500 | [diff] [blame] | 57 | sk_sp<GrFragmentProcessor> makeFGFrag(sk_sp<GrFragmentProcessor> bgFP) const; |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 58 | #endif |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 59 | |
| 60 | private: |
Brian Salomon | 89cb821 | 2017-01-09 10:48:23 -0500 | [diff] [blame] | 61 | static sk_sp<SkFlattenable> LegacyArithmeticCreateProc(SkReadBuffer& buffer); |
| 62 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 63 | SkBlendMode fMode; |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 64 | |
| 65 | friend class SkXfermodeImageFilter; |
| 66 | |
| 67 | typedef SkImageFilter INHERITED; |
| 68 | }; |
| 69 | |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 72 | sk_sp<SkImageFilter> SkXfermodeImageFilter::Make(SkBlendMode mode, |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 73 | sk_sp<SkImageFilter> background, |
| 74 | sk_sp<SkImageFilter> foreground, |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 75 | const SkImageFilter::CropRect* cropRect) { |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 76 | sk_sp<SkImageFilter> inputs[2] = { std::move(background), std::move(foreground) }; |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 77 | return sk_sp<SkImageFilter>(new SkXfermodeImageFilter_Base(mode, inputs, cropRect)); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 78 | } |
| 79 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 80 | SkXfermodeImageFilter_Base::SkXfermodeImageFilter_Base(SkBlendMode mode, |
| 81 | sk_sp<SkImageFilter> inputs[2], |
| 82 | const CropRect* cropRect) |
Mike Reed | 1834242 | 2016-10-04 10:06:20 -0400 | [diff] [blame] | 83 | : INHERITED(inputs, 2, cropRect) |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 84 | , fMode(mode) |
| 85 | {} |
| 86 | |
| 87 | static int unflatten_blendmode(SkReadBuffer& buffer, SkArithmeticParams* arith) { |
| 88 | if (buffer.isVersionLT(SkReadBuffer::kXfermodeToBlendMode_Version)) { |
| 89 | sk_sp<SkXfermode> xfer = buffer.readXfermode(); |
| 90 | if (xfer) { |
| 91 | if (xfer->isArithmetic(arith)) { |
| 92 | return -1; |
| 93 | } |
| 94 | return (int)xfer->blend(); |
| 95 | } else { |
| 96 | return (int)SkBlendMode::kSrcOver; |
| 97 | } |
| 98 | } else { |
| 99 | uint32_t mode = buffer.read32(); |
| 100 | (void)buffer.validate(mode <= (unsigned)SkBlendMode::kLastMode); |
| 101 | return mode; |
| 102 | } |
Mike Reed | 3ed485f | 2016-10-04 21:35:32 +0000 | [diff] [blame] | 103 | } |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 104 | |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 105 | sk_sp<SkFlattenable> SkXfermodeImageFilter_Base::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 106 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 2); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 107 | SkArithmeticParams arith; |
| 108 | int mode = unflatten_blendmode(buffer, &arith); |
| 109 | if (mode >= 0) { |
| 110 | return SkXfermodeImageFilter::Make((SkBlendMode)mode, common.getInput(0), |
| 111 | common.getInput(1), &common.cropRect()); |
| 112 | } else { |
Brian Salomon | 89cb821 | 2017-01-09 10:48:23 -0500 | [diff] [blame] | 113 | return SkArithmeticImageFilter::Make(arith.fK[0], arith.fK[1], arith.fK[2], arith.fK[3], |
| 114 | arith.fEnforcePMColor, common.getInput(0), |
| 115 | common.getInput(1), &common.cropRect()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 116 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 117 | } |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 118 | |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 119 | void SkXfermodeImageFilter_Base::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 120 | this->INHERITED::flatten(buffer); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 121 | buffer.write32((unsigned)fMode); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 124 | sk_sp<SkSpecialImage> SkXfermodeImageFilter_Base::onFilterImage(SkSpecialImage* source, |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 125 | const Context& ctx, |
| 126 | SkIPoint* offset) const { |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 127 | SkIPoint backgroundOffset = SkIPoint::Make(0, 0); |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 128 | sk_sp<SkSpecialImage> background(this->filterInput(0, source, ctx, &backgroundOffset)); |
| 129 | |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 130 | SkIPoint foregroundOffset = SkIPoint::Make(0, 0); |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 131 | sk_sp<SkSpecialImage> foreground(this->filterInput(1, source, ctx, &foregroundOffset)); |
| 132 | |
| 133 | SkIRect foregroundBounds = SkIRect::EmptyIRect(); |
| 134 | if (foreground) { |
| 135 | foregroundBounds = SkIRect::MakeXYWH(foregroundOffset.x(), foregroundOffset.y(), |
| 136 | foreground->width(), foreground->height()); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 137 | } |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 138 | |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 139 | SkIRect srcBounds = SkIRect::EmptyIRect(); |
| 140 | if (background) { |
| 141 | srcBounds = SkIRect::MakeXYWH(backgroundOffset.x(), backgroundOffset.y(), |
| 142 | background->width(), background->height()); |
| 143 | } |
| 144 | |
senorblanco | 9db0427 | 2016-03-31 08:24:29 -0700 | [diff] [blame] | 145 | srcBounds.join(foregroundBounds); |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 146 | if (srcBounds.isEmpty()) { |
| 147 | return nullptr; |
| 148 | } |
| 149 | |
senorblanco | 9db0427 | 2016-03-31 08:24:29 -0700 | [diff] [blame] | 150 | SkIRect bounds; |
| 151 | if (!this->applyCropRect(ctx, srcBounds, &bounds)) { |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 152 | return nullptr; |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 153 | } |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 154 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 155 | offset->fX = bounds.left(); |
| 156 | offset->fY = bounds.top(); |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 157 | |
| 158 | #if SK_SUPPORT_GPU |
| 159 | if (source->isTextureBacked()) { |
| 160 | return this->filterImageGPU(source, |
| 161 | background, backgroundOffset, |
| 162 | foreground, foregroundOffset, |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 163 | bounds, ctx.outputProperties()); |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 164 | } |
| 165 | #endif |
| 166 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 167 | sk_sp<SkSpecialSurface> surf(source->makeSurface(ctx.outputProperties(), bounds.size())); |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 168 | if (!surf) { |
| 169 | return nullptr; |
| 170 | } |
| 171 | |
| 172 | SkCanvas* canvas = surf->getCanvas(); |
| 173 | SkASSERT(canvas); |
| 174 | |
robertphillips | 3806b8f | 2016-05-12 10:46:39 -0700 | [diff] [blame] | 175 | canvas->clear(0x0); // can't count on background to fully clear the background |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 176 | canvas->translate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())); |
| 177 | |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 178 | if (background) { |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 179 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 180 | paint.setBlendMode(SkBlendMode::kSrc); |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 181 | background->draw(canvas, |
| 182 | SkIntToScalar(backgroundOffset.fX), SkIntToScalar(backgroundOffset.fY), |
| 183 | &paint); |
| 184 | } |
| 185 | |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 186 | this->drawForeground(canvas, foreground.get(), foregroundBounds); |
Mike Reed | 10ff5bf | 2016-09-26 20:40:57 +0000 | [diff] [blame] | 187 | |
| 188 | return surf->makeImageSnapshot(); |
Mike Reed | 0530c88 | 2016-09-26 15:33:00 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame^] | 191 | sk_sp<SkImageFilter> SkXfermodeImageFilter_Base::onMakeColorSpace(SkColorSpaceXformer* xformer) |
| 192 | const { |
| 193 | SkASSERT(2 == this->countInputs()); |
| 194 | if (!this->getInput(0) && !this->getInput(1)) { |
| 195 | return sk_ref_sp(const_cast<SkXfermodeImageFilter_Base*>(this)); |
| 196 | } |
| 197 | |
| 198 | sk_sp<SkImageFilter> background = |
| 199 | this->getInput(0) ? this->getInput(0)->makeColorSpace(xformer) : nullptr; |
| 200 | sk_sp<SkImageFilter> foreground = |
| 201 | this->getInput(1) ? this->getInput(1)->makeColorSpace(xformer) : nullptr; |
| 202 | |
| 203 | return SkXfermodeImageFilter::Make(fMode, std::move(background), std::move(foreground), |
| 204 | this->getCropRectIfSet()); |
| 205 | } |
| 206 | |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 207 | void SkXfermodeImageFilter_Base::drawForeground(SkCanvas* canvas, SkSpecialImage* img, |
| 208 | const SkIRect& fgBounds) const { |
| 209 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 210 | paint.setBlendMode(fMode); |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 211 | if (img) { |
| 212 | img->draw(canvas, SkIntToScalar(fgBounds.fLeft), SkIntToScalar(fgBounds.fTop), &paint); |
| 213 | } |
| 214 | |
| 215 | SkAutoCanvasRestore acr(canvas, true); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 216 | canvas->clipRect(SkRect::Make(fgBounds), kDifference_SkClipOp); |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 217 | paint.setColor(0); |
| 218 | canvas->drawPaint(paint); |
| 219 | } |
| 220 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 221 | #ifndef SK_IGNORE_TO_STRING |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 222 | void SkXfermodeImageFilter_Base::toString(SkString* str) const { |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 223 | str->appendf("SkXfermodeImageFilter: ("); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 224 | str->appendf("blendmode: (%d)", (int)fMode); |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 225 | if (this->getInput(0)) { |
| 226 | str->appendf("foreground: ("); |
| 227 | this->getInput(0)->toString(str); |
| 228 | str->appendf(")"); |
| 229 | } |
| 230 | if (this->getInput(1)) { |
| 231 | str->appendf("background: ("); |
| 232 | this->getInput(1)->toString(str); |
| 233 | str->appendf(")"); |
| 234 | } |
| 235 | str->append(")"); |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 236 | } |
| 237 | #endif |
| 238 | |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 239 | #if SK_SUPPORT_GPU |
| 240 | |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 241 | #include "SkXfermode_proccoeff.h" |
| 242 | |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 243 | sk_sp<SkSpecialImage> SkXfermodeImageFilter_Base::filterImageGPU( |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 244 | SkSpecialImage* source, |
| 245 | sk_sp<SkSpecialImage> background, |
| 246 | const SkIPoint& backgroundOffset, |
| 247 | sk_sp<SkSpecialImage> foreground, |
| 248 | const SkIPoint& foregroundOffset, |
| 249 | const SkIRect& bounds, |
| 250 | const OutputProperties& outputProperties) const { |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 251 | SkASSERT(source->isTextureBacked()); |
| 252 | |
| 253 | GrContext* context = source->getContext(); |
| 254 | |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 255 | sk_sp<GrTextureProxy> backgroundProxy, foregroundProxy; |
| 256 | |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 257 | if (background) { |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 258 | backgroundProxy = background->asTextureProxyRef(context); |
egdaniel | 38cd055 | 2015-01-14 08:05:11 -0800 | [diff] [blame] | 259 | } |
| 260 | |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 261 | if (foreground) { |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 262 | foregroundProxy = foreground->asTextureProxyRef(context); |
senorblanco | 06d54ad | 2016-01-13 13:48:54 -0800 | [diff] [blame] | 263 | } |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 264 | |
joshualitt | b245605 | 2015-07-08 09:36:59 -0700 | [diff] [blame] | 265 | GrPaint paint; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 266 | sk_sp<GrFragmentProcessor> bgFP; |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 267 | |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 268 | if (backgroundProxy) { |
| 269 | SkMatrix bgMatrix = SkMatrix::MakeTrans(-SkIntToScalar(backgroundOffset.fX), |
| 270 | -SkIntToScalar(backgroundOffset.fY)); |
Brian Osman | 7dc5452 | 2016-12-20 10:36:35 -0500 | [diff] [blame] | 271 | sk_sp<GrColorSpaceXform> bgXform = GrColorSpaceXform::Make(background->getColorSpace(), |
| 272 | outputProperties.colorSpace()); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 273 | bgFP = GrTextureDomainEffect::Make( |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 274 | context->resourceProvider(), std::move(backgroundProxy), |
| 275 | std::move(bgXform), bgMatrix, |
Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 276 | GrTextureDomain::MakeTexelDomain(background->subset()), |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 277 | GrTextureDomain::kDecal_Mode, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 278 | GrSamplerParams::kNone_FilterMode); |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 279 | } else { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 280 | bgFP = GrConstColorProcessor::Make(GrColor4f::TransparentBlack(), |
| 281 | GrConstColorProcessor::kIgnore_InputMode); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 284 | if (foregroundProxy) { |
| 285 | SkMatrix fgMatrix = SkMatrix::MakeTrans(-SkIntToScalar(foregroundOffset.fX), |
| 286 | -SkIntToScalar(foregroundOffset.fY)); |
Brian Osman | 7dc5452 | 2016-12-20 10:36:35 -0500 | [diff] [blame] | 287 | sk_sp<GrColorSpaceXform> fgXform = GrColorSpaceXform::Make(foreground->getColorSpace(), |
| 288 | outputProperties.colorSpace()); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 289 | sk_sp<GrFragmentProcessor> foregroundFP; |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 290 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 291 | foregroundFP = GrTextureDomainEffect::Make( |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 292 | context->resourceProvider(), std::move(foregroundProxy), |
| 293 | std::move(fgXform), fgMatrix, |
Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 294 | GrTextureDomain::MakeTexelDomain(foreground->subset()), |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 295 | GrTextureDomain::kDecal_Mode, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 296 | GrSamplerParams::kNone_FilterMode); |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 297 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 298 | paint.addColorFragmentProcessor(std::move(foregroundFP)); |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 299 | |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 300 | sk_sp<GrFragmentProcessor> xferFP = this->makeFGFrag(bgFP); |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 301 | |
| 302 | // A null 'xferFP' here means kSrc_Mode was used in which case we can just proceed |
| 303 | if (xferFP) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 304 | paint.addColorFragmentProcessor(std::move(xferFP)); |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 305 | } |
| 306 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 307 | paint.addColorFragmentProcessor(std::move(bgFP)); |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 308 | } |
robertphillips | 4f03794 | 2016-02-09 05:09:27 -0800 | [diff] [blame] | 309 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 310 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 311 | |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 312 | sk_sp<GrRenderTargetContext> renderTargetContext(context->makeDeferredRenderTargetContext( |
| 313 | SkBackingFit::kApprox, bounds.width(), bounds.height(), |
| 314 | GrRenderableConfigForColorSpace(outputProperties.colorSpace()), |
| 315 | sk_ref_sp(outputProperties.colorSpace()))); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 316 | if (!renderTargetContext) { |
robertphillips | f230c6d | 2016-04-15 12:47:42 -0700 | [diff] [blame] | 317 | return nullptr; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 318 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 319 | paint.setGammaCorrect(renderTargetContext->isGammaCorrect()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 320 | |
senorblanco | 06d54ad | 2016-01-13 13:48:54 -0800 | [diff] [blame] | 321 | SkMatrix matrix; |
| 322 | matrix.setTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 323 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, |
| 324 | SkRect::Make(bounds)); |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 325 | |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 326 | return SkSpecialImage::MakeDeferredFromGpu(context, |
| 327 | SkIRect::MakeWH(bounds.width(), bounds.height()), |
| 328 | kNeedNewImageUniqueID_SpecialImage, |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 329 | renderTargetContext->asTextureProxyRef(), |
Robert Phillips | 75a475c | 2017-01-13 09:18:59 -0500 | [diff] [blame] | 330 | renderTargetContext->refColorSpace()); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 333 | sk_sp<GrFragmentProcessor> |
| 334 | SkXfermodeImageFilter_Base::makeFGFrag(sk_sp<GrFragmentProcessor> bgFP) const { |
| 335 | // A null fMode is interpreted to mean kSrcOver_Mode (to match raster). |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 336 | SkXfermode* xfer = SkXfermode::Peek(fMode); |
| 337 | sk_sp<SkXfermode> srcover; |
| 338 | if (!xfer) { |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 339 | // It would be awesome to use SkXfermode::Create here but it knows better |
| 340 | // than us and won't return a kSrcOver_Mode SkXfermode. That means we |
| 341 | // have to get one the hard way. |
| 342 | struct ProcCoeff rec; |
Mike Reed | 6a01554 | 2016-11-09 10:38:09 -0500 | [diff] [blame] | 343 | rec.fProc = SkXfermode::GetProc(SkBlendMode::kSrcOver); |
| 344 | SkXfermode::ModeAsCoeff(SkBlendMode::kSrcOver, &rec.fSC, &rec.fDC); |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 345 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 346 | srcover.reset(new SkProcCoeffXfermode(rec, SkBlendMode::kSrcOver)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 347 | xfer = srcover.get(); |
| 348 | |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 349 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 350 | return xfer->makeFragmentProcessorForImageFilter(std::move(bgFP)); |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 351 | } |
| 352 | |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 353 | #endif |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 354 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 355 | |
Brian Salomon | 89cb821 | 2017-01-09 10:48:23 -0500 | [diff] [blame] | 356 | sk_sp<SkFlattenable> SkXfermodeImageFilter_Base::LegacyArithmeticCreateProc(SkReadBuffer& buffer) { |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 357 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 2); |
Brian Salomon | 89cb821 | 2017-01-09 10:48:23 -0500 | [diff] [blame] | 358 | // skip the unused mode (srcover) field |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 359 | SkDEBUGCODE(int mode =) unflatten_blendmode(buffer, nullptr); |
| 360 | if (!buffer.isValid()) { |
| 361 | return nullptr; |
| 362 | } |
| 363 | SkASSERT(SkBlendMode::kSrcOver == (SkBlendMode)mode); |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 364 | float k[4]; |
| 365 | for (int i = 0; i < 4; ++i) { |
| 366 | k[i] = buffer.readScalar(); |
| 367 | } |
| 368 | const bool enforcePMColor = buffer.readBool(); |
Brian Salomon | 89cb821 | 2017-01-09 10:48:23 -0500 | [diff] [blame] | 369 | return SkArithmeticImageFilter::Make(k[0], k[1], k[2], k[3], enforcePMColor, common.getInput(0), |
| 370 | common.getInput(1), &common.cropRect()); |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 374 | |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 375 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkXfermodeImageFilter) |
| 376 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkXfermodeImageFilter_Base) |
| 377 | // manually register the legacy serialized name "SkXfermodeImageFilter" |
| 378 | SkFlattenable::Register("SkXfermodeImageFilter", SkXfermodeImageFilter_Base::CreateProc, |
| 379 | SkFlattenable::kSkImageFilter_Type); |
Brian Salomon | 89cb821 | 2017-01-09 10:48:23 -0500 | [diff] [blame] | 380 | // manually register the legacy serialized name "SkArithmeticImageFilter" from when that filter |
| 381 | // was implemented as a xfermode image filter. |
| 382 | SkFlattenable::Register("SkArithmeticImageFilter", |
| 383 | SkXfermodeImageFilter_Base::LegacyArithmeticCreateProc, |
| 384 | SkFlattenable::kSkImageFilter_Type); |
Mike Reed | 667a007 | 2016-09-23 11:43:54 -0400 | [diff] [blame] | 385 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |