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 | */ |
| 7 | #include "gm.h" |
| 8 | #include "SkGradientShader.h" |
| 9 | |
| 10 | namespace skiagm { |
| 11 | |
| 12 | static 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 | |
| 26 | class ShaderBoundsGM : public GM { |
| 27 | public: |
| 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 | |
| 35 | protected: |
| 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; |
| 44 | SkScalar scale = SkFloatToScalar(0.8f); |
| 45 | 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; |
| 57 | SkScalar scale = SkFloatToScalar(0.7f); |
| 58 | 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) { |
| 77 | SkScalar scale = SkFloatToScalar(0.5f); |
| 78 | if (background) { |
| 79 | scale = SkFloatToScalar(0.6f); |
| 80 | } |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 81 | SkScalar shaderWidth = SkScalarDiv(SkIntToScalar(width), scale); |
| 82 | SkScalar shaderHeight = SkScalarDiv(SkIntToScalar(height), scale); |
vandebo@chromium.org | 79d3cb4 | 2012-03-21 17:34:30 +0000 | [diff] [blame] | 83 | SkShader* shader = fShaderMaker(shaderWidth, shaderHeight, background); |
| 84 | SkMatrix shaderScale; |
| 85 | shaderScale.setScale(scale, scale); |
| 86 | shader->setLocalMatrix(shaderScale); |
| 87 | return shader; |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | typedef GM INHERITED; |
| 92 | |
| 93 | ShaderGenFunc fShaderMaker; |
| 94 | SkString fName; |
| 95 | |
| 96 | SkShader* MakeShader(bool background); |
| 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 | } |