| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkMagnifierImageFilter.h" | 9 #include "SkMagnifierImageFilter.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| 11 #include "SkFlattenableBuffers.h" | 11 #include "SkFlattenableBuffers.h" |
| 12 | 12 |
| 13 //////////////////////////////////////////////////////////////////////////////// | 13 //////////////////////////////////////////////////////////////////////////////// |
| 14 #if SK_SUPPORT_GPU | 14 #if SK_SUPPORT_GPU |
| 15 #include "effects/GrSingleTextureEffect.h" | 15 #include "effects/GrSingleTextureEffect.h" |
| 16 #include "gl/GrGLEffect.h" | 16 #include "gl/GrGLEffect.h" |
| 17 #include "gl/GrGLEffectMatrix.h" | |
| 18 #include "gl/GrGLSL.h" | 17 #include "gl/GrGLSL.h" |
| 19 #include "gl/GrGLTexture.h" | 18 #include "gl/GrGLTexture.h" |
| 20 #include "GrTBackendEffectFactory.h" | 19 #include "GrTBackendEffectFactory.h" |
| 21 | 20 |
| 22 class GrGLMagnifierEffect; | 21 class GrGLMagnifierEffect; |
| 23 | 22 |
| 24 class GrMagnifierEffect : public GrSingleTextureEffect { | 23 class GrMagnifierEffect : public GrSingleTextureEffect { |
| 25 | 24 |
| 26 public: | 25 public: |
| 27 static GrEffectRef* Create(GrTexture* texture, | 26 static GrEffectRef* Create(GrTexture* texture, |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 91 |
| 93 class GrGLMagnifierEffect : public GrGLEffect { | 92 class GrGLMagnifierEffect : public GrGLEffect { |
| 94 public: | 93 public: |
| 95 GrGLMagnifierEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 94 GrGLMagnifierEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 96 | 95 |
| 97 virtual void emitCode(GrGLShaderBuilder*, | 96 virtual void emitCode(GrGLShaderBuilder*, |
| 98 const GrDrawEffect&, | 97 const GrDrawEffect&, |
| 99 EffectKey, | 98 EffectKey, |
| 100 const char* outputColor, | 99 const char* outputColor, |
| 101 const char* inputColor, | 100 const char* inputColor, |
| 101 const TransformedCoordsArray&, |
| 102 const TextureSamplerArray&) SK_OVERRIDE; | 102 const TextureSamplerArray&) SK_OVERRIDE; |
| 103 | 103 |
| 104 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; | 104 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; |
| 105 | 105 |
| 106 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | |
| 107 | |
| 108 private: | 106 private: |
| 109 UniformHandle fOffsetVar; | 107 UniformHandle fOffsetVar; |
| 110 UniformHandle fZoomVar; | 108 UniformHandle fZoomVar; |
| 111 UniformHandle fInsetVar; | 109 UniformHandle fInsetVar; |
| 112 | 110 |
| 113 GrGLEffectMatrix fEffectMatrix; | |
| 114 | |
| 115 typedef GrGLEffect INHERITED; | 111 typedef GrGLEffect INHERITED; |
| 116 }; | 112 }; |
| 117 | 113 |
| 118 GrGLMagnifierEffect::GrGLMagnifierEffect(const GrBackendEffectFactory& factory, | 114 GrGLMagnifierEffect::GrGLMagnifierEffect(const GrBackendEffectFactory& factory,
const GrDrawEffect&) |
| 119 const GrDrawEffect& drawEffect) | 115 : INHERITED(factory) { |
| 120 : INHERITED(factory) | |
| 121 , fEffectMatrix(drawEffect.castEffect<GrMagnifierEffect>().coordsType()) { | |
| 122 } | 116 } |
| 123 | 117 |
| 124 void GrGLMagnifierEffect::emitCode(GrGLShaderBuilder* builder, | 118 void GrGLMagnifierEffect::emitCode(GrGLShaderBuilder* builder, |
| 125 const GrDrawEffect&, | 119 const GrDrawEffect&, |
| 126 EffectKey key, | 120 EffectKey key, |
| 127 const char* outputColor, | 121 const char* outputColor, |
| 128 const char* inputColor, | 122 const char* inputColor, |
| 123 const TransformedCoordsArray& coords, |
| 129 const TextureSamplerArray& samplers) { | 124 const TextureSamplerArray& samplers) { |
| 130 SkString coords; | 125 SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
| 131 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); | |
| 132 fOffsetVar = builder->addUniform( | 126 fOffsetVar = builder->addUniform( |
| 133 GrGLShaderBuilder::kFragment_Visibility | | 127 GrGLShaderBuilder::kFragment_Visibility | |
| 134 GrGLShaderBuilder::kVertex_Visibility, | 128 GrGLShaderBuilder::kVertex_Visibility, |
| 135 kVec2f_GrSLType, "uOffset"); | 129 kVec2f_GrSLType, "uOffset"); |
| 136 fZoomVar = builder->addUniform( | 130 fZoomVar = builder->addUniform( |
| 137 GrGLShaderBuilder::kFragment_Visibility | | 131 GrGLShaderBuilder::kFragment_Visibility | |
| 138 GrGLShaderBuilder::kVertex_Visibility, | 132 GrGLShaderBuilder::kVertex_Visibility, |
| 139 kVec2f_GrSLType, "uZoom"); | 133 kVec2f_GrSLType, "uZoom"); |
| 140 fInsetVar = builder->addUniform( | 134 fInsetVar = builder->addUniform( |
| 141 GrGLShaderBuilder::kFragment_Visibility | | 135 GrGLShaderBuilder::kFragment_Visibility | |
| 142 GrGLShaderBuilder::kVertex_Visibility, | 136 GrGLShaderBuilder::kVertex_Visibility, |
| 143 kVec2f_GrSLType, "uInset"); | 137 kVec2f_GrSLType, "uInset"); |
| 144 | 138 |
| 145 builder->fsCodeAppendf("\t\tvec2 coord = %s;\n", coords.c_str()); | 139 builder->fsCodeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
| 146 builder->fsCodeAppendf("\t\tvec2 zoom_coord = %s + %s / %s;\n", | 140 builder->fsCodeAppendf("\t\tvec2 zoom_coord = %s + %s / %s;\n", |
| 147 builder->getUniformCStr(fOffsetVar), | 141 builder->getUniformCStr(fOffsetVar), |
| 148 coords.c_str(), | 142 coords2D.c_str(), |
| 149 builder->getUniformCStr(fZoomVar)); | 143 builder->getUniformCStr(fZoomVar)); |
| 150 | 144 |
| 151 builder->fsCodeAppend("\t\tvec2 delta = min(coord, vec2(1.0, 1.0) - coord);\
n"); | 145 builder->fsCodeAppend("\t\tvec2 delta = min(coord, vec2(1.0, 1.0) - coord);\
n"); |
| 152 | 146 |
| 153 builder->fsCodeAppendf("\t\tdelta = delta / %s;\n", builder->getUniformCStr(
fInsetVar)); | 147 builder->fsCodeAppendf("\t\tdelta = delta / %s;\n", builder->getUniformCStr(
fInsetVar)); |
| 154 | 148 |
| 155 builder->fsCodeAppend("\t\tfloat weight = 0.0;\n"); | 149 builder->fsCodeAppend("\t\tfloat weight = 0.0;\n"); |
| 156 builder->fsCodeAppend("\t\tif (delta.s < 2.0 && delta.t < 2.0) {\n"); | 150 builder->fsCodeAppend("\t\tif (delta.s < 2.0 && delta.t < 2.0) {\n"); |
| 157 builder->fsCodeAppend("\t\t\tdelta = vec2(2.0, 2.0) - delta;\n"); | 151 builder->fsCodeAppend("\t\t\tdelta = vec2(2.0, 2.0) - delta;\n"); |
| 158 builder->fsCodeAppend("\t\t\tfloat dist = length(delta);\n"); | 152 builder->fsCodeAppend("\t\t\tfloat dist = length(delta);\n"); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 173 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); | 167 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 174 builder->fsCodeAppend(modulate.c_str()); | 168 builder->fsCodeAppend(modulate.c_str()); |
| 175 } | 169 } |
| 176 | 170 |
| 177 void GrGLMagnifierEffect::setData(const GrGLUniformManager& uman, | 171 void GrGLMagnifierEffect::setData(const GrGLUniformManager& uman, |
| 178 const GrDrawEffect& drawEffect) { | 172 const GrDrawEffect& drawEffect) { |
| 179 const GrMagnifierEffect& zoom = drawEffect.castEffect<GrMagnifierEffect>(); | 173 const GrMagnifierEffect& zoom = drawEffect.castEffect<GrMagnifierEffect>(); |
| 180 uman.set2f(fOffsetVar, zoom.x_offset(), zoom.y_offset()); | 174 uman.set2f(fOffsetVar, zoom.x_offset(), zoom.y_offset()); |
| 181 uman.set2f(fZoomVar, zoom.x_zoom(), zoom.y_zoom()); | 175 uman.set2f(fZoomVar, zoom.x_zoom(), zoom.y_zoom()); |
| 182 uman.set2f(fInsetVar, zoom.x_inset(), zoom.y_inset()); | 176 uman.set2f(fInsetVar, zoom.x_inset(), zoom.y_inset()); |
| 183 fEffectMatrix.setData(uman, zoom.getMatrix(), drawEffect, zoom.texture(0)); | |
| 184 } | |
| 185 | |
| 186 GrGLEffect::EffectKey GrGLMagnifierEffect::GenKey(const GrDrawEffect& drawEffect
, | |
| 187 const GrGLCaps&) { | |
| 188 const GrMagnifierEffect& zoom = drawEffect.castEffect<GrMagnifierEffect>(); | |
| 189 return GrGLEffectMatrix::GenKey(zoom.getMatrix(), | |
| 190 drawEffect, | |
| 191 zoom.coordsType(), | |
| 192 zoom.texture(0)); | |
| 193 } | 177 } |
| 194 | 178 |
| 195 ///////////////////////////////////////////////////////////////////// | 179 ///////////////////////////////////////////////////////////////////// |
| 196 | 180 |
| 197 GR_DEFINE_EFFECT_TEST(GrMagnifierEffect); | 181 GR_DEFINE_EFFECT_TEST(GrMagnifierEffect); |
| 198 | 182 |
| 199 GrEffectRef* GrMagnifierEffect::TestCreate(SkRandom* random, | 183 GrEffectRef* GrMagnifierEffect::TestCreate(SkRandom* random, |
| 200 GrContext* context, | 184 GrContext* context, |
| 201 const GrDrawTargetCaps&, | 185 const GrDrawTargetCaps&, |
| 202 GrTexture** textures) { | 186 GrTexture** textures) { |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 | 326 |
| 343 int x_val = SkMin32(SkScalarFloorToInt(x_interp), width - 1); | 327 int x_val = SkMin32(SkScalarFloorToInt(x_interp), width - 1); |
| 344 int y_val = SkMin32(SkScalarFloorToInt(y_interp), height - 1); | 328 int y_val = SkMin32(SkScalarFloorToInt(y_interp), height - 1); |
| 345 | 329 |
| 346 *dptr = sptr[y_val * width + x_val]; | 330 *dptr = sptr[y_val * width + x_val]; |
| 347 dptr++; | 331 dptr++; |
| 348 } | 332 } |
| 349 } | 333 } |
| 350 return true; | 334 return true; |
| 351 } | 335 } |
| OLD | NEW |