bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 The Android Open Source Project |
| 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 "SkBitmap.h" |
| 9 | #include "SkMagnifierImageFilter.h" |
| 10 | #include "SkColorPriv.h" |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 11 | #include "SkDevice.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
| 13 | #include "SkWriteBuffer.h" |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 14 | #include "SkValidationUtils.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 15 | |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 16 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 0324570 | 2012-08-13 14:31:30 +0000 | [diff] [blame] | 17 | #if SK_SUPPORT_GPU |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 18 | #include "GrInvariantOutput.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 19 | #include "effects/GrSingleTextureEffect.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 20 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 21 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 22 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 23 | #include "glsl/GrGLSLUniformHandler.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 24 | |
| 25 | class GrMagnifierEffect : public GrSingleTextureEffect { |
| 26 | |
| 27 | public: |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 28 | static GrFragmentProcessor* Create(GrTexture* texture, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 29 | const SkRect& bounds, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 30 | float xOffset, |
| 31 | float yOffset, |
| 32 | float xInvZoom, |
| 33 | float yInvZoom, |
| 34 | float xInvInset, |
| 35 | float yInvInset) { |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 36 | return new GrMagnifierEffect(texture, bounds, xOffset, yOffset, xInvZoom, yInvZoom, xInvInset, |
| 37 | yInvInset); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 38 | } |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 39 | |
| 40 | virtual ~GrMagnifierEffect() {}; |
| 41 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 42 | const char* name() const override { return "Magnifier"; } |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 43 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 44 | const SkRect& bounds() const { return fBounds; } // Bounds of source image. |
| 45 | // Offset to apply to zoomed pixels, (srcRect position / texture size). |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 46 | float x_offset() const { return fXOffset; } |
| 47 | float y_offset() const { return fYOffset; } |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 48 | |
| 49 | // Scale to apply to zoomed pixels (srcRect size / bounds size). |
commit-bot@chromium.org | f642f8c | 2013-10-21 15:59:26 +0000 | [diff] [blame] | 50 | float x_inv_zoom() const { return fXInvZoom; } |
| 51 | float y_inv_zoom() const { return fYInvZoom; } |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 52 | |
| 53 | // 1/radius over which to transition from unzoomed to zoomed pixels (bounds size / inset). |
commit-bot@chromium.org | f642f8c | 2013-10-21 15:59:26 +0000 | [diff] [blame] | 54 | float x_inv_inset() const { return fXInvInset; } |
| 55 | float y_inv_inset() const { return fYInvInset; } |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 56 | |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 57 | private: |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 58 | GrMagnifierEffect(GrTexture* texture, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 59 | const SkRect& bounds, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 60 | float xOffset, |
| 61 | float yOffset, |
commit-bot@chromium.org | f642f8c | 2013-10-21 15:59:26 +0000 | [diff] [blame] | 62 | float xInvZoom, |
| 63 | float yInvZoom, |
| 64 | float xInvInset, |
| 65 | float yInvInset) |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 66 | : INHERITED(texture, GrCoordTransform::MakeDivByTextureWHMatrix(texture)) |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 67 | , fBounds(bounds) |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 68 | , fXOffset(xOffset) |
| 69 | , fYOffset(yOffset) |
commit-bot@chromium.org | f642f8c | 2013-10-21 15:59:26 +0000 | [diff] [blame] | 70 | , fXInvZoom(xInvZoom) |
| 71 | , fYInvZoom(yInvZoom) |
| 72 | , fXInvInset(xInvInset) |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 73 | , fYInvInset(yInvInset) { |
| 74 | this->initClassID<GrMagnifierEffect>(); |
| 75 | } |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 76 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 77 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 78 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 79 | void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 80 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 81 | bool onIsEqual(const GrFragmentProcessor&) const override; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 82 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 83 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 84 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 85 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 86 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 87 | SkRect fBounds; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 88 | float fXOffset; |
| 89 | float fYOffset; |
commit-bot@chromium.org | f642f8c | 2013-10-21 15:59:26 +0000 | [diff] [blame] | 90 | float fXInvZoom; |
| 91 | float fYInvZoom; |
| 92 | float fXInvInset; |
| 93 | float fYInvInset; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 94 | |
| 95 | typedef GrSingleTextureEffect INHERITED; |
| 96 | }; |
| 97 | |
| 98 | // For brevity |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 99 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 100 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 101 | class GrGLMagnifierEffect : public GrGLSLFragmentProcessor { |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 102 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 103 | void emitCode(EmitArgs&) override; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 104 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 105 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 106 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 107 | |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 108 | private: |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 109 | UniformHandle fOffsetVar; |
commit-bot@chromium.org | f642f8c | 2013-10-21 15:59:26 +0000 | [diff] [blame] | 110 | UniformHandle fInvZoomVar; |
| 111 | UniformHandle fInvInsetVar; |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 112 | UniformHandle fBoundsVar; |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 113 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 114 | typedef GrGLSLFragmentProcessor INHERITED; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 117 | void GrGLMagnifierEffect::emitCode(EmitArgs& args) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 118 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 119 | fOffsetVar = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 120 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 121 | "Offset"); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 122 | fInvZoomVar = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 123 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 124 | "InvZoom"); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 125 | fInvInsetVar = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 126 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 127 | "InvInset"); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 128 | fBoundsVar = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 129 | kVec4f_GrSLType, kDefault_GrSLPrecision, |
| 130 | "Bounds"); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 131 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 132 | GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 133 | SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); |
| 134 | fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
| 135 | fragBuilder->codeAppendf("\t\tvec2 zoom_coord = %s + %s * %s;\n", |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 136 | uniformHandler->getUniformCStr(fOffsetVar), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 137 | coords2D.c_str(), |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 138 | uniformHandler->getUniformCStr(fInvZoomVar)); |
| 139 | const char* bounds = uniformHandler->getUniformCStr(fBoundsVar); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 140 | fragBuilder->codeAppendf("\t\tvec2 delta = (coord - %s.xy) * %s.zw;\n", bounds, bounds); |
| 141 | fragBuilder->codeAppendf("\t\tdelta = min(delta, vec2(1.0, 1.0) - delta);\n"); |
| 142 | fragBuilder->codeAppendf("\t\tdelta = delta * %s;\n", |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 143 | uniformHandler->getUniformCStr(fInvInsetVar)); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 144 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 145 | fragBuilder->codeAppend("\t\tfloat weight = 0.0;\n"); |
| 146 | fragBuilder->codeAppend("\t\tif (delta.s < 2.0 && delta.t < 2.0) {\n"); |
| 147 | fragBuilder->codeAppend("\t\t\tdelta = vec2(2.0, 2.0) - delta;\n"); |
| 148 | fragBuilder->codeAppend("\t\t\tfloat dist = length(delta);\n"); |
| 149 | fragBuilder->codeAppend("\t\t\tdist = max(2.0 - dist, 0.0);\n"); |
| 150 | fragBuilder->codeAppend("\t\t\tweight = min(dist * dist, 1.0);\n"); |
| 151 | fragBuilder->codeAppend("\t\t} else {\n"); |
| 152 | fragBuilder->codeAppend("\t\t\tvec2 delta_squared = delta * delta;\n"); |
| 153 | fragBuilder->codeAppend("\t\t\tweight = min(min(delta_squared.x, delta_squared.y), 1.0);\n"); |
| 154 | fragBuilder->codeAppend("\t\t}\n"); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 155 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 156 | fragBuilder->codeAppend("\t\tvec2 mix_coord = mix(coord, zoom_coord, weight);\n"); |
| 157 | fragBuilder->codeAppend("\t\tvec4 output_color = "); |
| 158 | fragBuilder->appendTextureLookup(args.fSamplers[0], "mix_coord"); |
| 159 | fragBuilder->codeAppend(";\n"); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 160 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 161 | fragBuilder->codeAppendf("\t\t%s = output_color;", args.fOutputColor); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 162 | SkString modulate; |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 163 | GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 164 | fragBuilder->codeAppend(modulate.c_str()); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 165 | } |
| 166 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 167 | void GrGLMagnifierEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 168 | const GrProcessor& effect) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 169 | const GrMagnifierEffect& zoom = effect.cast<GrMagnifierEffect>(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 170 | pdman.set2f(fOffsetVar, zoom.x_offset(), zoom.y_offset()); |
| 171 | pdman.set2f(fInvZoomVar, zoom.x_inv_zoom(), zoom.y_inv_zoom()); |
| 172 | pdman.set2f(fInvInsetVar, zoom.x_inv_inset(), zoom.y_inv_inset()); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 173 | pdman.set4f(fBoundsVar, zoom.bounds().x(), zoom.bounds().y(), |
| 174 | zoom.bounds().width(), zoom.bounds().height()); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | ///////////////////////////////////////////////////////////////////// |
| 178 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 179 | void GrMagnifierEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 180 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 181 | GrGLMagnifierEffect::GenKey(*this, caps, b); |
| 182 | } |
| 183 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 184 | GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 185 | return new GrGLMagnifierEffect; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 186 | } |
| 187 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 188 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 189 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 190 | const GrFragmentProcessor* GrMagnifierEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 191 | GrTexture* texture = d->fTextures[0]; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 192 | const int kMaxWidth = 200; |
| 193 | const int kMaxHeight = 200; |
| 194 | const int kMaxInset = 20; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 195 | uint32_t width = d->fRandom->nextULessThan(kMaxWidth); |
| 196 | uint32_t height = d->fRandom->nextULessThan(kMaxHeight); |
| 197 | uint32_t x = d->fRandom->nextULessThan(kMaxWidth - width); |
| 198 | uint32_t y = d->fRandom->nextULessThan(kMaxHeight - height); |
| 199 | uint32_t inset = d->fRandom->nextULessThan(kMaxInset); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 200 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 201 | GrFragmentProcessor* effect = GrMagnifierEffect::Create( |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 202 | texture, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 203 | SkRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight)), |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 204 | (float) width / texture->width(), |
| 205 | (float) height / texture->height(), |
| 206 | texture->width() / (float) x, |
| 207 | texture->height() / (float) y, |
| 208 | (float) inset / texture->width(), |
| 209 | (float) inset / texture->height()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 210 | SkASSERT(effect); |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 211 | return effect; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /////////////////////////////////////////////////////////////////////////////// |
| 215 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 216 | bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 217 | const GrMagnifierEffect& s = sBase.cast<GrMagnifierEffect>(); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 218 | return (this->fBounds == s.fBounds && |
| 219 | this->fXOffset == s.fXOffset && |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 220 | this->fYOffset == s.fYOffset && |
commit-bot@chromium.org | f642f8c | 2013-10-21 15:59:26 +0000 | [diff] [blame] | 221 | this->fXInvZoom == s.fXInvZoom && |
| 222 | this->fYInvZoom == s.fYInvZoom && |
| 223 | this->fXInvInset == s.fXInvInset && |
| 224 | this->fYInvInset == s.fYInvInset); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 225 | } |
| 226 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 227 | void GrMagnifierEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 228 | this->updateInvariantOutputForModulation(inout); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 229 | } |
| 230 | |
bsalomon@google.com | 0324570 | 2012-08-13 14:31:30 +0000 | [diff] [blame] | 231 | #endif |
| 232 | |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 233 | //////////////////////////////////////////////////////////////////////////////// |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 234 | |
| 235 | SkImageFilter* SkMagnifierImageFilter::Create(const SkRect& srcRect, SkScalar inset, |
| 236 | SkImageFilter* input) { |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 237 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 238 | if (!SkScalarIsFinite(inset) || !SkIsValidRect(srcRect)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 239 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 240 | } |
| 241 | // Negative numbers in src rect are not supported |
| 242 | if (srcRect.fLeft < 0 || srcRect.fTop < 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 243 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 244 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 245 | return new SkMagnifierImageFilter(srcRect, inset, input); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 249 | SkMagnifierImageFilter::SkMagnifierImageFilter(const SkRect& srcRect, SkScalar inset, |
| 250 | SkImageFilter* input) |
| 251 | : INHERITED(1, &input), fSrcRect(srcRect), fInset(inset) { |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 252 | SkASSERT(srcRect.x() >= 0 && srcRect.y() >= 0 && inset >= 0); |
| 253 | } |
| 254 | |
bsalomon@google.com | 0324570 | 2012-08-13 14:31:30 +0000 | [diff] [blame] | 255 | #if SK_SUPPORT_GPU |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 256 | bool SkMagnifierImageFilter::asFragmentProcessor(GrFragmentProcessor** fp, |
joshualitt | 85ff25e | 2015-07-08 09:10:03 -0700 | [diff] [blame] | 257 | GrTexture* texture, const SkMatrix&, |
| 258 | const SkIRect&bounds) const { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 259 | if (fp) { |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 260 | SkScalar yOffset = texture->origin() == kTopLeft_GrSurfaceOrigin ? fSrcRect.y() : |
| 261 | texture->height() - fSrcRect.height() * texture->height() / bounds.height() |
| 262 | - fSrcRect.y(); |
| 263 | int boundsY = (texture->origin() == kTopLeft_GrSurfaceOrigin) ? bounds.y() : |
| 264 | (texture->height() - bounds.height()); |
| 265 | SkRect effectBounds = SkRect::MakeXYWH( |
| 266 | SkIntToScalar(bounds.x()) / texture->width(), |
| 267 | SkIntToScalar(boundsY) / texture->height(), |
| 268 | SkIntToScalar(texture->width()) / bounds.width(), |
| 269 | SkIntToScalar(texture->height()) / bounds.height()); |
commit-bot@chromium.org | f642f8c | 2013-10-21 15:59:26 +0000 | [diff] [blame] | 270 | SkScalar invInset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1; |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 271 | *fp = GrMagnifierEffect::Create(texture, |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 272 | effectBounds, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 273 | fSrcRect.x() / texture->width(), |
| 274 | yOffset / texture->height(), |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 275 | fSrcRect.width() / bounds.width(), |
| 276 | fSrcRect.height() / bounds.height(), |
| 277 | bounds.width() * invInset, |
| 278 | bounds.height() * invInset); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 279 | } |
| 280 | return true; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 281 | } |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 282 | #endif |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 283 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 284 | SkFlattenable* SkMagnifierImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 285 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 286 | SkRect src; |
| 287 | buffer.readRect(&src); |
| 288 | return Create(src, buffer.readScalar(), common.getInput(0)); |
| 289 | } |
| 290 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 291 | void SkMagnifierImageFilter::flatten(SkWriteBuffer& buffer) const { |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 292 | this->INHERITED::flatten(buffer); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 293 | buffer.writeRect(fSrcRect); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 294 | buffer.writeScalar(fInset); |
| 295 | } |
| 296 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 297 | bool SkMagnifierImageFilter::onFilterImageDeprecated(Proxy* proxy, const SkBitmap& src, |
| 298 | const Context&, SkBitmap* dst, |
| 299 | SkIPoint* offset) const { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 300 | if ((src.colorType() != kN32_SkColorType) || |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 301 | (fSrcRect.width() >= src.width()) || |
| 302 | (fSrcRect.height() >= src.height())) { |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 303 | return false; |
| 304 | } |
| 305 | |
| 306 | SkAutoLockPixels alp(src); |
| 307 | SkASSERT(src.getPixels()); |
| 308 | if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) { |
| 309 | return false; |
| 310 | } |
| 311 | |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 312 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(src.width(), src.height())); |
| 313 | if (!device) { |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 314 | return false; |
| 315 | } |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 316 | *dst = device->accessBitmap(false); |
| 317 | SkAutoLockPixels alp_dst(*dst); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 318 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 319 | SkScalar inv_inset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 320 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 321 | SkScalar inv_x_zoom = fSrcRect.width() / src.width(); |
| 322 | SkScalar inv_y_zoom = fSrcRect.height() / src.height(); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 323 | |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 324 | SkColor* sptr = src.getAddr32(0, 0); |
| 325 | SkColor* dptr = dst->getAddr32(0, 0); |
| 326 | int width = src.width(), height = src.height(); |
| 327 | for (int y = 0; y < height; ++y) { |
| 328 | for (int x = 0; x < width; ++x) { |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 329 | SkScalar x_dist = SkMin32(x, width - x - 1) * inv_inset; |
| 330 | SkScalar y_dist = SkMin32(y, height - y - 1) * inv_inset; |
| 331 | SkScalar weight = 0; |
| 332 | |
| 333 | static const SkScalar kScalar2 = SkScalar(2); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 334 | |
| 335 | // To create a smooth curve at the corners, we need to work on |
| 336 | // a square twice the size of the inset. |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 337 | if (x_dist < kScalar2 && y_dist < kScalar2) { |
| 338 | x_dist = kScalar2 - x_dist; |
| 339 | y_dist = kScalar2 - y_dist; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 340 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 341 | SkScalar dist = SkScalarSqrt(SkScalarSquare(x_dist) + |
| 342 | SkScalarSquare(y_dist)); |
| 343 | dist = SkMaxScalar(kScalar2 - dist, 0); |
| 344 | weight = SkMinScalar(SkScalarSquare(dist), SK_Scalar1); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 345 | } else { |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 346 | SkScalar sqDist = SkMinScalar(SkScalarSquare(x_dist), |
| 347 | SkScalarSquare(y_dist)); |
| 348 | weight = SkMinScalar(sqDist, SK_Scalar1); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 349 | } |
| 350 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 351 | SkScalar x_interp = SkScalarMul(weight, (fSrcRect.x() + x * inv_x_zoom)) + |
| 352 | (SK_Scalar1 - weight) * x; |
| 353 | SkScalar y_interp = SkScalarMul(weight, (fSrcRect.y() + y * inv_y_zoom)) + |
| 354 | (SK_Scalar1 - weight) * y; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 355 | |
bungeman | 62ce030 | 2015-08-28 09:09:32 -0700 | [diff] [blame] | 356 | int x_val = SkTPin(SkScalarFloorToInt(x_interp), 0, width - 1); |
| 357 | int y_val = SkTPin(SkScalarFloorToInt(y_interp), 0, height - 1); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 358 | |
| 359 | *dptr = sptr[y_val * width + x_val]; |
| 360 | dptr++; |
| 361 | } |
| 362 | } |
| 363 | return true; |
| 364 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 365 | |
| 366 | #ifndef SK_IGNORE_TO_STRING |
| 367 | void SkMagnifierImageFilter::toString(SkString* str) const { |
| 368 | str->appendf("SkMagnifierImageFilter: ("); |
| 369 | str->appendf("src: (%f,%f,%f,%f) ", |
| 370 | fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBottom); |
| 371 | str->appendf("inset: %f", fInset); |
| 372 | str->append(")"); |
| 373 | } |
| 374 | #endif |