mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +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 | |
Mike Reed | 17c574a | 2018-12-12 18:10:38 -0500 | [diff] [blame] | 8 | #include <SkFont.h> |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 9 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 10 | #include "sk_tool_utils.h" |
Brian Salomon | 89cb821 | 2017-01-09 10:48:23 -0500 | [diff] [blame] | 11 | #include "SkArithmeticImageFilter.h" |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 12 | #include "SkCanvas.h" |
| 13 | #include "SkColorPriv.h" |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 14 | #include "SkGradientShader.h" |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 15 | #include "SkImage.h" |
| 16 | #include "SkImageSource.h" |
| 17 | #include "SkShader.h" |
| 18 | #include "SkSurface.h" |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 19 | |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 20 | #define WW 100 |
| 21 | #define HH 32 |
| 22 | |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 23 | static sk_sp<SkImage> make_src() { |
| 24 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH)); |
| 25 | SkCanvas* canvas = surface->getCanvas(); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 26 | |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 27 | SkPaint paint; |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 28 | SkPoint pts[] = { {0, 0}, {SkIntToScalar(WW), SkIntToScalar(HH)} }; |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 29 | SkColor colors[] = { |
senorblanco@chromium.org | 35c733c | 2013-05-28 19:43:05 +0000 | [diff] [blame] | 30 | SK_ColorTRANSPARENT, SK_ColorGREEN, SK_ColorCYAN, |
| 31 | SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE, |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 32 | }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 33 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 34 | SkShader::kClamp_TileMode)); |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 35 | canvas->drawPaint(paint); |
| 36 | return surface->makeImageSnapshot(); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 37 | } |
| 38 | |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 39 | static sk_sp<SkImage> make_dst() { |
| 40 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH)); |
| 41 | SkCanvas* canvas = surface->getCanvas(); |
| 42 | |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 43 | SkPaint paint; |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 44 | SkPoint pts[] = { {0, SkIntToScalar(HH)}, {SkIntToScalar(WW), 0} }; |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 45 | SkColor colors[] = { |
caryclark | dfcb7ab | 2015-07-17 09:39:16 -0700 | [diff] [blame] | 46 | SK_ColorBLUE, SK_ColorYELLOW, SK_ColorBLACK, SK_ColorGREEN, |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 47 | SK_ColorGRAY, |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 48 | }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 49 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 50 | SkShader::kClamp_TileMode)); |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 51 | canvas->drawPaint(paint); |
| 52 | return surface->makeImageSnapshot(); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 53 | } |
| 54 | |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 55 | static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) { |
Mike Reed | 17c574a | 2018-12-12 18:10:38 -0500 | [diff] [blame] | 56 | SkFont font(sk_tool_utils::create_portable_typeface(), 24); |
| 57 | font.setEdging(SkFont::Edging::kAntiAlias); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 58 | SkPaint paint; |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 59 | paint.setAntiAlias(true); |
| 60 | for (int i = 0; i < 4; ++i) { |
| 61 | SkString str; |
| 62 | str.appendScalar(k[i]); |
Mike Reed | 17c574a | 2018-12-12 18:10:38 -0500 | [diff] [blame] | 63 | SkScalar width = font.measureText(str.c_str(), str.size(), kUTF8_SkTextEncoding); |
| 64 | canvas->drawSimpleText(str.c_str(), str.size(), kUTF8_SkTextEncoding, x, y + font.getSize(), font, paint); |
reed@google.com | eb0fa29 | 2011-12-12 22:01:06 +0000 | [diff] [blame] | 65 | x += width + SkIntToScalar(10); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | class ArithmodeGM : public skiagm::GM { |
| 70 | public: |
| 71 | ArithmodeGM () {} |
| 72 | |
| 73 | protected: |
| 74 | |
| 75 | virtual SkString onShortName() { |
| 76 | return SkString("arithmode"); |
| 77 | } |
| 78 | |
ericrk | af96fce | 2015-10-19 14:41:11 -0700 | [diff] [blame] | 79 | virtual SkISize onISize() { return SkISize::Make(640, 572); } |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 80 | |
| 81 | virtual void onDraw(SkCanvas* canvas) { |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 82 | sk_sp<SkImage> src = make_src(); |
| 83 | sk_sp<SkImage> dst = make_dst(); |
| 84 | sk_sp<SkImageFilter> srcFilter = SkImageSource::Make(src); |
| 85 | sk_sp<SkImageFilter> dstFilter = SkImageSource::Make(dst); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 86 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 87 | constexpr SkScalar one = SK_Scalar1; |
| 88 | constexpr SkScalar K[] = { |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 89 | 0, 0, 0, 0, |
reed@google.com | eb0fa29 | 2011-12-12 22:01:06 +0000 | [diff] [blame] | 90 | 0, 0, 0, one, |
| 91 | 0, one, 0, 0, |
| 92 | 0, 0, one, 0, |
| 93 | 0, one, one, 0, |
| 94 | 0, one, -one, 0, |
| 95 | 0, one/2, one/2, 0, |
| 96 | 0, one/2, one/2, one/4, |
| 97 | 0, one/2, one/2, -one/4, |
| 98 | one/4, one/2, one/2, 0, |
| 99 | -one/4, one/2, one/2, 0, |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 100 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 101 | |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 102 | const SkScalar* k = K; |
| 103 | const SkScalar* stop = k + SK_ARRAY_COUNT(K); |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 104 | const SkRect rect = SkRect::MakeWH(WW, HH); |
| 105 | SkScalar gap = SkIntToScalar(WW + 20); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 106 | while (k < stop) { |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 107 | { |
| 108 | SkAutoCanvasRestore acr(canvas, true); |
| 109 | canvas->drawImage(src, 0, 0); |
| 110 | canvas->translate(gap, 0); |
| 111 | canvas->drawImage(dst, 0, 0); |
| 112 | canvas->translate(gap, 0); |
| 113 | SkPaint paint; |
Brian Salomon | 89cb821 | 2017-01-09 10:48:23 -0500 | [diff] [blame] | 114 | paint.setImageFilter(SkArithmeticImageFilter::Make(k[0], k[1], k[2], k[3], true, |
| 115 | dstFilter, srcFilter, nullptr)); |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 116 | canvas->saveLayer(&rect, &paint); |
| 117 | canvas->restore(); |
| 118 | |
| 119 | canvas->translate(gap, 0); |
| 120 | show_k_text(canvas, 0, 0, k); |
| 121 | } |
| 122 | |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 123 | k += 4; |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 124 | canvas->translate(0, HH + 12); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 125 | } |
ericrk | af96fce | 2015-10-19 14:41:11 -0700 | [diff] [blame] | 126 | |
| 127 | // Draw two special cases to test enforcePMColor. In these cases, we |
| 128 | // draw the dst bitmap twice, the first time it is halved and inverted, |
| 129 | // leading to invalid premultiplied colors. If we enforcePMColor, these |
| 130 | // invalid values should be clamped, and will not contribute to the |
| 131 | // second draw. |
| 132 | for (int i = 0; i < 2; i++) { |
| 133 | const bool enforcePMColor = (i == 0); |
ericrk | af96fce | 2015-10-19 14:41:11 -0700 | [diff] [blame] | 134 | |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 135 | { |
| 136 | SkAutoCanvasRestore acr(canvas, true); |
| 137 | canvas->translate(gap, 0); |
| 138 | canvas->drawImage(dst, 0, 0); |
| 139 | canvas->translate(gap, 0); |
ericrk | af96fce | 2015-10-19 14:41:11 -0700 | [diff] [blame] | 140 | |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 141 | sk_sp<SkImageFilter> bg = |
Mike Reed | 6b9cd05 | 2017-06-18 21:32:48 -0400 | [diff] [blame] | 142 | SkArithmeticImageFilter::Make(0, 0, -one / 2, 1, enforcePMColor, dstFilter, |
| 143 | nullptr, nullptr); |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 144 | SkPaint p; |
Brian Salomon | 89cb821 | 2017-01-09 10:48:23 -0500 | [diff] [blame] | 145 | p.setImageFilter(SkArithmeticImageFilter::Make(0, one / 2, -one, 1, true, |
| 146 | std::move(bg), dstFilter, nullptr)); |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 147 | canvas->saveLayer(&rect, &p); |
| 148 | canvas->restore(); |
| 149 | canvas->translate(gap, 0); |
| 150 | |
| 151 | // Label |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame^] | 152 | SkFont font(sk_tool_utils::create_portable_typeface(), 24); |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 153 | SkString str(enforcePMColor ? "enforcePM" : "no enforcePM"); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame^] | 154 | canvas->drawString(str, 0, font.getSize(), font, SkPaint()); |
fmalita | ad7cb81 | 2016-09-29 12:25:26 -0700 | [diff] [blame] | 155 | } |
| 156 | canvas->translate(0, HH + 12); |
ericrk | af96fce | 2015-10-19 14:41:11 -0700 | [diff] [blame] | 157 | } |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | private: |
| 161 | typedef GM INHERITED; |
| 162 | }; |
| 163 | |
| 164 | /////////////////////////////////////////////////////////////////////////////// |
| 165 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 166 | DEF_GM( return new ArithmodeGM; ) |