OLD | NEW |
(Empty) | |
| 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, 0x80, 0xff, 0); |
| 17 |
| 18 static void draw_label(SkCanvas* canvas, const char* label, |
| 19 const SkPoint& offset) { |
| 20 SkPaint paint; |
| 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, SkColorFilter* filter, |
| 29 SkXfermode::Mode mode, SkShader* s1, |
| 30 SkShader* s2) { |
| 31 SkPaint paint; |
| 32 paint.setAntiAlias(true); |
| 33 SkRect r, c, bounds = SkRect::MakeWH(kSize, kSize); |
| 34 |
| 35 c = bounds; |
| 36 c.fRight = bounds.centerX(); |
| 37 canvas->drawRect(bounds, paint); |
| 38 |
| 39 canvas->saveLayer(&bounds, NULL); |
| 40 |
| 41 r = bounds; |
| 42 r.inset(kInset, 0); |
| 43 paint.setShader(s1); |
| 44 paint.setColor(s1 ? SK_ColorBLACK : SkColorSetA(kColor1, 0x80)); |
| 45 canvas->drawOval(r, paint); |
| 46 if (!s1) { |
| 47 canvas->save(); |
| 48 canvas->clipRect(c); |
| 49 paint.setColor(kColor1); |
| 50 canvas->drawOval(r, paint); |
| 51 canvas->restore(); |
| 52 } |
| 53 |
| 54 SkPaint xferPaint; |
| 55 xferPaint.setXfermodeMode(mode); |
| 56 canvas->saveLayer(&bounds, &xferPaint); |
| 57 |
| 58 r = bounds; |
| 59 r.inset(0, kInset); |
| 60 paint.setShader(s2); |
| 61 paint.setColor(s2 ? SK_ColorBLACK : SkColorSetA(kColor2, 0x80)); |
| 62 paint.setColorFilter(filter); |
| 63 canvas->drawOval(r, paint); |
| 64 if (!s2) { |
| 65 canvas->save(); |
| 66 canvas->clipRect(c); |
| 67 paint.setColor(kColor2); |
| 68 canvas->drawOval(r, paint); |
| 69 canvas->restore(); |
| 70 } |
| 71 |
| 72 canvas->restore(); |
| 73 canvas->restore(); |
| 74 } |
| 75 |
| 76 class LumaFilterGM : public skiagm::GM { |
| 77 public: |
| 78 LumaFilterGM() { |
| 79 SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) }; |
| 80 SkColor g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) }; |
| 81 SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } }; |
| 82 SkPoint g2Points[] = { { 0, 0 }, { kSize, 0 } }; |
| 83 SkScalar pos[] = { 0.2f, 1.0f }; |
| 84 |
| 85 fFilter.reset(SkLumaColorFilter::Create()); |
| 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 } |
| 97 |
| 98 protected: |
| 99 virtual SkString onShortName() SK_OVERRIDE { |
| 100 return SkString("lumafilter"); |
| 101 } |
| 102 |
| 103 virtual SkISize onISize() SK_OVERRIDE { |
| 104 return SkISize::Make(600, 420); |
| 105 } |
| 106 |
| 107 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 108 SkXfermode::Mode modes[] = { SkXfermode::kSrcOver_Mode, |
| 109 SkXfermode::kDstOver_Mode, |
| 110 SkXfermode::kSrcATop_Mode, |
| 111 SkXfermode::kDstATop_Mode, |
| 112 SkXfermode::kSrcIn_Mode, |
| 113 SkXfermode::kDstIn_Mode, |
| 114 }; |
| 115 struct { |
| 116 SkShader* fShader1; |
| 117 SkShader* fShader2; |
| 118 } shaders[] = { |
| 119 { NULL, NULL }, |
| 120 { NULL, fGr2 }, |
| 121 { fGr1, NULL }, |
| 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, SkXfermode::ModeName(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 SkAutoTUnref<SkColorFilter> fFilter; |
| 145 SkAutoTUnref<SkShader> fGr1, fGr2; |
| 146 |
| 147 typedef skiagm::GM INHERITED; |
| 148 }; |
| 149 |
| 150 ////////////////////////////////////////////////////////////////////////////// |
| 151 |
| 152 DEF_GM( return SkNEW(LumaFilterGM); ) |
OLD | NEW |