OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 #include "gm.h" | |
8 #include "SkGradientShader.h" | |
9 | |
10 SkColor gColors[] = { | |
11 SK_ColorRED, SK_ColorYELLOW | |
12 }; | |
13 | |
14 // these arrays define the gradient stop points | |
15 // as x1, y1, x2, y2 per gradient to draw | |
16 SkScalar linearPts[] = { | |
17 0, 0, 1, 0, | |
18 0, 0, 0, 1, | |
19 1, 0, 0, 0, | |
20 0, 1, 0, 0, | |
21 | |
22 0, 0, 1, 1, | |
23 1, 1, 0, 0, | |
24 1, 0, 0, 1, | |
25 0, 1, 1, 0 | |
26 }; | |
27 | |
28 const SkScalar TESTGRID_X = 200; // pixels allocated to each image in x dimen sion | |
29 const SkScalar TESTGRID_Y = 200; // pixels allocated to each image in y dimen sion | |
30 | |
31 const int IMAGES_X = 4; // number of images per row | |
32 | |
33 static SkShader* make_linear_gradient(SkPoint pts[2]) { | |
34 return SkGradientShader::CreateLinear(pts, gColors, NULL, SK_ARRAY_COUNT(gCo lors), | |
35 SkShader::kClamp_TileMode, NULL); | |
36 } | |
37 | |
38 void draw_gradients(SkCanvas* canvas, SkShader* (*makeShader)(SkPoint[2]), | |
39 SkScalar* ptsArray, int numImages) { | |
vandebo (ex-Chrome)
2013/06/11 22:11:05
nit: indent
ducky
2013/06/11 23:18:17
Done.
| |
40 for (int i = 0; i < numImages; i++) { | |
41 // advance line if necessary | |
42 if (i % IMAGES_X == 0 && i != 0) { | |
43 canvas->restore(); | |
44 canvas->translate(0, TESTGRID_Y); | |
45 canvas->save(); | |
46 } | |
47 | |
48 // unpack points, setup shader, draw | |
49 SkPoint pts[2] = { | |
vandebo (ex-Chrome)
2013/06/11 22:11:05
You shouldn't need this bit.
ducky
2013/06/11 23:18:17
Done.
| |
50 { *(ptsArray+0), *(ptsArray+1) }, | |
51 { *(ptsArray+2), *(ptsArray+3) } | |
52 }; | |
53 ptsArray += 4; | |
54 | |
55 SkMatrix shaderMat = SkMatrix(); | |
vandebo (ex-Chrome)
2013/06/11 22:11:05
Pull this stuff above the loop.
ducky
2013/06/11 23:18:17
Done.
| |
56 shaderMat.setAll(138, 0, 43, | |
57 0, 106, 61, | |
58 0, 0, 1); | |
59 SkRect rectGrad = { 43, 61, 181, 167 }; // some nice prime numbers | |
60 | |
61 SkShader* shader = makeShader(pts); | |
vandebo (ex-Chrome)
2013/06/11 22:11:05
use SkAutoTUnref
ducky
2013/06/11 23:18:17
Done.
| |
62 shader->setLocalMatrix(shaderMat); | |
63 | |
64 SkPaint paint; | |
65 paint.setShader(shader); | |
66 canvas->drawRect(rectGrad, paint); | |
67 | |
68 shader->unref(); | |
69 | |
70 // advance to next position | |
71 canvas->translate(TESTGRID_X, 0); | |
72 } | |
73 } | |
74 | |
75 namespace skiagm { | |
76 | |
77 class GradientMatrixGM : public GM { | |
78 public: | |
79 GradientMatrixGM() { | |
80 this->setBGColor(0xFFDDDDDD); | |
81 } | |
82 | |
83 protected: | |
84 SkString onShortName() { | |
85 return SkString("gradient_matrix"); | |
86 } | |
87 | |
88 virtual SkISize onISize() { | |
89 return SkISize::Make(800, 800); | |
90 } | |
91 | |
92 virtual void onDraw(SkCanvas* canvas) { | |
93 canvas->save(); | |
vandebo (ex-Chrome)
2013/06/11 22:11:05
Do the save restore around the for loop instead of
ducky
2013/06/11 23:18:17
Done.
| |
94 | |
95 draw_gradients(canvas, &make_linear_gradient, | |
96 linearPts, sizeof(linearPts) / sizeof(linearPts[0]) / 4); | |
vandebo (ex-Chrome)
2013/06/11 22:11:05
SK_ARRAY_COUNT()
ducky
2013/06/11 23:18:17
Done.
| |
97 | |
98 canvas->restore(); | |
99 } | |
100 | |
101 private: | |
102 typedef GM INHERITED; | |
103 }; | |
104 | |
105 static GM* MyFactory(void*) { return new GradientMatrixGM; } | |
106 static GMRegistry reg(MyFactory); | |
107 } | |
OLD | NEW |