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