2015-04-20 14:27:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 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-12-07 17:28:34 +00:00
|
|
|
#include "SkBlurImageFilter.h"
|
2015-04-20 14:27:15 +00:00
|
|
|
#include "SkColorMatrixFilter.h"
|
2015-12-07 17:28:34 +00:00
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkImageFilter.h"
|
|
|
|
#include "SkSurface.h"
|
2015-04-20 14:27:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test drawing a primitive w/ an imagefilter (in this case, just matrix w/ identity) to see
|
|
|
|
* that we apply the xfermode *after* the image has been created and filtered, and not during
|
|
|
|
* the creation step (i.e. before it is filtered).
|
|
|
|
*
|
2015-11-07 13:29:00 +00:00
|
|
|
* see https://bug.skia.org/3741
|
2015-04-20 14:27:15 +00:00
|
|
|
*/
|
2015-09-09 15:16:41 +00:00
|
|
|
static void do_draw(SkCanvas* canvas, SkXfermode::Mode mode, SkImageFilter* imf) {
|
2015-04-20 16:10:31 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->clipRect(SkRect::MakeWH(220, 220));
|
|
|
|
|
|
|
|
// want to force a layer, so modes like DstIn can combine meaningfully, but the final
|
|
|
|
// image can still be shown against our default (opaque) background. non-opaque GMs
|
|
|
|
// are a lot more trouble to compare/triage.
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(nullptr, nullptr);
|
2015-04-20 16:10:31 +00:00
|
|
|
canvas->drawColor(SK_ColorGREEN);
|
|
|
|
|
2015-04-20 14:27:15 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
SkRect r0 = SkRect::MakeXYWH(10, 60, 200, 100);
|
|
|
|
SkRect r1 = SkRect::MakeXYWH(60, 10, 100, 200);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawOval(r0, paint);
|
|
|
|
|
2015-04-20 16:10:31 +00:00
|
|
|
paint.setColor(0x660000FF);
|
2015-04-20 14:27:15 +00:00
|
|
|
paint.setImageFilter(imf);
|
|
|
|
paint.setXfermodeMode(mode);
|
|
|
|
canvas->drawOval(r1, paint);
|
2015-09-09 15:16:41 +00:00
|
|
|
}
|
2015-04-20 14:27:15 +00:00
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
DEF_SIMPLE_GM(imagefilters_xfermodes, canvas, 480, 480) {
|
2015-04-20 16:10:31 +00:00
|
|
|
canvas->translate(10, 10);
|
|
|
|
|
2015-04-20 14:27:15 +00:00
|
|
|
// just need an imagefilter to trigger the code-path (which creates a tmp layer)
|
|
|
|
SkAutoTUnref<SkImageFilter> imf(SkImageFilter::CreateMatrixFilter(SkMatrix::I(),
|
|
|
|
kNone_SkFilterQuality));
|
|
|
|
|
|
|
|
const SkXfermode::Mode modes[] = {
|
|
|
|
SkXfermode::kSrcATop_Mode, SkXfermode::kDstIn_Mode
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
|
|
|
|
canvas->save();
|
2015-09-09 15:16:41 +00:00
|
|
|
do_draw(canvas, modes[i], nullptr);
|
2015-04-20 16:10:31 +00:00
|
|
|
canvas->translate(240, 0);
|
2015-09-09 15:16:41 +00:00
|
|
|
do_draw(canvas, modes[i], imf);
|
2015-04-20 14:27:15 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
2015-04-20 16:10:31 +00:00
|
|
|
canvas->translate(0, 240);
|
2015-04-20 14:27:15 +00:00
|
|
|
}
|
2015-09-09 15:16:41 +00:00
|
|
|
}
|
2015-12-07 17:28:34 +00:00
|
|
|
|
|
|
|
static SkImage* make_image(SkCanvas* canvas) {
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
|
|
|
|
SkAutoTUnref<SkSurface> surface(canvas->newSurface(info));
|
|
|
|
if (!surface) {
|
|
|
|
surface.reset(SkSurface::NewRaster(info));
|
|
|
|
}
|
|
|
|
surface->getCanvas()->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), SkPaint());
|
|
|
|
return surface->newImageSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare blurs when we're tightly clipped (fast) and not as tightly (slower)
|
|
|
|
//
|
|
|
|
// Expect the two to draw the same (modulo the extra border of pixels when the clip is larger)
|
|
|
|
//
|
|
|
|
DEF_SIMPLE_GM(fast_slow_blurimagefilter, canvas, 620, 260) {
|
|
|
|
SkAutoTUnref<SkImage> image(make_image(canvas));
|
|
|
|
const SkRect r = SkRect::MakeIWH(image->width(), image->height());
|
|
|
|
|
|
|
|
canvas->translate(10, 10);
|
|
|
|
for (SkScalar sigma = 8; sigma <= 128; sigma *= 2) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(SkBlurImageFilter::Create(sigma, sigma))->unref();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
// we outset the clip by 1, to fall out of the fast-case in drawImage
|
|
|
|
// i.e. the clip is larger than the image
|
|
|
|
for (SkScalar outset = 0; outset <= 1; ++outset) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(r.makeOutset(outset, outset));
|
|
|
|
canvas->drawImage(image, 0, 0, &paint);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, r.height() + 20);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(r.width() + 20, 0);
|
|
|
|
}
|
|
|
|
}
|