commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
| 8 | #include "SkCanvas.h" |
| 9 | #include "SkColor.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | #include "SkMatrix.h" |
| 12 | #include "SkPaint.h" |
| 13 | #include "SkPoint.h" |
| 14 | #include "SkRect.h" |
| 15 | #include "SkRefCnt.h" |
| 16 | #include "SkScalar.h" |
| 17 | #include "SkSize.h" |
| 18 | #include "SkString.h" |
| 19 | |
| 20 | #include "gm.h" |
| 21 | |
| 22 | static const SkColor gColors[] = { |
| 23 | SK_ColorRED, SK_ColorYELLOW |
| 24 | }; |
| 25 | |
| 26 | // these arrays define the gradient stop points |
| 27 | // as x1, y1, x2, y2 per gradient to draw |
| 28 | static const SkPoint linearPts[][2] = { |
| 29 | {{0, 0}, {1, 0}}, |
| 30 | {{0, 0}, {0, 1}}, |
| 31 | {{1, 0}, {0, 0}}, |
| 32 | {{0, 1}, {0, 0}}, |
| 33 | |
| 34 | {{0, 0}, {1, 1}}, |
| 35 | {{1, 1}, {0, 0}}, |
| 36 | {{1, 0}, {0, 1}}, |
| 37 | {{0, 1}, {1, 0}} |
| 38 | }; |
| 39 | |
commit-bot@chromium.org | 1ac1cf6 | 2013-06-12 21:47:39 +0000 | [diff] [blame^] | 40 | static const SkPoint radialPts[][2] = { |
| 41 | {{0, 0.5}, {1, 0.5}}, |
| 42 | {{0.5, 0 }, {0.5, 1 }}, |
| 43 | {{1, 0.5}, {0, 0.5}}, |
| 44 | {{0.5, 1 }, {0.5, 0 }}, |
| 45 | |
| 46 | {{0, 0}, {1, 1}}, |
| 47 | {{1, 1}, {0, 0}}, |
| 48 | {{1, 0}, {0, 1}}, |
| 49 | {{0, 1}, {1, 0}} |
| 50 | }; |
| 51 | |
| 52 | |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 53 | static const SkScalar TESTGRID_X = 200; // pixels allocated to each image in x dimension |
| 54 | static const SkScalar TESTGRID_Y = 200; // pixels allocated to each image in y dimension |
| 55 | |
| 56 | static const int IMAGES_X = 4; // number of images per row |
| 57 | |
| 58 | static SkShader* make_linear_gradient(const SkPoint pts[2]) { |
| 59 | return SkGradientShader::CreateLinear(pts, gColors, NULL, SK_ARRAY_COUNT(gColors), |
| 60 | SkShader::kClamp_TileMode, NULL); |
| 61 | } |
| 62 | |
commit-bot@chromium.org | 1ac1cf6 | 2013-06-12 21:47:39 +0000 | [diff] [blame^] | 63 | static SkShader* make_radial_gradient(const SkPoint pts[2]) { |
| 64 | SkPoint center; |
| 65 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 66 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 67 | float radius = (center - pts[0]).length(); |
| 68 | return SkGradientShader::CreateRadial(center, radius, gColors, NULL, SK_ARRAY_COUNT(gColors), |
| 69 | SkShader::kClamp_TileMode, NULL); |
| 70 | } |
| 71 | |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 72 | static void draw_gradients(SkCanvas* canvas, SkShader* (*makeShader)(const SkPoint[2]), |
commit-bot@chromium.org | 1ac1cf6 | 2013-06-12 21:47:39 +0000 | [diff] [blame^] | 73 | const SkPoint ptsArray[][2], int numImages) { |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 74 | // Use some nice prime numbers for the rectangle and matrix with |
| 75 | // different scaling along the x and y axes (which is the bug this |
| 76 | // test addresses, where incorrect order of operations mixed up the axes) |
| 77 | SkRect rectGrad = { 43, 61, 181, 167 }; |
| 78 | SkMatrix shaderMat; |
| 79 | shaderMat.setScale(rectGrad.width(), rectGrad.height()); |
| 80 | shaderMat.postTranslate(rectGrad.left(), rectGrad.top()); |
| 81 | |
| 82 | canvas->save(); |
| 83 | for (int i = 0; i < numImages; i++) { |
| 84 | // Advance line downwards if necessary. |
| 85 | if (i % IMAGES_X == 0 && i != 0) { |
| 86 | canvas->restore(); |
| 87 | canvas->translate(0, TESTGRID_Y); |
| 88 | canvas->save(); |
| 89 | } |
| 90 | |
| 91 | // Setup shader and draw. |
| 92 | SkAutoTUnref<SkShader> shader(makeShader(*ptsArray)); |
| 93 | shader->setLocalMatrix(shaderMat); |
| 94 | |
| 95 | SkPaint paint; |
| 96 | paint.setShader(shader); |
| 97 | canvas->drawRect(rectGrad, paint); |
| 98 | |
| 99 | // Advance to next position. |
| 100 | canvas->translate(TESTGRID_X, 0); |
| 101 | ptsArray++; |
| 102 | } |
| 103 | canvas->restore(); |
| 104 | } |
| 105 | |
| 106 | namespace skiagm { |
| 107 | |
| 108 | class GradientMatrixGM : public GM { |
| 109 | public: |
| 110 | GradientMatrixGM() { |
| 111 | this->setBGColor(0xFFDDDDDD); |
| 112 | } |
| 113 | |
| 114 | protected: |
| 115 | SkString onShortName() SK_OVERRIDE { |
| 116 | return SkString("gradient_matrix"); |
| 117 | } |
| 118 | |
| 119 | virtual SkISize onISize() SK_OVERRIDE { |
| 120 | return SkISize::Make(800, 800); |
| 121 | } |
| 122 | |
| 123 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 124 | draw_gradients(canvas, &make_linear_gradient, |
| 125 | linearPts, SK_ARRAY_COUNT(linearPts)); |
commit-bot@chromium.org | 1ac1cf6 | 2013-06-12 21:47:39 +0000 | [diff] [blame^] | 126 | |
| 127 | canvas->translate(0, TESTGRID_Y); |
| 128 | |
| 129 | draw_gradients(canvas, &make_radial_gradient, |
| 130 | radialPts, SK_ARRAY_COUNT(radialPts)); |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | private: |
| 134 | typedef GM INHERITED; |
| 135 | }; |
| 136 | |
| 137 | DEF_GM( return new GradientMatrixGM; ) |
| 138 | } |