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