blob: 503f8f5b1eaafa191fc83cab876a7543b0f610d3 [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 */
reed1a9b9642016-03-13 14:13:58 -07007
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +00008#include "gm.h"
9#include "SkGradientShader.h"
10
11namespace skiagm {
12
reed1a9b9642016-03-13 14:13:58 -070013static sk_sp<SkShader> MakeLinear(SkScalar width, SkScalar height, bool alternate,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000014 const SkMatrix& localMatrix) {
reed1a9b9642016-03-13 14:13:58 -070015 SkPoint pts[2] = { {0, 0}, {width, height}};
16 SkColor colors[2] = {SK_ColorRED, SK_ColorGREEN};
17 if (alternate) {
18 pts[1].fY = 0;
19 colors[0] = SK_ColorBLUE;
20 colors[1] = SK_ColorYELLOW;
21 }
22 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode,
23 0, &localMatrix);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000024}
25
26///////////////////////////////////////////////////////////////////////////////
27
28class ShaderBoundsGM : public GM {
29public:
reed1a9b9642016-03-13 14:13:58 -070030 typedef sk_sp<SkShader> (*ShaderGenFunc)(SkScalar width, SkScalar height,
31 bool alternate, const SkMatrix& localMatrix);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000032 ShaderBoundsGM(ShaderGenFunc maker, const SkString& name)
33 : fShaderMaker(maker),
34 fName(name) {
35 }
36
37protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000038
mtklein36352bf2015-03-25 18:17:31 -070039 SkString onShortName() override {
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000040 return fName;
41 }
42
mtklein36352bf2015-03-25 18:17:31 -070043 SkISize onISize() override { return SkISize::Make(320, 240); }
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000044
mtklein36352bf2015-03-25 18:17:31 -070045 SkMatrix onGetInitialTransform() const override {
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000046 SkMatrix result;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000047 SkScalar scale = 0.8f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000048 result.setScale(scale, scale);
49 result.postTranslate(SkIntToScalar(7), SkIntToScalar(23));
50 return result;
51 }
52
mtklein36352bf2015-03-25 18:17:31 -070053 void onDraw(SkCanvas* canvas) override {
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000054 // The PDF device has already clipped to the content area, but we
55 // do it again here so that the raster and pdf results are consistent.
56 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(320),
57 SkIntToScalar(240)));
58
59 SkMatrix canvasScale;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000060 SkScalar scale = 0.7f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000061 canvasScale.setScale(scale, scale);
62 canvas->concat(canvasScale);
63
64 // Background shader.
65 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -070066 paint.setShader(MakeShader(559, 387, false));
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000067 SkRect r = SkRect::MakeXYWH(SkIntToScalar(-12), SkIntToScalar(-41),
68 SkIntToScalar(571), SkIntToScalar(428));
69 canvas->drawRect(r, paint);
70
71 // Constrained shader.
reed1a9b9642016-03-13 14:13:58 -070072 paint.setShader(MakeShader(101, 151, true));
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000073 r = SkRect::MakeXYWH(SkIntToScalar(43), SkIntToScalar(71),
74 SkIntToScalar(101), SkIntToScalar(151));
75 canvas->clipRect(r);
76 canvas->drawRect(r, paint);
77 }
78
reed1a9b9642016-03-13 14:13:58 -070079 sk_sp<SkShader> MakeShader(int width, int height, bool background) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000080 SkScalar scale = 0.5f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000081 if (background) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000082 scale = 0.6f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000083 }
reed80ea19c2015-05-12 10:37:34 -070084 SkScalar shaderWidth = width / scale;
85 SkScalar shaderHeight = height / scale;
86 SkMatrix shaderScale = SkMatrix::MakeScale(scale);
reed1a9b9642016-03-13 14:13:58 -070087 return fShaderMaker(shaderWidth, shaderHeight, background, shaderScale);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000088 }
89
90private:
91 typedef GM INHERITED;
92
93 ShaderGenFunc fShaderMaker;
94 SkString fName;
95
reed1a9b9642016-03-13 14:13:58 -070096 sk_sp<SkShader> MakeShader(bool background);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000097};
98
99///////////////////////////////////////////////////////////////////////////////
100
101static GM* MyFactory(void*) {
102 return new ShaderBoundsGM(MakeLinear, SkString("shaderbounds_linear"));
103}
104static GMRegistry reg(MyFactory);
105
106}