OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "gm.h" | 8 #include "gm.h" |
9 #include "SkColorMatrixFilter.h" | 9 #include "SkColorMatrixFilter.h" |
10 #include "SkGradientShader.h" | 10 #include "SkGradientShader.h" |
11 #include "SkImage.h" | |
11 | 12 |
12 #define WIDTH 500 | 13 #define WIDTH 500 |
13 #define HEIGHT 500 | 14 #define HEIGHT 500 |
14 | 15 |
robertphillips
2015/07/07 12:23:59
Can we delete this guy now?
reed2
2015/07/07 12:58:24
Done.
| |
15 class SkDoOnce { | 16 class SkDoOnce { |
16 public: | 17 public: |
17 SkDoOnce() : fOnce(false) {}; | 18 SkDoOnce() : fOnce(false) {}; |
18 | 19 |
19 bool once() const { | 20 bool once() const { |
20 if (fOnce) { | 21 if (fOnce) { |
21 return false; | 22 return false; |
22 } | 23 } |
23 fOnce = true; | 24 fOnce = true; |
24 return true; | 25 return true; |
25 } | 26 } |
26 | 27 |
27 private: | 28 private: |
28 mutable bool fOnce; | 29 mutable bool fOnce; |
29 }; | 30 }; |
30 | 31 |
31 static void setColorMatrix(SkPaint* paint, const SkColorMatrix& matrix) { | 32 static void set_color_matrix(SkPaint* paint, const SkColorMatrix& matrix) { |
32 paint->setColorFilter(SkColorMatrixFilter::Create(matrix))->unref(); | 33 paint->setColorFilter(SkColorMatrixFilter::Create(matrix))->unref(); |
33 } | 34 } |
34 | 35 |
35 static void setArray(SkPaint* paint, const SkScalar array[]) { | 36 static void set_array(SkPaint* paint, const SkScalar array[]) { |
36 paint->setColorFilter(SkColorMatrixFilter::Create(array))->unref(); | 37 paint->setColorFilter(SkColorMatrixFilter::Create(array))->unref(); |
37 } | 38 } |
38 | 39 |
39 namespace skiagm { | 40 class ColorMatrixGM : public skiagm::GM { |
40 | |
41 class ColorMatrixGM : public GM { | |
42 SkDoOnce fOnce; | |
43 void init() { | |
44 if (fOnce.once()) { | |
45 fSolidBitmap = this->createSolidBitmap(64, 64); | |
46 fTransparentBitmap = this->createTransparentBitmap(64, 64); | |
47 } | |
48 } | |
49 | |
50 public: | 41 public: |
51 ColorMatrixGM() { | 42 ColorMatrixGM() { |
52 this->setBGColor(sk_tool_utils::color_to_565(0xFF808080)); | 43 this->setBGColor(sk_tool_utils::color_to_565(0xFF808080)); |
53 } | 44 } |
54 | 45 |
55 protected: | 46 protected: |
56 virtual SkString onShortName() { | 47 SkString onShortName() override { |
57 return SkString("colormatrix"); | 48 return SkString("colormatrix"); |
58 } | 49 } |
59 | 50 |
60 virtual SkISize onISize() { | 51 SkISize onISize() override { |
61 return SkISize::Make(WIDTH, HEIGHT); | 52 return SkISize::Make(WIDTH, HEIGHT); |
62 } | 53 } |
54 | |
55 void onOnceBeforeDraw() override { | |
56 fSolidImg.reset(CreateSolidBitmap(64, 64)); | |
57 fTransparentImg.reset(CreateTransparentBitmap(64, 64)); | |
58 } | |
63 | 59 |
64 SkBitmap createSolidBitmap(int width, int height) { | 60 static SkImage* CreateSolidBitmap(int width, int height) { |
65 SkBitmap bm; | 61 SkBitmap bm; |
66 bm.allocN32Pixels(width, height); | 62 bm.allocN32Pixels(width, height); |
67 SkCanvas canvas(bm); | 63 SkCanvas canvas(bm); |
68 canvas.clear(0x0); | 64 canvas.clear(0x0); |
69 for (int y = 0; y < height; ++y) { | 65 for (int y = 0; y < height; ++y) { |
70 for (int x = 0; x < width; ++x) { | 66 for (int x = 0; x < width; ++x) { |
71 SkPaint paint; | 67 SkPaint paint; |
72 paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / he ight, 0)); | 68 paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / he ight, 0)); |
73 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(x), | 69 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(x), |
74 SkIntToScalar(y), SK_Scalar1, SK_Scalar1), paint); | 70 SkIntToScalar(y), SK_Scalar1, SK_Scalar1), paint); |
75 } | 71 } |
76 } | 72 } |
77 return bm; | 73 return SkImage::NewFromBitmap(bm); |
78 } | 74 } |
79 | 75 |
80 // creates a bitmap with shades of transparent gray. | 76 // creates a bitmap with shades of transparent gray. |
81 SkBitmap createTransparentBitmap(int width, int height) { | 77 static SkImage* CreateTransparentBitmap(int width, int height) { |
82 SkBitmap bm; | 78 SkBitmap bm; |
83 bm.allocN32Pixels(width, height); | 79 bm.allocN32Pixels(width, height); |
84 SkCanvas canvas(bm); | 80 SkCanvas canvas(bm); |
85 canvas.clear(0x0); | 81 canvas.clear(0x0); |
86 | 82 |
87 SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}}; | 83 SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}}; |
88 SkColor colors[] = {0x00000000, 0xFFFFFFFF}; | 84 SkColor colors[] = {0x00000000, 0xFFFFFFFF}; |
89 SkPaint paint; | 85 SkPaint paint; |
90 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2, | 86 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
91 SkShader::kClamp_TileMode ))->unref(); | 87 SkShader::kClamp_TileMode ))->unref(); |
92 canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(heigh t)), paint); | 88 canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(heigh t)), paint); |
93 return bm; | 89 return SkImage::NewFromBitmap(bm); |
94 } | 90 } |
95 | 91 |
96 virtual void onDraw(SkCanvas* canvas) { | 92 void onDraw(SkCanvas* canvas) override { |
97 this->init(); | |
98 | |
99 SkPaint paint; | 93 SkPaint paint; |
100 SkColorMatrix matrix; | 94 SkColorMatrix matrix; |
101 | 95 |
102 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 96 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
103 const SkBitmap bmps[] = { fSolidBitmap, fTransparentBitmap }; | 97 const SkImage* bmps[] = { fSolidImg, fTransparentImg }; |
104 | 98 |
105 for (size_t i = 0; i < SK_ARRAY_COUNT(bmps); ++i) { | 99 for (size_t i = 0; i < SK_ARRAY_COUNT(bmps); ++i) { |
106 matrix.setIdentity(); | 100 matrix.setIdentity(); |
107 setColorMatrix(&paint, matrix); | 101 set_color_matrix(&paint, matrix); |
108 canvas->drawBitmap(bmps[i], 0, 0, &paint); | 102 canvas->drawImage(bmps[i], 0, 0, &paint); |
109 | 103 |
110 matrix.setRotate(SkColorMatrix::kR_Axis, 90); | 104 matrix.setRotate(SkColorMatrix::kR_Axis, 90); |
111 setColorMatrix(&paint, matrix); | 105 set_color_matrix(&paint, matrix); |
112 canvas->drawBitmap(bmps[i], 80, 0, &paint); | 106 canvas->drawImage(bmps[i], 80, 0, &paint); |
113 | 107 |
114 matrix.setRotate(SkColorMatrix::kG_Axis, 90); | 108 matrix.setRotate(SkColorMatrix::kG_Axis, 90); |
115 setColorMatrix(&paint, matrix); | 109 set_color_matrix(&paint, matrix); |
116 canvas->drawBitmap(bmps[i], 160, 0, &paint); | 110 canvas->drawImage(bmps[i], 160, 0, &paint); |
117 | 111 |
118 matrix.setRotate(SkColorMatrix::kB_Axis, 90); | 112 matrix.setRotate(SkColorMatrix::kB_Axis, 90); |
119 setColorMatrix(&paint, matrix); | 113 set_color_matrix(&paint, matrix); |
120 canvas->drawBitmap(bmps[i], 240, 0, &paint); | 114 canvas->drawImage(bmps[i], 240, 0, &paint); |
121 | 115 /////////////////////////////////////////////// |
122 matrix.setSaturation(0.0f); | 116 matrix.setSaturation(0.0f); |
123 setColorMatrix(&paint, matrix); | 117 set_color_matrix(&paint, matrix); |
124 canvas->drawBitmap(bmps[i], 0, 80, &paint); | 118 canvas->drawImage(bmps[i], 0, 80, &paint); |
125 | 119 |
126 matrix.setSaturation(0.5f); | 120 matrix.setSaturation(0.5f); |
127 setColorMatrix(&paint, matrix); | 121 set_color_matrix(&paint, matrix); |
128 canvas->drawBitmap(bmps[i], 80, 80, &paint); | 122 canvas->drawImage(bmps[i], 80, 80, &paint); |
129 | 123 |
130 matrix.setSaturation(1.0f); | 124 matrix.setSaturation(1.0f); |
131 setColorMatrix(&paint, matrix); | 125 set_color_matrix(&paint, matrix); |
132 canvas->drawBitmap(bmps[i], 160, 80, &paint); | 126 canvas->drawImage(bmps[i], 160, 80, &paint); |
133 | 127 |
134 matrix.setSaturation(2.0f); | 128 matrix.setSaturation(2.0f); |
135 setColorMatrix(&paint, matrix); | 129 set_color_matrix(&paint, matrix); |
136 canvas->drawBitmap(bmps[i], 240, 80, &paint); | 130 canvas->drawImage(bmps[i], 240, 80, &paint); |
137 | 131 /////////////////////////////////////////////// |
138 matrix.setRGB2YUV(); | 132 matrix.setRGB2YUV(); |
139 setColorMatrix(&paint, matrix); | 133 set_color_matrix(&paint, matrix); |
140 canvas->drawBitmap(bmps[i], 0, 160, &paint); | 134 canvas->drawImage(bmps[i], 0, 160, &paint); |
141 | 135 |
142 matrix.setYUV2RGB(); | 136 matrix.setYUV2RGB(); |
143 setColorMatrix(&paint, matrix); | 137 set_color_matrix(&paint, matrix); |
144 canvas->drawBitmap(bmps[i], 80, 160, &paint); | 138 canvas->drawImage(bmps[i], 80, 160, &paint); |
145 | 139 |
146 SkScalar s1 = SK_Scalar1; | 140 SkScalar s1 = SK_Scalar1; |
147 SkScalar s255 = SkIntToScalar(255); | 141 SkScalar s255 = SkIntToScalar(255); |
148 // Move red into alpha, set color to white | 142 // Move red into alpha, set color to white |
149 SkScalar data[20] = { | 143 SkScalar data[20] = { |
150 0, 0, 0, 0, s255, | 144 0, 0, 0, 0, s255, |
151 0, 0, 0, 0, s255, | 145 0, 0, 0, 0, s255, |
152 0, 0, 0, 0, s255, | 146 0, 0, 0, 0, s255, |
153 s1, 0, 0, 0, 0, | 147 s1, 0, 0, 0, 0, |
154 }; | 148 }; |
155 | 149 |
156 setArray(&paint, data); | 150 set_array(&paint, data); |
157 canvas->drawBitmap(bmps[i], 160, 160, &paint); | 151 canvas->drawImage(bmps[i], 160, 160, &paint); |
158 | 152 /////////////////////////////////////////////// |
159 canvas->translate(0, 240); | 153 canvas->translate(0, 240); |
160 } | 154 } |
161 } | 155 } |
162 | 156 |
163 private: | 157 private: |
164 SkBitmap fSolidBitmap; | 158 SkAutoTUnref<SkImage> fSolidImg; |
165 SkBitmap fTransparentBitmap; | 159 SkAutoTUnref<SkImage> fTransparentImg; |
166 typedef GM INHERITED; | 160 |
161 typedef skiagm::GM INHERITED; | |
167 }; | 162 }; |
163 DEF_GM( return new ColorMatrixGM; ) | |
168 | 164 |
169 ////////////////////////////////////////////////////////////////////////////// | |
170 | |
171 static GM* MyFactory(void*) { return new ColorMatrixGM; } | |
172 static GMRegistry reg(MyFactory); | |
173 | |
174 } | |
OLD | NEW |