blob: dbf45411fb290c9ec65fd5b53bde00e29126f6bc [file] [log] [blame]
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +00001/*
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 "SkCanvas.h"
fmalita@google.coma17093f2013-08-23 19:55:15 +000010#include "SkGradientShader.h"
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000011#include "SkLumaXfermode.h"
12
fmalita@google.coma17093f2013-08-23 19:55:15 +000013static SkScalar kSize = 80;
14static SkScalar kInset = 10;
15static SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0);
16static SkColor kColor2 = SkColorSetARGB(0xff, 0x80, 0xff, 0);
17
18static void draw_label(SkCanvas* canvas, const char* label,
19 const SkPoint& offset) {
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000020 SkPaint paint;
fmalita@google.coma17093f2013-08-23 19:55:15 +000021 size_t len = strlen(label);
22
23 SkScalar width = paint.measureText(label, len);
24 canvas->drawText(label, len, offset.x() - width / 2, offset.y(),
25 paint);
26}
27
28static 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);
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000033
34 c = bounds;
35 c.fRight = bounds.centerX();
fmalita@google.coma17093f2013-08-23 19:55:15 +000036 canvas->drawRect(bounds, paint);
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000037
38 canvas->saveLayer(&bounds, NULL);
39
40 r = bounds;
fmalita@google.coma17093f2013-08-23 19:55:15 +000041 r.inset(kInset, 0);
42 paint.setShader(s1);
43 paint.setColor(s1 ? SK_ColorBLACK : SkColorSetA(kColor1, 0x80));
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000044 canvas->drawOval(r, paint);
fmalita@google.coma17093f2013-08-23 19:55:15 +000045 if (!s1) {
46 canvas->save();
47 canvas->clipRect(c);
48 paint.setColor(s1 ? SK_ColorBLACK : kColor1);
49 canvas->drawOval(r, paint);
50 canvas->restore();
51 }
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000052
53 SkPaint xferPaint;
54 xferPaint.setXfermode(mode);
55 canvas->saveLayer(&bounds, &xferPaint);
56
57 r = bounds;
fmalita@google.coma17093f2013-08-23 19:55:15 +000058 r.inset(0, kInset);
59 paint.setShader(s2);
60 paint.setColor(s2 ? SK_ColorBLACK : SkColorSetA(kColor2, 0x80));
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000061 canvas->drawOval(r, paint);
fmalita@google.coma17093f2013-08-23 19:55:15 +000062 if (!s2) {
63 canvas->save();
64 canvas->clipRect(c);
65 paint.setColor(s2 ? SK_ColorBLACK : kColor2);
66 canvas->drawOval(r, paint);
67 canvas->restore();
68 }
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000069
70 canvas->restore();
71 canvas->restore();
72}
73
74class LumaXfermodeGM : public skiagm::GM {
75public:
fmalita@google.coma17093f2013-08-23 19:55:15 +000076 LumaXfermodeGM() {
77 fSrcInXfer.reset(SkLumaMaskXfermode::Create(SkXfermode::kSrcIn_Mode));
78 fDstInXfer.reset(SkLumaMaskXfermode::Create(SkXfermode::kDstIn_Mode));
fmalita@google.comef45a642013-09-17 13:09:16 +000079 fSrcOverXfer.reset(SkLumaMaskXfermode::Create(SkXfermode::kSrcOver_Mode));
fmalita@google.coma17093f2013-08-23 19:55:15 +000080
81 SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) };
82 SkColor g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) };
83 SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } };
84 SkPoint g2Points[] = { { 0, 0 }, { kSize, 0 } };
85 SkScalar pos[] = { 0.2f, 1.0f };
86
87 fGr1.reset(SkGradientShader::CreateLinear(g1Points,
88 g1Colors,
89 pos,
90 SK_ARRAY_COUNT(g1Colors),
91 SkShader::kClamp_TileMode));
92 fGr2.reset(SkGradientShader::CreateLinear(g2Points,
93 g2Colors,
94 pos,
95 SK_ARRAY_COUNT(g2Colors),
96 SkShader::kClamp_TileMode));
97 }
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000098
99protected:
100 virtual SkString onShortName() SK_OVERRIDE {
101 return SkString("lumamode");
102 }
103
104 virtual SkISize onISize() SK_OVERRIDE {
fmalita@google.comef45a642013-09-17 13:09:16 +0000105 return SkISize::Make(800, 420);
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +0000106 }
107
108 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
fmalita@google.comef45a642013-09-17 13:09:16 +0000109 SkXfermode* modes[] = { NULL, fSrcInXfer, fDstInXfer, fSrcOverXfer };
fmalita@google.coma17093f2013-08-23 19:55:15 +0000110 struct {
111 SkShader* fShader1;
112 SkShader* fShader2;
113 } shaders[] = {
114 { NULL, NULL },
115 { NULL, fGr2 },
116 { fGr1, NULL },
117 { fGr1, fGr2 },
118 };
119
120 SkScalar gridStep = kSize + 2 * kInset;
121 draw_label(canvas, "SrcOver", SkPoint::Make(gridStep, 20));
122 draw_label(canvas, "SrcInLuma", SkPoint::Make(gridStep * 3, 20));
123 draw_label(canvas, "DstInLuma", SkPoint::Make(gridStep * 5, 20));
fmalita@google.comef45a642013-09-17 13:09:16 +0000124 draw_label(canvas, "SrcOverLuma", SkPoint::Make(gridStep * 7, 20));
fmalita@google.coma17093f2013-08-23 19:55:15 +0000125 for (size_t i = 0; i < SK_ARRAY_COUNT(shaders); ++i) {
126 canvas->save();
127 canvas->translate(kInset, gridStep * i + 30);
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(gridStep, 0);
132 draw_scene(canvas, modes[m], false, shaders[i].fShader1,
133 shaders[i].fShader2);
134 canvas->translate(gridStep, 0);
135 }
136 canvas->restore();
137 }
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +0000138 }
139
140private:
fmalita@google.coma17093f2013-08-23 19:55:15 +0000141 SkAutoTUnref<SkShader> fGr1, fGr2;
fmalita@google.comef45a642013-09-17 13:09:16 +0000142 SkAutoTUnref<SkXfermode> fSrcInXfer, fDstInXfer, fSrcOverXfer;
fmalita@google.coma17093f2013-08-23 19:55:15 +0000143
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +0000144 typedef skiagm::GM INHERITED;
145};
146
147//////////////////////////////////////////////////////////////////////////////
148
149DEF_GM( return SkNEW(LumaXfermodeGM); )