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/core/SkBitmap.h"
9 #include "include/effects/SkImageFilters.h"
10 #include "include/private/SkColorData.h"
11 #include "include/private/SkTPin.h"
12 #include "src/core/SkImageFilter_Base.h"
13 #include "src/core/SkReadBuffer.h"
14 #include "src/core/SkSpecialImage.h"
15 #include "src/core/SkValidationUtils.h"
16 #include "src/core/SkWriteBuffer.h"
17
18 ////////////////////////////////////////////////////////////////////////////////
19 #if SK_SUPPORT_GPU
20 #include "src/core/SkRuntimeEffectPriv.h"
21 #include "src/gpu/GrColorSpaceXform.h"
22 #include "src/gpu/effects/GrSkSLFP.h"
23 #include "src/gpu/effects/GrTextureEffect.h"
24 #endif
25
26 namespace {
27
28 class SkMagnifierImageFilter final : public SkImageFilter_Base {
29 public:
SkMagnifierImageFilter(const SkRect & srcRect,SkScalar inset,sk_sp<SkImageFilter> input,const SkRect * cropRect)30 SkMagnifierImageFilter(const SkRect& srcRect, SkScalar inset, sk_sp<SkImageFilter> input,
31 const SkRect* cropRect)
32 : INHERITED(&input, 1, cropRect)
33 , fSrcRect(srcRect)
34 , fInset(inset) {
35 SkASSERT(srcRect.left() >= 0 && srcRect.top() >= 0 && inset >= 0);
36 }
37
38 protected:
39 void flatten(SkWriteBuffer&) const override;
40
41 sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override;
42
43 private:
44 friend void ::SkRegisterMagnifierImageFilterFlattenable();
45 SK_FLATTENABLE_HOOKS(SkMagnifierImageFilter)
46
47 SkRect fSrcRect;
48 SkScalar fInset;
49
50 using INHERITED = SkImageFilter_Base;
51 };
52
53 } // end namespace
54
Magnifier(const SkRect & srcRect,SkScalar inset,sk_sp<SkImageFilter> input,const CropRect & cropRect)55 sk_sp<SkImageFilter> SkImageFilters::Magnifier(
56 const SkRect& srcRect, SkScalar inset, sk_sp<SkImageFilter> input,
57 const CropRect& cropRect) {
58 if (!SkScalarIsFinite(inset) || !SkIsValidRect(srcRect)) {
59 return nullptr;
60 }
61 if (inset < 0) {
62 return nullptr;
63 }
64 // Negative numbers in src rect are not supported
65 if (srcRect.fLeft < 0 || srcRect.fTop < 0) {
66 return nullptr;
67 }
68 return sk_sp<SkImageFilter>(new SkMagnifierImageFilter(srcRect, inset, std::move(input),
69 cropRect));
70 }
71
SkRegisterMagnifierImageFilterFlattenable()72 void SkRegisterMagnifierImageFilterFlattenable() {
73 SK_REGISTER_FLATTENABLE(SkMagnifierImageFilter);
74 // TODO (michaelludwig) - Remove after grace period for SKPs to stop using old name
75 SkFlattenable::Register("SkMagnifierImageFilterImpl", SkMagnifierImageFilter::CreateProc);
76 }
77
CreateProc(SkReadBuffer & buffer)78 sk_sp<SkFlattenable> SkMagnifierImageFilter::CreateProc(SkReadBuffer& buffer) {
79 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
80 SkRect src;
81 buffer.readRect(&src);
82 return SkImageFilters::Magnifier(src, buffer.readScalar(), common.getInput(0),
83 common.cropRect());
84 }
85
flatten(SkWriteBuffer & buffer) const86 void SkMagnifierImageFilter::flatten(SkWriteBuffer& buffer) const {
87 this->INHERITED::flatten(buffer);
88 buffer.writeRect(fSrcRect);
89 buffer.writeScalar(fInset);
90 }
91
92 ////////////////////////////////////////////////////////////////////////////////
93
94 #if SK_SUPPORT_GPU
make_magnifier_fp(std::unique_ptr<GrFragmentProcessor> input,SkIRect bounds,SkRect srcRect,float xInvZoom,float yInvZoom,float xInvInset,float yInvInset)95 static std::unique_ptr<GrFragmentProcessor> make_magnifier_fp(
96 std::unique_ptr<GrFragmentProcessor> input,
97 SkIRect bounds,
98 SkRect srcRect,
99 float xInvZoom,
100 float yInvZoom,
101 float xInvInset,
102 float yInvInset) {
103 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
104 uniform shader src;
105 uniform float4 boundsUniform;
106 uniform float xInvZoom;
107 uniform float yInvZoom;
108 uniform float xInvInset;
109 uniform float yInvInset;
110 uniform half2 offset;
111
112 half4 main(float2 coord) {
113 float2 zoom_coord = offset + coord * float2(xInvZoom, yInvZoom);
114 float2 delta = (coord - boundsUniform.xy) * boundsUniform.zw;
115 delta = min(delta, float2(1.0) - delta);
116 delta *= float2(xInvInset, yInvInset);
117
118 float weight = 0.0;
119 if (delta.s < 2.0 && delta.t < 2.0) {
120 delta = float2(2.0) - delta;
121 float dist = length(delta);
122 dist = max(2.0 - dist, 0.0);
123 weight = min(dist * dist, 1.0);
124 } else {
125 float2 delta_squared = delta * delta;
126 weight = min(min(delta_squared.x, delta_squared.y), 1.0);
127 }
128
129 return src.eval(mix(coord, zoom_coord, weight));
130 }
131 )");
132
133 SkV4 boundsUniform = {static_cast<float>(bounds.x()),
134 static_cast<float>(bounds.y()),
135 1.f / bounds.width(),
136 1.f / bounds.height()};
137
138 return GrSkSLFP::Make(effect, "magnifier_fp", /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone,
139 "src", std::move(input),
140 "boundsUniform", boundsUniform,
141 "xInvZoom", xInvZoom,
142 "yInvZoom", yInvZoom,
143 "xInvInset", xInvInset,
144 "yInvInset", yInvInset,
145 "offset", SkV2{srcRect.x(), srcRect.y()});
146 }
147 #endif
148
onFilterImage(const Context & ctx,SkIPoint * offset) const149 sk_sp<SkSpecialImage> SkMagnifierImageFilter::onFilterImage(const Context& ctx,
150 SkIPoint* offset) const {
151 SkIPoint inputOffset = SkIPoint::Make(0, 0);
152 sk_sp<SkSpecialImage> input(this->filterInput(0, ctx, &inputOffset));
153 if (!input) {
154 return nullptr;
155 }
156
157 const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(),
158 input->width(), input->height());
159
160 SkIRect bounds;
161 if (!this->applyCropRect(ctx, inputBounds, &bounds)) {
162 return nullptr;
163 }
164
165 SkScalar invInset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1;
166
167 SkScalar invXZoom = fSrcRect.width() / bounds.width();
168 SkScalar invYZoom = fSrcRect.height() / bounds.height();
169
170
171 #if SK_SUPPORT_GPU
172 if (ctx.gpuBacked()) {
173 auto context = ctx.getContext();
174
175 GrSurfaceProxyView inputView = input->view(context);
176 SkASSERT(inputView.asTextureProxy());
177
178 const auto isProtected = inputView.proxy()->isProtected();
179
180 offset->fX = bounds.left();
181 offset->fY = bounds.top();
182 bounds.offset(-inputOffset);
183
184 // Map bounds and srcRect into the proxy space. Due to the zoom effect,
185 // it's not just an offset for fSrcRect.
186 bounds.offset(input->subset().x(), input->subset().y());
187 SkRect srcRect = fSrcRect.makeOffset((1.f - invXZoom) * input->subset().x(),
188 (1.f - invYZoom) * input->subset().y());
189 auto inputFP = GrTextureEffect::Make(std::move(inputView), kPremul_SkAlphaType);
190
191 auto fp = make_magnifier_fp(std::move(inputFP),
192 bounds,
193 srcRect,
194 invXZoom,
195 invYZoom,
196 bounds.width() * invInset,
197 bounds.height() * invInset);
198
199 fp = GrColorSpaceXformEffect::Make(std::move(fp),
200 input->getColorSpace(), input->alphaType(),
201 ctx.colorSpace(), kPremul_SkAlphaType);
202 if (!fp) {
203 return nullptr;
204 }
205
206 return DrawWithFP(context, std::move(fp), bounds, ctx.colorType(), ctx.colorSpace(),
207 ctx.surfaceProps(), isProtected);
208 }
209 #endif
210
211 SkBitmap inputBM;
212
213 if (!input->getROPixels(&inputBM)) {
214 return nullptr;
215 }
216
217 if ((inputBM.colorType() != kN32_SkColorType) ||
218 (fSrcRect.width() >= inputBM.width()) || (fSrcRect.height() >= inputBM.height())) {
219 return nullptr;
220 }
221
222 SkASSERT(inputBM.getPixels());
223 if (!inputBM.getPixels() || inputBM.width() <= 0 || inputBM.height() <= 0) {
224 return nullptr;
225 }
226
227 const SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.width(), bounds.height());
228
229 SkBitmap dst;
230 if (!dst.tryAllocPixels(info)) {
231 return nullptr;
232 }
233
234 SkColor* dptr = dst.getAddr32(0, 0);
235 int dstWidth = dst.width(), dstHeight = dst.height();
236 for (int y = 0; y < dstHeight; ++y) {
237 for (int x = 0; x < dstWidth; ++x) {
238 SkScalar x_dist = std::min(x, dstWidth - x - 1) * invInset;
239 SkScalar y_dist = std::min(y, dstHeight - y - 1) * invInset;
240 SkScalar weight = 0;
241
242 static const SkScalar kScalar2 = SkScalar(2);
243
244 // To create a smooth curve at the corners, we need to work on
245 // a square twice the size of the inset.
246 if (x_dist < kScalar2 && y_dist < kScalar2) {
247 x_dist = kScalar2 - x_dist;
248 y_dist = kScalar2 - y_dist;
249
250 SkScalar dist = SkScalarSqrt(SkScalarSquare(x_dist) +
251 SkScalarSquare(y_dist));
252 dist = std::max(kScalar2 - dist, 0.0f);
253 // SkTPin rather than std::max to handle potential NaN
254 weight = SkTPin(SkScalarSquare(dist), 0.0f, SK_Scalar1);
255 } else {
256 SkScalar sqDist = std::min(SkScalarSquare(x_dist),
257 SkScalarSquare(y_dist));
258 // SkTPin rather than std::max to handle potential NaN
259 weight = SkTPin(sqDist, 0.0f, SK_Scalar1);
260 }
261
262 SkScalar x_interp = weight * (fSrcRect.x() + x * invXZoom) + (1 - weight) * x;
263 SkScalar y_interp = weight * (fSrcRect.y() + y * invYZoom) + (1 - weight) * y;
264
265 int x_val = SkTPin(bounds.x() + SkScalarFloorToInt(x_interp), 0, inputBM.width() - 1);
266 int y_val = SkTPin(bounds.y() + SkScalarFloorToInt(y_interp), 0, inputBM.height() - 1);
267
268 *dptr = *inputBM.getAddr32(x_val, y_val);
269 dptr++;
270 }
271 }
272
273 offset->fX = bounds.left();
274 offset->fY = bounds.top();
275 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()),
276 dst, ctx.surfaceProps());
277 }
278