blob: f79188f0165c503d30d85ff7412bc2a40a145c9e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
reed36e6e262014-11-14 08:27:40 -08007
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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkMaskFilter.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040014#include "include/core/SkPaint.h"
Mike Reed06d7c9d2020-08-26 12:56:51 -040015#include "include/core/SkPathBuilder.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040016#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkScalar.h"
19#include "include/core/SkTypeface.h"
20#include "include/core/SkTypes.h"
Herb Derbyf07a3632019-09-23 16:46:09 -040021#include "include/effects/SkBlurImageFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkBlurMask.h"
23#include "tools/Resources.h"
24#include "tools/ToolUtils.h"
reed@google.comc96e5c22011-02-16 21:50:04 +000025
Mike Kleind46dce32018-08-16 10:17:03 -040026DEF_SIMPLE_GM_BG(blurs, canvas, 700, 500, 0xFFDDDDDD) {
Mike Reed4de2f1f2019-01-05 16:35:13 -050027 SkBlurStyle NONE = SkBlurStyle(-999);
28 const struct {
29 SkBlurStyle fStyle;
30 int fCx, fCy;
31 } gRecs[] = {
32 { NONE, 0, 0 },
33 { kInner_SkBlurStyle, -1, 0 },
34 { kNormal_SkBlurStyle, 0, 1 },
35 { kSolid_SkBlurStyle, 0, -1 },
36 { kOuter_SkBlurStyle, 1, 0 },
37 };
reed@google.comc96e5c22011-02-16 21:50:04 +000038
Mike Reed4de2f1f2019-01-05 16:35:13 -050039 SkPaint paint;
40 paint.setAntiAlias(true);
41 paint.setColor(SK_ColorBLUE);
reed@google.comc96e5c22011-02-16 21:50:04 +000042
Mike Reed4de2f1f2019-01-05 16:35:13 -050043 canvas->translate(SkIntToScalar(-40), SkIntToScalar(0));
44
45 for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
46 if (gRecs[i].fStyle != NONE) {
47 paint.setMaskFilter(SkMaskFilter::MakeBlur(gRecs[i].fStyle,
48 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20))));
49 } else {
Mike Reed1be1f8d2018-03-14 13:01:17 -040050 paint.setMaskFilter(nullptr);
reed@google.comc96e5c22011-02-16 21:50:04 +000051 }
Mike Reed4de2f1f2019-01-05 16:35:13 -050052 canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100),
53 SkIntToScalar(200 + gRecs[i].fCy*100),
54 SkIntToScalar(50),
55 paint);
56 }
57 // draw text
58 {
Mike Kleinea3f0142019-03-20 11:12:10 -050059 SkFont font(ToolUtils::create_portable_typeface(), 25);
Mike Reed4de2f1f2019-01-05 16:35:13 -050060 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
61 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4))));
62 SkScalar x = SkIntToScalar(70);
63 SkScalar y = SkIntToScalar(400);
64 paint.setColor(SK_ColorBLACK);
65 canvas->drawString("Hamburgefons Style", x, y, font, paint);
66 canvas->drawString("Hamburgefons Style",
67 x, y + SkIntToScalar(50), font, paint);
68 paint.setMaskFilter(nullptr);
69 paint.setColor(SK_ColorWHITE);
70 x -= SkIntToScalar(2);
71 y -= SkIntToScalar(2);
72 canvas->drawString("Hamburgefons Style", x, y, font, paint);
73 }
halcanary2a243382015-09-09 08:16:41 -070074}
reed@google.comc96e5c22011-02-16 21:50:04 +000075
reed36e6e262014-11-14 08:27:40 -080076//////////////////////////////////////////////////////////////////////////////////////////////
reed@google.comc96e5c22011-02-16 21:50:04 +000077
reed36e6e262014-11-14 08:27:40 -080078// exercise a special-case of blurs, which is two nested rects. These are drawn specially,
79// and possibly cached.
80//
81// in particular, we want to notice that the 2nd rect draws slightly differently, since it
82// is translated a fractional amount.
83//
halcanary2a243382015-09-09 08:16:41 -070084DEF_SIMPLE_GM(blur2rects, canvas, 700, 500) {
Mike Reed06d7c9d2020-08-26 12:56:51 -040085 SkPaint paint;
reed36e6e262014-11-14 08:27:40 -080086
Mike Reed06d7c9d2020-08-26 12:56:51 -040087 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 2.3f));
reed36e6e262014-11-14 08:27:40 -080088
Mike Reed06d7c9d2020-08-26 12:56:51 -040089 SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100);
90 SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80);
91 SkPath path = SkPathBuilder().addRect(outer, SkPathDirection::kCW)
92 .addRect(inner, SkPathDirection::kCCW)
93 .detach();
reed36e6e262014-11-14 08:27:40 -080094
Mike Reed06d7c9d2020-08-26 12:56:51 -040095 canvas->drawPath(path, paint);
96 // important to translate by a factional amount to exercise a different "phase"
97 // of the same path w.r.t. the pixel grid
98 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f;
99 canvas->translate(dx, 0);
100 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -0700101}
qiankun.miao28828d02014-12-10 07:40:36 -0800102
halcanary2a243382015-09-09 08:16:41 -0700103DEF_SIMPLE_GM(blur2rectsnonninepatch, canvas, 700, 500) {
Mike Reed06d7c9d2020-08-26 12:56:51 -0400104 SkPaint paint;
105 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 4.3f));
qiankun.miao28828d02014-12-10 07:40:36 -0800106
Mike Reed06d7c9d2020-08-26 12:56:51 -0400107 SkRect outer = SkRect::MakeXYWH(10, 110, 100, 100);
108 SkRect inner = SkRect::MakeXYWH(50, 150, 10, 10);
109 SkPath path = SkPathBuilder().addRect(outer, SkPathDirection::kCW)
110 .addRect(inner, SkPathDirection::kCW)
111 .detach();
112 canvas->drawPath(path, paint);
qiankun.miao28828d02014-12-10 07:40:36 -0800113
Mike Reed06d7c9d2020-08-26 12:56:51 -0400114 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 40 + 0.25f;
115 canvas->translate(dx, 0);
116 canvas->drawPath(path, paint);
qiankun.miao28828d02014-12-10 07:40:36 -0800117
Mike Reed06d7c9d2020-08-26 12:56:51 -0400118 // Translate to outside of clip bounds.
119 canvas->translate(-dx, 0);
120 canvas->translate(-30, -150);
121 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -0700122}
Hal Canary9982c4e2017-06-07 15:23:49 -0400123
124DEF_SIMPLE_GM(BlurDrawImage, canvas, 256, 256) {
125 SkPaint paint;
Mike Reed1be1f8d2018-03-14 13:01:17 -0400126 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10));
Hal Canary9982c4e2017-06-07 15:23:49 -0400127 canvas->clear(0xFF88FF88);
Hal Canaryc465d132017-12-08 10:21:31 -0500128 if (auto image = GetResourceAsImage("images/mandrill_512_q075.jpg")) {
Hal Canary9982c4e2017-06-07 15:23:49 -0400129 canvas->scale(0.25, 0.25);
130 canvas->drawImage(image, 256, 256, &paint);
131 }
132}
Herb Derbyf07a3632019-09-23 16:46:09 -0400133
134DEF_SIMPLE_GM(BlurBigSigma, canvas, 1024, 1024) {
135 SkPaint layerPaint, p;
136
Herb Derby4e8f63a2019-09-25 18:44:35 -0400137 p.setImageFilter(SkBlurImageFilter::Make(500, 500, nullptr));
Herb Derbyf07a3632019-09-23 16:46:09 -0400138
139 canvas->drawRect(SkRect::MakeWH(700, 800), p);
140}