blob: 8c6793eb6746063b563266b391b52d17f69049d6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed@android.comba974cc2009-05-22 13:48:35 +00007#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00008#include "SkBlurMask.h"
reed@android.comba974cc2009-05-22 13:48:35 +00009#include "SkBlurMaskFilter.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000010#include "SkCanvas.h"
reed@android.comba974cc2009-05-22 13:48:35 +000011#include "SkColorMatrixFilter.h"
12#include "SkDiscretePathEffect.h"
13#include "SkGradientShader.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000014#include "SkPaint.h"
15#include "SkView.h"
16
reed@android.comba974cc2009-05-22 13:48:35 +000017
18//#define COLOR 0xFFFF8844
19#define COLOR 0xFF888888
20
sugoi@google.com93c7ee32013-03-12 14:36:57 +000021static void paint_proc0(SkPaint*) {
reed@android.comba974cc2009-05-22 13:48:35 +000022}
23
24static void paint_proc1(SkPaint* paint) {
reedefdfd512016-04-04 10:02:58 -070025 paint->setMaskFilter(SkBlurMaskFilter::Make(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000026 kNormal_SkBlurStyle,
reedefdfd512016-04-04 10:02:58 -070027 SkBlurMask::ConvertRadiusToSigma(2)));
reed@android.comba974cc2009-05-22 13:48:35 +000028}
29
30static void paint_proc2(SkPaint* paint) {
Mike Reedfd87be82016-12-07 13:41:57 -050031#ifdef SK_SUPPORT_LEGACY_EMBOSSMASKFILTER
reed@android.comba974cc2009-05-22 13:48:35 +000032 SkScalar dir[3] = { 1, 1, 1};
33 paint->setMaskFilter(
reedefdfd512016-04-04 10:02:58 -070034 SkBlurMaskFilter::MakeEmboss(SkBlurMask::ConvertRadiusToSigma(1), dir, 0.1f, 0.05f));
Mike Reedfd87be82016-12-07 13:41:57 -050035#endif
reed@android.comba974cc2009-05-22 13:48:35 +000036}
37
38static void paint_proc3(SkPaint* paint) {
39 SkColor colors[] = { SK_ColorRED, COLOR, SK_ColorBLUE };
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000040 SkPoint pts[] = { { 3, 0 }, { 7, 5 } };
reed8a21c9f2016-03-08 18:50:00 -080041 paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
42 SkShader::kMirror_TileMode));
reed@android.comba974cc2009-05-22 13:48:35 +000043}
44
45static void paint_proc5(SkPaint* paint) {
46 paint_proc3(paint);
47 paint_proc2(paint);
48}
49
50typedef void (*PaintProc)(SkPaint*);
51const PaintProc gPaintProcs[] = {
52 paint_proc0,
53 paint_proc1,
54 paint_proc2,
55 paint_proc3,
56 paint_proc5,
57};
58
59///////////////////////////////////////////////////////////////////////////////
60
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000061class EffectsView : public SampleView {
reed@android.comba974cc2009-05-22 13:48:35 +000062public:
63 SkPath fPath;
64 SkPaint fPaint[SK_ARRAY_COUNT(gPaintProcs)];
65
rmistry@google.comae933ce2012-08-23 18:19:56 +000066 EffectsView() {
reed@android.comba974cc2009-05-22 13:48:35 +000067 size_t i;
68 const float pts[] = {
69 0, 0,
70 10, 0,
71 10, 5,
72 20, -5,
73 10, -15,
74 10, -10,
75 0, -10
76 };
77 fPath.moveTo(pts[0], pts[1]);
78 for (i = 2; i < SK_ARRAY_COUNT(pts); i += 2) {
79 fPath.lineTo(pts[i], pts[i+1]);
80 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000081
reed@android.comba974cc2009-05-22 13:48:35 +000082 for (i = 0; i < SK_ARRAY_COUNT(gPaintProcs); i++) {
83 fPaint[i].setAntiAlias(true);
84 fPaint[i].setColor(COLOR);
85 gPaintProcs[i](&fPaint[i]);
86 }
reed@android.com7d970c72010-04-22 16:07:49 +000087
reed@android.com7d970c72010-04-22 16:07:49 +000088 SkColorMatrix cm;
89 cm.setRotate(SkColorMatrix::kG_Axis, 180);
90 cm.setIdentity();
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000092 this->setBGColor(0xFFDDDDDD);
reed@android.comba974cc2009-05-22 13:48:35 +000093 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
reed@android.comba974cc2009-05-22 13:48:35 +000095protected:
96 // overrides from SkEventSink
97 virtual bool onQuery(SkEvent* evt) {
98 if (SampleCode::TitleQ(*evt)) {
99 SampleCode::TitleR(evt, "Effects");
100 return true;
101 }
102 return this->INHERITED::onQuery(evt);
103 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000104
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000105 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.comba974cc2009-05-22 13:48:35 +0000106 canvas->scale(3, 3);
107 canvas->translate(10, 30);
108 for (size_t i = 0; i < SK_ARRAY_COUNT(fPaint); i++) {
109 canvas->drawPath(fPath, fPaint[i]);
110 canvas->translate(32, 0);
111 }
112 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113
reed@android.comba974cc2009-05-22 13:48:35 +0000114private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000115 typedef SampleView INHERITED;
reed@android.comba974cc2009-05-22 13:48:35 +0000116};
117
118///////////////////////////////////////////////////////////////////////////////
119
120static SkView* MyFactory() { return new EffectsView; }
121static SkViewRegister reg(MyFactory);