blob: 6892ad31bcfd623f06655b85cec3edefec3cc0f1 [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
reed@google.comc96e5c22011-02-16 21:50:04 +00008#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
Hal Canary9982c4e2017-06-07 15:23:49 -040010#include "Resources.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000011#include "SkBlurMask.h"
Hal Canary9982c4e2017-06-07 15:23:49 -040012#include "SkImage.h"
Mike Reed1be1f8d2018-03-14 13:01:17 -040013#include "SkMaskFilter.h"
bungemand3ebb482015-08-05 13:57:49 -070014#include "SkPath.h"
reed@google.comc96e5c22011-02-16 21:50:04 +000015
Mike Kleind46dce32018-08-16 10:17:03 -040016DEF_SIMPLE_GM_BG(blurs, canvas, 700, 500, 0xFFDDDDDD) {
Mike Reed4de2f1f2019-01-05 16:35:13 -050017 SkBlurStyle NONE = SkBlurStyle(-999);
18 const struct {
19 SkBlurStyle fStyle;
20 int fCx, fCy;
21 } gRecs[] = {
22 { NONE, 0, 0 },
23 { kInner_SkBlurStyle, -1, 0 },
24 { kNormal_SkBlurStyle, 0, 1 },
25 { kSolid_SkBlurStyle, 0, -1 },
26 { kOuter_SkBlurStyle, 1, 0 },
27 };
reed@google.comc96e5c22011-02-16 21:50:04 +000028
Mike Reed4de2f1f2019-01-05 16:35:13 -050029 SkPaint paint;
30 paint.setAntiAlias(true);
31 paint.setColor(SK_ColorBLUE);
reed@google.comc96e5c22011-02-16 21:50:04 +000032
Mike Reed4de2f1f2019-01-05 16:35:13 -050033 canvas->translate(SkIntToScalar(-40), SkIntToScalar(0));
34
35 for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
36 if (gRecs[i].fStyle != NONE) {
37 paint.setMaskFilter(SkMaskFilter::MakeBlur(gRecs[i].fStyle,
38 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20))));
39 } else {
Mike Reed1be1f8d2018-03-14 13:01:17 -040040 paint.setMaskFilter(nullptr);
reed@google.comc96e5c22011-02-16 21:50:04 +000041 }
Mike Reed4de2f1f2019-01-05 16:35:13 -050042 canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100),
43 SkIntToScalar(200 + gRecs[i].fCy*100),
44 SkIntToScalar(50),
45 paint);
46 }
47 // draw text
48 {
49 SkFont font(sk_tool_utils::create_portable_typeface(), 25);
50 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
51 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4))));
52 SkScalar x = SkIntToScalar(70);
53 SkScalar y = SkIntToScalar(400);
54 paint.setColor(SK_ColorBLACK);
55 canvas->drawString("Hamburgefons Style", x, y, font, paint);
56 canvas->drawString("Hamburgefons Style",
57 x, y + SkIntToScalar(50), font, paint);
58 paint.setMaskFilter(nullptr);
59 paint.setColor(SK_ColorWHITE);
60 x -= SkIntToScalar(2);
61 y -= SkIntToScalar(2);
62 canvas->drawString("Hamburgefons Style", x, y, font, paint);
63 }
halcanary2a243382015-09-09 08:16:41 -070064}
reed@google.comc96e5c22011-02-16 21:50:04 +000065
reed36e6e262014-11-14 08:27:40 -080066//////////////////////////////////////////////////////////////////////////////////////////////
reed@google.comc96e5c22011-02-16 21:50:04 +000067
reed36e6e262014-11-14 08:27:40 -080068// exercise a special-case of blurs, which is two nested rects. These are drawn specially,
69// and possibly cached.
70//
71// in particular, we want to notice that the 2nd rect draws slightly differently, since it
72// is translated a fractional amount.
73//
halcanary2a243382015-09-09 08:16:41 -070074DEF_SIMPLE_GM(blur2rects, canvas, 700, 500) {
reed36e6e262014-11-14 08:27:40 -080075 SkPaint paint;
76
Mike Reed1be1f8d2018-03-14 13:01:17 -040077 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 2.3f));
reed36e6e262014-11-14 08:27:40 -080078
reed468b6b42014-11-17 07:41:35 -080079 SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100);
80 SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80);
reed36e6e262014-11-14 08:27:40 -080081 SkPath path;
82 path.addRect(outer, SkPath::kCW_Direction);
83 path.addRect(inner, SkPath::kCCW_Direction);
84
85 canvas->drawPath(path, paint);
reedae760d52014-11-14 11:59:10 -080086 // important to translate by a factional amount to exercise a different "phase"
reed36e6e262014-11-14 08:27:40 -080087 // of the same path w.r.t. the pixel grid
reed468b6b42014-11-17 07:41:35 -080088 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f;
reedae760d52014-11-14 11:59:10 -080089 canvas->translate(dx, 0);
reed36e6e262014-11-14 08:27:40 -080090 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -070091}
qiankun.miao28828d02014-12-10 07:40:36 -080092
halcanary2a243382015-09-09 08:16:41 -070093DEF_SIMPLE_GM(blur2rectsnonninepatch, canvas, 700, 500) {
qiankun.miao28828d02014-12-10 07:40:36 -080094 SkPaint paint;
Mike Reed1be1f8d2018-03-14 13:01:17 -040095 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 4.3f));
qiankun.miao28828d02014-12-10 07:40:36 -080096
97 SkRect outer = SkRect::MakeXYWH(10, 110, 100, 100);
98 SkRect inner = SkRect::MakeXYWH(50, 150, 10, 10);
99 SkPath path;
100 path.addRect(outer, SkPath::kCW_Direction);
101 path.addRect(inner, SkPath::kCW_Direction);
102 canvas->drawPath(path, paint);
103
104 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 40 + 0.25f;
105 canvas->translate(dx, 0);
106 canvas->drawPath(path, paint);
107
108 // Translate to outside of clip bounds.
109 canvas->translate(-dx, 0);
110 canvas->translate(-30, -150);
111 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -0700112}
Hal Canary9982c4e2017-06-07 15:23:49 -0400113
114DEF_SIMPLE_GM(BlurDrawImage, canvas, 256, 256) {
115 SkPaint paint;
Mike Reed1be1f8d2018-03-14 13:01:17 -0400116 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10));
Hal Canary9982c4e2017-06-07 15:23:49 -0400117 canvas->clear(0xFF88FF88);
Hal Canaryc465d132017-12-08 10:21:31 -0500118 if (auto image = GetResourceAsImage("images/mandrill_512_q075.jpg")) {
Hal Canary9982c4e2017-06-07 15:23:49 -0400119 canvas->scale(0.25, 0.25);
120 canvas->drawImage(image, 256, 256, &paint);
121 }
122}