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