1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "gm.h"
9 #include "sk_tool_utils.h"
10 #include "SkColorFilter.h"
11 #include "SkColorFilterImageFilter.h"
12 #include "SkDropShadowImageFilter.h"
13 #include "SkTextUtils.h"
14
15 ///////////////////////////////////////////////////////////////////////////////
16
draw_paint(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)17 static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
18 SkPaint paint;
19 paint.setImageFilter(std::move(imf));
20 paint.setColor(SK_ColorBLACK);
21 canvas->save();
22 canvas->clipRect(r);
23 canvas->drawPaint(paint);
24 canvas->restore();
25 }
26
draw_path(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)27 static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
28 SkPaint paint;
29 paint.setColor(SK_ColorGREEN);
30 paint.setImageFilter(std::move(imf));
31 paint.setAntiAlias(true);
32 canvas->save();
33 canvas->clipRect(r);
34 canvas->drawCircle(r.centerX(), r.centerY(), r.width()/3, paint);
35 canvas->restore();
36 }
37
draw_text(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)38 static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
39 SkPaint paint;
40 paint.setImageFilter(std::move(imf));
41 paint.setColor(SK_ColorGREEN);
42 paint.setAntiAlias(true);
43
44 SkFont font(sk_tool_utils::create_portable_typeface(), r.height()/2);
45 canvas->save();
46 canvas->clipRect(r);
47 SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint, SkTextUtils::kCenter_Align);
48 canvas->restore();
49 }
50
draw_bitmap(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)51 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
52 SkPaint paint;
53
54 SkIRect bounds;
55 r.roundOut(&bounds);
56
57 SkBitmap bm;
58 bm.allocN32Pixels(bounds.width(), bounds.height());
59 bm.eraseColor(SK_ColorTRANSPARENT);
60 SkCanvas c(bm);
61 draw_path(&c, r, nullptr);
62
63 paint.setImageFilter(std::move(imf));
64 canvas->save();
65 canvas->clipRect(r);
66 canvas->drawBitmap(bm, 0, 0, &paint);
67 canvas->restore();
68 }
69
70 ///////////////////////////////////////////////////////////////////////////////
71
72 DEF_SIMPLE_GM(dropshadowimagefilter, canvas, 400, 656) {
73 void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = {
74 draw_bitmap, draw_path, draw_paint, draw_text
75 };
76
77 sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(SK_ColorMAGENTA,
78 SkBlendMode::kSrcIn));
79 sk_sp<SkImageFilter> cfif(SkColorFilterImageFilter::Make(std::move(cf), nullptr));
80 SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)),
81 SkImageFilter::CropRect::kHasAll_CropEdge);
82 SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)),
83 SkImageFilter::CropRect::kHasAll_CropEdge);
84
85 sk_sp<SkImageFilter> filters[] = {
86 nullptr,
87 SkDropShadowImageFilter::Make(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE,
88 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr),
89 SkDropShadowImageFilter::Make(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE,
90 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr),
91 SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
92 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr),
93 SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
94 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, std::move(cfif)),
95 SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
96 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr, &cropRect),
97 SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
98 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr, &bogusRect),
99 SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
100 SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode, nullptr),
101 };
102
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;
107
108 canvas->translate(MARGIN, MARGIN);
109 for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) {
110 canvas->save();
111 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
112 drawProc[j](canvas, r, filters[i]);
113 canvas->translate(0, DY);
114 }
115 canvas->restore();
116 canvas->translate(DX, 0);
117 }
118 }
119