blob: ea5e7b7f8d0f408e0db34f2f1a6a4e4a49235d24 [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 {
Hal Canarybd865e22019-07-18 11:51:19 -040084 SkString onShortName() override { return SkString("arithmode"); }
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000085
Hal Canarybd865e22019-07-18 11:51:19 -040086 SkISize onISize() override { return {640, 572}; }
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000087
Hal Canarybd865e22019-07-18 11:51:19 -040088 void onDraw(SkCanvas* canvas) override {
fmalitaad7cb812016-09-29 12:25:26 -070089 sk_sp<SkImage> src = make_src();
90 sk_sp<SkImage> dst = make_dst();
91 sk_sp<SkImageFilter> srcFilter = SkImageSource::Make(src);
92 sk_sp<SkImageFilter> dstFilter = SkImageSource::Make(dst);
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
mtkleindbfd7ab2016-09-01 11:24:54 -070094 constexpr SkScalar one = SK_Scalar1;
95 constexpr SkScalar K[] = {
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000096 0, 0, 0, 0,
reed@google.comeb0fa292011-12-12 22:01:06 +000097 0, 0, 0, one,
98 0, one, 0, 0,
99 0, 0, one, 0,
100 0, one, one, 0,
101 0, one, -one, 0,
102 0, one/2, one/2, 0,
103 0, one/2, one/2, one/4,
104 0, one/2, one/2, -one/4,
105 one/4, one/2, one/2, 0,
106 -one/4, one/2, one/2, 0,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000107 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000109 const SkScalar* k = K;
110 const SkScalar* stop = k + SK_ARRAY_COUNT(K);
fmalitaad7cb812016-09-29 12:25:26 -0700111 const SkRect rect = SkRect::MakeWH(WW, HH);
112 SkScalar gap = SkIntToScalar(WW + 20);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000113 while (k < stop) {
fmalitaad7cb812016-09-29 12:25:26 -0700114 {
115 SkAutoCanvasRestore acr(canvas, true);
116 canvas->drawImage(src, 0, 0);
117 canvas->translate(gap, 0);
118 canvas->drawImage(dst, 0, 0);
119 canvas->translate(gap, 0);
120 SkPaint paint;
Brian Salomon89cb8212017-01-09 10:48:23 -0500121 paint.setImageFilter(SkArithmeticImageFilter::Make(k[0], k[1], k[2], k[3], true,
122 dstFilter, srcFilter, nullptr));
fmalitaad7cb812016-09-29 12:25:26 -0700123 canvas->saveLayer(&rect, &paint);
124 canvas->restore();
125
126 canvas->translate(gap, 0);
127 show_k_text(canvas, 0, 0, k);
128 }
129
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000130 k += 4;
fmalitaad7cb812016-09-29 12:25:26 -0700131 canvas->translate(0, HH + 12);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000132 }
ericrkaf96fce2015-10-19 14:41:11 -0700133
134 // Draw two special cases to test enforcePMColor. In these cases, we
135 // draw the dst bitmap twice, the first time it is halved and inverted,
136 // leading to invalid premultiplied colors. If we enforcePMColor, these
137 // invalid values should be clamped, and will not contribute to the
138 // second draw.
139 for (int i = 0; i < 2; i++) {
140 const bool enforcePMColor = (i == 0);
ericrkaf96fce2015-10-19 14:41:11 -0700141
fmalitaad7cb812016-09-29 12:25:26 -0700142 {
143 SkAutoCanvasRestore acr(canvas, true);
144 canvas->translate(gap, 0);
145 canvas->drawImage(dst, 0, 0);
146 canvas->translate(gap, 0);
ericrkaf96fce2015-10-19 14:41:11 -0700147
fmalitaad7cb812016-09-29 12:25:26 -0700148 sk_sp<SkImageFilter> bg =
Mike Reed6b9cd052017-06-18 21:32:48 -0400149 SkArithmeticImageFilter::Make(0, 0, -one / 2, 1, enforcePMColor, dstFilter,
150 nullptr, nullptr);
fmalitaad7cb812016-09-29 12:25:26 -0700151 SkPaint p;
Brian Salomon89cb8212017-01-09 10:48:23 -0500152 p.setImageFilter(SkArithmeticImageFilter::Make(0, one / 2, -one, 1, true,
153 std::move(bg), dstFilter, nullptr));
fmalitaad7cb812016-09-29 12:25:26 -0700154 canvas->saveLayer(&rect, &p);
155 canvas->restore();
156 canvas->translate(gap, 0);
157
158 // Label
Mike Kleinea3f0142019-03-20 11:12:10 -0500159 SkFont font(ToolUtils::create_portable_typeface(), 24);
fmalitaad7cb812016-09-29 12:25:26 -0700160 SkString str(enforcePMColor ? "enforcePM" : "no enforcePM");
Mike Reed4de2f1f2019-01-05 16:35:13 -0500161 canvas->drawString(str, 0, font.getSize(), font, SkPaint());
fmalitaad7cb812016-09-29 12:25:26 -0700162 }
163 canvas->translate(0, HH + 12);
ericrkaf96fce2015-10-19 14:41:11 -0700164 }
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000165 }
166
167private:
168 typedef GM INHERITED;
169};
170
171///////////////////////////////////////////////////////////////////////////////
172
scroggo96f16e82015-12-10 13:31:59 -0800173DEF_GM( return new ArithmodeGM; )