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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2012-03-23 15:36:36 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkColorPriv.h"
|
2017-11-03 12:06:09 +00:00
|
|
|
#include "SkImageFilterPriv.h"
|
2012-03-23 15:36:36 +00:00
|
|
|
#include "SkShader.h"
|
|
|
|
|
|
|
|
#include "SkBlurImageFilter.h"
|
2012-08-20 19:23:24 +00:00
|
|
|
#include "SkColorFilterImageFilter.h"
|
2013-08-07 20:00:55 +00:00
|
|
|
#include "SkDropShadowImageFilter.h"
|
2016-04-04 19:07:47 +00:00
|
|
|
#include "SkSpecialImage.h"
|
2012-03-23 15:36:36 +00:00
|
|
|
|
|
|
|
class FailImageFilter : public SkImageFilter {
|
|
|
|
public:
|
2014-08-21 17:53:34 +00:00
|
|
|
class Registrar {
|
|
|
|
public:
|
|
|
|
Registrar() {
|
|
|
|
SkFlattenable::Register("FailImageFilter",
|
2016-04-04 21:57:19 +00:00
|
|
|
FailImageFilter::CreateProc,
|
|
|
|
FailImageFilter::GetFlattenableType());
|
2014-08-21 17:53:34 +00:00
|
|
|
}
|
|
|
|
};
|
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
|
|
|
|
2014-12-19 21:49:15 +00:00
|
|
|
SK_TO_STRING_OVERRIDE()
|
2012-03-26 17:57:35 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(FailImageFilter)
|
2014-08-21 14:59:51 +00:00
|
|
|
|
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
|
|
|
|
2016-04-04 19:07:47 +00:00
|
|
|
sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
|
|
|
|
SkIPoint* offset) const override {
|
|
|
|
return nullptr;
|
2012-03-23 15:36:36 +00:00
|
|
|
}
|
2017-04-17 15:57:29 +00:00
|
|
|
sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-03-23 15:36:36 +00:00
|
|
|
private:
|
|
|
|
typedef SkImageFilter INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-08-21 17:53:34 +00:00
|
|
|
static FailImageFilter::Registrar gReg0;
|
|
|
|
|
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
|
|
|
|
2014-12-19 21:49:15 +00:00
|
|
|
#ifndef SK_IGNORE_TO_STRING
|
|
|
|
void FailImageFilter::toString(SkString* str) const {
|
|
|
|
str->appendf("FailImageFilter: (");
|
|
|
|
str->append(")");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-23 15:36:36 +00:00
|
|
|
class IdentityImageFilter : public SkImageFilter {
|
|
|
|
public:
|
2014-08-21 17:53:34 +00:00
|
|
|
class Registrar {
|
|
|
|
public:
|
|
|
|
Registrar() {
|
|
|
|
SkFlattenable::Register("IdentityImageFilter",
|
2016-04-04 21:57:19 +00:00
|
|
|
IdentityImageFilter::CreateProc,
|
|
|
|
IdentityImageFilter::GetFlattenableType());
|
2014-08-21 17:53:34 +00:00
|
|
|
}
|
|
|
|
};
|
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-12-19 21:49:15 +00:00
|
|
|
SK_TO_STRING_OVERRIDE()
|
2012-03-26 17:57:35 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(IdentityImageFilter)
|
2014-08-21 14:59:51 +00:00
|
|
|
|
2016-04-04 12:05:11 +00:00
|
|
|
protected:
|
2016-04-04 19:07:47 +00:00
|
|
|
sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
|
|
|
|
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);
|
2016-04-04 19:07:47 +00:00
|
|
|
return sk_ref_sp<SkSpecialImage>(source);
|
2012-03-23 15:36:36 +00:00
|
|
|
}
|
2017-04-17 15:57:29 +00:00
|
|
|
sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override {
|
|
|
|
return sk_ref_sp(const_cast<IdentityImageFilter*>(this));
|
|
|
|
}
|
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) {}
|
|
|
|
|
2012-03-23 15:36:36 +00:00
|
|
|
typedef SkImageFilter INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-08-21 17:53:34 +00:00
|
|
|
static IdentityImageFilter::Registrar gReg1;
|
|
|
|
|
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
|
|
|
|
2014-12-19 21:49:15 +00:00
|
|
|
#ifndef SK_IGNORE_TO_STRING
|
|
|
|
void IdentityImageFilter::toString(SkString* str) const {
|
|
|
|
str->appendf("IdentityImageFilter: (");
|
|
|
|
str->append(")");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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);
|
|
|
|
paint.setAntiAlias(true);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2012-03-23 15:36:36 +00:00
|
|
|
paint.setTextSize(r.height()/2);
|
|
|
|
paint.setTextAlign(SkPaint::kCenter_Align);
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString("Text", r.centerX(), r.centerY(), paint);
|
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
|
|
|
|
2016-10-28 19:42:34 +00:00
|
|
|
auto cf = SkColorFilter::MakeModeFilter(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(),
|
|
|
|
SkColorFilterImageFilter::Make(std::move(cf), nullptr),
|
|
|
|
SkBlurImageFilter::Make(12.0f, 0.0f, nullptr),
|
2016-04-06 15:40:59 +00:00
|
|
|
SkDropShadowImageFilter::Make(
|
2016-04-05 18:50:42 +00:00
|
|
|
10.0f, 5.0f, 3.0f, 3.0f, SK_ColorBLUE,
|
2016-04-06 15:40:59 +00:00
|
|
|
SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
|
|
|
|
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
|
|
|
|
|
|
|
void drawWaterfall(SkCanvas* canvas, const SkPaint& origPaint) {
|
|
|
|
const uint32_t flags[] = {
|
|
|
|
0,
|
|
|
|
SkPaint::kAntiAlias_Flag,
|
|
|
|
SkPaint::kAntiAlias_Flag | SkPaint::kLCDRenderText_Flag,
|
|
|
|
};
|
|
|
|
SkPaint paint(origPaint);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2015-03-20 13:32:52 +00:00
|
|
|
paint.setTextSize(30);
|
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(flags); ++i) {
|
|
|
|
paint.setFlags(flags[i]);
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString("Hamburgefon", 0, 0, 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 {
|
2016-04-04 11:31:25 +00:00
|
|
|
paint->setImageFilter(SkBlurImageFilter::Make(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 {
|
2016-10-28 19:42:34 +00:00
|
|
|
paint->setColorFilter(SkColorFilter::MakeModeFilter(SK_ColorBLUE, SkBlendMode::kSrcIn));
|
2015-03-20 13:32:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_GM( return new ImageFiltersText_CF; )
|