2 * Copyright 2012 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkMagnifierImageFilter.h"
10 #include "SkColorPriv.h"
11 #include "SkFlattenableBuffers.h"
13 ////////////////////////////////////////////////////////////////////////////////
15 #include "effects/GrSingleTextureEffect.h"
16 #include "gl/GrGLEffect.h"
17 #include "gl/GrGLSL.h"
18 #include "gl/GrGLTexture.h"
19 #include "GrTBackendEffectFactory.h"
21 class GrGLMagnifierEffect;
23 class GrMagnifierEffect : public GrSingleTextureEffect {
26 static GrEffectRef* Create(GrTexture* texture,
33 AutoEffectUnref effect(SkNEW_ARGS(GrMagnifierEffect, (texture,
40 return CreateEffectRef(effect);
43 virtual ~GrMagnifierEffect() {};
45 static const char* Name() { return "Magnifier"; }
47 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
48 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
50 float x_offset() const { return fXOffset; }
51 float y_offset() const { return fYOffset; }
52 float x_zoom() const { return fXZoom; }
53 float y_zoom() const { return fYZoom; }
54 float x_inset() const { return fXInset; }
55 float y_inset() const { return fYInset; }
57 typedef GrGLMagnifierEffect GLEffect;
60 GrMagnifierEffect(GrTexture* texture,
67 : GrSingleTextureEffect(texture, MakeDivByTextureWHMatrix(texture))
75 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
77 GR_DECLARE_EFFECT_TEST;
86 typedef GrSingleTextureEffect INHERITED;
90 typedef GrGLUniformManager::UniformHandle UniformHandle;
92 class GrGLMagnifierEffect : public GrGLEffect {
94 GrGLMagnifierEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
96 virtual void emitCode(GrGLShaderBuilder*,
99 const char* outputColor,
100 const char* inputColor,
101 const TransformedCoordsArray&,
102 const TextureSamplerArray&) SK_OVERRIDE;
104 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
107 UniformHandle fOffsetVar;
108 UniformHandle fZoomVar;
109 UniformHandle fInsetVar;
111 typedef GrGLEffect INHERITED;
114 GrGLMagnifierEffect::GrGLMagnifierEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
115 : INHERITED(factory) {
118 void GrGLMagnifierEffect::emitCode(GrGLShaderBuilder* builder,
121 const char* outputColor,
122 const char* inputColor,
123 const TransformedCoordsArray& coords,
124 const TextureSamplerArray& samplers) {
125 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
126 fOffsetVar = builder->addUniform(
127 GrGLShaderBuilder::kFragment_Visibility |
128 GrGLShaderBuilder::kVertex_Visibility,
129 kVec2f_GrSLType, "uOffset");
130 fZoomVar = builder->addUniform(
131 GrGLShaderBuilder::kFragment_Visibility |
132 GrGLShaderBuilder::kVertex_Visibility,
133 kVec2f_GrSLType, "uZoom");
134 fInsetVar = builder->addUniform(
135 GrGLShaderBuilder::kFragment_Visibility |
136 GrGLShaderBuilder::kVertex_Visibility,
137 kVec2f_GrSLType, "uInset");
139 builder->fsCodeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
140 builder->fsCodeAppendf("\t\tvec2 zoom_coord = %s + %s / %s;\n",
141 builder->getUniformCStr(fOffsetVar),
143 builder->getUniformCStr(fZoomVar));
145 builder->fsCodeAppend("\t\tvec2 delta = min(coord, vec2(1.0, 1.0) - coord);\n");
147 builder->fsCodeAppendf("\t\tdelta = delta / %s;\n", builder->getUniformCStr(fInsetVar));
149 builder->fsCodeAppend("\t\tfloat weight = 0.0;\n");
150 builder->fsCodeAppend("\t\tif (delta.s < 2.0 && delta.t < 2.0) {\n");
151 builder->fsCodeAppend("\t\t\tdelta = vec2(2.0, 2.0) - delta;\n");
152 builder->fsCodeAppend("\t\t\tfloat dist = length(delta);\n");
153 builder->fsCodeAppend("\t\t\tdist = max(2.0 - dist, 0.0);\n");
154 builder->fsCodeAppend("\t\t\tweight = min(dist * dist, 1.0);\n");
155 builder->fsCodeAppend("\t\t} else {\n");
156 builder->fsCodeAppend("\t\t\tvec2 delta_squared = delta * delta;\n");
157 builder->fsCodeAppend("\t\t\tweight = min(min(delta_squared.s, delta_squared.y), 1.0);\n");
158 builder->fsCodeAppend("\t\t}\n");
160 builder->fsCodeAppend("\t\tvec2 mix_coord = mix(coord, zoom_coord, weight);\n");
161 builder->fsCodeAppend("\t\tvec4 output_color = ");
162 builder->fsAppendTextureLookup(samplers[0], "mix_coord");
163 builder->fsCodeAppend(";\n");
165 builder->fsCodeAppendf("\t\t%s = output_color;", outputColor);
167 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
168 builder->fsCodeAppend(modulate.c_str());
171 void GrGLMagnifierEffect::setData(const GrGLUniformManager& uman,
172 const GrDrawEffect& drawEffect) {
173 const GrMagnifierEffect& zoom = drawEffect.castEffect<GrMagnifierEffect>();
174 uman.set2f(fOffsetVar, zoom.x_offset(), zoom.y_offset());
175 uman.set2f(fZoomVar, zoom.x_zoom(), zoom.y_zoom());
176 uman.set2f(fInsetVar, zoom.x_inset(), zoom.y_inset());
179 /////////////////////////////////////////////////////////////////////
181 GR_DEFINE_EFFECT_TEST(GrMagnifierEffect);
183 GrEffectRef* GrMagnifierEffect::TestCreate(SkRandom* random,
185 const GrDrawTargetCaps&,
186 GrTexture** textures) {
187 GrTexture* texture = textures[0];
188 const int kMaxWidth = 200;
189 const int kMaxHeight = 200;
190 const int kMaxInset = 20;
191 uint32_t width = random->nextULessThan(kMaxWidth);
192 uint32_t height = random->nextULessThan(kMaxHeight);
193 uint32_t x = random->nextULessThan(kMaxWidth - width);
194 uint32_t y = random->nextULessThan(kMaxHeight - height);
195 uint32_t inset = random->nextULessThan(kMaxInset);
197 GrEffectRef* effect = GrMagnifierEffect::Create(
199 (float) width / texture->width(),
200 (float) height / texture->height(),
201 texture->width() / (float) x,
202 texture->height() / (float) y,
203 (float) inset / texture->width(),
204 (float) inset / texture->height());
205 SkASSERT(NULL != effect);
209 ///////////////////////////////////////////////////////////////////////////////
211 const GrBackendEffectFactory& GrMagnifierEffect::getFactory() const {
212 return GrTBackendEffectFactory<GrMagnifierEffect>::getInstance();
215 bool GrMagnifierEffect::onIsEqual(const GrEffect& sBase) const {
216 const GrMagnifierEffect& s = CastEffect<GrMagnifierEffect>(sBase);
217 return (this->texture(0) == s.texture(0) &&
218 this->fXOffset == s.fXOffset &&
219 this->fYOffset == s.fYOffset &&
220 this->fXZoom == s.fXZoom &&
221 this->fYZoom == s.fYZoom &&
222 this->fXInset == s.fXInset &&
223 this->fYInset == s.fYInset);
226 void GrMagnifierEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
227 this->updateConstantColorComponentsForModulation(color, validFlags);
232 ////////////////////////////////////////////////////////////////////////////////
233 SkMagnifierImageFilter::SkMagnifierImageFilter(SkFlattenableReadBuffer& buffer)
234 : INHERITED(buffer) {
235 float x = buffer.readScalar();
236 float y = buffer.readScalar();
237 float width = buffer.readScalar();
238 float height = buffer.readScalar();
239 fSrcRect = SkRect::MakeXYWH(x, y, width, height);
240 fInset = buffer.readScalar();
243 // FIXME: implement single-input semantics
244 SkMagnifierImageFilter::SkMagnifierImageFilter(SkRect srcRect, SkScalar inset)
245 : INHERITED(0), fSrcRect(srcRect), fInset(inset) {
246 SkASSERT(srcRect.x() >= 0 && srcRect.y() >= 0 && inset >= 0);
250 bool SkMagnifierImageFilter::asNewEffect(GrEffectRef** effect, GrTexture* texture, const SkMatrix&) const {
252 *effect = GrMagnifierEffect::Create(texture,
253 fSrcRect.x() / texture->width(),
254 fSrcRect.y() / texture->height(),
255 texture->width() / fSrcRect.width(),
256 texture->height() / fSrcRect.height(),
257 fInset / texture->width(),
258 fInset / texture->height());
264 void SkMagnifierImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const {
265 this->INHERITED::flatten(buffer);
266 buffer.writeScalar(fSrcRect.x());
267 buffer.writeScalar(fSrcRect.y());
268 buffer.writeScalar(fSrcRect.width());
269 buffer.writeScalar(fSrcRect.height());
270 buffer.writeScalar(fInset);
273 bool SkMagnifierImageFilter::onFilterImage(Proxy*, const SkBitmap& src,
274 const SkMatrix&, SkBitmap* dst,
276 SkASSERT(src.config() == SkBitmap::kARGB_8888_Config);
277 SkASSERT(fSrcRect.width() < src.width());
278 SkASSERT(fSrcRect.height() < src.height());
280 if (src.config() != SkBitmap::kARGB_8888_Config) {
284 SkAutoLockPixels alp(src);
285 SkASSERT(src.getPixels());
286 if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) {
290 SkScalar inv_inset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1;
292 SkScalar inv_x_zoom = fSrcRect.width() / src.width();
293 SkScalar inv_y_zoom = fSrcRect.height() / src.height();
295 dst->setConfig(src.config(), src.width(), src.height());
297 SkColor* sptr = src.getAddr32(0, 0);
298 SkColor* dptr = dst->getAddr32(0, 0);
299 int width = src.width(), height = src.height();
300 for (int y = 0; y < height; ++y) {
301 for (int x = 0; x < width; ++x) {
302 SkScalar x_dist = SkMin32(x, width - x - 1) * inv_inset;
303 SkScalar y_dist = SkMin32(y, height - y - 1) * inv_inset;
306 static const SkScalar kScalar2 = SkScalar(2);
308 // To create a smooth curve at the corners, we need to work on
309 // a square twice the size of the inset.
310 if (x_dist < kScalar2 && y_dist < kScalar2) {
311 x_dist = kScalar2 - x_dist;
312 y_dist = kScalar2 - y_dist;
314 SkScalar dist = SkScalarSqrt(SkScalarSquare(x_dist) +
315 SkScalarSquare(y_dist));
316 dist = SkMaxScalar(kScalar2 - dist, 0);
317 weight = SkMinScalar(SkScalarSquare(dist), SK_Scalar1);
319 SkScalar sqDist = SkMinScalar(SkScalarSquare(x_dist),
320 SkScalarSquare(y_dist));
321 weight = SkMinScalar(sqDist, SK_Scalar1);
324 SkScalar x_interp = SkScalarMul(weight, (fSrcRect.x() + x * inv_x_zoom)) +
325 (SK_Scalar1 - weight) * x;
326 SkScalar y_interp = SkScalarMul(weight, (fSrcRect.y() + y * inv_y_zoom)) +
327 (SK_Scalar1 - weight) * y;
329 int x_val = SkMin32(SkScalarFloorToInt(x_interp), width - 1);
330 int y_val = SkMin32(SkScalarFloorToInt(y_interp), height - 1);
332 *dptr = sptr[y_val * width + x_val];