2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorFilter.h"
14 #include "include/core/SkFont.h"
15 #include "include/core/SkImageFilter.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkTypeface.h"
21 #include "include/core/SkTypes.h"
22 #include "include/effects/SkImageFilters.h"
23 #include "include/utils/SkTextUtils.h"
24 #include "tools/ToolUtils.h"
28 ///////////////////////////////////////////////////////////////////////////////
30 static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
32 paint.setImageFilter(std::move(imf));
33 paint.setColor(SK_ColorBLACK);
36 canvas->drawPaint(paint);
40 static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
42 paint.setColor(SK_ColorGREEN);
43 paint.setImageFilter(std::move(imf));
44 paint.setAntiAlias(true);
47 canvas->drawCircle(r.centerX(), r.centerY(), r.width()/3, paint);
51 static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
53 paint.setImageFilter(std::move(imf));
54 paint.setColor(SK_ColorGREEN);
55 paint.setAntiAlias(true);
57 SkFont font(ToolUtils::create_portable_typeface(), r.height() / 2);
60 SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint, SkTextUtils::kCenter_Align);
64 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
70 auto surf = SkSurface::MakeRasterN32Premul(bounds.width(), bounds.height());
71 draw_path(surf->getCanvas(), r, nullptr);
73 paint.setImageFilter(std::move(imf));
76 surf->draw(canvas, 0, 0, SkSamplingOptions(), &paint);
80 ///////////////////////////////////////////////////////////////////////////////
82 DEF_SIMPLE_GM(dropshadowimagefilter, canvas, 400, 656) {
83 void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = {
84 draw_bitmap, draw_path, draw_paint, draw_text
87 sk_sp<SkColorFilter> cf(SkColorFilters::Blend(SK_ColorMAGENTA, SkBlendMode::kSrcIn));
88 sk_sp<SkImageFilter> cfif(SkImageFilters::ColorFilter(std::move(cf), nullptr));
89 SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
90 SkIRect bogusRect = SkIRect::MakeXYWH(-100, -100, 10, 10);
92 sk_sp<SkImageFilter> filters[] = {
94 SkImageFilters::DropShadow(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE, nullptr),
95 SkImageFilters::DropShadow(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE, nullptr),
96 SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr),
97 SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, std::move(cfif)),
98 SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr, &cropRect),
99 SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr, &bogusRect),
100 SkImageFilters::DropShadowOnly(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr),
103 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
104 SkScalar MARGIN = SkIntToScalar(16);
105 SkScalar DX = r.width() + MARGIN;
106 SkScalar DY = r.height() + MARGIN;
108 canvas->translate(MARGIN, MARGIN);
109 for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) {
111 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
112 drawProc[j](canvas, r, filters[i]);
113 canvas->translate(0, DY);
116 canvas->translate(DX, 0);