blob: 37c7a597b0d89c8012ba57bb6ffc28822d7dd80a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040010#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkFont.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040012#include "include/core/SkFontTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkImage.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040014#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 Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/core/SkShader.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040021#include "include/core/SkSize.h"
22#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkSurface.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040024#include "include/core/SkTileMode.h"
25#include "include/core/SkTypeface.h"
26#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/effects/SkArithmeticImageFilter.h"
28#include "include/effects/SkGradientShader.h"
29#include "include/effects/SkImageSource.h"
30#include "tools/ToolUtils.h"
fmalitaad7cb812016-09-29 12:25:26 -070031
Ben Wagnerd1701ba2019-04-30 13:44:26 -040032#include <utility>
33
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000034#define WW 100
35#define HH 32
36
fmalitaad7cb812016-09-29 12:25:26 -070037static sk_sp<SkImage> make_src() {
38 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH));
39 SkCanvas* canvas = surface->getCanvas();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000040
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000041 SkPaint paint;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000042 SkPoint pts[] = { {0, 0}, {SkIntToScalar(WW), SkIntToScalar(HH)} };
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000043 SkColor colors[] = {
senorblanco@chromium.org35c733c2013-05-28 19:43:05 +000044 SK_ColorTRANSPARENT, SK_ColorGREEN, SK_ColorCYAN,
45 SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000046 };
reed8a21c9f2016-03-08 18:50:00 -080047 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040048 SkTileMode::kClamp));
fmalitaad7cb812016-09-29 12:25:26 -070049 canvas->drawPaint(paint);
50 return surface->makeImageSnapshot();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000051}
52
fmalitaad7cb812016-09-29 12:25:26 -070053static sk_sp<SkImage> make_dst() {
54 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH));
55 SkCanvas* canvas = surface->getCanvas();
56
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000057 SkPaint paint;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000058 SkPoint pts[] = { {0, SkIntToScalar(HH)}, {SkIntToScalar(WW), 0} };
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000059 SkColor colors[] = {
caryclarkdfcb7ab2015-07-17 09:39:16 -070060 SK_ColorBLUE, SK_ColorYELLOW, SK_ColorBLACK, SK_ColorGREEN,
Mike Kleind46dce32018-08-16 10:17:03 -040061 SK_ColorGRAY,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000062 };
reed8a21c9f2016-03-08 18:50:00 -080063 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040064 SkTileMode::kClamp));
fmalitaad7cb812016-09-29 12:25:26 -070065 canvas->drawPaint(paint);
66 return surface->makeImageSnapshot();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000067}
68
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000069static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
Mike Kleinea3f0142019-03-20 11:12:10 -050070 SkFont font(ToolUtils::create_portable_typeface(), 24);
Mike Reed17c574a2018-12-12 18:10:38 -050071 font.setEdging(SkFont::Edging::kAntiAlias);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000072 SkPaint paint;
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000073 paint.setAntiAlias(true);
74 for (int i = 0; i < 4; ++i) {
75 SkString str;
76 str.appendScalar(k[i]);
Ben Wagner51e15a62019-05-07 15:38:46 -040077 SkScalar width = font.measureText(str.c_str(), str.size(), SkTextEncoding::kUTF8);
Hal Canary89a644b2019-01-07 09:36:09 -050078 canvas->drawString(str, x, y + font.getSize(), font, paint);
reed@google.comeb0fa292011-12-12 22:01:06 +000079 x += width + SkIntToScalar(10);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000080 }
81}
82
83class ArithmodeGM : public skiagm::GM {
84public:
85 ArithmodeGM () {}
86
87protected:
88
89 virtual SkString onShortName() {
90 return SkString("arithmode");
91 }
92
ericrkaf96fce2015-10-19 14:41:11 -070093 virtual SkISize onISize() { return SkISize::Make(640, 572); }
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000094
95 virtual void onDraw(SkCanvas* canvas) {
fmalitaad7cb812016-09-29 12:25:26 -070096 sk_sp<SkImage> src = make_src();
97 sk_sp<SkImage> dst = make_dst();
98 sk_sp<SkImageFilter> srcFilter = SkImageSource::Make(src);
99 sk_sp<SkImageFilter> dstFilter = SkImageSource::Make(dst);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
mtkleindbfd7ab2016-09-01 11:24:54 -0700101 constexpr SkScalar one = SK_Scalar1;
102 constexpr SkScalar K[] = {
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000103 0, 0, 0, 0,
reed@google.comeb0fa292011-12-12 22:01:06 +0000104 0, 0, 0, one,
105 0, one, 0, 0,
106 0, 0, one, 0,
107 0, one, one, 0,
108 0, one, -one, 0,
109 0, one/2, one/2, 0,
110 0, one/2, one/2, one/4,
111 0, one/2, one/2, -one/4,
112 one/4, one/2, one/2, 0,
113 -one/4, one/2, one/2, 0,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000114 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000116 const SkScalar* k = K;
117 const SkScalar* stop = k + SK_ARRAY_COUNT(K);
fmalitaad7cb812016-09-29 12:25:26 -0700118 const SkRect rect = SkRect::MakeWH(WW, HH);
119 SkScalar gap = SkIntToScalar(WW + 20);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000120 while (k < stop) {
fmalitaad7cb812016-09-29 12:25:26 -0700121 {
122 SkAutoCanvasRestore acr(canvas, true);
123 canvas->drawImage(src, 0, 0);
124 canvas->translate(gap, 0);
125 canvas->drawImage(dst, 0, 0);
126 canvas->translate(gap, 0);
127 SkPaint paint;
Brian Salomon89cb8212017-01-09 10:48:23 -0500128 paint.setImageFilter(SkArithmeticImageFilter::Make(k[0], k[1], k[2], k[3], true,
129 dstFilter, srcFilter, nullptr));
fmalitaad7cb812016-09-29 12:25:26 -0700130 canvas->saveLayer(&rect, &paint);
131 canvas->restore();
132
133 canvas->translate(gap, 0);
134 show_k_text(canvas, 0, 0, k);
135 }
136
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000137 k += 4;
fmalitaad7cb812016-09-29 12:25:26 -0700138 canvas->translate(0, HH + 12);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000139 }
ericrkaf96fce2015-10-19 14:41:11 -0700140
141 // Draw two special cases to test enforcePMColor. In these cases, we
142 // draw the dst bitmap twice, the first time it is halved and inverted,
143 // leading to invalid premultiplied colors. If we enforcePMColor, these
144 // invalid values should be clamped, and will not contribute to the
145 // second draw.
146 for (int i = 0; i < 2; i++) {
147 const bool enforcePMColor = (i == 0);
ericrkaf96fce2015-10-19 14:41:11 -0700148
fmalitaad7cb812016-09-29 12:25:26 -0700149 {
150 SkAutoCanvasRestore acr(canvas, true);
151 canvas->translate(gap, 0);
152 canvas->drawImage(dst, 0, 0);
153 canvas->translate(gap, 0);
ericrkaf96fce2015-10-19 14:41:11 -0700154
fmalitaad7cb812016-09-29 12:25:26 -0700155 sk_sp<SkImageFilter> bg =
Mike Reed6b9cd052017-06-18 21:32:48 -0400156 SkArithmeticImageFilter::Make(0, 0, -one / 2, 1, enforcePMColor, dstFilter,
157 nullptr, nullptr);
fmalitaad7cb812016-09-29 12:25:26 -0700158 SkPaint p;
Brian Salomon89cb8212017-01-09 10:48:23 -0500159 p.setImageFilter(SkArithmeticImageFilter::Make(0, one / 2, -one, 1, true,
160 std::move(bg), dstFilter, nullptr));
fmalitaad7cb812016-09-29 12:25:26 -0700161 canvas->saveLayer(&rect, &p);
162 canvas->restore();
163 canvas->translate(gap, 0);
164
165 // Label
Mike Kleinea3f0142019-03-20 11:12:10 -0500166 SkFont font(ToolUtils::create_portable_typeface(), 24);
fmalitaad7cb812016-09-29 12:25:26 -0700167 SkString str(enforcePMColor ? "enforcePM" : "no enforcePM");
Mike Reed4de2f1f2019-01-05 16:35:13 -0500168 canvas->drawString(str, 0, font.getSize(), font, SkPaint());
fmalitaad7cb812016-09-29 12:25:26 -0700169 }
170 canvas->translate(0, HH + 12);
ericrkaf96fce2015-10-19 14:41:11 -0700171 }
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000172 }
173
174private:
175 typedef GM INHERITED;
176};
177
178///////////////////////////////////////////////////////////////////////////////
179
scroggo96f16e82015-12-10 13:31:59 -0800180DEF_GM( return new ArithmodeGM; )