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. |
| 231 | if (!this->applyCropRect(ctx, color, colorOffset, &bounds)) { |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 232 | return false; |
| 233 | } |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 234 | SkIRect displBounds; |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 235 | if (!this->applyCropRect(ctx, proxy, displ, &displOffset, &displBounds, &displ)) { |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | if (!bounds.intersect(displBounds)) { |
| 239 | return false; |
| 240 | } |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 241 | SkAutoLockPixels alp_displacement(displ), alp_color(color); |
| 242 | if (!displ.getPixels() || !color.getPixels()) { |
| 243 | return false; |
| 244 | } |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 245 | |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 246 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
| 247 | if (!device) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 248 | return false; |
| 249 | } |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 250 | *dst = device->accessBitmap(false); |
| 251 | SkAutoLockPixels alp_dst(*dst); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 252 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 253 | SkVector scale = SkVector::Make(fScale, fScale); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 254 | ctx.ctm().mapVectors(&scale, 1); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 255 | SkIRect colorBounds = bounds; |
| 256 | colorBounds.offset(-colorOffset); |
| 257 | |
| 258 | computeDisplacement(fXChannelSelector, fYChannelSelector, scale, dst, |
| 259 | &displ, colorOffset - displOffset, &color, colorBounds); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 260 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 261 | offset->fX = bounds.left(); |
| 262 | offset->fY = bounds.top(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 263 | return true; |
| 264 | } |
| 265 | |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 266 | void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst) const { |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 267 | if (this->getColorInput()) { |
| 268 | this->getColorInput()->computeFastBounds(src, dst); |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 269 | } else { |
| 270 | *dst = src; |
| 271 | } |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 272 | dst->outset(fScale * SK_ScalarHalf, fScale * SK_ScalarHalf); |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 273 | } |
| 274 | |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 275 | void SkDisplacementMapEffect::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm, |
| 276 | SkIRect* dst, MapDirection) const { |
| 277 | *dst = src; |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 278 | SkVector scale = SkVector::Make(fScale, fScale); |
| 279 | ctm.mapVectors(&scale, 1); |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 280 | dst->outset(SkScalarCeilToInt(scale.fX * SK_ScalarHalf), |
| 281 | SkScalarCeilToInt(scale.fY * SK_ScalarHalf)); |
| 282 | } |
| 283 | |
| 284 | bool SkDisplacementMapEffect::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, |
| 285 | SkIRect* dst) const { |
| 286 | SkIRect bounds; |
| 287 | this->onFilterNodeBounds(src, ctm, &bounds, kReverse_MapDirection); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 288 | if (this->getColorInput()) { |
| 289 | return this->getColorInput()->filterBounds(bounds, ctm, dst); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 290 | } |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 291 | *dst = bounds; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 295 | #ifndef SK_IGNORE_TO_STRING |
| 296 | void SkDisplacementMapEffect::toString(SkString* str) const { |
| 297 | str->appendf("SkDisplacementMapEffect: ("); |
| 298 | str->appendf("scale: %f ", fScale); |
| 299 | str->appendf("displacement: ("); |
| 300 | if (this->getDisplacementInput()) { |
| 301 | this->getDisplacementInput()->toString(str); |
| 302 | } |
| 303 | str->appendf(") color: ("); |
| 304 | if (this->getColorInput()) { |
| 305 | this->getColorInput()->toString(str); |
| 306 | } |
| 307 | str->appendf("))"); |
| 308 | } |
| 309 | #endif |
| 310 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 311 | /////////////////////////////////////////////////////////////////////////////// |
| 312 | |
| 313 | #if SK_SUPPORT_GPU |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 314 | class GrGLDisplacementMapEffect : public GrGLSLFragmentProcessor { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 315 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 316 | GrGLDisplacementMapEffect(const GrProcessor&); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 317 | virtual ~GrGLDisplacementMapEffect(); |
| 318 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 319 | virtual void emitCode(EmitArgs&) override; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 320 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 321 | static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 322 | |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 323 | const GrTextureDomain::GLDomain& glDomain() const { return fGLDomain; } |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 324 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 325 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 326 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 327 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 328 | private: |
| 329 | SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; |
| 330 | SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 331 | GrGLSLProgramDataManager::UniformHandle fScaleUni; |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 332 | GrTextureDomain::GLDomain fGLDomain; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 333 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 334 | typedef GrGLSLFragmentProcessor INHERITED; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | /////////////////////////////////////////////////////////////////////////////// |
| 338 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 339 | class GrDisplacementMapEffect : public GrFragmentProcessor { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 340 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 341 | static GrFragmentProcessor* Create( |
| 342 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, |
| 343 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, SkVector scale, |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 344 | GrTexture* displacement, const SkMatrix& offsetMatrix, GrTexture* color, |
| 345 | const SkISize& colorDimensions) { |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 346 | return new GrDisplacementMapEffect(xChannelSelector, yChannelSelector, scale, displacement, |
| 347 | offsetMatrix, color, colorDimensions); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 348 | } |
| 349 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 350 | virtual ~GrDisplacementMapEffect(); |
| 351 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 352 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector() const |
| 353 | { return fXChannelSelector; } |
| 354 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector() const |
| 355 | { return fYChannelSelector; } |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 356 | const SkVector& scale() const { return fScale; } |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 357 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 358 | const char* name() const override { return "DisplacementMap"; } |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 359 | const GrTextureDomain& domain() const { return fDomain; } |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 360 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 361 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 362 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 363 | return new GrGLDisplacementMapEffect(*this); |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 364 | } |
| 365 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 366 | virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 367 | GrProcessorKeyBuilder* b) const override { |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 368 | GrGLDisplacementMapEffect::GenKey(*this, caps, b); |
| 369 | } |
| 370 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 371 | bool onIsEqual(const GrFragmentProcessor&) const override; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 372 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 373 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 374 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 375 | GrDisplacementMapEffect(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 376 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 377 | const SkVector& scale, |
| 378 | GrTexture* displacement, const SkMatrix& offsetMatrix, |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 379 | GrTexture* color, |
| 380 | const SkISize& colorDimensions); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 381 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 382 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 383 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 384 | GrCoordTransform fDisplacementTransform; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 385 | GrTextureAccess fDisplacementAccess; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 386 | GrCoordTransform fColorTransform; |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 387 | GrTextureDomain fDomain; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 388 | GrTextureAccess fColorAccess; |
| 389 | SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; |
| 390 | SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 391 | SkVector fScale; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 392 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 393 | typedef GrFragmentProcessor INHERITED; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 394 | }; |
| 395 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 396 | 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] | 397 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 398 | SkBitmap colorBM = src; |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 399 | SkIPoint colorOffset = SkIPoint::Make(0, 0); |
senorblanco | 9a70b6e | 2015-10-16 11:35:14 -0700 | [diff] [blame] | 400 | if (!this->filterInputGPU(1, proxy, src, ctx, &colorBM, &colorOffset)) { |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 401 | return false; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 402 | } |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 403 | SkBitmap displacementBM = src; |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 404 | SkIPoint displacementOffset = SkIPoint::Make(0, 0); |
senorblanco | 9a70b6e | 2015-10-16 11:35:14 -0700 | [diff] [blame] | 405 | if (!this->filterInputGPU(0, proxy, src, ctx, &displacementBM, &displacementOffset)) { |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 406 | return false; |
| 407 | } |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 408 | SkIRect bounds; |
| 409 | // Since GrDisplacementMapEffect does bounds checking on color pixel access, we don't need to |
| 410 | // pad the color bitmap to bounds here. |
| 411 | if (!this->applyCropRect(ctx, colorBM, colorOffset, &bounds)) { |
| 412 | return false; |
| 413 | } |
| 414 | SkIRect displBounds; |
| 415 | if (!this->applyCropRect(ctx, proxy, displacementBM, |
| 416 | &displacementOffset, &displBounds, &displacementBM)) { |
| 417 | return false; |
| 418 | } |
| 419 | if (!bounds.intersect(displBounds)) { |
| 420 | return false; |
| 421 | } |
| 422 | GrTexture* color = colorBM.getTexture(); |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 423 | GrTexture* displacement = displacementBM.getTexture(); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 424 | GrContext* context = color->getContext(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 425 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 426 | GrSurfaceDesc desc; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 427 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
senorblanco@chromium.org | d9cdff6 | 2014-03-17 23:41:40 +0000 | [diff] [blame] | 428 | desc.fWidth = bounds.width(); |
| 429 | desc.fHeight = bounds.height(); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 430 | desc.fConfig = kSkia8888_GrPixelConfig; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 431 | |
reed | 4e23cda | 2016-01-11 10:56:59 -0800 | [diff] [blame] | 432 | SkAutoTUnref<GrTexture> dst(context->textureProvider()->createApproxTexture(desc)); |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 433 | |
| 434 | if (!dst) { |
senorblanco | 673d973 | 2014-08-15 10:48:43 -0700 | [diff] [blame] | 435 | return false; |
| 436 | } |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 437 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 438 | SkVector scale = SkVector::Make(fScale, fScale); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 439 | ctx.ctm().mapVectors(&scale, 1); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 440 | |
| 441 | GrPaint paint; |
bsalomon | 6267f81 | 2014-08-29 15:05:53 -0700 | [diff] [blame] | 442 | SkMatrix offsetMatrix = GrCoordTransform::MakeDivByTextureWHMatrix(displacement); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 443 | offsetMatrix.preTranslate(SkIntToScalar(colorOffset.fX - displacementOffset.fX), |
| 444 | SkIntToScalar(colorOffset.fY - displacementOffset.fY)); |
| 445 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 446 | paint.addColorFragmentProcessor( |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 447 | GrDisplacementMapEffect::Create(fXChannelSelector, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 448 | fYChannelSelector, |
| 449 | scale, |
| 450 | displacement, |
| 451 | offsetMatrix, |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 452 | color, |
| 453 | colorBM.dimensions()))->unref(); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 454 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 455 | SkIRect colorBounds = bounds; |
| 456 | colorBounds.offset(-colorOffset); |
| 457 | SkMatrix matrix; |
| 458 | matrix.setTranslate(-SkIntToScalar(colorBounds.x()), |
| 459 | -SkIntToScalar(colorBounds.y())); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 460 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 461 | SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(dst->asRenderTarget())); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 462 | if (!drawContext) { |
| 463 | return false; |
| 464 | } |
| 465 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 466 | drawContext->drawRect(GrClip::WideOpen(), paint, matrix, SkRect::Make(colorBounds)); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 467 | offset->fX = bounds.left(); |
| 468 | offset->fY = bounds.top(); |
robertphillips | 1de87df | 2016-01-14 06:03:29 -0800 | [diff] [blame^] | 469 | GrWrapTextureInBitmap(dst, bounds.width(), bounds.height(), false, result); |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 470 | return true; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /////////////////////////////////////////////////////////////////////////////// |
| 474 | |
| 475 | GrDisplacementMapEffect::GrDisplacementMapEffect( |
| 476 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, |
| 477 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 478 | const SkVector& scale, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 479 | GrTexture* displacement, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 480 | const SkMatrix& offsetMatrix, |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 481 | GrTexture* color, |
| 482 | const SkISize& colorDimensions) |
bsalomon | 9f876a3 | 2014-12-09 10:51:07 -0800 | [diff] [blame] | 483 | : fDisplacementTransform(kLocal_GrCoordSet, offsetMatrix, displacement, |
| 484 | GrTextureParams::kNone_FilterMode) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 485 | , fDisplacementAccess(displacement) |
bsalomon | 9f876a3 | 2014-12-09 10:51:07 -0800 | [diff] [blame] | 486 | , fColorTransform(kLocal_GrCoordSet, color, GrTextureParams::kNone_FilterMode) |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 487 | , fDomain(GrTextureDomain::MakeTexelDomain(color, SkIRect::MakeSize(colorDimensions)), |
| 488 | GrTextureDomain::kDecal_Mode) |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 489 | , fColorAccess(color) |
| 490 | , fXChannelSelector(xChannelSelector) |
| 491 | , fYChannelSelector(yChannelSelector) |
| 492 | , fScale(scale) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 493 | this->initClassID<GrDisplacementMapEffect>(); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 494 | this->addCoordTransform(&fDisplacementTransform); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 495 | this->addTextureAccess(&fDisplacementAccess); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 496 | this->addCoordTransform(&fColorTransform); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 497 | this->addTextureAccess(&fColorAccess); |
| 498 | } |
| 499 | |
| 500 | GrDisplacementMapEffect::~GrDisplacementMapEffect() { |
| 501 | } |
| 502 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 503 | bool GrDisplacementMapEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 504 | const GrDisplacementMapEffect& s = sBase.cast<GrDisplacementMapEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 505 | return fXChannelSelector == s.fXChannelSelector && |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 506 | fYChannelSelector == s.fYChannelSelector && |
| 507 | fScale == s.fScale; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 508 | } |
| 509 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 510 | void GrDisplacementMapEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 511 | // Any displacement offset bringing a pixel out of bounds will output a color of (0,0,0,0), |
| 512 | // 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] | 513 | // 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] | 514 | // alpha is 0. Since this isn't trivial to compute at this point, let's assume the output is |
| 515 | // not of constant color when a displacement effect is applied. |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 516 | inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /////////////////////////////////////////////////////////////////////////////// |
| 520 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 521 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDisplacementMapEffect); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 522 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 523 | const GrFragmentProcessor* GrDisplacementMapEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 524 | int texIdxDispl = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 525 | GrProcessorUnitTest::kAlphaTextureIdx; |
| 526 | int texIdxColor = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 527 | GrProcessorUnitTest::kAlphaTextureIdx; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 528 | static const int kMaxComponent = 4; |
| 529 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector = |
| 530 | static_cast<SkDisplacementMapEffect::ChannelSelectorType>( |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 531 | d->fRandom->nextRangeU(1, kMaxComponent)); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 532 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector = |
| 533 | static_cast<SkDisplacementMapEffect::ChannelSelectorType>( |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 534 | d->fRandom->nextRangeU(1, kMaxComponent)); |
| 535 | SkVector scale = SkVector::Make(d->fRandom->nextRangeScalar(0, 100.0f), |
| 536 | d->fRandom->nextRangeScalar(0, 100.0f)); |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 537 | SkISize colorDimensions; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 538 | colorDimensions.fWidth = d->fRandom->nextRangeU(0, d->fTextures[texIdxColor]->width()); |
| 539 | colorDimensions.fHeight = d->fRandom->nextRangeU(0, d->fTextures[texIdxColor]->height()); |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 540 | return GrDisplacementMapEffect::Create(xChannelSelector, yChannelSelector, scale, |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 541 | d->fTextures[texIdxDispl], SkMatrix::I(), |
| 542 | d->fTextures[texIdxColor], colorDimensions); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /////////////////////////////////////////////////////////////////////////////// |
| 546 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 547 | GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrProcessor& proc) |
| 548 | : fXChannelSelector(proc.cast<GrDisplacementMapEffect>().xChannelSelector()) |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 549 | , fYChannelSelector(proc.cast<GrDisplacementMapEffect>().yChannelSelector()) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | GrGLDisplacementMapEffect::~GrGLDisplacementMapEffect() { |
| 553 | } |
| 554 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 555 | void GrGLDisplacementMapEffect::emitCode(EmitArgs& args) { |
| 556 | const GrTextureDomain& domain = args.fFp.cast<GrDisplacementMapEffect>().domain(); |
sugoi@google.com | 4d94e1b | 2013-03-21 18:38:03 +0000 | [diff] [blame] | 557 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 558 | fScaleUni = args.fUniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility, |
| 559 | kVec2f_GrSLType, kDefault_GrSLPrecision, "Scale"); |
| 560 | const char* scaleUni = args.fUniformHandler->getUniformCStr(fScaleUni); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 561 | const char* dColor = "dColor"; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 562 | const char* cCoords = "cCoords"; |
| 563 | const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half float, use |
| 564 | // a number smaller than that to approximate 0, but |
| 565 | // leave room for 32-bit float GPU rounding errors. |
| 566 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 567 | GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 568 | fragBuilder->codeAppendf("\t\tvec4 %s = ", dColor); |
| 569 | fragBuilder->appendTextureLookup(args.fSamplers[0], args.fCoords[0].c_str(), |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 570 | args.fCoords[0].getType()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 571 | fragBuilder->codeAppend(";\n"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 572 | |
| 573 | // Unpremultiply the displacement |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 574 | fragBuilder->codeAppendf( |
| 575 | "\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb / %s.a, 0.0, 1.0);", |
| 576 | dColor, dColor, nearZero, dColor, dColor); |
| 577 | SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 1); |
| 578 | fragBuilder->codeAppendf("\t\tvec2 %s = %s + %s*(%s.", |
| 579 | cCoords, coords2D.c_str(), scaleUni, dColor); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 580 | |
| 581 | switch (fXChannelSelector) { |
| 582 | case SkDisplacementMapEffect::kR_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 583 | fragBuilder->codeAppend("r"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 584 | break; |
| 585 | case SkDisplacementMapEffect::kG_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 586 | fragBuilder->codeAppend("g"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 587 | break; |
| 588 | case SkDisplacementMapEffect::kB_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 589 | fragBuilder->codeAppend("b"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 590 | break; |
| 591 | case SkDisplacementMapEffect::kA_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 592 | fragBuilder->codeAppend("a"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 593 | break; |
| 594 | case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
| 595 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 596 | SkDEBUGFAIL("Unknown X channel selector"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | switch (fYChannelSelector) { |
| 600 | case SkDisplacementMapEffect::kR_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 601 | fragBuilder->codeAppend("r"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 602 | break; |
| 603 | case SkDisplacementMapEffect::kG_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 604 | fragBuilder->codeAppend("g"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 605 | break; |
| 606 | case SkDisplacementMapEffect::kB_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 607 | fragBuilder->codeAppend("b"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 608 | break; |
| 609 | case SkDisplacementMapEffect::kA_ChannelSelectorType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 610 | fragBuilder->codeAppend("a"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 611 | break; |
| 612 | case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
| 613 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 614 | SkDEBUGFAIL("Unknown Y channel selector"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 615 | } |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 616 | fragBuilder->codeAppend("-vec2(0.5));\t\t"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 617 | |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 618 | fGLDomain.sampleTexture(fragBuilder, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 619 | args.fUniformHandler, |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 620 | args.fGLSLCaps, |
| 621 | domain, |
| 622 | args.fOutputColor, |
| 623 | SkString(cCoords), |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 624 | args.fSamplers[1]); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 625 | fragBuilder->codeAppend(";\n"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 626 | } |
| 627 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 628 | void GrGLDisplacementMapEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 629 | const GrProcessor& proc) { |
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 | GrTexture* colorTex = displacementMap.texture(1); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 632 | SkScalar scaleX = displacementMap.scale().fX / colorTex->width(); |
| 633 | SkScalar scaleY = displacementMap.scale().fY / colorTex->height(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 634 | pdman.set2f(fScaleUni, SkScalarToFloat(scaleX), |
| 635 | colorTex->origin() == kTopLeft_GrSurfaceOrigin ? |
| 636 | SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY)); |
senorblanco | d762ca2 | 2015-04-07 12:16:55 -0700 | [diff] [blame] | 637 | fGLDomain.setData(pdman, displacementMap.domain(), colorTex->origin()); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 638 | } |
| 639 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 640 | void GrGLDisplacementMapEffect::GenKey(const GrProcessor& proc, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 641 | const GrGLSLCaps&, GrProcessorKeyBuilder* b) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 642 | const GrDisplacementMapEffect& displacementMap = proc.cast<GrDisplacementMapEffect>(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 643 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 644 | uint32_t xKey = displacementMap.xChannelSelector(); |
| 645 | uint32_t yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBits; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 646 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 647 | b->add32(xKey | yKey); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 648 | } |
| 649 | #endif |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 650 | |