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