blob: c1ff65e3d903e5c4e6c1f44ef390c8a3d1fbe6e9 [file] [log] [blame]
commit-bot@chromium.orgace22692013-06-12 21:33:02 +00001/*
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
22static 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
28static 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.org1ac1cf62013-06-12 21:47:39 +000040static 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.orgace22692013-06-12 21:33:02 +000053static const SkScalar TESTGRID_X = 200; // pixels allocated to each image in x dimension
54static const SkScalar TESTGRID_Y = 200; // pixels allocated to each image in y dimension
55
56static const int IMAGES_X = 4; // number of images per row
57
58static 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.org1ac1cf62013-06-12 21:47:39 +000063static 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.orgace22692013-06-12 21:33:02 +000072static void draw_gradients(SkCanvas* canvas, SkShader* (*makeShader)(const SkPoint[2]),
commit-bot@chromium.org1ac1cf62013-06-12 21:47:39 +000073 const SkPoint ptsArray[][2], int numImages) {
commit-bot@chromium.orgace22692013-06-12 21:33:02 +000074 // 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
106namespace skiagm {
107
108class GradientMatrixGM : public GM {
109public:
110 GradientMatrixGM() {
111 this->setBGColor(0xFFDDDDDD);
112 }
113
114protected:
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.org1ac1cf62013-06-12 21:47:39 +0000126
127 canvas->translate(0, TESTGRID_Y);
128
129 draw_gradients(canvas, &make_radial_gradient,
130 radialPts, SK_ARRAY_COUNT(radialPts));
commit-bot@chromium.orgace22692013-06-12 21:33:02 +0000131 }
132
133private:
134 typedef GM INHERITED;
135};
136
137DEF_GM( return new GradientMatrixGM; )
138}