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