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-03-10 10:51:58 +00:00
|
|
|
static FailImageFilter* Create() {
|
|
|
|
return SkNEW(FailImageFilter);
|
|
|
|
}
|
2012-03-26 17:57:35 +00:00
|
|
|
|
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(FailImageFilter)
|
2012-03-23 15:36:36 +00:00
|
|
|
protected:
|
2014-03-10 10:51:58 +00:00
|
|
|
FailImageFilter() : INHERITED(0) {}
|
2014-03-14 15:44:01 +00:00
|
|
|
virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
|
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
|
|
|
SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE {
|
2012-03-23 15:36:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2014-01-30 18:58:24 +00:00
|
|
|
FailImageFilter(SkReadBuffer& buffer)
|
2013-11-25 21:46:31 +00:00
|
|
|
: INHERITED(1, buffer) {}
|
2012-03-23 15:36:36 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkImageFilter INHERITED;
|
|
|
|
};
|
|
|
|
|
2012-03-26 17:57:35 +00:00
|
|
|
// register the filter with the flattenable registry
|
|
|
|
static SkFlattenable::Registrar gFailImageFilterReg("FailImageFilter",
|
2013-10-23 17:06:21 +00:00
|
|
|
FailImageFilter::CreateProc,
|
|
|
|
FailImageFilter::GetFlattenableType());
|
2012-03-26 17:57:35 +00:00
|
|
|
|
2012-03-23 15:36:36 +00:00
|
|
|
class IdentityImageFilter : public SkImageFilter {
|
|
|
|
public:
|
2014-03-10 10:51:58 +00:00
|
|
|
static IdentityImageFilter* Create() {
|
|
|
|
return SkNEW(IdentityImageFilter);
|
|
|
|
}
|
2012-03-23 15:36:36 +00:00
|
|
|
|
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-03-10 10:51:58 +00:00
|
|
|
IdentityImageFilter() : INHERITED(0) {}
|
2014-03-14 15:44:01 +00:00
|
|
|
virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
|
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
|
|
|
SkBitmap* result, SkIPoint* offset) const SK_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;
|
|
|
|
}
|
|
|
|
|
2014-01-30 18:58:24 +00:00
|
|
|
IdentityImageFilter(SkReadBuffer& buffer)
|
2013-11-25 21:46:31 +00:00
|
|
|
: INHERITED(1, buffer) {}
|
2012-03-23 15:36:36 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkImageFilter INHERITED;
|
|
|
|
};
|
|
|
|
|
2012-03-26 17:57:35 +00:00
|
|
|
// register the filter with the flattenable registry
|
|
|
|
static SkFlattenable::Registrar gIdentityImageFilterReg("IdentityImageFilter",
|
2013-10-23 17:06:21 +00:00
|
|
|
IdentityImageFilter::CreateProc,
|
|
|
|
IdentityImageFilter::GetFlattenableType());
|
2012-03-26 17:57:35 +00:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
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:
|
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("imagefiltersbase");
|
|
|
|
}
|
|
|
|
|
2013-08-07 20:00:55 +00:00
|
|
|
virtual SkISize onISize() { 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);
|
|
|
|
}
|
|
|
|
|
2013-01-14 16:27:50 +00:00
|
|
|
virtual uint32_t onGetFlags() const {
|
|
|
|
// Because of the use of drawSprite, this test is excluded
|
|
|
|
// from scaled replay tests because drawSprite ignores the
|
|
|
|
// reciprocal scale that is applied at record time, which is
|
|
|
|
// the intended behavior of drawSprite.
|
2014-04-30 13:20:45 +00:00
|
|
|
return kSkipScaledReplay_Flag | kSkipTiled_Flag;
|
2013-01-14 16:27:50 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 15:36:36 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
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),
|
|
|
|
SkDropShadowImageFilter::Create(10.0f, 5.0f, 3.0f, SK_ColorBLUE),
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static skiagm::GM* MyFactory(void*) { return new ImageFiltersBaseGM; }
|
|
|
|
static skiagm::GMRegistry reg(MyFactory);
|