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 | e24ad23 | 2014-02-16 22:03:38 +0000 | [diff] [blame] | 208 | if ((displ.colorType() != kPMColor_SkColorType) || |
| 209 | (color.colorType() != kPMColor_SkColorType)) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 210 | return false; |
| 211 | } |
| 212 | |
| 213 | SkAutoLockPixels alp_displacement(displ), alp_color(color); |
| 214 | if (!displ.getPixels() || !color.getPixels()) { |
| 215 | return false; |
| 216 | } |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 217 | SkIRect bounds; |
| 218 | color.getBounds(&bounds); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 219 | bounds.offset(colorOffset); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame^] | 220 | if (!this->applyCropRect(&bounds, ctx.ctm())) { |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 221 | return false; |
| 222 | } |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 223 | SkIRect displBounds; |
| 224 | displ.getBounds(&displBounds); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 225 | displBounds.offset(displOffset); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame^] | 226 | if (!this->applyCropRect(&displBounds, ctx.ctm())) { |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 227 | return false; |
| 228 | } |
| 229 | if (!bounds.intersect(displBounds)) { |
| 230 | return false; |
| 231 | } |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 232 | |
| 233 | dst->setConfig(color.config(), bounds.width(), bounds.height()); |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 234 | if (!dst->allocPixels()) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 238 | SkVector scale = SkVector::Make(fScale, fScale); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame^] | 239 | ctx.ctm().mapVectors(&scale, 1); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 240 | SkIRect colorBounds = bounds; |
| 241 | colorBounds.offset(-colorOffset); |
| 242 | |
| 243 | computeDisplacement(fXChannelSelector, fYChannelSelector, scale, dst, |
| 244 | &displ, colorOffset - displOffset, &color, colorBounds); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 245 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 246 | offset->fX = bounds.left(); |
| 247 | offset->fY = bounds.top(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 248 | return true; |
| 249 | } |
| 250 | |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 251 | void SkDisplacementMapEffect::computeFastBounds(const SkRect& src, SkRect* dst) const { |
| 252 | if (getColorInput()) { |
| 253 | getColorInput()->computeFastBounds(src, dst); |
| 254 | } else { |
| 255 | *dst = src; |
| 256 | } |
| 257 | } |
| 258 | |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 259 | bool SkDisplacementMapEffect::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, |
| 260 | SkIRect* dst) const { |
| 261 | if (getColorInput()) { |
| 262 | return getColorInput()->filterBounds(src, ctm, dst); |
| 263 | } |
| 264 | *dst = src; |
| 265 | return true; |
| 266 | } |
| 267 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 268 | /////////////////////////////////////////////////////////////////////////////// |
| 269 | |
| 270 | #if SK_SUPPORT_GPU |
| 271 | class GrGLDisplacementMapEffect : public GrGLEffect { |
| 272 | public: |
| 273 | GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 274 | const GrDrawEffect& drawEffect); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 275 | virtual ~GrGLDisplacementMapEffect(); |
| 276 | |
| 277 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 278 | const GrDrawEffect&, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 279 | EffectKey, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 280 | const char* outputColor, |
| 281 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 282 | const TransformedCoordsArray&, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 283 | const TextureSamplerArray&) SK_OVERRIDE; |
| 284 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 285 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 286 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 287 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 288 | |
| 289 | private: |
| 290 | SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; |
| 291 | SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 292 | GrGLUniformManager::UniformHandle fScaleUni; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 293 | |
| 294 | typedef GrGLEffect INHERITED; |
| 295 | }; |
| 296 | |
| 297 | /////////////////////////////////////////////////////////////////////////////// |
| 298 | |
| 299 | class GrDisplacementMapEffect : public GrEffect { |
| 300 | public: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 301 | static GrEffectRef* Create(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, |
| 302 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 303 | SkVector scale, |
| 304 | GrTexture* displacement, const SkMatrix& offsetMatrix, |
| 305 | GrTexture* color) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 306 | AutoEffectUnref effect(SkNEW_ARGS(GrDisplacementMapEffect, (xChannelSelector, |
| 307 | yChannelSelector, |
| 308 | scale, |
| 309 | displacement, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 310 | offsetMatrix, |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 311 | color))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 312 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 313 | } |
| 314 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 315 | virtual ~GrDisplacementMapEffect(); |
| 316 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 317 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 318 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector() const |
| 319 | { return fXChannelSelector; } |
| 320 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector() const |
| 321 | { return fYChannelSelector; } |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 322 | const SkVector& scale() const { return fScale; } |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 323 | |
| 324 | typedef GrGLDisplacementMapEffect GLEffect; |
| 325 | static const char* Name() { return "DisplacementMap"; } |
| 326 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 327 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 328 | |
| 329 | private: |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 330 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 331 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 332 | GrDisplacementMapEffect(SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, |
| 333 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 334 | const SkVector& scale, |
| 335 | GrTexture* displacement, const SkMatrix& offsetMatrix, |
| 336 | GrTexture* color); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 337 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 338 | GR_DECLARE_EFFECT_TEST; |
| 339 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 340 | GrCoordTransform fDisplacementTransform; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 341 | GrTextureAccess fDisplacementAccess; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 342 | GrCoordTransform fColorTransform; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 343 | GrTextureAccess fColorAccess; |
| 344 | SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; |
| 345 | SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 346 | SkVector fScale; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 347 | |
| 348 | typedef GrEffect INHERITED; |
| 349 | }; |
| 350 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame^] | 351 | 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] | 352 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 353 | SkBitmap colorBM = src; |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 354 | SkIPoint colorOffset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame^] | 355 | if (getColorInput() && !getColorInput()->getInputResultGPU(proxy, src, ctx, &colorBM, |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 356 | &colorOffset)) { |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 357 | return false; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 358 | } |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 359 | GrTexture* color = colorBM.getTexture(); |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 360 | SkBitmap displacementBM = src; |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 361 | SkIPoint displacementOffset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 362 | if (getDisplacementInput() && |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame^] | 363 | !getDisplacementInput()->getInputResultGPU(proxy, src, ctx, &displacementBM, |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 364 | &displacementOffset)) { |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 365 | return false; |
| 366 | } |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 367 | GrTexture* displacement = displacementBM.getTexture(); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 368 | GrContext* context = color->getContext(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 369 | |
| 370 | GrTextureDesc desc; |
| 371 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 372 | desc.fWidth = colorBM.width(); |
| 373 | desc.fHeight = colorBM.height(); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 374 | desc.fConfig = kSkia8888_GrPixelConfig; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 375 | |
| 376 | GrAutoScratchTexture ast(context, desc); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 377 | SkAutoTUnref<GrTexture> dst(ast.detach()); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 378 | |
| 379 | GrContext::AutoRenderTarget art(context, dst->asRenderTarget()); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 380 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 381 | SkVector scale = SkVector::Make(fScale, fScale); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame^] | 382 | ctx.ctm().mapVectors(&scale, 1); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 383 | SkIRect bounds; |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 384 | colorBM.getBounds(&bounds); |
| 385 | bounds.offset(colorOffset); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame^] | 386 | if (!this->applyCropRect(&bounds, ctx.ctm())) { |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 387 | return false; |
| 388 | } |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 389 | SkIRect displBounds; |
| 390 | displacementBM.getBounds(&displBounds); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 391 | displBounds.offset(displacementOffset); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame^] | 392 | if (!this->applyCropRect(&displBounds, ctx.ctm())) { |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 393 | return false; |
| 394 | } |
| 395 | if (!bounds.intersect(displBounds)) { |
| 396 | return false; |
| 397 | } |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 398 | |
| 399 | GrPaint paint; |
| 400 | SkMatrix offsetMatrix = GrEffect::MakeDivByTextureWHMatrix(displacement); |
| 401 | offsetMatrix.preTranslate(SkIntToScalar(colorOffset.fX - displacementOffset.fX), |
| 402 | SkIntToScalar(colorOffset.fY - displacementOffset.fY)); |
| 403 | |
| 404 | paint.addColorEffect( |
| 405 | GrDisplacementMapEffect::Create(fXChannelSelector, |
| 406 | fYChannelSelector, |
| 407 | scale, |
| 408 | displacement, |
| 409 | offsetMatrix, |
| 410 | color))->unref(); |
| 411 | SkIRect colorBounds = bounds; |
| 412 | colorBounds.offset(-colorOffset); |
| 413 | SkMatrix matrix; |
| 414 | matrix.setTranslate(-SkIntToScalar(colorBounds.x()), |
| 415 | -SkIntToScalar(colorBounds.y())); |
| 416 | context->concatMatrix(matrix); |
| 417 | context->drawRect(paint, SkRect::Make(colorBounds)); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 418 | offset->fX = bounds.left(); |
| 419 | offset->fY = bounds.top(); |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 420 | WrapTexture(dst, bounds.width(), bounds.height(), result); |
| 421 | return true; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /////////////////////////////////////////////////////////////////////////////// |
| 425 | |
| 426 | GrDisplacementMapEffect::GrDisplacementMapEffect( |
| 427 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector, |
| 428 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 429 | const SkVector& scale, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 430 | GrTexture* displacement, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 431 | const SkMatrix& offsetMatrix, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 432 | GrTexture* color) |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 433 | : fDisplacementTransform(kLocal_GrCoordSet, offsetMatrix, displacement) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 434 | , fDisplacementAccess(displacement) |
| 435 | , fColorTransform(kLocal_GrCoordSet, color) |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 436 | , fColorAccess(color) |
| 437 | , fXChannelSelector(xChannelSelector) |
| 438 | , fYChannelSelector(yChannelSelector) |
| 439 | , fScale(scale) { |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 440 | this->addCoordTransform(&fDisplacementTransform); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 441 | this->addTextureAccess(&fDisplacementAccess); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 442 | this->addCoordTransform(&fColorTransform); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 443 | this->addTextureAccess(&fColorAccess); |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 444 | this->setWillNotUseInputColor(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | GrDisplacementMapEffect::~GrDisplacementMapEffect() { |
| 448 | } |
| 449 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 450 | bool GrDisplacementMapEffect::onIsEqual(const GrEffect& sBase) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 451 | const GrDisplacementMapEffect& s = CastEffect<GrDisplacementMapEffect>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 452 | return fDisplacementAccess.getTexture() == s.fDisplacementAccess.getTexture() && |
| 453 | fColorAccess.getTexture() == s.fColorAccess.getTexture() && |
| 454 | fXChannelSelector == s.fXChannelSelector && |
| 455 | fYChannelSelector == s.fYChannelSelector && |
| 456 | fScale == s.fScale; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | const GrBackendEffectFactory& GrDisplacementMapEffect::getFactory() const { |
| 460 | return GrTBackendEffectFactory<GrDisplacementMapEffect>::getInstance(); |
| 461 | } |
| 462 | |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 463 | void GrDisplacementMapEffect::getConstantColorComponents(GrColor*, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 464 | uint32_t* validFlags) const { |
| 465 | // Any displacement offset bringing a pixel out of bounds will output a color of (0,0,0,0), |
| 466 | // 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] | 467 | // 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] | 468 | // alpha is 0. Since this isn't trivial to compute at this point, let's assume the output is |
| 469 | // not of constant color when a displacement effect is applied. |
| 470 | *validFlags = 0; |
| 471 | } |
| 472 | |
| 473 | /////////////////////////////////////////////////////////////////////////////// |
| 474 | |
| 475 | GR_DEFINE_EFFECT_TEST(GrDisplacementMapEffect); |
| 476 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 477 | GrEffectRef* GrDisplacementMapEffect::TestCreate(SkRandom* random, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 478 | GrContext*, |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 479 | const GrDrawTargetCaps&, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 480 | GrTexture* textures[]) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 481 | int texIdxDispl = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 482 | GrEffectUnitTest::kAlphaTextureIdx; |
| 483 | int texIdxColor = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 484 | GrEffectUnitTest::kAlphaTextureIdx; |
| 485 | static const int kMaxComponent = 4; |
| 486 | SkDisplacementMapEffect::ChannelSelectorType xChannelSelector = |
| 487 | static_cast<SkDisplacementMapEffect::ChannelSelectorType>( |
| 488 | random->nextRangeU(1, kMaxComponent)); |
| 489 | SkDisplacementMapEffect::ChannelSelectorType yChannelSelector = |
| 490 | static_cast<SkDisplacementMapEffect::ChannelSelectorType>( |
| 491 | random->nextRangeU(1, kMaxComponent)); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 492 | SkVector scale = SkVector::Make(random->nextRangeScalar(0, 100.0f), |
| 493 | random->nextRangeScalar(0, 100.0f)); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 494 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 495 | return GrDisplacementMapEffect::Create(xChannelSelector, yChannelSelector, scale, |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 496 | textures[texIdxDispl], SkMatrix::I(), |
| 497 | textures[texIdxColor]); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /////////////////////////////////////////////////////////////////////////////// |
| 501 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 502 | GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 503 | const GrDrawEffect& drawEffect) |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 504 | : INHERITED(factory) |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 505 | , fXChannelSelector(drawEffect.castEffect<GrDisplacementMapEffect>().xChannelSelector()) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 506 | , fYChannelSelector(drawEffect.castEffect<GrDisplacementMapEffect>().yChannelSelector()) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | GrGLDisplacementMapEffect::~GrGLDisplacementMapEffect() { |
| 510 | } |
| 511 | |
| 512 | void GrGLDisplacementMapEffect::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 513 | const GrDrawEffect&, |
| 514 | EffectKey key, |
| 515 | const char* outputColor, |
| 516 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 517 | const TransformedCoordsArray& coords, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 518 | const TextureSamplerArray& samplers) { |
sugoi@google.com | 4d94e1b | 2013-03-21 18:38:03 +0000 | [diff] [blame] | 519 | sk_ignore_unused_variable(inputColor); |
| 520 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 521 | fScaleUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 522 | kVec2f_GrSLType, "Scale"); |
| 523 | const char* scaleUni = builder->getUniformCStr(fScaleUni); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 524 | const char* dColor = "dColor"; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 525 | const char* cCoords = "cCoords"; |
sugoi@google.com | 4d94e1b | 2013-03-21 18:38:03 +0000 | [diff] [blame] | 526 | const char* outOfBounds = "outOfBounds"; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 527 | const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half float, use |
| 528 | // a number smaller than that to approximate 0, but |
| 529 | // leave room for 32-bit float GPU rounding errors. |
| 530 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 531 | builder->fsCodeAppendf("\t\tvec4 %s = ", dColor); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 532 | builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type()); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 533 | builder->fsCodeAppend(";\n"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 534 | |
| 535 | // Unpremultiply the displacement |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 536 | builder->fsCodeAppendf("\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb / %s.a, 0.0, 1.0);", |
| 537 | dColor, dColor, nearZero, dColor, dColor); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 538 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 539 | builder->fsCodeAppendf("\t\tvec2 %s = %s + %s*(%s.", |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 540 | cCoords, coords[1].c_str(), scaleUni, dColor); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 541 | |
| 542 | switch (fXChannelSelector) { |
| 543 | case SkDisplacementMapEffect::kR_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 544 | builder->fsCodeAppend("r"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 545 | break; |
| 546 | case SkDisplacementMapEffect::kG_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 547 | builder->fsCodeAppend("g"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 548 | break; |
| 549 | case SkDisplacementMapEffect::kB_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 550 | builder->fsCodeAppend("b"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 551 | break; |
| 552 | case SkDisplacementMapEffect::kA_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 553 | builder->fsCodeAppend("a"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 554 | break; |
| 555 | case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
| 556 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 557 | SkDEBUGFAIL("Unknown X channel selector"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | switch (fYChannelSelector) { |
| 561 | case SkDisplacementMapEffect::kR_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 562 | builder->fsCodeAppend("r"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 563 | break; |
| 564 | case SkDisplacementMapEffect::kG_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 565 | builder->fsCodeAppend("g"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 566 | break; |
| 567 | case SkDisplacementMapEffect::kB_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 568 | builder->fsCodeAppend("b"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 569 | break; |
| 570 | case SkDisplacementMapEffect::kA_ChannelSelectorType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 571 | builder->fsCodeAppend("a"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 572 | break; |
| 573 | case SkDisplacementMapEffect::kUnknown_ChannelSelectorType: |
| 574 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 575 | SkDEBUGFAIL("Unknown Y channel selector"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 576 | } |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 577 | builder->fsCodeAppend("-vec2(0.5));\t\t"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 578 | |
| 579 | // FIXME : This can be achieved with a "clamp to border" texture repeat mode and |
| 580 | // 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] | 581 | builder->fsCodeAppendf( |
sugoi@google.com | 4d94e1b | 2013-03-21 18:38:03 +0000 | [diff] [blame] | 582 | "bool %s = (%s.x < 0.0) || (%s.y < 0.0) || (%s.x > 1.0) || (%s.y > 1.0);\t\t", |
| 583 | outOfBounds, cCoords, cCoords, cCoords, cCoords); |
| 584 | builder->fsCodeAppendf("%s = %s ? vec4(0.0) : ", outputColor, outOfBounds); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 585 | builder->fsAppendTextureLookup(samplers[1], cCoords, coords[1].type()); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 586 | builder->fsCodeAppend(";\n"); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 587 | } |
| 588 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 589 | void GrGLDisplacementMapEffect::setData(const GrGLUniformManager& uman, |
| 590 | const GrDrawEffect& drawEffect) { |
| 591 | const GrDisplacementMapEffect& displacementMap = |
| 592 | drawEffect.castEffect<GrDisplacementMapEffect>(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 593 | GrTexture* colorTex = displacementMap.texture(1); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 594 | SkScalar scaleX = SkScalarDiv(displacementMap.scale().fX, SkIntToScalar(colorTex->width())); |
| 595 | SkScalar scaleY = SkScalarDiv(displacementMap.scale().fY, SkIntToScalar(colorTex->height())); |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 596 | uman.set2f(fScaleUni, SkScalarToFloat(scaleX), |
| 597 | colorTex->origin() == kTopLeft_GrSurfaceOrigin ? |
| 598 | SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY)); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 599 | } |
| 600 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 601 | GrGLEffect::EffectKey GrGLDisplacementMapEffect::GenKey(const GrDrawEffect& drawEffect, |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 602 | const GrGLCaps&) { |
| 603 | const GrDisplacementMapEffect& displacementMap = |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 604 | drawEffect.castEffect<GrDisplacementMapEffect>(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 605 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 606 | EffectKey xKey = displacementMap.xChannelSelector(); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 607 | EffectKey yKey = displacementMap.yChannelSelector() << kChannelSelectorKeyBits; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 608 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 609 | return xKey | yKey; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 610 | } |
| 611 | #endif |