blob: ef9a7e9015e3b042c5804a93c10261136db0832e [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
12static SkShader* MakeLinear(SkScalar width, SkScalar height, bool alternate) {
13 SkPoint pts[2] = { {0, 0}, {width, height}};
14 SkColor colors[2] = {SK_ColorRED, SK_ColorGREEN};
15 if (alternate) {
16 pts[1].fY = 0;
17 colors[0] = SK_ColorBLUE;
18 colors[1] = SK_ColorYELLOW;
19 }
20 return SkGradientShader::CreateLinear(pts, colors, NULL, 2,
21 SkShader::kClamp_TileMode, NULL);
22}
23
24///////////////////////////////////////////////////////////////////////////////
25
26class ShaderBoundsGM : public GM {
27public:
28 typedef SkShader* (*ShaderGenFunc)(SkScalar width, SkScalar height,
29 bool alternate);
30 ShaderBoundsGM(ShaderGenFunc maker, const SkString& name)
31 : fShaderMaker(maker),
32 fName(name) {
33 }
34
35protected:
36 SkString onShortName() {
37 return fName;
38 }
39
40 virtual SkISize onISize() { return make_isize(320, 240); }
41
42 virtual SkMatrix onGetInitialTransform() const SK_OVERRIDE {
43 SkMatrix result;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000044 SkScalar scale = 0.8f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000045 result.setScale(scale, scale);
46 result.postTranslate(SkIntToScalar(7), SkIntToScalar(23));
47 return result;
48 }
49
50 virtual void onDraw(SkCanvas* canvas) {
51 // The PDF device has already clipped to the content area, but we
52 // do it again here so that the raster and pdf results are consistent.
53 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(320),
54 SkIntToScalar(240)));
55
56 SkMatrix canvasScale;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000057 SkScalar scale = 0.7f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000058 canvasScale.setScale(scale, scale);
59 canvas->concat(canvasScale);
60
61 // Background shader.
62 SkPaint paint;
63 paint.setShader(MakeShader(559, 387, false))->unref();
64 SkRect r = SkRect::MakeXYWH(SkIntToScalar(-12), SkIntToScalar(-41),
65 SkIntToScalar(571), SkIntToScalar(428));
66 canvas->drawRect(r, paint);
67
68 // Constrained shader.
69 paint.setShader(MakeShader(101, 151, true))->unref();
70 r = SkRect::MakeXYWH(SkIntToScalar(43), SkIntToScalar(71),
71 SkIntToScalar(101), SkIntToScalar(151));
72 canvas->clipRect(r);
73 canvas->drawRect(r, paint);
74 }
75
76 SkShader* MakeShader(int width, int height, bool background) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000077 SkScalar scale = 0.5f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000078 if (background) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000079 scale = 0.6f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000080 }
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000081 SkScalar shaderWidth = SkScalarDiv(SkIntToScalar(width), scale);
82 SkScalar shaderHeight = SkScalarDiv(SkIntToScalar(height), scale);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000083 SkShader* shader = fShaderMaker(shaderWidth, shaderHeight, background);
84 SkMatrix shaderScale;
85 shaderScale.setScale(scale, scale);
86 shader->setLocalMatrix(shaderScale);
87 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}