• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "SkMagnifierImageFilter.h"
9 
10 #include "SkBitmap.h"
11 #include "SkColorPriv.h"
12 #include "SkColorSpaceXformer.h"
13 #include "SkReadBuffer.h"
14 #include "SkSpecialImage.h"
15 #include "SkWriteBuffer.h"
16 #include "SkValidationUtils.h"
17 
18 ////////////////////////////////////////////////////////////////////////////////
19 #if SK_SUPPORT_GPU
20 #include "GrContext.h"
21 #include "GrTexture.h"
22 #include "effects/GrProxyMove.h"
23 #include "effects/GrSingleTextureEffect.h"
24 #include "glsl/GrGLSLColorSpaceXformHelper.h"
25 #include "glsl/GrGLSLFragmentProcessor.h"
26 #include "glsl/GrGLSLFragmentShaderBuilder.h"
27 #include "glsl/GrGLSLProgramDataManager.h"
28 #include "glsl/GrGLSLUniformHandler.h"
29 #include "../private/GrGLSL.h"
30 #endif
31 
Make(const SkRect & srcRect,SkScalar inset,sk_sp<SkImageFilter> input,const CropRect * cropRect)32 sk_sp<SkImageFilter> SkMagnifierImageFilter::Make(const SkRect& srcRect, SkScalar inset,
33                                                   sk_sp<SkImageFilter> input,
34                                                   const CropRect* cropRect) {
35     if (!SkScalarIsFinite(inset) || !SkIsValidRect(srcRect)) {
36         return nullptr;
37     }
38     if (inset < 0) {
39         return nullptr;
40     }
41     // Negative numbers in src rect are not supported
42     if (srcRect.fLeft < 0 || srcRect.fTop < 0) {
43         return nullptr;
44     }
45     return sk_sp<SkImageFilter>(new SkMagnifierImageFilter(srcRect, inset,
46                                                            std::move(input),
47                                                            cropRect));
48 }
49 
50 #if SK_SUPPORT_GPU
51 class GrMagnifierEffect : public GrSingleTextureEffect {
52 public:
Make(sk_sp<GrTextureProxy> proxy,sk_sp<GrColorSpaceXform> colorSpaceXform,const SkIRect & bounds,const SkRect & srcRect,float xInvZoom,float yInvZoom,float xInvInset,float yInvInset)53     static sk_sp<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
54                                            sk_sp<GrColorSpaceXform> colorSpaceXform,
55                                            const SkIRect& bounds,
56                                            const SkRect& srcRect,
57                                            float xInvZoom,
58                                            float yInvZoom,
59                                            float xInvInset,
60                                            float yInvInset) {
61         return sk_sp<GrFragmentProcessor>(new GrMagnifierEffect(std::move(proxy),
62                                                                 std::move(colorSpaceXform),
63                                                                 bounds, srcRect,
64                                                                 xInvZoom, yInvZoom,
65                                                                 xInvInset, yInvInset));
66     }
67 
~GrMagnifierEffect()68     ~GrMagnifierEffect() override {}
69 
name() const70     const char* name() const override { return "Magnifier"; }
71 
bounds() const72     const SkIRect& bounds() const { return fBounds; }    // Bounds of source image.
srcRect() const73     const SkRect& srcRect() const { return fSrcRect; }
74 
75     // Scale to apply to zoomed pixels (srcRect size / bounds size).
xInvZoom() const76     float xInvZoom() const { return fXInvZoom; }
yInvZoom() const77     float yInvZoom() const { return fYInvZoom; }
78 
79     // 1/radius over which to transition from unzoomed to zoomed pixels (bounds size / inset).
xInvInset() const80     float xInvInset() const { return fXInvInset; }
yInvInset() const81     float yInvInset() const { return fYInvInset; }
82 
83 private:
GrMagnifierEffect(sk_sp<GrTextureProxy> proxy,sk_sp<GrColorSpaceXform> colorSpaceXform,const SkIRect & bounds,const SkRect & srcRect,float xInvZoom,float yInvZoom,float xInvInset,float yInvInset)84     GrMagnifierEffect(sk_sp<GrTextureProxy> proxy,
85                       sk_sp<GrColorSpaceXform> colorSpaceXform,
86                       const SkIRect& bounds,
87                       const SkRect& srcRect,
88                       float xInvZoom,
89                       float yInvZoom,
90                       float xInvInset,
91                       float yInvInset)
92             : INHERITED{ModulationFlags(proxy->config()),
93                         GR_PROXY_MOVE(proxy),
94                         std::move(colorSpaceXform),
95                         SkMatrix::I()} // TODO: no GrSamplerParams::kBilerp_FilterMode?
96             , fBounds(bounds)
97             , fSrcRect(srcRect)
98             , fXInvZoom(xInvZoom)
99             , fYInvZoom(yInvZoom)
100             , fXInvInset(xInvInset)
101             , fYInvInset(yInvInset) {
102         this->initClassID<GrMagnifierEffect>();
103     }
104 
105     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
106 
107     void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
108 
109     bool onIsEqual(const GrFragmentProcessor&) const override;
110 
111     GR_DECLARE_FRAGMENT_PROCESSOR_TEST
112 
113     SkIRect fBounds;
114     SkRect  fSrcRect;
115     float fXInvZoom;
116     float fYInvZoom;
117     float fXInvInset;
118     float fYInvInset;
119 
120     typedef GrSingleTextureEffect INHERITED;
121 };
122 
123 // For brevity
124 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
125 
126 class GrGLMagnifierEffect : public GrGLSLFragmentProcessor {
127 public:
128     void emitCode(EmitArgs&) override;
129 
GenKey(const GrProcessor & effect,const GrShaderCaps &,GrProcessorKeyBuilder * b)130     static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&,
131                               GrProcessorKeyBuilder* b) {
132         const GrMagnifierEffect& zoom = effect.cast<GrMagnifierEffect>();
133         b->add32(GrColorSpaceXform::XformKey(zoom.colorSpaceXform()));
134     }
135 
136 protected:
137     void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
138 
139 private:
140     UniformHandle       fOffsetVar;
141     UniformHandle       fInvZoomVar;
142     UniformHandle       fInvInsetVar;
143     UniformHandle       fBoundsVar;
144     GrGLSLColorSpaceXformHelper fColorSpaceHelper;
145 
146     typedef GrGLSLFragmentProcessor INHERITED;
147 };
148 
emitCode(EmitArgs & args)149 void GrGLMagnifierEffect::emitCode(EmitArgs& args) {
150     GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
151     fOffsetVar = uniformHandler->addUniform(kFragment_GrShaderFlag,
152                                             kVec2f_GrSLType, kDefault_GrSLPrecision,
153                                             "Offset");
154     fInvZoomVar = uniformHandler->addUniform(kFragment_GrShaderFlag,
155                                              kVec2f_GrSLType, kDefault_GrSLPrecision,
156                                              "InvZoom");
157     fInvInsetVar = uniformHandler->addUniform(kFragment_GrShaderFlag,
158                                               kVec2f_GrSLType, kDefault_GrSLPrecision,
159                                               "InvInset");
160     fBoundsVar = uniformHandler->addUniform(kFragment_GrShaderFlag,
161                                             kVec4f_GrSLType, kDefault_GrSLPrecision,
162                                             "Bounds");
163 
164     const GrMagnifierEffect& zoom = args.fFp.cast<GrMagnifierEffect>();
165     fColorSpaceHelper.emitCode(uniformHandler, zoom.colorSpaceXform());
166 
167     GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
168     SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
169     fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
170     fragBuilder->codeAppendf("\t\tvec2 zoom_coord = %s + %s * %s;\n",
171                              uniformHandler->getUniformCStr(fOffsetVar),
172                              coords2D.c_str(),
173                              uniformHandler->getUniformCStr(fInvZoomVar));
174     const char* bounds = uniformHandler->getUniformCStr(fBoundsVar);
175     fragBuilder->codeAppendf("\t\tvec2 delta = (coord - %s.xy) * %s.zw;\n", bounds, bounds);
176     fragBuilder->codeAppendf("\t\tdelta = min(delta, vec2(1.0, 1.0) - delta);\n");
177     fragBuilder->codeAppendf("\t\tdelta = delta * %s;\n",
178                              uniformHandler->getUniformCStr(fInvInsetVar));
179 
180     fragBuilder->codeAppend("\t\tfloat weight = 0.0;\n");
181     fragBuilder->codeAppend("\t\tif (delta.s < 2.0 && delta.t < 2.0) {\n");
182     fragBuilder->codeAppend("\t\t\tdelta = vec2(2.0, 2.0) - delta;\n");
183     fragBuilder->codeAppend("\t\t\tfloat dist = length(delta);\n");
184     fragBuilder->codeAppend("\t\t\tdist = max(2.0 - dist, 0.0);\n");
185     fragBuilder->codeAppend("\t\t\tweight = min(dist * dist, 1.0);\n");
186     fragBuilder->codeAppend("\t\t} else {\n");
187     fragBuilder->codeAppend("\t\t\tvec2 delta_squared = delta * delta;\n");
188     fragBuilder->codeAppend("\t\t\tweight = min(min(delta_squared.x, delta_squared.y), 1.0);\n");
189     fragBuilder->codeAppend("\t\t}\n");
190 
191     fragBuilder->codeAppend("\t\tvec2 mix_coord = mix(coord, zoom_coord, weight);\n");
192     fragBuilder->codeAppend("\t\tvec4 output_color = ");
193     fragBuilder->appendTextureLookup(args.fTexSamplers[0], "mix_coord", kVec2f_GrSLType,
194                                      &fColorSpaceHelper);
195     fragBuilder->codeAppend(";\n");
196 
197     fragBuilder->codeAppendf("\t\t%s = output_color;\n", args.fOutputColor);
198     fragBuilder->codeAppendf("%s *= %s;\n", args.fOutputColor, args.fInputColor);
199 }
200 
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & effect)201 void GrGLMagnifierEffect::onSetData(const GrGLSLProgramDataManager& pdman,
202                                     const GrFragmentProcessor& effect) {
203     const GrMagnifierEffect& zoom = effect.cast<GrMagnifierEffect>();
204 
205     GrTexture* tex = zoom.textureSampler(0).peekTexture();
206 
207     SkScalar invW = 1.0f / tex->width();
208     SkScalar invH = 1.0f / tex->height();
209 
210     {
211         SkScalar y = zoom.srcRect().y() * invH;
212         if (tex->origin() != kTopLeft_GrSurfaceOrigin) {
213             y = 1.0f - (zoom.srcRect().height() / zoom.bounds().height()) - y;
214         }
215 
216         pdman.set2f(fOffsetVar, zoom.srcRect().x() * invW, y);
217     }
218 
219     pdman.set2f(fInvZoomVar, zoom.xInvZoom(), zoom.yInvZoom());
220     pdman.set2f(fInvInsetVar, zoom.xInvInset(), zoom.yInvInset());
221 
222     {
223         SkScalar y = zoom.bounds().y() * invH;
224         if (tex->origin() != kTopLeft_GrSurfaceOrigin) {
225             y = 1.0f - zoom.bounds().height() * invH;
226         }
227 
228         pdman.set4f(fBoundsVar,
229                     zoom.bounds().x() * invW,
230                     y,
231                     SkIntToScalar(tex->width()) / zoom.bounds().width(),
232                     SkIntToScalar(tex->height()) / zoom.bounds().height());
233     }
234 
235     if (SkToBool(zoom.colorSpaceXform())) {
236         fColorSpaceHelper.setData(pdman, zoom.colorSpaceXform());
237     }
238 }
239 
240 /////////////////////////////////////////////////////////////////////
241 
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const242 void GrMagnifierEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
243                                               GrProcessorKeyBuilder* b) const {
244     GrGLMagnifierEffect::GenKey(*this, caps, b);
245 }
246 
onCreateGLSLInstance() const247 GrGLSLFragmentProcessor* GrMagnifierEffect::onCreateGLSLInstance() const {
248     return new GrGLMagnifierEffect;
249 }
250 
251 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMagnifierEffect);
252 
253 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)254 sk_sp<GrFragmentProcessor> GrMagnifierEffect::TestCreate(GrProcessorTestData* d) {
255     sk_sp<GrTextureProxy> proxy = d->textureProxy(0);
256     const int kMaxWidth = 200;
257     const int kMaxHeight = 200;
258     const SkScalar kMaxInset = 20.0f;
259     uint32_t width = d->fRandom->nextULessThan(kMaxWidth);
260     uint32_t height = d->fRandom->nextULessThan(kMaxHeight);
261     SkScalar inset = d->fRandom->nextRangeScalar(1.0f, kMaxInset);
262     sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom);
263 
264     SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight));
265     SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
266 
267     sk_sp<GrFragmentProcessor> effect(GrMagnifierEffect::Make(
268         std::move(proxy),
269         std::move(colorSpaceXform),
270         bounds,
271         srcRect,
272         srcRect.width() / bounds.width(),
273         srcRect.height() / bounds.height(),
274         bounds.width() / inset,
275         bounds.height() / inset));
276     SkASSERT(effect);
277     return effect;
278 }
279 #endif
280 
281 ///////////////////////////////////////////////////////////////////////////////
282 
onIsEqual(const GrFragmentProcessor & sBase) const283 bool GrMagnifierEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
284     const GrMagnifierEffect& s = sBase.cast<GrMagnifierEffect>();
285     return (this->fBounds == s.fBounds &&
286             this->fSrcRect == s.fSrcRect &&
287             this->fXInvZoom == s.fXInvZoom &&
288             this->fYInvZoom == s.fYInvZoom &&
289             this->fXInvInset == s.fXInvInset &&
290             this->fYInvInset == s.fYInvInset);
291 }
292 
293 #endif
294 
295 ////////////////////////////////////////////////////////////////////////////////
296 
SkMagnifierImageFilter(const SkRect & srcRect,SkScalar inset,sk_sp<SkImageFilter> input,const CropRect * cropRect)297 SkMagnifierImageFilter::SkMagnifierImageFilter(const SkRect& srcRect,
298                                                SkScalar inset,
299                                                sk_sp<SkImageFilter> input,
300                                                const CropRect* cropRect)
301     : INHERITED(&input, 1, cropRect)
302     , fSrcRect(srcRect)
303     , fInset(inset) {
304     SkASSERT(srcRect.left() >= 0 && srcRect.top() >= 0 && inset >= 0);
305 }
306 
CreateProc(SkReadBuffer & buffer)307 sk_sp<SkFlattenable> SkMagnifierImageFilter::CreateProc(SkReadBuffer& buffer) {
308     SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
309     SkRect src;
310     buffer.readRect(&src);
311     return Make(src, buffer.readScalar(), common.getInput(0), &common.cropRect());
312 }
313 
flatten(SkWriteBuffer & buffer) const314 void SkMagnifierImageFilter::flatten(SkWriteBuffer& buffer) const {
315     this->INHERITED::flatten(buffer);
316     buffer.writeRect(fSrcRect);
317     buffer.writeScalar(fInset);
318 }
319 
onFilterImage(SkSpecialImage * source,const Context & ctx,SkIPoint * offset) const320 sk_sp<SkSpecialImage> SkMagnifierImageFilter::onFilterImage(SkSpecialImage* source,
321                                                             const Context& ctx,
322                                                             SkIPoint* offset) const {
323     SkIPoint inputOffset = SkIPoint::Make(0, 0);
324     sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset));
325     if (!input) {
326         return nullptr;
327     }
328 
329     const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(),
330                                                   input->width(), input->height());
331 
332     SkIRect bounds;
333     if (!this->applyCropRect(ctx, inputBounds, &bounds)) {
334         return nullptr;
335     }
336 
337     SkScalar invInset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1;
338 
339     SkScalar invXZoom = fSrcRect.width() / bounds.width();
340     SkScalar invYZoom = fSrcRect.height() / bounds.height();
341 
342 
343 #if SK_SUPPORT_GPU
344     if (source->isTextureBacked()) {
345         GrContext* context = source->getContext();
346 
347         sk_sp<GrTextureProxy> inputProxy(input->asTextureProxyRef(context));
348         SkASSERT(inputProxy);
349 
350         offset->fX = bounds.left();
351         offset->fY = bounds.top();
352         bounds.offset(-inputOffset);
353 
354         SkColorSpace* dstColorSpace = ctx.outputProperties().colorSpace();
355         sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(input->getColorSpace(),
356                                                                            dstColorSpace);
357         sk_sp<GrFragmentProcessor> fp(GrMagnifierEffect::Make(
358                                                         std::move(inputProxy),
359                                                         std::move(colorSpaceXform),
360                                                         bounds,
361                                                         fSrcRect,
362                                                         invXZoom,
363                                                         invYZoom,
364                                                         bounds.width() * invInset,
365                                                         bounds.height() * invInset));
366         if (!fp) {
367             return nullptr;
368         }
369 
370         return DrawWithFP(context, std::move(fp), bounds, ctx.outputProperties());
371     }
372 #endif
373 
374     SkBitmap inputBM;
375 
376     if (!input->getROPixels(&inputBM)) {
377         return nullptr;
378     }
379 
380     if ((inputBM.colorType() != kN32_SkColorType) ||
381         (fSrcRect.width() >= inputBM.width()) || (fSrcRect.height() >= inputBM.height())) {
382         return nullptr;
383     }
384 
385     SkASSERT(inputBM.getPixels());
386     if (!inputBM.getPixels() || inputBM.width() <= 0 || inputBM.height() <= 0) {
387         return nullptr;
388     }
389 
390     const SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.width(), bounds.height());
391 
392     SkBitmap dst;
393     if (!dst.tryAllocPixels(info)) {
394         return nullptr;
395     }
396 
397     SkColor* dptr = dst.getAddr32(0, 0);
398     int dstWidth = dst.width(), dstHeight = dst.height();
399     for (int y = 0; y < dstHeight; ++y) {
400         for (int x = 0; x < dstWidth; ++x) {
401             SkScalar x_dist = SkMin32(x, dstWidth - x - 1) * invInset;
402             SkScalar y_dist = SkMin32(y, dstHeight - y - 1) * invInset;
403             SkScalar weight = 0;
404 
405             static const SkScalar kScalar2 = SkScalar(2);
406 
407             // To create a smooth curve at the corners, we need to work on
408             // a square twice the size of the inset.
409             if (x_dist < kScalar2 && y_dist < kScalar2) {
410                 x_dist = kScalar2 - x_dist;
411                 y_dist = kScalar2 - y_dist;
412 
413                 SkScalar dist = SkScalarSqrt(SkScalarSquare(x_dist) +
414                                              SkScalarSquare(y_dist));
415                 dist = SkMaxScalar(kScalar2 - dist, 0);
416                 weight = SkMinScalar(SkScalarSquare(dist), SK_Scalar1);
417             } else {
418                 SkScalar sqDist = SkMinScalar(SkScalarSquare(x_dist),
419                                               SkScalarSquare(y_dist));
420                 weight = SkMinScalar(sqDist, SK_Scalar1);
421             }
422 
423             SkScalar x_interp = weight * (fSrcRect.x() + x * invXZoom) + (1 - weight) * x;
424             SkScalar y_interp = weight * (fSrcRect.y() + y * invYZoom) + (1 - weight) * y;
425 
426             int x_val = SkTPin(bounds.x() + SkScalarFloorToInt(x_interp), 0, inputBM.width() - 1);
427             int y_val = SkTPin(bounds.y() + SkScalarFloorToInt(y_interp), 0, inputBM.height() - 1);
428 
429             *dptr = *inputBM.getAddr32(x_val, y_val);
430             dptr++;
431         }
432     }
433 
434     offset->fX = bounds.left();
435     offset->fY = bounds.top();
436     return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()),
437                                           dst);
438 }
439 
onMakeColorSpace(SkColorSpaceXformer * xformer) const440 sk_sp<SkImageFilter> SkMagnifierImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
441     SkASSERT(1 == this->countInputs());
442     auto input = xformer->apply(this->getInput(0));
443     if (input.get() != this->getInput(0)) {
444         return SkMagnifierImageFilter::Make(fSrcRect, fInset, std::move(input),
445                                             this->getCropRectIfSet());
446     }
447     return this->refMe();
448 }
449 
450 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const451 void SkMagnifierImageFilter::toString(SkString* str) const {
452     str->appendf("SkMagnifierImageFilter: (");
453     str->appendf("src: (%f,%f,%f,%f) ",
454                  fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBottom);
455     str->appendf("inset: %f", fInset);
456     str->append(")");
457 }
458 #endif
459