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 #include "SkRefCnt.h" | 7 #include "SkRefCnt.h" |
8 | 8 |
9 #include "gm.h" | 9 #include "gm.h" |
10 #include "SkGradientShader.h" | 10 #include "SkGradientShader.h" |
11 | 11 |
12 SkColor gColors[] = { | 12 SkColor gColors[] = { |
13 SK_ColorRED, SK_ColorYELLOW | 13 SK_ColorRED, SK_ColorYELLOW |
14 }; | 14 }; |
15 | 15 |
16 // these arrays define the gradient stop points | 16 // these arrays define the gradient stop points |
17 // as x1, y1, x2, y2 per gradient to draw | 17 // as {{x1, y1}, {x2, y2}} per gradient to draw |
18 SkPoint linearPts[][2] = { | 18 SkPoint linearPts[][2] = { |
19 {{0, 0}, {1, 0}}, | 19 {{0, 0}, {1, 0}}, |
20 {{0, 0}, {0, 1}}, | 20 {{0, 0}, {0, 1}}, |
21 {{1, 0}, {0, 0}}, | 21 {{1, 0}, {0, 0}}, |
22 {{0, 1}, {0, 0}}, | 22 {{0, 1}, {0, 0}}, |
23 | 23 |
24 {{0, 0}, {1, 1}}, | 24 {{0, 0}, {1, 1}}, |
25 {{1, 1}, {0, 0}}, | 25 {{1, 1}, {0, 0}}, |
26 {{1, 0}, {0, 1}}, | 26 {{1, 0}, {0, 1}}, |
27 {{0, 1}, {1, 0}} | 27 {{0, 1}, {1, 0}} |
28 }; | 28 }; |
29 | 29 |
30 SkPoint radialPts[][2] = { | |
31 {{0, 0.5}, {1, 0.5}}, | |
32 {{0.5, 0 }, {0.5, 1 }}, | |
33 {{1, 0.5}, {0, 0.5}}, | |
34 {{0.5, 1 }, {0.5, 0 }}, | |
35 | |
36 {{0, 0}, {1, 1}}, | |
37 {{1, 1}, {0, 0}}, | |
38 {{1, 0}, {0, 1}}, | |
39 {{0, 1}, {1, 0}} | |
40 }; | |
41 | |
42 | |
30 const SkScalar TESTGRID_X = 200; // pixels allocated to each image in x dimen sion | 43 const SkScalar TESTGRID_X = 200; // pixels allocated to each image in x dimen sion |
31 const SkScalar TESTGRID_Y = 200; // pixels allocated to each image in y dimen sion | 44 const SkScalar TESTGRID_Y = 200; // pixels allocated to each image in y dimen sion |
32 | 45 |
33 const int IMAGES_X = 4; // number of images per row | 46 const int IMAGES_X = 4; // number of images per row |
34 | 47 |
35 static SkShader* make_linear_gradient(SkPoint pts[2]) { | 48 static SkShader* make_linear_gradient(SkPoint pts[2]) { |
36 return SkGradientShader::CreateLinear(pts, gColors, NULL, SK_ARRAY_COUNT(gCo lors), | 49 return SkGradientShader::CreateLinear(pts, gColors, NULL, SK_ARRAY_COUNT(gCo lors), |
37 SkShader::kClamp_TileMode, NULL); | 50 SkShader::kClamp_TileMode, NULL); |
38 } | 51 } |
39 | 52 |
53 static SkShader* make_radial_gradient(SkPoint pts[2]) { | |
54 SkPoint center; | |
55 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | |
56 SkScalarAve(pts[0].fY, pts[1].fY)); | |
57 float radius = (center - pts[0]).length(); | |
58 return SkGradientShader::CreateRadial(center, radius, gColors, NULL, SK_ARRA Y_COUNT(gColors), | |
59 SkShader::kClamp_TileMode, NULL); | |
60 } | |
61 | |
40 void draw_gradients(SkCanvas* canvas, SkShader* (*makeShader)(SkPoint[2]), | 62 void draw_gradients(SkCanvas* canvas, SkShader* (*makeShader)(SkPoint[2]), |
41 SkPoint ptsArray[][2], int numImages) { | 63 SkPoint ptsArray[][2], int numImages) { |
42 // common test parameters | 64 // common test parameters |
43 SkMatrix shaderMat = SkMatrix(); | 65 SkMatrix shaderMat = SkMatrix(); |
44 shaderMat.setAll(138, 0, 43, | 66 shaderMat.setAll(138, 0, 43, |
45 0, 106, 61, | 67 0, 106, 61, |
46 0, 0, 1); | 68 0, 0, 1); |
47 SkRect rectGrad = { 43, 61, 181, 167 }; // some nice prime numbers | 69 SkRect rectGrad = { 43, 61, 181, 167 }; // some nice prime numbers |
48 | 70 |
49 canvas->save(); | |
vandebo (ex-Chrome)
2013/06/12 16:28:03
These can stay here.
ducky
2013/06/12 18:24:06
Done.
| |
50 for (int i = 0; i < numImages; i++) { | 71 for (int i = 0; i < numImages; i++) { |
51 // advance line if necessary | 72 // advance line if necessary |
52 if (i % IMAGES_X == 0 && i != 0) { | 73 if (i % IMAGES_X == 0 && i != 0) { |
53 canvas->restore(); | 74 canvas->restore(); |
54 canvas->translate(0, TESTGRID_Y); | 75 canvas->translate(0, TESTGRID_Y); |
55 canvas->save(); | 76 canvas->save(); |
56 } | 77 } |
57 | 78 |
58 // unpack points, setup shader, draw | 79 // unpack points, setup shader, draw |
59 SkAutoTUnref<SkShader> shader(makeShader(*ptsArray)); | 80 SkAutoTUnref<SkShader> shader(makeShader(*ptsArray)); |
60 shader->setLocalMatrix(shaderMat); | 81 shader->setLocalMatrix(shaderMat); |
61 | 82 |
62 SkPaint paint; | 83 SkPaint paint; |
63 paint.setShader(shader); | 84 paint.setShader(shader); |
64 canvas->drawRect(rectGrad, paint); | 85 canvas->drawRect(rectGrad, paint); |
65 | 86 |
66 // advance to next position | 87 // advance to next position |
67 canvas->translate(TESTGRID_X, 0); | 88 canvas->translate(TESTGRID_X, 0); |
68 ptsArray++; | 89 ptsArray++; |
69 } | 90 } |
70 canvas->restore(); | |
71 } | 91 } |
72 | 92 |
73 namespace skiagm { | 93 namespace skiagm { |
74 | 94 |
75 class GradientMatrixGM : public GM { | 95 class GradientMatrixGM : public GM { |
76 public: | 96 public: |
77 GradientMatrixGM() { | 97 GradientMatrixGM() { |
78 this->setBGColor(0xFFDDDDDD); | 98 this->setBGColor(0xFFDDDDDD); |
79 } | 99 } |
80 | 100 |
81 protected: | 101 protected: |
82 SkString onShortName() { | 102 SkString onShortName() { |
83 return SkString("gradient_matrix"); | 103 return SkString("gradient_matrix"); |
84 } | 104 } |
85 | 105 |
86 virtual SkISize onISize() { | 106 virtual SkISize onISize() { |
87 return SkISize::Make(800, 800); | 107 return SkISize::Make(800, 800); |
88 } | 108 } |
89 | 109 |
90 virtual void onDraw(SkCanvas* canvas) { | 110 virtual void onDraw(SkCanvas* canvas) { |
111 canvas->save(); | |
112 | |
91 draw_gradients(canvas, &make_linear_gradient, | 113 draw_gradients(canvas, &make_linear_gradient, |
92 linearPts, SK_ARRAY_COUNT(linearPts)); | 114 linearPts, SK_ARRAY_COUNT(linearPts)); |
115 | |
116 canvas->restore(); | |
117 canvas->translate(0, TESTGRID_Y); | |
118 canvas->save(); | |
119 | |
120 draw_gradients(canvas, &make_radial_gradient, | |
121 radialPts, SK_ARRAY_COUNT(radialPts)); | |
122 | |
123 canvas->restore(); | |
93 } | 124 } |
94 | 125 |
95 private: | 126 private: |
96 typedef GM INHERITED; | 127 typedef GM INHERITED; |
97 }; | 128 }; |
98 | 129 |
99 static GM* MyFactory(void*) { return new GradientMatrixGM; } | 130 static GM* MyFactory(void*) { return new GradientMatrixGM; } |
100 static GMRegistry reg(MyFactory); | 131 static GMRegistry reg(MyFactory); |
101 } | 132 } |
OLD | NEW |