blob: bd666bb2bedb09090d562ca1964c35f695ed1a75 [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) {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000017 SkBlurStyle NONE = SkBlurStyle(-999);
mtkleindbfd7ab2016-09-01 11:24:54 -070018 const struct {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000019 SkBlurStyle fStyle;
20 int fCx, fCy;
reed@google.comc96e5c22011-02-16 21:50:04 +000021 } gRecs[] = {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000022 { NONE, 0, 0 },
23 { kInner_SkBlurStyle, -1, 0 },
24 { kNormal_SkBlurStyle, 0, 1 },
25 { kSolid_SkBlurStyle, 0, -1 },
26 { kOuter_SkBlurStyle, 1, 0 },
reed@google.comc96e5c22011-02-16 21:50:04 +000027 };
28
29 SkPaint paint;
30 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070031 sk_tool_utils::set_portable_typeface(&paint);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000032 paint.setTextSize(SkIntToScalar(25));
33 canvas->translate(SkIntToScalar(-40), SkIntToScalar(0));
reed@google.comc96e5c22011-02-16 21:50:04 +000034
Mike Reed1be1f8d2018-03-14 13:01:17 -040035 paint.setColor(SK_ColorBLUE);
36 for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
37 if (gRecs[i].fStyle != NONE) {
38 paint.setMaskFilter(SkMaskFilter::MakeBlur(gRecs[i].fStyle,
39 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20))));
40 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070041 paint.setMaskFilter(nullptr);
reed@google.comc96e5c22011-02-16 21:50:04 +000042 }
Mike Reed1be1f8d2018-03-14 13:01:17 -040043 canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100),
44 SkIntToScalar(200 + gRecs[i].fCy*100),
45 SkIntToScalar(50),
46 paint);
47 }
48 // draw text
49 {
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, paint);
56 canvas->drawString("Hamburgefons Style",
57 x, y + SkIntToScalar(50), 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, paint);
reed@google.comc96e5c22011-02-16 21:50:04 +000063 }
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}