blob: d0d62c03f13d7c4130fa8c5e0b31ec6603d201cb [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
bsalomon@google.com82aa7482012-08-13 14:22:17 +000013////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com03245702012-08-13 14:31:30 +000014#if SK_SUPPORT_GPU
bsalomon@google.com82aa7482012-08-13 14:22:17 +000015#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000016#include "gl/GrGLEffect.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000017#include "gl/GrGLSL.h"
18#include "gl/GrGLTexture.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000019#include "GrTBackendEffectFactory.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000020
21class GrGLMagnifierEffect;
22
23class GrMagnifierEffect : public GrSingleTextureEffect {
24
25public:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000026 static GrEffectRef* Create(GrTexture* texture,
27 float xOffset,
28 float yOffset,
29 float xZoom,
30 float yZoom,
31 float xInset,
32 float yInset) {
bsalomon@google.com6340a412013-01-22 19:55:59 +000033 AutoEffectUnref effect(SkNEW_ARGS(GrMagnifierEffect, (texture,
34 xOffset,
35 yOffset,
36 xZoom,
37 yZoom,
38 xInset,
39 yInset)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +000040 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000041 }
bsalomon@google.com82aa7482012-08-13 14:22:17 +000042
43 virtual ~GrMagnifierEffect() {};
44
45 static const char* Name() { return "Magnifier"; }
46
bsalomon@google.com396e61f2012-10-25 19:00:29 +000047 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000048 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
bsalomon@google.com82aa7482012-08-13 14:22:17 +000049
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; }
56
bsalomon@google.com422e81a2012-10-25 14:11:03 +000057 typedef GrGLMagnifierEffect GLEffect;
bsalomon@google.com82aa7482012-08-13 14:22:17 +000058
59private:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000060 GrMagnifierEffect(GrTexture* texture,
61 float xOffset,
62 float yOffset,
63 float xZoom,
64 float yZoom,
65 float xInset,
66 float yInset)
67 : GrSingleTextureEffect(texture, MakeDivByTextureWHMatrix(texture))
68 , fXOffset(xOffset)
69 , fYOffset(yOffset)
70 , fXZoom(xZoom)
71 , fYZoom(yZoom)
72 , fXInset(xInset)
73 , fYInset(yInset) {}
74
bsalomon@google.com8a252f72013-01-22 20:35:13 +000075 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000076
bsalomon@google.comf271cc72012-10-24 19:35:13 +000077 GR_DECLARE_EFFECT_TEST;
bsalomon@google.com82aa7482012-08-13 14:22:17 +000078
79 float fXOffset;
80 float fYOffset;
81 float fXZoom;
82 float fYZoom;
83 float fXInset;
84 float fYInset;
85
86 typedef GrSingleTextureEffect INHERITED;
87};
88
89// For brevity
90typedef GrGLUniformManager::UniformHandle UniformHandle;
91
bsalomon@google.com47d7a882012-10-29 12:47:51 +000092class GrGLMagnifierEffect : public GrGLEffect {
bsalomon@google.com82aa7482012-08-13 14:22:17 +000093public:
bsalomon@google.comc7818882013-03-20 19:19:53 +000094 GrGLMagnifierEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
bsalomon@google.com82aa7482012-08-13 14:22:17 +000095
bsalomon@google.com47d7a882012-10-29 12:47:51 +000096 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.comc7818882013-03-20 19:19:53 +000097 const GrDrawEffect&,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000098 EffectKey,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000099 const char* outputColor,
100 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000101 const TransformedCoordsArray&,
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000102 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000103
bsalomon@google.comc7818882013-03-20 19:19:53 +0000104 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000105
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000106private:
bsalomon@google.com17fc6512012-11-02 21:45:01 +0000107 UniformHandle fOffsetVar;
108 UniformHandle fZoomVar;
109 UniformHandle fInsetVar;
110
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000111 typedef GrGLEffect INHERITED;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000112};
113
bsalomon@google.com77af6802013-10-02 13:04:56 +0000114GrGLMagnifierEffect::GrGLMagnifierEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
115 : INHERITED(factory) {
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000116}
117
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000118void GrGLMagnifierEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000119 const GrDrawEffect&,
bsalomon@google.com17fc6512012-11-02 21:45:01 +0000120 EffectKey key,
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000121 const char* outputColor,
122 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000123 const TransformedCoordsArray& coords,
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000124 const TextureSamplerArray& samplers) {
bsalomon@google.com77af6802013-10-02 13:04:56 +0000125 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000126 fOffsetVar = builder->addUniform(
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000127 GrGLShaderBuilder::kFragment_Visibility |
128 GrGLShaderBuilder::kVertex_Visibility,
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000129 kVec2f_GrSLType, "uOffset");
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000130 fZoomVar = builder->addUniform(
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000131 GrGLShaderBuilder::kFragment_Visibility |
132 GrGLShaderBuilder::kVertex_Visibility,
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000133 kVec2f_GrSLType, "uZoom");
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000134 fInsetVar = builder->addUniform(
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000135 GrGLShaderBuilder::kFragment_Visibility |
136 GrGLShaderBuilder::kVertex_Visibility,
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000137 kVec2f_GrSLType, "uInset");
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000138
bsalomon@google.com77af6802013-10-02 13:04:56 +0000139 builder->fsCodeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000140 builder->fsCodeAppendf("\t\tvec2 zoom_coord = %s + %s / %s;\n",
141 builder->getUniformCStr(fOffsetVar),
bsalomon@google.com77af6802013-10-02 13:04:56 +0000142 coords2D.c_str(),
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000143 builder->getUniformCStr(fZoomVar));
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000144
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000145 builder->fsCodeAppend("\t\tvec2 delta = min(coord, vec2(1.0, 1.0) - coord);\n");
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000146
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000147 builder->fsCodeAppendf("\t\tdelta = delta / %s;\n", builder->getUniformCStr(fInsetVar));
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000148
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000149 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");
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000159
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000160 builder->fsCodeAppend("\t\tvec2 mix_coord = mix(coord, zoom_coord, weight);\n");
161 builder->fsCodeAppend("\t\tvec4 output_color = ");
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000162 builder->fsAppendTextureLookup(samplers[0], "mix_coord");
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000163 builder->fsCodeAppend(";\n");
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000164
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000165 builder->fsCodeAppendf("\t\t%s = output_color;", outputColor);
166 SkString modulate;
167 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
168 builder->fsCodeAppend(modulate.c_str());
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000169}
170
171void GrGLMagnifierEffect::setData(const GrGLUniformManager& uman,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000172 const GrDrawEffect& drawEffect) {
173 const GrMagnifierEffect& zoom = drawEffect.castEffect<GrMagnifierEffect>();
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000174 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());
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000177}
178
179/////////////////////////////////////////////////////////////////////
180
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000181GR_DEFINE_EFFECT_TEST(GrMagnifierEffect);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000182
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000183GrEffectRef* GrMagnifierEffect::TestCreate(SkRandom* random,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000184 GrContext* context,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000185 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000186 GrTexture** textures) {
senorblanco@chromium.org1aa68722013-10-17 19:35:09 +0000187 GrTexture* texture = textures[0];
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000188 const int kMaxWidth = 200;
189 const int kMaxHeight = 200;
190 const int kMaxInset = 20;
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000191 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);
senorblanco@chromium.org1aa68722013-10-17 19:35:09 +0000195 uint32_t inset = random->nextULessThan(kMaxInset);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000196
senorblanco@chromium.org1aa68722013-10-17 19:35:09 +0000197 GrEffectRef* effect = GrMagnifierEffect::Create(
198 texture,
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());
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +0000205 SkASSERT(NULL != effect);
bsalomon@google.com021fc732012-10-25 12:47:42 +0000206 return effect;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000207}
208
209///////////////////////////////////////////////////////////////////////////////
210
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000211const GrBackendEffectFactory& GrMagnifierEffect::getFactory() const {
212 return GrTBackendEffectFactory<GrMagnifierEffect>::getInstance();
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000213}
214
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000215bool GrMagnifierEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000216 const GrMagnifierEffect& s = CastEffect<GrMagnifierEffect>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000217 return (this->texture(0) == s.texture(0) &&
218 this->fXOffset == s.fXOffset &&
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000219 this->fYOffset == s.fYOffset &&
220 this->fXZoom == s.fXZoom &&
221 this->fYZoom == s.fYZoom &&
222 this->fXInset == s.fXInset &&
223 this->fYInset == s.fYInset);
224}
225
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000226void GrMagnifierEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
227 this->updateConstantColorComponentsForModulation(color, validFlags);
228}
229
bsalomon@google.com03245702012-08-13 14:31:30 +0000230#endif
231
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000232////////////////////////////////////////////////////////////////////////////////
233SkMagnifierImageFilter::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();
241}
242
senorblanco@chromium.org9f25de72012-10-10 20:36:13 +0000243// FIXME: implement single-input semantics
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000244SkMagnifierImageFilter::SkMagnifierImageFilter(SkRect srcRect, SkScalar inset)
senorblanco@chromium.org9f25de72012-10-10 20:36:13 +0000245 : INHERITED(0), fSrcRect(srcRect), fInset(inset) {
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000246 SkASSERT(srcRect.x() >= 0 && srcRect.y() >= 0 && inset >= 0);
247}
248
bsalomon@google.com03245702012-08-13 14:31:30 +0000249#if SK_SUPPORT_GPU
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000250bool SkMagnifierImageFilter::asNewEffect(GrEffectRef** effect, GrTexture* texture, const SkMatrix&, const SkIRect&) const {
bsalomon@google.com021fc732012-10-25 12:47:42 +0000251 if (effect) {
senorblanco@chromium.orgd043cce2013-04-08 19:43:22 +0000252 *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());
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000259 }
260 return true;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000261}
senorblanco@chromium.orgd043cce2013-04-08 19:43:22 +0000262#endif
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000263
264void 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);
271}
272
273bool SkMagnifierImageFilter::onFilterImage(Proxy*, const SkBitmap& src,
274 const SkMatrix&, SkBitmap* dst,
275 SkIPoint* offset) {
276 SkASSERT(src.config() == SkBitmap::kARGB_8888_Config);
277 SkASSERT(fSrcRect.width() < src.width());
278 SkASSERT(fSrcRect.height() < src.height());
279
280 if (src.config() != SkBitmap::kARGB_8888_Config) {
281 return false;
282 }
283
284 SkAutoLockPixels alp(src);
285 SkASSERT(src.getPixels());
286 if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) {
287 return false;
288 }
289
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000290 SkScalar inv_inset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000291
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000292 SkScalar inv_x_zoom = fSrcRect.width() / src.width();
293 SkScalar inv_y_zoom = fSrcRect.height() / src.height();
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000294
295 dst->setConfig(src.config(), src.width(), src.height());
296 dst->allocPixels();
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) {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000302 SkScalar x_dist = SkMin32(x, width - x - 1) * inv_inset;
303 SkScalar y_dist = SkMin32(y, height - y - 1) * inv_inset;
304 SkScalar weight = 0;
305
306 static const SkScalar kScalar2 = SkScalar(2);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000307
308 // To create a smooth curve at the corners, we need to work on
309 // a square twice the size of the inset.
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000310 if (x_dist < kScalar2 && y_dist < kScalar2) {
311 x_dist = kScalar2 - x_dist;
312 y_dist = kScalar2 - y_dist;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000313
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000314 SkScalar dist = SkScalarSqrt(SkScalarSquare(x_dist) +
315 SkScalarSquare(y_dist));
316 dist = SkMaxScalar(kScalar2 - dist, 0);
317 weight = SkMinScalar(SkScalarSquare(dist), SK_Scalar1);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000318 } else {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000319 SkScalar sqDist = SkMinScalar(SkScalarSquare(x_dist),
320 SkScalarSquare(y_dist));
321 weight = SkMinScalar(sqDist, SK_Scalar1);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000322 }
323
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000324 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;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000328
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000329 int x_val = SkMin32(SkScalarFloorToInt(x_interp), width - 1);
330 int y_val = SkMin32(SkScalarFloorToInt(y_interp), height - 1);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000331
332 *dptr = sptr[y_val * width + x_val];
333 dptr++;
334 }
335 }
336 return true;
337}