blob: cc1683e08939a197b9c0f246031faba68c8d700d [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
40static const SkScalar TESTGRID_X = 200; // pixels allocated to each image in x dimension
41static const SkScalar TESTGRID_Y = 200; // pixels allocated to each image in y dimension
42
43static const int IMAGES_X = 4; // number of images per row
44
45static SkShader* make_linear_gradient(const SkPoint pts[2]) {
46 return SkGradientShader::CreateLinear(pts, gColors, NULL, SK_ARRAY_COUNT(gColors),
47 SkShader::kClamp_TileMode, NULL);
48}
49
50static void draw_gradients(SkCanvas* canvas, SkShader* (*makeShader)(const SkPoint[2]),
51 const SkPoint ptsArray[][2], int numImages) {
52 // Use some nice prime numbers for the rectangle and matrix with
53 // different scaling along the x and y axes (which is the bug this
54 // test addresses, where incorrect order of operations mixed up the axes)
55 SkRect rectGrad = { 43, 61, 181, 167 };
56 SkMatrix shaderMat;
57 shaderMat.setScale(rectGrad.width(), rectGrad.height());
58 shaderMat.postTranslate(rectGrad.left(), rectGrad.top());
59
60 canvas->save();
61 for (int i = 0; i < numImages; i++) {
62 // Advance line downwards if necessary.
63 if (i % IMAGES_X == 0 && i != 0) {
64 canvas->restore();
65 canvas->translate(0, TESTGRID_Y);
66 canvas->save();
67 }
68
69 // Setup shader and draw.
70 SkAutoTUnref<SkShader> shader(makeShader(*ptsArray));
71 shader->setLocalMatrix(shaderMat);
72
73 SkPaint paint;
74 paint.setShader(shader);
75 canvas->drawRect(rectGrad, paint);
76
77 // Advance to next position.
78 canvas->translate(TESTGRID_X, 0);
79 ptsArray++;
80 }
81 canvas->restore();
82}
83
84namespace skiagm {
85
86class GradientMatrixGM : public GM {
87public:
88 GradientMatrixGM() {
89 this->setBGColor(0xFFDDDDDD);
90 }
91
92protected:
93 SkString onShortName() SK_OVERRIDE {
94 return SkString("gradient_matrix");
95 }
96
97 virtual SkISize onISize() SK_OVERRIDE {
98 return SkISize::Make(800, 800);
99 }
100
101 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
102 draw_gradients(canvas, &make_linear_gradient,
103 linearPts, SK_ARRAY_COUNT(linearPts));
104 }
105
106private:
107 typedef GM INHERITED;
108};
109
110DEF_GM( return new GradientMatrixGM; )
111}