2012-08-20 14:53:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 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"
|
|
|
|
|
2013-10-24 01:46:11 +00:00
|
|
|
#include "SkArithmeticMode.h"
|
2012-08-20 14:53:21 +00:00
|
|
|
#include "SkBitmapSource.h"
|
|
|
|
#include "SkBlurImageFilter.h"
|
2012-08-20 19:23:24 +00:00
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkColorFilterImageFilter.h"
|
2013-11-20 21:32:10 +00:00
|
|
|
#include "SkColorMatrixFilter.h"
|
2013-10-24 01:46:11 +00:00
|
|
|
#include "SkFlattenableBuffers.h"
|
2012-12-04 14:18:50 +00:00
|
|
|
#include "SkMergeImageFilter.h"
|
2012-08-20 14:53:21 +00:00
|
|
|
#include "SkMorphologyImageFilter.h"
|
2013-10-24 01:46:11 +00:00
|
|
|
#include "SkOnce.h"
|
2012-08-20 14:53:21 +00:00
|
|
|
#include "SkTestImageFilters.h"
|
2013-11-20 21:32:10 +00:00
|
|
|
#include "SkXfermodeImageFilter.h"
|
2012-08-20 14:53:21 +00:00
|
|
|
|
2013-10-24 01:46:11 +00:00
|
|
|
// More closely models how Blink's OffsetFilter works as of 10/23/13. SkOffsetImageFilter doesn't
|
|
|
|
// perform a draw and this one does.
|
|
|
|
class SimpleOffsetFilter : public SkImageFilter {
|
|
|
|
public:
|
|
|
|
SimpleOffsetFilter(SkScalar dx, SkScalar dy, SkImageFilter* input)
|
|
|
|
: SkImageFilter(input), fDX(dx), fDY(dy) {}
|
|
|
|
|
|
|
|
virtual bool onFilterImage(Proxy* proxy, const SkBitmap& src, const SkMatrix& ctm,
|
|
|
|
SkBitmap* dst, SkIPoint* offset) SK_OVERRIDE {
|
|
|
|
SkBitmap source = src;
|
|
|
|
SkImageFilter* input = getInput(0);
|
|
|
|
SkIPoint srcOffset = SkIPoint::Make(0, 0);
|
|
|
|
if (NULL != input && !input->filterImage(proxy, src, ctm, &source, &srcOffset)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkIRect bounds;
|
|
|
|
source.getBounds(&bounds);
|
|
|
|
|
|
|
|
if (!this->applyCropRect(&bounds, ctm)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height()));
|
|
|
|
SkCanvas canvas(device);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
|
|
|
|
canvas.drawBitmap(source, fDX - bounds.left(), fDY - bounds.top(), &paint);
|
|
|
|
*dst = device->accessBitmap(false);
|
|
|
|
offset->fX += bounds.left();
|
|
|
|
offset->fY += bounds.top();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SimpleOffsetFilter);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
explicit SimpleOffsetFilter(SkFlattenableReadBuffer& buffer)
|
2013-11-25 21:46:31 +00:00
|
|
|
: SkImageFilter(1, buffer) {
|
2013-10-24 01:46:11 +00:00
|
|
|
fDX = buffer.readScalar();
|
|
|
|
fDY = buffer.readScalar();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {
|
|
|
|
this->SkImageFilter::flatten(buffer);
|
|
|
|
buffer.writeScalar(fDX);
|
|
|
|
buffer.writeScalar(fDY);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkScalar fDX, fDY;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void init_flattenable(int*) {
|
|
|
|
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SimpleOffsetFilter)
|
|
|
|
}
|
|
|
|
|
2012-08-20 14:53:21 +00:00
|
|
|
class ImageFiltersGraphGM : public skiagm::GM {
|
|
|
|
public:
|
2013-10-24 01:46:11 +00:00
|
|
|
ImageFiltersGraphGM() : fInitialized(false) {
|
|
|
|
int dummy;
|
|
|
|
SK_DECLARE_STATIC_ONCE(once);
|
|
|
|
SkOnce(&once, init_flattenable, &dummy);
|
|
|
|
}
|
2012-08-20 14:53:21 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("imagefiltersgraph");
|
|
|
|
}
|
|
|
|
|
|
|
|
void make_bitmap() {
|
2014-01-25 16:46:20 +00:00
|
|
|
fBitmap.allocN32Pixels(100, 100);
|
2013-08-29 11:54:56 +00:00
|
|
|
SkBitmapDevice device(fBitmap);
|
2012-08-20 14:53:21 +00:00
|
|
|
SkCanvas canvas(&device);
|
|
|
|
canvas.clear(0x00000000);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(0xFFFFFFFF);
|
|
|
|
paint.setTextSize(SkIntToScalar(96));
|
|
|
|
const char* str = "e";
|
|
|
|
canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
|
|
|
|
}
|
|
|
|
|
2013-10-24 15:59:31 +00:00
|
|
|
void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(SkRect::MakeXYWH(0, 0,
|
|
|
|
SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())));
|
|
|
|
canvas->drawBitmap(bitmap, 0, 0, &paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2013-10-24 01:46:11 +00:00
|
|
|
virtual SkISize onISize() { return SkISize::Make(500, 150); }
|
2012-08-20 14:53:21 +00:00
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
if (!fInitialized) {
|
|
|
|
this->make_bitmap();
|
|
|
|
fInitialized = true;
|
|
|
|
}
|
|
|
|
canvas->clear(0x00000000);
|
2012-10-24 15:14:26 +00:00
|
|
|
{
|
|
|
|
SkAutoTUnref<SkImageFilter> bitmapSource(new SkBitmapSource(fBitmap));
|
|
|
|
SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED,
|
|
|
|
SkXfermode::kSrcIn_Mode));
|
|
|
|
SkAutoTUnref<SkImageFilter> blur(new SkBlurImageFilter(4.0f, 4.0f, bitmapSource));
|
|
|
|
SkAutoTUnref<SkImageFilter> erode(new SkErodeImageFilter(4, 4, blur));
|
2012-10-26 19:37:00 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> color(SkColorFilterImageFilter::Create(cf, erode));
|
2012-10-24 15:14:26 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> merge(new SkMergeImageFilter(blur, color));
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(merge);
|
|
|
|
canvas->drawPaint(paint);
|
2013-10-24 15:59:31 +00:00
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
2012-10-24 15:14:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SkAutoTUnref<SkImageFilter> morph(new SkDilateImageFilter(5, 5));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-10-24 15:14:26 +00:00
|
|
|
SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0,
|
|
|
|
0, SK_Scalar1, 0, 0, 0,
|
|
|
|
0, 0, SK_Scalar1, 0, 0,
|
2013-11-25 19:44:07 +00:00
|
|
|
0, 0, 0, 0.5f, 0 };
|
2012-08-20 14:53:21 +00:00
|
|
|
|
2012-10-24 15:14:26 +00:00
|
|
|
SkAutoTUnref<SkColorFilter> matrixFilter(new SkColorMatrixFilter(matrix));
|
2012-10-26 19:37:00 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> colorMorph(SkColorFilterImageFilter::Create(matrixFilter, morph));
|
2013-08-01 14:59:05 +00:00
|
|
|
SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
|
|
|
|
SkAutoTUnref<SkImageFilter> blendColor(new SkXfermodeImageFilter(mode, colorMorph));
|
2012-08-20 14:53:21 +00:00
|
|
|
|
2012-10-24 15:14:26 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(blendColor);
|
2013-10-24 15:59:31 +00:00
|
|
|
drawClippedBitmap(canvas, fBitmap, paint);
|
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
2012-10-24 15:14:26 +00:00
|
|
|
}
|
2013-10-24 01:46:11 +00:00
|
|
|
{
|
|
|
|
SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0,
|
|
|
|
0, SK_Scalar1, 0, 0, 0,
|
|
|
|
0, 0, SK_Scalar1, 0, 0,
|
2013-11-25 19:44:07 +00:00
|
|
|
0, 0, 0, 0.5f, 0 };
|
2013-10-24 01:46:11 +00:00
|
|
|
SkColorMatrixFilter matrixCF(matrix);
|
|
|
|
SkAutoTUnref<SkImageFilter> matrixFilter(SkColorFilterImageFilter::Create(&matrixCF));
|
|
|
|
SimpleOffsetFilter offsetFilter(SkIntToScalar(10), SkIntToScalar(10), matrixFilter);
|
|
|
|
|
|
|
|
SkAutoTUnref<SkXfermode> arith(SkArithmeticMode::Create(0, SK_Scalar1, SK_Scalar1, 0));
|
|
|
|
SkXfermodeImageFilter arithFilter(arith, matrixFilter, &offsetFilter);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(&arithFilter);
|
2013-10-24 15:59:31 +00:00
|
|
|
drawClippedBitmap(canvas, fBitmap, paint);
|
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
SkAutoTUnref<SkImageFilter> blur(new SkBlurImageFilter(
|
|
|
|
SkIntToScalar(10), SkIntToScalar(10)));
|
|
|
|
|
|
|
|
SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcIn_Mode));
|
|
|
|
SkImageFilter::CropRect cropRect(SkRect::MakeWH(SkIntToScalar(95), SkIntToScalar(100)));
|
|
|
|
SkAutoTUnref<SkImageFilter> blend(new SkXfermodeImageFilter(mode, blur, NULL, &cropRect));
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(blend);
|
|
|
|
drawClippedBitmap(canvas, fBitmap, paint);
|
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
2013-10-24 01:46:11 +00:00
|
|
|
}
|
Make SkImageFilter crop rects relative to the primitive origin, instead of relative to their parent's crop rect. This is required by SVG semantics, and is more sane anyway.
To do this, this patch changes the "offset/loc" parameter in filterImage() / onFilterImage() from an inout-param to an out-param only, so that the calling filter can know how much the input filter wants its result offset (and doesn't include the original primitive position). This offset can then be applied to the current filter's crop rect. (I've renamed the parameter "offset" in all cases to make this clear.) This makes the call sites in SkCanvas/SkGpuDevice responsible for applying the resulting offset to the primitive's position, which is actually a fairly small change.
This change also fixes SkTileImageFilter and SkOffsetImageFilter to correctly handle an input offset, which they weren't before. This required modifying the GM's, since they assumed the broken behaviour.
NOTE: this will require rebaselining the imagefiltersgraph test, since it has a new test case.
NOTE: this will "break" the Blink layout tests css3/filters/effect-reference-subregion-chained-hw.html and css3/filters/effect-reference-subregion-hw.html, but it actually makes them give correct results. It should be suppressed on the skia roll, and I'll rebaseline it.
R=reed@google.com
Review URL: https://codereview.chromium.org/112803004
git-svn-id: http://skia.googlecode.com/svn/trunk@12895 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-03 21:48:22 +00:00
|
|
|
{
|
|
|
|
// Test that crop offsets are absolute, not relative to the parent's crop rect.
|
|
|
|
SkAutoTUnref<SkColorFilter> cf1(SkColorFilter::CreateModeFilter(SK_ColorBLUE,
|
|
|
|
SkXfermode::kSrcIn_Mode));
|
|
|
|
SkAutoTUnref<SkColorFilter> cf2(SkColorFilter::CreateModeFilter(SK_ColorGREEN,
|
|
|
|
SkXfermode::kSrcIn_Mode));
|
|
|
|
SkImageFilter::CropRect outerRect(SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(10),
|
|
|
|
SkIntToScalar(80), SkIntToScalar(80)));
|
|
|
|
SkImageFilter::CropRect innerRect(SkRect::MakeXYWH(SkIntToScalar(20), SkIntToScalar(20),
|
|
|
|
SkIntToScalar(60), SkIntToScalar(60)));
|
|
|
|
SkAutoTUnref<SkImageFilter> color1(SkColorFilterImageFilter::Create(cf1, NULL, &outerRect));
|
|
|
|
SkAutoTUnref<SkImageFilter> color2(SkColorFilterImageFilter::Create(cf2, color1, &innerRect));
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(color2);
|
|
|
|
paint.setColor(0xFFFF0000);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), paint);
|
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
|
|
|
}
|
2012-08-20 14:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
bool fInitialized;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static skiagm::GM* MyFactory(void*) { return new ImageFiltersGraphGM; }
|
|
|
|
static skiagm::GMRegistry reg(MyFactory);
|