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"
|
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,
|
|
|
|
sk_tool_utils::color_to_565(0xFFA0A0A0),
|
|
|
|
sk_tool_utils::color_to_565(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;
|
|
|
|
|
|
|
|
const struct {
|
2016-10-03 18:02:50 +00:00
|
|
|
SkXfermode::Mode fMode;
|
|
|
|
const char* fLabel;
|
2013-05-31 17:49:12 +00:00
|
|
|
} gModes[] = {
|
2016-10-03 18:02:50 +00:00
|
|
|
{ SkXfermode::kClear_Mode, "Clear" },
|
|
|
|
{ SkXfermode::kSrc_Mode, "Src" },
|
|
|
|
{ SkXfermode::kDst_Mode, "Dst" },
|
|
|
|
{ SkXfermode::kSrcOver_Mode, "SrcOver" },
|
|
|
|
{ SkXfermode::kDstOver_Mode, "DstOver" },
|
|
|
|
{ SkXfermode::kSrcIn_Mode, "SrcIn" },
|
|
|
|
{ SkXfermode::kDstIn_Mode, "DstIn" },
|
|
|
|
{ SkXfermode::kSrcOut_Mode, "SrcOut" },
|
|
|
|
{ SkXfermode::kDstOut_Mode, "DstOut" },
|
|
|
|
{ SkXfermode::kSrcATop_Mode, "SrcATop" },
|
|
|
|
{ SkXfermode::kDstATop_Mode, "DstATop" },
|
|
|
|
{ SkXfermode::kXor_Mode, "Xor" },
|
2013-05-31 17:49:12 +00:00
|
|
|
|
2016-10-03 18:02:50 +00:00
|
|
|
{ SkXfermode::kPlus_Mode, "Plus" },
|
|
|
|
{ SkXfermode::kModulate_Mode, "Modulate" },
|
|
|
|
{ SkXfermode::kScreen_Mode, "Screen" },
|
|
|
|
{ SkXfermode::kOverlay_Mode, "Overlay" },
|
|
|
|
{ SkXfermode::kDarken_Mode, "Darken" },
|
|
|
|
{ SkXfermode::kLighten_Mode, "Lighten" },
|
|
|
|
{ SkXfermode::kColorDodge_Mode, "ColorDodge" },
|
|
|
|
{ SkXfermode::kColorBurn_Mode, "ColorBurn" },
|
|
|
|
{ SkXfermode::kHardLight_Mode, "HardLight" },
|
|
|
|
{ SkXfermode::kSoftLight_Mode, "SoftLight" },
|
|
|
|
{ SkXfermode::kDifference_Mode, "Difference" },
|
|
|
|
{ SkXfermode::kExclusion_Mode, "Exclusion" },
|
|
|
|
{ SkXfermode::kMultiply_Mode, "Multiply" },
|
|
|
|
{ SkXfermode::kHue_Mode, "Hue" },
|
|
|
|
{ SkXfermode::kSaturation_Mode, "Saturation" },
|
|
|
|
{ SkXfermode::kColor_Mode, "Color" },
|
|
|
|
{ SkXfermode::kLuminosity_Mode, "Luminosity" },
|
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++) {
|
2016-10-03 18:02:50 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::Make(SkXfermode::Make(gModes[i].fMode),
|
|
|
|
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
|
2016-09-26 22:51:24 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::MakeArithmetic(0, 1, 1, 0, true, 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;
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
// Test nullptr mode
|
2016-10-03 18:02:50 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::Make(nullptr, 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-03 18:02:50 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::Make(
|
|
|
|
SkXfermode::Make(SkXfermode::kSrcOver_Mode),
|
|
|
|
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-03 18:02:50 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::Make(SkXfermode::Make(SkXfermode::kDarken_Mode),
|
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-03 18:02:50 +00:00
|
|
|
SkXfermode::Mode sampledModes[nbSamples] = {SkXfermode::kOverlay_Mode,
|
|
|
|
SkXfermode::kSrcOver_Mode,
|
|
|
|
SkXfermode::kPlus_Mode};
|
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-03 18:02:50 +00:00
|
|
|
paint.setImageFilter(SkXfermodeImageFilter::Make(SkXfermode::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-03 18:02:50 +00:00
|
|
|
auto mode = SkXfermode::Make(SkXfermode::kScreen_Mode);
|
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-03 18:02:50 +00:00
|
|
|
mode = SkXfermode::Make(SkXfermode::kSrcIn_Mode);
|
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,
|
|
|
|
int x, int y) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
canvas->clipRect(SkRect::MakeWH(
|
|
|
|
SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())));
|
|
|
|
canvas->drawBitmap(bitmap, 0, 0, &paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DrawClippedPaint(SkCanvas* canvas, const SkRect& rect, const SkPaint& paint,
|
|
|
|
int x, int y) {
|
|
|
|
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
|
|
|
|
|
|
|
}
|