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 "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 "include/gpu/GrContext.h" |
21 | #include "src/gpu/GrColorSpaceXform.h" |
22 | #include "src/gpu/GrCoordTransform.h" |
23 | #include "src/gpu/GrTexture.h" |
24 | #include "src/gpu/effects/generated/GrMagnifierEffect.h" |
25 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
26 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
27 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
28 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
29 | #endif |
30 | |
31 | namespace { |
32 | |
33 | class SkMagnifierImageFilterImpl final : public SkImageFilter_Base { |
34 | public: |
35 | SkMagnifierImageFilterImpl(const SkRect& srcRect, SkScalar inset, sk_sp<SkImageFilter> input, |
36 | const CropRect* cropRect) |
37 | : INHERITED(&input, 1, cropRect) |
38 | , fSrcRect(srcRect) |
39 | , fInset(inset) { |
40 | SkASSERT(srcRect.left() >= 0 && srcRect.top() >= 0 && inset >= 0); |
41 | } |
42 | |
43 | protected: |
44 | void flatten(SkWriteBuffer&) const override; |
45 | |
46 | sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override; |
47 | |
48 | private: |
49 | friend void SkMagnifierImageFilter::RegisterFlattenables(); |
50 | SK_FLATTENABLE_HOOKS(SkMagnifierImageFilterImpl) |
51 | |
52 | SkRect fSrcRect; |
53 | SkScalar fInset; |
54 | |
55 | typedef SkImageFilter_Base INHERITED; |
56 | }; |
57 | |
58 | } // end namespace |
59 | |
60 | sk_sp<SkImageFilter> SkMagnifierImageFilter::Make(const SkRect& srcRect, SkScalar inset, |
61 | sk_sp<SkImageFilter> input, |
62 | const SkImageFilter::CropRect* cropRect) { |
63 | if (!SkScalarIsFinite(inset) || !SkIsValidRect(srcRect)) { |
64 | return nullptr; |
65 | } |
66 | if (inset < 0) { |
67 | return nullptr; |
68 | } |
69 | // Negative numbers in src rect are not supported |
70 | if (srcRect.fLeft < 0 || srcRect.fTop < 0) { |
71 | return nullptr; |
72 | } |
73 | return sk_sp<SkImageFilter>(new SkMagnifierImageFilterImpl(srcRect, inset, std::move(input), |
74 | cropRect)); |
75 | } |
76 | |
77 | void SkMagnifierImageFilter::RegisterFlattenables() { |
78 | SK_REGISTER_FLATTENABLE(SkMagnifierImageFilterImpl); |
79 | // TODO (michaelludwig) - Remove after grace period for SKPs to stop using old name |
80 | SkFlattenable::Register("SkMagnifierImageFilter" , SkMagnifierImageFilterImpl::CreateProc); |
81 | } |
82 | |
83 | //////////////////////////////////////////////////////////////////////////////// |
84 | |
85 | sk_sp<SkFlattenable> SkMagnifierImageFilterImpl::CreateProc(SkReadBuffer& buffer) { |
86 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
87 | SkRect src; |
88 | buffer.readRect(&src); |
89 | return SkMagnifierImageFilter::Make(src, buffer.readScalar(), common.getInput(0), |
90 | &common.cropRect()); |
91 | } |
92 | |
93 | void SkMagnifierImageFilterImpl::flatten(SkWriteBuffer& buffer) const { |
94 | this->INHERITED::flatten(buffer); |
95 | buffer.writeRect(fSrcRect); |
96 | buffer.writeScalar(fInset); |
97 | } |
98 | |
99 | sk_sp<SkSpecialImage> SkMagnifierImageFilterImpl::onFilterImage(const Context& ctx, |
100 | SkIPoint* offset) const { |
101 | SkIPoint inputOffset = SkIPoint::Make(0, 0); |
102 | sk_sp<SkSpecialImage> input(this->filterInput(0, ctx, &inputOffset)); |
103 | if (!input) { |
104 | return nullptr; |
105 | } |
106 | |
107 | const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(), |
108 | input->width(), input->height()); |
109 | |
110 | SkIRect bounds; |
111 | if (!this->applyCropRect(ctx, inputBounds, &bounds)) { |
112 | return nullptr; |
113 | } |
114 | |
115 | SkScalar invInset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1; |
116 | |
117 | SkScalar invXZoom = fSrcRect.width() / bounds.width(); |
118 | SkScalar invYZoom = fSrcRect.height() / bounds.height(); |
119 | |
120 | |
121 | #if SK_SUPPORT_GPU |
122 | if (ctx.gpuBacked()) { |
123 | auto context = ctx.getContext(); |
124 | |
125 | GrSurfaceProxyView inputView = input->view(context); |
126 | SkASSERT(inputView.asTextureProxy()); |
127 | |
128 | const auto isProtected = inputView.proxy()->isProtected(); |
129 | |
130 | offset->fX = bounds.left(); |
131 | offset->fY = bounds.top(); |
132 | bounds.offset(-inputOffset); |
133 | |
134 | // Map bounds and srcRect into the proxy space. Due to the zoom effect, |
135 | // it's not just an offset for fSrcRect. |
136 | bounds.offset(input->subset().x(), input->subset().y()); |
137 | SkRect srcRect = fSrcRect.makeOffset((1.f - invXZoom) * input->subset().x(), |
138 | (1.f - invYZoom) * input->subset().y()); |
139 | |
140 | // TODO: Update generated fp file Make functions to take views instead of proxies |
141 | auto fp = GrMagnifierEffect::Make(std::move(inputView), |
142 | bounds, |
143 | srcRect, |
144 | invXZoom, |
145 | invYZoom, |
146 | bounds.width() * invInset, |
147 | bounds.height() * invInset); |
148 | fp = GrColorSpaceXformEffect::Make(std::move(fp), input->getColorSpace(), |
149 | input->alphaType(), ctx.colorSpace()); |
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 | |