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