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 "sk_tool_utils.h" 10 #include "SkBlendModePriv.h" 11 #include "SkCanvas.h" 12 #include "SkGradientShader.h" 13 #include "SkLumaColorFilter.h" 14 15 static SkScalar kSize = 80; 16 static SkScalar kInset = 10; 17 static SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0); 18 static SkColor kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0); 19 20 static void draw_label(SkCanvas* canvas, const char* label, 21 const SkPoint& offset) { 22 SkFont font(sk_tool_utils::create_portable_typeface()); 23 font.setEdging(SkFont::Edging::kAlias); 24 25 size_t len = strlen(label); 26 27 SkScalar width = font.measureText(label, len, kUTF8_SkTextEncoding); 28 canvas->drawSimpleText(label, len, kUTF8_SkTextEncoding, offset.x() - width / 2, offset.y(), 29 font, SkPaint()); 30 } 31 32 static void draw_scene(SkCanvas* canvas, const sk_sp<SkColorFilter>& filter, SkBlendMode mode, 33 const sk_sp<SkShader>& s1, const sk_sp<SkShader>& s2) { 34 SkPaint paint; 35 paint.setAntiAlias(true); 36 SkRect r, c, bounds = SkRect::MakeWH(kSize, kSize); 37 38 c = bounds; 39 c.fRight = bounds.centerX(); 40 paint.setARGB(0x20, 0, 0, 0xff); 41 canvas->drawRect(bounds, paint); 42 43 canvas->saveLayer(&bounds, nullptr); 44 45 r = bounds; 46 r.inset(kInset, 0); 47 paint.setShader(s1); 48 paint.setColor(s1 ? SK_ColorBLACK : SkColorSetA(kColor1, 0x80)); 49 canvas->drawOval(r, paint); 50 if (!s1) { 51 canvas->save(); 52 canvas->clipRect(c); 53 paint.setColor(kColor1); 54 canvas->drawOval(r, paint); 55 canvas->restore(); 56 } 57 58 SkPaint xferPaint; 59 xferPaint.setBlendMode(mode); 60 canvas->saveLayer(&bounds, &xferPaint); 61 62 r = bounds; 63 r.inset(0, kInset); 64 paint.setShader(s2); 65 paint.setColor(s2 ? SK_ColorBLACK : SkColorSetA(kColor2, 0x80)); 66 paint.setColorFilter(filter); 67 canvas->drawOval(r, paint); 68 if (!s2) { 69 canvas->save(); 70 canvas->clipRect(c); 71 paint.setColor(kColor2); 72 canvas->drawOval(r, paint); 73 canvas->restore(); 74 } 75 76 canvas->restore(); 77 canvas->restore(); 78 } 79 80 class LumaFilterGM : public skiagm::GM { 81 public: 82 LumaFilterGM() { 83 SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) }; 84 SkColor g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) }; 85 SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } }; 86 SkPoint g2Points[] = { { 0, 0 }, { kSize, 0 } }; 87 SkScalar pos[] = { 0.2f, 1.0f }; 88 89 fFilter = SkLumaColorFilter::Make(); 90 fGr1 = SkGradientShader::MakeLinear(g1Points, g1Colors, pos, SK_ARRAY_COUNT(g1Colors), 91 SkShader::kClamp_TileMode); 92 fGr2 = SkGradientShader::MakeLinear(g2Points, g2Colors, pos, SK_ARRAY_COUNT(g2Colors), 93 SkShader::kClamp_TileMode); 94 } 95 96 protected: 97 98 SkString onShortName() override { 99 return SkString("lumafilter"); 100 } 101 102 SkISize onISize() override { 103 return SkISize::Make(600, 420); 104 } 105 106 void onDraw(SkCanvas* canvas) override { 107 SkBlendMode modes[] = { 108 SkBlendMode::kSrcOver, 109 SkBlendMode::kDstOver, 110 SkBlendMode::kSrcATop, 111 SkBlendMode::kDstATop, 112 SkBlendMode::kSrcIn, 113 SkBlendMode::kDstIn, 114 }; 115 struct { 116 const sk_sp<SkShader>& fShader1; 117 const sk_sp<SkShader>& fShader2; 118 } shaders[] = { 119 { nullptr, nullptr }, 120 { nullptr, fGr2 }, 121 { fGr1, nullptr }, 122 { fGr1, fGr2 }, 123 }; 124 125 SkScalar gridStep = kSize + 2 * kInset; 126 for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) { 127 draw_label(canvas, SkBlendMode_Name(modes[i]), 128 SkPoint::Make(gridStep * (0.5f + i), 20)); 129 } 130 131 for (size_t i = 0; i < SK_ARRAY_COUNT(shaders); ++i) { 132 canvas->save(); 133 canvas->translate(kInset, gridStep * i + 30); 134 for (size_t m = 0; m < SK_ARRAY_COUNT(modes); ++m) { 135 draw_scene(canvas, fFilter, modes[m], shaders[i].fShader1, 136 shaders[i].fShader2); 137 canvas->translate(gridStep, 0); 138 } 139 canvas->restore(); 140 } 141 } 142 143 private: 144 sk_sp<SkColorFilter> fFilter; 145 sk_sp<SkShader> fGr1, fGr2; 146 147 typedef skiagm::GM INHERITED; 148 }; 149 150 ////////////////////////////////////////////////////////////////////////////// 151 152 DEF_GM(return new LumaFilterGM;) 153