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" | |
11 #include "SkLumaXfermode.h" | 10 #include "SkLumaXfermode.h" |
12 | 11 |
13 static unsigned kSize = 80; | 12 static void show_scene(SkCanvas* canvas, SkXfermode* mode, const char* label) { |
14 static unsigned kInset = 10; | |
15 static SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0); | |
16 static SkColor kColor2 = SkColorSetARGB(0xff, 0x80, 0xff, 0); | |
17 | |
18 static void draw_label(SkCanvas* canvas, const char* label, | |
19 const SkPoint& offset) { | |
20 SkPaint paint; | 13 SkPaint paint; |
21 size_t len = strlen(label); | 14 paint.setAntiAlias(true); |
22 | 15 SkRect r, c, bounds = { 10, 10, 110, 110 }; |
23 SkScalar width = paint.measureText(label, len); | |
24 canvas->drawText(label, len, offset.x() - width / 2, offset.y(), | |
25 paint); | |
26 } | |
27 | |
28 static void draw_scene(SkCanvas* canvas, SkXfermode* mode, bool aa, | |
29 SkShader* s1, SkShader* s2) { | |
30 SkPaint paint; | |
31 paint.setAntiAlias(aa); | |
32 SkRect r, c, bounds = SkRect::MakeWH(kSize, kSize); | |
33 | 16 |
34 c = bounds; | 17 c = bounds; |
35 c.fRight = bounds.centerX(); | 18 c.fRight = bounds.centerX(); |
36 canvas->drawRect(bounds, paint); | 19 r = bounds; |
| 20 r.fBottom = bounds.centerY(); |
| 21 canvas->drawRect(r, paint); |
| 22 |
| 23 SkScalar tw = paint.measureText(label, strlen(label)); |
| 24 canvas->drawText(label, strlen(label), bounds.centerX() - tw / 2, |
| 25 bounds.bottom() + 15, paint); |
37 | 26 |
38 canvas->saveLayer(&bounds, NULL); | 27 canvas->saveLayer(&bounds, NULL); |
39 | 28 |
40 r = bounds; | 29 r = bounds; |
41 r.inset(kInset, 0); | 30 r.inset(20, 0); |
42 paint.setShader(s1); | 31 paint.setColor(0x80FFFF00); |
43 paint.setColor(s1 ? SK_ColorBLACK : SkColorSetA(kColor1, 0x80)); | |
44 canvas->drawOval(r, paint); | 32 canvas->drawOval(r, paint); |
45 if (!s1) { | 33 canvas->save(); |
46 canvas->save(); | 34 canvas->clipRect(c); |
47 canvas->clipRect(c); | 35 paint.setColor(0xFFFFFF00); |
48 paint.setColor(s1 ? SK_ColorBLACK : kColor1); | 36 canvas->drawOval(r, paint); |
49 canvas->drawOval(r, paint); | 37 canvas->restore(); |
50 canvas->restore(); | |
51 } | |
52 | 38 |
53 SkPaint xferPaint; | 39 SkPaint xferPaint; |
54 xferPaint.setXfermode(mode); | 40 xferPaint.setXfermode(mode); |
55 canvas->saveLayer(&bounds, &xferPaint); | 41 canvas->saveLayer(&bounds, &xferPaint); |
56 | 42 |
57 r = bounds; | 43 r = bounds; |
58 r.inset(0, kInset); | 44 r.inset(0, 20); |
59 paint.setShader(s2); | 45 paint.setColor(0x8080FF00); |
60 paint.setColor(s2 ? SK_ColorBLACK : SkColorSetA(kColor2, 0x80)); | |
61 canvas->drawOval(r, paint); | 46 canvas->drawOval(r, paint); |
62 if (!s2) { | 47 canvas->save(); |
63 canvas->save(); | 48 canvas->clipRect(c); |
64 canvas->clipRect(c); | 49 paint.setColor(0xFF80FF00); |
65 paint.setColor(s2 ? SK_ColorBLACK : kColor2); | 50 canvas->drawOval(r, paint); |
66 canvas->drawOval(r, paint); | 51 canvas->restore(); |
67 canvas->restore(); | |
68 } | |
69 | 52 |
70 canvas->restore(); | 53 canvas->restore(); |
71 canvas->restore(); | 54 canvas->restore(); |
72 } | 55 } |
73 | 56 |
74 class LumaXfermodeGM : public skiagm::GM { | 57 class LumaXfermodeGM : public skiagm::GM { |
75 public: | 58 public: |
76 LumaXfermodeGM() { | 59 LumaXfermodeGM() {} |
77 fSrcInXfer.reset(SkLumaMaskXfermode::Create(SkXfermode::kSrcIn_Mode)); | |
78 fDstInXfer.reset(SkLumaMaskXfermode::Create(SkXfermode::kDstIn_Mode)); | |
79 | |
80 SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) }; | |
81 SkColor g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) }; | |
82 SkPoint g1Points[] = { {0, 0}, {0, 100} }; | |
83 SkPoint g2Points[] = { {0, 0}, {kSize, 0} }; | |
84 SkScalar pos[] = { 0.2f, 1.0f }; | |
85 | |
86 fGr1.reset(SkGradientShader::CreateLinear(g1Points, | |
87 g1Colors, | |
88 pos, | |
89 SK_ARRAY_COUNT(g1Colors), | |
90 SkShader::kClamp_TileMode)); | |
91 fGr2.reset(SkGradientShader::CreateLinear(g2Points, | |
92 g2Colors, | |
93 pos, | |
94 SK_ARRAY_COUNT(g2Colors), | |
95 SkShader::kClamp_TileMode)); | |
96 } | |
97 | 60 |
98 protected: | 61 protected: |
99 virtual SkString onShortName() SK_OVERRIDE { | 62 virtual SkString onShortName() SK_OVERRIDE { |
100 return SkString("lumamode"); | 63 return SkString("lumamode"); |
101 } | 64 } |
102 | 65 |
103 virtual SkISize onISize() SK_OVERRIDE { | 66 virtual SkISize onISize() SK_OVERRIDE { |
104 return SkISize::Make(600, 420); | 67 return SkISize::Make(450, 140); |
105 } | 68 } |
106 | 69 |
107 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 70 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
108 SkXfermode* modes[] = { NULL, fSrcInXfer, fDstInXfer }; | 71 show_scene(canvas, NULL, "SrcOver"); |
109 struct { | 72 canvas->translate(150, 0); |
110 SkShader* fShader1; | 73 SkAutoTUnref<SkXfermode> src_in(SkLumaMaskXfermode::Create(SkXfermode::k
SrcIn_Mode)); |
111 SkShader* fShader2; | 74 show_scene(canvas, src_in.get(), "SrcInLuma"); |
112 } shaders[] = { | 75 canvas->translate(150, 0); |
113 { NULL, NULL }, | 76 SkAutoTUnref<SkXfermode> dst_in(SkLumaMaskXfermode::Create(SkXfermode::k
DstIn_Mode)); |
114 { NULL, fGr2 }, | 77 show_scene(canvas, dst_in.get(), "DstInLuma"); |
115 { fGr1, NULL }, | |
116 { fGr1, fGr2 }, | |
117 }; | |
118 | |
119 draw_label(canvas, "SrcOver", | |
120 SkPoint::Make(kSize + 2 * kInset, 20)); | |
121 draw_label(canvas, "SrcInLuma", | |
122 SkPoint::Make(3 * (kSize + 2 * kInset), 20)); | |
123 draw_label(canvas, "DstInLuma", | |
124 SkPoint::Make(5 * (kSize + 2 * kInset), 20)); | |
125 for (size_t i = 0; i < SK_ARRAY_COUNT(shaders); ++i) { | |
126 canvas->save(); | |
127 canvas->translate(kInset, 30 + i * (kSize + 2 * kInset)); | |
128 for (size_t m = 0; m < SK_ARRAY_COUNT(modes); ++m) { | |
129 draw_scene(canvas, modes[m], true, shaders[i].fShader1, | |
130 shaders[i].fShader2); | |
131 canvas->translate(kSize + 2 * kInset, 0); | |
132 draw_scene(canvas, modes[m], false, shaders[i].fShader1, | |
133 shaders[i].fShader2); | |
134 canvas->translate(kSize + 2 * kInset, 0); | |
135 } | |
136 canvas->restore(); | |
137 } | |
138 } | 78 } |
139 | 79 |
140 private: | 80 private: |
141 SkAutoTUnref<SkShader> fGr1, fGr2; | |
142 SkAutoTUnref<SkXfermode> fSrcInXfer, fDstInXfer; | |
143 | |
144 typedef skiagm::GM INHERITED; | 81 typedef skiagm::GM INHERITED; |
145 }; | 82 }; |
146 | 83 |
147 ////////////////////////////////////////////////////////////////////////////// | 84 ////////////////////////////////////////////////////////////////////////////// |
148 | 85 |
149 DEF_GM( return SkNEW(LumaXfermodeGM); ) | 86 DEF_GM( return SkNEW(LumaXfermodeGM); ) |
OLD | NEW |