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