2012-03-23 15:36:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 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"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColorFilter.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFlattenable.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkImageFilter.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.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/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/utils/SkTextUtils.h"
|
2019-08-02 13:57:04 +00:00
|
|
|
#include "src/core/SkImageFilter_Base.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkSpecialImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
class SkReadBuffer;
|
2012-03-23 15:36:36 +00:00
|
|
|
|
2019-08-02 13:57:04 +00:00
|
|
|
class FailImageFilter : public SkImageFilter_Base {
|
2012-03-23 15:36:36 +00:00
|
|
|
public:
|
2016-04-04 12:05:11 +00:00
|
|
|
static sk_sp<SkImageFilter> Make() {
|
|
|
|
return sk_sp<SkImageFilter>(new FailImageFilter);
|
|
|
|
}
|
2012-03-26 17:57:35 +00:00
|
|
|
|
2018-10-20 11:54:41 +00:00
|
|
|
SK_FLATTENABLE_HOOKS(FailImageFilter)
|
2012-03-23 15:36:36 +00:00
|
|
|
protected:
|
2016-04-04 12:05:11 +00:00
|
|
|
FailImageFilter() : INHERITED(nullptr, 0, nullptr) {}
|
2014-08-21 14:59:51 +00:00
|
|
|
|
2019-08-14 18:35:42 +00:00
|
|
|
sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override {
|
2016-04-04 19:07:47 +00:00
|
|
|
return nullptr;
|
2012-03-23 15:36:36 +00:00
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-03-23 15:36:36 +00:00
|
|
|
private:
|
2018-10-18 21:27:16 +00:00
|
|
|
|
2019-08-02 13:57:04 +00:00
|
|
|
typedef SkImageFilter_Base INHERITED;
|
2012-03-23 15:36:36 +00:00
|
|
|
};
|
|
|
|
|
2016-04-03 16:11:13 +00:00
|
|
|
sk_sp<SkFlattenable> FailImageFilter::CreateProc(SkReadBuffer& buffer) {
|
2014-08-21 14:59:51 +00:00
|
|
|
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
|
2016-04-04 12:05:11 +00:00
|
|
|
return FailImageFilter::Make();
|
2014-08-21 14:59:51 +00:00
|
|
|
}
|
2012-03-26 17:57:35 +00:00
|
|
|
|
2019-08-02 13:57:04 +00:00
|
|
|
class IdentityImageFilter : public SkImageFilter_Base {
|
2012-03-23 15:36:36 +00:00
|
|
|
public:
|
2016-04-04 12:05:11 +00:00
|
|
|
static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilter> input) {
|
|
|
|
return sk_sp<SkImageFilter>(new IdentityImageFilter(std::move(input)));
|
2014-03-10 10:51:58 +00:00
|
|
|
}
|
2012-03-23 15:36:36 +00:00
|
|
|
|
2014-08-21 14:59:51 +00:00
|
|
|
|
2018-10-20 11:54:41 +00:00
|
|
|
SK_FLATTENABLE_HOOKS(IdentityImageFilter)
|
2016-04-04 12:05:11 +00:00
|
|
|
protected:
|
2019-08-14 18:35:42 +00:00
|
|
|
sk_sp<SkSpecialImage> onFilterImage(const Context& ctx, SkIPoint* offset) const override {
|
Implement intra-frame cacheing in image filters.
When image filters are processed within Skia, they simply do
a blind recursion. This has the side-effect of turning the
DAG into a tree. I.e., nodes visited more than once during
the traversal will be processed more than once.
This change implements a very simple cacheing scheme: a
cache is created before traversing the DAG, and handed
into the processing traversal. Before recursing into a child
in SkImageFilter::filterImage(), the cache is checked for a
hit, and early-out is performed. Otherwise, the node is
processed, and its result bitmap and location (offset) are
cached, but only if it contains two or more children and
thus will be visited again during the traversal.
Currently, the child count is approximated with the
refcount. This is good enough in most cases (and exactly
correct for the Chrome use case). We could add an exact
child count to the image filter, but this will require
violating the immutability of image filters slightly in
order to bump the child count as nodes are connected. I
leave it up to the reviewer to decide which is better.
R=reed@google.com
Author: senorblanco@chromium.org
Review URL: https://codereview.chromium.org/230653005
git-svn-id: http://skia.googlecode.com/svn/trunk@14160 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-04-11 18:57:00 +00:00
|
|
|
offset->set(0, 0);
|
2019-08-14 18:35:42 +00:00
|
|
|
return sk_ref_sp<SkSpecialImage>(ctx.sourceImage());
|
2012-03-23 15:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-04-04 12:05:11 +00:00
|
|
|
IdentityImageFilter(sk_sp<SkImageFilter> input) : INHERITED(&input, 1, nullptr) {}
|
|
|
|
|
2019-08-02 13:57:04 +00:00
|
|
|
typedef SkImageFilter_Base INHERITED;
|
2012-03-23 15:36:36 +00:00
|
|
|
};
|
|
|
|
|
2018-10-20 11:54:41 +00:00
|
|
|
// Register these image filters as deserializable before main().
|
|
|
|
namespace {
|
|
|
|
static struct Initializer {
|
|
|
|
Initializer() {
|
2018-10-20 12:21:31 +00:00
|
|
|
SK_REGISTER_FLATTENABLE(IdentityImageFilter);
|
|
|
|
SK_REGISTER_FLATTENABLE(FailImageFilter);
|
2018-10-20 11:54:41 +00:00
|
|
|
}
|
|
|
|
} initializer;
|
|
|
|
}
|
2014-08-21 17:53:34 +00:00
|
|
|
|
2016-04-03 16:11:13 +00:00
|
|
|
sk_sp<SkFlattenable> IdentityImageFilter::CreateProc(SkReadBuffer& buffer) {
|
2014-08-21 14:59:51 +00:00
|
|
|
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
|
2016-04-04 12:05:11 +00:00
|
|
|
return IdentityImageFilter::Make(common.getInput(0));
|
2014-08-21 14:59:51 +00:00
|
|
|
}
|
2012-03-26 17:57:35 +00:00
|
|
|
|
2012-03-23 15:36:36 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-05 18:50:42 +00:00
|
|
|
static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2012-03-23 15:36:36 +00:00
|
|
|
SkPaint paint;
|
2016-04-05 18:50:42 +00:00
|
|
|
paint.setImageFilter(std::move(imf));
|
2012-03-23 15:36:36 +00:00
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(r);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2016-04-05 18:50:42 +00:00
|
|
|
static void draw_line(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2012-03-23 15:36:36 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
paint.setImageFilter(imf);
|
|
|
|
paint.setStrokeWidth(r.width()/10);
|
|
|
|
canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, paint);
|
|
|
|
}
|
|
|
|
|
2016-04-05 18:50:42 +00:00
|
|
|
static void draw_rect(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2012-03-23 15:36:36 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorYELLOW);
|
|
|
|
paint.setImageFilter(imf);
|
|
|
|
SkRect rr(r);
|
|
|
|
rr.inset(r.width()/10, r.height()/10);
|
|
|
|
canvas->drawRect(rr, paint);
|
|
|
|
}
|
|
|
|
|
2016-04-05 18:50:42 +00:00
|
|
|
static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2012-03-23 15:36:36 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorMAGENTA);
|
|
|
|
paint.setImageFilter(imf);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint);
|
|
|
|
}
|
|
|
|
|
2016-04-05 18:50:42 +00:00
|
|
|
static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2012-03-23 15:36:36 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(imf);
|
|
|
|
paint.setColor(SK_ColorCYAN);
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), r.height() / 2);
|
2018-12-24 04:19:14 +00:00
|
|
|
SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint,
|
2018-10-30 16:53:21 +00:00
|
|
|
SkTextUtils::kCenter_Align);
|
2012-03-23 15:36:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 18:50:42 +00:00
|
|
|
static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2012-03-23 15:36:36 +00:00
|
|
|
SkPaint paint;
|
2016-04-05 18:50:42 +00:00
|
|
|
paint.setImageFilter(std::move(imf));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-03-23 15:36:36 +00:00
|
|
|
SkIRect bounds;
|
|
|
|
r.roundOut(&bounds);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-03-23 15:36:36 +00:00
|
|
|
SkBitmap bm;
|
2014-01-25 16:46:20 +00:00
|
|
|
bm.allocN32Pixels(bounds.width(), bounds.height());
|
2012-12-06 21:47:40 +00:00
|
|
|
bm.eraseColor(SK_ColorTRANSPARENT);
|
2012-03-23 15:36:36 +00:00
|
|
|
SkCanvas c(bm);
|
2015-08-27 14:41:13 +00:00
|
|
|
draw_path(&c, r, nullptr);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-03-23 15:36:36 +00:00
|
|
|
canvas->drawBitmap(bm, 0, 0, &paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class ImageFiltersBaseGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
ImageFiltersBaseGM () {}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-03-23 15:36:36 +00:00
|
|
|
return SkString("imagefiltersbase");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(700, 500); }
|
2012-03-23 15:36:36 +00:00
|
|
|
|
|
|
|
void draw_frame(SkCanvas* canvas, const SkRect& r) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-04-05 18:50:42 +00:00
|
|
|
void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = {
|
2012-03-23 15:36:36 +00:00
|
|
|
draw_paint,
|
|
|
|
draw_line, draw_rect, draw_path, draw_text,
|
|
|
|
draw_bitmap,
|
|
|
|
};
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2019-04-08 20:23:20 +00:00
|
|
|
auto cf = SkColorFilters::Blend(SK_ColorRED, SkBlendMode::kSrcIn);
|
2016-04-05 18:50:42 +00:00
|
|
|
sk_sp<SkImageFilter> filters[] = {
|
2015-08-27 14:41:13 +00:00
|
|
|
nullptr,
|
2016-04-05 18:50:42 +00:00
|
|
|
IdentityImageFilter::Make(nullptr),
|
|
|
|
FailImageFilter::Make(),
|
2019-08-02 19:21:23 +00:00
|
|
|
SkImageFilters::ColorFilter(std::move(cf), nullptr),
|
|
|
|
// The strange 0.29 value tickles an edge case where crop rect calculates
|
|
|
|
// a small border, but the blur really needs no border. This tickles
|
2017-12-18 20:06:29 +00:00
|
|
|
// an msan uninitialized value bug.
|
2019-08-02 19:21:23 +00:00
|
|
|
SkImageFilters::Blur(12.0f, 0.29f, nullptr),
|
|
|
|
SkImageFilters::DropShadow(10.0f, 5.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr),
|
2012-03-23 15:36:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
|
|
|
|
SkScalar MARGIN = SkIntToScalar(16);
|
|
|
|
SkScalar DX = r.width() + MARGIN;
|
|
|
|
SkScalar DY = r.height() + MARGIN;
|
|
|
|
|
|
|
|
canvas->translate(MARGIN, MARGIN);
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(drawProc); ++i) {
|
|
|
|
canvas->save();
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) {
|
|
|
|
drawProc[i](canvas, r, filters[j]);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-03-23 15:36:36 +00:00
|
|
|
draw_frame(canvas, r);
|
|
|
|
canvas->translate(0, DY);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(DX, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
2015-03-20 13:32:52 +00:00
|
|
|
DEF_GM( return new ImageFiltersBaseGM; )
|
2012-03-23 15:36:36 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-03-20 13:32:52 +00:00
|
|
|
/*
|
|
|
|
* Want to test combos of filter and LCD text, to be sure we disable LCD in the presence of
|
|
|
|
* a filter.
|
|
|
|
*/
|
|
|
|
class ImageFiltersTextBaseGM : public skiagm::GM {
|
|
|
|
SkString fSuffix;
|
|
|
|
public:
|
|
|
|
ImageFiltersTextBaseGM(const char suffix[]) : fSuffix(suffix) {}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2015-03-20 13:32:52 +00:00
|
|
|
SkString name;
|
|
|
|
name.printf("%s_%s", "textfilter", fSuffix.c_str());
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(512, 342); }
|
2015-03-20 13:32:52 +00:00
|
|
|
|
2019-01-07 21:01:22 +00:00
|
|
|
void drawWaterfall(SkCanvas* canvas, const SkPaint& paint) {
|
|
|
|
static const SkFont::Edging kEdgings[3] = {
|
|
|
|
SkFont::Edging::kAlias,
|
|
|
|
SkFont::Edging::kAntiAlias,
|
|
|
|
SkFont::Edging::kSubpixelAntiAlias,
|
2015-03-20 13:32:52 +00:00
|
|
|
};
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 30);
|
2015-03-20 13:32:52 +00:00
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
2019-01-07 21:01:22 +00:00
|
|
|
for (SkFont::Edging edging : kEdgings) {
|
|
|
|
font.setEdging(edging);
|
|
|
|
canvas->drawString("Hamburgefon", 0, 0, font, paint);
|
2015-03-20 13:32:52 +00:00
|
|
|
canvas->translate(0, 40);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void installFilter(SkPaint* paint) = 0;
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2015-03-20 13:32:52 +00:00
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
canvas->translate(20, 40);
|
|
|
|
|
|
|
|
for (int doSaveLayer = 0; doSaveLayer <= 1; ++doSaveLayer) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
for (int useFilter = 0; useFilter <= 1; ++useFilter) {
|
|
|
|
SkAutoCanvasRestore acr2(canvas, true);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
if (useFilter) {
|
|
|
|
this->installFilter(&paint);
|
|
|
|
}
|
|
|
|
if (doSaveLayer) {
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(nullptr, &paint);
|
|
|
|
paint.setImageFilter(nullptr);
|
2015-03-20 13:32:52 +00:00
|
|
|
}
|
|
|
|
this->drawWaterfall(canvas, paint);
|
|
|
|
|
|
|
|
acr2.restore();
|
|
|
|
canvas->translate(250, 0);
|
|
|
|
}
|
|
|
|
acr.restore();
|
|
|
|
canvas->translate(0, 200);
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-03-20 13:32:52 +00:00
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ImageFiltersText_IF : public ImageFiltersTextBaseGM {
|
|
|
|
public:
|
|
|
|
ImageFiltersText_IF() : ImageFiltersTextBaseGM("image") {}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void installFilter(SkPaint* paint) override {
|
2019-08-02 19:21:23 +00:00
|
|
|
paint->setImageFilter(SkImageFilters::Blur(1.5f, 1.5f, nullptr));
|
2015-03-20 13:32:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_GM( return new ImageFiltersText_IF; )
|
|
|
|
|
|
|
|
class ImageFiltersText_CF : public ImageFiltersTextBaseGM {
|
|
|
|
public:
|
|
|
|
ImageFiltersText_CF() : ImageFiltersTextBaseGM("color") {}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void installFilter(SkPaint* paint) override {
|
2019-04-08 20:23:20 +00:00
|
|
|
paint->setColorFilter(SkColorFilters::Blend(SK_ColorBLUE, SkBlendMode::kSrcIn));
|
2015-03-20 13:32:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_GM( return new ImageFiltersText_CF; )
|