2013-01-16 18:54:15 +00:00
|
|
|
/*
|
|
|
|
* 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 "SkCanvas.h"
|
|
|
|
#include "SkBlurImageFilter.h"
|
2015-06-24 17:29:17 +00:00
|
|
|
#include "SkRSXform.h"
|
|
|
|
#include "SkSurface.h"
|
2013-01-16 18:54:15 +00:00
|
|
|
|
|
|
|
static void make_bm(SkBitmap* bm) {
|
2014-01-25 16:46:20 +00:00
|
|
|
bm->allocN32Pixels(100, 100);
|
2013-01-16 18:54:15 +00:00
|
|
|
bm->eraseColor(SK_ColorBLUE);
|
|
|
|
|
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas.drawCircle(50, 50, 50, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_2_bitmaps(SkCanvas* canvas, const SkBitmap& bm, bool doClip,
|
2015-08-27 14:41:13 +00:00
|
|
|
int dx, int dy, SkImageFilter* filter = nullptr) {
|
2013-01-16 18:54:15 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
SkPaint paint;
|
|
|
|
|
2013-01-17 07:06:06 +00:00
|
|
|
SkRect clipR = SkRect::MakeXYWH(SkIntToScalar(dx),
|
2013-01-16 19:57:02 +00:00
|
|
|
SkIntToScalar(dy),
|
2013-01-16 19:24:15 +00:00
|
|
|
SkIntToScalar(bm.width()),
|
|
|
|
SkIntToScalar(bm.height()));
|
2013-01-16 18:54:15 +00:00
|
|
|
|
|
|
|
paint.setImageFilter(filter);
|
|
|
|
clipR.inset(5, 5);
|
|
|
|
|
|
|
|
if (doClip) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(clipR);
|
|
|
|
}
|
|
|
|
canvas->drawSprite(bm, dx, dy, &paint);
|
|
|
|
if (doClip) {
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2013-01-16 19:24:15 +00:00
|
|
|
canvas->translate(SkIntToScalar(bm.width() + 20), 0);
|
2013-01-16 18:54:15 +00:00
|
|
|
|
|
|
|
if (doClip) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(clipR);
|
|
|
|
}
|
2013-01-16 19:24:15 +00:00
|
|
|
canvas->drawBitmap(bm, SkIntToScalar(dx), SkIntToScalar(dy), &paint);
|
2013-01-16 18:54:15 +00:00
|
|
|
if (doClip) {
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters)
|
|
|
|
*/
|
|
|
|
class SpriteBitmapGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
SpriteBitmapGM() {}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-01-16 18:54:15 +00:00
|
|
|
return SkString("spritebitmap");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2013-01-16 18:54:15 +00:00
|
|
|
return SkISize::Make(640, 480);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-01-16 18:54:15 +00:00
|
|
|
SkBitmap bm;
|
|
|
|
make_bm(&bm);
|
|
|
|
|
|
|
|
int dx = 10;
|
|
|
|
int dy = 10;
|
|
|
|
|
|
|
|
SkScalar sigma = 8;
|
2014-03-10 10:51:58 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> filter(SkBlurImageFilter::Create(sigma, sigma));
|
2013-01-16 18:54:15 +00:00
|
|
|
|
|
|
|
draw_2_bitmaps(canvas, bm, false, dx, dy);
|
|
|
|
dy += bm.height() + 20;
|
|
|
|
draw_2_bitmaps(canvas, bm, false, dx, dy, filter);
|
|
|
|
dy += bm.height() + 20;
|
|
|
|
draw_2_bitmaps(canvas, bm, true, dx, dy);
|
|
|
|
dy += bm.height() + 20;
|
|
|
|
draw_2_bitmaps(canvas, bm, true, dx, dy, filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new SpriteBitmapGM; )
|
2015-06-24 17:29:17 +00:00
|
|
|
|
2015-10-12 18:30:02 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "SkColorFilterImageFilter.h"
|
|
|
|
#include "SkModeColorFilter.h"
|
|
|
|
#include "SkMorphologyImageFilter.h"
|
|
|
|
#include "SkOffsetImageFilter.h"
|
|
|
|
|
|
|
|
static SkImage* make_image(SkCanvas* rootCanvas) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
|
|
|
|
SkAutoTUnref<SkSurface> surface(rootCanvas->newSurface(info));
|
|
|
|
if (!surface) {
|
|
|
|
surface.reset(SkSurface::NewRaster(info));
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
surface->getCanvas()->drawCircle(50, 50, 50, paint);
|
|
|
|
return surface->newImageSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_image(SkCanvas* canvas, SkImage* image, const SkIPoint& offset) {
|
|
|
|
SkScalar x = SkIntToScalar(offset.x());
|
|
|
|
SkScalar y = SkIntToScalar(offset.y());
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeIWH(image->width(), image->height());
|
|
|
|
r.offset(x, y);
|
|
|
|
// get on pixel-centers to make the hairline land on a numerical stable boundary
|
|
|
|
r.outset(SK_ScalarHalf, SK_ScalarHalf);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
canvas->drawImage(image, x, y, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef SkImageFilter* (*ImageFilterFactory)();
|
|
|
|
|
|
|
|
// +[]{...} did not work on windows (VS)
|
|
|
|
// (ImageFilterFactory)[]{...} did not work on linux (gcc)
|
|
|
|
// hence this cast function
|
|
|
|
template <typename T> ImageFilterFactory IFCCast(T arg) { return arg; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters)
|
|
|
|
*/
|
|
|
|
class ApplyFilterGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
ApplyFilterGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("apply-filter");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
2015-10-12 21:38:34 +00:00
|
|
|
return SkISize::Make(780, 780);
|
2015-10-12 18:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
SkAutoTUnref<SkImage> image0(make_image(canvas));
|
|
|
|
|
|
|
|
const ImageFilterFactory factories[] = {
|
|
|
|
IFCCast([]{ return SkBlurImageFilter::Create(8, 8); }),
|
|
|
|
IFCCast([]{ SkAutoTUnref<SkColorFilter> cf(SkModeColorFilter::Create(SK_ColorBLUE,
|
|
|
|
SkXfermode::kSrcIn_Mode));
|
|
|
|
return SkColorFilterImageFilter::Create(cf);
|
|
|
|
}),
|
|
|
|
IFCCast([]{ return SkDilateImageFilter::Create(8, 8); }),
|
|
|
|
IFCCast([]{ return SkErodeImageFilter::Create(8, 8); }),
|
|
|
|
IFCCast([]{ return SkOffsetImageFilter::Create(8, 8); }),
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkScalar spacer = image0->width() * 3.0f / 2;
|
|
|
|
|
|
|
|
for (auto&& factory : factories) {
|
|
|
|
SkAutoTUnref<SkImageFilter> filter(factory());
|
|
|
|
|
|
|
|
SkIPoint offset1, offset2;
|
|
|
|
SkAutoTUnref<SkImage> image1(image0->applyFilter(filter, &offset1, true));
|
|
|
|
SkAutoTUnref<SkImage> image2(image0->applyFilter(filter, &offset2, false));
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(30, 30);
|
|
|
|
show_image(canvas, image0, SkIPoint::Make(0, 0)); // original
|
|
|
|
canvas->translate(spacer, 0);
|
|
|
|
show_image(canvas, image1, offset1); // snug
|
|
|
|
canvas->translate(spacer, 0);
|
|
|
|
show_image(canvas, image2, offset2); // not snug
|
|
|
|
|
|
|
|
// Try drawing the original w/ the filter, to see that it "draws" the same as
|
|
|
|
// when we have manually applied the filter (above).
|
|
|
|
{
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(filter);
|
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
image0->asLegacyBitmap(&bm, SkImage::kRO_LegacyBitmapMode);
|
|
|
|
SkPoint loc = { 0, 0 };
|
|
|
|
canvas->translate(spacer, 0);
|
|
|
|
canvas->getTotalMatrix().mapPoints(&loc, 1);
|
|
|
|
canvas->drawSprite(bm, (int)loc.x(), (int)loc.y(), &paint); // like snug
|
|
|
|
|
|
|
|
canvas->translate(spacer, 0);
|
|
|
|
canvas->drawImage(image0, 0, 0, &paint); // like not snug
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(0, spacer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new ApplyFilterGM; )
|
|
|
|
|