blob: e7f266eeff6efe17f0c51c5818121509e2d196a6 [file] [log] [blame]
bsalomon@google.com82aa7482012-08-13 14:22:17 +00001/*
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.com82aa7482012-08-13 14:22:17 +000012
13#include <algorithm>
14
15////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com03245702012-08-13 14:31:30 +000016#if SK_SUPPORT_GPU
bsalomon@google.com82aa7482012-08-13 14:22:17 +000017#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000018#include "gl/GrGLEffect.h"
bsalomon@google.com17fc6512012-11-02 21:45:01 +000019#include "gl/GrGLEffectMatrix.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000020#include "gl/GrGLSL.h"
21#include "gl/GrGLTexture.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000022#include "GrTBackendEffectFactory.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000023
24class GrGLMagnifierEffect;
25
26class GrMagnifierEffect : public GrSingleTextureEffect {
27
28public:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000029 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.coma1ebbe42013-01-16 15:51:47 +000043 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000044 }
bsalomon@google.com82aa7482012-08-13 14:22:17 +000045
46 virtual ~GrMagnifierEffect() {};
47
48 static const char* Name() { return "Magnifier"; }
49
bsalomon@google.com396e61f2012-10-25 19:00:29 +000050 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
bsalomon@google.coma469c282012-10-24 18:28:34 +000051 virtual bool isEqual(const GrEffect&) const SK_OVERRIDE;
bsalomon@google.com82aa7482012-08-13 14:22:17 +000052
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.com422e81a2012-10-25 14:11:03 +000060 typedef GrGLMagnifierEffect GLEffect;
bsalomon@google.com82aa7482012-08-13 14:22:17 +000061
62private:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000063 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.comf271cc72012-10-24 19:35:13 +000078 GR_DECLARE_EFFECT_TEST;
bsalomon@google.com82aa7482012-08-13 14:22:17 +000079
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
91typedef GrGLUniformManager::UniformHandle UniformHandle;
92
bsalomon@google.com47d7a882012-10-29 12:47:51 +000093class GrGLMagnifierEffect : public GrGLEffect {
bsalomon@google.com82aa7482012-08-13 14:22:17 +000094public:
bsalomon@google.com396e61f2012-10-25 19:00:29 +000095 GrGLMagnifierEffect(const GrBackendEffectFactory& factory,
bsalomon@google.com021fc732012-10-25 12:47:42 +000096 const GrEffect& effect);
bsalomon@google.com82aa7482012-08-13 14:22:17 +000097
bsalomon@google.com47d7a882012-10-29 12:47:51 +000098 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000099 const GrEffectStage&,
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000100 EffectKey,
101 const char* vertexCoords,
102 const char* outputColor,
103 const char* inputColor,
104 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000105
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000106 virtual void setData(const GrGLUniformManager& uman, const GrEffectStage& stage) SK_OVERRIDE;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000107
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000108 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000109
110private:
111
bsalomon@google.com17fc6512012-11-02 21:45:01 +0000112 UniformHandle fOffsetVar;
113 UniformHandle fZoomVar;
114 UniformHandle fInsetVar;
115
116 GrGLEffectMatrix fEffectMatrix;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000117
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000118 typedef GrGLEffect INHERITED;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000119};
120
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000121GrGLMagnifierEffect::GrGLMagnifierEffect(const GrBackendEffectFactory& factory,
bsalomon@google.com021fc732012-10-25 12:47:42 +0000122 const GrEffect& effect)
bsalomon@google.com374e7592012-10-23 17:30:45 +0000123 : INHERITED(factory)
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000124 , fOffsetVar(GrGLUniformManager::kInvalidUniformHandle)
125 , fZoomVar(GrGLUniformManager::kInvalidUniformHandle)
126 , fInsetVar(GrGLUniformManager::kInvalidUniformHandle) {
127}
128
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000129void GrGLMagnifierEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000130 const GrEffectStage&,
bsalomon@google.com17fc6512012-11-02 21:45:01 +0000131 EffectKey key,
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000132 const char* vertexCoords,
133 const char* outputColor,
134 const char* inputColor,
135 const TextureSamplerArray& samplers) {
bsalomon@google.com17fc6512012-11-02 21:45:01 +0000136 const char* coords;
137 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords);
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000138 fOffsetVar = builder->addUniform(
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000139 GrGLShaderBuilder::kFragment_ShaderType |
140 GrGLShaderBuilder::kVertex_ShaderType,
141 kVec2f_GrSLType, "uOffset");
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000142 fZoomVar = builder->addUniform(
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000143 GrGLShaderBuilder::kFragment_ShaderType |
144 GrGLShaderBuilder::kVertex_ShaderType,
145 kVec2f_GrSLType, "uZoom");
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000146 fInsetVar = builder->addUniform(
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000147 GrGLShaderBuilder::kFragment_ShaderType |
148 GrGLShaderBuilder::kVertex_ShaderType,
149 kVec2f_GrSLType, "uInset");
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000150
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000151 SkString* code = &builder->fFSCode;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000152
bsalomon@google.com17fc6512012-11-02 21:45:01 +0000153 code->appendf("\t\tvec2 coord = %s;\n", coords);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000154 code->appendf("\t\tvec2 zoom_coord = %s + %s / %s;\n",
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000155 builder->getUniformCStr(fOffsetVar),
bsalomon@google.com17fc6512012-11-02 21:45:01 +0000156 coords,
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000157 builder->getUniformCStr(fZoomVar));
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000158
159 code->appendf("\t\tvec2 delta = min(coord, vec2(1.0, 1.0) - coord);\n");
160
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000161 code->appendf("\t\tdelta = delta / %s;\n", builder->getUniformCStr(fInsetVar));
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000162
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.com47d7a882012-10-29 12:47:51 +0000171 code->appendf("\t\t\tweight = min(min(delta_squared.s, delta_squared.y), 1.0);\n");
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000172 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.com47d7a882012-10-29 12:47:51 +0000176 builder->appendTextureLookup(code, samplers[0], "mix_coord");
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000177 code->append(";\n");
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000178
179 code->appendf("\t\t%s = output_color;", outputColor);
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000180 GrGLSLMulVarBy4f(code, 2, outputColor, inputColor);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000181}
182
183void GrGLMagnifierEffect::setData(const GrGLUniformManager& uman,
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000184 const GrEffectStage& stage) {
185 const GrMagnifierEffect& zoom = static_cast<const GrMagnifierEffect&>(*stage.getEffect());
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000186
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.com17fc6512012-11-02 21:45:01 +0000190 fEffectMatrix.setData(uman, zoom.getMatrix(), stage.getCoordChangeMatrix(), zoom.texture(0));
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000191}
192
bsalomon@google.com17fc6512012-11-02 21:45:01 +0000193GrGLEffect::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.com82aa7482012-08-13 14:22:17 +0000198}
199
200/////////////////////////////////////////////////////////////////////
201
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000202GR_DEFINE_EFFECT_TEST(GrMagnifierEffect);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000203
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000204GrEffectRef* GrMagnifierEffect::TestCreate(SkRandom* random,
205 GrContext* context,
206 GrTexture** textures) {
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000207 const int kMaxWidth = 200;
208 const int kMaxHeight = 200;
209 const int kMaxInset = 20;
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000210 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.com82aa7482012-08-13 14:22:17 +0000215
216 SkAutoTUnref<SkImageFilter> filter(
217 new SkMagnifierImageFilter(
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000218 SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
219 SkIntToScalar(width), SkIntToScalar(height)),
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000220 inset));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000221 GrEffectRef* effect;
bsalomon@google.com021fc732012-10-25 12:47:42 +0000222 filter->asNewEffect(&effect, textures[0]);
223 GrAssert(NULL != effect);
224 return effect;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000225}
226
227///////////////////////////////////////////////////////////////////////////////
228
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000229const GrBackendEffectFactory& GrMagnifierEffect::getFactory() const {
230 return GrTBackendEffectFactory<GrMagnifierEffect>::getInstance();
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000231}
232
bsalomon@google.coma469c282012-10-24 18:28:34 +0000233bool GrMagnifierEffect::isEqual(const GrEffect& sBase) const {
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000234 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.com03245702012-08-13 14:31:30 +0000244#endif
245
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000246////////////////////////////////////////////////////////////////////////////////
247SkMagnifierImageFilter::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.org9f25de72012-10-10 20:36:13 +0000257// FIXME: implement single-input semantics
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000258SkMagnifierImageFilter::SkMagnifierImageFilter(SkRect srcRect, SkScalar inset)
senorblanco@chromium.org9f25de72012-10-10 20:36:13 +0000259 : INHERITED(0), fSrcRect(srcRect), fInset(inset) {
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000260 SkASSERT(srcRect.x() >= 0 && srcRect.y() >= 0 && inset >= 0);
261}
262
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000263bool SkMagnifierImageFilter::asNewEffect(GrEffectRef** effect, GrTexture* texture) const {
bsalomon@google.com03245702012-08-13 14:31:30 +0000264#if SK_SUPPORT_GPU
bsalomon@google.com021fc732012-10-25 12:47:42 +0000265 if (effect) {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000266 *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.com82aa7482012-08-13 14:22:17 +0000273 }
274 return true;
bsalomon@google.com03245702012-08-13 14:31:30 +0000275#else
276 return false;
277#endif
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000278}
279
280void 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
289bool 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.com5d8d1862012-08-15 14:36:41 +0000306 SkScalar inv_inset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000307
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000308 SkScalar inv_x_zoom = fSrcRect.width() / src.width();
309 SkScalar inv_y_zoom = fSrcRect.height() / src.height();
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000310
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.com5d8d1862012-08-15 14:36:41 +0000318 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.com82aa7482012-08-13 14:22:17 +0000323
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.com5d8d1862012-08-15 14:36:41 +0000326 if (x_dist < kScalar2 && y_dist < kScalar2) {
327 x_dist = kScalar2 - x_dist;
328 y_dist = kScalar2 - y_dist;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000329
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000330 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.com82aa7482012-08-13 14:22:17 +0000334 } else {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000335 SkScalar sqDist = SkMinScalar(SkScalarSquare(x_dist),
336 SkScalarSquare(y_dist));
337 weight = SkMinScalar(sqDist, SK_Scalar1);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000338 }
339
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000340 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.com82aa7482012-08-13 14:22:17 +0000344
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000345 int x_val = SkMin32(SkScalarFloorToInt(x_interp), width - 1);
346 int y_val = SkMin32(SkScalarFloorToInt(y_interp), height - 1);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000347
348 *dptr = sptr[y_val * width + x_val];
349 dptr++;
350 }
351 }
352 return true;
353}