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