blob: bf83bae97e7add1ec5bcf780a47f50fba8d0c69c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.comba974cc2009-05-22 13:48:35 +00008#include "SampleCode.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
reed@android.comba974cc2009-05-22 13:48:35 +000011#include "SkView.h"
12
13#include "SkBlurMaskFilter.h"
14#include "SkColorMatrixFilter.h"
15#include "SkDiscretePathEffect.h"
16#include "SkGradientShader.h"
17
reed@android.com7d970c72010-04-22 16:07:49 +000018#include "SkEdgeClipper.h"
19
20static void test_edgeclipper() {
21 SkPoint pts[] = {
reed@google.com261b8e22011-04-14 17:53:24 +000022 { -8.38822452e+21f, -7.69721471e+19f },
23 { 1.57645875e+23f, 1.44634003e+21f },
24 { 1.61519691e+23f, 1.48208059e+21f },
25 { 3.13963584e+23f, 2.88057438e+21f }
reed@android.com7d970c72010-04-22 16:07:49 +000026 };
27 SkRect clip = { 0, 0, 300, 200 };
28
29 SkEdgeClipper clipper;
30 clipper.clipCubic(pts, clip);
31}
32
33///////////
34
reed@android.comba974cc2009-05-22 13:48:35 +000035//#define COLOR 0xFFFF8844
36#define COLOR 0xFF888888
37
38static void paint_proc0(SkPaint* paint) {
39}
40
41static void paint_proc1(SkPaint* paint) {
42 paint->setMaskFilter(SkBlurMaskFilter::Create(2,
43 SkBlurMaskFilter::kNormal_BlurStyle))->unref();
44}
45
46static void paint_proc2(SkPaint* paint) {
47 SkScalar dir[3] = { 1, 1, 1};
48 paint->setMaskFilter(
reed@google.com261b8e22011-04-14 17:53:24 +000049 SkBlurMaskFilter::CreateEmboss(dir, 0.1f, 0.05f, 1))->unref();
reed@android.comba974cc2009-05-22 13:48:35 +000050}
51
52static void paint_proc3(SkPaint* paint) {
53 SkColor colors[] = { SK_ColorRED, COLOR, SK_ColorBLUE };
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000054 SkPoint pts[] = { { 3, 0 }, { 7, 5 } };
reed@android.comba974cc2009-05-22 13:48:35 +000055 paint->setShader(SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors),
56 SkShader::kMirror_TileMode))->unref();
57}
58
59static void paint_proc5(SkPaint* paint) {
60 paint_proc3(paint);
61 paint_proc2(paint);
62}
63
64typedef void (*PaintProc)(SkPaint*);
65const PaintProc gPaintProcs[] = {
66 paint_proc0,
67 paint_proc1,
68 paint_proc2,
69 paint_proc3,
70 paint_proc5,
71};
72
73///////////////////////////////////////////////////////////////////////////////
74
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000075class EffectsView : public SampleView {
reed@android.comba974cc2009-05-22 13:48:35 +000076public:
77 SkPath fPath;
78 SkPaint fPaint[SK_ARRAY_COUNT(gPaintProcs)];
79
80 EffectsView() {
81 size_t i;
82 const float pts[] = {
83 0, 0,
84 10, 0,
85 10, 5,
86 20, -5,
87 10, -15,
88 10, -10,
89 0, -10
90 };
91 fPath.moveTo(pts[0], pts[1]);
92 for (i = 2; i < SK_ARRAY_COUNT(pts); i += 2) {
93 fPath.lineTo(pts[i], pts[i+1]);
94 }
95
96 for (i = 0; i < SK_ARRAY_COUNT(gPaintProcs); i++) {
97 fPaint[i].setAntiAlias(true);
98 fPaint[i].setColor(COLOR);
99 gPaintProcs[i](&fPaint[i]);
100 }
reed@android.com7d970c72010-04-22 16:07:49 +0000101
102 test_edgeclipper();
103 SkColorMatrix cm;
104 cm.setRotate(SkColorMatrix::kG_Axis, 180);
105 cm.setIdentity();
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000106
107 this->setBGColor(0xFFDDDDDD);
reed@android.comba974cc2009-05-22 13:48:35 +0000108 }
109
110protected:
111 // overrides from SkEventSink
112 virtual bool onQuery(SkEvent* evt) {
113 if (SampleCode::TitleQ(*evt)) {
114 SampleCode::TitleR(evt, "Effects");
115 return true;
116 }
117 return this->INHERITED::onQuery(evt);
118 }
119
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000120 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.comba974cc2009-05-22 13:48:35 +0000121 canvas->scale(3, 3);
122 canvas->translate(10, 30);
123 for (size_t i = 0; i < SK_ARRAY_COUNT(fPaint); i++) {
124 canvas->drawPath(fPath, fPaint[i]);
125 canvas->translate(32, 0);
126 }
127 }
128
129private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000130 typedef SampleView INHERITED;
reed@android.comba974cc2009-05-22 13:48:35 +0000131};
132
133///////////////////////////////////////////////////////////////////////////////
134
135static SkView* MyFactory() { return new EffectsView; }
136static SkViewRegister reg(MyFactory);