blob: deba9e55e99f2768916ee7d7b0eb3b331f4ba559 [file] [log] [blame]
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +00001/*
2 * Copyright 2011 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
8#include "gm.h"
9#include "SkCanvas.h"
cwallezc12b74d2015-01-26 07:45:53 -080010#include "SkColorFilterImageFilter.h"
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000011#include "SkGradientShader.h"
12#include "SkTableColorFilter.h"
13
reeddb873d82015-03-01 19:53:47 -080014static SkShader* make_shader0(int w, int h) {
15 SkPoint pts[] = { {0, 0}, {SkIntToScalar(w), SkIntToScalar(h)} };
16 SkColor colors[] = {
17 SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN,
18 SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE
19 };
20 return SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors),
21 SkShader::kClamp_TileMode);
22}
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000023static void make_bm0(SkBitmap* bm) {
24 int W = 120;
25 int H = 120;
reed@google.comeb9a46c2014-01-25 16:46:20 +000026 bm->allocN32Pixels(W, H);
junov@google.comdbfac8a2012-12-06 21:47:40 +000027 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000028
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000029 SkCanvas canvas(*bm);
30 SkPaint paint;
reeddb873d82015-03-01 19:53:47 -080031 paint.setShader(make_shader0(W, H))->unref();
reed5bd055c2015-03-01 19:16:38 -080032 canvas.drawPaint(paint);
reedb675a732015-03-01 18:00:47 -080033}
reeddb873d82015-03-01 19:53:47 -080034static SkShader* make_shader1(int w, int h) {
35 SkScalar cx = SkIntToScalar(w)/2;
36 SkScalar cy = SkIntToScalar(h)/2;
37 SkColor colors[] = {
38 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
39 };
40 return SkGradientShader::CreateRadial(SkPoint::Make(cx, cy), cx, colors, NULL,
41 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode);
42}
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000043static void make_bm1(SkBitmap* bm) {
44 int W = 120;
45 int H = 120;
reeddb873d82015-03-01 19:53:47 -080046 SkScalar cx = SkIntToScalar(W)/2;
47 SkScalar cy = SkIntToScalar(H)/2;
reed@google.comeb9a46c2014-01-25 16:46:20 +000048 bm->allocN32Pixels(W, H);
junov@google.comdbfac8a2012-12-06 21:47:40 +000049 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000050
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000051 SkCanvas canvas(*bm);
52 SkPaint paint;
reeddb873d82015-03-01 19:53:47 -080053 paint.setShader(make_shader1(W, H))->unref();
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000054 paint.setAntiAlias(true);
55 canvas.drawCircle(cx, cy, cx, paint);
56}
57
58static void make_table0(uint8_t table[]) {
59 for (int i = 0; i < 256; ++i) {
60 int n = i >> 5;
61 table[i] = (n << 5) | (n << 2) | (n >> 1);
62 }
63}
64static void make_table1(uint8_t table[]) {
65 for (int i = 0; i < 256; ++i) {
66 table[i] = i * i / 255;
67 }
68}
69static void make_table2(uint8_t table[]) {
70 for (int i = 0; i < 256; ++i) {
71 float fi = i / 255.0f;
robertphillips@google.com6853e802012-04-16 15:50:18 +000072 table[i] = static_cast<uint8_t>(sqrtf(fi) * 255);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000073 }
74}
75
cwallezc12b74d2015-01-26 07:45:53 -080076static SkColorFilter* make_null_cf() {
77 return NULL;
78}
79
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000080static SkColorFilter* make_cf0() {
81 uint8_t table[256]; make_table0(table);
82 return SkTableColorFilter::Create(table);
83}
84static SkColorFilter* make_cf1() {
85 uint8_t table[256]; make_table1(table);
86 return SkTableColorFilter::Create(table);
87}
88static SkColorFilter* make_cf2() {
89 uint8_t table[256]; make_table2(table);
90 return SkTableColorFilter::Create(table);
91}
92static SkColorFilter* make_cf3() {
93 uint8_t table0[256]; make_table0(table0);
94 uint8_t table1[256]; make_table1(table1);
95 uint8_t table2[256]; make_table2(table2);
96 return SkTableColorFilter::CreateARGB(NULL, table0, table1, table2);
97}
98
99class TableColorFilterGM : public skiagm::GM {
100public:
101 TableColorFilterGM() {}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000102
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000103protected:
104 virtual SkString onShortName() {
105 return SkString("tablecolorfilter");
106 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000107
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000108 virtual SkISize onISize() {
cwallezc12b74d2015-01-26 07:45:53 -0800109 return SkISize::Make(700, 1650);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000110 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000111
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000112 virtual void onDraw(SkCanvas* canvas) {
113 canvas->drawColor(0xFFDDDDDD);
114 canvas->translate(20, 20);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000115
cwallezc12b74d2015-01-26 07:45:53 -0800116
117 static SkColorFilter* (*gColorFilterMakers[])() = { make_null_cf, make_cf0, make_cf1,
118 make_cf2, make_cf3 };
119 static void (*gBitmapMakers[])(SkBitmap*) = { make_bm0, make_bm1 };
120
121 // This test will be done once for each bitmap with the results stacked vertically.
122 // For a single bitmap the resulting image will be the following:
123 // - A first line with the original bitmap, followed by the image drawn once
124 // with each of the N color filters
125 // - N lines of the bitmap drawn N times, this will cover all N*N combinations of
126 // pair of color filters in order to test the collpsing of consecutive table
127 // color filters.
128 //
129 // Here is a graphical representation of the result for 2 bitmaps and 2 filters
130 // with the number corresponding to the number of filters the bitmap goes through:
131 //
132 // --bitmap1
133 // 011
134 // 22
135 // 22
136 // --bitmap2
137 // 011
138 // 22
139 // 22
140
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000141 SkScalar x = 0, y = 0;
cwallezc12b74d2015-01-26 07:45:53 -0800142 for (size_t bitmapMaker = 0; bitmapMaker < SK_ARRAY_COUNT(gBitmapMakers); ++bitmapMaker) {
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000143 SkBitmap bm;
cwallezc12b74d2015-01-26 07:45:53 -0800144 gBitmapMakers[bitmapMaker](&bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000145
cwallezc12b74d2015-01-26 07:45:53 -0800146 SkScalar xOffset = SkScalar(bm.width() * 9 / 8);
147 SkScalar yOffset = SkScalar(bm.height() * 9 / 8);
148
149 // Draw the first element of the first line
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000150 x = 0;
cwallezc12b74d2015-01-26 07:45:53 -0800151 SkPaint paint;
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000152 canvas->drawBitmap(bm, x, y, &paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000153
cwallezc12b74d2015-01-26 07:45:53 -0800154 // Draws the rest of the first line for this bitmap
155 // each draw being at xOffset of the previous one
156 for (unsigned i = 1; i < SK_ARRAY_COUNT(gColorFilterMakers); ++i) {
157 x += xOffset;
158 paint.setColorFilter(gColorFilterMakers[i]())->unref();
159 canvas->drawBitmap(bm, x, y, &paint);
160 }
161
162 paint.setColorFilter(NULL);
163
164 for (unsigned i = 0; i < SK_ARRAY_COUNT(gColorFilterMakers); ++i) {
165 SkAutoTUnref<SkColorFilter> colorFilter1(gColorFilterMakers[i]());
166 SkAutoTUnref<SkImageFilter> imageFilter1(SkColorFilterImageFilter::Create(
senorblanco24e06d52015-03-18 12:11:33 -0700167 colorFilter1, NULL, NULL));
cwallezc12b74d2015-01-26 07:45:53 -0800168
169 // Move down to the next line and draw it
170 // each draw being at xOffset of the previous one
171 y += yOffset;
172 x = 0;
173 for (unsigned j = 1; j < SK_ARRAY_COUNT(gColorFilterMakers); ++j) {
174 SkAutoTUnref<SkColorFilter> colorFilter2(gColorFilterMakers[j]());
175 SkAutoTUnref<SkImageFilter> imageFilter2(SkColorFilterImageFilter::Create(
senorblanco24e06d52015-03-18 12:11:33 -0700176 colorFilter2, imageFilter1, NULL));
cwallezc12b74d2015-01-26 07:45:53 -0800177 paint.setImageFilter(imageFilter2);
178 canvas->drawBitmap(bm, x, y, &paint);
179 x += xOffset;
180 }
181 }
182
183 // Move down one line to the beginning of the block for next bitmap
184 y += yOffset;
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000185 }
186 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000187
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000188private:
189 typedef GM INHERITED;
190};
reeddb873d82015-03-01 19:53:47 -0800191DEF_GM( return new TableColorFilterGM; )
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000192
193//////////////////////////////////////////////////////////////////////////////
194
reeddb873d82015-03-01 19:53:47 -0800195class ComposeColorFilterGM : public skiagm::GM {
reedcff10b22015-03-03 06:41:45 -0800196 enum {
197 COLOR_COUNT = 3,
198 MODE_COUNT = 4,
199 };
200 const SkColor* fColors;
201 const SkXfermode::Mode* fModes;
202 SkString fName;
203
reeddb873d82015-03-01 19:53:47 -0800204public:
reedcff10b22015-03-03 06:41:45 -0800205 ComposeColorFilterGM(const SkColor colors[], const SkXfermode::Mode modes[],
206 const char suffix[])
207 : fColors(colors), fModes(modes)
208 {
209 fName.printf("colorcomposefilter_%s", suffix);
210 }
reeddb873d82015-03-01 19:53:47 -0800211
212protected:
213 virtual SkString onShortName() {
reedcff10b22015-03-03 06:41:45 -0800214 return fName;
reeddb873d82015-03-01 19:53:47 -0800215 }
216
217 virtual SkISize onISize() {
reedcff10b22015-03-03 06:41:45 -0800218 return SkISize::Make(790, 790);
reeddb873d82015-03-01 19:53:47 -0800219 }
220
221 virtual void onDraw(SkCanvas* canvas) {
222 SkBitmap bm;
223 make_bm1(&bm);
224
225 canvas->drawColor(0xFFDDDDDD);
226
reedcff10b22015-03-03 06:41:45 -0800227 const int MODES = MODE_COUNT * COLOR_COUNT;
reeddb873d82015-03-01 19:53:47 -0800228 SkAutoTUnref<SkColorFilter> filters[MODES];
229 int index = 0;
reedcff10b22015-03-03 06:41:45 -0800230 for (int i = 0; i < MODE_COUNT; ++i) {
231 for (int j = 0; j < COLOR_COUNT; ++j) {
232 filters[index++].reset(SkColorFilter::CreateModeFilter(fColors[j], fModes[i]));
reeddb873d82015-03-01 19:53:47 -0800233 }
234 }
235
236 SkPaint paint;
237 paint.setShader(make_shader1(50, 50))->unref();
238 SkRect r = SkRect::MakeWH(50, 50);
239 const SkScalar spacer = 10;
240
241 canvas->translate(spacer, spacer);
242
reedcff10b22015-03-03 06:41:45 -0800243 canvas->drawRect(r, paint); // orig
244
245 for (int i = 0; i < MODES; ++i) {
246 paint.setColorFilter(filters[i]);
247
reeddb873d82015-03-01 19:53:47 -0800248 canvas->save();
reedcff10b22015-03-03 06:41:45 -0800249 canvas->translate((i + 1) * (r.width() + spacer), 0);
250 canvas->drawRect(r, paint);
251 canvas->restore();
252
253 canvas->save();
254 canvas->translate(0, (i + 1) * (r.width() + spacer));
255 canvas->drawRect(r, paint);
256 canvas->restore();
257 }
258
259 canvas->translate(r.width() + spacer, r.width() + spacer);
260
261 for (int y = 0; y < MODES; ++y) {
262 canvas->save();
263 for (int x = 0; x < MODES; ++x) {
reeddb873d82015-03-01 19:53:47 -0800264 SkAutoTUnref<SkColorFilter> compose(SkColorFilter::CreateComposeFilter(filters[y],
265 filters[x]));
266 paint.setColorFilter(compose);
267 canvas->drawRect(r, paint);
268 canvas->translate(r.width() + spacer, 0);
269 }
270 canvas->restore();
271 canvas->translate(0, r.height() + spacer);
272 }
273 }
274
275private:
276 typedef GM INHERITED;
277};
reeddb873d82015-03-01 19:53:47 -0800278
reedcff10b22015-03-03 06:41:45 -0800279const SkColor gColors0[] = { SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW };
280const SkXfermode::Mode gModes0[] = {
281 SkXfermode::kOverlay_Mode,
282 SkXfermode::kDarken_Mode,
283 SkXfermode::kColorBurn_Mode,
284 SkXfermode::kExclusion_Mode,
285};
286DEF_GM( return new ComposeColorFilterGM(gColors0, gModes0, "wacky"); )
287
288const SkColor gColors1[] = { 0x80FF0000, 0x8000FF00, 0x800000FF };
289const SkXfermode::Mode gModes1[] = {
290 SkXfermode::kSrcOver_Mode,
291 SkXfermode::kXor_Mode,
292 SkXfermode::kDstOut_Mode,
293 SkXfermode::kSrcATop_Mode,
294};
295DEF_GM( return new ComposeColorFilterGM(gColors1, gModes1, "alpha"); )