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