2013-05-31 17:49:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2015-01-26 20:49:00 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2017-01-09 15:48:23 +00:00
|
|
|
#include "SkArithmeticImageFilter.h"
|
2015-09-15 18:26:13 +00:00
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkImageSource.h"
|
2013-07-10 21:22:18 +00:00
|
|
|
#include "SkOffsetImageFilter.h"
|
2013-05-31 17:49:12 +00:00
|
|
|
#include "SkXfermodeImageFilter.h"
|
|
|
|
|
|
|
|
#define WIDTH 600
|
2016-01-13 21:48:54 +00:00
|
|
|
#define HEIGHT 700
|
2013-05-31 17:49:12 +00:00
|
|
|
#define MARGIN 12
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class XfermodeImageFilterGM : public GM {
|
|
|
|
public:
|
2013-12-09 18:31:42 +00:00
|
|
|
XfermodeImageFilterGM(){
|
2013-05-31 17:49:12 +00:00
|
|
|
this->setBGColor(0xFF000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-05-31 17:49:12 +00:00
|
|
|
return SkString("xfermodeimagefilter");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(WIDTH, HEIGHT);
|
2013-05-31 17:49:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2015-09-03 20:32:33 +00:00
|
|
|
fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xD000D000, 15, 65, 96, "e");
|
2015-01-26 19:24:32 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
fCheckerboard = SkImage::MakeFromBitmap(
|
2015-09-15 18:26:13 +00:00
|
|
|
sk_tool_utils::create_checkerboard_bitmap(80, 80,
|
2018-08-16 14:17:03 +00:00
|
|
|
0xFFA0A0A0,
|
|
|
|
0xFF404040,
|
2016-03-17 17:51:11 +00:00
|
|
|
8));
|
2013-12-09 18:31:42 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2015-04-09 18:13:24 +00:00
|
|
|
canvas->clear(SK_ColorBLACK);
|
2013-05-31 17:49:12 +00:00
|
|
|
SkPaint paint;
|
|
|
|
|
2017-03-15 16:19:37 +00:00
|
|
|
const SkBlendMode gModes[] = {
|
|
|
|
SkBlendMode::kClear,
|
|
|
|
SkBlendMode::kSrc,
|
|
|
|
SkBlendMode::kDst,
|
|
|
|
SkBlendMode::kSrcOver,
|
|
|
|
SkBlendMode::kDstOver,
|
|
|
|
SkBlendMode::kSrcIn,
|
|
|
|
SkBlendMode::kDstIn,
|
|
|
|
SkBlendMode::kSrcOut,
|
|
|
|
SkBlendMode::kDstOut,
|
|
|
|
SkBlendMode::kSrcATop,
|
|
|
|
SkBlendMode::kDstATop,
|
|
|
|
SkBlendMode::kXor,
|
2013-05-31 17:49:12 +00:00
|
|
|
|
2017-03-15 16:19:37 +00:00
|
|
|
SkBlendMode::kPlus,
|
|
|
|
SkBlendMode::kModulate,
|
|
|
|
SkBlendMode::kScreen,
|
|
|
|
SkBlendMode::kOverlay,
|
|
|
|
SkBlendMode::kDarken,
|
|
|
|
SkBlendMode::kLighten,
|
|
|
|
SkBlendMode::kColorDodge,
|
|
|
|
SkBlendMode::kColorBurn,
|
|
|
|
SkBlendMode::kHardLight,
|
|
|
|
SkBlendMode::kSoftLight,
|
|
|
|
SkBlendMode::kDifference,
|
|
|
|
SkBlendMode::kExclusion,
|
|
|
|
SkBlendMode::kMultiply,
|
|
|
|
SkBlendMode::kHue,
|
|
|
|
SkBlendMode::kSaturation,
|
|
|
|
SkBlendMode::kColor,
|
|
|
|
SkBlendMode::kLuminosity,
|
2013-05-31 17:49:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int x = 0, y = 0;
|
2016-04-01 16:28:51 +00:00
|
|
|
sk_sp<SkImageFilter> background(SkImageSource::Make(fCheckerboard));
|
2013-05-31 17:49:12 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
|
2017-03-15 16:19:37 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::Make(gModes[i], background));
|
2015-09-15 18:26:13 +00:00
|
|
|
DrawClippedBitmap(canvas, fBitmap, paint, x, y);
|
2013-05-31 17:49:12 +00:00
|
|
|
x += fBitmap.width() + MARGIN;
|
|
|
|
if (x + fBitmap.width() > WIDTH) {
|
|
|
|
x = 0;
|
|
|
|
y += fBitmap.height() + MARGIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Test arithmetic mode as image filter
|
2017-06-19 01:32:48 +00:00
|
|
|
paint.setImageFilter(SkArithmeticImageFilter::Make(0, 1, 1, 0, true, background,
|
|
|
|
nullptr, nullptr));
|
2015-09-15 18:26:13 +00:00
|
|
|
DrawClippedBitmap(canvas, fBitmap, paint, x, y);
|
2013-05-31 17:49:12 +00:00
|
|
|
x += fBitmap.width() + MARGIN;
|
|
|
|
if (x + fBitmap.width() > WIDTH) {
|
|
|
|
x = 0;
|
|
|
|
y += fBitmap.height() + MARGIN;
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
// Test nullptr mode
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::Make(SkBlendMode::kSrcOver, background));
|
2015-09-15 18:26:13 +00:00
|
|
|
DrawClippedBitmap(canvas, fBitmap, paint, x, y);
|
2013-07-10 21:22:18 +00:00
|
|
|
x += fBitmap.width() + MARGIN;
|
|
|
|
if (x + fBitmap.width() > WIDTH) {
|
|
|
|
x = 0;
|
|
|
|
y += fBitmap.height() + MARGIN;
|
|
|
|
}
|
2013-12-09 18:31:42 +00:00
|
|
|
SkRect clipRect = SkRect::MakeWH(SkIntToScalar(fBitmap.width() + 4),
|
|
|
|
SkIntToScalar(fBitmap.height() + 4));
|
2013-07-10 21:22:18 +00:00
|
|
|
// Test offsets on SrcMode (uses fixed-function blend)
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(fBitmap));
|
2016-04-01 16:28:51 +00:00
|
|
|
sk_sp<SkImageFilter> foreground(SkImageSource::Make(std::move(bitmapImage)));
|
2016-03-31 16:05:49 +00:00
|
|
|
sk_sp<SkImageFilter> offsetForeground(SkOffsetImageFilter::Make(SkIntToScalar(4),
|
|
|
|
SkIntToScalar(-4),
|
|
|
|
foreground));
|
|
|
|
sk_sp<SkImageFilter> offsetBackground(SkOffsetImageFilter::Make(SkIntToScalar(4),
|
|
|
|
SkIntToScalar(4),
|
|
|
|
background));
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::Make(SkBlendMode::kSrcOver,
|
|
|
|
offsetBackground,
|
|
|
|
offsetForeground,
|
|
|
|
nullptr));
|
2015-09-15 18:26:13 +00:00
|
|
|
DrawClippedPaint(canvas, clipRect, paint, x, y);
|
2013-07-10 21:22:18 +00:00
|
|
|
x += fBitmap.width() + MARGIN;
|
|
|
|
if (x + fBitmap.width() > WIDTH) {
|
|
|
|
x = 0;
|
|
|
|
y += fBitmap.height() + MARGIN;
|
|
|
|
}
|
|
|
|
// Test offsets on Darken (uses shader blend)
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::Make(SkBlendMode::kDarken,
|
2016-04-05 19:48:34 +00:00
|
|
|
offsetBackground,
|
|
|
|
offsetForeground,
|
2016-03-31 16:05:49 +00:00
|
|
|
nullptr));
|
2015-09-15 18:26:13 +00:00
|
|
|
DrawClippedPaint(canvas, clipRect, paint, x, y);
|
Moving 4 SkImageFilter derived classes from blink to skia
There were 4 classes in blink that derived from SkImageFilter :
- TileImageFilter -> SkTileImageFilter
- OffsetImageFilter -> SkOffsetImageFilter (already existed)
- FloodImageFilter -> SkFloodImageFilter
- CompositeImageFilter -> SkCompositeImageFilter
All functions were copied as is, without modification (except for warnings fixes), except for the offset filter, which was merged into the existing SkOffsetImageFilter class, as a special case when a crop rect is provided. Since the names won't clash with the names in blink, it should be easy to integrate them in blink later and fix issues, if needed.
BUG=
R=senorblanco@google.com, senorblanco@chromium.org, bsalomon@google.com, reed@google.com, mtklein@google.com
Author: sugoi@chromium.org
Review URL: https://chromiumcodereview.appspot.com/24157005
git-svn-id: http://skia.googlecode.com/svn/trunk@11475 2bbb7eff-a529-9590-31e7-b0007b416f81
2013-09-26 16:09:28 +00:00
|
|
|
x += fBitmap.width() + MARGIN;
|
|
|
|
if (x + fBitmap.width() > WIDTH) {
|
|
|
|
x = 0;
|
|
|
|
y += fBitmap.height() + MARGIN;
|
|
|
|
}
|
|
|
|
// Test cropping
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr size_t nbSamples = 3;
|
2016-10-06 00:33:02 +00:00
|
|
|
const SkBlendMode sampledModes[nbSamples] = {
|
|
|
|
SkBlendMode::kOverlay, SkBlendMode::kSrcOver, SkBlendMode::kPlus
|
|
|
|
};
|
Moving 4 SkImageFilter derived classes from blink to skia
There were 4 classes in blink that derived from SkImageFilter :
- TileImageFilter -> SkTileImageFilter
- OffsetImageFilter -> SkOffsetImageFilter (already existed)
- FloodImageFilter -> SkFloodImageFilter
- CompositeImageFilter -> SkCompositeImageFilter
All functions were copied as is, without modification (except for warnings fixes), except for the offset filter, which was merged into the existing SkOffsetImageFilter class, as a special case when a crop rect is provided. Since the names won't clash with the names in blink, it should be easy to integrate them in blink later and fix issues, if needed.
BUG=
R=senorblanco@google.com, senorblanco@chromium.org, bsalomon@google.com, reed@google.com, mtklein@google.com
Author: sugoi@chromium.org
Review URL: https://chromiumcodereview.appspot.com/24157005
git-svn-id: http://skia.googlecode.com/svn/trunk@11475 2bbb7eff-a529-9590-31e7-b0007b416f81
2013-09-26 16:09:28 +00:00
|
|
|
int offsets[nbSamples][4] = {{ 10, 10, -16, -16},
|
|
|
|
{ 10, 10, 10, 10},
|
|
|
|
{-10, -10, -6, -6}};
|
|
|
|
for (size_t i = 0; i < nbSamples; ++i) {
|
2013-12-09 18:31:42 +00:00
|
|
|
SkIRect cropRect = SkIRect::MakeXYWH(offsets[i][0],
|
|
|
|
offsets[i][1],
|
Moving 4 SkImageFilter derived classes from blink to skia
There were 4 classes in blink that derived from SkImageFilter :
- TileImageFilter -> SkTileImageFilter
- OffsetImageFilter -> SkOffsetImageFilter (already existed)
- FloodImageFilter -> SkFloodImageFilter
- CompositeImageFilter -> SkCompositeImageFilter
All functions were copied as is, without modification (except for warnings fixes), except for the offset filter, which was merged into the existing SkOffsetImageFilter class, as a special case when a crop rect is provided. Since the names won't clash with the names in blink, it should be easy to integrate them in blink later and fix issues, if needed.
BUG=
R=senorblanco@google.com, senorblanco@chromium.org, bsalomon@google.com, reed@google.com, mtklein@google.com
Author: sugoi@chromium.org
Review URL: https://chromiumcodereview.appspot.com/24157005
git-svn-id: http://skia.googlecode.com/svn/trunk@11475 2bbb7eff-a529-9590-31e7-b0007b416f81
2013-09-26 16:09:28 +00:00
|
|
|
fBitmap.width() + offsets[i][2],
|
|
|
|
fBitmap.height() + offsets[i][3]);
|
2013-10-10 13:51:19 +00:00
|
|
|
SkImageFilter::CropRect rect(SkRect::Make(cropRect));
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::Make(sampledModes[i],
|
2016-04-05 19:48:34 +00:00
|
|
|
offsetBackground,
|
|
|
|
offsetForeground,
|
2016-03-31 16:05:49 +00:00
|
|
|
&rect));
|
2015-09-15 18:26:13 +00:00
|
|
|
DrawClippedPaint(canvas, clipRect, paint, x, y);
|
Moving 4 SkImageFilter derived classes from blink to skia
There were 4 classes in blink that derived from SkImageFilter :
- TileImageFilter -> SkTileImageFilter
- OffsetImageFilter -> SkOffsetImageFilter (already existed)
- FloodImageFilter -> SkFloodImageFilter
- CompositeImageFilter -> SkCompositeImageFilter
All functions were copied as is, without modification (except for warnings fixes), except for the offset filter, which was merged into the existing SkOffsetImageFilter class, as a special case when a crop rect is provided. Since the names won't clash with the names in blink, it should be easy to integrate them in blink later and fix issues, if needed.
BUG=
R=senorblanco@google.com, senorblanco@chromium.org, bsalomon@google.com, reed@google.com, mtklein@google.com
Author: sugoi@chromium.org
Review URL: https://chromiumcodereview.appspot.com/24157005
git-svn-id: http://skia.googlecode.com/svn/trunk@11475 2bbb7eff-a529-9590-31e7-b0007b416f81
2013-09-26 16:09:28 +00:00
|
|
|
x += fBitmap.width() + MARGIN;
|
|
|
|
if (x + fBitmap.width() > WIDTH) {
|
|
|
|
x = 0;
|
|
|
|
y += fBitmap.height() + MARGIN;
|
|
|
|
}
|
|
|
|
}
|
2016-01-13 21:48:54 +00:00
|
|
|
// Test small bg, large fg with Screen (uses shader blend)
|
2016-10-06 00:33:02 +00:00
|
|
|
SkBlendMode mode = SkBlendMode::kScreen;
|
2016-01-13 21:48:54 +00:00
|
|
|
SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(10, 10, 60, 60));
|
2016-03-31 16:05:49 +00:00
|
|
|
sk_sp<SkImageFilter> cropped(SkOffsetImageFilter::Make(0, 0, foreground, &cropRect));
|
2016-04-05 19:48:34 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::Make(mode, cropped, background, nullptr));
|
2016-01-13 21:48:54 +00:00
|
|
|
DrawClippedPaint(canvas, clipRect, paint, x, y);
|
|
|
|
x += fBitmap.width() + MARGIN;
|
|
|
|
if (x + fBitmap.width() > WIDTH) {
|
|
|
|
x = 0;
|
|
|
|
y += fBitmap.height() + MARGIN;
|
|
|
|
}
|
|
|
|
// Test small fg, large bg with Screen (uses shader blend)
|
2016-04-05 19:48:34 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::Make(mode, background, cropped, nullptr));
|
2016-01-13 21:48:54 +00:00
|
|
|
DrawClippedPaint(canvas, clipRect, paint, x, y);
|
|
|
|
x += fBitmap.width() + MARGIN;
|
|
|
|
if (x + fBitmap.width() > WIDTH) {
|
|
|
|
x = 0;
|
|
|
|
y += fBitmap.height() + MARGIN;
|
2016-03-31 15:24:29 +00:00
|
|
|
}
|
|
|
|
// Test small fg, large bg with SrcIn with a crop that forces it to full size.
|
|
|
|
// This tests that SkXfermodeImageFilter correctly applies the compositing mode to
|
|
|
|
// the region outside the foreground.
|
2016-10-06 00:33:02 +00:00
|
|
|
mode = SkBlendMode::kSrcIn;
|
2016-03-31 15:24:29 +00:00
|
|
|
SkImageFilter::CropRect cropRectFull(SkRect::MakeXYWH(0, 0, 80, 80));
|
2016-04-05 19:48:34 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::Make(mode, background,
|
|
|
|
cropped, &cropRectFull));
|
2016-03-31 15:24:29 +00:00
|
|
|
DrawClippedPaint(canvas, clipRect, paint, x, y);
|
|
|
|
x += fBitmap.width() + MARGIN;
|
|
|
|
if (x + fBitmap.width() > WIDTH) {
|
|
|
|
x = 0;
|
|
|
|
y += fBitmap.height() + MARGIN;
|
2016-01-13 21:48:54 +00:00
|
|
|
}
|
2013-05-31 17:49:12 +00:00
|
|
|
}
|
2015-09-15 18:26:13 +00:00
|
|
|
|
2013-05-31 17:49:12 +00:00
|
|
|
private:
|
2015-09-15 18:26:13 +00:00
|
|
|
static void DrawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint,
|
2017-04-11 16:54:57 +00:00
|
|
|
int x, int y) {
|
2015-09-15 18:26:13 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
2017-04-11 16:54:57 +00:00
|
|
|
canvas->clipRect(SkRect::MakeIWH(bitmap.width(), bitmap.height()));
|
2015-09-15 18:26:13 +00:00
|
|
|
canvas->drawBitmap(bitmap, 0, 0, &paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DrawClippedPaint(SkCanvas* canvas, const SkRect& rect, const SkPaint& paint,
|
2017-04-11 16:54:57 +00:00
|
|
|
int x, int y) {
|
2015-09-15 18:26:13 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
canvas->clipRect(rect);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
SkBitmap fBitmap;
|
|
|
|
sk_sp<SkImage> fCheckerboard;
|
2015-09-15 18:26:13 +00:00
|
|
|
|
2013-12-09 18:31:42 +00:00
|
|
|
typedef GM INHERITED;
|
2013-05-31 17:49:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-12-09 18:31:42 +00:00
|
|
|
DEF_GM( return new XfermodeImageFilterGM; );
|
2013-05-31 17:49:12 +00:00
|
|
|
|
|
|
|
}
|