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"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#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"
|
2012-03-23 15:36:36 +00:00
|
|
|
#include "SkTestImageFilters.h"
|
|
|
|
|
|
|
|
class FailImageFilter : public SkImageFilter {
|
|
|
|
public:
|
2014-08-21 17:53:34 +00:00
|
|
|
class Registrar {
|
|
|
|
public:
|
|
|
|
Registrar() {
|
|
|
|
SkFlattenable::Register("FailImageFilter",
|
|
|
|
FailImageFilter::CreateProc,
|
|
|
|
FailImageFilter::GetFlattenableType());
|
|
|
|
}
|
|
|
|
};
|
2014-03-10 10:51:58 +00:00
|
|
|
static FailImageFilter* Create() {
|
|
|
|
return SkNEW(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:
|
2014-08-21 17:53:34 +00:00
|
|
|
FailImageFilter() : INHERITED(0, NULL) {}
|
2014-08-21 14:59:51 +00:00
|
|
|
|
2015-03-20 13:32:52 +00:00
|
|
|
bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
|
2015-03-26 01:17:31 +00:00
|
|
|
SkBitmap* result, SkIPoint* offset) const override {
|
2012-03-23 15:36:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
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;
|
|
|
|
|
2014-08-21 14:59:51 +00:00
|
|
|
SkFlattenable* FailImageFilter::CreateProc(SkReadBuffer& buffer) {
|
|
|
|
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
|
|
|
|
return FailImageFilter::Create();
|
|
|
|
}
|
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",
|
|
|
|
IdentityImageFilter::CreateProc,
|
|
|
|
IdentityImageFilter::GetFlattenableType());
|
|
|
|
}
|
|
|
|
};
|
2014-07-08 16:16:22 +00:00
|
|
|
static IdentityImageFilter* Create(SkImageFilter* input = NULL) {
|
|
|
|
return SkNEW_ARGS(IdentityImageFilter, (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)
|
2012-03-23 15:36:36 +00:00
|
|
|
protected:
|
2014-08-21 17:53:34 +00:00
|
|
|
IdentityImageFilter(SkImageFilter* input) : INHERITED(1, &input) {}
|
2014-08-21 14:59:51 +00:00
|
|
|
|
2015-03-20 13:32:52 +00:00
|
|
|
bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
|
2015-03-26 01:17:31 +00:00
|
|
|
SkBitmap* result, SkIPoint* offset) const override {
|
2012-03-23 15:36:36 +00:00
|
|
|
*result = src;
|
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);
|
2012-03-23 15:36:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkImageFilter INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-08-21 17:53:34 +00:00
|
|
|
static IdentityImageFilter::Registrar gReg1;
|
|
|
|
|
2014-08-21 14:59:51 +00:00
|
|
|
SkFlattenable* IdentityImageFilter::CreateProc(SkReadBuffer& buffer) {
|
|
|
|
SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
|
|
|
|
return IdentityImageFilter::Create(common.getInput(0));
|
|
|
|
}
|
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
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static void draw_paint(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(imf);
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(r);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_line(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_rect(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorYELLOW);
|
|
|
|
paint.setImageFilter(imf);
|
|
|
|
SkRect rr(r);
|
|
|
|
rr.inset(r.width()/10, r.height()/10);
|
|
|
|
canvas->drawRect(rr, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_path(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorMAGENTA);
|
|
|
|
paint.setImageFilter(imf);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_text(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(imf);
|
|
|
|
paint.setColor(SK_ColorCYAN);
|
|
|
|
paint.setAntiAlias(true);
|
2014-07-31 12:58:44 +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);
|
|
|
|
canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(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);
|
|
|
|
draw_path(&c, r, NULL);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-03-23 15:36:36 +00:00
|
|
|
canvas->drawBitmap(bm, 0, 0, &paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_sprite(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(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);
|
|
|
|
draw_path(&c, r, NULL);
|
|
|
|
|
|
|
|
SkPoint loc = { r.fLeft, r.fTop };
|
|
|
|
canvas->getTotalMatrix().mapPoints(&loc, 1);
|
|
|
|
canvas->drawSprite(bm,
|
|
|
|
SkScalarRoundToInt(loc.fX), SkScalarRoundToInt(loc.fY),
|
|
|
|
&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 {
|
2012-03-23 15:36:36 +00:00
|
|
|
void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = {
|
|
|
|
draw_paint,
|
|
|
|
draw_line, draw_rect, draw_path, draw_text,
|
|
|
|
draw_bitmap,
|
|
|
|
draw_sprite
|
|
|
|
};
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-03-23 15:36:36 +00:00
|
|
|
SkColorFilter* cf = SkColorFilter::CreateModeFilter(SK_ColorRED,
|
|
|
|
SkXfermode::kSrcIn_Mode);
|
|
|
|
SkImageFilter* filters[] = {
|
|
|
|
NULL,
|
2014-03-10 10:51:58 +00:00
|
|
|
IdentityImageFilter::Create(),
|
|
|
|
FailImageFilter::Create(),
|
2012-10-26 19:37:00 +00:00
|
|
|
SkColorFilterImageFilter::Create(cf),
|
2014-03-10 10:51:58 +00:00
|
|
|
SkBlurImageFilter::Create(12.0f, 0.0f),
|
2014-10-23 20:59:52 +00:00
|
|
|
SkDropShadowImageFilter::Create(10.0f, 5.0f, 3.0f, 3.0f, SK_ColorBLUE,
|
|
|
|
SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode),
|
2012-03-23 15:36:36 +00:00
|
|
|
};
|
|
|
|
cf->unref();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-06-27 14:00:06 +00:00
|
|
|
for(size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) {
|
|
|
|
SkSafeUnref(filters[j]);
|
|
|
|
}
|
2012-03-23 15:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
paint.setTextSize(30);
|
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(flags); ++i) {
|
|
|
|
paint.setFlags(flags[i]);
|
|
|
|
canvas->drawText("Hamburgefons", 11, 0, 0, paint);
|
|
|
|
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) {
|
|
|
|
canvas->saveLayer(NULL, &paint);
|
|
|
|
paint.setImageFilter(NULL);
|
|
|
|
}
|
|
|
|
this->drawWaterfall(canvas, paint);
|
|
|
|
|
|
|
|
acr2.restore();
|
|
|
|
canvas->translate(250, 0);
|
|
|
|
}
|
|
|
|
acr.restore();
|
|
|
|
canvas->translate(0, 200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-03-20 13:32:52 +00:00
|
|
|
paint->setImageFilter(SkBlurImageFilter::Create(1.5f, 1.5f))->unref();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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 {
|
2015-03-20 13:32:52 +00:00
|
|
|
paint->setColorFilter(SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mode))->unref();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_GM( return new ImageFiltersText_CF; )
|
|
|
|
|