OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2011 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 "SkAutoPixmapStorage.h" | |
9 #include "SkColorPriv.h" | |
10 #include "SkGpuBlurUtils.h" | |
11 #include "SkOpts.h" | |
12 #include "SkReadBuffer.h" | |
13 #include "SkSpecialImage.h" | |
14 #include "SkWriteBuffer.h" | |
15 | |
16 #if SK_SUPPORT_GPU | |
17 #include "GrContext.h" | |
18 #include "SkGr.h" | |
19 #endif | |
20 | |
21 class SkBlurImageFilterImpl : public SkImageFilter { | |
22 public: | |
23 SkBlurImageFilterImpl(SkScalar sigmaX, | |
24 SkScalar sigmaY, | |
25 sk_sp<SkImageFilter> input, | |
26 const CropRect* cropRect); | |
27 | |
28 SkRect computeFastBounds(const SkRect&) const override; | |
29 | |
30 SK_TO_STRING_OVERRIDE() | |
31 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBlurImageFilterImpl) | |
32 | |
33 #ifdef SK_SUPPORT_LEGACY_IMAGEFILTER_PTR | |
34 static SkImageFilter* Create(SkScalar sigmaX, SkScalar sigmaY, SkImageFilter
* input = nullptr, | |
35 const CropRect* cropRect = nullptr) { | |
36 return SkImageFilter::MakeBlur(sigmaX, sigmaY, sk_ref_sp<SkImageFilter>(
input), | |
37 cropRect).release(); | |
38 } | |
39 #endif | |
40 | |
41 protected: | |
42 void flatten(SkWriteBuffer&) const override; | |
43 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&, | |
44 SkIPoint* offset) const override; | |
45 SkIRect onFilterNodeBounds(const SkIRect& src, const SkMatrix&, MapDirection
) const override; | |
46 | |
47 private: | |
48 SkSize fSigma; | |
49 typedef SkImageFilter INHERITED; | |
50 | |
51 friend class SkImageFilter; | |
52 }; | |
53 | |
54 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkImageFilter) | |
55 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurImageFilterImpl) | |
56 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | |
57 | |
58 /////////////////////////////////////////////////////////////////////////////// | |
59 | |
60 sk_sp<SkImageFilter> SkImageFilter::MakeBlur(SkScalar sigmaX, SkScalar sigmaY, | |
61 sk_sp<SkImageFilter> input, | |
62 const CropRect* cropRect) { | |
63 if (0 == sigmaX && 0 == sigmaY && !cropRect) { | |
64 return input; | |
65 } | |
66 return sk_sp<SkImageFilter>(new SkBlurImageFilterImpl(sigmaX, sigmaY, input,
cropRect)); | |
67 } | |
68 | |
69 // This rather arbitrary-looking value results in a maximum box blur kernel size | |
70 // of 1000 pixels on the raster path, which matches the WebKit and Firefox | |
71 // implementations. Since the GPU path does not compute a box blur, putting | |
72 // the limit on sigma ensures consistent behaviour between the GPU and | |
73 // raster paths. | |
74 #define MAX_SIGMA SkIntToScalar(532) | |
75 | |
76 static SkVector map_sigma(const SkSize& localSigma, const SkMatrix& ctm) { | |
77 SkVector sigma = SkVector::Make(localSigma.width(), localSigma.height()); | |
78 ctm.mapVectors(&sigma, 1); | |
79 sigma.fX = SkMinScalar(SkScalarAbs(sigma.fX), MAX_SIGMA); | |
80 sigma.fY = SkMinScalar(SkScalarAbs(sigma.fY), MAX_SIGMA); | |
81 return sigma; | |
82 } | |
83 | |
84 SkBlurImageFilterImpl::SkBlurImageFilterImpl(SkScalar sigmaX, | |
85 SkScalar sigmaY, | |
86 sk_sp<SkImageFilter> input, | |
87 const CropRect* cropRect) | |
88 : INHERITED(&input, 1, cropRect) | |
89 , fSigma(SkSize::Make(sigmaX, sigmaY)) { | |
90 } | |
91 | |
92 sk_sp<SkFlattenable> SkBlurImageFilterImpl::CreateProc(SkReadBuffer& buffer) { | |
93 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); | |
94 SkScalar sigmaX = buffer.readScalar(); | |
95 SkScalar sigmaY = buffer.readScalar(); | |
96 return SkImageFilter::MakeBlur(sigmaX, sigmaY, common.getInput(0), &common.c
ropRect()); | |
97 } | |
98 | |
99 void SkBlurImageFilterImpl::flatten(SkWriteBuffer& buffer) const { | |
100 this->INHERITED::flatten(buffer); | |
101 buffer.writeScalar(fSigma.fWidth); | |
102 buffer.writeScalar(fSigma.fHeight); | |
103 } | |
104 | |
105 static void get_box3_params(SkScalar s, int *kernelSize, int* kernelSize3, int *
lowOffset, | |
106 int *highOffset) { | |
107 float pi = SkScalarToFloat(SK_ScalarPI); | |
108 int d = static_cast<int>(floorf(SkScalarToFloat(s) * 3.0f * sqrtf(2.0f * pi)
/ 4.0f + 0.5f)); | |
109 *kernelSize = d; | |
110 if (d % 2 == 1) { | |
111 *lowOffset = *highOffset = (d - 1) / 2; | |
112 *kernelSize3 = d; | |
113 } else { | |
114 *highOffset = d / 2; | |
115 *lowOffset = *highOffset - 1; | |
116 *kernelSize3 = d + 1; | |
117 } | |
118 } | |
119 | |
120 sk_sp<SkSpecialImage> SkBlurImageFilterImpl::onFilterImage(SkSpecialImage* sourc
e, | |
121 const Context& ctx, | |
122 SkIPoint* offset) const { | |
123 SkIPoint inputOffset = SkIPoint::Make(0, 0); | |
124 | |
125 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset))
; | |
126 if (!input) { | |
127 return nullptr; | |
128 } | |
129 | |
130 SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.fX, inputOffset.fY, | |
131 input->width(), input->height()); | |
132 | |
133 SkIRect dstBounds; | |
134 if (!this->applyCropRect(this->mapContext(ctx), inputBounds, &dstBounds)) { | |
135 return nullptr; | |
136 } | |
137 if (!inputBounds.intersect(dstBounds)) { | |
138 return nullptr; | |
139 } | |
140 | |
141 const SkVector sigma = map_sigma(fSigma, ctx.ctm()); | |
142 | |
143 #if SK_SUPPORT_GPU | |
144 if (source->isTextureBacked()) { | |
145 GrContext* context = source->getContext(); | |
146 sk_sp<GrTexture> inputTexture(input->asTextureRef(context)); | |
147 SkASSERT(inputTexture); | |
148 | |
149 if (0 == sigma.x() && 0 == sigma.y()) { | |
150 offset->fX = inputBounds.x(); | |
151 offset->fY = inputBounds.y(); | |
152 return input->makeSubset(inputBounds.makeOffset(-inputOffset.x(), | |
153 -inputOffset.y())); | |
154 } | |
155 | |
156 offset->fX = dstBounds.fLeft; | |
157 offset->fY = dstBounds.fTop; | |
158 inputBounds.offset(-inputOffset); | |
159 dstBounds.offset(-inputOffset); | |
160 sk_sp<GrDrawContext> drawContext(SkGpuBlurUtils::GaussianBlur( | |
161 context, | |
162 inputTexture.get
(), | |
163 sk_ref_sp(source
->getColorSpace()), | |
164 dstBounds, | |
165 &inputBounds, | |
166 sigma.x(), | |
167 sigma.y())); | |
168 if (!drawContext) { | |
169 return nullptr; | |
170 } | |
171 | |
172 // TODO: Get the colorSpace from the drawContext (once it has one) | |
173 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(dstBounds.width(), ds
tBounds.height()), | |
174 kNeedNewImageUniqueID_SpecialImage, | |
175 drawContext->asTexture(), | |
176 sk_ref_sp(input->getColorSpace()), &s
ource->props()); | |
177 } | |
178 #endif | |
179 | |
180 int kernelSizeX, kernelSizeX3, lowOffsetX, highOffsetX; | |
181 int kernelSizeY, kernelSizeY3, lowOffsetY, highOffsetY; | |
182 get_box3_params(sigma.x(), &kernelSizeX, &kernelSizeX3, &lowOffsetX, &highOf
fsetX); | |
183 get_box3_params(sigma.y(), &kernelSizeY, &kernelSizeY3, &lowOffsetY, &highOf
fsetY); | |
184 | |
185 if (kernelSizeX < 0 || kernelSizeY < 0) { | |
186 return nullptr; | |
187 } | |
188 | |
189 if (kernelSizeX == 0 && kernelSizeY == 0) { | |
190 offset->fX = inputBounds.x(); | |
191 offset->fY = inputBounds.y(); | |
192 return input->makeSubset(inputBounds.makeOffset(-inputOffset.x(), | |
193 -inputOffset.y())); | |
194 } | |
195 | |
196 SkBitmap inputBM; | |
197 | |
198 if (!input->getROPixels(&inputBM)) { | |
199 return nullptr; | |
200 } | |
201 | |
202 if (inputBM.colorType() != kN32_SkColorType) { | |
203 return nullptr; | |
204 } | |
205 | |
206 SkImageInfo info = SkImageInfo::Make(dstBounds.width(), dstBounds.height(), | |
207 inputBM.colorType(), inputBM.alphaType(
)); | |
208 | |
209 SkBitmap tmp, dst; | |
210 if (!tmp.tryAllocPixels(info) || !dst.tryAllocPixels(info)) { | |
211 return nullptr; | |
212 } | |
213 | |
214 SkAutoLockPixels inputLock(inputBM), tmpLock(tmp), dstLock(dst); | |
215 | |
216 offset->fX = dstBounds.fLeft; | |
217 offset->fY = dstBounds.fTop; | |
218 SkPMColor* t = tmp.getAddr32(0, 0); | |
219 SkPMColor* d = dst.getAddr32(0, 0); | |
220 int w = dstBounds.width(), h = dstBounds.height(); | |
221 const SkPMColor* s = inputBM.getAddr32(inputBounds.x() - inputOffset.x(), | |
222 inputBounds.y() - inputOffset.y()); | |
223 inputBounds.offset(-dstBounds.x(), -dstBounds.y()); | |
224 dstBounds.offset(-dstBounds.x(), -dstBounds.y()); | |
225 SkIRect inputBoundsT = SkIRect::MakeLTRB(inputBounds.top(), inputBounds.left
(), | |
226 inputBounds.bottom(), inputBounds.r
ight()); | |
227 SkIRect dstBoundsT = SkIRect::MakeWH(dstBounds.height(), dstBounds.width()); | |
228 int sw = int(inputBM.rowBytes() >> 2); | |
229 | |
230 /** | |
231 * | |
232 * In order to make memory accesses cache-friendly, we reorder the passes to | |
233 * use contiguous memory reads wherever possible. | |
234 * | |
235 * For example, the 6 passes of the X-and-Y blur case are rewritten as | |
236 * follows. Instead of 3 passes in X and 3 passes in Y, we perform | |
237 * 2 passes in X, 1 pass in X transposed to Y on write, 2 passes in X, | |
238 * then 1 pass in X transposed to Y on write. | |
239 * | |
240 * +----+ +----+ +----+ +---+ +---+ +---+
+----+ | |
241 * + AB + ----> | AB | ----> | AB | -----> | A | ----> | A | ----> | A | ---
--> | AB | | |
242 * +----+ blurX +----+ blurX +----+ blurXY | B | blurX | B | blurX | B | blu
rXY +----+ | |
243 * +---+ +---+ +---+ | |
244 * | |
245 * In this way, two of the y-blurs become x-blurs applied to transposed | |
246 * images, and all memory reads are contiguous. | |
247 */ | |
248 if (kernelSizeX > 0 && kernelSizeY > 0) { | |
249 SkOpts::box_blur_xx(s, sw, inputBounds, t, kernelSizeX, lowOffsetX,
highOffsetX, w, h); | |
250 SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX, highOffsetX,
lowOffsetX, w, h); | |
251 SkOpts::box_blur_xy(d, w, dstBounds, t, kernelSizeX3, highOffsetX,
highOffsetX, w, h); | |
252 SkOpts::box_blur_xx(t, h, dstBoundsT, d, kernelSizeY, lowOffsetY,
highOffsetY, h, w); | |
253 SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY,
lowOffsetY, h, w); | |
254 SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY,
highOffsetY, h, w); | |
255 } else if (kernelSizeX > 0) { | |
256 SkOpts::box_blur_xx(s, sw, inputBounds, d, kernelSizeX, lowOffsetX,
highOffsetX, w, h); | |
257 SkOpts::box_blur_xx(d, w, dstBounds, t, kernelSizeX, highOffsetX,
lowOffsetX, w, h); | |
258 SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX3, highOffsetX,
highOffsetX, w, h); | |
259 } else if (kernelSizeY > 0) { | |
260 SkOpts::box_blur_yx(s, sw, inputBoundsT, d, kernelSizeY, lowOffsetY,
highOffsetY, h, w); | |
261 SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY,
lowOffsetY, h, w); | |
262 SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY,
highOffsetY, h, w); | |
263 } | |
264 | |
265 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(dstBounds.width(), | |
266 dstBounds.height()), | |
267 dst, &source->props()); | |
268 } | |
269 | |
270 | |
271 SkRect SkBlurImageFilterImpl::computeFastBounds(const SkRect& src) const { | |
272 SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src
) : src; | |
273 bounds.outset(SkScalarMul(fSigma.width(), SkIntToScalar(3)), | |
274 SkScalarMul(fSigma.height(), SkIntToScalar(3))); | |
275 return bounds; | |
276 } | |
277 | |
278 SkIRect SkBlurImageFilterImpl::onFilterNodeBounds(const SkIRect& src, const SkMa
trix& ctm, | |
279 MapDirection) const { | |
280 SkVector sigma = map_sigma(fSigma, ctm); | |
281 return src.makeOutset(SkScalarCeilToInt(SkScalarMul(sigma.x(), SkIntToScalar
(3))), | |
282 SkScalarCeilToInt(SkScalarMul(sigma.y(), SkIntToScalar
(3)))); | |
283 } | |
284 | |
285 #ifndef SK_IGNORE_TO_STRING | |
286 void SkBlurImageFilterImpl::toString(SkString* str) const { | |
287 str->appendf("SkBlurImageFilterImpl: ("); | |
288 str->appendf("sigma: (%f, %f) input (", fSigma.fWidth, fSigma.fHeight); | |
289 | |
290 if (this->getInput(0)) { | |
291 this->getInput(0)->toString(str); | |
292 } | |
293 | |
294 str->append("))"); | |
295 } | |
296 #endif | |
OLD | NEW |