blob: 3c1583859908491790423fd7b6783d497f705f15 [file] [log] [blame]
Herb Derbya48ae6e2017-07-10 13:49:05 -04001/*
2 * Copyright 2017 Google Inc.
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -04009#include "include/core/SkBlurTypes.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkImage.h"
14#include "include/core/SkImageFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkMaskFilter.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040016#include "include/core/SkPaint.h"
17#include "include/core/SkRect.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkString.h"
20#include "include/core/SkTypeface.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040021#include "include/effects/SkImageFilters.h"
Kevin Lubick8b741882023-10-06 11:41:38 -040022#include "tools/DecodeUtils.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040023#include "tools/Resources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040025#include "tools/fonts/FontToolUtils.h"
Herb Derbya48ae6e2017-07-10 13:49:05 -040026
Ben Wagner6a34f3a2019-05-01 10:59:30 -040027#include <stdio.h>
28
Herb Derbya48ae6e2017-07-10 13:49:05 -040029DEF_SIMPLE_GM(blurimagevmask, canvas, 700, 1200) {
30 SkPaint paint;
31 paint.setAntiAlias(true);
32 paint.setColor(SK_ColorBLACK);
33
Kevin Lubicke836c3a2023-10-20 06:55:35 -040034 SkFont font(ToolUtils::DefaultPortableTypeface(), 25);
Herb Derbya48ae6e2017-07-10 13:49:05 -040035
36 const double sigmas[] = {3.0, 8.0, 16.0, 24.0, 32.0};
37
Mike Reed4de2f1f2019-01-05 16:35:13 -050038 canvas->drawString("mask blur", 285, 50, font, paint);
39 canvas->drawString("image blur", 285 + 250, 50, font, paint);
Herb Derbya48ae6e2017-07-10 13:49:05 -040040
41
42 SkRect r = {35, 100, 135, 200};
43 for (auto sigma:sigmas) {
44
45 canvas->drawRect(r, paint);
46
47 char out[100];
48 sprintf(out, "Sigma: %g", sigma);
Mike Reed4de2f1f2019-01-05 16:35:13 -050049 canvas->drawString(out, r.left(), r.bottom() + 35, font, paint);
Herb Derbya48ae6e2017-07-10 13:49:05 -040050
51 r.offset(250, 0);
52
Mike Reed1be1f8d2018-03-14 13:01:17 -040053 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma));
Herb Derbya48ae6e2017-07-10 13:49:05 -040054 canvas->drawRect(r, paint);
55 paint.setMaskFilter(nullptr);
56
57 SkPaint imageBlurPaint;
58 r.offset(250, 0);
Michael Ludwig898bbfa2019-08-02 15:21:23 -040059 imageBlurPaint.setImageFilter(SkImageFilters::Blur(sigma, sigma, nullptr));
Herb Derbya48ae6e2017-07-10 13:49:05 -040060 canvas->saveLayer(nullptr, &imageBlurPaint);
61
62 canvas->drawRect(r, paint);
63 canvas->restore();
64 r.offset(-500, 200);
65 }
66
67}
Mike Reed28266272018-01-17 23:04:04 -050068
Chris Dalton50e24d72019-02-07 16:20:09 -070069DEF_SIMPLE_GM_CAN_FAIL(blur_image, canvas, errorMsg, 500, 500) {
Kevin Lubick8b741882023-10-06 11:41:38 -040070 auto image = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
Hal Canarybaa2a282018-11-26 15:34:12 -050071 if (!image) {
Chris Dalton50e24d72019-02-07 16:20:09 -070072 *errorMsg = "Could not load mandrill_128.png. Did you forget to set the resourcePath?";
73 return skiagm::DrawResult::kFail;
Hal Canarybaa2a282018-11-26 15:34:12 -050074 }
Mike Reed28266272018-01-17 23:04:04 -050075
76 SkPaint paint;
Mike Reed1be1f8d2018-03-14 13:01:17 -040077 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 4));
Mike Reed28266272018-01-17 23:04:04 -050078
79 // both of these should draw with the blur, but (formerally) we had a bug where the unscaled
80 // version (taking the spriteblitter code path) ignore the maskfilter.
81
Mike Reed07c5f522021-01-23 12:23:23 -050082 canvas->drawImage(image, 10, 10, SkSamplingOptions(), &paint);
Mike Reed28266272018-01-17 23:04:04 -050083 canvas->scale(1.01f, 1.01f);
Mike Reed07c5f522021-01-23 12:23:23 -050084 canvas->drawImage(image, 10 + image->width() + 10.f, 10, SkSamplingOptions(), &paint);
Chris Dalton50e24d72019-02-07 16:20:09 -070085 return skiagm::DrawResult::kOk;
Mike Reed28266272018-01-17 23:04:04 -050086}