sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "SkDisplacementMapEffect.h" |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 9 | |
| 10 | #include "SkBitmap.h" |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame] | 11 | #include "SkColorSpaceXformer.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 13 | #include "SkSpecialImage.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 14 | #include "SkWriteBuffer.h" |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 15 | #include "SkUnPreMultiply.h" |
sugoi@google.com | 8cec63e | 2013-01-15 15:59:45 +0000 | [diff] [blame] | 16 | #include "SkColorPriv.h" |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +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 | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 19 | #include "GrContext.h" |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 20 | #include "GrCoordTransform.h" |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 21 | #include "GrRenderTargetContext.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 22 | #include "GrTexture.h" |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 23 | #include "GrTextureProxy.h" |
robertphillips | 1de87df | 2016-01-14 06:03:29 -0800 | [diff] [blame] | 24 | #include "SkGr.h" |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 25 | #include "effects/GrTextureDomain.h" |
Brian Osman | 721a2c4 | 2016-12-20 15:00:09 -0500 | [diff] [blame] | 26 | #include "glsl/GrGLSLColorSpaceXformHelper.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 27 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 28 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 29 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 30 | #include "glsl/GrGLSLUniformHandler.h" |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
| 33 | namespace { |
| 34 | |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 35 | #define kChannelSelectorKeyBits 3; // Max value is 4, so 3 bits are required at most |
| 36 | |
Mike Reed | 1a74c53 | 2017-06-18 08:40:38 -0400 | [diff] [blame] | 37 | const uint8_t gChannelTypeToShift[] = { |
| 38 | 0, // unknown |
| 39 | SK_R32_SHIFT, |
| 40 | SK_G32_SHIFT, |
| 41 | SK_B32_SHIFT, |
| 42 | SK_A32_SHIFT, |
| 43 | }; |
| 44 | struct Extractor { |
| 45 | Extractor(SkDisplacementMapEffect::ChannelSelectorType typeX, |
| 46 | SkDisplacementMapEffect::ChannelSelectorType typeY) |
| 47 | : fShiftX(gChannelTypeToShift[typeX]) |
| 48 | , fShiftY(gChannelTypeToShift[typeY]) |
| 49 | {} |
| 50 | |
| 51 | unsigned fShiftX, fShiftY; |
| 52 | |
| 53 | unsigned getX(SkPMColor c) const { return (c >> fShiftX) & 0xFF; } |
| 54 | unsigned getY(SkPMColor c) const { return (c >> fShiftY) & 0xFF; } |
| 55 | }; |
| 56 | |
| 57 | static SkPMColor unpremul_pm(SkPMColor c) { |
| 58 | const U8CPU a = SkGetPackedA32(c); |
| 59 | if (0 == a) { |
| 60 | return 0; |
| 61 | } else if (0xFF == a) { |
| 62 | return c; |
| 63 | } |
| 64 | const unsigned scale = SkUnPreMultiply::GetScale(a); |
| 65 | return SkPackARGB32NoCheck(a, |
| 66 | SkUnPreMultiply::ApplyScale(scale, SkGetPackedR32(c)), |
| 67 | SkUnPreMultiply::ApplyScale(scale, SkGetPackedG32(c)), |
| 68 | SkUnPreMultiply::ApplyScale(scale, SkGetPackedB32(c))); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Mike Reed | 1a74c53 | 2017-06-18 08:40:38 -0400 | [diff] [blame] | 71 | void computeDisplacement(Extractor ex, const SkVector& scale, SkBitmap* dst, |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 72 | const SkBitmap& displ, const SkIPoint& offset, |
| 73 | const SkBitmap& src, |
| 74 | const SkIRect& bounds) { |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 75 | static const SkScalar Inv8bit = SkScalarInvert(255); |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 76 | const int srcW = src.width(); |
| 77 | const int srcH = src.height(); |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 78 | const SkVector scaleForColor = SkVector::Make(scale.fX * Inv8bit, scale.fY * Inv8bit); |
| 79 | const SkVector scaleAdj = SkVector::Make(SK_ScalarHalf - scale.fX * SK_ScalarHalf, |
| 80 | SK_ScalarHalf - scale.fY * SK_ScalarHalf); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 81 | SkPMColor* dstPtr = dst->getAddr32(0, 0); |
| 82 | for (int y = bounds.top(); y < bounds.bottom(); ++y) { |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 83 | const SkPMColor* displPtr = displ.getAddr32(bounds.left() + offset.fX, y + offset.fY); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 84 | for (int x = bounds.left(); x < bounds.right(); ++x, ++displPtr) { |
Mike Reed | 1a74c53 | 2017-06-18 08:40:38 -0400 | [diff] [blame] | 85 | SkPMColor c = unpremul_pm(*displPtr); |
| 86 | |
| 87 | SkScalar displX = scaleForColor.fX * ex.getX(c) + scaleAdj.fX; |
| 88 | SkScalar displY = scaleForColor.fY * ex.getY(c) + scaleAdj.fY; |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 89 | // Truncate the displacement values |
| 90 | const int srcX = x + SkScalarTruncToInt(displX); |
| 91 | const int srcY = y + SkScalarTruncToInt(displY); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 92 | *dstPtr++ = ((srcX < 0) || (srcX >= srcW) || (srcY < 0) || (srcY >= srcH)) ? |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 93 | 0 : *(src.getAddr32(srcX, srcY)); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 98 | bool channel_selector_type_is_valid(SkDisplacementMapEffect::ChannelSelectorType cst) { |
| 99 | switch (cst) { |
| 100 | case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
| 101 | case SkDisplacementMapEffect::kR_ChannelSelectorType: |
| 102 | case SkDisplacementMapEffect::kG_ChannelSelectorType: |
| 103 | case SkDisplacementMapEffect::kB_ChannelSelectorType: |
| 104 | case SkDisplacementMapEffect::kA_ChannelSelectorType: |
| 105 | return true; |
| 106 | default: |
| 107 | break; |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 112 | } // end namespace |
| 113 | |
| 114 | /////////////////////////////////////////////////////////////////////////////// |
| 115 | |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 116 | sk_sp<SkImageFilter> SkDisplacementMapEffect::Make(ChannelSelectorType xChannelSelector, |
| 117 | ChannelSelectorType yChannelSelector, |
| 118 | SkScalar scale, |
| 119 | sk_sp<SkImageFilter> displacement, |
| 120 | sk_sp<SkImageFilter> color, |
| 121 | const CropRect* cropRect) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 122 | if (!channel_selector_type_is_valid(xChannelSelector) || |
| 123 | !channel_selector_type_is_valid(yChannelSelector)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 124 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 125 | } |
| 126 | |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 127 | sk_sp<SkImageFilter> inputs[2] = { std::move(displacement), std::move(color) }; |
| 128 | return sk_sp<SkImageFilter>(new SkDisplacementMapEffect(xChannelSelector, |
| 129 | yChannelSelector, |
| 130 | scale, inputs, cropRect)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 131 | } |
| 132 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 133 | SkDisplacementMapEffect::SkDisplacementMapEffect(ChannelSelectorType xChannelSelector, |
| 134 | ChannelSelectorType yChannelSelector, |
| 135 | SkScalar scale, |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 136 | sk_sp<SkImageFilter> inputs[2], |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 137 | const CropRect* cropRect) |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 138 | : INHERITED(inputs, 2, cropRect) |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 139 | , fXChannelSelector(xChannelSelector) |
| 140 | , fYChannelSelector(yChannelSelector) |
| 141 | , fScale(scale) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | SkDisplacementMapEffect::~SkDisplacementMapEffect() { |
| 145 | } |
| 146 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 147 | sk_sp<SkFlattenable> SkDisplacementMapEffect::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 148 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 2); |
| 149 | ChannelSelectorType xsel = (ChannelSelectorType)buffer.readInt(); |
| 150 | ChannelSelectorType ysel = (ChannelSelectorType)buffer.readInt(); |
| 151 | SkScalar scale = buffer.readScalar(); |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 152 | return Make(xsel, ysel, scale, |
| 153 | common.getInput(0), common.getInput(1), |
| 154 | &common.cropRect()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 155 | } |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 156 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 157 | void SkDisplacementMapEffect::flatten(SkWriteBuffer& buffer) const { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 158 | this->INHERITED::flatten(buffer); |
| 159 | buffer.writeInt((int) fXChannelSelector); |
| 160 | buffer.writeInt((int) fYChannelSelector); |
| 161 | buffer.writeScalar(fScale); |
| 162 | } |
| 163 | |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 164 | #if SK_SUPPORT_GPU |
| 165 | class GrDisplacementMapEffect : public GrFragmentProcessor { |
| 166 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 167 | static std::unique_ptr<GrFragmentProcessor> Make( |
| 168 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, |
| 169 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, SkVector scale, |
| 170 | sk_sp<GrTextureProxy> displacement, const SkMatrix& offsetMatrix, |
| 171 | sk_sp<GrTextureProxy> color, sk_sp<GrColorSpaceXform> colorSpaceXform, |
| 172 | const SkISize& colorDimensions) { |
| 173 | return std::unique_ptr<GrFragmentProcessor>(new GrDisplacementMapEffect( |
| 174 | xChannelSelector, yChannelSelector, scale, std::move(displacement), offsetMatrix, |
| 175 | std::move(color), std::move(colorSpaceXform), colorDimensions)); |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 176 | } |
| 177 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 178 | ~GrDisplacementMapEffect() override; |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 179 | |
| 180 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector() const { |
| 181 | return fXChannelSelector; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 182 | } |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 183 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector() const { |
| 184 | return fYChannelSelector; |
| 185 | } |
| 186 | const SkVector& scale() const { return fScale; } |
| 187 | |
| 188 | const char* name() const override { return "DisplacementMap"; } |
| 189 | const GrTextureDomain& domain() const { return fDomain; } |
Brian Osman | 721a2c4 | 2016-12-20 15:00:09 -0500 | [diff] [blame] | 190 | GrColorSpaceXform* colorSpaceXform() const { return fColorSpaceXform.get(); } |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 191 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 192 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
Brian Salomon | ffc2ec4 | 2017-07-25 14:59:03 -0400 | [diff] [blame] | 193 | |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 194 | private: |
Brian Salomon | ffc2ec4 | 2017-07-25 14:59:03 -0400 | [diff] [blame] | 195 | static OptimizationFlags OptimizationFlags(GrPixelConfig colorConfig); |
| 196 | |
| 197 | GrDisplacementMapEffect(const GrDisplacementMapEffect&); |
| 198 | |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 199 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 200 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 201 | void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override; |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 202 | |
| 203 | bool onIsEqual(const GrFragmentProcessor&) const override; |
| 204 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 205 | GrDisplacementMapEffect(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 206 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, |
| 207 | const SkVector& scale, |
| 208 | sk_sp<GrTextureProxy> displacement, const SkMatrix& offsetMatrix, |
| 209 | sk_sp<GrTextureProxy> color, sk_sp<GrColorSpaceXform> colorSpaceXform, |
| 210 | const SkISize& colorDimensions); |
| 211 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 212 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 213 | |
| 214 | GrCoordTransform fDisplacementTransform; |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 215 | TextureSampler fDisplacementSampler; |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 216 | GrCoordTransform fColorTransform; |
| 217 | GrTextureDomain fDomain; |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 218 | TextureSampler fColorSampler; |
Brian Osman | 721a2c4 | 2016-12-20 15:00:09 -0500 | [diff] [blame] | 219 | sk_sp<GrColorSpaceXform> fColorSpaceXform; |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 220 | SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; |
| 221 | SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; |
| 222 | SkVector fScale; |
| 223 | |
| 224 | typedef GrFragmentProcessor INHERITED; |
| 225 | }; |
| 226 | #endif |
| 227 | |
| 228 | sk_sp<SkSpecialImage> SkDisplacementMapEffect::onFilterImage(SkSpecialImage* source, |
| 229 | const Context& ctx, |
| 230 | SkIPoint* offset) const { |
| 231 | SkIPoint colorOffset = SkIPoint::Make(0, 0); |
| 232 | sk_sp<SkSpecialImage> color(this->filterInput(1, source, ctx, &colorOffset)); |
| 233 | if (!color) { |
| 234 | return nullptr; |
| 235 | } |
| 236 | |
| 237 | SkIPoint displOffset = SkIPoint::Make(0, 0); |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 238 | // Creation of the displacement map should happen in a non-colorspace aware context. This |
| 239 | // texture is a purely mathematical construct, so we want to just operate on the stored |
| 240 | // values. Consider: |
| 241 | // User supplies an sRGB displacement map. If we're rendering to a wider gamut, then we could |
| 242 | // end up filtering the displacement map into that gamut, which has the effect of reducing |
| 243 | // the amount of displacement that it represents (as encoded values move away from the |
| 244 | // primaries). |
| 245 | // With a more complex DAG attached to this input, it's not clear that working in ANY specific |
| 246 | // color space makes sense, so we ignore color spaces (and gamma) entirely. This may not be |
| 247 | // ideal, but it's at least consistent and predictable. |
| 248 | Context displContext(ctx.ctm(), ctx.clipBounds(), ctx.cache(), OutputProperties(nullptr)); |
| 249 | sk_sp<SkSpecialImage> displ(this->filterInput(0, source, displContext, &displOffset)); |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 250 | if (!displ) { |
| 251 | return nullptr; |
| 252 | } |
| 253 | |
| 254 | const SkIRect srcBounds = SkIRect::MakeXYWH(colorOffset.x(), colorOffset.y(), |
| 255 | color->width(), color->height()); |
| 256 | |
| 257 | // Both paths do bounds checking on color pixel access, we don't need to |
| 258 | // pad the color bitmap to bounds here. |
| 259 | SkIRect bounds; |
| 260 | if (!this->applyCropRect(ctx, srcBounds, &bounds)) { |
| 261 | return nullptr; |
| 262 | } |
| 263 | |
| 264 | SkIRect displBounds; |
| 265 | displ = this->applyCropRect(ctx, displ.get(), &displOffset, &displBounds); |
| 266 | if (!displ) { |
| 267 | return nullptr; |
| 268 | } |
| 269 | |
| 270 | if (!bounds.intersect(displBounds)) { |
| 271 | return nullptr; |
| 272 | } |
| 273 | |
| 274 | const SkIRect colorBounds = bounds.makeOffset(-colorOffset.x(), -colorOffset.y()); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 275 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 276 | SkVector scale = SkVector::Make(fScale, fScale); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 277 | ctx.ctm().mapVectors(&scale, 1); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 278 | |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 279 | #if SK_SUPPORT_GPU |
| 280 | if (source->isTextureBacked()) { |
| 281 | GrContext* context = source->getContext(); |
| 282 | |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 283 | sk_sp<GrTextureProxy> colorProxy(color->asTextureProxyRef(context)); |
| 284 | sk_sp<GrTextureProxy> displProxy(displ->asTextureProxyRef(context)); |
| 285 | if (!colorProxy || !displProxy) { |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 286 | return nullptr; |
| 287 | } |
| 288 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 289 | SkMatrix offsetMatrix = SkMatrix::MakeTrans(SkIntToScalar(colorOffset.fX - displOffset.fX), |
| 290 | SkIntToScalar(colorOffset.fY - displOffset.fY)); |
Brian Osman | 721a2c4 | 2016-12-20 15:00:09 -0500 | [diff] [blame] | 291 | SkColorSpace* colorSpace = ctx.outputProperties().colorSpace(); |
| 292 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(color->getColorSpace(), |
| 293 | colorSpace); |
| 294 | GrPaint paint; |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 295 | paint.addColorFragmentProcessor( |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 296 | GrDisplacementMapEffect::Make(fXChannelSelector, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 297 | fYChannelSelector, |
| 298 | scale, |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 299 | std::move(displProxy), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 300 | offsetMatrix, |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 301 | std::move(colorProxy), |
Brian Osman | 721a2c4 | 2016-12-20 15:00:09 -0500 | [diff] [blame] | 302 | std::move(colorSpaceXform), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 303 | SkISize::Make(color->width(), color->height()))); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 304 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 305 | SkMatrix matrix; |
| 306 | matrix.setTranslate(-SkIntToScalar(colorBounds.x()), -SkIntToScalar(colorBounds.y())); |
| 307 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 308 | sk_sp<GrRenderTargetContext> renderTargetContext( |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 309 | context->makeDeferredRenderTargetContext(SkBackingFit::kApprox, |
| 310 | bounds.width(), bounds.height(), |
| 311 | GrRenderableConfigForColorSpace(colorSpace), |
| 312 | sk_ref_sp(colorSpace))); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 313 | if (!renderTargetContext) { |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 314 | return nullptr; |
| 315 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 316 | paint.setGammaCorrect(renderTargetContext->isGammaCorrect()); |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 317 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 318 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 319 | SkRect::Make(colorBounds)); |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 320 | |
| 321 | offset->fX = bounds.left(); |
| 322 | offset->fY = bounds.top(); |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 323 | return SkSpecialImage::MakeDeferredFromGpu( |
| 324 | context, |
| 325 | SkIRect::MakeWH(bounds.width(), bounds.height()), |
| 326 | kNeedNewImageUniqueID_SpecialImage, |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 327 | renderTargetContext->asTextureProxyRef(), |
Robert Phillips | 75a475c | 2017-01-13 09:18:59 -0500 | [diff] [blame] | 328 | renderTargetContext->refColorSpace()); |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 329 | } |
| 330 | #endif |
| 331 | |
| 332 | SkBitmap colorBM, displBM; |
| 333 | |
| 334 | if (!color->getROPixels(&colorBM) || !displ->getROPixels(&displBM)) { |
| 335 | return nullptr; |
| 336 | } |
| 337 | |
| 338 | if ((colorBM.colorType() != kN32_SkColorType) || |
| 339 | (displBM.colorType() != kN32_SkColorType)) { |
| 340 | return nullptr; |
| 341 | } |
| 342 | |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 343 | if (!colorBM.getPixels() || !displBM.getPixels()) { |
| 344 | return nullptr; |
| 345 | } |
| 346 | |
| 347 | SkImageInfo info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), |
| 348 | colorBM.alphaType()); |
| 349 | |
| 350 | SkBitmap dst; |
| 351 | if (!dst.tryAllocPixels(info)) { |
| 352 | return nullptr; |
| 353 | } |
| 354 | |
Mike Reed | 1a74c53 | 2017-06-18 08:40:38 -0400 | [diff] [blame] | 355 | computeDisplacement(Extractor(fXChannelSelector, fYChannelSelector), scale, &dst, |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 356 | displBM, colorOffset - displOffset, colorBM, colorBounds); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 357 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 358 | offset->fX = bounds.left(); |
| 359 | offset->fY = bounds.top(); |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 360 | return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()), |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 361 | dst); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 364 | sk_sp<SkImageFilter> SkDisplacementMapEffect::onMakeColorSpace(SkColorSpaceXformer* xformer) const { |
| 365 | SkASSERT(2 == this->countInputs()); |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 366 | // Intentionally avoid xforming the displacement filter. The values will be used as |
| 367 | // offsets, not as colors. |
| 368 | sk_sp<SkImageFilter> displacement = sk_ref_sp(const_cast<SkImageFilter*>(this->getInput(0))); |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame] | 369 | sk_sp<SkImageFilter> color = xformer->apply(this->getInput(1)); |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 370 | |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame] | 371 | if (color.get() != this->getInput(1)) { |
| 372 | return SkDisplacementMapEffect::Make(fXChannelSelector, fYChannelSelector, fScale, |
| 373 | std::move(displacement), std::move(color), |
| 374 | this->getCropRectIfSet()); |
| 375 | } |
| 376 | return this->refMe(); |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 377 | } |
| 378 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 379 | SkRect SkDisplacementMapEffect::computeFastBounds(const SkRect& src) const { |
| 380 | SkRect bounds = this->getColorInput() ? this->getColorInput()->computeFastBounds(src) : src; |
| 381 | bounds.outset(SkScalarAbs(fScale) * SK_ScalarHalf, SkScalarAbs(fScale) * SK_ScalarHalf); |
| 382 | return bounds; |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 383 | } |
| 384 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 385 | SkIRect SkDisplacementMapEffect::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm, |
| 386 | MapDirection) const { |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 387 | SkVector scale = SkVector::Make(fScale, fScale); |
| 388 | ctm.mapVectors(&scale, 1); |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 389 | return src.makeOutset(SkScalarCeilToInt(SkScalarAbs(scale.fX) * SK_ScalarHalf), |
| 390 | SkScalarCeilToInt(SkScalarAbs(scale.fY) * SK_ScalarHalf)); |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 391 | } |
| 392 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 393 | SkIRect SkDisplacementMapEffect::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, |
| 394 | MapDirection direction) const { |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 395 | // Recurse only into color input. |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 396 | if (this->getColorInput()) { |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 397 | return this->getColorInput()->filterBounds(src, ctm, direction); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 398 | } |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 399 | return src; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 400 | } |
| 401 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 402 | #ifndef SK_IGNORE_TO_STRING |
| 403 | void SkDisplacementMapEffect::toString(SkString* str) const { |
| 404 | str->appendf("SkDisplacementMapEffect: ("); |
| 405 | str->appendf("scale: %f ", fScale); |
| 406 | str->appendf("displacement: ("); |
| 407 | if (this->getDisplacementInput()) { |
| 408 | this->getDisplacementInput()->toString(str); |
| 409 | } |
| 410 | str->appendf(") color: ("); |
| 411 | if (this->getColorInput()) { |
| 412 | this->getColorInput()->toString(str); |
| 413 | } |
| 414 | str->appendf("))"); |
| 415 | } |
| 416 | #endif |
| 417 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 418 | /////////////////////////////////////////////////////////////////////////////// |
| 419 | |
| 420 | #if SK_SUPPORT_GPU |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 421 | class GrGLDisplacementMapEffect : public GrGLSLFragmentProcessor { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 422 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 423 | void emitCode(EmitArgs&) override; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 424 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 425 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 426 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 427 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 428 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 429 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 430 | private: |
Brian Osman | 721a2c4 | 2016-12-20 15:00:09 -0500 | [diff] [blame] | 431 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
| 432 | |
| 433 | UniformHandle fScaleUni; |
Brian Osman | c624d9d | 2017-03-08 11:42:02 -0500 | [diff] [blame] | 434 | GrGLSLColorSpaceXformHelper fColorSpaceHelper; |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 435 | GrTextureDomain::GLDomain fGLDomain; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 436 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 437 | typedef GrGLSLFragmentProcessor INHERITED; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 438 | }; |
| 439 | |
| 440 | /////////////////////////////////////////////////////////////////////////////// |
| 441 | |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 442 | GrGLSLFragmentProcessor* GrDisplacementMapEffect::onCreateGLSLInstance() const { |
| 443 | return new GrGLDisplacementMapEffect; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 446 | void GrDisplacementMapEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
robertphillips | e6277de | 2016-04-11 07:23:34 -0700 | [diff] [blame] | 447 | GrProcessorKeyBuilder* b) const { |
| 448 | GrGLDisplacementMapEffect::GenKey(*this, caps, b); |
| 449 | } |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 450 | |
Brian Salomon | ffc2ec4 | 2017-07-25 14:59:03 -0400 | [diff] [blame] | 451 | GrFragmentProcessor::OptimizationFlags GrDisplacementMapEffect::OptimizationFlags( |
| 452 | GrPixelConfig colorConfig) { |
| 453 | return GrPixelConfigIsOpaque(colorConfig) |
| 454 | ? GrFragmentProcessor::kPreservesOpaqueInput_OptimizationFlag |
| 455 | : GrFragmentProcessor::kNone_OptimizationFlags; |
| 456 | } |
| 457 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 458 | GrDisplacementMapEffect::GrDisplacementMapEffect( |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 459 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, |
| 460 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, |
| 461 | const SkVector& scale, |
| 462 | sk_sp<GrTextureProxy> displacement, |
| 463 | const SkMatrix& offsetMatrix, |
| 464 | sk_sp<GrTextureProxy> color, |
| 465 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
| 466 | const SkISize& colorDimensions) |
Brian Salomon | ffc2ec4 | 2017-07-25 14:59:03 -0400 | [diff] [blame] | 467 | : INHERITED(OptimizationFlags(color->config())) |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 468 | , fDisplacementTransform(offsetMatrix, displacement.get()) |
| 469 | , fDisplacementSampler(displacement) |
| 470 | , fColorTransform(color.get()) |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 471 | , fDomain(color.get(), GrTextureDomain::MakeTexelDomain(SkIRect::MakeSize(colorDimensions)), |
| 472 | GrTextureDomain::kDecal_Mode) |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 473 | , fColorSampler(color) |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 474 | , fColorSpaceXform(std::move(colorSpaceXform)) |
| 475 | , fXChannelSelector(xChannelSelector) |
| 476 | , fYChannelSelector(yChannelSelector) |
| 477 | , fScale(scale) { |
| 478 | this->initClassID<GrDisplacementMapEffect>(); |
| 479 | this->addCoordTransform(&fDisplacementTransform); |
| 480 | this->addTextureSampler(&fDisplacementSampler); |
| 481 | this->addCoordTransform(&fColorTransform); |
| 482 | this->addTextureSampler(&fColorSampler); |
| 483 | } |
| 484 | |
Brian Salomon | ffc2ec4 | 2017-07-25 14:59:03 -0400 | [diff] [blame] | 485 | GrDisplacementMapEffect::GrDisplacementMapEffect(const GrDisplacementMapEffect& that) |
| 486 | : INHERITED(OptimizationFlags(that.fColorSampler.proxy()->config())) |
| 487 | , fDisplacementTransform(that.fDisplacementTransform) |
| 488 | , fDisplacementSampler(that.fDisplacementSampler) |
| 489 | , fColorTransform(that.fColorTransform) |
| 490 | , fDomain(that.fDomain) |
| 491 | , fColorSampler(that.fColorSampler) |
| 492 | , fColorSpaceXform(that.fColorSpaceXform) |
| 493 | , fXChannelSelector(that.fXChannelSelector) |
| 494 | , fYChannelSelector(that.fYChannelSelector) |
| 495 | , fScale(that.fScale) { |
| 496 | this->initClassID<GrDisplacementMapEffect>(); |
| 497 | this->addCoordTransform(&fDisplacementTransform); |
| 498 | this->addTextureSampler(&fDisplacementSampler); |
| 499 | this->addCoordTransform(&fColorTransform); |
| 500 | this->addTextureSampler(&fColorSampler); |
| 501 | } |
| 502 | |
| 503 | GrDisplacementMapEffect::~GrDisplacementMapEffect() {} |
| 504 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 505 | std::unique_ptr<GrFragmentProcessor> GrDisplacementMapEffect::clone() const { |
| 506 | return std::unique_ptr<GrFragmentProcessor>(new GrDisplacementMapEffect(*this)); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 507 | } |
| 508 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 509 | bool GrDisplacementMapEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 510 | const GrDisplacementMapEffect& s = sBase.cast<GrDisplacementMapEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 511 | return fXChannelSelector == s.fXChannelSelector && |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 512 | fYChannelSelector == s.fYChannelSelector && |
| 513 | fScale == s.fScale; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 514 | } |
| 515 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 516 | /////////////////////////////////////////////////////////////////////////////// |
| 517 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 518 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDisplacementMapEffect); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 519 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 520 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 521 | std::unique_ptr<GrFragmentProcessor> GrDisplacementMapEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 522 | int texIdxDispl = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 523 | GrProcessorUnitTest::kAlphaTextureIdx; |
| 524 | int texIdxColor = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 525 | GrProcessorUnitTest::kAlphaTextureIdx; |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 526 | sk_sp<GrTextureProxy> dispProxy = d->textureProxy(texIdxDispl); |
| 527 | sk_sp<GrTextureProxy> colorProxy = d->textureProxy(texIdxColor); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 528 | static const int kMaxComponent = 4; |
| 529 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector = |
| 530 | static_cast<SkDisplacementMapEffect::ChannelSelectorType>( |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 531 | d->fRandom->nextRangeU(1, kMaxComponent)); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 532 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector = |
| 533 | static_cast<SkDisplacementMapEffect::ChannelSelectorType>( |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 534 | d->fRandom->nextRangeU(1, kMaxComponent)); |
| 535 | SkVector scale = SkVector::Make(d->fRandom->nextRangeScalar(0, 100.0f), |
| 536 | d->fRandom->nextRangeScalar(0, 100.0f)); |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 537 | SkISize colorDimensions; |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 538 | colorDimensions.fWidth = d->fRandom->nextRangeU(0, colorProxy->width()); |
| 539 | colorDimensions.fHeight = d->fRandom->nextRangeU(0, colorProxy->height()); |
Brian Osman | 721a2c4 | 2016-12-20 15:00:09 -0500 | [diff] [blame] | 540 | auto colorSpaceXform = GrTest::TestColorXform(d->fRandom); |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 541 | return GrDisplacementMapEffect::Make(xChannelSelector, yChannelSelector, scale, |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 542 | std::move(dispProxy), SkMatrix::I(), |
| 543 | std::move(colorProxy), colorSpaceXform, |
Brian Osman | 721a2c4 | 2016-12-20 15:00:09 -0500 | [diff] [blame] | 544 | colorDimensions); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 545 | } |
Brian Salomon | ffc2ec4 | 2017-07-25 14:59:03 -0400 | [diff] [blame] | 546 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 547 | #endif |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 548 | |
| 549 | /////////////////////////////////////////////////////////////////////////////// |
| 550 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 551 | void GrGLDisplacementMapEffect::emitCode(EmitArgs& args) { |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 552 | const GrDisplacementMapEffect& displacementMap = args.fFp.cast<GrDisplacementMapEffect>(); |
| 553 | const GrTextureDomain& domain = displacementMap.domain(); |
sugoi@google.com | 4d94e1b | 2013-03-21 18:38:03 +0000 | [diff] [blame] | 554 | |
Ethan Nicholas | 88d99c6 | 2017-08-16 16:41:30 -0400 | [diff] [blame] | 555 | fScaleUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, "Scale"); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 556 | const char* scaleUni = args.fUniformHandler->getUniformCStr(fScaleUni); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 557 | const char* dColor = "dColor"; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 558 | const char* cCoords = "cCoords"; |
Hal Canary | 55325b7 | 2017-01-03 10:36:17 -0500 | [diff] [blame] | 559 | const char* nearZero = "1e-6"; // Since 6.10352e-5 is the smallest half float, use |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 560 | // a number smaller than that to approximate 0, but |
| 561 | // leave room for 32-bit float GPU rounding errors. |
| 562 | |
Brian Osman | c624d9d | 2017-03-08 11:42:02 -0500 | [diff] [blame] | 563 | fColorSpaceHelper.emitCode(args.fUniformHandler, displacementMap.colorSpaceXform()); |
Brian Osman | 721a2c4 | 2016-12-20 15:00:09 -0500 | [diff] [blame] | 564 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 565 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | 88d99c6 | 2017-08-16 16:41:30 -0400 | [diff] [blame] | 566 | fragBuilder->codeAppendf("\t\thalf4 %s = ", dColor); |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 567 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], args.fTransformedCoords[0].c_str(), |
| 568 | args.fTransformedCoords[0].getType()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 569 | fragBuilder->codeAppend(";\n"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 570 | |
| 571 | // Unpremultiply the displacement |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 572 | fragBuilder->codeAppendf( |
Ethan Nicholas | 88d99c6 | 2017-08-16 16:41:30 -0400 | [diff] [blame] | 573 | "\t\t%s.rgb = (%s.a < %s) ? half3(0.0) : clamp(%s.rgb / %s.a, 0.0, 1.0);", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 574 | dColor, dColor, nearZero, dColor, dColor); |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 575 | SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[1]); |
Ethan Nicholas | 88d99c6 | 2017-08-16 16:41:30 -0400 | [diff] [blame] | 576 | fragBuilder->codeAppendf("\t\thighfloat2 %s = %s + %s*(%s.", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 577 | cCoords, coords2D.c_str(), scaleUni, dColor); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 578 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 579 | switch (displacementMap.xChannelSelector()) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 580 | case SkDisplacementMapEffect::kR_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 581 | fragBuilder->codeAppend("r"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 582 | break; |
| 583 | case SkDisplacementMapEffect::kG_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 584 | fragBuilder->codeAppend("g"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 585 | break; |
| 586 | case SkDisplacementMapEffect::kB_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 587 | fragBuilder->codeAppend("b"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 588 | break; |
| 589 | case SkDisplacementMapEffect::kA_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 590 | fragBuilder->codeAppend("a"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 591 | break; |
| 592 | case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
| 593 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 594 | SkDEBUGFAIL("Unknown X channel selector"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 595 | } |
| 596 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 597 | switch (displacementMap.yChannelSelector()) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 598 | case SkDisplacementMapEffect::kR_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 599 | fragBuilder->codeAppend("r"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 600 | break; |
| 601 | case SkDisplacementMapEffect::kG_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 602 | fragBuilder->codeAppend("g"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 603 | break; |
| 604 | case SkDisplacementMapEffect::kB_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 605 | fragBuilder->codeAppend("b"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 606 | break; |
| 607 | case SkDisplacementMapEffect::kA_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 608 | fragBuilder->codeAppend("a"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 609 | break; |
| 610 | case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
| 611 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 612 | SkDEBUGFAIL("Unknown Y channel selector"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 613 | } |
Ethan Nicholas | 88d99c6 | 2017-08-16 16:41:30 -0400 | [diff] [blame] | 614 | fragBuilder->codeAppend("-half2(0.5));\t\t"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 615 | |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 616 | fGLDomain.sampleTexture(fragBuilder, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 617 | args.fUniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 618 | args.fShaderCaps, |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 619 | domain, |
| 620 | args.fOutputColor, |
| 621 | SkString(cCoords), |
Brian Osman | 721a2c4 | 2016-12-20 15:00:09 -0500 | [diff] [blame] | 622 | args.fTexSamplers[1], |
| 623 | nullptr, |
Brian Osman | c624d9d | 2017-03-08 11:42:02 -0500 | [diff] [blame] | 624 | &fColorSpaceHelper); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 625 | fragBuilder->codeAppend(";\n"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 626 | } |
| 627 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 628 | void GrGLDisplacementMapEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 629 | const GrFragmentProcessor& proc) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 630 | const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMapEffect>(); |
Robert Phillips | c686ce3 | 2017-07-21 14:12:29 -0400 | [diff] [blame] | 631 | GrSurfaceProxy* proxy = displacementMap.textureSampler(1).proxy(); |
| 632 | GrTexture* colorTex = proxy->priv().peekTexture(); |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 633 | |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 634 | SkScalar scaleX = displacementMap.scale().fX / colorTex->width(); |
| 635 | SkScalar scaleY = displacementMap.scale().fY / colorTex->height(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 636 | pdman.set2f(fScaleUni, SkScalarToFloat(scaleX), |
Robert Phillips | c686ce3 | 2017-07-21 14:12:29 -0400 | [diff] [blame] | 637 | proxy->origin() == kTopLeft_GrSurfaceOrigin ? |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 638 | SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY)); |
Robert Phillips | c686ce3 | 2017-07-21 14:12:29 -0400 | [diff] [blame] | 639 | fGLDomain.setData(pdman, displacementMap.domain(), proxy); |
Brian Osman | 721a2c4 | 2016-12-20 15:00:09 -0500 | [diff] [blame] | 640 | if (SkToBool(displacementMap.colorSpaceXform())) { |
Brian Osman | c624d9d | 2017-03-08 11:42:02 -0500 | [diff] [blame] | 641 | fColorSpaceHelper.setData(pdman, displacementMap.colorSpaceXform()); |
Brian Osman | 721a2c4 | 2016-12-20 15:00:09 -0500 | [diff] [blame] | 642 | } |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 643 | } |
| 644 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 645 | void GrGLDisplacementMapEffect::GenKey(const GrProcessor& proc, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 646 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 647 | const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMapEffect>(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 648 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 649 | uint32_t xKey = displacementMap.xChannelSelector(); |
| 650 | uint32_t yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBits; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 651 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 652 | b->add32(xKey | yKey); |
Brian Osman | 721a2c4 | 2016-12-20 15:00:09 -0500 | [diff] [blame] | 653 | b->add32(GrColorSpaceXform::XformKey(displacementMap.colorSpaceXform())); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 654 | } |
| 655 | #endif |