skia2/gm/tileimagefilter.cpp
senorblanco@chromium.org 6776b82d46 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

117 lines
3.9 KiB
C++

/*
* 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 "SkTileImageFilter.h"
#include "SkBitmapSource.h"
#define WIDTH 400
#define HEIGHT 100
#define MARGIN 12
namespace skiagm {
class TileImageFilterGM : public GM {
public:
TileImageFilterGM() : fInitialized(false) {
this->setBGColor(0xFF000000);
}
protected:
virtual SkString onShortName() {
return SkString("tileimagefilter");
}
void make_bitmap() {
fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
fBitmap.allocPixels();
SkBitmapDevice device(fBitmap);
SkCanvas canvas(&device);
canvas.clear(0xFF000000);
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(0xD000D000);
paint.setTextSize(SkIntToScalar(96));
const char* str = "e";
canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), paint);
}
void make_checkerboard() {
fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
fCheckerboard.allocPixels();
SkBitmapDevice device(fCheckerboard);
SkCanvas canvas(&device);
canvas.clear(0x00000000);
SkPaint darkPaint;
darkPaint.setColor(0xFF404040);
SkPaint lightPaint;
lightPaint.setColor(0xFFA0A0A0);
for (int y = 0; y < 80; y += 16) {
for (int x = 0; x < 80; x += 16) {
canvas.save();
canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
canvas.restore();
}
}
}
virtual SkISize onISize() {
return make_isize(WIDTH, HEIGHT);
}
virtual void onDraw(SkCanvas* canvas) {
if (!fInitialized) {
make_bitmap();
make_checkerboard();
fInitialized = true;
}
canvas->clear(0x00000000);
SkPaint paint;
int x = 0, y = 0;
for (size_t i = 0; i < 4; i++) {
SkBitmap* bitmap = (i & 0x01) ? &fCheckerboard : &fBitmap;
SkRect srcRect = SkRect::MakeXYWH(SkIntToScalar(bitmap->width()/4),
SkIntToScalar(bitmap->height()/4),
SkIntToScalar(bitmap->width()/(i+1)),
SkIntToScalar(bitmap->height()/(i+1)));
SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(i * 8),
SkIntToScalar(i * 4),
SkIntToScalar(bitmap->width() - i * 12),
SkIntToScalar(bitmap->height()) - i * 12);
SkAutoTUnref<SkImageFilter> tileInput(SkNEW_ARGS(SkBitmapSource, (*bitmap)));
SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS(
SkTileImageFilter, (srcRect, dstRect, tileInput)));
canvas->save();
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
paint.setImageFilter(filter);
canvas->drawBitmap(fBitmap, 0, 0, &paint);
canvas->restore();
x += bitmap->width() + MARGIN;
if (x + bitmap->width() > WIDTH) {
x = 0;
y += bitmap->height() + MARGIN;
}
}
}
private:
typedef GM INHERITED;
SkBitmap fBitmap, fCheckerboard;
bool fInitialized;
};
//////////////////////////////////////////////////////////////////////////////
static GM* MyFactory(void*) { return new TileImageFilterGM; }
static GMRegistry reg(MyFactory);
}