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 "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkGradientShader.h" | 10 #include "SkGradientShader.h" |
11 #include "SkLumaColorFilter.h" | 11 #include "SkLumaColorFilter.h" |
12 | 12 |
13 static SkScalar kSize = 80; | 13 static SkScalar kSize = 80; |
14 static SkScalar kInset = 10; | 14 static SkScalar kInset = 10; |
15 static SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0); | 15 static SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0); |
16 static SkColor kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0); | 16 static SkColor kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0); |
17 | 17 |
18 static void draw_label(SkCanvas* canvas, const char* label, | 18 static void draw_label(SkCanvas* canvas, const char* label, |
19 const SkPoint& offset) { | 19 const SkPoint& offset) { |
20 SkPaint paint; | 20 SkPaint paint; |
21 sk_tool_utils::set_portable_typeface(&paint); | 21 sk_tool_utils::set_portable_typeface(&paint); |
22 size_t len = strlen(label); | 22 size_t len = strlen(label); |
23 | 23 |
24 SkScalar width = paint.measureText(label, len); | 24 SkScalar width = paint.measureText(label, len); |
25 canvas->drawText(label, len, offset.x() - width / 2, offset.y(), | 25 canvas->drawText(label, len, offset.x() - width / 2, offset.y(), |
26 paint); | 26 paint); |
27 } | 27 } |
28 | 28 |
29 static void draw_scene(SkCanvas* canvas, SkColorFilter* filter, | 29 static void draw_scene(SkCanvas* canvas, SkColorFilter* filter, SkXfermode::Mode
mode, |
30 SkXfermode::Mode mode, SkShader* s1, | 30 const sk_sp<SkShader>& s1, const sk_sp<SkShader>& s2) { |
31 SkShader* s2) { | |
32 SkPaint paint; | 31 SkPaint paint; |
33 paint.setAntiAlias(true); | 32 paint.setAntiAlias(true); |
34 SkRect r, c, bounds = SkRect::MakeWH(kSize, kSize); | 33 SkRect r, c, bounds = SkRect::MakeWH(kSize, kSize); |
35 | 34 |
36 c = bounds; | 35 c = bounds; |
37 c.fRight = bounds.centerX(); | 36 c.fRight = bounds.centerX(); |
38 paint.setARGB(0x20, 0, 0, 0xff); | 37 paint.setARGB(0x20, 0, 0, 0xff); |
39 canvas->drawRect(bounds, paint); | 38 canvas->drawRect(bounds, paint); |
40 | 39 |
41 canvas->saveLayer(&bounds, nullptr); | 40 canvas->saveLayer(&bounds, nullptr); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 class LumaFilterGM : public skiagm::GM { | 77 class LumaFilterGM : public skiagm::GM { |
79 public: | 78 public: |
80 LumaFilterGM() { | 79 LumaFilterGM() { |
81 SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) }; | 80 SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) }; |
82 SkColor g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) }; | 81 SkColor g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) }; |
83 SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } }; | 82 SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } }; |
84 SkPoint g2Points[] = { { 0, 0 }, { kSize, 0 } }; | 83 SkPoint g2Points[] = { { 0, 0 }, { kSize, 0 } }; |
85 SkScalar pos[] = { 0.2f, 1.0f }; | 84 SkScalar pos[] = { 0.2f, 1.0f }; |
86 | 85 |
87 fFilter.reset(SkLumaColorFilter::Create()); | 86 fFilter.reset(SkLumaColorFilter::Create()); |
88 fGr1.reset(SkGradientShader::CreateLinear(g1Points, | 87 fGr1 = SkGradientShader::MakeLinear(g1Points, g1Colors, pos, SK_ARRAY_CO
UNT(g1Colors), |
89 g1Colors, | 88 SkShader::kClamp_TileMode); |
90 pos, | 89 fGr2 = SkGradientShader::MakeLinear(g2Points, g2Colors, pos, SK_ARRAY_CO
UNT(g2Colors), |
91 SK_ARRAY_COUNT(g1Colors), | 90 SkShader::kClamp_TileMode); |
92 SkShader::kClamp_TileMode)); | |
93 fGr2.reset(SkGradientShader::CreateLinear(g2Points, | |
94 g2Colors, | |
95 pos, | |
96 SK_ARRAY_COUNT(g2Colors), | |
97 SkShader::kClamp_TileMode)); | |
98 } | 91 } |
99 | 92 |
100 protected: | 93 protected: |
101 | 94 |
102 SkString onShortName() override { | 95 SkString onShortName() override { |
103 return SkString("lumafilter"); | 96 return SkString("lumafilter"); |
104 } | 97 } |
105 | 98 |
106 SkISize onISize() override { | 99 SkISize onISize() override { |
107 return SkISize::Make(600, 420); | 100 return SkISize::Make(600, 420); |
108 } | 101 } |
109 | 102 |
110 void onDraw(SkCanvas* canvas) override { | 103 void onDraw(SkCanvas* canvas) override { |
111 SkXfermode::Mode modes[] = { SkXfermode::kSrcOver_Mode, | 104 SkXfermode::Mode modes[] = { SkXfermode::kSrcOver_Mode, |
112 SkXfermode::kDstOver_Mode, | 105 SkXfermode::kDstOver_Mode, |
113 SkXfermode::kSrcATop_Mode, | 106 SkXfermode::kSrcATop_Mode, |
114 SkXfermode::kDstATop_Mode, | 107 SkXfermode::kDstATop_Mode, |
115 SkXfermode::kSrcIn_Mode, | 108 SkXfermode::kSrcIn_Mode, |
116 SkXfermode::kDstIn_Mode, | 109 SkXfermode::kDstIn_Mode, |
117 }; | 110 }; |
118 struct { | 111 struct { |
119 SkShader* fShader1; | 112 const sk_sp<SkShader>& fShader1; |
120 SkShader* fShader2; | 113 const sk_sp<SkShader>& fShader2; |
121 } shaders[] = { | 114 } shaders[] = { |
122 { nullptr, nullptr }, | 115 { nullptr, nullptr }, |
123 { nullptr, fGr2 }, | 116 { nullptr, fGr2 }, |
124 { fGr1, nullptr }, | 117 { fGr1, nullptr }, |
125 { fGr1, fGr2 }, | 118 { fGr1, fGr2 }, |
126 }; | 119 }; |
127 | 120 |
128 SkScalar gridStep = kSize + 2 * kInset; | 121 SkScalar gridStep = kSize + 2 * kInset; |
129 for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) { | 122 for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) { |
130 draw_label(canvas, SkXfermode::ModeName(modes[i]), | 123 draw_label(canvas, SkXfermode::ModeName(modes[i]), |
131 SkPoint::Make(gridStep * (0.5f + i), 20)); | 124 SkPoint::Make(gridStep * (0.5f + i), 20)); |
132 } | 125 } |
133 | 126 |
134 for (size_t i = 0; i < SK_ARRAY_COUNT(shaders); ++i) { | 127 for (size_t i = 0; i < SK_ARRAY_COUNT(shaders); ++i) { |
135 canvas->save(); | 128 canvas->save(); |
136 canvas->translate(kInset, gridStep * i + 30); | 129 canvas->translate(kInset, gridStep * i + 30); |
137 for (size_t m = 0; m < SK_ARRAY_COUNT(modes); ++m) { | 130 for (size_t m = 0; m < SK_ARRAY_COUNT(modes); ++m) { |
138 draw_scene(canvas, fFilter, modes[m], shaders[i].fShader1, | 131 draw_scene(canvas, fFilter, modes[m], shaders[i].fShader1, |
139 shaders[i].fShader2); | 132 shaders[i].fShader2); |
140 canvas->translate(gridStep, 0); | 133 canvas->translate(gridStep, 0); |
141 } | 134 } |
142 canvas->restore(); | 135 canvas->restore(); |
143 } | 136 } |
144 } | 137 } |
145 | 138 |
146 private: | 139 private: |
147 SkAutoTUnref<SkColorFilter> fFilter; | 140 SkAutoTUnref<SkColorFilter> fFilter; |
148 SkAutoTUnref<SkShader> fGr1, fGr2; | 141 sk_sp<SkShader> fGr1, fGr2; |
149 | 142 |
150 typedef skiagm::GM INHERITED; | 143 typedef skiagm::GM INHERITED; |
151 }; | 144 }; |
152 | 145 |
153 ////////////////////////////////////////////////////////////////////////////// | 146 ////////////////////////////////////////////////////////////////////////////// |
154 | 147 |
155 DEF_GM(return new LumaFilterGM;) | 148 DEF_GM(return new LumaFilterGM;) |
OLD | NEW |