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 #include "SampleCode.h"
8 #include "SkBlurMask.h"
9 #include "SkBlurMaskFilter.h"
10 #include "SkCanvas.h"
11 #include "SkColorMatrixFilter.h"
12 #include "SkDiscretePathEffect.h"
13 #include "SkEmbossMaskFilter.h"
14 #include "SkGradientShader.h"
15 #include "SkPaint.h"
16 #include "SkView.h"
17 
18 
19 //#define COLOR 0xFFFF8844
20 #define COLOR 0xFF888888
21 
22 static void paint_proc0(SkPaint*) {
23 }
24 
25 static void paint_proc1(SkPaint* paint) {
26     paint->setMaskFilter(SkBlurMaskFilter::Make(
27                                 kNormal_SkBlurStyle,
28                                 SkBlurMask::ConvertRadiusToSigma(2)));
29 }
30 
31 static void paint_proc2(SkPaint* paint) {
32     paint->setMaskFilter(SkEmbossMaskFilter::Make(
33             SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
34             { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 64, 16 }));
35 }
36 
37 static void paint_proc3(SkPaint* paint) {
38     SkColor colors[] = { SK_ColorRED, COLOR, SK_ColorBLUE };
39     SkPoint pts[] = { { 3, 0 }, { 7, 5 } };
40     paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
41                                                   SkShader::kMirror_TileMode));
42 }
43 
44 static void paint_proc5(SkPaint* paint) {
45     paint_proc3(paint);
46     paint_proc2(paint);
47 }
48 
49 typedef void (*PaintProc)(SkPaint*);
50 const PaintProc gPaintProcs[] = {
51     paint_proc0,
52     paint_proc1,
53     paint_proc2,
54     paint_proc3,
55     paint_proc5,
56 };
57 
58 ///////////////////////////////////////////////////////////////////////////////
59 
60 class EffectsView : public SampleView {
61 public:
62     SkPath fPath;
63     SkPaint fPaint[SK_ARRAY_COUNT(gPaintProcs)];
64 
65     EffectsView() {
66         size_t i;
67         const float pts[] = {
68             0, 0,
69             10, 0,
70             10, 5,
71             20, -5,
72             10, -15,
73             10, -10,
74             0, -10
75         };
76         fPath.moveTo(pts[0], pts[1]);
77         for (i = 2; i < SK_ARRAY_COUNT(pts); i += 2) {
78             fPath.lineTo(pts[i], pts[i+1]);
79         }
80 
81         for (i = 0; i < SK_ARRAY_COUNT(gPaintProcs); i++) {
82             fPaint[i].setAntiAlias(true);
83             fPaint[i].setColor(COLOR);
84             gPaintProcs[i](&fPaint[i]);
85         }
86 
87         SkColorMatrix cm;
88         cm.setRotate(SkColorMatrix::kG_Axis, 180);
89         cm.setIdentity();
90 
91         this->setBGColor(0xFFDDDDDD);
92     }
93 
94 protected:
95     // overrides from SkEventSink
96     virtual bool onQuery(SkEvent* evt) {
97         if (SampleCode::TitleQ(*evt)) {
98             SampleCode::TitleR(evt, "Effects");
99             return true;
100         }
101         return this->INHERITED::onQuery(evt);
102     }
103 
104     virtual void onDrawContent(SkCanvas* canvas) {
105         canvas->scale(3, 3);
106         canvas->translate(10, 30);
107         for (size_t i = 0; i < SK_ARRAY_COUNT(fPaint); i++) {
108             canvas->drawPath(fPath, fPaint[i]);
109             canvas->translate(32, 0);
110         }
111     }
112 
113 private:
114     typedef SampleView INHERITED;
115 };
116 
117 ///////////////////////////////////////////////////////////////////////////////
118 
119 static SkView* MyFactory() { return new EffectsView; }
120 static SkViewRegister reg(MyFactory);
121