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