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" |
| 11 | #include "SkFlattenableBuffers.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | |
| 15 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 0324570 | 2012-08-13 14:31:30 +0000 | [diff] [blame] | 16 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 17 | #include "effects/GrSingleTextureEffect.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 18 | #include "gl/GrGLEffect.h" |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 19 | #include "gl/GrGLEffectMatrix.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 20 | #include "gl/GrGLSL.h" |
| 21 | #include "gl/GrGLTexture.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 22 | #include "GrTBackendEffectFactory.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 23 | |
| 24 | class GrGLMagnifierEffect; |
| 25 | |
| 26 | class GrMagnifierEffect : public GrSingleTextureEffect { |
| 27 | |
| 28 | public: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 29 | static GrEffectRef* Create(GrTexture* texture, |
| 30 | float xOffset, |
| 31 | float yOffset, |
| 32 | float xZoom, |
| 33 | float yZoom, |
| 34 | float xInset, |
| 35 | float yInset) { |
| 36 | SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrMagnifierEffect, (texture, |
| 37 | xOffset, |
| 38 | yOffset, |
| 39 | xZoom, |
| 40 | yZoom, |
| 41 | xInset, |
| 42 | yInset))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame^] | 43 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 44 | } |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 45 | |
| 46 | virtual ~GrMagnifierEffect() {}; |
| 47 | |
| 48 | static const char* Name() { return "Magnifier"; } |
| 49 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 50 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 51 | virtual bool isEqual(const GrEffect&) const SK_OVERRIDE; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 52 | |
| 53 | float x_offset() const { return fXOffset; } |
| 54 | float y_offset() const { return fYOffset; } |
| 55 | float x_zoom() const { return fXZoom; } |
| 56 | float y_zoom() const { return fYZoom; } |
| 57 | float x_inset() const { return fXInset; } |
| 58 | float y_inset() const { return fYInset; } |
| 59 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 60 | typedef GrGLMagnifierEffect GLEffect; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 61 | |
| 62 | private: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 63 | GrMagnifierEffect(GrTexture* texture, |
| 64 | float xOffset, |
| 65 | float yOffset, |
| 66 | float xZoom, |
| 67 | float yZoom, |
| 68 | float xInset, |
| 69 | float yInset) |
| 70 | : GrSingleTextureEffect(texture, MakeDivByTextureWHMatrix(texture)) |
| 71 | , fXOffset(xOffset) |
| 72 | , fYOffset(yOffset) |
| 73 | , fXZoom(xZoom) |
| 74 | , fYZoom(yZoom) |
| 75 | , fXInset(xInset) |
| 76 | , fYInset(yInset) {} |
| 77 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 78 | GR_DECLARE_EFFECT_TEST; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 79 | |
| 80 | float fXOffset; |
| 81 | float fYOffset; |
| 82 | float fXZoom; |
| 83 | float fYZoom; |
| 84 | float fXInset; |
| 85 | float fYInset; |
| 86 | |
| 87 | typedef GrSingleTextureEffect INHERITED; |
| 88 | }; |
| 89 | |
| 90 | // For brevity |
| 91 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 92 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 93 | class GrGLMagnifierEffect : public GrGLEffect { |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 94 | public: |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 95 | GrGLMagnifierEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 96 | const GrEffect& effect); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 97 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 98 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 99 | const GrEffectStage&, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 100 | EffectKey, |
| 101 | const char* vertexCoords, |
| 102 | const char* outputColor, |
| 103 | const char* inputColor, |
| 104 | const TextureSamplerArray&) SK_OVERRIDE; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 105 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 106 | virtual void setData(const GrGLUniformManager& uman, const GrEffectStage& stage) SK_OVERRIDE; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 107 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 108 | static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 109 | |
| 110 | private: |
| 111 | |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 112 | UniformHandle fOffsetVar; |
| 113 | UniformHandle fZoomVar; |
| 114 | UniformHandle fInsetVar; |
| 115 | |
| 116 | GrGLEffectMatrix fEffectMatrix; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 117 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 118 | typedef GrGLEffect INHERITED; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 121 | GrGLMagnifierEffect::GrGLMagnifierEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 122 | const GrEffect& effect) |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 123 | : INHERITED(factory) |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 124 | , fOffsetVar(GrGLUniformManager::kInvalidUniformHandle) |
| 125 | , fZoomVar(GrGLUniformManager::kInvalidUniformHandle) |
| 126 | , fInsetVar(GrGLUniformManager::kInvalidUniformHandle) { |
| 127 | } |
| 128 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 129 | void GrGLMagnifierEffect::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 130 | const GrEffectStage&, |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 131 | EffectKey key, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 132 | const char* vertexCoords, |
| 133 | const char* outputColor, |
| 134 | const char* inputColor, |
| 135 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 136 | const char* coords; |
| 137 | fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords); |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 138 | fOffsetVar = builder->addUniform( |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 139 | GrGLShaderBuilder::kFragment_ShaderType | |
| 140 | GrGLShaderBuilder::kVertex_ShaderType, |
| 141 | kVec2f_GrSLType, "uOffset"); |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 142 | fZoomVar = builder->addUniform( |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 143 | GrGLShaderBuilder::kFragment_ShaderType | |
| 144 | GrGLShaderBuilder::kVertex_ShaderType, |
| 145 | kVec2f_GrSLType, "uZoom"); |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 146 | fInsetVar = builder->addUniform( |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 147 | GrGLShaderBuilder::kFragment_ShaderType | |
| 148 | GrGLShaderBuilder::kVertex_ShaderType, |
| 149 | kVec2f_GrSLType, "uInset"); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 150 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 151 | SkString* code = &builder->fFSCode; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 152 | |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 153 | code->appendf("\t\tvec2 coord = %s;\n", coords); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 154 | code->appendf("\t\tvec2 zoom_coord = %s + %s / %s;\n", |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 155 | builder->getUniformCStr(fOffsetVar), |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 156 | coords, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 157 | builder->getUniformCStr(fZoomVar)); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 158 | |
| 159 | code->appendf("\t\tvec2 delta = min(coord, vec2(1.0, 1.0) - coord);\n"); |
| 160 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 161 | code->appendf("\t\tdelta = delta / %s;\n", builder->getUniformCStr(fInsetVar)); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 162 | |
| 163 | code->appendf("\t\tfloat weight = 0.0;\n"); |
| 164 | code->appendf("\t\tif (delta.s < 2.0 && delta.t < 2.0) {\n"); |
| 165 | code->appendf("\t\t\tdelta = vec2(2.0, 2.0) - delta;\n"); |
| 166 | code->appendf("\t\t\tfloat dist = length(delta);\n"); |
| 167 | code->appendf("\t\t\tdist = max(2.0 - dist, 0.0);\n"); |
| 168 | code->appendf("\t\t\tweight = min(dist * dist, 1.0);\n"); |
| 169 | code->appendf("\t\t} else {\n"); |
| 170 | code->appendf("\t\t\tvec2 delta_squared = delta * delta;\n"); |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 171 | code->appendf("\t\t\tweight = min(min(delta_squared.s, delta_squared.y), 1.0);\n"); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 172 | code->appendf("\t\t}\n"); |
| 173 | |
| 174 | code->appendf("\t\tvec2 mix_coord = mix(coord, zoom_coord, weight);\n"); |
| 175 | code->appendf("\t\tvec4 output_color = "); |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 176 | builder->appendTextureLookup(code, samplers[0], "mix_coord"); |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 177 | code->append(";\n"); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 178 | |
| 179 | code->appendf("\t\t%s = output_color;", outputColor); |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 180 | GrGLSLMulVarBy4f(code, 2, outputColor, inputColor); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void GrGLMagnifierEffect::setData(const GrGLUniformManager& uman, |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 184 | const GrEffectStage& stage) { |
| 185 | const GrMagnifierEffect& zoom = static_cast<const GrMagnifierEffect&>(*stage.getEffect()); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 186 | |
| 187 | uman.set2f(fOffsetVar, zoom.x_offset(), zoom.y_offset()); |
| 188 | uman.set2f(fZoomVar, zoom.x_zoom(), zoom.y_zoom()); |
| 189 | uman.set2f(fInsetVar, zoom.x_inset(), zoom.y_inset()); |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 190 | fEffectMatrix.setData(uman, zoom.getMatrix(), stage.getCoordChangeMatrix(), zoom.texture(0)); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 191 | } |
| 192 | |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 193 | GrGLEffect::EffectKey GrGLMagnifierEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) { |
| 194 | const GrMagnifierEffect& zoom = static_cast<const GrMagnifierEffect&>(*stage.getEffect()); |
| 195 | return GrGLEffectMatrix::GenKey(zoom.getMatrix(), |
| 196 | stage.getCoordChangeMatrix(), |
| 197 | zoom.texture(0)); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | ///////////////////////////////////////////////////////////////////// |
| 201 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 202 | GR_DEFINE_EFFECT_TEST(GrMagnifierEffect); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 203 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 204 | GrEffectRef* GrMagnifierEffect::TestCreate(SkRandom* random, |
| 205 | GrContext* context, |
| 206 | GrTexture** textures) { |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 207 | const int kMaxWidth = 200; |
| 208 | const int kMaxHeight = 200; |
| 209 | const int kMaxInset = 20; |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 210 | uint32_t width = random->nextULessThan(kMaxWidth); |
| 211 | uint32_t height = random->nextULessThan(kMaxHeight); |
| 212 | uint32_t x = random->nextULessThan(kMaxWidth - width); |
| 213 | uint32_t y = random->nextULessThan(kMaxHeight - height); |
| 214 | SkScalar inset = SkIntToScalar(random->nextULessThan(kMaxInset)); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 215 | |
| 216 | SkAutoTUnref<SkImageFilter> filter( |
| 217 | new SkMagnifierImageFilter( |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 218 | SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 219 | SkIntToScalar(width), SkIntToScalar(height)), |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 220 | inset)); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 221 | GrEffectRef* effect; |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 222 | filter->asNewEffect(&effect, textures[0]); |
| 223 | GrAssert(NULL != effect); |
| 224 | return effect; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /////////////////////////////////////////////////////////////////////////////// |
| 228 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 229 | const GrBackendEffectFactory& GrMagnifierEffect::getFactory() const { |
| 230 | return GrTBackendEffectFactory<GrMagnifierEffect>::getInstance(); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 231 | } |
| 232 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 233 | bool GrMagnifierEffect::isEqual(const GrEffect& sBase) const { |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 234 | const GrMagnifierEffect& s = |
| 235 | static_cast<const GrMagnifierEffect&>(sBase); |
| 236 | return (this->fXOffset == s.fXOffset && |
| 237 | this->fYOffset == s.fYOffset && |
| 238 | this->fXZoom == s.fXZoom && |
| 239 | this->fYZoom == s.fYZoom && |
| 240 | this->fXInset == s.fXInset && |
| 241 | this->fYInset == s.fYInset); |
| 242 | } |
| 243 | |
bsalomon@google.com | 0324570 | 2012-08-13 14:31:30 +0000 | [diff] [blame] | 244 | #endif |
| 245 | |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 246 | //////////////////////////////////////////////////////////////////////////////// |
| 247 | SkMagnifierImageFilter::SkMagnifierImageFilter(SkFlattenableReadBuffer& buffer) |
| 248 | : INHERITED(buffer) { |
| 249 | float x = buffer.readScalar(); |
| 250 | float y = buffer.readScalar(); |
| 251 | float width = buffer.readScalar(); |
| 252 | float height = buffer.readScalar(); |
| 253 | fSrcRect = SkRect::MakeXYWH(x, y, width, height); |
| 254 | fInset = buffer.readScalar(); |
| 255 | } |
| 256 | |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 257 | // FIXME: implement single-input semantics |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 258 | SkMagnifierImageFilter::SkMagnifierImageFilter(SkRect srcRect, SkScalar inset) |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 259 | : INHERITED(0), fSrcRect(srcRect), fInset(inset) { |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 260 | SkASSERT(srcRect.x() >= 0 && srcRect.y() >= 0 && inset >= 0); |
| 261 | } |
| 262 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 263 | bool SkMagnifierImageFilter::asNewEffect(GrEffectRef** effect, GrTexture* texture) const { |
bsalomon@google.com | 0324570 | 2012-08-13 14:31:30 +0000 | [diff] [blame] | 264 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 265 | if (effect) { |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 266 | *effect = GrMagnifierEffect::Create(texture, |
| 267 | fSrcRect.x() / texture->width(), |
| 268 | fSrcRect.y() / texture->height(), |
| 269 | texture->width() / fSrcRect.width(), |
| 270 | texture->height() / fSrcRect.height(), |
| 271 | fInset / texture->width(), |
| 272 | fInset / texture->height()); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 273 | } |
| 274 | return true; |
bsalomon@google.com | 0324570 | 2012-08-13 14:31:30 +0000 | [diff] [blame] | 275 | #else |
| 276 | return false; |
| 277 | #endif |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void SkMagnifierImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 281 | this->INHERITED::flatten(buffer); |
| 282 | buffer.writeScalar(fSrcRect.x()); |
| 283 | buffer.writeScalar(fSrcRect.y()); |
| 284 | buffer.writeScalar(fSrcRect.width()); |
| 285 | buffer.writeScalar(fSrcRect.height()); |
| 286 | buffer.writeScalar(fInset); |
| 287 | } |
| 288 | |
| 289 | bool SkMagnifierImageFilter::onFilterImage(Proxy*, const SkBitmap& src, |
| 290 | const SkMatrix&, SkBitmap* dst, |
| 291 | SkIPoint* offset) { |
| 292 | SkASSERT(src.config() == SkBitmap::kARGB_8888_Config); |
| 293 | SkASSERT(fSrcRect.width() < src.width()); |
| 294 | SkASSERT(fSrcRect.height() < src.height()); |
| 295 | |
| 296 | if (src.config() != SkBitmap::kARGB_8888_Config) { |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | SkAutoLockPixels alp(src); |
| 301 | SkASSERT(src.getPixels()); |
| 302 | if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) { |
| 303 | return false; |
| 304 | } |
| 305 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 306 | SkScalar inv_inset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 307 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 308 | SkScalar inv_x_zoom = fSrcRect.width() / src.width(); |
| 309 | SkScalar inv_y_zoom = fSrcRect.height() / src.height(); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 310 | |
| 311 | dst->setConfig(src.config(), src.width(), src.height()); |
| 312 | dst->allocPixels(); |
| 313 | SkColor* sptr = src.getAddr32(0, 0); |
| 314 | SkColor* dptr = dst->getAddr32(0, 0); |
| 315 | int width = src.width(), height = src.height(); |
| 316 | for (int y = 0; y < height; ++y) { |
| 317 | for (int x = 0; x < width; ++x) { |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 318 | SkScalar x_dist = SkMin32(x, width - x - 1) * inv_inset; |
| 319 | SkScalar y_dist = SkMin32(y, height - y - 1) * inv_inset; |
| 320 | SkScalar weight = 0; |
| 321 | |
| 322 | static const SkScalar kScalar2 = SkScalar(2); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 323 | |
| 324 | // To create a smooth curve at the corners, we need to work on |
| 325 | // a square twice the size of the inset. |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 326 | if (x_dist < kScalar2 && y_dist < kScalar2) { |
| 327 | x_dist = kScalar2 - x_dist; |
| 328 | y_dist = kScalar2 - y_dist; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 329 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 330 | SkScalar dist = SkScalarSqrt(SkScalarSquare(x_dist) + |
| 331 | SkScalarSquare(y_dist)); |
| 332 | dist = SkMaxScalar(kScalar2 - dist, 0); |
| 333 | weight = SkMinScalar(SkScalarSquare(dist), SK_Scalar1); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 334 | } else { |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 335 | SkScalar sqDist = SkMinScalar(SkScalarSquare(x_dist), |
| 336 | SkScalarSquare(y_dist)); |
| 337 | weight = SkMinScalar(sqDist, SK_Scalar1); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 338 | } |
| 339 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 340 | SkScalar x_interp = SkScalarMul(weight, (fSrcRect.x() + x * inv_x_zoom)) + |
| 341 | (SK_Scalar1 - weight) * x; |
| 342 | SkScalar y_interp = SkScalarMul(weight, (fSrcRect.y() + y * inv_y_zoom)) + |
| 343 | (SK_Scalar1 - weight) * y; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 344 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 345 | int x_val = SkMin32(SkScalarFloorToInt(x_interp), width - 1); |
| 346 | int y_val = SkMin32(SkScalarFloorToInt(y_interp), height - 1); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 347 | |
| 348 | *dptr = sptr[y_val * width + x_val]; |
| 349 | dptr++; |
| 350 | } |
| 351 | } |
| 352 | return true; |
| 353 | } |