blob: fead71473fee1b0016c3496f26e1ca68893c8847 [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.coma3c05882009-04-02 20:49:39 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12
reed@android.com1c12abe2009-07-02 15:01:02 +000013static SkShader* setgrad(const SkRect& r, SkColor c0, SkColor c1) {
14 SkColor colors[] = { c0, c1 };
15 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } };
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +000016 return SkGradientShader::CreateLinear(pts, colors, NULL, 2,
17 SkShader::kClamp_TileMode, NULL);
reed@android.com1c12abe2009-07-02 15:01:02 +000018}
19
20static void test_alphagradients(SkCanvas* canvas) {
21 SkRect r;
22 r.set(SkIntToScalar(10), SkIntToScalar(10),
23 SkIntToScalar(410), SkIntToScalar(30));
24 SkPaint p, p2;
25 p2.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +000026
reed@android.com1c12abe2009-07-02 15:01:02 +000027 p.setShader(setgrad(r, 0xFF00FF00, 0x0000FF00))->unref();
28 canvas->drawRect(r, p);
29 canvas->drawRect(r, p2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000030
reed@android.com1c12abe2009-07-02 15:01:02 +000031 r.offset(0, r.height() + SkIntToScalar(4));
32 p.setShader(setgrad(r, 0xFF00FF00, 0x00000000))->unref();
33 canvas->drawRect(r, p);
34 canvas->drawRect(r, p2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000035
reed@android.com1c12abe2009-07-02 15:01:02 +000036 r.offset(0, r.height() + SkIntToScalar(4));
37 p.setShader(setgrad(r, 0xFF00FF00, 0x00FF0000))->unref();
38 canvas->drawRect(r, p);
39 canvas->drawRect(r, p2);
40}
41
42///////////////////////////////////////////////////////////////////////////////
43
reed@android.coma3c05882009-04-02 20:49:39 +000044struct GradData {
45 int fCount;
46 const SkColor* fColors;
47 const SkScalar* fPos;
48};
49
50static const SkColor gColors[] = {
51 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
52};
53static const SkScalar gPos0[] = { 0, SK_Scalar1 };
54static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
55static const SkScalar gPos2[] = {
56 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
57};
58
59static const GradData gGradData[] = {
60 { 2, gColors, NULL },
61 { 2, gColors, gPos0 },
62 { 2, gColors, gPos1 },
63 { 5, gColors, NULL },
64 { 5, gColors, gPos2 }
65};
66
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +000067static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data,
68 SkShader::TileMode tm, SkUnitMapper* mapper) {
69 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos,
70 data.fCount, tm, mapper);
reed@android.coma3c05882009-04-02 20:49:39 +000071}
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +000073static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data,
74 SkShader::TileMode tm, SkUnitMapper* mapper) {
reed@android.coma3c05882009-04-02 20:49:39 +000075 SkPoint center;
76 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
77 SkScalarAve(pts[0].fY, pts[1].fY));
78 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +000079 data.fPos, data.fCount, tm, mapper);
reed@android.coma3c05882009-04-02 20:49:39 +000080}
81
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +000082static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data,
83 SkShader::TileMode tm, SkUnitMapper* mapper) {
reed@android.coma3c05882009-04-02 20:49:39 +000084 SkPoint center;
85 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
86 SkScalarAve(pts[0].fY, pts[1].fY));
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +000087 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors,
88 data.fPos, data.fCount, mapper);
reed@android.coma3c05882009-04-02 20:49:39 +000089}
90
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +000091static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data,
92 SkShader::TileMode tm, SkUnitMapper* mapper) {
reed@android.com8e2af7e2009-09-22 20:14:42 +000093 SkPoint center0, center1;
94 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
95 SkScalarAve(pts[0].fY, pts[1].fY));
96 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
97 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
98 return SkGradientShader::CreateTwoPointRadial(
99 center1, (pts[1].fX - pts[0].fX) / 7,
100 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +0000101 data.fColors, data.fPos, data.fCount, tm, mapper);
reed@android.com8e2af7e2009-09-22 20:14:42 +0000102}
103
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000104static SkShader* Make2RadialConcentric(const SkPoint pts[2], const GradData& data,
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +0000105 SkShader::TileMode tm, SkUnitMapper* mapper) {
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000106 SkPoint center;
107 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
108 SkScalarAve(pts[0].fY, pts[1].fY));
109 return SkGradientShader::CreateTwoPointRadial(
110 center, (pts[1].fX - pts[0].fX) / 7,
111 center, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +0000112 data.fColors, data.fPos, data.fCount, tm, mapper);
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000113}
114
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +0000115typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data,
116 SkShader::TileMode tm, SkUnitMapper* mapper);
reed@android.coma3c05882009-04-02 20:49:39 +0000117static const GradMaker gGradMakers[] = {
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000118 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2RadialConcentric
reed@android.coma3c05882009-04-02 20:49:39 +0000119};
120
121///////////////////////////////////////////////////////////////////////////////
122
reed@google.comf2183392011-04-22 14:10:48 +0000123class GradientsView : public SampleView {
reed@android.coma3c05882009-04-02 20:49:39 +0000124public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125 GradientsView() {
reed@google.comf2183392011-04-22 14:10:48 +0000126 this->setBGColor(0xFFDDDDDD);
127 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
reed@android.coma3c05882009-04-02 20:49:39 +0000129protected:
130 // overrides from SkEventSink
131 virtual bool onQuery(SkEvent* evt) {
132 if (SampleCode::TitleQ(*evt)) {
133 SampleCode::TitleR(evt, "Gradients");
134 return true;
135 }
136 return this->INHERITED::onQuery(evt);
137 }
138
reed@google.comf2183392011-04-22 14:10:48 +0000139 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.coma3c05882009-04-02 20:49:39 +0000140 SkPoint pts[2] = {
141 { 0, 0 },
142 { SkIntToScalar(100), SkIntToScalar(100) }
143 };
reed@android.coma3c05882009-04-02 20:49:39 +0000144 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
145 SkPaint paint;
reed@android.com32a42492009-07-10 03:33:52 +0000146 paint.setDither(true);
reed@android.com1c12abe2009-07-02 15:01:02 +0000147
148 canvas->save();
reed@android.com8e2af7e2009-09-22 20:14:42 +0000149 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000150
151 for (int tm = 0; tm < SkShader::kTileModeCount; ++tm) {
reed@android.coma3c05882009-04-02 20:49:39 +0000152 canvas->save();
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000153 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
154 canvas->save();
155 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
156 SkShader* shader;
commit-bot@chromium.orge2b193c2014-05-16 13:49:08 +0000157 shader = gGradMakers[j](pts, gGradData[i], (SkShader::TileMode)tm, NULL);
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000158 paint.setShader(shader)->unref();
159 canvas->drawRect(r, paint);
160 canvas->translate(0, SkIntToScalar(120));
161 }
162 canvas->restore();
163 canvas->translate(SkIntToScalar(120), 0);
reed@android.coma3c05882009-04-02 20:49:39 +0000164 }
165 canvas->restore();
bsalomon@google.comdc66c422011-04-06 19:35:37 +0000166 canvas->translate(SK_ARRAY_COUNT(gGradData)*SkIntToScalar(120), 0);
reed@android.coma3c05882009-04-02 20:49:39 +0000167 }
reed@android.com1c12abe2009-07-02 15:01:02 +0000168 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000169
reed@android.com1c12abe2009-07-02 15:01:02 +0000170 canvas->translate(0, SkIntToScalar(370));
caryclark@google.com02939ce2012-06-06 12:09:51 +0000171 if (false) { // avoid bit rot, suppress warning
172 test_alphagradients(canvas);
173 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000174 this->inval(NULL);
reed@android.coma3c05882009-04-02 20:49:39 +0000175 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000176
reed@android.coma3c05882009-04-02 20:49:39 +0000177private:
reed@google.comf2183392011-04-22 14:10:48 +0000178 typedef SampleView INHERITED;
reed@android.coma3c05882009-04-02 20:49:39 +0000179};
180
181///////////////////////////////////////////////////////////////////////////////
182
183static SkView* MyFactory() { return new GradientsView; }
184static SkViewRegister reg(MyFactory);