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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageFilter.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2013-05-31 17:49:12 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2013-05-31 17:49:12 +00:00
|
|
|
#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 {
|
2019-03-20 16:12:10 +00:00
|
|
|
fBitmap = ToolUtils::create_string_bitmap(80, 80, 0xD000D000, 15, 65, 96, "e");
|
2015-01-26 19:24:32 +00:00
|
|
|
|
2020-12-23 15:11:33 +00:00
|
|
|
fCheckerboard = ToolUtils::create_checkerboard_image(80, 80, 0xFFA0A0A0, 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;
|
|
|
|
|
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;
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> background(SkImageFilters::Image(fCheckerboard));
|
2022-06-21 13:49:17 +00:00
|
|
|
for (size_t i = 0; i < std::size(gModes); i++) {
|
2020-10-09 14:45:07 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Blend(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
|
2019-08-02 19:21:23 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Arithmetic(0, 1, 1, 0, true, background, 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
|
2020-10-09 14:45:07 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Blend(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)
|
2020-12-23 15:11:33 +00:00
|
|
|
sk_sp<SkImage> bitmapImage(fBitmap.asImage());
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> foreground(SkImageFilters::Image(std::move(bitmapImage)));
|
|
|
|
sk_sp<SkImageFilter> offsetForeground(SkImageFilters::Offset(4, -4, foreground));
|
|
|
|
sk_sp<SkImageFilter> offsetBackground(SkImageFilters::Offset(4, 4, background));
|
2020-10-09 14:45:07 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Blend(
|
2019-08-02 19:21:23 +00:00
|
|
|
SkBlendMode::kSrcOver, offsetBackground, offsetForeground));
|
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)
|
2020-10-09 14:45:07 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Blend(
|
2019-08-02 19:21:23 +00:00
|
|
|
SkBlendMode::kDarken, offsetBackground, offsetForeground));
|
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]);
|
2020-10-09 14:45:07 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Blend(sampledModes[i], offsetBackground,
|
|
|
|
offsetForeground, &cropRect));
|
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)
|
2019-08-02 19:21:23 +00:00
|
|
|
SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 60, 60);
|
|
|
|
sk_sp<SkImageFilter> cropped(SkImageFilters::Offset(0, 0, foreground, &cropRect));
|
2020-10-09 14:45:07 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Blend(SkBlendMode::kScreen, cropped, background));
|
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)
|
2020-10-09 14:45:07 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Blend(SkBlendMode::kScreen, background, cropped));
|
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.
|
2019-08-02 19:21:23 +00:00
|
|
|
SkIRect cropRectFull = SkIRect::MakeXYWH(0, 0, 80, 80);
|
2020-10-09 14:45:07 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Blend(SkBlendMode::kSrcIn, 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));
|
2021-01-24 13:57:23 +00:00
|
|
|
canvas->clipIRect(bitmap.bounds());
|
2021-01-25 00:49:21 +00:00
|
|
|
canvas->drawImage(bitmap.asImage(), 0, 0, SkSamplingOptions(), &paint);
|
2015-09-15 18:26:13 +00:00
|
|
|
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
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
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
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|