• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkBlendModePriv.h"
11 #include "SkCanvas.h"
12 #include "SkGradientShader.h"
13 #include "SkLumaColorFilter.h"
14 
15 static SkScalar kSize   = 80;
16 static SkScalar kInset  = 10;
17 static SkColor  kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0);
18 static SkColor  kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0);
19 
draw_label(SkCanvas * canvas,const char * label,const SkPoint & offset)20 static void draw_label(SkCanvas* canvas, const char* label,
21                        const SkPoint& offset) {
22     SkPaint paint;
23     sk_tool_utils::set_portable_typeface(&paint);
24     size_t len = strlen(label);
25 
26     SkScalar width = paint.measureText(label, len);
27     canvas->drawText(label, len, offset.x() - width / 2, offset.y(),
28                      paint);
29 }
30 
draw_scene(SkCanvas * canvas,const sk_sp<SkColorFilter> & filter,SkBlendMode mode,const sk_sp<SkShader> & s1,const sk_sp<SkShader> & s2)31 static void draw_scene(SkCanvas* canvas, const sk_sp<SkColorFilter>& filter, SkBlendMode mode,
32                        const sk_sp<SkShader>& s1, const sk_sp<SkShader>& s2) {
33     SkPaint paint;
34     paint.setAntiAlias(true);
35     SkRect r, c, bounds = SkRect::MakeWH(kSize, kSize);
36 
37     c = bounds;
38     c.fRight = bounds.centerX();
39     paint.setARGB(0x20, 0, 0, 0xff);
40     canvas->drawRect(bounds, paint);
41 
42     canvas->saveLayer(&bounds, nullptr);
43 
44     r = bounds;
45     r.inset(kInset, 0);
46     paint.setShader(s1);
47     paint.setColor(s1 ? SK_ColorBLACK : SkColorSetA(kColor1, 0x80));
48     canvas->drawOval(r, paint);
49     if (!s1) {
50         canvas->save();
51         canvas->clipRect(c);
52         paint.setColor(kColor1);
53         canvas->drawOval(r, paint);
54         canvas->restore();
55     }
56 
57     SkPaint xferPaint;
58     xferPaint.setBlendMode(mode);
59     canvas->saveLayer(&bounds, &xferPaint);
60 
61     r = bounds;
62     r.inset(0, kInset);
63     paint.setShader(s2);
64     paint.setColor(s2 ? SK_ColorBLACK : SkColorSetA(kColor2, 0x80));
65     paint.setColorFilter(filter);
66     canvas->drawOval(r, paint);
67     if (!s2) {
68         canvas->save();
69         canvas->clipRect(c);
70         paint.setColor(kColor2);
71         canvas->drawOval(r, paint);
72         canvas->restore();
73     }
74 
75     canvas->restore();
76     canvas->restore();
77 }
78 
79 class LumaFilterGM : public skiagm::GM {
80 public:
LumaFilterGM()81     LumaFilterGM() {
82         SkColor  g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) };
83         SkColor  g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) };
84         SkPoint  g1Points[] = { { 0, 0 }, { 0,     100 } };
85         SkPoint  g2Points[] = { { 0, 0 }, { kSize, 0   } };
86         SkScalar pos[] = { 0.2f, 1.0f };
87 
88         fFilter = SkLumaColorFilter::Make();
89         fGr1 = SkGradientShader::MakeLinear(g1Points, g1Colors, pos, SK_ARRAY_COUNT(g1Colors),
90                                             SkShader::kClamp_TileMode);
91         fGr2 = SkGradientShader::MakeLinear(g2Points, g2Colors, pos, SK_ARRAY_COUNT(g2Colors),
92                                             SkShader::kClamp_TileMode);
93     }
94 
95 protected:
96 
onShortName()97     SkString onShortName() override {
98         return SkString("lumafilter");
99     }
100 
onISize()101     SkISize onISize() override {
102         return SkISize::Make(600, 420);
103     }
104 
onDraw(SkCanvas * canvas)105     void onDraw(SkCanvas* canvas) override {
106         SkBlendMode modes[] = {
107             SkBlendMode::kSrcOver,
108             SkBlendMode::kDstOver,
109             SkBlendMode::kSrcATop,
110             SkBlendMode::kDstATop,
111             SkBlendMode::kSrcIn,
112             SkBlendMode::kDstIn,
113         };
114         struct {
115             const sk_sp<SkShader>& fShader1;
116             const sk_sp<SkShader>& fShader2;
117         } shaders[] = {
118             { nullptr, nullptr },
119             { nullptr, fGr2 },
120             { fGr1, nullptr },
121             { fGr1, fGr2 },
122         };
123 
124         SkScalar gridStep = kSize + 2 * kInset;
125         for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
126             draw_label(canvas, SkBlendMode_Name(modes[i]),
127                        SkPoint::Make(gridStep * (0.5f + i), 20));
128         }
129 
130         for (size_t i = 0; i < SK_ARRAY_COUNT(shaders); ++i) {
131             canvas->save();
132             canvas->translate(kInset, gridStep * i + 30);
133             for (size_t m = 0; m < SK_ARRAY_COUNT(modes); ++m) {
134                 draw_scene(canvas, fFilter, modes[m], shaders[i].fShader1,
135                            shaders[i].fShader2);
136                 canvas->translate(gridStep, 0);
137             }
138             canvas->restore();
139         }
140     }
141 
142 private:
143     sk_sp<SkColorFilter>    fFilter;
144     sk_sp<SkShader>         fGr1, fGr2;
145 
146     typedef skiagm::GM INHERITED;
147 };
148 
149 //////////////////////////////////////////////////////////////////////////////
150 
151 DEF_GM(return new LumaFilterGM;)
152