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) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 36 | AutoEffectUnref 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 | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 51 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) 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 | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame^] | 78 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 79 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 80 | GR_DECLARE_EFFECT_TEST; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 81 | |
| 82 | float fXOffset; |
| 83 | float fYOffset; |
| 84 | float fXZoom; |
| 85 | float fYZoom; |
| 86 | float fXInset; |
| 87 | float fYInset; |
| 88 | |
| 89 | typedef GrSingleTextureEffect INHERITED; |
| 90 | }; |
| 91 | |
| 92 | // For brevity |
| 93 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 94 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 95 | class GrGLMagnifierEffect : public GrGLEffect { |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 96 | public: |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 97 | GrGLMagnifierEffect(const GrBackendEffectFactory& factory, const GrEffectRef& effect); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 98 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 99 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 100 | const GrEffectStage&, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 101 | EffectKey, |
| 102 | const char* vertexCoords, |
| 103 | const char* outputColor, |
| 104 | const char* inputColor, |
| 105 | const TextureSamplerArray&) SK_OVERRIDE; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 106 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 107 | virtual void setData(const GrGLUniformManager& uman, const GrEffectStage& stage) SK_OVERRIDE; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 108 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 109 | static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 110 | |
| 111 | private: |
| 112 | |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 113 | UniformHandle fOffsetVar; |
| 114 | UniformHandle fZoomVar; |
| 115 | UniformHandle fInsetVar; |
| 116 | |
| 117 | GrGLEffectMatrix fEffectMatrix; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 118 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 119 | typedef GrGLEffect INHERITED; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 122 | GrGLMagnifierEffect::GrGLMagnifierEffect(const GrBackendEffectFactory& factory, const GrEffectRef&) |
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) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 185 | const GrMagnifierEffect& zoom = GetEffectFromStage<GrMagnifierEffect>(stage); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 186 | uman.set2f(fOffsetVar, zoom.x_offset(), zoom.y_offset()); |
| 187 | uman.set2f(fZoomVar, zoom.x_zoom(), zoom.y_zoom()); |
| 188 | uman.set2f(fInsetVar, zoom.x_inset(), zoom.y_inset()); |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 189 | fEffectMatrix.setData(uman, zoom.getMatrix(), stage.getCoordChangeMatrix(), zoom.texture(0)); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 190 | } |
| 191 | |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 192 | GrGLEffect::EffectKey GrGLMagnifierEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 193 | const GrMagnifierEffect& zoom = GetEffectFromStage<GrMagnifierEffect>(stage); |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 194 | return GrGLEffectMatrix::GenKey(zoom.getMatrix(), |
| 195 | stage.getCoordChangeMatrix(), |
| 196 | zoom.texture(0)); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | ///////////////////////////////////////////////////////////////////// |
| 200 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 201 | GR_DEFINE_EFFECT_TEST(GrMagnifierEffect); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 202 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 203 | GrEffectRef* GrMagnifierEffect::TestCreate(SkRandom* random, |
| 204 | GrContext* context, |
| 205 | GrTexture** textures) { |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 206 | const int kMaxWidth = 200; |
| 207 | const int kMaxHeight = 200; |
| 208 | const int kMaxInset = 20; |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 209 | uint32_t width = random->nextULessThan(kMaxWidth); |
| 210 | uint32_t height = random->nextULessThan(kMaxHeight); |
| 211 | uint32_t x = random->nextULessThan(kMaxWidth - width); |
| 212 | uint32_t y = random->nextULessThan(kMaxHeight - height); |
| 213 | SkScalar inset = SkIntToScalar(random->nextULessThan(kMaxInset)); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 214 | |
| 215 | SkAutoTUnref<SkImageFilter> filter( |
| 216 | new SkMagnifierImageFilter( |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 217 | SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 218 | SkIntToScalar(width), SkIntToScalar(height)), |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 219 | inset)); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 220 | GrEffectRef* effect; |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 221 | filter->asNewEffect(&effect, textures[0]); |
| 222 | GrAssert(NULL != effect); |
| 223 | return effect; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /////////////////////////////////////////////////////////////////////////////// |
| 227 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 228 | const GrBackendEffectFactory& GrMagnifierEffect::getFactory() const { |
| 229 | return GrTBackendEffectFactory<GrMagnifierEffect>::getInstance(); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 230 | } |
| 231 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame^] | 232 | bool GrMagnifierEffect::onIsEqual(const GrEffect& sBase) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 233 | const GrMagnifierEffect& s = CastEffect<GrMagnifierEffect>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 234 | return (this->texture(0) == s.texture(0) && |
| 235 | this->fXOffset == s.fXOffset && |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 236 | this->fYOffset == s.fYOffset && |
| 237 | this->fXZoom == s.fXZoom && |
| 238 | this->fYZoom == s.fYZoom && |
| 239 | this->fXInset == s.fXInset && |
| 240 | this->fYInset == s.fYInset); |
| 241 | } |
| 242 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 243 | void GrMagnifierEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 244 | this->updateConstantColorComponentsForModulation(color, validFlags); |
| 245 | } |
| 246 | |
bsalomon@google.com | 0324570 | 2012-08-13 14:31:30 +0000 | [diff] [blame] | 247 | #endif |
| 248 | |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 249 | //////////////////////////////////////////////////////////////////////////////// |
| 250 | SkMagnifierImageFilter::SkMagnifierImageFilter(SkFlattenableReadBuffer& buffer) |
| 251 | : INHERITED(buffer) { |
| 252 | float x = buffer.readScalar(); |
| 253 | float y = buffer.readScalar(); |
| 254 | float width = buffer.readScalar(); |
| 255 | float height = buffer.readScalar(); |
| 256 | fSrcRect = SkRect::MakeXYWH(x, y, width, height); |
| 257 | fInset = buffer.readScalar(); |
| 258 | } |
| 259 | |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 260 | // FIXME: implement single-input semantics |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 261 | SkMagnifierImageFilter::SkMagnifierImageFilter(SkRect srcRect, SkScalar inset) |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 262 | : INHERITED(0), fSrcRect(srcRect), fInset(inset) { |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 263 | SkASSERT(srcRect.x() >= 0 && srcRect.y() >= 0 && inset >= 0); |
| 264 | } |
| 265 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 266 | bool SkMagnifierImageFilter::asNewEffect(GrEffectRef** effect, GrTexture* texture) const { |
bsalomon@google.com | 0324570 | 2012-08-13 14:31:30 +0000 | [diff] [blame] | 267 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 268 | if (effect) { |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 269 | *effect = GrMagnifierEffect::Create(texture, |
| 270 | fSrcRect.x() / texture->width(), |
| 271 | fSrcRect.y() / texture->height(), |
| 272 | texture->width() / fSrcRect.width(), |
| 273 | texture->height() / fSrcRect.height(), |
| 274 | fInset / texture->width(), |
| 275 | fInset / texture->height()); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 276 | } |
| 277 | return true; |
bsalomon@google.com | 0324570 | 2012-08-13 14:31:30 +0000 | [diff] [blame] | 278 | #else |
| 279 | return false; |
| 280 | #endif |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void SkMagnifierImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 284 | this->INHERITED::flatten(buffer); |
| 285 | buffer.writeScalar(fSrcRect.x()); |
| 286 | buffer.writeScalar(fSrcRect.y()); |
| 287 | buffer.writeScalar(fSrcRect.width()); |
| 288 | buffer.writeScalar(fSrcRect.height()); |
| 289 | buffer.writeScalar(fInset); |
| 290 | } |
| 291 | |
| 292 | bool SkMagnifierImageFilter::onFilterImage(Proxy*, const SkBitmap& src, |
| 293 | const SkMatrix&, SkBitmap* dst, |
| 294 | SkIPoint* offset) { |
| 295 | SkASSERT(src.config() == SkBitmap::kARGB_8888_Config); |
| 296 | SkASSERT(fSrcRect.width() < src.width()); |
| 297 | SkASSERT(fSrcRect.height() < src.height()); |
| 298 | |
| 299 | if (src.config() != SkBitmap::kARGB_8888_Config) { |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | SkAutoLockPixels alp(src); |
| 304 | SkASSERT(src.getPixels()); |
| 305 | if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) { |
| 306 | return false; |
| 307 | } |
| 308 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 309 | SkScalar inv_inset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 310 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 311 | SkScalar inv_x_zoom = fSrcRect.width() / src.width(); |
| 312 | SkScalar inv_y_zoom = fSrcRect.height() / src.height(); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 313 | |
| 314 | dst->setConfig(src.config(), src.width(), src.height()); |
| 315 | dst->allocPixels(); |
| 316 | SkColor* sptr = src.getAddr32(0, 0); |
| 317 | SkColor* dptr = dst->getAddr32(0, 0); |
| 318 | int width = src.width(), height = src.height(); |
| 319 | for (int y = 0; y < height; ++y) { |
| 320 | for (int x = 0; x < width; ++x) { |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 321 | SkScalar x_dist = SkMin32(x, width - x - 1) * inv_inset; |
| 322 | SkScalar y_dist = SkMin32(y, height - y - 1) * inv_inset; |
| 323 | SkScalar weight = 0; |
| 324 | |
| 325 | static const SkScalar kScalar2 = SkScalar(2); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 326 | |
| 327 | // To create a smooth curve at the corners, we need to work on |
| 328 | // a square twice the size of the inset. |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 329 | if (x_dist < kScalar2 && y_dist < kScalar2) { |
| 330 | x_dist = kScalar2 - x_dist; |
| 331 | y_dist = kScalar2 - y_dist; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 332 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 333 | SkScalar dist = SkScalarSqrt(SkScalarSquare(x_dist) + |
| 334 | SkScalarSquare(y_dist)); |
| 335 | dist = SkMaxScalar(kScalar2 - dist, 0); |
| 336 | weight = SkMinScalar(SkScalarSquare(dist), SK_Scalar1); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 337 | } else { |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 338 | SkScalar sqDist = SkMinScalar(SkScalarSquare(x_dist), |
| 339 | SkScalarSquare(y_dist)); |
| 340 | weight = SkMinScalar(sqDist, SK_Scalar1); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 341 | } |
| 342 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 343 | SkScalar x_interp = SkScalarMul(weight, (fSrcRect.x() + x * inv_x_zoom)) + |
| 344 | (SK_Scalar1 - weight) * x; |
| 345 | SkScalar y_interp = SkScalarMul(weight, (fSrcRect.y() + y * inv_y_zoom)) + |
| 346 | (SK_Scalar1 - weight) * y; |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 347 | |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 348 | int x_val = SkMin32(SkScalarFloorToInt(x_interp), width - 1); |
| 349 | int y_val = SkMin32(SkScalarFloorToInt(y_interp), height - 1); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 350 | |
| 351 | *dptr = sptr[y_val * width + x_val]; |
| 352 | dptr++; |
| 353 | } |
| 354 | } |
| 355 | return true; |
| 356 | } |