blob: fd42973dba6295a049b7e9f07b92ea7b185ac588 [file] [log] [blame]
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +00001/*
2 * Copyright 2012 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 */
7#include "gm.h"
8#include "SkGradientShader.h"
9
10namespace skiagm {
11
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000012static SkShader* MakeLinear(SkScalar width, SkScalar height, bool alternate,
13 const SkMatrix& localMatrix) {
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000014 SkPoint pts[2] = { {0, 0}, {width, height}};
15 SkColor colors[2] = {SK_ColorRED, SK_ColorGREEN};
16 if (alternate) {
17 pts[1].fY = 0;
18 colors[0] = SK_ColorBLUE;
19 colors[1] = SK_ColorYELLOW;
20 }
halcanary96fcdcc2015-08-27 07:41:13 -070021 return SkGradientShader::CreateLinear(pts, colors, nullptr, 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000022 SkShader::kClamp_TileMode, 0, &localMatrix);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000023}
24
25///////////////////////////////////////////////////////////////////////////////
26
27class ShaderBoundsGM : public GM {
28public:
29 typedef SkShader* (*ShaderGenFunc)(SkScalar width, SkScalar height,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000030 bool alternate, const SkMatrix& localMatrix);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000031 ShaderBoundsGM(ShaderGenFunc maker, const SkString& name)
32 : fShaderMaker(maker),
33 fName(name) {
34 }
35
36protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000037
mtklein36352bf2015-03-25 18:17:31 -070038 SkString onShortName() override {
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000039 return fName;
40 }
41
mtklein36352bf2015-03-25 18:17:31 -070042 SkISize onISize() override { return SkISize::Make(320, 240); }
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000043
mtklein36352bf2015-03-25 18:17:31 -070044 SkMatrix onGetInitialTransform() const override {
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000045 SkMatrix result;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000046 SkScalar scale = 0.8f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000047 result.setScale(scale, scale);
48 result.postTranslate(SkIntToScalar(7), SkIntToScalar(23));
49 return result;
50 }
51
mtklein36352bf2015-03-25 18:17:31 -070052 void onDraw(SkCanvas* canvas) override {
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000053 // The PDF device has already clipped to the content area, but we
54 // do it again here so that the raster and pdf results are consistent.
55 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(320),
56 SkIntToScalar(240)));
57
58 SkMatrix canvasScale;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000059 SkScalar scale = 0.7f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000060 canvasScale.setScale(scale, scale);
61 canvas->concat(canvasScale);
62
63 // Background shader.
64 SkPaint paint;
65 paint.setShader(MakeShader(559, 387, false))->unref();
66 SkRect r = SkRect::MakeXYWH(SkIntToScalar(-12), SkIntToScalar(-41),
67 SkIntToScalar(571), SkIntToScalar(428));
68 canvas->drawRect(r, paint);
69
70 // Constrained shader.
71 paint.setShader(MakeShader(101, 151, true))->unref();
72 r = SkRect::MakeXYWH(SkIntToScalar(43), SkIntToScalar(71),
73 SkIntToScalar(101), SkIntToScalar(151));
74 canvas->clipRect(r);
75 canvas->drawRect(r, paint);
76 }
77
78 SkShader* MakeShader(int width, int height, bool background) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000079 SkScalar scale = 0.5f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000080 if (background) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000081 scale = 0.6f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000082 }
reed80ea19c2015-05-12 10:37:34 -070083 SkScalar shaderWidth = width / scale;
84 SkScalar shaderHeight = height / scale;
85 SkMatrix shaderScale = SkMatrix::MakeScale(scale);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000086 SkShader* shader = fShaderMaker(shaderWidth, shaderHeight, background, shaderScale);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000087 return shader;
88 }
89
90private:
91 typedef GM INHERITED;
92
93 ShaderGenFunc fShaderMaker;
94 SkString fName;
95
96 SkShader* MakeShader(bool background);
97};
98
99///////////////////////////////////////////////////////////////////////////////
100
101static GM* MyFactory(void*) {
102 return new ShaderBoundsGM(MakeLinear, SkString("shaderbounds_linear"));
103}
104static GMRegistry reg(MyFactory);
105
106}