OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkColorFilter.h" | 9 #include "SkColorFilter.h" |
10 | 10 |
11 #include "SkColorFilterImageFilter.h" | 11 #include "SkColorFilterImageFilter.h" |
12 #include "SkDropShadowImageFilter.h" | 12 #include "SkDropShadowImageFilter.h" |
13 | 13 |
14 /////////////////////////////////////////////////////////////////////////////// | 14 /////////////////////////////////////////////////////////////////////////////// |
15 | 15 |
16 static void draw_paint(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { | 16 static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> i
mf) { |
17 SkPaint paint; | 17 SkPaint paint; |
18 paint.setImageFilter(imf); | 18 paint.setImageFilter(std::move(imf)); |
19 paint.setColor(SK_ColorBLACK); | 19 paint.setColor(SK_ColorBLACK); |
20 canvas->save(); | 20 canvas->save(); |
21 canvas->clipRect(r); | 21 canvas->clipRect(r); |
22 canvas->drawPaint(paint); | 22 canvas->drawPaint(paint); |
23 canvas->restore(); | 23 canvas->restore(); |
24 } | 24 } |
25 | 25 |
26 static void draw_path(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { | 26 static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> im
f) { |
27 SkPaint paint; | 27 SkPaint paint; |
28 paint.setColor(SK_ColorGREEN); | 28 paint.setColor(SK_ColorGREEN); |
29 paint.setImageFilter(imf); | 29 paint.setImageFilter(std::move(imf)); |
30 paint.setAntiAlias(true); | 30 paint.setAntiAlias(true); |
31 canvas->save(); | 31 canvas->save(); |
32 canvas->clipRect(r); | 32 canvas->clipRect(r); |
33 canvas->drawCircle(r.centerX(), r.centerY(), r.width()/3, paint); | 33 canvas->drawCircle(r.centerX(), r.centerY(), r.width()/3, paint); |
34 canvas->restore(); | 34 canvas->restore(); |
35 } | 35 } |
36 | 36 |
37 static void draw_text(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { | 37 static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> im
f) { |
38 SkPaint paint; | 38 SkPaint paint; |
39 paint.setImageFilter(imf); | 39 paint.setImageFilter(std::move(imf)); |
40 paint.setColor(SK_ColorGREEN); | 40 paint.setColor(SK_ColorGREEN); |
41 paint.setAntiAlias(true); | 41 paint.setAntiAlias(true); |
42 sk_tool_utils::set_portable_typeface(&paint); | 42 sk_tool_utils::set_portable_typeface(&paint); |
43 paint.setTextSize(r.height()/2); | 43 paint.setTextSize(r.height()/2); |
44 paint.setTextAlign(SkPaint::kCenter_Align); | 44 paint.setTextAlign(SkPaint::kCenter_Align); |
45 canvas->save(); | 45 canvas->save(); |
46 canvas->clipRect(r); | 46 canvas->clipRect(r); |
47 canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint); | 47 canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint); |
48 canvas->restore(); | 48 canvas->restore(); |
49 } | 49 } |
50 | 50 |
51 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { | 51 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter>
imf) { |
52 SkPaint paint; | 52 SkPaint paint; |
53 | 53 |
54 SkIRect bounds; | 54 SkIRect bounds; |
55 r.roundOut(&bounds); | 55 r.roundOut(&bounds); |
56 | 56 |
57 SkBitmap bm; | 57 SkBitmap bm; |
58 bm.allocN32Pixels(bounds.width(), bounds.height()); | 58 bm.allocN32Pixels(bounds.width(), bounds.height()); |
59 bm.eraseColor(SK_ColorTRANSPARENT); | 59 bm.eraseColor(SK_ColorTRANSPARENT); |
60 SkCanvas c(bm); | 60 SkCanvas c(bm); |
61 draw_path(&c, r, nullptr); | 61 draw_path(&c, r, nullptr); |
62 | 62 |
63 paint.setImageFilter(imf); | 63 paint.setImageFilter(std::move(imf)); |
64 canvas->save(); | 64 canvas->save(); |
65 canvas->clipRect(r); | 65 canvas->clipRect(r); |
66 canvas->drawBitmap(bm, 0, 0, &paint); | 66 canvas->drawBitmap(bm, 0, 0, &paint); |
67 canvas->restore(); | 67 canvas->restore(); |
68 } | 68 } |
69 | 69 |
70 /////////////////////////////////////////////////////////////////////////////// | 70 /////////////////////////////////////////////////////////////////////////////// |
71 | 71 |
72 class DropShadowImageFilterGM : public skiagm::GM { | 72 class DropShadowImageFilterGM : public skiagm::GM { |
73 public: | 73 public: |
74 DropShadowImageFilterGM () {} | 74 DropShadowImageFilterGM () {} |
75 | 75 |
76 protected: | 76 protected: |
77 | 77 |
78 virtual SkString onShortName() { | 78 SkString onShortName() override { |
79 return SkString("dropshadowimagefilter"); | 79 return SkString("dropshadowimagefilter"); |
80 } | 80 } |
81 | 81 |
82 virtual SkISize onISize() { return SkISize::Make(400, 656); } | 82 SkISize onISize() override { return SkISize::Make(400, 656); } |
83 | 83 |
84 void draw_frame(SkCanvas* canvas, const SkRect& r) { | 84 void onDraw(SkCanvas* canvas) override { |
85 SkPaint paint; | 85 void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = { |
86 paint.setStyle(SkPaint::kStroke_Style); | |
87 paint.setColor(SK_ColorRED); | |
88 canvas->drawRect(r, paint); | |
89 } | |
90 | |
91 virtual void onDraw(SkCanvas* canvas) { | |
92 void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = { | |
93 draw_bitmap, draw_path, draw_paint, draw_text | 86 draw_bitmap, draw_path, draw_paint, draw_text |
94 }; | 87 }; |
95 | 88 |
96 sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(SK_ColorMAGENTA, | 89 sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(SK_ColorMAGENTA, |
97 SkXfermode::kSrcIn
_Mode)); | 90 SkXfermode::kSrcIn
_Mode)); |
98 sk_sp<SkImageFilter> cfif(SkColorFilterImageFilter::Make(std::move(cf),
nullptr)); | 91 sk_sp<SkImageFilter> cfif(SkColorFilterImageFilter::Make(std::move(cf),
nullptr)); |
99 SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10,
44, 44)), | 92 SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10,
44, 44)), |
100 SkImageFilter::CropRect::kHasAll_CropEd
ge); | 93 SkImageFilter::CropRect::kHasAll_CropEd
ge); |
101 SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -
100, 10, 10)), | 94 SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -
100, 10, 10)), |
102 SkImageFilter::CropRect::kHasAll_CropE
dge); | 95 SkImageFilter::CropRect::kHasAll_CropE
dge); |
103 | 96 |
104 SkImageFilter* filters[] = { | 97 sk_sp<SkImageFilter> filters[] = { |
105 nullptr, | 98 nullptr, |
106 SkDropShadowImageFilter::Create(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE
, | 99 SkDropShadowImageFilter::Make(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE, |
107 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), | 100 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nu
llptr), |
108 SkDropShadowImageFilter::Create(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE
, | 101 SkDropShadowImageFilter::Make(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE, |
109 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), | 102 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nu
llptr), |
110 SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE
, | 103 SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
111 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), | 104 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nu
llptr), |
112 SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE
, | 105 SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
113 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, cf
if.get(), nullptr), | 106 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, st
d::move(cfif)), |
114 SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE
, | 107 SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
115 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nu
llptr, &cropRect), | 108 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nu
llptr, &cropRect), |
116 SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE
, | 109 SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
117 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nu
llptr, &bogusRect), | 110 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nu
llptr, &bogusRect), |
118 SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE
, | 111 SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
119 SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode), | 112 SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode, nullptr), |
120 }; | 113 }; |
121 | 114 |
122 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); | 115 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); |
123 SkScalar MARGIN = SkIntToScalar(16); | 116 SkScalar MARGIN = SkIntToScalar(16); |
124 SkScalar DX = r.width() + MARGIN; | 117 SkScalar DX = r.width() + MARGIN; |
125 SkScalar DY = r.height() + MARGIN; | 118 SkScalar DY = r.height() + MARGIN; |
126 | 119 |
127 canvas->translate(MARGIN, MARGIN); | 120 canvas->translate(MARGIN, MARGIN); |
128 for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) { | 121 for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) { |
129 canvas->save(); | 122 canvas->save(); |
130 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { | 123 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
131 drawProc[j](canvas, r, filters[i]); | 124 drawProc[j](canvas, r, filters[i]); |
132 canvas->translate(0, DY); | 125 canvas->translate(0, DY); |
133 } | 126 } |
134 canvas->restore(); | 127 canvas->restore(); |
135 canvas->translate(DX, 0); | 128 canvas->translate(DX, 0); |
136 } | 129 } |
137 | |
138 for(size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) { | |
139 SkSafeUnref(filters[j]); | |
140 } | |
141 } | 130 } |
142 | 131 |
143 private: | 132 private: |
144 typedef GM INHERITED; | 133 typedef GM INHERITED; |
145 }; | 134 }; |
146 | 135 |
147 /////////////////////////////////////////////////////////////////////////////// | 136 /////////////////////////////////////////////////////////////////////////////// |
148 | 137 |
149 DEF_GM( return new DropShadowImageFilterGM; ) | 138 DEF_GM( return new DropShadowImageFilterGM; ) |
OLD | NEW |