2013-11-26 21:52:38 +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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColorFilter.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkImageFilter.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/utils/SkTextUtils.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2013-11-26 21:52:38 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2013-11-26 21:52:38 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-06 15:40:59 +00:00
|
|
|
static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2013-11-26 21:52:38 +00:00
|
|
|
SkPaint paint;
|
2016-04-06 15:40:59 +00:00
|
|
|
paint.setImageFilter(std::move(imf));
|
2013-11-26 21:52:38 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(r);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2016-04-06 15:40:59 +00:00
|
|
|
static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2013-11-26 21:52:38 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
2016-04-06 15:40:59 +00:00
|
|
|
paint.setImageFilter(std::move(imf));
|
2013-11-26 21:52:38 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(r);
|
|
|
|
canvas->drawCircle(r.centerX(), r.centerY(), r.width()/3, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2016-04-06 15:40:59 +00:00
|
|
|
static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2013-11-26 21:52:38 +00:00
|
|
|
SkPaint paint;
|
2016-04-06 15:40:59 +00:00
|
|
|
paint.setImageFilter(std::move(imf));
|
2013-11-26 21:52:38 +00:00
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
paint.setAntiAlias(true);
|
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);
|
2013-11-26 21:52:38 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(r);
|
2019-01-03 20:45:53 +00:00
|
|
|
SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint, SkTextUtils::kCenter_Align);
|
2013-11-26 21:52:38 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2016-04-06 15:40:59 +00:00
|
|
|
static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
|
2013-11-26 21:52:38 +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-11-26 21:52:38 +00:00
|
|
|
bm.eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
SkCanvas c(bm);
|
2015-08-27 14:41:13 +00:00
|
|
|
draw_path(&c, r, nullptr);
|
2013-11-26 21:52:38 +00:00
|
|
|
|
2016-04-06 15:40:59 +00:00
|
|
|
paint.setImageFilter(std::move(imf));
|
2013-11-26 21:52:38 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(r);
|
|
|
|
canvas->drawBitmap(bm, 0, 0, &paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-01-12 22:15:50 +00:00
|
|
|
DEF_SIMPLE_GM(dropshadowimagefilter, canvas, 400, 656) {
|
|
|
|
void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = {
|
|
|
|
draw_bitmap, draw_path, draw_paint, draw_text
|
|
|
|
};
|
|
|
|
|
2019-04-08 20:23:20 +00:00
|
|
|
sk_sp<SkColorFilter> cf(SkColorFilters::Blend(SK_ColorMAGENTA, SkBlendMode::kSrcIn));
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> cfif(SkImageFilters::ColorFilter(std::move(cf), nullptr));
|
|
|
|
SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
|
|
|
|
SkIRect bogusRect = SkIRect::MakeXYWH(-100, -100, 10, 10);
|
2017-01-12 22:15:50 +00:00
|
|
|
|
|
|
|
sk_sp<SkImageFilter> filters[] = {
|
|
|
|
nullptr,
|
2019-08-02 19:21:23 +00:00
|
|
|
SkImageFilters::DropShadow(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE, nullptr),
|
|
|
|
SkImageFilters::DropShadow(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE, nullptr),
|
|
|
|
SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr),
|
|
|
|
SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, std::move(cfif)),
|
|
|
|
SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr, &cropRect),
|
|
|
|
SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr, &bogusRect),
|
|
|
|
SkImageFilters::DropShadowOnly(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr),
|
2017-01-12 22:15:50 +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);
|
|
|
|
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);
|
2013-11-26 21:52:38 +00:00
|
|
|
}
|
2017-01-12 22:15:50 +00:00
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(DX, 0);
|
2013-11-26 21:52:38 +00:00
|
|
|
}
|
2017-01-12 22:15:50 +00:00
|
|
|
}
|