Debian Codegraph @

gm/gpu_blur_utils.cpp

raw
1 /*
2 * Copyright 2020 Google LLC.
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 "gm/gm.h"
9
10 #include "include/core/SkColorSpace.h"
11 #include "include/effects/SkGradient.h"
12 #include "include/gpu/ganesh/GrRecordingContext.h"
13 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
14 #include "src/core/SkCanvasPriv.h"
15 #include "src/gpu/BlurUtils.h"
16 #include "src/gpu/ganesh/GrBlurUtils.h"
17 #include "src/gpu/ganesh/GrCanvas.h"
18 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
19 #include "src/gpu/ganesh/GrStyle.h"
20 #include "src/gpu/ganesh/SkGr.h"
21 #include "src/gpu/ganesh/SurfaceDrawContext.h"
22 #include "src/gpu/ganesh/effects/GrBlendFragmentProcessor.h"
23 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
24 #include "src/gpu/ganesh/image/GrImageUtils.h"
25 #include "src/image/SkImage_Base.h"
26
27 namespace {
28
29 static GrSurfaceProxyView blur(GrRecordingContext* ctx,
30 GrSurfaceProxyView src,
31 SkIRect dstB,
32 SkIRect srcB,
33 float sigmaX,
34 float sigmaY,
35 SkTileMode mode) {
36 auto resultSDC = GrBlurUtils::GaussianBlur(ctx,
37 src,
38 GrColorType::kRGBA_8888,
39 kPremul_SkAlphaType,
40 nullptr,
41 dstB,
42 srcB,
43 sigmaX,
44 sigmaY,
45 mode);
46 if (!resultSDC) {
47 return {};
48 }
49 return resultSDC->readSurfaceView();
50 }
51
52 // Performs tiling first of the src into dst bounds with a surrounding skirt so the blur can use
53 // clamp. Does repeated blurs rather than invoking downsampling.
54 static GrSurfaceProxyView slow_blur(GrRecordingContext* rContext,
55 GrSurfaceProxyView src,
56 SkIRect dstB,
57 SkIRect srcB,
58 float sigmaX,
59 float sigmaY,
60 SkTileMode mode) {
61 auto tileInto = [rContext](GrSurfaceProxyView src,
62 SkIRect srcTileRect,
63 SkISize resultSize,
64 SkIPoint offset,
65 SkTileMode mode) {
66 GrImageInfo info(GrColorType::kRGBA_8888, kPremul_SkAlphaType, nullptr, resultSize);
67 auto sfc = rContext->priv().makeSFC(info, /*label=*/{});
68 if (!sfc) {
69 return GrSurfaceProxyView{};
70 }
71 GrSamplerState sampler(SkTileModeToWrapMode(mode), SkFilterMode::kNearest);
72 auto fp = GrTextureEffect::MakeSubset(src,
73 kPremul_SkAlphaType,
74 SkMatrix::Translate(-offset.x(), -offset.y()),
75 sampler,
76 SkRect::Make(srcTileRect),
77 *rContext->priv().caps());
78 sfc->fillWithFP(std::move(fp));
79 return sfc->readSurfaceView();
80 };
81
82 SkIPoint outset = {skgpu::BlurSigmaRadius(sigmaX), skgpu::BlurSigmaRadius(sigmaY)};
83 SkISize size = {dstB.width() + 2*outset.x(), dstB.height() + 2*outset.y()};
84 src = tileInto(std::move(src), srcB, size, outset - dstB.topLeft(), mode);
85 if (!src) {
86 return {};
87 }
88 dstB = SkIRect::MakePtSize(outset, dstB.size());
89
90 while (sigmaX || sigmaY) {
91 float stepX = sigmaX;
92 if (stepX > skgpu::kMaxLinearBlurSigma) {
93 stepX = skgpu::kMaxLinearBlurSigma;
94 // A blur of sigma1 followed by a blur of sigma2 is equiv. to a single blur of
95 // sqrt(sigma1^2 + sigma2^2).
96 sigmaX = sqrt(sigmaX*sigmaX - skgpu::kMaxLinearBlurSigma*skgpu::kMaxLinearBlurSigma);
97 } else {
98 sigmaX = 0.f;
99 }
100 float stepY = sigmaY;
101 if (stepY > skgpu::kMaxLinearBlurSigma) {
102 stepY = skgpu::kMaxLinearBlurSigma;
103 sigmaY = sqrt(sigmaY*sigmaY- skgpu::kMaxLinearBlurSigma*skgpu::kMaxLinearBlurSigma);
104 } else {
105 sigmaY = 0.f;
106 }
107 auto bounds = SkIRect::MakeSize(src.dimensions());
108 auto sdc = GrBlurUtils::GaussianBlur(rContext,
109 std::move(src),
110 GrColorType::kRGBA_8888,
111 kPremul_SkAlphaType,
112 nullptr,
113 bounds,
114 bounds,
115 stepX,
116 stepY,
117 SkTileMode::kClamp);
118 if (!sdc) {
119 return {};
120 }
121 src = sdc->readSurfaceView();
122 }
123 // We have o use the original mode here because we may have only blurred in X or Y and then
124 // the other dimension was not expanded.
125 auto srcRect = SkIRect::MakeSize(src.dimensions());
126 return tileInto(std::move(src), srcRect, dstB.size(), -outset, SkTileMode::kClamp);
127 }
128
129 // Makes a src texture for as a source for blurs. If 'contentArea' then the content will
130 // be in that rect, the 1-pixel surrounding border will be transparent black, and red outside of
131 // that. Otherwise, the content fills the dimensions.
132 GrSurfaceProxyView make_src_image(GrRecordingContext* rContext,
133 SkISize dimensions,
134 const SkIRect* contentArea = nullptr) {
135 auto srcII = SkImageInfo::Make(dimensions, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
136 auto surf = SkSurfaces::RenderTarget(rContext, skgpu::Budgeted::kYes, srcII);
137 if (!surf) {
138 return {};
139 }
140
141 float w, h;
142 if (contentArea) {
143 surf->getCanvas()->clear(SK_ColorRED);
144 surf->getCanvas()->clipIRect(contentArea->makeOutset(1, 1));
145 surf->getCanvas()->clear(SK_ColorTRANSPARENT);
146 surf->getCanvas()->clipIRect(*contentArea);
147 surf->getCanvas()->translate(contentArea->top(), contentArea->left());
148 w = contentArea->width();
149 h = contentArea->height();
150 } else {
151 w = dimensions.width();
152 h = dimensions.height();
153 }
154
155 surf->getCanvas()->drawColor(SK_ColorDKGRAY);
156 SkPaint paint;
157 paint.setAntiAlias(true);
158 paint.setStyle(SkPaint::kStroke_Style);
159 // Draw four horizontal lines at 1/8, 1/4, 3/4, 7/8.
160 paint.setStrokeWidth(h/12.f);
161 paint.setColor(SK_ColorRED);
162 surf->getCanvas()->drawLine({0.f, 1.f*h/8.f}, {w, 1.f*h/8.f}, paint);
163 paint.setColor(/* sea foam */ 0xFF71EEB8);
164 surf->getCanvas()->drawLine({0.f, 1.f*h/4.f}, {w, 1.f*h/4.f}, paint);
165 paint.setColor(SK_ColorYELLOW);
166 surf->getCanvas()->drawLine({0.f, 3.f*h/4.f}, {w, 3.f*h/4.f}, paint);
167 paint.setColor(SK_ColorCYAN);
168 surf->getCanvas()->drawLine({0.f, 7.f*h/8.f}, {w, 7.f*h/8.f}, paint);
169
170 // Draw four vertical lines at 1/8, 1/4, 3/4, 7/8.
171 paint.setStrokeWidth(w/12.f);
172 paint.setColor(/* orange */ 0xFFFFA500);
173 surf->getCanvas()->drawLine({1.f*w/8.f, 0.f}, {1.f*h/8.f, h}, paint);
174 paint.setColor(SK_ColorBLUE);
175 surf->getCanvas()->drawLine({1.f*w/4.f, 0.f}, {1.f*h/4.f, h}, paint);
176 paint.setColor(SK_ColorMAGENTA);
177 surf->getCanvas()->drawLine({3.f*w/4.f, 0.f}, {3.f*h/4.f, h}, paint);
178 paint.setColor(SK_ColorGREEN);
179 surf->getCanvas()->drawLine({7.f*w/8.f, 0.f}, {7.f*h/8.f, h}, paint);
180
181 auto img = surf->makeImageSnapshot();
182 // We throw away the surface immediately so we know the image won't be drawn into itself. Thus
183 // we pass nullptr for targetSurface.
184 auto [src, ct] = skgpu::ganesh::AsView(rContext, img, skgpu::Mipmapped::kNo,
185 /*targetSurface=*/nullptr);
186 return src;
187 }
188
189 } // namespace
190
191 namespace skiagm {
192
193 static GM::DrawResult run(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg,
194 bool subsetSrc, bool ref) {
195 GrSurfaceProxyView src = make_src_image(rContext, {60, 60});
196 if (!src) {
197 *errorMsg = "Failed to create source image";
198 return DrawResult::kSkip;
199 }
200
201 auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas);
202 if (!sdc) {
203 *errorMsg = GM::kErrorMsg_DrawSkippedGpuOnly;
204 return DrawResult::kSkip;
205 }
206
207 SkIRect srcRect = SkIRect::MakeSize(src.dimensions());
208 if (subsetSrc) {
209 srcRect = SkIRect::MakeXYWH(2.f*srcRect.width() /8.f,
210 1.f*srcRect.height()/8.f,
211 5.f*srcRect.width() /8.f,
212 6.f*srcRect.height()/8.f);
213 }
214 int srcW = srcRect.width();
215 int srcH = srcRect.height();
216 // Each set of rects is drawn in one test area so they probably should not abut or overlap
217 // to visualize the blurs separately.
218 const std::vector<SkIRect> dstRectSets[] = {
219 // encloses source bounds.
220 {
221 srcRect.makeOutset(srcW/5, srcH/5)
222 },
223
224 // partial overlap from above/below.
225 {
226 SkIRect::MakeXYWH(srcRect.x(), srcRect.y() + 3*srcH/4, srcW, srcH),
227 SkIRect::MakeXYWH(srcRect.x(), srcRect.y() - 3*srcH/4, srcW, srcH)
228 },
229
230 // adjacent to each side of src bounds.
231 {
232 srcRect.makeOffset( 0, srcH),
233 srcRect.makeOffset( srcW, 0),
234 srcRect.makeOffset( 0, -srcH),
235 srcRect.makeOffset(-srcW, 0),
236 },
237
238 // fully outside src bounds in one direction.
239 {
240 SkIRect::MakeXYWH(-6.f*srcW/8.f, -7.f*srcH/8.f, 4.f*srcW/8.f, 20.f*srcH/8.f)
241 .makeOffset(srcRect.topLeft()),
242 SkIRect::MakeXYWH(-1.f*srcW/8.f, -7.f*srcH/8.f, 16.f*srcW/8.f, 2.f*srcH/8.f)
243 .makeOffset(srcRect.topLeft()),
244 SkIRect::MakeXYWH(10.f*srcW/8.f, -3.f*srcH/8.f, 4.f*srcW/8.f, 16.f*srcH/8.f)
245 .makeOffset(srcRect.topLeft()),
246 SkIRect::MakeXYWH(-7.f*srcW/8.f, 14.f*srcH/8.f, 18.f*srcW/8.f, 1.f*srcH/8.f)
247 .makeOffset(srcRect.topLeft()),
248 },
249
250 // outside of src bounds in both directions.
251 {
252 SkIRect::MakeXYWH(-5.f*srcW/8.f, -5.f*srcH/8.f, 2.f*srcW/8.f, 2.f*srcH/8.f)
253 .makeOffset(srcRect.topLeft()),
254 SkIRect::MakeXYWH(-5.f*srcW/8.f, 12.f*srcH/8.f, 2.f*srcW/8.f, 2.f*srcH/8.f)
255 .makeOffset(srcRect.topLeft()),
256 SkIRect::MakeXYWH(12.f*srcW/8.f, -5.f*srcH/8.f, 2.f*srcW/8.f, 2.f*srcH/8.f)
257 .makeOffset(srcRect.topLeft()),
258 SkIRect::MakeXYWH(12.f*srcW/8.f, 12.f*srcH/8.f, 2.f*srcW/8.f, 2.f*srcH/8.f)
259 .makeOffset(srcRect.topLeft()),
260 },
261 };
262
263 const auto& caps = *rContext->priv().caps();
264
265 static constexpr SkScalar kPad = 10;
266 SkVector trans = {kPad, kPad};
267
268 sdc->clear(SK_PMColor4fWHITE);
269
270 SkIRect testArea = srcRect;
271 testArea.outset(testArea.width(), testArea.height());
272 for (const auto& dstRectSet : dstRectSets) {
273 for (int t = 0; t < kSkTileModeCount; ++t) {
274 auto mode = static_cast<SkTileMode>(t);
275 GrSamplerState sampler(SkTileModeToWrapMode(mode), GrSamplerState::Filter::kNearest);
276 SkMatrix m = SkMatrix::Translate(trans.x() - testArea.x(), trans.y() - testArea.y());
277 // Draw the src subset in the tile mode faded as a reference before drawing the blur
278 // on top.
279 {
280 static constexpr float kAlpha = 0.2f;
281 auto fp = GrTextureEffect::MakeSubset(src, kPremul_SkAlphaType, SkMatrix::I(),
282 sampler, SkRect::Make(srcRect), caps);
283 fp = GrFragmentProcessor::ModulateRGBA(std::move(fp),
284 {kAlpha, kAlpha, kAlpha, kAlpha});
285 GrPaint paint;
286 paint.setColorFragmentProcessor(std::move(fp));
287 sdc->drawRect(nullptr, std::move(paint), GrAA::kNo, m, SkRect::Make(testArea));
288 }
289 // Do a blur for each dstRect in the set over our testArea-sized background.
290 for (const auto& dstRect : dstRectSet) {
291 const SkScalar sigmaX = src.width() / 10.f;
292 const SkScalar sigmaY = src.height() / 10.f;
293 auto blurFn = ref ? slow_blur : blur;
294 // Blur using the rect and draw on top.
295 if (auto blurView = blurFn(rContext,
296 src,
297 dstRect,
298 srcRect,
299 sigmaX,
300 sigmaY,
301 mode)) {
302 auto fp = GrTextureEffect::Make(blurView,
303 kPremul_SkAlphaType,
304 SkMatrix::I(),
305 sampler,
306 caps);
307 // Compose against white (default paint color)
308 fp = GrBlendFragmentProcessor::Make<SkBlendMode::kSrcOver>(std::move(fp),
309 /*dst=*/nullptr);
310 GrPaint paint;
311 // Compose against white (default paint color) and then replace the dst
312 // (SkBlendMode::kSrc).
313 fp = GrBlendFragmentProcessor::Make<SkBlendMode::kSrcOver>(std::move(fp),
314 /*dst=*/nullptr);
315 paint.setColorFragmentProcessor(std::move(fp));
316 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
317 sdc->fillRectToRect(nullptr,
318 std::move(paint),
319 GrAA::kNo,
320 m,
321 SkRect::Make(dstRect),
322 SkRect::Make(blurView.dimensions()));
323 }
324 // Show the outline of the dst rect. Mostly for kDecal but also allows visual
325 // confirmation that the resulting blur is the right size and in the right place.
326 {
327 GrPaint paint;
328 static constexpr float kAlpha = 0.6f;
329 paint.setColor4f({0, kAlpha, 0, kAlpha});
330 SkPaint stroke;
331 stroke.setStyle(SkPaint::kStroke_Style);
332 stroke.setStrokeWidth(1.f);
333 GrStyle style(stroke);
334 auto dstR = SkRect::Make(dstRect).makeOutset(0.5f, 0.5f);
335 sdc->drawRect(nullptr, std::move(paint), GrAA::kNo, m, dstR, &style);
336 }
337 }
338 // Show the rect that's being blurred.
339 {
340 GrPaint paint;
341 static constexpr float kAlpha = 0.3f;
342 paint.setColor4f({0, 0, 0, kAlpha});
343 SkPaint stroke;
344 stroke.setStyle(SkPaint::kStroke_Style);
345 stroke.setStrokeWidth(1.f);
346 GrStyle style(stroke);
347 auto srcR = SkRect::Make(srcRect).makeOutset(0.5f, 0.5f);
348 sdc->drawRect(nullptr, std::move(paint), GrAA::kNo, m, srcR, &style);
349 }
350 trans.fX += testArea.width() + kPad;
351 }
352 trans.fX = kPad;
353 trans.fY += testArea.height() + kPad;
354 }
355
356 return DrawResult::kOk;
357 }
358
359 DEF_SIMPLE_GPU_GM_CAN_FAIL(gpu_blur_utils, rContext, canvas, errorMsg, 765, 955) {
360 return run(rContext, canvas, errorMsg, false, false);
361 }
362
363 DEF_SIMPLE_GPU_GM_CAN_FAIL(gpu_blur_utils_ref, rContext, canvas, errorMsg, 765, 955) {
364 return run(rContext, canvas, errorMsg, false, true);
365 }
366
367 DEF_SIMPLE_GPU_GM_CAN_FAIL(gpu_blur_utils_subset_rect, rContext, canvas, errorMsg, 485, 730) {
368 return run(rContext, canvas, errorMsg, true, false);
369 }
370
371 DEF_SIMPLE_GPU_GM_CAN_FAIL(gpu_blur_utils_subset_ref, rContext, canvas, errorMsg, 485, 730) {
372 return run(rContext, canvas, errorMsg, true, true);
373 }
374
375 // Because of the way blur sigmas concat (sigTotal = sqrt(sig1^2 + sig2^2) generating these images
376 // for very large sigmas is incredibly slow. This can be enabled while working on the blur code to
377 // check results.
378 static bool constexpr kShowSlowRefImages = false;
379
380 static DrawResult do_very_large_blur_gm(GrRecordingContext* rContext,
381 SkCanvas* canvas,
382 SkString* errorMsg,
383 GrSurfaceProxyView src,
384 SkIRect srcB) {
385 auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas);
386 if (!sdc) {
387 *errorMsg = GM::kErrorMsg_DrawSkippedGpuOnly;
388 return DrawResult::kSkip;
389 }
390
391 // Clear to a color other than gray to contrast with test image.
392 sdc->clear(SkColor4f{0.3f, 0.4f, 0.2f, 1});
393
394 int x = 10;
395 int y = 10;
396 for (auto blurDirs : {0b01, 0b10, 0b11}) {
397 for (int t = 0; t < kSkTileModeCount; ++t) {
398 auto tm = static_cast<SkTileMode>(t);
399 auto dstB = srcB.makeOutset(30, 30);
400 for (float sigma : {0.f, 5.f, 25.f, 80.f}) {
401 std::vector<decltype(blur)*> blurs;
402 blurs.push_back(blur);
403 if (kShowSlowRefImages) {
404 blurs.push_back(slow_blur);
405 }
406 for (auto b : blurs) {
407 float sigX = sigma*((blurDirs & 0b01) >> 0);
408 float sigY = sigma*((blurDirs & 0b10) >> 1);
409 GrSurfaceProxyView result = b(rContext, src, dstB, srcB, sigX, sigY, tm);
410 auto dstRect = SkIRect::MakeSize(dstB.size()).makeOffset(x, y);
411 // Draw a rect to show where the result should be so it's obvious if it's
412 // missing.
413 GrPaint paint;
414 paint.setColor4f(b == blur ? SkPMColor4f{0, 0, 1, 1} : SkPMColor4f{1, 0, 0, 1});
415 sdc->drawRect(nullptr,
416 std::move(paint),
417 GrAA::kNo,
418 SkMatrix::I(),
419 SkRect::Make(dstRect).makeOutset(0.5, 0.5),
420 &GrStyle::SimpleHairline());
421 if (result) {
422 std::unique_ptr<GrFragmentProcessor> fp =
423 GrTextureEffect::Make(std::move(result), kPremul_SkAlphaType);
424 fp = GrBlendFragmentProcessor::Make<SkBlendMode::kSrcOver>(std::move(fp),
425 /*dst=*/nullptr);
426 sdc->fillRectToRectWithFP(SkIRect::MakeSize(dstB.size()),
427 dstRect,
428 std::move(fp));
429 }
430 x += dstB.width() + 10;
431 }
432 }
433 x = 10;
434 y += dstB.height() + 10;
435 }
436 }
437
438 return DrawResult::kOk;
439 }
440
441 DEF_SIMPLE_GPU_GM_CAN_FAIL(very_large_sigma_gpu_blur, rContext, canvas, errorMsg, 350, 1030) {
442 auto src = make_src_image(rContext, {15, 15});
443 auto srcB = SkIRect::MakeSize(src.dimensions());
444 return do_very_large_blur_gm(rContext, canvas, errorMsg, std::move(src), srcB);
445 }
446
447 DEF_SIMPLE_GPU_GM_CAN_FAIL(very_large_sigma_gpu_blur_subset,
448 rContext,
449 canvas,
450 errorMsg,
451 350, 1030) {
452 auto srcB = SkIRect::MakeXYWH(2, 2, 15, 15);
453 SkISize imageSize = SkISize{srcB.width() + 4, srcB.height() + 4};
454 auto src = make_src_image(rContext, imageSize, &srcB);
455 return do_very_large_blur_gm(rContext, canvas, errorMsg, std::move(src), srcB);
456 }
457
458 DEF_SIMPLE_GPU_GM_CAN_FAIL(very_large_sigma_gpu_blur_subset_transparent_border,
459 rContext,
460 canvas,
461 errorMsg,
462 355, 1055) {
463 auto srcB = SkIRect::MakeXYWH(3, 3, 15, 15);
464 SkISize imageSize = SkISize{srcB.width() + 4, srcB.height() + 4};
465 auto src = make_src_image(rContext, imageSize, &srcB);
466 return do_very_large_blur_gm(rContext, canvas, errorMsg, std::move(src), srcB.makeOutset(1, 1));
467 }
468
469 } // namespace skiagm