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