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