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