blob: 5603c784560f22686dcd4d43e597dc795f307342 [file] [log] [blame]
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +00001/*
2 * Copyright 2013 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"
halcanaryb0cce2c2015-01-26 12:49:00 -08009#include "sk_tool_utils.h"
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +000010#include "SkArithmeticMode.h"
fmalita5598b632015-09-15 11:26:13 -070011#include "SkImage.h"
12#include "SkImageSource.h"
commit-bot@chromium.org7b320702013-07-10 21:22:18 +000013#include "SkOffsetImageFilter.h"
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +000014#include "SkXfermodeImageFilter.h"
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +000015
16#define WIDTH 600
senorblanco06d54ad2016-01-13 13:48:54 -080017#define HEIGHT 700
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +000018#define MARGIN 12
19
20namespace skiagm {
21
22class XfermodeImageFilterGM : public GM {
23public:
senorblanco@chromium.orge93e1db2013-12-09 18:31:42 +000024 XfermodeImageFilterGM(){
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +000025 this->setBGColor(0xFF000000);
26 }
27
28protected:
mtklein36352bf2015-03-25 18:17:31 -070029 SkString onShortName() override {
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +000030 return SkString("xfermodeimagefilter");
31 }
32
mtklein36352bf2015-03-25 18:17:31 -070033 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070034 return SkISize::Make(WIDTH, HEIGHT);
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +000035 }
36
mtklein36352bf2015-03-25 18:17:31 -070037 void onOnceBeforeDraw() override {
robertphillips943a4622015-09-03 13:32:33 -070038 fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xD000D000, 15, 65, 96, "e");
halcanary878fa022015-01-26 11:24:32 -080039
reed9ce9d672016-03-17 10:51:11 -070040 fCheckerboard = SkImage::MakeFromBitmap(
fmalita5598b632015-09-15 11:26:13 -070041 sk_tool_utils::create_checkerboard_bitmap(80, 80,
42 sk_tool_utils::color_to_565(0xFFA0A0A0),
43 sk_tool_utils::color_to_565(0xFF404040),
reed9ce9d672016-03-17 10:51:11 -070044 8));
senorblanco@chromium.orge93e1db2013-12-09 18:31:42 +000045 }
46
mtklein36352bf2015-03-25 18:17:31 -070047 void onDraw(SkCanvas* canvas) override {
senorblanco16b254a2015-04-09 11:13:24 -070048 canvas->clear(SK_ColorBLACK);
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +000049 SkPaint paint;
50
51 const struct {
52 SkXfermode::Mode fMode;
53 const char* fLabel;
54 } gModes[] = {
55 { SkXfermode::kClear_Mode, "Clear" },
56 { SkXfermode::kSrc_Mode, "Src" },
57 { SkXfermode::kDst_Mode, "Dst" },
58 { SkXfermode::kSrcOver_Mode, "SrcOver" },
59 { SkXfermode::kDstOver_Mode, "DstOver" },
60 { SkXfermode::kSrcIn_Mode, "SrcIn" },
61 { SkXfermode::kDstIn_Mode, "DstIn" },
62 { SkXfermode::kSrcOut_Mode, "SrcOut" },
63 { SkXfermode::kDstOut_Mode, "DstOut" },
64 { SkXfermode::kSrcATop_Mode, "SrcATop" },
65 { SkXfermode::kDstATop_Mode, "DstATop" },
66 { SkXfermode::kXor_Mode, "Xor" },
67
68 { SkXfermode::kPlus_Mode, "Plus" },
69 { SkXfermode::kModulate_Mode, "Modulate" },
70 { SkXfermode::kScreen_Mode, "Screen" },
71 { SkXfermode::kOverlay_Mode, "Overlay" },
72 { SkXfermode::kDarken_Mode, "Darken" },
73 { SkXfermode::kLighten_Mode, "Lighten" },
74 { SkXfermode::kColorDodge_Mode, "ColorDodge" },
75 { SkXfermode::kColorBurn_Mode, "ColorBurn" },
76 { SkXfermode::kHardLight_Mode, "HardLight" },
77 { SkXfermode::kSoftLight_Mode, "SoftLight" },
78 { SkXfermode::kDifference_Mode, "Difference" },
79 { SkXfermode::kExclusion_Mode, "Exclusion" },
80 { SkXfermode::kMultiply_Mode, "Multiply" },
81 { SkXfermode::kHue_Mode, "Hue" },
82 { SkXfermode::kSaturation_Mode, "Saturation" },
83 { SkXfermode::kColor_Mode, "Color" },
84 { SkXfermode::kLuminosity_Mode, "Luminosity" },
85 };
86
87 int x = 0, y = 0;
reed9ce9d672016-03-17 10:51:11 -070088 SkAutoTUnref<SkImageFilter> background(SkImageSource::Create(fCheckerboard.get()));
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +000089 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
reedcfb6bdf2016-03-29 11:32:50 -070090 paint.setImageFilter(SkXfermodeImageFilter::Make(SkXfermode::Make(gModes[i].fMode),
91 background));
fmalita5598b632015-09-15 11:26:13 -070092 DrawClippedBitmap(canvas, fBitmap, paint, x, y);
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +000093 x += fBitmap.width() + MARGIN;
94 if (x + fBitmap.width() > WIDTH) {
95 x = 0;
96 y += fBitmap.height() + MARGIN;
97 }
98 }
99 // Test arithmetic mode as image filter
reedcfb6bdf2016-03-29 11:32:50 -0700100 paint.setImageFilter(SkXfermodeImageFilter::Make(
101 SkArithmeticMode::Make(0, SK_Scalar1, SK_Scalar1, 0), background));
fmalita5598b632015-09-15 11:26:13 -0700102 DrawClippedBitmap(canvas, fBitmap, paint, x, y);
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +0000103 x += fBitmap.width() + MARGIN;
104 if (x + fBitmap.width() > WIDTH) {
105 x = 0;
106 y += fBitmap.height() + MARGIN;
107 }
halcanary96fcdcc2015-08-27 07:41:13 -0700108 // Test nullptr mode
reedcfb6bdf2016-03-29 11:32:50 -0700109 paint.setImageFilter(SkXfermodeImageFilter::Make(nullptr, background));
fmalita5598b632015-09-15 11:26:13 -0700110 DrawClippedBitmap(canvas, fBitmap, paint, x, y);
commit-bot@chromium.org7b320702013-07-10 21:22:18 +0000111 x += fBitmap.width() + MARGIN;
112 if (x + fBitmap.width() > WIDTH) {
113 x = 0;
114 y += fBitmap.height() + MARGIN;
115 }
senorblanco@chromium.orge93e1db2013-12-09 18:31:42 +0000116 SkRect clipRect = SkRect::MakeWH(SkIntToScalar(fBitmap.width() + 4),
117 SkIntToScalar(fBitmap.height() + 4));
commit-bot@chromium.org7b320702013-07-10 21:22:18 +0000118 // Test offsets on SrcMode (uses fixed-function blend)
reed9ce9d672016-03-17 10:51:11 -0700119 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(fBitmap));
120 SkAutoTUnref<SkImageFilter> foreground(SkImageSource::Create(bitmapImage.get()));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000121 SkAutoTUnref<SkImageFilter> offsetForeground(SkOffsetImageFilter::Create(
122 SkIntToScalar(4), SkIntToScalar(-4), foreground));
123 SkAutoTUnref<SkImageFilter> offsetBackground(SkOffsetImageFilter::Create(
124 SkIntToScalar(4), SkIntToScalar(4), background));
reedcfb6bdf2016-03-29 11:32:50 -0700125 paint.setImageFilter(SkXfermodeImageFilter::Make(
126 SkXfermode::Make(SkXfermode::kSrcOver_Mode), offsetBackground,
127 offsetForeground, nullptr));
fmalita5598b632015-09-15 11:26:13 -0700128 DrawClippedPaint(canvas, clipRect, paint, x, y);
commit-bot@chromium.org7b320702013-07-10 21:22:18 +0000129 x += fBitmap.width() + MARGIN;
130 if (x + fBitmap.width() > WIDTH) {
131 x = 0;
132 y += fBitmap.height() + MARGIN;
133 }
134 // Test offsets on Darken (uses shader blend)
reedcfb6bdf2016-03-29 11:32:50 -0700135 paint.setImageFilter(SkXfermodeImageFilter::Make(SkXfermode::Make(SkXfermode::kDarken_Mode),
136 offsetBackground, offsetForeground, nullptr));
fmalita5598b632015-09-15 11:26:13 -0700137 DrawClippedPaint(canvas, clipRect, paint, x, y);
commit-bot@chromium.org1a4fb702013-09-26 16:09:28 +0000138 x += fBitmap.width() + MARGIN;
139 if (x + fBitmap.width() > WIDTH) {
140 x = 0;
141 y += fBitmap.height() + MARGIN;
142 }
143 // Test cropping
144 static const size_t nbSamples = 3;
145 SkXfermode::Mode sampledModes[nbSamples] = {SkXfermode::kOverlay_Mode,
146 SkXfermode::kSrcOver_Mode,
147 SkXfermode::kPlus_Mode};
148 int offsets[nbSamples][4] = {{ 10, 10, -16, -16},
149 { 10, 10, 10, 10},
150 {-10, -10, -6, -6}};
151 for (size_t i = 0; i < nbSamples; ++i) {
senorblanco@chromium.orge93e1db2013-12-09 18:31:42 +0000152 SkIRect cropRect = SkIRect::MakeXYWH(offsets[i][0],
153 offsets[i][1],
commit-bot@chromium.org1a4fb702013-09-26 16:09:28 +0000154 fBitmap.width() + offsets[i][2],
155 fBitmap.height() + offsets[i][3]);
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +0000156 SkImageFilter::CropRect rect(SkRect::Make(cropRect));
reedcfb6bdf2016-03-29 11:32:50 -0700157 paint.setImageFilter(SkXfermodeImageFilter::Make(
158 SkXfermode::Make(sampledModes[i]), offsetBackground, offsetForeground, &rect));
fmalita5598b632015-09-15 11:26:13 -0700159 DrawClippedPaint(canvas, clipRect, paint, x, y);
commit-bot@chromium.org1a4fb702013-09-26 16:09:28 +0000160 x += fBitmap.width() + MARGIN;
161 if (x + fBitmap.width() > WIDTH) {
162 x = 0;
163 y += fBitmap.height() + MARGIN;
164 }
165 }
senorblanco06d54ad2016-01-13 13:48:54 -0800166 // Test small bg, large fg with Screen (uses shader blend)
reedcfb6bdf2016-03-29 11:32:50 -0700167 auto mode = SkXfermode::Make(SkXfermode::kScreen_Mode);
senorblanco06d54ad2016-01-13 13:48:54 -0800168 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(10, 10, 60, 60));
169 SkAutoTUnref<SkImageFilter> cropped(
170 SkOffsetImageFilter::Create(0, 0, foreground, &cropRect));
reedcfb6bdf2016-03-29 11:32:50 -0700171 paint.setImageFilter(SkXfermodeImageFilter::Make(mode, cropped, background, nullptr));
senorblanco06d54ad2016-01-13 13:48:54 -0800172 DrawClippedPaint(canvas, clipRect, paint, x, y);
173 x += fBitmap.width() + MARGIN;
174 if (x + fBitmap.width() > WIDTH) {
175 x = 0;
176 y += fBitmap.height() + MARGIN;
177 }
178 // Test small fg, large bg with Screen (uses shader blend)
reedcfb6bdf2016-03-29 11:32:50 -0700179 paint.setImageFilter(SkXfermodeImageFilter::Make(mode, background, cropped, nullptr));
senorblanco06d54ad2016-01-13 13:48:54 -0800180 DrawClippedPaint(canvas, clipRect, paint, x, y);
181 x += fBitmap.width() + MARGIN;
182 if (x + fBitmap.width() > WIDTH) {
183 x = 0;
184 y += fBitmap.height() + MARGIN;
185 }
senorblanco9db04272016-03-31 08:24:29 -0700186 // Test small fg, large bg with SrcIn with a crop that forces it to full size.
187 // This tests that SkXfermodeImageFilter correctly applies the compositing mode to
188 // the region outside the foreground.
189 mode = SkXfermode::Make(SkXfermode::kSrcIn_Mode);
190 SkImageFilter::CropRect cropRectFull(SkRect::MakeXYWH(0, 0, 80, 80));
191 paint.setImageFilter(SkXfermodeImageFilter::Make(mode, background, cropped, &cropRectFull));
192 DrawClippedPaint(canvas, clipRect, paint, x, y);
193 x += fBitmap.width() + MARGIN;
194 if (x + fBitmap.width() > WIDTH) {
195 x = 0;
196 y += fBitmap.height() + MARGIN;
197 }
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +0000198 }
fmalita5598b632015-09-15 11:26:13 -0700199
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +0000200private:
fmalita5598b632015-09-15 11:26:13 -0700201 static void DrawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint,
202 int x, int y) {
203 canvas->save();
204 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
205 canvas->clipRect(SkRect::MakeWH(
206 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())));
207 canvas->drawBitmap(bitmap, 0, 0, &paint);
208 canvas->restore();
209 }
210
211 static void DrawClippedPaint(SkCanvas* canvas, const SkRect& rect, const SkPaint& paint,
212 int x, int y) {
213 canvas->save();
214 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
215 canvas->clipRect(rect);
216 canvas->drawPaint(paint);
217 canvas->restore();
218 }
219
reed9ce9d672016-03-17 10:51:11 -0700220 SkBitmap fBitmap;
221 sk_sp<SkImage> fCheckerboard;
fmalita5598b632015-09-15 11:26:13 -0700222
senorblanco@chromium.orge93e1db2013-12-09 18:31:42 +0000223 typedef GM INHERITED;
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +0000224};
225
226//////////////////////////////////////////////////////////////////////////////
227
senorblanco@chromium.orge93e1db2013-12-09 18:31:42 +0000228DEF_GM( return new XfermodeImageFilterGM; );
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +0000229
230}