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