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" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 9 | #include "SkReadBuffer.h" |
| 10 | #include "SkWriteBuffer.h" |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 11 | #include "SkUnPreMultiply.h" |
sugoi@google.com | 8cec63e | 2013-01-15 15:59:45 +0000 | [diff] [blame] | 12 | #include "SkColorPriv.h" |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 13 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 14 | #include "GrContext.h" |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 15 | #include "GrCoordTransform.h" |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 16 | #include "gl/GrGLEffect.h" |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 17 | #include "GrTBackendEffectFactory.h" |
| 18 | #endif |
| 19 | |
| 20 | namespace { |
| 21 | |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 22 | #define kChannelSelectorKeyBits 3; // Max value is 4, so 3 bits are required at most |
| 23 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 24 | template<SkDisplacementMapEffect::ChannelSelectorType type> |
| 25 | uint32_t getValue(SkColor, const SkUnPreMultiply::Scale*) { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 26 | SkDEBUGFAIL("Unknown channel selector"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | template<> uint32_t getValue<SkDisplacementMapEffect::kR_ChannelSelectorType>( |
| 31 | SkColor l, const SkUnPreMultiply::Scale* table) { |
| 32 | return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedR32(l)); |
| 33 | } |
| 34 | |
| 35 | template<> uint32_t getValue<SkDisplacementMapEffect::kG_ChannelSelectorType>( |
| 36 | SkColor l, const SkUnPreMultiply::Scale* table) { |
| 37 | return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedG32(l)); |
| 38 | } |
| 39 | |
| 40 | template<> uint32_t getValue<SkDisplacementMapEffect::kB_ChannelSelectorType>( |
| 41 | SkColor l, const SkUnPreMultiply::Scale* table) { |
| 42 | return SkUnPreMultiply::ApplyScale(table[SkGetPackedA32(l)], SkGetPackedB32(l)); |
| 43 | } |
| 44 | |
| 45 | template<> uint32_t getValue<SkDisplacementMapEffect::kA_ChannelSelectorType>( |
| 46 | SkColor l, const SkUnPreMultiply::Scale*) { |
| 47 | return SkGetPackedA32(l); |
| 48 | } |
| 49 | |
| 50 | template<SkDisplacementMapEffect::ChannelSelectorType typeX, |
| 51 | SkDisplacementMapEffect::ChannelSelectorType typeY> |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 52 | void computeDisplacement(const SkVector& scale, SkBitmap* dst, |
| 53 | SkBitmap* displ, const SkIPoint& offset, |
| 54 | SkBitmap* src, |
| 55 | const SkIRect& bounds) |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 56 | { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 57 | static const SkScalar Inv8bit = SkScalarDiv(SK_Scalar1, 255.0f); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 58 | const int srcW = src->width(); |
| 59 | const int srcH = src->height(); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 60 | const SkVector scaleForColor = SkVector::Make(SkScalarMul(scale.fX, Inv8bit), |
| 61 | SkScalarMul(scale.fY, Inv8bit)); |
| 62 | const SkVector scaleAdj = SkVector::Make(SK_ScalarHalf - SkScalarMul(scale.fX, SK_ScalarHalf), |
| 63 | SK_ScalarHalf - SkScalarMul(scale.fY, SK_ScalarHalf)); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 64 | const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable(); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 65 | SkPMColor* dstPtr = dst->getAddr32(0, 0); |
| 66 | for (int y = bounds.top(); y < bounds.bottom(); ++y) { |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 67 | const SkPMColor* displPtr = displ->getAddr32(bounds.left() + offset.fX, |
| 68 | y + offset.fY); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 69 | for (int x = bounds.left(); x < bounds.right(); ++x, ++displPtr) { |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 70 | const SkScalar displX = SkScalarMul(scaleForColor.fX, |
| 71 | SkIntToScalar(getValue<typeX>(*displPtr, table))) + scaleAdj.fX; |
| 72 | const SkScalar displY = SkScalarMul(scaleForColor.fY, |
| 73 | SkIntToScalar(getValue<typeY>(*displPtr, table))) + scaleAdj.fY; |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 74 | // Truncate the displacement values |
| 75 | const int srcX = x + SkScalarTruncToInt(displX); |
| 76 | const int srcY = y + SkScalarTruncToInt(displY); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 77 | *dstPtr++ = ((srcX < 0) || (srcX >= srcW) || (srcY < 0) || (srcY >= srcH)) ? |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 78 | 0 : *(src->getAddr32(srcX, srcY)); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | template<SkDisplacementMapEffect::ChannelSelectorType typeX> |
| 84 | void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 85 | const SkVector& scale, SkBitmap* dst, |
| 86 | SkBitmap* displ, const SkIPoint& offset, |
| 87 | SkBitmap* src, |
| 88 | const SkIRect& bounds) |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 89 | { |
| 90 | switch (yChannelSelector) { |
| 91 | case SkDisplacementMapEffect::kR_ChannelSelectorType: |
| 92 | computeDisplacement<typeX, SkDisplacementMapEffect::kR_ChannelSelectorType>( |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 93 | scale, dst, displ, offset, src, bounds); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 94 | break; |
| 95 | case SkDisplacementMapEffect::kG_ChannelSelectorType: |
| 96 | computeDisplacement<typeX, SkDisplacementMapEffect::kG_ChannelSelectorType>( |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 97 | scale, dst, displ, offset, src, bounds); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 98 | break; |
| 99 | case SkDisplacementMapEffect::kB_ChannelSelectorType: |
| 100 | computeDisplacement<typeX, SkDisplacementMapEffect::kB_ChannelSelectorType>( |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 101 | scale, dst, displ, offset, src, bounds); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 102 | break; |
| 103 | case SkDisplacementMapEffect::kA_ChannelSelectorType: |
| 104 | computeDisplacement<typeX, SkDisplacementMapEffect::kA_ChannelSelectorType>( |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 105 | scale, dst, displ, offset, src, bounds); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 106 | break; |
| 107 | case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
| 108 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 109 | SkDEBUGFAIL("Unknown Y channel selector"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | void computeDisplacement(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, |
| 114 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 115 | const SkVector& scale, SkBitmap* dst, |
| 116 | SkBitmap* displ, const SkIPoint& offset, |
| 117 | SkBitmap* src, |
| 118 | const SkIRect& bounds) |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 119 | { |
| 120 | switch (xChannelSelector) { |
| 121 | case SkDisplacementMapEffect::kR_ChannelSelectorType: |
| 122 | computeDisplacement<SkDisplacementMapEffect::kR_ChannelSelectorType>( |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 123 | yChannelSelector, scale, dst, displ, offset, src, bounds); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 124 | break; |
| 125 | case SkDisplacementMapEffect::kG_ChannelSelectorType: |
| 126 | computeDisplacement<SkDisplacementMapEffect::kG_ChannelSelectorType>( |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 127 | yChannelSelector, scale, dst, displ, offset, src, bounds); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 128 | break; |
| 129 | case SkDisplacementMapEffect::kB_ChannelSelectorType: |
| 130 | computeDisplacement<SkDisplacementMapEffect::kB_ChannelSelectorType>( |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 131 | yChannelSelector, scale, dst, displ, offset, src, bounds); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 132 | break; |
| 133 | case SkDisplacementMapEffect::kA_ChannelSelectorType: |
| 134 | computeDisplacement<SkDisplacementMapEffect::kA_ChannelSelectorType>( |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 135 | yChannelSelector, scale, dst, displ, offset, src, bounds); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 136 | break; |
| 137 | case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
| 138 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 139 | SkDEBUGFAIL("Unknown X channel selector"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 143 | bool channel_selector_type_is_valid(SkDisplacementMapEffect::ChannelSelectorType cst) { |
| 144 | switch (cst) { |
| 145 | case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
| 146 | case SkDisplacementMapEffect::kR_ChannelSelectorType: |
| 147 | case SkDisplacementMapEffect::kG_ChannelSelectorType: |
| 148 | case SkDisplacementMapEffect::kB_ChannelSelectorType: |
| 149 | case SkDisplacementMapEffect::kA_ChannelSelectorType: |
| 150 | return true; |
| 151 | default: |
| 152 | break; |
| 153 | } |
| 154 | return false; |
| 155 | } |
| 156 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 157 | } // end namespace |
| 158 | |
| 159 | /////////////////////////////////////////////////////////////////////////////// |
| 160 | |
| 161 | SkDisplacementMapEffect::SkDisplacementMapEffect(ChannelSelectorType xChannelSelector, |
| 162 | ChannelSelectorType yChannelSelector, |
| 163 | SkScalar scale, |
| 164 | SkImageFilter* displacement, |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 165 | SkImageFilter* color, |
| 166 | const CropRect* cropRect) |
| 167 | : INHERITED(displacement, color, cropRect) |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 168 | , fXChannelSelector(xChannelSelector) |
| 169 | , fYChannelSelector(yChannelSelector) |
| 170 | , fScale(scale) |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | SkDisplacementMapEffect::~SkDisplacementMapEffect() { |
| 175 | } |
| 176 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 177 | SkDisplacementMapEffect::SkDisplacementMapEffect(SkReadBuffer& buffer) |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 178 | : INHERITED(2, buffer) |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 179 | { |
| 180 | fXChannelSelector = (SkDisplacementMapEffect::ChannelSelectorType) buffer.readInt(); |
| 181 | fYChannelSelector = (SkDisplacementMapEffect::ChannelSelectorType) buffer.readInt(); |
| 182 | fScale = buffer.readScalar(); |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 183 | buffer.validate(channel_selector_type_is_valid(fXChannelSelector) && |
| 184 | channel_selector_type_is_valid(fYChannelSelector) && |
| 185 | SkScalarIsFinite(fScale)); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 186 | } |
| 187 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 188 | void SkDisplacementMapEffect::flatten(SkWriteBuffer& buffer) const { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 189 | this->INHERITED::flatten(buffer); |
| 190 | buffer.writeInt((int) fXChannelSelector); |
| 191 | buffer.writeInt((int) fYChannelSelector); |
| 192 | buffer.writeScalar(fScale); |
| 193 | } |
| 194 | |
| 195 | bool SkDisplacementMapEffect::onFilterImage(Proxy* proxy, |
| 196 | const SkBitmap& src, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 197 | const Context& ctx, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 198 | SkBitmap* dst, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 199 | SkIPoint* offset) const { |
commit-bot@chromium.org | 6d7296a | 2013-12-19 17:00:46 +0000 | [diff] [blame] | 200 | SkBitmap displ = src, color = src; |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 201 | const SkImageFilter* colorInput = getColorInput(); |
| 202 | const SkImageFilter* displInput = getDisplacementInput(); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 203 | SkIPoint colorOffset = SkIPoint::Make(0, 0), displOffset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 204 | if ((colorInput && !colorInput->filterImage(proxy, src, ctx, &color, &colorOffset)) || |
| 205 | (displInput && !displInput->filterImage(proxy, src, ctx, &displ, &displOffset))) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 206 | return false; |
| 207 | } |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 208 | if ((displ.colorType() != kN32_SkColorType) || |
| 209 | (color.colorType() != kN32_SkColorType)) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 210 | return false; |
| 211 | } |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 212 | SkIRect bounds; |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 213 | // Since computeDisplacement does bounds checking on color pixel access, we don't need to pad |
| 214 | // the color bitmap to bounds here. |
| 215 | if (!this->applyCropRect(ctx, color, colorOffset, &bounds)) { |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 216 | return false; |
| 217 | } |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 218 | SkIRect displBounds; |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 219 | if (!this->applyCropRect(ctx, proxy, displ, &displOffset, &displBounds, &displ)) { |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | if (!bounds.intersect(displBounds)) { |
| 223 | return false; |
| 224 | } |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 225 | SkAutoLockPixels alp_displacement(displ), alp_color(color); |
| 226 | if (!displ.getPixels() || !color.getPixels()) { |
| 227 | return false; |
| 228 | } |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 229 | |
| 230 | dst->setConfig(color.config(), bounds.width(), bounds.height()); |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 231 | if (!dst->allocPixels()) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 235 | SkVector scale = SkVector::Make(fScale, fScale); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 236 | ctx.ctm().mapVectors(&scale, 1); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 237 | SkIRect colorBounds = bounds; |
| 238 | colorBounds.offset(-colorOffset); |
| 239 | |
| 240 | computeDisplacement(fXChannelSelector, fYChannelSelector, scale, dst, |
| 241 | &displ, colorOffset - displOffset, &color, colorBounds); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 242 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 243 | offset->fX = bounds.left(); |
| 244 | offset->fY = bounds.top(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 248 | void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst) const { |
| 249 | if (getColorInput()) { |
| 250 | getColorInput()->computeFastBounds(src, dst); |
| 251 | } else { |
| 252 | *dst = src; |
| 253 | } |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 254 | dst->outset(fScale * SK_ScalarHalf, fScale * SK_ScalarHalf); |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 255 | } |
| 256 | |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 257 | bool SkDisplacementMapEffect::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, |
| 258 | SkIRect* dst) const { |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 259 | SkIRect bounds = src; |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame^] | 260 | SkVector scale = SkVector::Make(fScale, fScale); |
| 261 | ctm.mapVectors(&scale, 1); |
| 262 | bounds.outset(SkScalarCeilToInt(scale.fX * SK_ScalarHalf), |
| 263 | SkScalarCeilToInt(scale.fY * SK_ScalarHalf)); |
| 264 | if (getColorInput()) { |
| 265 | return getColorInput()->filterBounds(bounds, ctm, dst); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 266 | } |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 267 | *dst = bounds; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 268 | return true; |
| 269 | } |
| 270 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 271 | /////////////////////////////////////////////////////////////////////////////// |
| 272 | |
| 273 | #if SK_SUPPORT_GPU |
| 274 | class GrGLDisplacementMapEffect : public GrGLEffect { |
| 275 | public: |
| 276 | GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 277 | const GrDrawEffect& drawEffect); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 278 | virtual ~GrGLDisplacementMapEffect(); |
| 279 | |
| 280 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 281 | const GrDrawEffect&, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 282 | EffectKey, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 283 | const char* outputColor, |
| 284 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 285 | const TransformedCoordsArray&, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 286 | const TextureSamplerArray&) SK_OVERRIDE; |
| 287 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 288 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 289 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 290 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 291 | |
| 292 | private: |
| 293 | SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; |
| 294 | SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 295 | GrGLUniformManager::UniformHandle fScaleUni; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 296 | |
| 297 | typedef GrGLEffect INHERITED; |
| 298 | }; |
| 299 | |
| 300 | /////////////////////////////////////////////////////////////////////////////// |
| 301 | |
| 302 | class GrDisplacementMapEffect : public GrEffect { |
| 303 | public: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 304 | static GrEffectRef* Create(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, |
| 305 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 306 | SkVector scale, |
| 307 | GrTexture* displacement, const SkMatrix& offsetMatrix, |
| 308 | GrTexture* color) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 309 | AutoEffectUnref effect(SkNEW_ARGS(GrDisplacementMapEffect, (xChannelSelector, |
| 310 | yChannelSelector, |
| 311 | scale, |
| 312 | displacement, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 313 | offsetMatrix, |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 314 | color))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 315 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 316 | } |
| 317 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 318 | virtual ~GrDisplacementMapEffect(); |
| 319 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 320 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 321 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector() const |
| 322 | { return fXChannelSelector; } |
| 323 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector() const |
| 324 | { return fYChannelSelector; } |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 325 | const SkVector& scale() const { return fScale; } |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 326 | |
| 327 | typedef GrGLDisplacementMapEffect GLEffect; |
| 328 | static const char* Name() { return "DisplacementMap"; } |
| 329 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 330 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 331 | |
| 332 | private: |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 333 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 334 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 335 | GrDisplacementMapEffect(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, |
| 336 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 337 | const SkVector& scale, |
| 338 | GrTexture* displacement, const SkMatrix& offsetMatrix, |
| 339 | GrTexture* color); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 340 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 341 | GR_DECLARE_EFFECT_TEST; |
| 342 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 343 | GrCoordTransform fDisplacementTransform; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 344 | GrTextureAccess fDisplacementAccess; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 345 | GrCoordTransform fColorTransform; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 346 | GrTextureAccess fColorAccess; |
| 347 | SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; |
| 348 | SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 349 | SkVector fScale; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 350 | |
| 351 | typedef GrEffect INHERITED; |
| 352 | }; |
| 353 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 354 | 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] | 355 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 356 | SkBitmap colorBM = src; |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 357 | SkIPoint colorOffset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 358 | if (getColorInput() && !getColorInput()->getInputResultGPU(proxy, src, ctx, &colorBM, |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 359 | &colorOffset)) { |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 360 | return false; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 361 | } |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 362 | SkBitmap displacementBM = src; |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 363 | SkIPoint displacementOffset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 364 | if (getDisplacementInput() && |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 365 | !getDisplacementInput()->getInputResultGPU(proxy, src, ctx, &displacementBM, |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 366 | &displacementOffset)) { |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 367 | return false; |
| 368 | } |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 369 | SkIRect bounds; |
| 370 | // Since GrDisplacementMapEffect does bounds checking on color pixel access, we don't need to |
| 371 | // pad the color bitmap to bounds here. |
| 372 | if (!this->applyCropRect(ctx, colorBM, colorOffset, &bounds)) { |
| 373 | return false; |
| 374 | } |
| 375 | SkIRect displBounds; |
| 376 | if (!this->applyCropRect(ctx, proxy, displacementBM, |
| 377 | &displacementOffset, &displBounds, &displacementBM)) { |
| 378 | return false; |
| 379 | } |
| 380 | if (!bounds.intersect(displBounds)) { |
| 381 | return false; |
| 382 | } |
| 383 | GrTexture* color = colorBM.getTexture(); |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 384 | GrTexture* displacement = displacementBM.getTexture(); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 385 | GrContext* context = color->getContext(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 386 | |
| 387 | GrTextureDesc desc; |
| 388 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
senorblanco@chromium.org | d9cdff6 | 2014-03-17 23:41:40 +0000 | [diff] [blame] | 389 | desc.fWidth = bounds.width(); |
| 390 | desc.fHeight = bounds.height(); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 391 | desc.fConfig = kSkia8888_GrPixelConfig; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 392 | |
| 393 | GrAutoScratchTexture ast(context, desc); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 394 | SkAutoTUnref<GrTexture> dst(ast.detach()); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 395 | |
| 396 | GrContext::AutoRenderTarget art(context, dst->asRenderTarget()); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 397 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 398 | SkVector scale = SkVector::Make(fScale, fScale); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 399 | ctx.ctm().mapVectors(&scale, 1); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 400 | |
| 401 | GrPaint paint; |
| 402 | SkMatrix offsetMatrix = GrEffect::MakeDivByTextureWHMatrix(displacement); |
| 403 | offsetMatrix.preTranslate(SkIntToScalar(colorOffset.fX - displacementOffset.fX), |
| 404 | SkIntToScalar(colorOffset.fY - displacementOffset.fY)); |
| 405 | |
| 406 | paint.addColorEffect( |
| 407 | GrDisplacementMapEffect::Create(fXChannelSelector, |
| 408 | fYChannelSelector, |
| 409 | scale, |
| 410 | displacement, |
| 411 | offsetMatrix, |
| 412 | color))->unref(); |
| 413 | SkIRect colorBounds = bounds; |
| 414 | colorBounds.offset(-colorOffset); |
senorblanco@chromium.org | d9cdff6 | 2014-03-17 23:41:40 +0000 | [diff] [blame] | 415 | GrContext::AutoMatrix am; |
| 416 | am.setIdentity(context); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 417 | SkMatrix matrix; |
| 418 | matrix.setTranslate(-SkIntToScalar(colorBounds.x()), |
| 419 | -SkIntToScalar(colorBounds.y())); |
| 420 | context->concatMatrix(matrix); |
| 421 | context->drawRect(paint, SkRect::Make(colorBounds)); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 422 | offset->fX = bounds.left(); |
| 423 | offset->fY = bounds.top(); |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 424 | WrapTexture(dst, bounds.width(), bounds.height(), result); |
| 425 | return true; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /////////////////////////////////////////////////////////////////////////////// |
| 429 | |
| 430 | GrDisplacementMapEffect::GrDisplacementMapEffect( |
| 431 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, |
| 432 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 433 | const SkVector& scale, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 434 | GrTexture* displacement, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 435 | const SkMatrix& offsetMatrix, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 436 | GrTexture* color) |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 437 | : fDisplacementTransform(kLocal_GrCoordSet, offsetMatrix, displacement) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 438 | , fDisplacementAccess(displacement) |
| 439 | , fColorTransform(kLocal_GrCoordSet, color) |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 440 | , fColorAccess(color) |
| 441 | , fXChannelSelector(xChannelSelector) |
| 442 | , fYChannelSelector(yChannelSelector) |
| 443 | , fScale(scale) { |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 444 | this->addCoordTransform(&fDisplacementTransform); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 445 | this->addTextureAccess(&fDisplacementAccess); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 446 | this->addCoordTransform(&fColorTransform); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 447 | this->addTextureAccess(&fColorAccess); |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 448 | this->setWillNotUseInputColor(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | GrDisplacementMapEffect::~GrDisplacementMapEffect() { |
| 452 | } |
| 453 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 454 | bool GrDisplacementMapEffect::onIsEqual(const GrEffect& sBase) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 455 | const GrDisplacementMapEffect& s = CastEffect<GrDisplacementMapEffect>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 456 | return fDisplacementAccess.getTexture() == s.fDisplacementAccess.getTexture() && |
| 457 | fColorAccess.getTexture() == s.fColorAccess.getTexture() && |
| 458 | fXChannelSelector == s.fXChannelSelector && |
| 459 | fYChannelSelector == s.fYChannelSelector && |
| 460 | fScale == s.fScale; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | const GrBackendEffectFactory& GrDisplacementMapEffect::getFactory() const { |
| 464 | return GrTBackendEffectFactory<GrDisplacementMapEffect>::getInstance(); |
| 465 | } |
| 466 | |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 467 | void GrDisplacementMapEffect::getConstantColorComponents(GrColor*, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 468 | uint32_t* validFlags) const { |
| 469 | // Any displacement offset bringing a pixel out of bounds will output a color of (0,0,0,0), |
| 470 | // 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] | 471 | // 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] | 472 | // alpha is 0. Since this isn't trivial to compute at this point, let's assume the output is |
| 473 | // not of constant color when a displacement effect is applied. |
| 474 | *validFlags = 0; |
| 475 | } |
| 476 | |
| 477 | /////////////////////////////////////////////////////////////////////////////// |
| 478 | |
| 479 | GR_DEFINE_EFFECT_TEST(GrDisplacementMapEffect); |
| 480 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 481 | GrEffectRef* GrDisplacementMapEffect::TestCreate(SkRandom* random, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 482 | GrContext*, |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 483 | const GrDrawTargetCaps&, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 484 | GrTexture* textures[]) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 485 | int texIdxDispl = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 486 | GrEffectUnitTest::kAlphaTextureIdx; |
| 487 | int texIdxColor = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 488 | GrEffectUnitTest::kAlphaTextureIdx; |
| 489 | static const int kMaxComponent = 4; |
| 490 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector = |
| 491 | static_cast<SkDisplacementMapEffect::ChannelSelectorType>( |
| 492 | random->nextRangeU(1, kMaxComponent)); |
| 493 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector = |
| 494 | static_cast<SkDisplacementMapEffect::ChannelSelectorType>( |
| 495 | random->nextRangeU(1, kMaxComponent)); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 496 | SkVector scale = SkVector::Make(random->nextRangeScalar(0, 100.0f), |
| 497 | random->nextRangeScalar(0, 100.0f)); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 498 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 499 | return GrDisplacementMapEffect::Create(xChannelSelector, yChannelSelector, scale, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 500 | textures[texIdxDispl], SkMatrix::I(), |
| 501 | textures[texIdxColor]); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /////////////////////////////////////////////////////////////////////////////// |
| 505 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 506 | GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 507 | const GrDrawEffect& drawEffect) |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 508 | : INHERITED(factory) |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 509 | , fXChannelSelector(drawEffect.castEffect<GrDisplacementMapEffect>().xChannelSelector()) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 510 | , fYChannelSelector(drawEffect.castEffect<GrDisplacementMapEffect>().yChannelSelector()) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | GrGLDisplacementMapEffect::~GrGLDisplacementMapEffect() { |
| 514 | } |
| 515 | |
| 516 | void GrGLDisplacementMapEffect::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 517 | const GrDrawEffect&, |
| 518 | EffectKey key, |
| 519 | const char* outputColor, |
| 520 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 521 | const TransformedCoordsArray& coords, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 522 | const TextureSamplerArray& samplers) { |
sugoi@google.com | 4d94e1b | 2013-03-21 18:38:03 +0000 | [diff] [blame] | 523 | sk_ignore_unused_variable(inputColor); |
| 524 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 525 | fScaleUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 526 | kVec2f_GrSLType, "Scale"); |
| 527 | const char* scaleUni = builder->getUniformCStr(fScaleUni); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 528 | const char* dColor = "dColor"; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 529 | const char* cCoords = "cCoords"; |
sugoi@google.com | 4d94e1b | 2013-03-21 18:38:03 +0000 | [diff] [blame] | 530 | const char* outOfBounds = "outOfBounds"; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 531 | const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half float, use |
| 532 | // a number smaller than that to approximate 0, but |
| 533 | // leave room for 32-bit float GPU rounding errors. |
| 534 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 535 | builder->fsCodeAppendf("\t\tvec4 %s = ", dColor); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 536 | builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type()); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 537 | builder->fsCodeAppend(";\n"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 538 | |
| 539 | // Unpremultiply the displacement |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 540 | builder->fsCodeAppendf("\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb / %s.a, 0.0, 1.0);", |
| 541 | dColor, dColor, nearZero, dColor, dColor); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 542 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 543 | builder->fsCodeAppendf("\t\tvec2 %s = %s + %s*(%s.", |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 544 | cCoords, coords[1].c_str(), scaleUni, dColor); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 545 | |
| 546 | switch (fXChannelSelector) { |
| 547 | case SkDisplacementMapEffect::kR_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 548 | builder->fsCodeAppend("r"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 549 | break; |
| 550 | case SkDisplacementMapEffect::kG_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 551 | builder->fsCodeAppend("g"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 552 | break; |
| 553 | case SkDisplacementMapEffect::kB_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 554 | builder->fsCodeAppend("b"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 555 | break; |
| 556 | case SkDisplacementMapEffect::kA_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 557 | builder->fsCodeAppend("a"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 558 | break; |
| 559 | case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
| 560 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 561 | SkDEBUGFAIL("Unknown X channel selector"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | switch (fYChannelSelector) { |
| 565 | case SkDisplacementMapEffect::kR_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 566 | builder->fsCodeAppend("r"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 567 | break; |
| 568 | case SkDisplacementMapEffect::kG_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 569 | builder->fsCodeAppend("g"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 570 | break; |
| 571 | case SkDisplacementMapEffect::kB_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 572 | builder->fsCodeAppend("b"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 573 | break; |
| 574 | case SkDisplacementMapEffect::kA_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 575 | builder->fsCodeAppend("a"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 576 | break; |
| 577 | case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
| 578 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 579 | SkDEBUGFAIL("Unknown Y channel selector"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 580 | } |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 581 | builder->fsCodeAppend("-vec2(0.5));\t\t"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 582 | |
| 583 | // FIXME : This can be achieved with a "clamp to border" texture repeat mode and |
| 584 | // a 0 border color instead of computing if cCoords is out of bounds here. |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 585 | builder->fsCodeAppendf( |
sugoi@google.com | 4d94e1b | 2013-03-21 18:38:03 +0000 | [diff] [blame] | 586 | "bool %s = (%s.x < 0.0) || (%s.y < 0.0) || (%s.x > 1.0) || (%s.y > 1.0);\t\t", |
| 587 | outOfBounds, cCoords, cCoords, cCoords, cCoords); |
| 588 | builder->fsCodeAppendf("%s = %s ? vec4(0.0) : ", outputColor, outOfBounds); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 589 | builder->fsAppendTextureLookup(samplers[1], cCoords, coords[1].type()); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 590 | builder->fsCodeAppend(";\n"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 591 | } |
| 592 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 593 | void GrGLDisplacementMapEffect::setData(const GrGLUniformManager& uman, |
| 594 | const GrDrawEffect& drawEffect) { |
| 595 | const GrDisplacementMapEffect& displacementMap = |
| 596 | drawEffect.castEffect<GrDisplacementMapEffect>(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 597 | GrTexture* colorTex = displacementMap.texture(1); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 598 | SkScalar scaleX = SkScalarDiv(displacementMap.scale().fX, SkIntToScalar(colorTex->width())); |
| 599 | SkScalar scaleY = SkScalarDiv(displacementMap.scale().fY, SkIntToScalar(colorTex->height())); |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 600 | uman.set2f(fScaleUni, SkScalarToFloat(scaleX), |
| 601 | colorTex->origin() == kTopLeft_GrSurfaceOrigin ? |
| 602 | SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY)); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 603 | } |
| 604 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 605 | GrGLEffect::EffectKey GrGLDisplacementMapEffect::GenKey(const GrDrawEffect& drawEffect, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 606 | const GrGLCaps&) { |
| 607 | const GrDisplacementMapEffect& displacementMap = |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 608 | drawEffect.castEffect<GrDisplacementMapEffect>(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 609 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 610 | EffectKey xKey = displacementMap.xChannelSelector(); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 611 | EffectKey yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBits; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 612 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 613 | return xKey | yKey; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 614 | } |
| 615 | #endif |