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