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" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 {{sOne, sZero}, {sZero, sOne}}, | 54 {{sOne, sZero}, {sZero, sOne}}, |
55 {{sZero, sOne}, {sOne, sZero}} | 55 {{sZero, sOne}, {sOne, sZero}} |
56 }; | 56 }; |
57 | 57 |
58 // These define the pixels allocated to each gradient image. | 58 // These define the pixels allocated to each gradient image. |
59 static const SkScalar TESTGRID_X = SkIntToScalar(200); | 59 static const SkScalar TESTGRID_X = SkIntToScalar(200); |
60 static const SkScalar TESTGRID_Y = SkIntToScalar(200); | 60 static const SkScalar TESTGRID_Y = SkIntToScalar(200); |
61 | 61 |
62 static const int IMAGES_X = 4; // number of images per row | 62 static const int IMAGES_X = 4; // number of images per row |
63 | 63 |
64 static SkShader* make_linear_gradient(const SkPoint pts[2]) { | 64 static SkShader* make_linear_gradient(const SkPoint pts[2], const SkMatrix& loca
lMatrix) { |
65 return SkGradientShader::CreateLinear(pts, gColors, NULL, SK_ARRAY_COUNT(gCo
lors), | 65 return SkGradientShader::CreateLinear(pts, gColors, NULL, SK_ARRAY_COUNT(gCo
lors), |
66 SkShader::kClamp_TileMode, NULL); | 66 SkShader::kClamp_TileMode, NULL, 0, &l
ocalMatrix); |
67 } | 67 } |
68 | 68 |
69 static SkShader* make_radial_gradient(const SkPoint pts[2]) { | 69 static SkShader* make_radial_gradient(const SkPoint pts[2], const SkMatrix& loca
lMatrix) { |
70 SkPoint center; | 70 SkPoint center; |
71 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 71 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
72 SkScalarAve(pts[0].fY, pts[1].fY)); | 72 SkScalarAve(pts[0].fY, pts[1].fY)); |
73 float radius = (center - pts[0]).length(); | 73 float radius = (center - pts[0]).length(); |
74 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), |
75 SkShader::kClamp_TileMode, NULL); | 75 SkShader::kClamp_TileMode, NULL, 0, &l
ocalMatrix); |
76 } | 76 } |
77 | 77 |
78 static void draw_gradients(SkCanvas* canvas, SkShader* (*makeShader)(const SkPoi
nt[2]), | 78 static void draw_gradients(SkCanvas* canvas, |
| 79 SkShader* (*makeShader)(const SkPoint[2], const SkMat
rix&), |
79 const SkPoint ptsArray[][2], int numImages) { | 80 const SkPoint ptsArray[][2], int numImages) { |
80 // Use some nice prime numbers for the rectangle and matrix with | 81 // Use some nice prime numbers for the rectangle and matrix with |
81 // different scaling along the x and y axes (which is the bug this | 82 // different scaling along the x and y axes (which is the bug this |
82 // test addresses, where incorrect order of operations mixed up the axes) | 83 // test addresses, where incorrect order of operations mixed up the axes) |
83 SkRect rectGrad = { | 84 SkRect rectGrad = { |
84 SkIntToScalar(43), SkIntToScalar(61), | 85 SkIntToScalar(43), SkIntToScalar(61), |
85 SkIntToScalar(181), SkIntToScalar(167) }; | 86 SkIntToScalar(181), SkIntToScalar(167) }; |
86 SkMatrix shaderMat; | 87 SkMatrix shaderMat; |
87 shaderMat.setScale(rectGrad.width(), rectGrad.height()); | 88 shaderMat.setScale(rectGrad.width(), rectGrad.height()); |
88 shaderMat.postTranslate(rectGrad.left(), rectGrad.top()); | 89 shaderMat.postTranslate(rectGrad.left(), rectGrad.top()); |
89 | 90 |
90 canvas->save(); | 91 canvas->save(); |
91 for (int i = 0; i < numImages; i++) { | 92 for (int i = 0; i < numImages; i++) { |
92 // Advance line downwards if necessary. | 93 // Advance line downwards if necessary. |
93 if (i % IMAGES_X == 0 && i != 0) { | 94 if (i % IMAGES_X == 0 && i != 0) { |
94 canvas->restore(); | 95 canvas->restore(); |
95 canvas->translate(0, TESTGRID_Y); | 96 canvas->translate(0, TESTGRID_Y); |
96 canvas->save(); | 97 canvas->save(); |
97 } | 98 } |
98 | 99 |
99 // Setup shader and draw. | 100 // Setup shader and draw. |
100 SkAutoTUnref<SkShader> shader(makeShader(*ptsArray)); | 101 SkAutoTUnref<SkShader> shader(makeShader(*ptsArray, shaderMat)); |
101 shader->setLocalMatrix(shaderMat); | |
102 | 102 |
103 SkPaint paint; | 103 SkPaint paint; |
104 paint.setShader(shader); | 104 paint.setShader(shader); |
105 canvas->drawRect(rectGrad, paint); | 105 canvas->drawRect(rectGrad, paint); |
106 | 106 |
107 // Advance to next position. | 107 // Advance to next position. |
108 canvas->translate(TESTGRID_X, 0); | 108 canvas->translate(TESTGRID_X, 0); |
109 ptsArray++; | 109 ptsArray++; |
110 } | 110 } |
111 canvas->restore(); | 111 canvas->restore(); |
(...skipping 25 matching lines...) Expand all Loading... |
137 draw_gradients(canvas, &make_radial_gradient, | 137 draw_gradients(canvas, &make_radial_gradient, |
138 radialPts, SK_ARRAY_COUNT(radialPts)); | 138 radialPts, SK_ARRAY_COUNT(radialPts)); |
139 } | 139 } |
140 | 140 |
141 private: | 141 private: |
142 typedef GM INHERITED; | 142 typedef GM INHERITED; |
143 }; | 143 }; |
144 | 144 |
145 DEF_GM( return new GradientMatrixGM; ) | 145 DEF_GM( return new GradientMatrixGM; ) |
146 } | 146 } |
OLD | NEW |