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