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 "include/core/SkCanvas.h"
8 #include "include/core/SkPaint.h"
9 #include "include/effects/SkColorMatrixFilter.h"
10 #include "include/effects/SkDiscretePathEffect.h"
11 #include "include/effects/SkGradientShader.h"
12 #include "samplecode/Sample.h"
13 #include "src/core/SkBlurMask.h"
14 #include "src/effects/SkEmbossMaskFilter.h"
15 
16 
17 //#define COLOR 0xFFFF8844
18 #define COLOR 0xFF888888
19 
paint_proc0(SkPaint *)20 static void paint_proc0(SkPaint*) {
21 }
22 
paint_proc1(SkPaint * paint)23 static void paint_proc1(SkPaint* paint) {
24     paint->setMaskFilter(SkMaskFilter::MakeBlur(
25                                 kNormal_SkBlurStyle,
26                                 SkBlurMask::ConvertRadiusToSigma(2)));
27 }
28 
paint_proc2(SkPaint * paint)29 static void paint_proc2(SkPaint* paint) {
30     paint->setMaskFilter(SkEmbossMaskFilter::Make(
31             SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
32             { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 64, 16 }));
33 }
34 
paint_proc3(SkPaint * paint)35 static void paint_proc3(SkPaint* paint) {
36     SkColor colors[] = { SK_ColorRED, COLOR, SK_ColorBLUE };
37     SkPoint pts[] = { { 3, 0 }, { 7, 5 } };
38     paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
39                                                   SkTileMode::kMirror));
40 }
41 
paint_proc5(SkPaint * paint)42 static void paint_proc5(SkPaint* paint) {
43     paint_proc3(paint);
44     paint_proc2(paint);
45 }
46 
47 typedef void (*PaintProc)(SkPaint*);
48 const PaintProc gPaintProcs[] = {
49     paint_proc0,
50     paint_proc1,
51     paint_proc2,
52     paint_proc3,
53     paint_proc5,
54 };
55 
56 ///////////////////////////////////////////////////////////////////////////////
57 
58 class EffectsView : public Sample {
59 public:
60     SkPath fPath;
61     SkPaint fPaint[SK_ARRAY_COUNT(gPaintProcs)];
62 
EffectsView()63     EffectsView() {
64         size_t i;
65         const float pts[] = {
66             0, 0,
67             10, 0,
68             10, 5,
69             20, -5,
70             10, -15,
71             10, -10,
72             0, -10
73         };
74         fPath.moveTo(pts[0], pts[1]);
75         for (i = 2; i < SK_ARRAY_COUNT(pts); i += 2) {
76             fPath.lineTo(pts[i], pts[i+1]);
77         }
78 
79         for (i = 0; i < SK_ARRAY_COUNT(gPaintProcs); i++) {
80             fPaint[i].setAntiAlias(true);
81             fPaint[i].setColor(COLOR);
82             gPaintProcs[i](&fPaint[i]);
83         }
84 
85         this->setBGColor(0xFFDDDDDD);
86     }
87 
88 protected:
name()89     virtual SkString name() { return SkString("Effects"); }
90 
onDrawContent(SkCanvas * canvas)91     virtual void onDrawContent(SkCanvas* canvas) {
92         canvas->scale(3, 3);
93         canvas->translate(10, 30);
94         for (size_t i = 0; i < SK_ARRAY_COUNT(fPaint); i++) {
95             canvas->drawPath(fPath, fPaint[i]);
96             canvas->translate(32, 0);
97         }
98     }
99 
100 private:
101     typedef Sample INHERITED;
102 };
103 
104 ///////////////////////////////////////////////////////////////////////////////
105 
106 DEF_SAMPLE( return new EffectsView(); )
107