Enhance SkDropShadowImageFilter to support separate X & Y sigmas, and crop rect. This required a new flavour of the constructor.

This is driven by the feDropShadow filter effect in SVG, which unlike the shorthand version accepts separate X & Y sigmas.

Covered by new GM dropshadowimagefilter.

R=reed@google.com

Review URL: https://codereview.chromium.org/88243004

git-svn-id: http://skia.googlecode.com/svn/trunk@12401 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
senorblanco@chromium.org 2013-11-26 21:52:38 +00:00
parent 7435f34b47
commit d8f82a4c44
4 changed files with 207 additions and 8 deletions

View File

@ -0,0 +1,174 @@
/*
* Copyright 2013 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 "SkColorFilter.h"
#include "SkColorFilterImageFilter.h"
#include "SkDropShadowImageFilter.h"
///////////////////////////////////////////////////////////////////////////////
static void draw_paint(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
SkPaint paint;
paint.setImageFilter(imf);
paint.setColor(SK_ColorBLACK);
canvas->save();
canvas->clipRect(r);
canvas->drawPaint(paint);
canvas->restore();
}
static void draw_path(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
SkPaint paint;
paint.setColor(SK_ColorGREEN);
paint.setImageFilter(imf);
paint.setAntiAlias(true);
canvas->save();
canvas->clipRect(r);
canvas->drawCircle(r.centerX(), r.centerY(), r.width()/3, paint);
canvas->restore();
}
static void draw_text(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
SkPaint paint;
paint.setImageFilter(imf);
paint.setColor(SK_ColorGREEN);
paint.setAntiAlias(true);
paint.setTextSize(r.height()/2);
paint.setTextAlign(SkPaint::kCenter_Align);
canvas->save();
canvas->clipRect(r);
canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint);
canvas->restore();
}
static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
SkPaint paint;
SkIRect bounds;
r.roundOut(&bounds);
SkBitmap bm;
bm.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height());
bm.allocPixels();
bm.eraseColor(SK_ColorTRANSPARENT);
SkCanvas c(bm);
draw_path(&c, r, NULL);
paint.setImageFilter(imf);
canvas->save();
canvas->clipRect(r);
canvas->drawBitmap(bm, 0, 0, &paint);
canvas->restore();
}
static void draw_sprite(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
SkPaint paint;
SkIRect bounds;
r.roundOut(&bounds);
SkBitmap bm;
bm.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height());
bm.allocPixels();
bm.eraseColor(SK_ColorRED);
SkCanvas c(bm);
SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
paint.setColor(SK_ColorGREEN);
c.drawRect(SkRect::Make(cropRect), paint);
paint.setImageFilter(imf);
SkPoint loc = { r.fLeft, r.fTop };
canvas->getTotalMatrix().mapPoints(&loc, 1);
canvas->drawSprite(bm,
SkScalarRoundToInt(loc.fX), SkScalarRoundToInt(loc.fY),
&paint);
}
///////////////////////////////////////////////////////////////////////////////
class DropShadowImageFilterGM : public skiagm::GM {
public:
DropShadowImageFilterGM () {}
protected:
virtual SkString onShortName() {
return SkString("dropshadowimagefilter");
}
virtual SkISize onISize() { return SkISize::Make(400, 700); }
void draw_frame(SkCanvas* canvas, const SkRect& r) {
SkPaint paint;
paint.setStyle(SkPaint::kStroke_Style);
paint.setColor(SK_ColorRED);
canvas->drawRect(r, paint);
}
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.
return kSkipScaledReplay_Flag;
}
virtual void onDraw(SkCanvas* canvas) {
void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = {
draw_sprite, draw_bitmap, draw_path, draw_paint, draw_text
};
SkAutoTUnref<SkColorFilter> cf(
SkColorFilter::CreateModeFilter(SK_ColorMAGENTA, SkXfermode::kSrcIn_Mode));
SkAutoTUnref<SkImageFilter> cfif(SkColorFilterImageFilter::Create(cf.get()));
SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)),
SkImageFilter::CropRect::kHasAll_CropEdge);
SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)),
SkImageFilter::CropRect::kHasAll_CropEdge);
SkImageFilter* filters[] = {
NULL,
new SkDropShadowImageFilter(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE),
new SkDropShadowImageFilter(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE),
new SkDropShadowImageFilter(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE),
new SkDropShadowImageFilter(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, cfif),
new SkDropShadowImageFilter(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, NULL, &cropRect),
new SkDropShadowImageFilter(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, NULL, &bogusRect),
};
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 j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) {
canvas->save();
for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
drawProc[j](canvas, r, filters[i]);
canvas->translate(0, DY);
}
canvas->restore();
canvas->translate(DX, 0);
}
for(size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) {
SkSafeUnref(filters[j]);
}
}
private:
typedef GM INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
static skiagm::GM* MyFactory(void*) { return new DropShadowImageFilterGM; }
static skiagm::GMRegistry reg(MyFactory);

View File

@ -50,6 +50,7 @@
'../gm/downsamplebitmap.cpp',
'../gm/drawbitmaprect.cpp',
'../gm/drawlooper.cpp',
'../gm/dropshadowimagefilter.cpp',
'../gm/extractbitmap.cpp',
'../gm/emptypath.cpp',
'../gm/fatpathfill.cpp',

View File

@ -12,6 +12,7 @@
class SK_API SkDropShadowImageFilter : public SkImageFilter {
public:
SkDropShadowImageFilter(SkScalar dx, SkScalar dy, SkScalar sigma, SkColor, SkImageFilter* input = NULL);
SkDropShadowImageFilter(SkScalar dx, SkScalar dy, SkScalar sigmaX, SkScalar sigmaY, SkColor, SkImageFilter* input = NULL, const CropRect* cropRect = NULL);
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDropShadowImageFilter)
protected:
@ -20,7 +21,7 @@ protected:
virtual bool onFilterImage(Proxy*, const SkBitmap& source, const SkMatrix&, SkBitmap* result, SkIPoint* loc) SK_OVERRIDE;
private:
SkScalar fDx, fDy, fSigma;
SkScalar fDx, fDy, fSigmaX, fSigmaY;
SkColor fColor;
typedef SkImageFilter INHERITED;
};

View File

@ -15,10 +15,21 @@
#include "SkFlattenableBuffers.h"
SkDropShadowImageFilter::SkDropShadowImageFilter(SkScalar dx, SkScalar dy, SkScalar sigma, SkColor color, SkImageFilter* input)
: SkImageFilter(input)
: INHERITED(input)
, fDx(dx)
, fDy(dy)
, fSigma(sigma)
, fSigmaX(sigma)
, fSigmaY(sigma)
, fColor(color)
{
}
SkDropShadowImageFilter::SkDropShadowImageFilter(SkScalar dx, SkScalar dy, SkScalar sigmaX, SkScalar sigmaY, SkColor color, SkImageFilter* input, const CropRect* cropRect)
: INHERITED(input, cropRect)
, fDx(dx)
, fDy(dy)
, fSigmaX(sigmaX)
, fSigmaY(sigmaY)
, fColor(color)
{
}
@ -27,11 +38,13 @@ SkDropShadowImageFilter::SkDropShadowImageFilter(SkFlattenableReadBuffer& buffer
: INHERITED(1, buffer) {
fDx = buffer.readScalar();
fDy = buffer.readScalar();
fSigma = buffer.readScalar();
fSigmaX = buffer.readScalar();
fSigmaY = buffer.readScalar();
fColor = buffer.readColor();
buffer.validate(SkScalarIsFinite(fDx) &&
SkScalarIsFinite(fDy) &&
SkScalarIsFinite(fSigma));
SkScalarIsFinite(fSigmaX) &&
SkScalarIsFinite(fSigmaY));
}
void SkDropShadowImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const
@ -39,7 +52,8 @@ void SkDropShadowImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const
this->INHERITED::flatten(buffer);
buffer.writeScalar(fDx);
buffer.writeScalar(fDy);
buffer.writeScalar(fSigma);
buffer.writeScalar(fSigmaX);
buffer.writeScalar(fSigmaY);
buffer.writeColor(fColor);
}
@ -49,17 +63,26 @@ bool SkDropShadowImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& source
if (getInput(0) && !getInput(0)->filterImage(proxy, source, matrix, &src, loc))
return false;
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(src.width(), src.height()));
SkIRect bounds;
src.getBounds(&bounds);
if (!this->applyCropRect(&bounds, matrix)) {
return false;
}
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height()));
SkCanvas canvas(device.get());
SkAutoTUnref<SkImageFilter> blurFilter(new SkBlurImageFilter(fSigma, fSigma));
SkAutoTUnref<SkImageFilter> blurFilter(new SkBlurImageFilter(fSigmaX, fSigmaY));
SkAutoTUnref<SkColorFilter> colorFilter(SkColorFilter::CreateModeFilter(fColor, SkXfermode::kSrcIn_Mode));
SkPaint paint;
paint.setImageFilter(blurFilter.get());
paint.setColorFilter(colorFilter.get());
paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
canvas.translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop));
canvas.drawBitmap(src, fDx, fDy, &paint);
canvas.drawBitmap(src, 0, 0);
*result = device->accessBitmap(false);
loc->fX += bounds.fLeft;
loc->fY += bounds.fTop;
return true;
}