epoger@google.com | ec3ed6a | 2011-07-28 14:26: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 | */ |
reed | c3b3266 | 2014-06-17 08:38:31 -0700 | [diff] [blame] | 7 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 8 | #include "gm.h" |
reed | c3b3266 | 2014-06-17 08:38:31 -0700 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 10 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 11 | static void make_bm(SkBitmap* bm) { |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 12 | const SkColor colors[4] = { |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 13 | SK_ColorRED, SK_ColorGREEN, |
| 14 | SK_ColorBLUE, SK_ColorWHITE |
| 15 | }; |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 16 | SkPMColor colorsPM[4]; |
| 17 | for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) { |
| 18 | colorsPM[i] = SkPreMultiplyColor(colors[i]); |
| 19 | } |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 20 | bm->allocN32Pixels(2, 2, true); |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 21 | |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 22 | *bm->getAddr32(0, 0) = colorsPM[0]; |
| 23 | *bm->getAddr32(1, 0) = colorsPM[1]; |
| 24 | *bm->getAddr32(0, 1) = colorsPM[2]; |
| 25 | *bm->getAddr32(1, 1) = colorsPM[3]; |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm, |
| 29 | SkScalar x, SkScalar y, SkPaint* paint) { |
| 30 | canvas->drawBitmap(bm, x, y, paint); |
| 31 | return SkIntToScalar(bm.width()) * 5/4; |
| 32 | } |
| 33 | |
| 34 | static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x, |
| 35 | SkPaint* p) { |
| 36 | x += draw_bm(c, bm, x, 0, p); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 37 | p->setFilterQuality(kLow_SkFilterQuality); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 38 | x += draw_bm(c, bm, x, 0, p); |
| 39 | p->setDither(true); |
| 40 | return x + draw_bm(c, bm, x, 0, p); |
| 41 | } |
| 42 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 43 | static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) { |
| 44 | SkAutoCanvasRestore acr(canvas, true); |
| 45 | |
| 46 | SkPaint paint; |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame^] | 47 | paint.setAntiAlias(true); |
| 48 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 49 | SkScalar x = 0; |
| 50 | const int scale = 32; |
| 51 | |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame^] | 52 | SkFont font(sk_tool_utils::create_portable_typeface()); |
reed | c3b3266 | 2014-06-17 08:38:31 -0700 | [diff] [blame] | 53 | const char* name = sk_tool_utils::colortype_name(bm.colorType()); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 54 | canvas->drawString(name, x, SkIntToScalar(bm.height())*scale*5/8, |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame^] | 55 | font, paint); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 56 | canvas->translate(SkIntToScalar(48), 0); |
| 57 | |
| 58 | canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale)); |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 59 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 60 | x += draw_set(canvas, bm, 0, &paint); |
| 61 | paint.reset(); |
| 62 | paint.setAlpha(0x80); |
| 63 | draw_set(canvas, bm, x, &paint); |
| 64 | return x * scale / 3; |
| 65 | } |
| 66 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 67 | class FilterGM : public skiagm::GM { |
| 68 | void onOnceBeforeDraw() override { |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 69 | make_bm(&fBM32); |
| 70 | sk_tool_utils::copy_to(&fBM4444, kARGB_4444_SkColorType, fBM32); |
| 71 | sk_tool_utils::copy_to(&fBM16, kRGB_565_SkColorType, fBM32); |
reed@google.com | 26b73d3 | 2012-03-08 22:13:04 +0000 | [diff] [blame] | 72 | } |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 73 | |
reed@google.com | 26b73d3 | 2012-03-08 22:13:04 +0000 | [diff] [blame] | 74 | public: |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 75 | SkBitmap fBM4444, fBM16, fBM32; |
reed@google.com | 26b73d3 | 2012-03-08 22:13:04 +0000 | [diff] [blame] | 76 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 77 | FilterGM() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 78 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | protected: |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 82 | SkString onShortName() override { |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 83 | return SkString("bitmapfilters"); |
| 84 | } |
| 85 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 86 | SkISize onISize() override { |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 87 | return SkISize::Make(540, 250); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 88 | } |
| 89 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 90 | void onDraw(SkCanvas* canvas) override { |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 91 | SkScalar x = SkIntToScalar(10); |
| 92 | SkScalar y = SkIntToScalar(10); |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 93 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 94 | canvas->translate(x, y); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 95 | y = draw_row(canvas, fBM4444); |
| 96 | canvas->translate(0, y); |
| 97 | y = draw_row(canvas, fBM16); |
| 98 | canvas->translate(0, y); |
| 99 | draw_row(canvas, fBM32); |
| 100 | } |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 101 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 102 | private: |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 103 | typedef skiagm::GM INHERITED; |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 104 | }; |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 105 | DEF_GM( return new FilterGM; ) |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 106 | |
| 107 | ////////////////////////////////////////////////////////////////////////////// |
| 108 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 109 | class TestExtractAlphaGM : public skiagm::GM { |
| 110 | void onOnceBeforeDraw() override { |
| 111 | // Make a bitmap with per-pixels alpha (stroked circle) |
| 112 | fBitmap.allocN32Pixels(100, 100); |
| 113 | SkCanvas canvas(fBitmap); |
| 114 | canvas.clear(0); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 115 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 116 | SkPaint paint; |
| 117 | paint.setAntiAlias(true); |
| 118 | paint.setColor(SK_ColorBLUE); |
| 119 | paint.setStyle(SkPaint::kStroke_Style); |
| 120 | paint.setStrokeWidth(20); |
| 121 | |
| 122 | canvas.drawCircle(50, 50, 39, paint); |
| 123 | canvas.flush(); |
| 124 | |
| 125 | fBitmap.extractAlpha(&fAlpha); |
| 126 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 127 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 128 | public: |
| 129 | SkBitmap fBitmap, fAlpha; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 130 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 131 | protected: |
| 132 | SkString onShortName() override { |
| 133 | return SkString("extractalpha"); |
| 134 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 135 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 136 | SkISize onISize() override { |
| 137 | return SkISize::Make(540, 330); |
| 138 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 139 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 140 | void onDraw(SkCanvas* canvas) override { |
| 141 | SkPaint paint; |
| 142 | paint.setAntiAlias(true); |
| 143 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 144 | paint.setColor(SK_ColorRED); |
| 145 | |
| 146 | canvas->drawBitmap(fBitmap, 10, 10, &paint); // should stay blue (ignore paint's color) |
| 147 | canvas->drawBitmap(fAlpha, 120, 10, &paint); // should draw red |
| 148 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 149 | |
reed | 2ba21a0 | 2015-05-18 12:57:56 -0700 | [diff] [blame] | 150 | private: |
| 151 | typedef skiagm::GM INHERITED; |
| 152 | }; |
| 153 | DEF_GM( return new TestExtractAlphaGM; ) |