blob: e6f3984bddda3a5beb5393a09d17ccf0ac86e65e [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"
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000012#include "SkValidationUtils.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000013
bsalomon@google.com82aa7482012-08-13 14:22:17 +000014////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com03245702012-08-13 14:31:30 +000015#if SK_SUPPORT_GPU
bsalomon@google.com82aa7482012-08-13 14:22:17 +000016#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000017#include "gl/GrGLEffect.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000018#include "gl/GrGLSL.h"
19#include "gl/GrGLTexture.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000020#include "GrTBackendEffectFactory.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +000021
22class GrGLMagnifierEffect;
23
24class GrMagnifierEffect : public GrSingleTextureEffect {
25
26public:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000027 static GrEffectRef* Create(GrTexture* texture,
28 float xOffset,
29 float yOffset,
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +000030 float xInvZoom,
31 float yInvZoom,
32 float xInvInset,
33 float yInvInset) {
bsalomon@google.com6340a412013-01-22 19:55:59 +000034 AutoEffectUnref effect(SkNEW_ARGS(GrMagnifierEffect, (texture,
35 xOffset,
36 yOffset,
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +000037 xInvZoom,
38 yInvZoom,
39 xInvInset,
40 yInvInset)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +000041 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000042 }
bsalomon@google.com82aa7482012-08-13 14:22:17 +000043
44 virtual ~GrMagnifierEffect() {};
45
46 static const char* Name() { return "Magnifier"; }
47
bsalomon@google.com396e61f2012-10-25 19:00:29 +000048 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000049 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
bsalomon@google.com82aa7482012-08-13 14:22:17 +000050
51 float x_offset() const { return fXOffset; }
52 float y_offset() const { return fYOffset; }
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +000053 float x_inv_zoom() const { return fXInvZoom; }
54 float y_inv_zoom() const { return fYInvZoom; }
55 float x_inv_inset() const { return fXInvInset; }
56 float y_inv_inset() const { return fYInvInset; }
bsalomon@google.com82aa7482012-08-13 14:22:17 +000057
bsalomon@google.com422e81a2012-10-25 14:11:03 +000058 typedef GrGLMagnifierEffect GLEffect;
bsalomon@google.com82aa7482012-08-13 14:22:17 +000059
60private:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000061 GrMagnifierEffect(GrTexture* texture,
62 float xOffset,
63 float yOffset,
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +000064 float xInvZoom,
65 float yInvZoom,
66 float xInvInset,
67 float yInvInset)
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000068 : GrSingleTextureEffect(texture, MakeDivByTextureWHMatrix(texture))
69 , fXOffset(xOffset)
70 , fYOffset(yOffset)
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +000071 , fXInvZoom(xInvZoom)
72 , fYInvZoom(yInvZoom)
73 , fXInvInset(xInvInset)
74 , fYInvInset(yInvInset) {}
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000075
bsalomon@google.com8a252f72013-01-22 20:35:13 +000076 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000077
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;
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +000082 float fXInvZoom;
83 float fYInvZoom;
84 float fXInvInset;
85 float fYInvInset;
bsalomon@google.com82aa7482012-08-13 14:22:17 +000086
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.comc7818882013-03-20 19:19:53 +000095 GrGLMagnifierEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
bsalomon@google.com82aa7482012-08-13 14:22:17 +000096
bsalomon@google.com47d7a882012-10-29 12:47:51 +000097 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.comc7818882013-03-20 19:19:53 +000098 const GrDrawEffect&,
bsalomon@google.com47d7a882012-10-29 12:47:51 +000099 EffectKey,
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000100 const char* outputColor,
101 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000102 const TransformedCoordsArray&,
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000103 const TextureSamplerArray&) SK_OVERRIDE;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000104
bsalomon@google.comc7818882013-03-20 19:19:53 +0000105 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000106
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000107private:
bsalomon@google.com17fc6512012-11-02 21:45:01 +0000108 UniformHandle fOffsetVar;
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +0000109 UniformHandle fInvZoomVar;
110 UniformHandle fInvInsetVar;
bsalomon@google.com17fc6512012-11-02 21:45:01 +0000111
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000112 typedef GrGLEffect INHERITED;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000113};
114
bsalomon@google.com77af6802013-10-02 13:04:56 +0000115GrGLMagnifierEffect::GrGLMagnifierEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
116 : INHERITED(factory) {
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000117}
118
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000119void GrGLMagnifierEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000120 const GrDrawEffect&,
bsalomon@google.com17fc6512012-11-02 21:45:01 +0000121 EffectKey key,
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000122 const char* outputColor,
123 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000124 const TransformedCoordsArray& coords,
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000125 const TextureSamplerArray& samplers) {
bsalomon@google.com77af6802013-10-02 13:04:56 +0000126 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
bsalomon@google.com47d7a882012-10-29 12:47:51 +0000127 fOffsetVar = builder->addUniform(
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000128 GrGLShaderBuilder::kFragment_Visibility |
129 GrGLShaderBuilder::kVertex_Visibility,
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +0000130 kVec2f_GrSLType, "Offset");
131 fInvZoomVar = builder->addUniform(
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000132 GrGLShaderBuilder::kFragment_Visibility |
133 GrGLShaderBuilder::kVertex_Visibility,
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +0000134 kVec2f_GrSLType, "InvZoom");
135 fInvInsetVar = builder->addUniform(
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000136 GrGLShaderBuilder::kFragment_Visibility |
137 GrGLShaderBuilder::kVertex_Visibility,
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +0000138 kVec2f_GrSLType, "InvInset");
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000139
bsalomon@google.com77af6802013-10-02 13:04:56 +0000140 builder->fsCodeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +0000141 builder->fsCodeAppendf("\t\tvec2 zoom_coord = %s + %s * %s;\n",
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000142 builder->getUniformCStr(fOffsetVar),
bsalomon@google.com77af6802013-10-02 13:04:56 +0000143 coords2D.c_str(),
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +0000144 builder->getUniformCStr(fInvZoomVar));
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000145
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000146 builder->fsCodeAppend("\t\tvec2 delta = min(coord, vec2(1.0, 1.0) - coord);\n");
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000147
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +0000148 builder->fsCodeAppendf("\t\tdelta = delta * %s;\n", builder->getUniformCStr(fInvInsetVar));
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000149
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000150 builder->fsCodeAppend("\t\tfloat weight = 0.0;\n");
151 builder->fsCodeAppend("\t\tif (delta.s < 2.0 && delta.t < 2.0) {\n");
152 builder->fsCodeAppend("\t\t\tdelta = vec2(2.0, 2.0) - delta;\n");
153 builder->fsCodeAppend("\t\t\tfloat dist = length(delta);\n");
154 builder->fsCodeAppend("\t\t\tdist = max(2.0 - dist, 0.0);\n");
155 builder->fsCodeAppend("\t\t\tweight = min(dist * dist, 1.0);\n");
156 builder->fsCodeAppend("\t\t} else {\n");
157 builder->fsCodeAppend("\t\t\tvec2 delta_squared = delta * delta;\n");
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +0000158 builder->fsCodeAppend("\t\t\tweight = min(min(delta_squared.x, delta_squared.y), 1.0);\n");
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000159 builder->fsCodeAppend("\t\t}\n");
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000160
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000161 builder->fsCodeAppend("\t\tvec2 mix_coord = mix(coord, zoom_coord, weight);\n");
162 builder->fsCodeAppend("\t\tvec4 output_color = ");
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000163 builder->fsAppendTextureLookup(samplers[0], "mix_coord");
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000164 builder->fsCodeAppend(";\n");
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000165
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000166 builder->fsCodeAppendf("\t\t%s = output_color;", outputColor);
167 SkString modulate;
168 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
169 builder->fsCodeAppend(modulate.c_str());
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000170}
171
172void GrGLMagnifierEffect::setData(const GrGLUniformManager& uman,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000173 const GrDrawEffect& drawEffect) {
174 const GrMagnifierEffect& zoom = drawEffect.castEffect<GrMagnifierEffect>();
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000175 uman.set2f(fOffsetVar, zoom.x_offset(), zoom.y_offset());
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +0000176 uman.set2f(fInvZoomVar, zoom.x_inv_zoom(), zoom.y_inv_zoom());
177 uman.set2f(fInvInsetVar, zoom.x_inv_inset(), zoom.y_inv_inset());
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000178}
179
180/////////////////////////////////////////////////////////////////////
181
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000182GR_DEFINE_EFFECT_TEST(GrMagnifierEffect);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000183
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000184GrEffectRef* GrMagnifierEffect::TestCreate(SkRandom* random,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000185 GrContext* context,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000186 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000187 GrTexture** textures) {
senorblanco@chromium.org1aa68722013-10-17 19:35:09 +0000188 GrTexture* texture = textures[0];
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000189 const int kMaxWidth = 200;
190 const int kMaxHeight = 200;
191 const int kMaxInset = 20;
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000192 uint32_t width = random->nextULessThan(kMaxWidth);
193 uint32_t height = random->nextULessThan(kMaxHeight);
194 uint32_t x = random->nextULessThan(kMaxWidth - width);
195 uint32_t y = random->nextULessThan(kMaxHeight - height);
senorblanco@chromium.org1aa68722013-10-17 19:35:09 +0000196 uint32_t inset = random->nextULessThan(kMaxInset);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000197
senorblanco@chromium.org1aa68722013-10-17 19:35:09 +0000198 GrEffectRef* effect = GrMagnifierEffect::Create(
199 texture,
200 (float) width / texture->width(),
201 (float) height / texture->height(),
202 texture->width() / (float) x,
203 texture->height() / (float) y,
204 (float) inset / texture->width(),
205 (float) inset / texture->height());
commit-bot@chromium.org96ae6882013-08-14 12:09:00 +0000206 SkASSERT(NULL != effect);
bsalomon@google.com021fc732012-10-25 12:47:42 +0000207 return effect;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000208}
209
210///////////////////////////////////////////////////////////////////////////////
211
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000212const GrBackendEffectFactory& GrMagnifierEffect::getFactory() const {
213 return GrTBackendEffectFactory<GrMagnifierEffect>::getInstance();
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000214}
215
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000216bool GrMagnifierEffect::onIsEqual(const GrEffect& sBase) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000217 const GrMagnifierEffect& s = CastEffect<GrMagnifierEffect>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000218 return (this->texture(0) == s.texture(0) &&
219 this->fXOffset == s.fXOffset &&
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000220 this->fYOffset == s.fYOffset &&
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +0000221 this->fXInvZoom == s.fXInvZoom &&
222 this->fYInvZoom == s.fYInvZoom &&
223 this->fXInvInset == s.fXInvInset &&
224 this->fYInvInset == s.fYInvInset);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000225}
226
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000227void GrMagnifierEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
228 this->updateConstantColorComponentsForModulation(color, validFlags);
229}
230
bsalomon@google.com03245702012-08-13 14:31:30 +0000231#endif
232
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000233////////////////////////////////////////////////////////////////////////////////
234SkMagnifierImageFilter::SkMagnifierImageFilter(SkFlattenableReadBuffer& buffer)
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +0000235 : INHERITED(1, buffer) {
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000236 float x = buffer.readScalar();
237 float y = buffer.readScalar();
238 float width = buffer.readScalar();
239 float height = buffer.readScalar();
240 fSrcRect = SkRect::MakeXYWH(x, y, width, height);
241 fInset = buffer.readScalar();
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000242
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000243 buffer.validate(SkScalarIsFinite(fInset) && SkIsValidRect(fSrcRect) &&
244 // Negative numbers in src rect are not supported
245 (fSrcRect.fLeft >= 0) && (fSrcRect.fTop >= 0));
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000246}
247
senorblanco@chromium.org9f25de72012-10-10 20:36:13 +0000248// FIXME: implement single-input semantics
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000249SkMagnifierImageFilter::SkMagnifierImageFilter(SkRect srcRect, SkScalar inset)
senorblanco@chromium.org9f25de72012-10-10 20:36:13 +0000250 : INHERITED(0), fSrcRect(srcRect), fInset(inset) {
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000251 SkASSERT(srcRect.x() >= 0 && srcRect.y() >= 0 && inset >= 0);
252}
253
bsalomon@google.com03245702012-08-13 14:31:30 +0000254#if SK_SUPPORT_GPU
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000255bool SkMagnifierImageFilter::asNewEffect(GrEffectRef** effect, GrTexture* texture, const SkMatrix&, const SkIRect&) const {
bsalomon@google.com021fc732012-10-25 12:47:42 +0000256 if (effect) {
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +0000257 SkScalar yOffset = (texture->origin() == kTopLeft_GrSurfaceOrigin) ? fSrcRect.y() :
258 (texture->height() - (fSrcRect.y() + fSrcRect.height()));
259 SkScalar invInset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1;
senorblanco@chromium.orgd043cce2013-04-08 19:43:22 +0000260 *effect = GrMagnifierEffect::Create(texture,
261 fSrcRect.x() / texture->width(),
commit-bot@chromium.orgf642f8c2013-10-21 15:59:26 +0000262 yOffset / texture->height(),
263 fSrcRect.width() / texture->width(),
264 fSrcRect.height() / texture->height(),
265 texture->width() * invInset,
266 texture->height() * invInset);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000267 }
268 return true;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000269}
senorblanco@chromium.orgd043cce2013-04-08 19:43:22 +0000270#endif
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000271
272void SkMagnifierImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const {
273 this->INHERITED::flatten(buffer);
274 buffer.writeScalar(fSrcRect.x());
275 buffer.writeScalar(fSrcRect.y());
276 buffer.writeScalar(fSrcRect.width());
277 buffer.writeScalar(fSrcRect.height());
278 buffer.writeScalar(fInset);
279}
280
281bool SkMagnifierImageFilter::onFilterImage(Proxy*, const SkBitmap& src,
282 const SkMatrix&, SkBitmap* dst,
283 SkIPoint* offset) {
284 SkASSERT(src.config() == SkBitmap::kARGB_8888_Config);
285 SkASSERT(fSrcRect.width() < src.width());
286 SkASSERT(fSrcRect.height() < src.height());
287
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000288 if ((src.config() != SkBitmap::kARGB_8888_Config) ||
289 (fSrcRect.width() >= src.width()) ||
290 (fSrcRect.height() >= src.height())) {
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000291 return false;
292 }
293
294 SkAutoLockPixels alp(src);
295 SkASSERT(src.getPixels());
296 if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) {
297 return false;
298 }
299
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000300 dst->setConfig(src.config(), src.width(), src.height());
301 dst->allocPixels();
302 if (!dst->getPixels()) {
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
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000311 SkColor* sptr = src.getAddr32(0, 0);
312 SkColor* dptr = dst->getAddr32(0, 0);
313 int width = src.width(), height = src.height();
314 for (int y = 0; y < height; ++y) {
315 for (int x = 0; x < width; ++x) {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000316 SkScalar x_dist = SkMin32(x, width - x - 1) * inv_inset;
317 SkScalar y_dist = SkMin32(y, height - y - 1) * inv_inset;
318 SkScalar weight = 0;
319
320 static const SkScalar kScalar2 = SkScalar(2);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000321
322 // To create a smooth curve at the corners, we need to work on
323 // a square twice the size of the inset.
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000324 if (x_dist < kScalar2 && y_dist < kScalar2) {
325 x_dist = kScalar2 - x_dist;
326 y_dist = kScalar2 - y_dist;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000327
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000328 SkScalar dist = SkScalarSqrt(SkScalarSquare(x_dist) +
329 SkScalarSquare(y_dist));
330 dist = SkMaxScalar(kScalar2 - dist, 0);
331 weight = SkMinScalar(SkScalarSquare(dist), SK_Scalar1);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000332 } else {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000333 SkScalar sqDist = SkMinScalar(SkScalarSquare(x_dist),
334 SkScalarSquare(y_dist));
335 weight = SkMinScalar(sqDist, SK_Scalar1);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000336 }
337
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000338 SkScalar x_interp = SkScalarMul(weight, (fSrcRect.x() + x * inv_x_zoom)) +
339 (SK_Scalar1 - weight) * x;
340 SkScalar y_interp = SkScalarMul(weight, (fSrcRect.y() + y * inv_y_zoom)) +
341 (SK_Scalar1 - weight) * y;
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000342
commit-bot@chromium.orgcd3b15c2013-12-04 17:06:49 +0000343 int x_val = SkPin32(SkScalarFloorToInt(x_interp), 0, width - 1);
344 int y_val = SkPin32(SkScalarFloorToInt(y_interp), 0, height - 1);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000345
346 *dptr = sptr[y_val * width + x_val];
347 dptr++;
348 }
349 }
350 return true;
351}