vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 7 | |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 8 | #include "gm.h" |
| 9 | #include "SkGradientShader.h" |
| 10 | |
| 11 | namespace skiagm { |
| 12 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 13 | static sk_sp<SkShader> MakeLinear(SkScalar width, SkScalar height, bool alternate, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 14 | const SkMatrix& localMatrix) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 15 | 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.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
| 28 | class ShaderBoundsGM : public GM { |
| 29 | public: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 30 | typedef sk_sp<SkShader> (*ShaderGenFunc)(SkScalar width, SkScalar height, |
| 31 | bool alternate, const SkMatrix& localMatrix); |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 32 | ShaderBoundsGM(ShaderGenFunc maker, const SkString& name) |
| 33 | : fShaderMaker(maker), |
| 34 | fName(name) { |
| 35 | } |
| 36 | |
| 37 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | SkString onShortName() override { |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 40 | return fName; |
| 41 | } |
| 42 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | SkISize onISize() override { return SkISize::Make(320, 240); } |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 44 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 45 | SkMatrix onGetInitialTransform() const override { |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 46 | SkMatrix result; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 47 | SkScalar scale = 0.8f; |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 48 | result.setScale(scale, scale); |
| 49 | result.postTranslate(SkIntToScalar(7), SkIntToScalar(23)); |
| 50 | return result; |
| 51 | } |
| 52 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 53 | void onDraw(SkCanvas* canvas) override { |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 54 | // 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.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 60 | SkScalar scale = 0.7f; |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 61 | canvasScale.setScale(scale, scale); |
| 62 | canvas->concat(canvasScale); |
| 63 | |
| 64 | // Background shader. |
| 65 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 66 | paint.setShader(MakeShader(559, 387, false)); |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 67 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(-12), SkIntToScalar(-41), |
| 68 | SkIntToScalar(571), SkIntToScalar(428)); |
| 69 | canvas->drawRect(r, paint); |
| 70 | |
| 71 | // Constrained shader. |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 72 | paint.setShader(MakeShader(101, 151, true)); |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 73 | r = SkRect::MakeXYWH(SkIntToScalar(43), SkIntToScalar(71), |
| 74 | SkIntToScalar(101), SkIntToScalar(151)); |
| 75 | canvas->clipRect(r); |
| 76 | canvas->drawRect(r, paint); |
| 77 | } |
| 78 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 79 | sk_sp<SkShader> MakeShader(int width, int height, bool background) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 80 | SkScalar scale = 0.5f; |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 81 | if (background) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 82 | scale = 0.6f; |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 83 | } |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 84 | SkScalar shaderWidth = width / scale; |
| 85 | SkScalar shaderHeight = height / scale; |
| 86 | SkMatrix shaderScale = SkMatrix::MakeScale(scale); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 87 | return fShaderMaker(shaderWidth, shaderHeight, background, shaderScale); |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private: |
| 91 | typedef GM INHERITED; |
| 92 | |
| 93 | ShaderGenFunc fShaderMaker; |
| 94 | SkString fName; |
| 95 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 96 | sk_sp<SkShader> MakeShader(bool background); |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | |
| 101 | static GM* MyFactory(void*) { |
| 102 | return new ShaderBoundsGM(MakeLinear, SkString("shaderbounds_linear")); |
| 103 | } |
| 104 | static GMRegistry reg(MyFactory); |
| 105 | |
| 106 | } |