OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
9 #include "SkColor.h" | 9 #include "SkColor.h" |
10 #include "SkGradientShader.h" | 10 #include "SkGradientShader.h" |
11 #include "SkMatrix.h" | 11 #include "SkMatrix.h" |
12 #include "SkPaint.h" | 12 #include "SkPaint.h" |
13 #include "SkPoint.h" | 13 #include "SkPoint.h" |
14 #include "SkRect.h" | 14 #include "SkRect.h" |
15 #include "SkRefCnt.h" | 15 #include "SkRefCnt.h" |
16 #include "SkScalar.h" | 16 #include "SkScalar.h" |
17 #include "SkSize.h" | 17 #include "SkSize.h" |
18 #include "SkString.h" | 18 #include "SkString.h" |
19 | 19 |
20 #include "gm.h" | 20 #include "gm.h" |
21 | 21 |
22 static const SkColor gColors[] = { | 22 static const SkColor gColors[] = { |
23 SK_ColorRED, SK_ColorYELLOW | 23 SK_ColorRED, SK_ColorYELLOW |
24 }; | 24 }; |
25 | 25 |
26 // these arrays define the gradient stop points | 26 // These annoying defines are necessary, because the only other alternative |
27 // as x1, y1, x2, y2 per gradient to draw | 27 // is to use SkIntToScalar(...) or SkFloatToScalar(...) everywhere. |
| 28 static const SkScalar sZero = 0; |
| 29 static const SkScalar sHalf = SK_ScalarHalf; |
| 30 static const SkScalar sOne = SK_Scalar1; |
| 31 |
| 32 // These arrays define the gradient stop points |
| 33 // as x1, y1, x2, y2 per gradient to draw. |
28 static const SkPoint linearPts[][2] = { | 34 static const SkPoint linearPts[][2] = { |
29 {{0, 0}, {1, 0}}, | 35 {{sZero, sZero}, {sOne, sZero}}, |
30 {{0, 0}, {0, 1}}, | 36 {{sZero, sZero}, {sZero, sOne}}, |
31 {{1, 0}, {0, 0}}, | 37 {{sOne, sZero}, {sZero, sZero}}, |
32 {{0, 1}, {0, 0}}, | 38 {{sZero, sOne}, {sZero, sZero}}, |
33 | 39 |
34 {{0, 0}, {1, 1}}, | 40 {{sZero, sZero}, {sOne, sOne}}, |
35 {{1, 1}, {0, 0}}, | 41 {{sOne, sOne}, {sZero, sZero}}, |
36 {{1, 0}, {0, 1}}, | 42 {{sOne, sZero}, {sZero, sOne}}, |
37 {{0, 1}, {1, 0}} | 43 {{sZero, sOne}, {sOne, sZero}} |
38 }; | 44 }; |
39 | 45 |
40 static const SkPoint radialPts[][2] = { | 46 static const SkPoint radialPts[][2] = { |
41 {{0, 0.5f}, {1, 0.5f}}, | 47 {{sZero, sHalf}, {sOne, sHalf}}, |
42 {{0.5f, 0 }, {0.5f, 1 }}, | 48 {{sHalf, sZero}, {sHalf, sOne}}, |
43 {{1, 0.5f}, {0, 0.5f}}, | 49 {{sOne, sHalf}, {sZero, sHalf}}, |
44 {{0.5f, 1 }, {0.5f, 0 }}, | 50 {{sHalf, sOne}, {sHalf, sZero}}, |
45 | 51 |
46 {{0, 0}, {1, 1}}, | 52 {{sZero, sZero}, {sOne, sOne}}, |
47 {{1, 1}, {0, 0}}, | 53 {{sOne, sOne}, {sZero, sZero}}, |
48 {{1, 0}, {0, 1}}, | 54 {{sOne, sZero}, {sZero, sOne}}, |
49 {{0, 1}, {1, 0}} | 55 {{sZero, sOne}, {sOne, sZero}} |
50 }; | 56 }; |
51 | 57 |
52 | 58 // These define the pixels allocated to each gradient image. |
53 static const SkScalar TESTGRID_X = 200; // pixels allocated to each image in
x dimension | 59 static const SkScalar TESTGRID_X = SkIntToScalar(200); |
54 static const SkScalar TESTGRID_Y = 200; // pixels allocated to each image in
y dimension | 60 static const SkScalar TESTGRID_Y = SkIntToScalar(200); |
55 | 61 |
56 static const int IMAGES_X = 4; // number of images per row | 62 static const int IMAGES_X = 4; // number of images per row |
57 | 63 |
58 static SkShader* make_linear_gradient(const SkPoint pts[2]) { | 64 static SkShader* make_linear_gradient(const SkPoint pts[2]) { |
59 return SkGradientShader::CreateLinear(pts, gColors, NULL, SK_ARRAY_COUNT(gCo
lors), | 65 return SkGradientShader::CreateLinear(pts, gColors, NULL, SK_ARRAY_COUNT(gCo
lors), |
60 SkShader::kClamp_TileMode, NULL); | 66 SkShader::kClamp_TileMode, NULL); |
61 } | 67 } |
62 | 68 |
63 static SkShader* make_radial_gradient(const SkPoint pts[2]) { | 69 static SkShader* make_radial_gradient(const SkPoint pts[2]) { |
64 SkPoint center; | 70 SkPoint center; |
65 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 71 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
66 SkScalarAve(pts[0].fY, pts[1].fY)); | 72 SkScalarAve(pts[0].fY, pts[1].fY)); |
67 float radius = (center - pts[0]).length(); | 73 float radius = (center - pts[0]).length(); |
68 return SkGradientShader::CreateRadial(center, radius, gColors, NULL, SK_ARRA
Y_COUNT(gColors), | 74 return SkGradientShader::CreateRadial(center, radius, gColors, NULL, SK_ARRA
Y_COUNT(gColors), |
69 SkShader::kClamp_TileMode, NULL); | 75 SkShader::kClamp_TileMode, NULL); |
70 } | 76 } |
71 | 77 |
72 static void draw_gradients(SkCanvas* canvas, SkShader* (*makeShader)(const SkPoi
nt[2]), | 78 static void draw_gradients(SkCanvas* canvas, SkShader* (*makeShader)(const SkPoi
nt[2]), |
73 const SkPoint ptsArray[][2], int numImages) { | 79 const SkPoint ptsArray[][2], int numImages) { |
74 // Use some nice prime numbers for the rectangle and matrix with | 80 // 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 | 81 // 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) | 82 // test addresses, where incorrect order of operations mixed up the axes) |
77 SkRect rectGrad = { 43, 61, 181, 167 }; | 83 SkRect rectGrad = { |
| 84 SkIntToScalar(43), SkIntToScalar(61), |
| 85 SkIntToScalar(181), SkIntToScalar(167) }; |
78 SkMatrix shaderMat; | 86 SkMatrix shaderMat; |
79 shaderMat.setScale(rectGrad.width(), rectGrad.height()); | 87 shaderMat.setScale(rectGrad.width(), rectGrad.height()); |
80 shaderMat.postTranslate(rectGrad.left(), rectGrad.top()); | 88 shaderMat.postTranslate(rectGrad.left(), rectGrad.top()); |
81 | 89 |
82 canvas->save(); | 90 canvas->save(); |
83 for (int i = 0; i < numImages; i++) { | 91 for (int i = 0; i < numImages; i++) { |
84 // Advance line downwards if necessary. | 92 // Advance line downwards if necessary. |
85 if (i % IMAGES_X == 0 && i != 0) { | 93 if (i % IMAGES_X == 0 && i != 0) { |
86 canvas->restore(); | 94 canvas->restore(); |
87 canvas->translate(0, TESTGRID_Y); | 95 canvas->translate(0, TESTGRID_Y); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 draw_gradients(canvas, &make_radial_gradient, | 137 draw_gradients(canvas, &make_radial_gradient, |
130 radialPts, SK_ARRAY_COUNT(radialPts)); | 138 radialPts, SK_ARRAY_COUNT(radialPts)); |
131 } | 139 } |
132 | 140 |
133 private: | 141 private: |
134 typedef GM INHERITED; | 142 typedef GM INHERITED; |
135 }; | 143 }; |
136 | 144 |
137 DEF_GM( return new GradientMatrixGM; ) | 145 DEF_GM( return new GradientMatrixGM; ) |
138 } | 146 } |
OLD | NEW |