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/gm.h"
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"
25
26 #include <utility>
27
28 ///////////////////////////////////////////////////////////////////////////////
29
draw_paint(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)30 static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
31 SkPaint paint;
32 paint.setImageFilter(std::move(imf));
33 paint.setColor(SK_ColorBLACK);
34 canvas->save();
35 canvas->clipRect(r);
36 canvas->drawPaint(paint);
37 canvas->restore();
38 }
39
draw_path(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)40 static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
41 SkPaint paint;
42 paint.setColor(SK_ColorGREEN);
43 paint.setImageFilter(std::move(imf));
44 paint.setAntiAlias(true);
45 canvas->save();
46 canvas->clipRect(r);
47 canvas->drawCircle(r.centerX(), r.centerY(), r.width()/3, paint);
48 canvas->restore();
49 }
50
draw_text(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)51 static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
52 SkPaint paint;
53 paint.setImageFilter(std::move(imf));
54 paint.setColor(SK_ColorGREEN);
55 paint.setAntiAlias(true);
56
57 SkFont font(ToolUtils::create_portable_typeface(), r.height() / 2);
58 canvas->save();
59 canvas->clipRect(r);
60 SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint, SkTextUtils::kCenter_Align);
61 canvas->restore();
62 }
63
draw_bitmap(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)64 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
65 SkPaint paint;
66
67 SkIRect bounds;
68 r.roundOut(&bounds);
69
70 SkBitmap bm;
71 bm.allocN32Pixels(bounds.width(), bounds.height());
72 bm.eraseColor(SK_ColorTRANSPARENT);
73 SkCanvas c(bm);
74 draw_path(&c, r, nullptr);
75
76 paint.setImageFilter(std::move(imf));
77 canvas->save();
78 canvas->clipRect(r);
79 canvas->drawBitmap(bm, 0, 0, &paint);
80 canvas->restore();
81 }
82
83 ///////////////////////////////////////////////////////////////////////////////
84
85 DEF_SIMPLE_GM(dropshadowimagefilter, canvas, 400, 656) {
86 void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = {
87 draw_bitmap, draw_path, draw_paint, draw_text
88 };
89
90 sk_sp<SkColorFilter> cf(SkColorFilters::Blend(SK_ColorMAGENTA, SkBlendMode::kSrcIn));
91 sk_sp<SkImageFilter> cfif(SkImageFilters::ColorFilter(std::move(cf), nullptr));
92 SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
93 SkIRect bogusRect = SkIRect::MakeXYWH(-100, -100, 10, 10);
94
95 sk_sp<SkImageFilter> filters[] = {
96 nullptr,
97 SkImageFilters::DropShadow(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE, nullptr),
98 SkImageFilters::DropShadow(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE, nullptr),
99 SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr),
100 SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, std::move(cfif)),
101 SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr, &cropRect),
102 SkImageFilters::DropShadow(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr, &bogusRect),
103 SkImageFilters::DropShadowOnly(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr),
104 };
105
106 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
107 SkScalar MARGIN = SkIntToScalar(16);
108 SkScalar DX = r.width() + MARGIN;
109 SkScalar DY = r.height() + MARGIN;
110
111 canvas->translate(MARGIN, MARGIN);
112 for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) {
113 canvas->save();
114 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
115 drawProc[j](canvas, r, filters[i]);
116 canvas->translate(0, DY);
117 }
118 canvas->restore();
119 canvas->translate(DX, 0);
120 }
121 }
122