blob: 2acb15c677c3d50e2b58fbe2c5172666419c92db [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(
167 colorFilter1, NULL, NULL, 0));
168
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(
176 colorFilter2, imageFilter1, NULL, 0));
177 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 {
196public:
197 ComposeColorFilterGM() {}
198
199protected:
200 virtual SkString onShortName() {
201 return SkString("composecolorfilter");
202 }
203
204 virtual SkISize onISize() {
205 return SkISize::Make(730, 730);
206 }
207
208 virtual void onDraw(SkCanvas* canvas) {
209 SkBitmap bm;
210 make_bm1(&bm);
211
212 canvas->drawColor(0xFFDDDDDD);
213
214 SkColor colors[] = { SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW };
215 SkXfermode::Mode modes[] = {
216 SkXfermode::kOverlay_Mode,
217 SkXfermode::kDarken_Mode,
218 SkXfermode::kColorBurn_Mode,
219 SkXfermode::kExclusion_Mode,
220 };
221
222 const int MODES = SK_ARRAY_COUNT(modes) * SK_ARRAY_COUNT(colors);
223 SkAutoTUnref<SkColorFilter> filters[MODES];
224 int index = 0;
225 for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
226 for (size_t j = 0; j < SK_ARRAY_COUNT(colors); ++j) {
227 filters[index++].reset(SkColorFilter::CreateModeFilter(colors[j], modes[i]));
228 }
229 }
230
231 SkPaint paint;
232 paint.setShader(make_shader1(50, 50))->unref();
233 SkRect r = SkRect::MakeWH(50, 50);
234 const SkScalar spacer = 10;
235
236 canvas->translate(spacer, spacer);
237
238 for (size_t y = 0; y < MODES; ++y) {
239 canvas->save();
240 for (size_t x = 0; x < MODES; ++x) {
241 SkAutoTUnref<SkColorFilter> compose(SkColorFilter::CreateComposeFilter(filters[y],
242 filters[x]));
243 paint.setColorFilter(compose);
244 canvas->drawRect(r, paint);
245 canvas->translate(r.width() + spacer, 0);
246 }
247 canvas->restore();
248 canvas->translate(0, r.height() + spacer);
249 }
250 }
251
252private:
253 typedef GM INHERITED;
254};
255DEF_GM( return new ComposeColorFilterGM; )
256