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