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