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