mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 1 | /* |
| 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" |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 10 | #include "SkColorFilterImageFilter.h" |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 11 | #include "SkGradientShader.h" |
| 12 | #include "SkTableColorFilter.h" |
| 13 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 14 | static 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 | }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 20 | return SkGradientShader::CreateLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 21 | SkShader::kClamp_TileMode); |
| 22 | } |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 23 | static void make_bm0(SkBitmap* bm) { |
| 24 | int W = 120; |
| 25 | int H = 120; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 26 | bm->allocN32Pixels(W, H); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 27 | bm->eraseColor(SK_ColorTRANSPARENT); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 28 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 29 | SkCanvas canvas(*bm); |
| 30 | SkPaint paint; |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 31 | paint.setShader(make_shader0(W, H))->unref(); |
reed | 5bd055c | 2015-03-01 19:16:38 -0800 | [diff] [blame] | 32 | canvas.drawPaint(paint); |
reed | b675a73 | 2015-03-01 18:00:47 -0800 | [diff] [blame] | 33 | } |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 34 | static 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 | }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 40 | return SkGradientShader::CreateRadial(SkPoint::Make(cx, cy), cx, colors, nullptr, |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 41 | SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode); |
| 42 | } |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 43 | static void make_bm1(SkBitmap* bm) { |
| 44 | int W = 120; |
| 45 | int H = 120; |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 46 | SkScalar cx = SkIntToScalar(W)/2; |
| 47 | SkScalar cy = SkIntToScalar(H)/2; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 48 | bm->allocN32Pixels(W, H); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 49 | bm->eraseColor(SK_ColorTRANSPARENT); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 50 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 51 | SkCanvas canvas(*bm); |
| 52 | SkPaint paint; |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 53 | paint.setShader(make_shader1(W, H))->unref(); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 54 | paint.setAntiAlias(true); |
| 55 | canvas.drawCircle(cx, cy, cx, paint); |
| 56 | } |
| 57 | |
| 58 | static 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 | } |
| 64 | static void make_table1(uint8_t table[]) { |
| 65 | for (int i = 0; i < 256; ++i) { |
| 66 | table[i] = i * i / 255; |
| 67 | } |
| 68 | } |
| 69 | static void make_table2(uint8_t table[]) { |
| 70 | for (int i = 0; i < 256; ++i) { |
| 71 | float fi = i / 255.0f; |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 72 | table[i] = static_cast<uint8_t>(sqrtf(fi) * 255); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 76 | static SkColorFilter* make_null_cf() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | return nullptr; |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 78 | } |
| 79 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 80 | static SkColorFilter* make_cf0() { |
| 81 | uint8_t table[256]; make_table0(table); |
| 82 | return SkTableColorFilter::Create(table); |
| 83 | } |
| 84 | static SkColorFilter* make_cf1() { |
| 85 | uint8_t table[256]; make_table1(table); |
| 86 | return SkTableColorFilter::Create(table); |
| 87 | } |
| 88 | static SkColorFilter* make_cf2() { |
| 89 | uint8_t table[256]; make_table2(table); |
| 90 | return SkTableColorFilter::Create(table); |
| 91 | } |
| 92 | static 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 96 | return SkTableColorFilter::CreateARGB(nullptr, table0, table1, table2); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | class TableColorFilterGM : public skiagm::GM { |
| 100 | public: |
| 101 | TableColorFilterGM() {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 102 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 103 | protected: |
| 104 | virtual SkString onShortName() { |
| 105 | return SkString("tablecolorfilter"); |
| 106 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 107 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 108 | virtual SkISize onISize() { |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 109 | return SkISize::Make(700, 1650); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 110 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 111 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 112 | virtual void onDraw(SkCanvas* canvas) { |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 113 | canvas->drawColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 114 | canvas->translate(20, 20); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 115 | |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 116 | |
| 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.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 141 | SkScalar x = 0, y = 0; |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 142 | for (size_t bitmapMaker = 0; bitmapMaker < SK_ARRAY_COUNT(gBitmapMakers); ++bitmapMaker) { |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 143 | SkBitmap bm; |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 144 | gBitmapMakers[bitmapMaker](&bm); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 145 | |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 146 | 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.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 150 | x = 0; |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 151 | SkPaint paint; |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 152 | canvas->drawBitmap(bm, x, y, &paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 153 | |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 154 | // 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 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 162 | paint.setColorFilter(nullptr); |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 163 | |
| 164 | for (unsigned i = 0; i < SK_ARRAY_COUNT(gColorFilterMakers); ++i) { |
| 165 | SkAutoTUnref<SkColorFilter> colorFilter1(gColorFilterMakers[i]()); |
| 166 | SkAutoTUnref<SkImageFilter> imageFilter1(SkColorFilterImageFilter::Create( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 167 | colorFilter1, nullptr, nullptr)); |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 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( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 176 | colorFilter2, imageFilter1, nullptr)); |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 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.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 187 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 188 | private: |
| 189 | typedef GM INHERITED; |
| 190 | }; |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 191 | DEF_GM( return new TableColorFilterGM; ) |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 192 | |
| 193 | ////////////////////////////////////////////////////////////////////////////// |
| 194 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 195 | class ComposeColorFilterGM : public skiagm::GM { |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 196 | enum { |
| 197 | COLOR_COUNT = 3, |
| 198 | MODE_COUNT = 4, |
| 199 | }; |
| 200 | const SkColor* fColors; |
| 201 | const SkXfermode::Mode* fModes; |
| 202 | SkString fName; |
| 203 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 204 | public: |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 205 | 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 | } |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 211 | |
| 212 | protected: |
| 213 | virtual SkString onShortName() { |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 214 | return fName; |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | virtual SkISize onISize() { |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 218 | return SkISize::Make(790, 790); |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | virtual void onDraw(SkCanvas* canvas) { |
| 222 | SkBitmap bm; |
| 223 | make_bm1(&bm); |
| 224 | |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 225 | canvas->drawColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 226 | |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 227 | const int MODES = MODE_COUNT * COLOR_COUNT; |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 228 | SkAutoTUnref<SkColorFilter> filters[MODES]; |
| 229 | int index = 0; |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 230 | 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])); |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 233 | } |
| 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 | |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 243 | canvas->drawRect(r, paint); // orig |
| 244 | |
| 245 | for (int i = 0; i < MODES; ++i) { |
| 246 | paint.setColorFilter(filters[i]); |
| 247 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 248 | canvas->save(); |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 249 | 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) { |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 264 | 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 | |
| 275 | private: |
| 276 | typedef GM INHERITED; |
| 277 | }; |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 278 | |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 279 | const SkColor gColors0[] = { SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW }; |
| 280 | const SkXfermode::Mode gModes0[] = { |
| 281 | SkXfermode::kOverlay_Mode, |
| 282 | SkXfermode::kDarken_Mode, |
| 283 | SkXfermode::kColorBurn_Mode, |
| 284 | SkXfermode::kExclusion_Mode, |
| 285 | }; |
| 286 | DEF_GM( return new ComposeColorFilterGM(gColors0, gModes0, "wacky"); ) |
| 287 | |
| 288 | const SkColor gColors1[] = { 0x80FF0000, 0x8000FF00, 0x800000FF }; |
| 289 | const SkXfermode::Mode gModes1[] = { |
| 290 | SkXfermode::kSrcOver_Mode, |
| 291 | SkXfermode::kXor_Mode, |
| 292 | SkXfermode::kDstOut_Mode, |
| 293 | SkXfermode::kSrcATop_Mode, |
| 294 | }; |
| 295 | DEF_GM( return new ComposeColorFilterGM(gColors1, gModes1, "alpha"); ) |