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