1 /*
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 */
7
8 #include "gm.h"
9 #include "SkBlurDrawLooper.h"
10 #include "SkBlurMask.h"
11 #include "SkPath.h"
12
13 namespace skiagm {
14
15 ///////////////////////////////////////////////////////////////////////////////
16
setup(SkPaint * paint,SkColor c,SkScalar strokeWidth)17 static void setup(SkPaint* paint, SkColor c, SkScalar strokeWidth) {
18 paint->setColor(c);
19 if (strokeWidth < 0) {
20 paint->setStyle(SkPaint::kFill_Style);
21 } else {
22 paint->setStyle(SkPaint::kStroke_Style);
23 paint->setStrokeWidth(strokeWidth);
24 }
25 }
26
27 class ShadowsGM : public GM {
28 public:
29 SkPath fCirclePath;
30 SkRect fRect;
31 SkBitmap fBitmap;
32
33 protected:
onOnceBeforeDraw()34 void onOnceBeforeDraw() override {
35 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
36 fCirclePath.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(10) );
37 fRect.set(SkIntToScalar(10), SkIntToScalar(10),
38 SkIntToScalar(30), SkIntToScalar(30));
39 fBitmap.allocPixels(SkImageInfo::Make(20, 20, SkColorType::kAlpha_8_SkColorType,
40 kPremul_SkAlphaType));
41 SkCanvas canvas(fBitmap);
42 canvas.clear(0x0);
43 SkPaint p;
44 canvas.drawRect(SkRect::MakeXYWH(10, 0, 10, 10), p);
45 canvas.drawRect(SkRect::MakeXYWH(0, 10, 10, 10), p);
46 }
47
onShortName()48 SkString onShortName() override {
49 return SkString("shadows");
50 }
51
onISize()52 SkISize onISize() override {
53 return SkISize::Make(200, 200);
54 }
55
onDraw(SkCanvas * canvas)56 void onDraw(SkCanvas* canvas) override {
57 SkDrawLooper* shadowLoopers[5];
58 shadowLoopers[0] =
59 SkBlurDrawLooper::Create(SK_ColorBLUE,
60 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
61 SkIntToScalar(5), SkIntToScalar(10),
62 SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
63 SkBlurDrawLooper::kOverrideColor_BlurFlag |
64 SkBlurDrawLooper::kHighQuality_BlurFlag);
65 SkAutoUnref aurL0(shadowLoopers[0]);
66 shadowLoopers[1] =
67 SkBlurDrawLooper::Create(SK_ColorBLUE,
68 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
69 SkIntToScalar(5), SkIntToScalar(10),
70 SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
71 SkBlurDrawLooper::kOverrideColor_BlurFlag);
72 SkAutoUnref aurL1(shadowLoopers[1]);
73 shadowLoopers[2] =
74 SkBlurDrawLooper::Create(SK_ColorBLACK,
75 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
76 SkIntToScalar(5),
77 SkIntToScalar(10),
78 SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
79 SkBlurDrawLooper::kHighQuality_BlurFlag);
80 SkAutoUnref aurL2(shadowLoopers[2]);
81 shadowLoopers[3] =
82 SkBlurDrawLooper::Create(0x7FFF0000,
83 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
84 SkIntToScalar(-5), SkIntToScalar(-10),
85 SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
86 SkBlurDrawLooper::kOverrideColor_BlurFlag |
87 SkBlurDrawLooper::kHighQuality_BlurFlag);
88 SkAutoUnref aurL3(shadowLoopers[3]);
89 shadowLoopers[4] =
90 SkBlurDrawLooper::Create(SK_ColorBLACK, SkIntToScalar(0),
91 SkIntToScalar(5), SkIntToScalar(5),
92 SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
93 SkBlurDrawLooper::kOverrideColor_BlurFlag |
94 SkBlurDrawLooper::kHighQuality_BlurFlag);
95 SkAutoUnref aurL4(shadowLoopers[4]);
96
97 static const struct {
98 SkColor fColor;
99 SkScalar fStrokeWidth;
100 } gRec[] = {
101 { SK_ColorRED, -SK_Scalar1 },
102 { SK_ColorGREEN, SkIntToScalar(4) },
103 { SK_ColorBLUE, SkIntToScalar(0)},
104 };
105
106 SkPaint paint;
107 paint.setAntiAlias(true);
108 for (size_t i = 0; i < SK_ARRAY_COUNT(shadowLoopers); ++i) {
109 SkAutoCanvasRestore acr(canvas, true);
110
111 paint.setLooper(shadowLoopers[i]);
112
113 canvas->translate(SkIntToScalar((unsigned int)i*40), SkIntToScalar(0));
114 setup(&paint, gRec[0].fColor, gRec[0].fStrokeWidth);
115 canvas->drawRect(fRect, paint);
116
117 canvas->translate(SkIntToScalar(0), SkIntToScalar(40));
118 setup(&paint, gRec[1].fColor, gRec[1].fStrokeWidth);
119 canvas->drawPath(fCirclePath, paint);
120
121 canvas->translate(SkIntToScalar(0), SkIntToScalar(40));
122 setup(&paint, gRec[2].fColor, gRec[2].fStrokeWidth);
123 canvas->drawPath(fCirclePath, paint);
124
125 // see bug.skia.org/562 (reference, draws correct)
126 canvas->translate(0, 40);
127 paint.setColor(SK_ColorBLACK);
128 canvas->drawBitmap(fBitmap, 10, 10, &paint);
129
130 canvas->translate(0, 40);
131 SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(
132 fBitmap, SkShader::kRepeat_TileMode,
133 SkShader::kRepeat_TileMode));
134
135 // see bug.skia.org/562 (shows bug as reported)
136 paint.setShader(shader);
137 paint.setStyle(SkPaint::kFill_Style);
138 canvas->drawRect(SkRect::MakeXYWH(10, 10, 20, 20), paint);
139 paint.setShader(nullptr);
140 }
141 }
142
143 private:
144 typedef GM INHERITED;
145 };
146
147 ///////////////////////////////////////////////////////////////////////////////
148
MyFactory(void *)149 static GM* MyFactory(void*) { return new ShadowsGM; }
150 static GMRegistry reg(MyFactory);
151
152 }
153