2015-10-14 11:53:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 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"
|
|
|
|
#include "SkColorFilterImageFilter.h"
|
|
|
|
#include "SkModeColorFilter.h"
|
|
|
|
#include "SkMorphologyImageFilter.h"
|
|
|
|
#include "SkOffsetImageFilter.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_image(SkCanvas* rootCanvas) {
|
2015-10-14 11:53:31 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(rootCanvas->makeSurface(info));
|
2015-10-14 11:53:31 +00:00
|
|
|
if (!surface) {
|
2016-03-24 01:59:25 +00:00
|
|
|
surface = SkSurface::MakeRaster(info);
|
2015-10-14 11:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
surface->getCanvas()->drawCircle(50, 50, 50, paint);
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-10-14 11:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
// Show the effect of localmatriximagefilter with various matrices, on various filters
|
|
|
|
class LocalMatrixImageFilterGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
LocalMatrixImageFilterGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("localmatriximagefilter");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(640, 640);
|
|
|
|
}
|
|
|
|
|
2016-03-30 14:32:28 +00:00
|
|
|
static void show_image(SkCanvas* canvas, SkImage* image, sk_sp<SkImageFilter> filter) {
|
2015-10-14 11:53:31 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
SkRect r = SkRect::MakeIWH(image->width(), image->height()).makeOutset(SK_ScalarHalf,
|
|
|
|
SK_ScalarHalf);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setImageFilter(filter);
|
|
|
|
canvas->drawImage(image, 0, 0, &paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image0(make_image(canvas));
|
2015-10-14 11:53:31 +00:00
|
|
|
|
|
|
|
const ImageFilterFactory factories[] = {
|
|
|
|
IFCCast([]{ return SkBlurImageFilter::Create(8, 8); }),
|
|
|
|
IFCCast([]{ return SkDilateImageFilter::Create(8, 8); }),
|
|
|
|
IFCCast([]{ return SkErodeImageFilter::Create(8, 8); }),
|
|
|
|
IFCCast([]{ return SkOffsetImageFilter::Create(8, 8); }),
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkMatrix matrices[] = {
|
|
|
|
SkMatrix::MakeScale(SK_ScalarHalf, SK_ScalarHalf),
|
|
|
|
SkMatrix::MakeScale(2, 2),
|
|
|
|
SkMatrix::MakeTrans(10, 10)
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkScalar spacer = image0->width() * 3.0f / 2;
|
|
|
|
|
|
|
|
canvas->translate(40, 40);
|
|
|
|
for (auto&& factory : factories) {
|
2016-03-30 14:32:28 +00:00
|
|
|
sk_sp<SkImageFilter> filter(factory());
|
2015-10-14 11:53:31 +00:00
|
|
|
|
|
|
|
canvas->save();
|
2016-03-17 17:51:11 +00:00
|
|
|
show_image(canvas, image0.get(), filter);
|
2015-10-14 11:53:31 +00:00
|
|
|
for (const auto& matrix : matrices) {
|
2016-03-30 14:32:28 +00:00
|
|
|
sk_sp<SkImageFilter> localFilter(filter->makeWithLocalMatrix(matrix));
|
2015-10-14 11:53:31 +00:00
|
|
|
canvas->translate(spacer, 0);
|
2016-03-30 14:32:28 +00:00
|
|
|
show_image(canvas, image0.get(), std::move(localFilter));
|
2015-10-14 11:53:31 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, spacer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new LocalMatrixImageFilterGM; )
|