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" |
| 11 | #include "SkShader.h" |
| 12 | |
| 13 | #include "SkArithmeticMode.h" |
| 14 | #include "SkGradientShader.h" |
| 15 | #define WW 100 |
| 16 | #define HH 32 |
| 17 | |
| 18 | static SkBitmap make_bm() { |
| 19 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 20 | bm.allocN32Pixels(WW, HH); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 21 | bm.eraseColor(SK_ColorTRANSPARENT); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 22 | return bm; |
| 23 | } |
| 24 | |
| 25 | static SkBitmap make_src() { |
| 26 | SkBitmap bm = make_bm(); |
| 27 | SkCanvas canvas(bm); |
| 28 | SkPaint paint; |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 29 | SkPoint pts[] = { {0, 0}, {SkIntToScalar(WW), SkIntToScalar(HH)} }; |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 30 | SkColor colors[] = { |
senorblanco@chromium.org | 35c733c | 2013-05-28 19:43:05 +0000 | [diff] [blame] | 31 | SK_ColorTRANSPARENT, SK_ColorGREEN, SK_ColorCYAN, |
| 32 | SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE, |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 33 | }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 34 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 35 | SkShader::kClamp_TileMode)); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 36 | canvas.drawPaint(paint); |
| 37 | return bm; |
| 38 | } |
| 39 | |
| 40 | static SkBitmap make_dst() { |
| 41 | SkBitmap bm = make_bm(); |
| 42 | SkCanvas canvas(bm); |
| 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, |
| 47 | sk_tool_utils::color_to_565(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)); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 51 | canvas.drawPaint(paint); |
| 52 | return bm; |
| 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[]) { |
| 56 | SkPaint paint; |
reed@google.com | eb0fa29 | 2011-12-12 22:01:06 +0000 | [diff] [blame] | 57 | paint.setTextSize(SkIntToScalar(24)); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 58 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 59 | sk_tool_utils::set_portable_typeface(&paint); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 60 | for (int i = 0; i < 4; ++i) { |
| 61 | SkString str; |
| 62 | str.appendScalar(k[i]); |
| 63 | SkScalar width = paint.measureText(str.c_str(), str.size()); |
reed@google.com | eb0fa29 | 2011-12-12 22:01:06 +0000 | [diff] [blame] | 64 | canvas->drawText(str.c_str(), str.size(), x, y + paint.getTextSize(), paint); |
| 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) { |
| 82 | SkBitmap src = make_src(); |
| 83 | SkBitmap dst = 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); |
| 102 | SkScalar y = 0; |
reed@google.com | eb85630 | 2011-12-12 22:15:18 +0000 | [diff] [blame] | 103 | SkScalar gap = SkIntToScalar(src.width() + 20); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 104 | while (k < stop) { |
| 105 | SkScalar x = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 106 | canvas->drawBitmap(src, x, y, nullptr); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 107 | x += gap; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 108 | canvas->drawBitmap(dst, x, y, nullptr); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 109 | x += gap; |
senorblanco@chromium.org | 9a6eb0e | 2013-05-29 20:55:09 +0000 | [diff] [blame] | 110 | SkRect rect = SkRect::MakeXYWH(x, y, SkIntToScalar(WW), SkIntToScalar(HH)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 111 | canvas->saveLayer(&rect, nullptr); |
| 112 | canvas->drawBitmap(dst, x, y, nullptr); |
senorblanco@chromium.org | 9a6eb0e | 2013-05-29 20:55:09 +0000 | [diff] [blame] | 113 | SkPaint paint; |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 114 | paint.setXfermode(SkArithmeticMode::Make(k[0], k[1], k[2], k[3])); |
senorblanco@chromium.org | 9a6eb0e | 2013-05-29 20:55:09 +0000 | [diff] [blame] | 115 | canvas->drawBitmap(src, x, y, &paint); |
| 116 | canvas->restore(); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 117 | x += gap; |
| 118 | show_k_text(canvas, x, y, k); |
| 119 | k += 4; |
reed@google.com | eb85630 | 2011-12-12 22:15:18 +0000 | [diff] [blame] | 120 | y += SkIntToScalar(src.height() + 12); |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 121 | } |
ericrk | af96fce | 2015-10-19 14:41:11 -0700 | [diff] [blame] | 122 | |
| 123 | // Draw two special cases to test enforcePMColor. In these cases, we |
| 124 | // draw the dst bitmap twice, the first time it is halved and inverted, |
| 125 | // leading to invalid premultiplied colors. If we enforcePMColor, these |
| 126 | // invalid values should be clamped, and will not contribute to the |
| 127 | // second draw. |
| 128 | for (int i = 0; i < 2; i++) { |
| 129 | const bool enforcePMColor = (i == 0); |
| 130 | SkScalar x = gap; |
| 131 | canvas->drawBitmap(dst, x, y, nullptr); |
| 132 | x += gap; |
| 133 | SkRect rect = SkRect::MakeXYWH(x, y, SkIntToScalar(WW), SkIntToScalar(HH)); |
| 134 | canvas->saveLayer(&rect, nullptr); |
ericrk | af96fce | 2015-10-19 14:41:11 -0700 | [diff] [blame] | 135 | SkPaint paint1; |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 136 | paint1.setXfermode(SkArithmeticMode::Make(0, -one / 2, 0, 1, enforcePMColor)); |
ericrk | af96fce | 2015-10-19 14:41:11 -0700 | [diff] [blame] | 137 | canvas->drawBitmap(dst, x, y, &paint1); |
ericrk | af96fce | 2015-10-19 14:41:11 -0700 | [diff] [blame] | 138 | SkPaint paint2; |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 139 | paint2.setXfermode(SkArithmeticMode::Make(0, one / 2, -one, 1)); |
ericrk | af96fce | 2015-10-19 14:41:11 -0700 | [diff] [blame] | 140 | canvas->drawBitmap(dst, x, y, &paint2); |
| 141 | canvas->restore(); |
| 142 | x += gap; |
| 143 | |
| 144 | // Label |
| 145 | SkPaint paint; |
| 146 | paint.setTextSize(SkIntToScalar(24)); |
| 147 | paint.setAntiAlias(true); |
| 148 | sk_tool_utils::set_portable_typeface(&paint); |
| 149 | SkString str(enforcePMColor ? "enforcePM" : "no enforcePM"); |
| 150 | canvas->drawText(str.c_str(), str.size(), x, y + paint.getTextSize(), paint); |
| 151 | |
| 152 | y += SkIntToScalar(src.height() + 12); |
| 153 | } |
mike@reedtribe.org | e51755f | 2011-12-10 19:36:56 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | private: |
| 157 | typedef GM INHERITED; |
| 158 | }; |
| 159 | |
| 160 | /////////////////////////////////////////////////////////////////////////////// |
| 161 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 162 | DEF_GM( return new ArithmodeGM; ) |