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"
|
2013-05-31 17:49:12 +00:00
|
|
|
#include "SkArithmeticMode.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
|
|
|
|
2015-09-15 18:26:13 +00:00
|
|
|
fCheckerboard.reset(SkImage::NewFromBitmap(
|
|
|
|
sk_tool_utils::create_checkerboard_bitmap(80, 80,
|
|
|
|
sk_tool_utils::color_to_565(0xFFA0A0A0),
|
|
|
|
sk_tool_utils::color_to_565(0xFF404040),
|
|
|
|
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 {
|
|
|
|
SkXfermode::Mode fMode;
|
|
|
|
const char* fLabel;
|
|
|
|
} gModes[] = {
|
|
|
|
{ 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" },
|
|
|
|
|
|
|
|
{ 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" },
|
|
|
|
};
|
|
|
|
|
|
|
|
int x = 0, y = 0;
|
2015-09-15 18:26:13 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> background(SkImageSource::Create(fCheckerboard));
|
2013-05-31 17:49:12 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
|
|
|
|
SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(gModes[i].fMode));
|
2014-03-10 10:51:58 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> filter(SkXfermodeImageFilter::Create(mode, background));
|
2013-05-31 17:49:12 +00:00
|
|
|
paint.setImageFilter(filter);
|
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
|
|
|
|
SkAutoTUnref<SkXfermode> mode(SkArithmeticMode::Create(0, SK_Scalar1, SK_Scalar1, 0));
|
2014-03-10 10:51:58 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> filter(SkXfermodeImageFilter::Create(mode, background));
|
2013-05-31 17:49:12 +00:00
|
|
|
paint.setImageFilter(filter);
|
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
|
|
|
|
filter.reset(SkXfermodeImageFilter::Create(nullptr, background));
|
2013-05-31 17:49:12 +00:00
|
|
|
paint.setImageFilter(filter);
|
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)
|
2015-09-15 18:26:13 +00:00
|
|
|
SkAutoTUnref<SkImage> bitmapImage(SkImage::NewFromBitmap(fBitmap));
|
|
|
|
SkAutoTUnref<SkImageFilter> foreground(SkImageSource::Create(bitmapImage));
|
2014-03-10 10:51:58 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> offsetForeground(SkOffsetImageFilter::Create(
|
|
|
|
SkIntToScalar(4), SkIntToScalar(-4), foreground));
|
|
|
|
SkAutoTUnref<SkImageFilter> offsetBackground(SkOffsetImageFilter::Create(
|
|
|
|
SkIntToScalar(4), SkIntToScalar(4), background));
|
2013-07-10 21:22:18 +00:00
|
|
|
mode.reset(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
|
2014-03-10 10:51:58 +00:00
|
|
|
filter.reset(SkXfermodeImageFilter::Create(mode, offsetBackground, offsetForeground));
|
2013-07-10 21:22:18 +00:00
|
|
|
paint.setImageFilter(filter);
|
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)
|
|
|
|
mode.reset(SkXfermode::Create(SkXfermode::kDarken_Mode));
|
2014-03-10 10:51:58 +00:00
|
|
|
filter.reset(SkXfermodeImageFilter::Create(mode, offsetBackground, offsetForeground));
|
2013-07-10 21:22:18 +00:00
|
|
|
paint.setImageFilter(filter);
|
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
|
|
|
|
static const size_t nbSamples = 3;
|
|
|
|
SkXfermode::Mode sampledModes[nbSamples] = {SkXfermode::kOverlay_Mode,
|
|
|
|
SkXfermode::kSrcOver_Mode,
|
|
|
|
SkXfermode::kPlus_Mode};
|
|
|
|
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));
|
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
|
|
|
mode.reset(SkXfermode::Create(sampledModes[i]));
|
2014-03-10 10:51:58 +00:00
|
|
|
filter.reset(SkXfermodeImageFilter::Create(
|
|
|
|
mode, offsetBackground, offsetForeground, &rect));
|
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
|
|
|
paint.setImageFilter(filter);
|
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)
|
|
|
|
mode.reset(SkXfermode::Create(SkXfermode::kScreen_Mode));
|
|
|
|
SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(10, 10, 60, 60));
|
|
|
|
SkAutoTUnref<SkImageFilter> cropped(
|
|
|
|
SkOffsetImageFilter::Create(0, 0, foreground, &cropRect));
|
|
|
|
filter.reset(SkXfermodeImageFilter::Create(mode, cropped, background));
|
|
|
|
paint.setImageFilter(filter);
|
|
|
|
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)
|
|
|
|
filter.reset(SkXfermodeImageFilter::Create(mode, background, cropped));
|
|
|
|
paint.setImageFilter(filter);
|
|
|
|
DrawClippedPaint(canvas, clipRect, paint, x, y);
|
|
|
|
x += fBitmap.width() + MARGIN;
|
|
|
|
if (x + fBitmap.width() > WIDTH) {
|
|
|
|
x = 0;
|
|
|
|
y += fBitmap.height() + MARGIN;
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
SkAutoTUnref<SkImage> fCheckerboard;
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|