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 "include/effects/SkMagnifierImageFilter.h"
9 
10 #include "include/core/SkBitmap.h"
11 #include "include/private/SkColorData.h"
12 #include "include/private/SkTPin.h"
13 #include "src/core/SkImageFilter_Base.h"
14 #include "src/core/SkReadBuffer.h"
15 #include "src/core/SkSpecialImage.h"
16 #include "src/core/SkValidationUtils.h"
17 #include "src/core/SkWriteBuffer.h"
18 
19 ////////////////////////////////////////////////////////////////////////////////
20 #if SK_SUPPORT_GPU
21 #include "src/gpu/GrColorSpaceXform.h"
22 #include "src/gpu/effects/GrTextureEffect.h"
23 #include "src/gpu/effects/generated/GrMagnifierEffect.h"
24 #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
25 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
26 #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
27 #include "src/gpu/glsl/GrGLSLUniformHandler.h"
28 #endif
29 
30 namespace {
31 
32 class SkMagnifierImageFilterImpl final : public SkImageFilter_Base {
33 public:
SkMagnifierImageFilterImpl(const SkRect & srcRect,SkScalar inset,sk_sp<SkImageFilter> input,const CropRect * cropRect)34     SkMagnifierImageFilterImpl(const SkRect& srcRect, SkScalar inset, sk_sp<SkImageFilter> input,
35                                const CropRect* cropRect)
36             : INHERITED(&input, 1, cropRect)
37             , fSrcRect(srcRect)
38             , fInset(inset) {
39         SkASSERT(srcRect.left() >= 0 && srcRect.top() >= 0 && inset >= 0);
40     }
41 
42 protected:
43     void flatten(SkWriteBuffer&) const override;
44 
45     sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override;
46 
47 private:
48     friend void SkMagnifierImageFilter::RegisterFlattenables();
49     SK_FLATTENABLE_HOOKS(SkMagnifierImageFilterImpl)
50 
51     SkRect   fSrcRect;
52     SkScalar fInset;
53 
54     using INHERITED = SkImageFilter_Base;
55 };
56 
57 } // end namespace
58 
Make(const SkRect & srcRect,SkScalar inset,sk_sp<SkImageFilter> input,const SkImageFilter::CropRect * cropRect)59 sk_sp<SkImageFilter> SkMagnifierImageFilter::Make(const SkRect& srcRect, SkScalar inset,
60                                                   sk_sp<SkImageFilter> input,
61                                                   const SkImageFilter::CropRect* cropRect) {
62     if (!SkScalarIsFinite(inset) || !SkIsValidRect(srcRect)) {
63         return nullptr;
64     }
65     if (inset < 0) {
66         return nullptr;
67     }
68     // Negative numbers in src rect are not supported
69     if (srcRect.fLeft < 0 || srcRect.fTop < 0) {
70         return nullptr;
71     }
72     return sk_sp<SkImageFilter>(new SkMagnifierImageFilterImpl(srcRect, inset, std::move(input),
73                                                                cropRect));
74 }
75 
RegisterFlattenables()76 void SkMagnifierImageFilter::RegisterFlattenables() {
77     SK_REGISTER_FLATTENABLE(SkMagnifierImageFilterImpl);
78     // TODO (michaelludwig) - Remove after grace period for SKPs to stop using old name
79     SkFlattenable::Register("SkMagnifierImageFilter", SkMagnifierImageFilterImpl::CreateProc);
80 }
81 
82 ////////////////////////////////////////////////////////////////////////////////
83 
CreateProc(SkReadBuffer & buffer)84 sk_sp<SkFlattenable> SkMagnifierImageFilterImpl::CreateProc(SkReadBuffer& buffer) {
85     SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
86     SkRect src;
87     buffer.readRect(&src);
88     return SkMagnifierImageFilter::Make(src, buffer.readScalar(), common.getInput(0),
89                                         &common.cropRect());
90 }
91 
flatten(SkWriteBuffer & buffer) const92 void SkMagnifierImageFilterImpl::flatten(SkWriteBuffer& buffer) const {
93     this->INHERITED::flatten(buffer);
94     buffer.writeRect(fSrcRect);
95     buffer.writeScalar(fInset);
96 }
97 
onFilterImage(const Context & ctx,SkIPoint * offset) const98 sk_sp<SkSpecialImage> SkMagnifierImageFilterImpl::onFilterImage(const Context& ctx,
99                                                                 SkIPoint* offset) const {
100     SkIPoint inputOffset = SkIPoint::Make(0, 0);
101     sk_sp<SkSpecialImage> input(this->filterInput(0, ctx, &inputOffset));
102     if (!input) {
103         return nullptr;
104     }
105 
106     const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(),
107                                                   input->width(), input->height());
108 
109     SkIRect bounds;
110     if (!this->applyCropRect(ctx, inputBounds, &bounds)) {
111         return nullptr;
112     }
113 
114     SkScalar invInset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1;
115 
116     SkScalar invXZoom = fSrcRect.width() / bounds.width();
117     SkScalar invYZoom = fSrcRect.height() / bounds.height();
118 
119 
120 #if SK_SUPPORT_GPU
121     if (ctx.gpuBacked()) {
122         auto context = ctx.getContext();
123 
124         GrSurfaceProxyView inputView = input->view(context);
125         SkASSERT(inputView.asTextureProxy());
126 
127         const auto isProtected = inputView.proxy()->isProtected();
128 
129         offset->fX = bounds.left();
130         offset->fY = bounds.top();
131         bounds.offset(-inputOffset);
132 
133         // Map bounds and srcRect into the proxy space. Due to the zoom effect,
134         // it's not just an offset for fSrcRect.
135         bounds.offset(input->subset().x(), input->subset().y());
136         SkRect srcRect = fSrcRect.makeOffset((1.f - invXZoom) * input->subset().x(),
137                                              (1.f - invYZoom) * input->subset().y());
138         auto inputFP = GrTextureEffect::Make(std::move(inputView), kPremul_SkAlphaType);
139 
140         auto fp = GrMagnifierEffect::Make(std::move(inputFP),
141                                           bounds,
142                                           srcRect,
143                                           invXZoom,
144                                           invYZoom,
145                                           bounds.width() * invInset,
146                                           bounds.height() * invInset);
147         fp = GrColorSpaceXformEffect::Make(std::move(fp),
148                                            input->getColorSpace(), input->alphaType(),
149                                            ctx.colorSpace(), kPremul_SkAlphaType);
150         if (!fp) {
151             return nullptr;
152         }
153 
154         return DrawWithFP(context, std::move(fp), bounds, ctx.colorType(), ctx.colorSpace(),
155                           isProtected);
156     }
157 #endif
158 
159     SkBitmap inputBM;
160 
161     if (!input->getROPixels(&inputBM)) {
162         return nullptr;
163     }
164 
165     if ((inputBM.colorType() != kN32_SkColorType) ||
166         (fSrcRect.width() >= inputBM.width()) || (fSrcRect.height() >= inputBM.height())) {
167         return nullptr;
168     }
169 
170     SkASSERT(inputBM.getPixels());
171     if (!inputBM.getPixels() || inputBM.width() <= 0 || inputBM.height() <= 0) {
172         return nullptr;
173     }
174 
175     const SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.width(), bounds.height());
176 
177     SkBitmap dst;
178     if (!dst.tryAllocPixels(info)) {
179         return nullptr;
180     }
181 
182     SkColor* dptr = dst.getAddr32(0, 0);
183     int dstWidth = dst.width(), dstHeight = dst.height();
184     for (int y = 0; y < dstHeight; ++y) {
185         for (int x = 0; x < dstWidth; ++x) {
186             SkScalar x_dist = std::min(x, dstWidth - x - 1) * invInset;
187             SkScalar y_dist = std::min(y, dstHeight - y - 1) * invInset;
188             SkScalar weight = 0;
189 
190             static const SkScalar kScalar2 = SkScalar(2);
191 
192             // To create a smooth curve at the corners, we need to work on
193             // a square twice the size of the inset.
194             if (x_dist < kScalar2 && y_dist < kScalar2) {
195                 x_dist = kScalar2 - x_dist;
196                 y_dist = kScalar2 - y_dist;
197 
198                 SkScalar dist = SkScalarSqrt(SkScalarSquare(x_dist) +
199                                              SkScalarSquare(y_dist));
200                 dist = std::max(kScalar2 - dist, 0.0f);
201                 // SkTPin rather than std::max to handle potential NaN
202                 weight = SkTPin(SkScalarSquare(dist), 0.0f, SK_Scalar1);
203             } else {
204                 SkScalar sqDist = std::min(SkScalarSquare(x_dist),
205                                            SkScalarSquare(y_dist));
206                 // SkTPin rather than std::max to handle potential NaN
207                 weight = SkTPin(sqDist, 0.0f, SK_Scalar1);
208             }
209 
210             SkScalar x_interp = weight * (fSrcRect.x() + x * invXZoom) + (1 - weight) * x;
211             SkScalar y_interp = weight * (fSrcRect.y() + y * invYZoom) + (1 - weight) * y;
212 
213             int x_val = SkTPin(bounds.x() + SkScalarFloorToInt(x_interp), 0, inputBM.width() - 1);
214             int y_val = SkTPin(bounds.y() + SkScalarFloorToInt(y_interp), 0, inputBM.height() - 1);
215 
216             *dptr = *inputBM.getAddr32(x_val, y_val);
217             dptr++;
218         }
219     }
220 
221     offset->fX = bounds.left();
222     offset->fY = bounds.top();
223     return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()),
224                                           dst);
225 }
226