blob: f835eb7d1cf33e7a18c3e0adb0108e603e1f2dba [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/SkGradientShader.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040028#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "tools/ToolUtils.h"
fmalitaad7cb812016-09-29 12:25:26 -070030
Ben Wagnerd1701ba2019-04-30 13:44:26 -040031#include <utility>
32
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000033#define WW 100
34#define HH 32
35
fmalitaad7cb812016-09-29 12:25:26 -070036static sk_sp<SkImage> make_src() {
37 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH));
38 SkCanvas* canvas = surface->getCanvas();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000039
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000040 SkPaint paint;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000041 SkPoint pts[] = { {0, 0}, {SkIntToScalar(WW), SkIntToScalar(HH)} };
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000042 SkColor colors[] = {
senorblanco@chromium.org35c733c2013-05-28 19:43:05 +000043 SK_ColorTRANSPARENT, SK_ColorGREEN, SK_ColorCYAN,
44 SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000045 };
reed8a21c9f2016-03-08 18:50:00 -080046 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040047 SkTileMode::kClamp));
fmalitaad7cb812016-09-29 12:25:26 -070048 canvas->drawPaint(paint);
49 return surface->makeImageSnapshot();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000050}
51
fmalitaad7cb812016-09-29 12:25:26 -070052static sk_sp<SkImage> make_dst() {
53 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH));
54 SkCanvas* canvas = surface->getCanvas();
55
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000056 SkPaint paint;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000057 SkPoint pts[] = { {0, SkIntToScalar(HH)}, {SkIntToScalar(WW), 0} };
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000058 SkColor colors[] = {
caryclarkdfcb7ab2015-07-17 09:39:16 -070059 SK_ColorBLUE, SK_ColorYELLOW, SK_ColorBLACK, SK_ColorGREEN,
Mike Kleind46dce32018-08-16 10:17:03 -040060 SK_ColorGRAY,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000061 };
reed8a21c9f2016-03-08 18:50:00 -080062 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040063 SkTileMode::kClamp));
fmalitaad7cb812016-09-29 12:25:26 -070064 canvas->drawPaint(paint);
65 return surface->makeImageSnapshot();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000066}
67
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000068static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
Mike Kleinea3f0142019-03-20 11:12:10 -050069 SkFont font(ToolUtils::create_portable_typeface(), 24);
Mike Reed17c574a2018-12-12 18:10:38 -050070 font.setEdging(SkFont::Edging::kAntiAlias);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000071 SkPaint paint;
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000072 paint.setAntiAlias(true);
73 for (int i = 0; i < 4; ++i) {
74 SkString str;
75 str.appendScalar(k[i]);
Ben Wagner51e15a62019-05-07 15:38:46 -040076 SkScalar width = font.measureText(str.c_str(), str.size(), SkTextEncoding::kUTF8);
Hal Canary89a644b2019-01-07 09:36:09 -050077 canvas->drawString(str, x, y + font.getSize(), font, paint);
reed@google.comeb0fa292011-12-12 22:01:06 +000078 x += width + SkIntToScalar(10);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000079 }
80}
81
82class ArithmodeGM : public skiagm::GM {
Hal Canarybd865e22019-07-18 11:51:19 -040083 SkString onShortName() override { return SkString("arithmode"); }
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000084
Hal Canarybd865e22019-07-18 11:51:19 -040085 SkISize onISize() override { return {640, 572}; }
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000086
Hal Canarybd865e22019-07-18 11:51:19 -040087 void onDraw(SkCanvas* canvas) override {
fmalitaad7cb812016-09-29 12:25:26 -070088 sk_sp<SkImage> src = make_src();
89 sk_sp<SkImage> dst = make_dst();
Michael Ludwig898bbfa2019-08-02 15:21:23 -040090 sk_sp<SkImageFilter> srcFilter = SkImageFilters::Image(src);
91 sk_sp<SkImageFilter> dstFilter = SkImageFilters::Image(dst);
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
mtkleindbfd7ab2016-09-01 11:24:54 -070093 constexpr SkScalar one = SK_Scalar1;
94 constexpr SkScalar K[] = {
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000095 0, 0, 0, 0,
reed@google.comeb0fa292011-12-12 22:01:06 +000096 0, 0, 0, one,
97 0, one, 0, 0,
98 0, 0, one, 0,
99 0, one, one, 0,
100 0, one, -one, 0,
101 0, one/2, one/2, 0,
102 0, one/2, one/2, one/4,
103 0, one/2, one/2, -one/4,
104 one/4, one/2, one/2, 0,
105 -one/4, one/2, one/2, 0,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000106 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000107
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000108 const SkScalar* k = K;
109 const SkScalar* stop = k + SK_ARRAY_COUNT(K);
fmalitaad7cb812016-09-29 12:25:26 -0700110 const SkRect rect = SkRect::MakeWH(WW, HH);
111 SkScalar gap = SkIntToScalar(WW + 20);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000112 while (k < stop) {
fmalitaad7cb812016-09-29 12:25:26 -0700113 {
114 SkAutoCanvasRestore acr(canvas, true);
115 canvas->drawImage(src, 0, 0);
116 canvas->translate(gap, 0);
117 canvas->drawImage(dst, 0, 0);
118 canvas->translate(gap, 0);
119 SkPaint paint;
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400120 paint.setImageFilter(SkImageFilters::Arithmetic(k[0], k[1], k[2], k[3], true,
121 dstFilter, srcFilter, nullptr));
fmalitaad7cb812016-09-29 12:25:26 -0700122 canvas->saveLayer(&rect, &paint);
123 canvas->restore();
124
125 canvas->translate(gap, 0);
126 show_k_text(canvas, 0, 0, k);
127 }
128
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000129 k += 4;
fmalitaad7cb812016-09-29 12:25:26 -0700130 canvas->translate(0, HH + 12);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000131 }
ericrkaf96fce2015-10-19 14:41:11 -0700132
133 // Draw two special cases to test enforcePMColor. In these cases, we
134 // draw the dst bitmap twice, the first time it is halved and inverted,
135 // leading to invalid premultiplied colors. If we enforcePMColor, these
136 // invalid values should be clamped, and will not contribute to the
137 // second draw.
138 for (int i = 0; i < 2; i++) {
139 const bool enforcePMColor = (i == 0);
ericrkaf96fce2015-10-19 14:41:11 -0700140
fmalitaad7cb812016-09-29 12:25:26 -0700141 {
142 SkAutoCanvasRestore acr(canvas, true);
143 canvas->translate(gap, 0);
144 canvas->drawImage(dst, 0, 0);
145 canvas->translate(gap, 0);
ericrkaf96fce2015-10-19 14:41:11 -0700146
fmalitaad7cb812016-09-29 12:25:26 -0700147 sk_sp<SkImageFilter> bg =
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400148 SkImageFilters::Arithmetic(0, 0, -one / 2, 1, enforcePMColor, dstFilter,
149 nullptr, nullptr);
fmalitaad7cb812016-09-29 12:25:26 -0700150 SkPaint p;
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400151 p.setImageFilter(SkImageFilters::Arithmetic(0, one / 2, -one, 1, true,
152 std::move(bg), dstFilter, nullptr));
fmalitaad7cb812016-09-29 12:25:26 -0700153 canvas->saveLayer(&rect, &p);
154 canvas->restore();
155 canvas->translate(gap, 0);
156
157 // Label
Mike Kleinea3f0142019-03-20 11:12:10 -0500158 SkFont font(ToolUtils::create_portable_typeface(), 24);
fmalitaad7cb812016-09-29 12:25:26 -0700159 SkString str(enforcePMColor ? "enforcePM" : "no enforcePM");
Mike Reed4de2f1f2019-01-05 16:35:13 -0500160 canvas->drawString(str, 0, font.getSize(), font, SkPaint());
fmalitaad7cb812016-09-29 12:25:26 -0700161 }
162 canvas->translate(0, HH + 12);
ericrkaf96fce2015-10-19 14:41:11 -0700163 }
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000164 }
165
166private:
167 typedef GM INHERITED;
168};
169
170///////////////////////////////////////////////////////////////////////////////
171
scroggo96f16e82015-12-10 13:31:59 -0800172DEF_GM( return new ArithmodeGM; )