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