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"
|
2013-07-10 21:22:18 +00:00
|
|
|
#include "SkOffsetImageFilter.h"
|
2013-05-31 17:49:12 +00:00
|
|
|
#include "SkXfermodeImageFilter.h"
|
|
|
|
#include "SkBitmapSource.h"
|
|
|
|
|
|
|
|
#define WIDTH 600
|
|
|
|
#define HEIGHT 600
|
|
|
|
#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-01-09 18:06:39 +00:00
|
|
|
SkString onShortName() SK_OVERRIDE {
|
2013-05-31 17:49:12 +00:00
|
|
|
return SkString("xfermodeimagefilter");
|
|
|
|
}
|
|
|
|
|
|
|
|
void make_bitmap() {
|
2014-01-25 16:46:20 +00:00
|
|
|
fBitmap.allocN32Pixels(80, 80);
|
2014-02-13 17:14:46 +00:00
|
|
|
SkCanvas canvas(fBitmap);
|
2013-05-31 17:49:12 +00:00
|
|
|
canvas.clear(0x00000000);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2014-07-31 12:58:44 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2013-05-31 17:49:12 +00:00
|
|
|
paint.setColor(0xD000D000);
|
|
|
|
paint.setTextSize(SkIntToScalar(96));
|
|
|
|
const char* str = "e";
|
|
|
|
canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), paint);
|
|
|
|
}
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
SkISize onISize() SK_OVERRIDE {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(WIDTH, HEIGHT);
|
2013-05-31 17:49:12 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 18:31:42 +00:00
|
|
|
static void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint,
|
|
|
|
int x, int y) {
|
2013-05-31 17:49:12 +00:00
|
|
|
canvas->save();
|
2013-12-09 18:31:42 +00:00
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
canvas->clipRect(SkRect::MakeWH(
|
2013-05-31 17:49:12 +00:00
|
|
|
SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())));
|
2013-12-09 18:31:42 +00:00
|
|
|
canvas->drawBitmap(bitmap, 0, 0, &paint);
|
2013-05-31 17:49:12 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2013-12-09 18:31:42 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
void onOnceBeforeDraw() SK_OVERRIDE {
|
2013-12-09 18:31:42 +00:00
|
|
|
make_bitmap();
|
2015-01-26 19:24:32 +00:00
|
|
|
|
|
|
|
fCheckerboard.allocN32Pixels(80, 80);
|
|
|
|
SkCanvas checkerboardCanvas(fCheckerboard);
|
2015-01-26 20:49:00 +00:00
|
|
|
sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8);
|
2013-12-09 18:31:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
2013-05-31 17:49:12 +00:00
|
|
|
canvas->clear(0x00000000);
|
|
|
|
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;
|
2014-03-10 10:51:58 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> background(SkBitmapSource::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);
|
2013-12-09 18:31:42 +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);
|
2013-12-09 18:31:42 +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 NULL mode
|
2014-03-10 10:51:58 +00:00
|
|
|
filter.reset(SkXfermodeImageFilter::Create(NULL, background));
|
2013-05-31 17:49:12 +00:00
|
|
|
paint.setImageFilter(filter);
|
2013-12-09 18:31:42 +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)
|
2014-03-10 10:51:58 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> foreground(SkBitmapSource::Create(fBitmap));
|
|
|
|
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);
|
2013-12-09 18:31:42 +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);
|
2013-12-09 18:31:42 +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);
|
2013-12-09 18:31:42 +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;
|
|
|
|
}
|
|
|
|
}
|
2013-05-31 17:49:12 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
SkBitmap fBitmap, 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
|
|
|
|
|
|
|
}
|