blob: 7a25c2e182dc323f1801335faf7b8f5826521ebe [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 Reed17c574a2018-12-12 18:10:38 -05008#include <SkFont.h>
mike@reedtribe.orge51755f2011-12-10 19:36:56 +00009#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -040010#include "sk_tool_utils.h"
Brian Salomon89cb8212017-01-09 10:48:23 -050011#include "SkArithmeticImageFilter.h"
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000012#include "SkCanvas.h"
13#include "SkColorPriv.h"
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000014#include "SkGradientShader.h"
fmalitaad7cb812016-09-29 12:25:26 -070015#include "SkImage.h"
16#include "SkImageSource.h"
17#include "SkShader.h"
18#include "SkSurface.h"
fmalitaad7cb812016-09-29 12:25:26 -070019
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000020#define WW 100
21#define HH 32
22
fmalitaad7cb812016-09-29 12:25:26 -070023static sk_sp<SkImage> make_src() {
24 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH));
25 SkCanvas* canvas = surface->getCanvas();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000026
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000027 SkPaint paint;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000028 SkPoint pts[] = { {0, 0}, {SkIntToScalar(WW), SkIntToScalar(HH)} };
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000029 SkColor colors[] = {
senorblanco@chromium.org35c733c2013-05-28 19:43:05 +000030 SK_ColorTRANSPARENT, SK_ColorGREEN, SK_ColorCYAN,
31 SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000032 };
reed8a21c9f2016-03-08 18:50:00 -080033 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
34 SkShader::kClamp_TileMode));
fmalitaad7cb812016-09-29 12:25:26 -070035 canvas->drawPaint(paint);
36 return surface->makeImageSnapshot();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000037}
38
fmalitaad7cb812016-09-29 12:25:26 -070039static sk_sp<SkImage> make_dst() {
40 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH));
41 SkCanvas* canvas = surface->getCanvas();
42
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000043 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,
Mike Kleind46dce32018-08-16 10:17:03 -040047 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));
fmalitaad7cb812016-09-29 12:25:26 -070051 canvas->drawPaint(paint);
52 return surface->makeImageSnapshot();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000053}
54
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000055static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
Mike Reed17c574a2018-12-12 18:10:38 -050056 SkFont font(sk_tool_utils::create_portable_typeface(), 24);
57 font.setEdging(SkFont::Edging::kAntiAlias);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000058 SkPaint paint;
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000059 paint.setAntiAlias(true);
60 for (int i = 0; i < 4; ++i) {
61 SkString str;
62 str.appendScalar(k[i]);
Mike Reed17c574a2018-12-12 18:10:38 -050063 SkScalar width = font.measureText(str.c_str(), str.size(), kUTF8_SkTextEncoding);
64 canvas->drawSimpleText(str.c_str(), str.size(), kUTF8_SkTextEncoding, x, y + font.getSize(), font, paint);
reed@google.comeb0fa292011-12-12 22:01:06 +000065 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) {
fmalitaad7cb812016-09-29 12:25:26 -070082 sk_sp<SkImage> src = make_src();
83 sk_sp<SkImage> dst = make_dst();
84 sk_sp<SkImageFilter> srcFilter = SkImageSource::Make(src);
85 sk_sp<SkImageFilter> dstFilter = SkImageSource::Make(dst);
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
mtkleindbfd7ab2016-09-01 11:24:54 -070087 constexpr SkScalar one = SK_Scalar1;
88 constexpr SkScalar K[] = {
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000089 0, 0, 0, 0,
reed@google.comeb0fa292011-12-12 22:01:06 +000090 0, 0, 0, one,
91 0, one, 0, 0,
92 0, 0, one, 0,
93 0, one, one, 0,
94 0, one, -one, 0,
95 0, one/2, one/2, 0,
96 0, one/2, one/2, one/4,
97 0, one/2, one/2, -one/4,
98 one/4, one/2, one/2, 0,
99 -one/4, one/2, one/2, 0,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000100 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000101
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000102 const SkScalar* k = K;
103 const SkScalar* stop = k + SK_ARRAY_COUNT(K);
fmalitaad7cb812016-09-29 12:25:26 -0700104 const SkRect rect = SkRect::MakeWH(WW, HH);
105 SkScalar gap = SkIntToScalar(WW + 20);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000106 while (k < stop) {
fmalitaad7cb812016-09-29 12:25:26 -0700107 {
108 SkAutoCanvasRestore acr(canvas, true);
109 canvas->drawImage(src, 0, 0);
110 canvas->translate(gap, 0);
111 canvas->drawImage(dst, 0, 0);
112 canvas->translate(gap, 0);
113 SkPaint paint;
Brian Salomon89cb8212017-01-09 10:48:23 -0500114 paint.setImageFilter(SkArithmeticImageFilter::Make(k[0], k[1], k[2], k[3], true,
115 dstFilter, srcFilter, nullptr));
fmalitaad7cb812016-09-29 12:25:26 -0700116 canvas->saveLayer(&rect, &paint);
117 canvas->restore();
118
119 canvas->translate(gap, 0);
120 show_k_text(canvas, 0, 0, k);
121 }
122
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000123 k += 4;
fmalitaad7cb812016-09-29 12:25:26 -0700124 canvas->translate(0, HH + 12);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000125 }
ericrkaf96fce2015-10-19 14:41:11 -0700126
127 // Draw two special cases to test enforcePMColor. In these cases, we
128 // draw the dst bitmap twice, the first time it is halved and inverted,
129 // leading to invalid premultiplied colors. If we enforcePMColor, these
130 // invalid values should be clamped, and will not contribute to the
131 // second draw.
132 for (int i = 0; i < 2; i++) {
133 const bool enforcePMColor = (i == 0);
ericrkaf96fce2015-10-19 14:41:11 -0700134
fmalitaad7cb812016-09-29 12:25:26 -0700135 {
136 SkAutoCanvasRestore acr(canvas, true);
137 canvas->translate(gap, 0);
138 canvas->drawImage(dst, 0, 0);
139 canvas->translate(gap, 0);
ericrkaf96fce2015-10-19 14:41:11 -0700140
fmalitaad7cb812016-09-29 12:25:26 -0700141 sk_sp<SkImageFilter> bg =
Mike Reed6b9cd052017-06-18 21:32:48 -0400142 SkArithmeticImageFilter::Make(0, 0, -one / 2, 1, enforcePMColor, dstFilter,
143 nullptr, nullptr);
fmalitaad7cb812016-09-29 12:25:26 -0700144 SkPaint p;
Brian Salomon89cb8212017-01-09 10:48:23 -0500145 p.setImageFilter(SkArithmeticImageFilter::Make(0, one / 2, -one, 1, true,
146 std::move(bg), dstFilter, nullptr));
fmalitaad7cb812016-09-29 12:25:26 -0700147 canvas->saveLayer(&rect, &p);
148 canvas->restore();
149 canvas->translate(gap, 0);
150
151 // Label
Mike Reed4de2f1f2019-01-05 16:35:13 -0500152 SkFont font(sk_tool_utils::create_portable_typeface(), 24);
fmalitaad7cb812016-09-29 12:25:26 -0700153 SkString str(enforcePMColor ? "enforcePM" : "no enforcePM");
Mike Reed4de2f1f2019-01-05 16:35:13 -0500154 canvas->drawString(str, 0, font.getSize(), font, SkPaint());
fmalitaad7cb812016-09-29 12:25:26 -0700155 }
156 canvas->translate(0, HH + 12);
ericrkaf96fce2015-10-19 14:41:11 -0700157 }
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000158 }
159
160private:
161 typedef GM INHERITED;
162};
163
164///////////////////////////////////////////////////////////////////////////////
165
scroggo96f16e82015-12-10 13:31:59 -0800166DEF_GM( return new ArithmodeGM; )