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