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