blob: c01ece7990ffd5782d6da8ef5006db8895a429b7 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
Brian Salomon89cb8212017-01-09 10:48:23 -050010#include "SkArithmeticImageFilter.h"
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000011#include "SkCanvas.h"
12#include "SkColorPriv.h"
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000013#include "SkGradientShader.h"
fmalitaad7cb812016-09-29 12:25:26 -070014#include "SkImage.h"
15#include "SkImageSource.h"
16#include "SkShader.h"
17#include "SkSurface.h"
fmalitaad7cb812016-09-29 12:25:26 -070018
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000019#define WW 100
20#define HH 32
21
fmalitaad7cb812016-09-29 12:25:26 -070022static sk_sp<SkImage> make_src() {
23 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH));
24 SkCanvas* canvas = surface->getCanvas();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000025
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000026 SkPaint paint;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000027 SkPoint pts[] = { {0, 0}, {SkIntToScalar(WW), SkIntToScalar(HH)} };
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000028 SkColor colors[] = {
senorblanco@chromium.org35c733c2013-05-28 19:43:05 +000029 SK_ColorTRANSPARENT, SK_ColorGREEN, SK_ColorCYAN,
30 SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000031 };
reed8a21c9f2016-03-08 18:50:00 -080032 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
33 SkShader::kClamp_TileMode));
fmalitaad7cb812016-09-29 12:25:26 -070034 canvas->drawPaint(paint);
35 return surface->makeImageSnapshot();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000036}
37
fmalitaad7cb812016-09-29 12:25:26 -070038static sk_sp<SkImage> make_dst() {
39 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(WW, HH));
40 SkCanvas* canvas = surface->getCanvas();
41
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000042 SkPaint paint;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000043 SkPoint pts[] = { {0, SkIntToScalar(HH)}, {SkIntToScalar(WW), 0} };
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000044 SkColor colors[] = {
caryclarkdfcb7ab2015-07-17 09:39:16 -070045 SK_ColorBLUE, SK_ColorYELLOW, SK_ColorBLACK, SK_ColorGREEN,
46 sk_tool_utils::color_to_565(SK_ColorGRAY)
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000047 };
reed8a21c9f2016-03-08 18:50:00 -080048 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
49 SkShader::kClamp_TileMode));
fmalitaad7cb812016-09-29 12:25:26 -070050 canvas->drawPaint(paint);
51 return surface->makeImageSnapshot();
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000052}
53
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000054static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
55 SkPaint paint;
reed@google.comeb0fa292011-12-12 22:01:06 +000056 paint.setTextSize(SkIntToScalar(24));
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000057 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070058 sk_tool_utils::set_portable_typeface(&paint);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000059 for (int i = 0; i < 4; ++i) {
60 SkString str;
61 str.appendScalar(k[i]);
62 SkScalar width = paint.measureText(str.c_str(), str.size());
reed@google.comeb0fa292011-12-12 22:01:06 +000063 canvas->drawText(str.c_str(), str.size(), x, y + paint.getTextSize(), paint);
64 x += width + SkIntToScalar(10);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000065 }
66}
67
68class ArithmodeGM : public skiagm::GM {
69public:
70 ArithmodeGM () {}
71
72protected:
73
74 virtual SkString onShortName() {
75 return SkString("arithmode");
76 }
77
ericrkaf96fce2015-10-19 14:41:11 -070078 virtual SkISize onISize() { return SkISize::Make(640, 572); }
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000079
80 virtual void onDraw(SkCanvas* canvas) {
fmalitaad7cb812016-09-29 12:25:26 -070081 sk_sp<SkImage> src = make_src();
82 sk_sp<SkImage> dst = make_dst();
83 sk_sp<SkImageFilter> srcFilter = SkImageSource::Make(src);
84 sk_sp<SkImageFilter> dstFilter = SkImageSource::Make(dst);
rmistry@google.comae933ce2012-08-23 18:19:56 +000085
mtkleindbfd7ab2016-09-01 11:24:54 -070086 constexpr SkScalar one = SK_Scalar1;
87 constexpr SkScalar K[] = {
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000088 0, 0, 0, 0,
reed@google.comeb0fa292011-12-12 22:01:06 +000089 0, 0, 0, one,
90 0, one, 0, 0,
91 0, 0, one, 0,
92 0, one, one, 0,
93 0, one, -one, 0,
94 0, one/2, one/2, 0,
95 0, one/2, one/2, one/4,
96 0, one/2, one/2, -one/4,
97 one/4, one/2, one/2, 0,
98 -one/4, one/2, one/2, 0,
mike@reedtribe.orge51755f2011-12-10 19:36:56 +000099 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000101 const SkScalar* k = K;
102 const SkScalar* stop = k + SK_ARRAY_COUNT(K);
fmalitaad7cb812016-09-29 12:25:26 -0700103 const SkRect rect = SkRect::MakeWH(WW, HH);
104 SkScalar gap = SkIntToScalar(WW + 20);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000105 while (k < stop) {
fmalitaad7cb812016-09-29 12:25:26 -0700106 {
107 SkAutoCanvasRestore acr(canvas, true);
108 canvas->drawImage(src, 0, 0);
109 canvas->translate(gap, 0);
110 canvas->drawImage(dst, 0, 0);
111 canvas->translate(gap, 0);
112 SkPaint paint;
Brian Salomon89cb8212017-01-09 10:48:23 -0500113 paint.setImageFilter(SkArithmeticImageFilter::Make(k[0], k[1], k[2], k[3], true,
114 dstFilter, srcFilter, nullptr));
fmalitaad7cb812016-09-29 12:25:26 -0700115 canvas->saveLayer(&rect, &paint);
116 canvas->restore();
117
118 canvas->translate(gap, 0);
119 show_k_text(canvas, 0, 0, k);
120 }
121
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000122 k += 4;
fmalitaad7cb812016-09-29 12:25:26 -0700123 canvas->translate(0, HH + 12);
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000124 }
ericrkaf96fce2015-10-19 14:41:11 -0700125
126 // Draw two special cases to test enforcePMColor. In these cases, we
127 // draw the dst bitmap twice, the first time it is halved and inverted,
128 // leading to invalid premultiplied colors. If we enforcePMColor, these
129 // invalid values should be clamped, and will not contribute to the
130 // second draw.
131 for (int i = 0; i < 2; i++) {
132 const bool enforcePMColor = (i == 0);
ericrkaf96fce2015-10-19 14:41:11 -0700133
fmalitaad7cb812016-09-29 12:25:26 -0700134 {
135 SkAutoCanvasRestore acr(canvas, true);
136 canvas->translate(gap, 0);
137 canvas->drawImage(dst, 0, 0);
138 canvas->translate(gap, 0);
ericrkaf96fce2015-10-19 14:41:11 -0700139
fmalitaad7cb812016-09-29 12:25:26 -0700140 sk_sp<SkImageFilter> bg =
Brian Salomon89cb8212017-01-09 10:48:23 -0500141 SkArithmeticImageFilter::Make(0, 0, -one / 2, 1, enforcePMColor, dstFilter);
fmalitaad7cb812016-09-29 12:25:26 -0700142 SkPaint p;
Brian Salomon89cb8212017-01-09 10:48:23 -0500143 p.setImageFilter(SkArithmeticImageFilter::Make(0, one / 2, -one, 1, true,
144 std::move(bg), dstFilter, nullptr));
fmalitaad7cb812016-09-29 12:25:26 -0700145 canvas->saveLayer(&rect, &p);
146 canvas->restore();
147 canvas->translate(gap, 0);
148
149 // Label
150 SkPaint paint;
151 paint.setTextSize(SkIntToScalar(24));
152 paint.setAntiAlias(true);
153 sk_tool_utils::set_portable_typeface(&paint);
154 SkString str(enforcePMColor ? "enforcePM" : "no enforcePM");
155 canvas->drawText(str.c_str(), str.size(), 0, paint.getTextSize(), paint);
156 }
157 canvas->translate(0, HH + 12);
ericrkaf96fce2015-10-19 14:41:11 -0700158 }
mike@reedtribe.orge51755f2011-12-10 19:36:56 +0000159 }
160
161private:
162 typedef GM INHERITED;
163};
164
165///////////////////////////////////////////////////////////////////////////////
166
scroggo96f16e82015-12-10 13:31:59 -0800167DEF_GM( return new ArithmodeGM; )