blob: 55e334cb3a0753429b5bce2a64e504ab5c6d7132 [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 }
21 return SkGradientShader::CreateLinear(pts, colors, NULL, 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 virtual uint32_t onGetFlags() const SK_OVERRIDE {
38 return kSkipTiled_Flag;
39 }
40
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000041 SkString onShortName() {
42 return fName;
43 }
44
tfarinaf5393182014-06-09 23:59:03 -070045 virtual SkISize onISize() { return SkISize::Make(320, 240); }
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000046
47 virtual SkMatrix onGetInitialTransform() const SK_OVERRIDE {
48 SkMatrix result;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000049 SkScalar scale = 0.8f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000050 result.setScale(scale, scale);
51 result.postTranslate(SkIntToScalar(7), SkIntToScalar(23));
52 return result;
53 }
54
55 virtual void onDraw(SkCanvas* canvas) {
56 // The PDF device has already clipped to the content area, but we
57 // do it again here so that the raster and pdf results are consistent.
58 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(320),
59 SkIntToScalar(240)));
60
61 SkMatrix canvasScale;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000062 SkScalar scale = 0.7f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000063 canvasScale.setScale(scale, scale);
64 canvas->concat(canvasScale);
65
66 // Background shader.
67 SkPaint paint;
68 paint.setShader(MakeShader(559, 387, false))->unref();
69 SkRect r = SkRect::MakeXYWH(SkIntToScalar(-12), SkIntToScalar(-41),
70 SkIntToScalar(571), SkIntToScalar(428));
71 canvas->drawRect(r, paint);
72
73 // Constrained shader.
74 paint.setShader(MakeShader(101, 151, true))->unref();
75 r = SkRect::MakeXYWH(SkIntToScalar(43), SkIntToScalar(71),
76 SkIntToScalar(101), SkIntToScalar(151));
77 canvas->clipRect(r);
78 canvas->drawRect(r, paint);
79 }
80
81 SkShader* MakeShader(int width, int height, bool background) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000082 SkScalar scale = 0.5f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000083 if (background) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000084 scale = 0.6f;
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000085 }
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000086 SkScalar shaderWidth = SkScalarDiv(SkIntToScalar(width), scale);
87 SkScalar shaderHeight = SkScalarDiv(SkIntToScalar(height), scale);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000088 SkMatrix shaderScale;
89 shaderScale.setScale(scale, scale);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000090 SkShader* shader = fShaderMaker(shaderWidth, shaderHeight, background, shaderScale);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +000091 return shader;
92 }
93
94private:
95 typedef GM INHERITED;
96
97 ShaderGenFunc fShaderMaker;
98 SkString fName;
99
100 SkShader* MakeShader(bool background);
101};
102
103///////////////////////////////////////////////////////////////////////////////
104
105static GM* MyFactory(void*) {
106 return new ShaderBoundsGM(MakeLinear, SkString("shaderbounds_linear"));
107}
108static GMRegistry reg(MyFactory);
109
110}