commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 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 "SkCanvas.h" |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 10 | #include "SkGradientShader.h" |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 11 | #include "SkLumaXfermode.h" |
| 12 | |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 13 | static SkScalar kSize = 80; |
| 14 | static SkScalar 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) { |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 20 | SkPaint paint; |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 21 | 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 | |
| 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); |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 33 | |
| 34 | c = bounds; |
| 35 | c.fRight = bounds.centerX(); |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 36 | canvas->drawRect(bounds, paint); |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 37 | |
| 38 | canvas->saveLayer(&bounds, NULL); |
| 39 | |
| 40 | r = bounds; |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 41 | r.inset(kInset, 0); |
| 42 | paint.setShader(s1); |
| 43 | paint.setColor(s1 ? SK_ColorBLACK : SkColorSetA(kColor1, 0x80)); |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 44 | canvas->drawOval(r, paint); |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 45 | 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.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 52 | |
| 53 | SkPaint xferPaint; |
| 54 | xferPaint.setXfermode(mode); |
| 55 | canvas->saveLayer(&bounds, &xferPaint); |
| 56 | |
| 57 | r = bounds; |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 58 | r.inset(0, kInset); |
| 59 | paint.setShader(s2); |
| 60 | paint.setColor(s2 ? SK_ColorBLACK : SkColorSetA(kColor2, 0x80)); |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 61 | canvas->drawOval(r, paint); |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 62 | 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.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 69 | |
| 70 | canvas->restore(); |
| 71 | canvas->restore(); |
| 72 | } |
| 73 | |
| 74 | class LumaXfermodeGM : public skiagm::GM { |
| 75 | public: |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 76 | LumaXfermodeGM() { |
| 77 | fSrcInXfer.reset(SkLumaMaskXfermode::Create(SkXfermode::kSrcIn_Mode)); |
| 78 | fDstInXfer.reset(SkLumaMaskXfermode::Create(SkXfermode::kDstIn_Mode)); |
fmalita@google.com | ef45a64 | 2013-09-17 13:09:16 +0000 | [diff] [blame] | 79 | fSrcOverXfer.reset(SkLumaMaskXfermode::Create(SkXfermode::kSrcOver_Mode)); |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 80 | |
| 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.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 98 | |
| 99 | protected: |
| 100 | virtual SkString onShortName() SK_OVERRIDE { |
| 101 | return SkString("lumamode"); |
| 102 | } |
| 103 | |
| 104 | virtual SkISize onISize() SK_OVERRIDE { |
fmalita@google.com | ef45a64 | 2013-09-17 13:09:16 +0000 | [diff] [blame] | 105 | return SkISize::Make(800, 420); |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
fmalita@google.com | ef45a64 | 2013-09-17 13:09:16 +0000 | [diff] [blame] | 109 | SkXfermode* modes[] = { NULL, fSrcInXfer, fDstInXfer, fSrcOverXfer }; |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 110 | 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.com | ef45a64 | 2013-09-17 13:09:16 +0000 | [diff] [blame] | 124 | draw_label(canvas, "SrcOverLuma", SkPoint::Make(gridStep * 7, 20)); |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 125 | 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.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | private: |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 141 | SkAutoTUnref<SkShader> fGr1, fGr2; |
fmalita@google.com | ef45a64 | 2013-09-17 13:09:16 +0000 | [diff] [blame] | 142 | SkAutoTUnref<SkXfermode> fSrcInXfer, fDstInXfer, fSrcOverXfer; |
fmalita@google.com | a17093f | 2013-08-23 19:55:15 +0000 | [diff] [blame] | 143 | |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 144 | typedef skiagm::GM INHERITED; |
| 145 | }; |
| 146 | |
| 147 | ////////////////////////////////////////////////////////////////////////////// |
| 148 | |
| 149 | DEF_GM( return SkNEW(LumaXfermodeGM); ) |