blob: 70661a0fbaa45b331244bb37c88750faa53c572e [file] [log] [blame]
mike@reedtribe.orge51755f2011-12-10 19:36:56 +00001/*
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
18static SkBitmap make_bm() {
19 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +000020 bm.allocN32Pixels(WW, HH);
junov@google.comdbfac8a2012-12-06 21:47:40 +000021 bm.eraseColor(SK_ColorTRANSPARENT);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000022 return bm;
23}
24
25static SkBitmap make_src() {
26 SkBitmap bm = make_bm();
27 SkCanvas canvas(bm);
28 SkPaint paint;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000029 SkPoint pts[] = { {0, 0}, {SkIntToScalar(WW), SkIntToScalar(HH)} };
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000030 SkColor colors[] = {
senorblanco@chromium.org35c733c2013-05-28 19:43:05 +000031 SK_ColorTRANSPARENT, SK_ColorGREEN, SK_ColorCYAN,
32 SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000033 };
reed8a21c9f2016-03-08 18:50:00 -080034 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
35 SkShader::kClamp_TileMode));
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000036 canvas.drawPaint(paint);
37 return bm;
38}
39
40static SkBitmap make_dst() {
41 SkBitmap bm = make_bm();
42 SkCanvas canvas(bm);
43 SkPaint paint;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000044 SkPoint pts[] = { {0, SkIntToScalar(HH)}, {SkIntToScalar(WW), 0} };
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000045 SkColor colors[] = {
caryclarkdfcb7ab2015-07-17 09:39:16 -070046 SK_ColorBLUE, SK_ColorYELLOW, SK_ColorBLACK, SK_ColorGREEN,
47 sk_tool_utils::color_to_565(SK_ColorGRAY)
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000048 };
reed8a21c9f2016-03-08 18:50:00 -080049 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
50 SkShader::kClamp_TileMode));
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000051 canvas.drawPaint(paint);
52 return bm;
53}
54
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000055static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
56 SkPaint paint;
reed@google.comeb0fa292011-12-12 22:01:06 +000057 paint.setTextSize(SkIntToScalar(24));
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000058 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070059 sk_tool_utils::set_portable_typeface(&paint);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000060 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.comeb0fa292011-12-12 22:01:06 +000064 canvas->drawText(str.c_str(), str.size(), x, y + paint.getTextSize(), paint);
65 x += width + SkIntToScalar(10);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000066 }
67}
68
69class ArithmodeGM : public skiagm::GM {
70public:
71 ArithmodeGM () {}
72
73protected:
74
75 virtual SkString onShortName() {
76 return SkString("arithmode");
77 }
78
ericrkaf96fce2015-10-19 14:41:11 -070079 virtual SkISize onISize() { return SkISize::Make(640, 572); }
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000080
81 virtual void onDraw(SkCanvas* canvas) {
82 SkBitmap src = make_src();
83 SkBitmap dst = make_dst();
rmistry@google.comae933ce2012-08-23 18:19:56 +000084
mtkleindbfd7ab2016-09-01 11:24:54 -070085 constexpr SkScalar one = SK_Scalar1;
86 constexpr SkScalar K[] = {
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000087 0, 0, 0, 0,
reed@google.comeb0fa292011-12-12 22:01:06 +000088 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.orge51755f2011-12-10 19:36:56 +000098 };
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000100 const SkScalar* k = K;
101 const SkScalar* stop = k + SK_ARRAY_COUNT(K);
102 SkScalar y = 0;
reed@google.comeb856302011-12-12 22:15:18 +0000103 SkScalar gap = SkIntToScalar(src.width() + 20);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000104 while (k < stop) {
105 SkScalar x = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700106 canvas->drawBitmap(src, x, y, nullptr);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000107 x += gap;
halcanary96fcdcc2015-08-27 07:41:13 -0700108 canvas->drawBitmap(dst, x, y, nullptr);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000109 x += gap;
senorblanco@chromium.org9a6eb0e2013-05-29 20:55:09 +0000110 SkRect rect = SkRect::MakeXYWH(x, y, SkIntToScalar(WW), SkIntToScalar(HH));
halcanary96fcdcc2015-08-27 07:41:13 -0700111 canvas->saveLayer(&rect, nullptr);
112 canvas->drawBitmap(dst, x, y, nullptr);
senorblanco@chromium.org9a6eb0e2013-05-29 20:55:09 +0000113 SkPaint paint;
reedcfb6bdf2016-03-29 11:32:50 -0700114 paint.setXfermode(SkArithmeticMode::Make(k[0], k[1], k[2], k[3]));
senorblanco@chromium.org9a6eb0e2013-05-29 20:55:09 +0000115 canvas->drawBitmap(src, x, y, &paint);
116 canvas->restore();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000117 x += gap;
118 show_k_text(canvas, x, y, k);
119 k += 4;
reed@google.comeb856302011-12-12 22:15:18 +0000120 y += SkIntToScalar(src.height() + 12);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000121 }
ericrkaf96fce2015-10-19 14:41:11 -0700122
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);
ericrkaf96fce2015-10-19 14:41:11 -0700135 SkPaint paint1;
reedcfb6bdf2016-03-29 11:32:50 -0700136 paint1.setXfermode(SkArithmeticMode::Make(0, -one / 2, 0, 1, enforcePMColor));
ericrkaf96fce2015-10-19 14:41:11 -0700137 canvas->drawBitmap(dst, x, y, &paint1);
ericrkaf96fce2015-10-19 14:41:11 -0700138 SkPaint paint2;
reedcfb6bdf2016-03-29 11:32:50 -0700139 paint2.setXfermode(SkArithmeticMode::Make(0, one / 2, -one, 1));
ericrkaf96fce2015-10-19 14:41:11 -0700140 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.orge51755f2011-12-10 19:36:56 +0000154 }
155
156private:
157 typedef GM INHERITED;
158};
159
160///////////////////////////////////////////////////////////////////////////////
161
scroggo96f16e82015-12-10 13:31:59 -0800162DEF_GM( return new ArithmodeGM; )