2013-07-24 22:19:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkShader.h"
|
2018-10-25 20:12:39 +00:00
|
|
|
#include "SkTextUtils.h"
|
2019-03-20 16:12:10 +00:00
|
|
|
#include "ToolUtils.h"
|
|
|
|
#include "gm.h"
|
2013-07-24 22:19:24 +00:00
|
|
|
|
|
|
|
#include "SkBlurImageFilter.h"
|
2015-01-26 20:20:14 +00:00
|
|
|
#include "SkMorphologyImageFilter.h"
|
2013-07-24 22:19:24 +00:00
|
|
|
#include "SkColorFilterImageFilter.h"
|
2013-09-19 17:26:47 +00:00
|
|
|
#include "SkMergeImageFilter.h"
|
|
|
|
#include "SkOffsetImageFilter.h"
|
2013-07-24 22:19:24 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-05 16:09:36 +00:00
|
|
|
static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2013-07-24 22:19:24 +00:00
|
|
|
SkPaint paint;
|
2016-04-05 16:09:36 +00:00
|
|
|
paint.setImageFilter(std::move(imf));
|
2013-07-24 22:19:24 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(r);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2016-04-05 16:09:36 +00:00
|
|
|
static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2013-07-24 22:19:24 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorMAGENTA);
|
2016-04-05 16:09:36 +00:00
|
|
|
paint.setImageFilter(std::move(imf));
|
2013-07-24 22:19:24 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint);
|
|
|
|
}
|
|
|
|
|
2016-04-05 16:09:36 +00:00
|
|
|
static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2013-07-24 22:19:24 +00:00
|
|
|
SkPaint paint;
|
2016-04-05 16:09:36 +00:00
|
|
|
paint.setImageFilter(std::move(imf));
|
2013-07-24 22:19:24 +00:00
|
|
|
paint.setColor(SK_ColorGREEN);
|
2019-01-03 20:45:53 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), r.height() / 2);
|
2019-01-03 20:45:53 +00:00
|
|
|
SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint, SkTextUtils::kCenter_Align);
|
2013-07-24 22:19:24 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 16:09:36 +00:00
|
|
|
static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2013-07-24 22:19:24 +00:00
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
SkIRect bounds;
|
|
|
|
r.roundOut(&bounds);
|
|
|
|
|
|
|
|
SkBitmap bm;
|
2014-01-25 16:46:20 +00:00
|
|
|
bm.allocN32Pixels(bounds.width(), bounds.height());
|
2013-07-24 22:19:24 +00:00
|
|
|
bm.eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
SkCanvas c(bm);
|
2015-08-27 14:41:13 +00:00
|
|
|
draw_path(&c, r, nullptr);
|
2013-07-24 22:19:24 +00:00
|
|
|
|
2016-04-05 16:09:36 +00:00
|
|
|
paint.setImageFilter(std::move(imf));
|
2013-07-24 22:19:24 +00:00
|
|
|
canvas->drawBitmap(bm, 0, 0, &paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class ImageFiltersCroppedGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
ImageFiltersCroppedGM () {}
|
|
|
|
|
|
|
|
protected:
|
2016-02-01 18:32:42 +00:00
|
|
|
SkString onShortName() override {
|
2013-07-24 22:19:24 +00:00
|
|
|
return SkString("imagefilterscropped");
|
|
|
|
}
|
|
|
|
|
2016-02-01 18:32:42 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(400, 960); }
|
2015-01-26 20:20:14 +00:00
|
|
|
|
|
|
|
void make_checkerboard() {
|
|
|
|
fCheckerboard.allocN32Pixels(80, 80);
|
|
|
|
SkCanvas canvas(fCheckerboard);
|
|
|
|
canvas.clear(SK_ColorTRANSPARENT);
|
|
|
|
SkPaint darkPaint;
|
2018-08-16 14:17:03 +00:00
|
|
|
darkPaint.setColor(0xFF404040);
|
2015-01-26 20:20:14 +00:00
|
|
|
SkPaint lightPaint;
|
2018-08-16 14:17:03 +00:00
|
|
|
lightPaint.setColor(0xFFA0A0A0);
|
2015-01-26 20:20:14 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 22:19:24 +00:00
|
|
|
|
|
|
|
void draw_frame(SkCanvas* canvas, const SkRect& r) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
|
|
|
|
2016-04-05 18:50:42 +00:00
|
|
|
void onOnceBeforeDraw() override{
|
2015-01-26 20:20:14 +00:00
|
|
|
make_checkerboard();
|
|
|
|
}
|
|
|
|
|
2016-04-05 18:50:42 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-04-05 16:09:36 +00:00
|
|
|
void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = {
|
2015-12-16 16:38:15 +00:00
|
|
|
draw_bitmap, draw_path, draw_paint, draw_text
|
2013-07-24 22:19:24 +00:00
|
|
|
};
|
|
|
|
|
2019-04-08 20:23:20 +00:00
|
|
|
sk_sp<SkColorFilter> cf(SkColorFilters::Blend(SK_ColorBLUE,
|
2016-10-28 19:42:34 +00:00
|
|
|
SkBlendMode::kSrcIn));
|
2016-04-05 16:09:36 +00:00
|
|
|
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);
|
2013-07-24 22:19:24 +00:00
|
|
|
|
2016-03-31 16:05:49 +00:00
|
|
|
sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(SkIntToScalar(-10),
|
|
|
|
SkIntToScalar(-10),
|
|
|
|
nullptr));
|
2013-09-19 17:26:47 +00:00
|
|
|
|
2016-04-05 18:50:42 +00:00
|
|
|
sk_sp<SkImageFilter> cfOffset(SkColorFilterImageFilter::Make(cf, std::move(offset)));
|
2015-01-24 00:09:32 +00:00
|
|
|
|
2016-04-05 16:09:36 +00:00
|
|
|
sk_sp<SkImageFilter> erodeX(SkErodeImageFilter::Make(8, 0, nullptr, &cropRect));
|
|
|
|
sk_sp<SkImageFilter> erodeY(SkErodeImageFilter::Make(0, 8, nullptr, &cropRect));
|
2015-01-26 20:20:14 +00:00
|
|
|
|
2016-04-05 16:09:36 +00:00
|
|
|
sk_sp<SkImageFilter> filters[] = {
|
2015-08-27 14:41:13 +00:00
|
|
|
nullptr,
|
2016-04-05 18:50:42 +00:00
|
|
|
SkColorFilterImageFilter::Make(cf, nullptr, &cropRect),
|
2016-04-05 16:09:36 +00:00
|
|
|
SkBlurImageFilter::Make(0.0f, 0.0f, nullptr, &cropRect),
|
|
|
|
SkBlurImageFilter::Make(1.0f, 1.0f, nullptr, &cropRect),
|
|
|
|
SkBlurImageFilter::Make(8.0f, 0.0f, nullptr, &cropRect),
|
|
|
|
SkBlurImageFilter::Make(0.0f, 8.0f, nullptr, &cropRect),
|
|
|
|
SkBlurImageFilter::Make(8.0f, 8.0f, nullptr, &cropRect),
|
|
|
|
SkErodeImageFilter::Make(1, 1, nullptr, &cropRect),
|
|
|
|
SkErodeImageFilter::Make(8, 0, std::move(erodeY), &cropRect),
|
|
|
|
SkErodeImageFilter::Make(0, 8, std::move(erodeX), &cropRect),
|
|
|
|
SkErodeImageFilter::Make(8, 8, nullptr, &cropRect),
|
|
|
|
SkMergeImageFilter::Make(nullptr,
|
|
|
|
std::move(cfOffset),
|
|
|
|
&cropRect),
|
|
|
|
SkBlurImageFilter::Make(8.0f, 8.0f, nullptr, &bogusRect),
|
2016-04-05 18:50:42 +00:00
|
|
|
SkColorFilterImageFilter::Make(cf, nullptr, &bogusRect),
|
2013-07-24 22:19:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2013-07-25 18:08:13 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) {
|
2013-07-24 22:19:24 +00:00
|
|
|
canvas->save();
|
2013-07-25 18:08:13 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
|
2015-01-26 20:20:14 +00:00
|
|
|
SkPaint paint;
|
|
|
|
canvas->drawBitmap(fCheckerboard, 0, 0);
|
2013-07-24 22:19:24 +00:00
|
|
|
drawProc[j](canvas, r, filters[i]);
|
|
|
|
canvas->translate(0, DY);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(DX, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-01-26 20:20:14 +00:00
|
|
|
SkBitmap fCheckerboard;
|
2013-07-24 22:19:24 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-12-10 21:31:59 +00:00
|
|
|
DEF_GM( return new ImageFiltersCroppedGM; )
|