reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 "SkColorPriv.h" |
| 11 | #include "SkShader.h" |
| 12 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 13 | constexpr struct { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 14 | SkBlendMode fMode; |
| 15 | const char* fLabel; |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 16 | } gModes[] = { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 17 | { SkBlendMode::kClear, "Clear" }, |
| 18 | { SkBlendMode::kSrc, "Src" }, |
| 19 | { SkBlendMode::kDst, "Dst" }, |
| 20 | { SkBlendMode::kSrcOver, "SrcOver" }, |
| 21 | { SkBlendMode::kDstOver, "DstOver" }, |
| 22 | { SkBlendMode::kSrcIn, "SrcIn" }, |
| 23 | { SkBlendMode::kDstIn, "DstIn" }, |
| 24 | { SkBlendMode::kSrcOut, "SrcOut" }, |
| 25 | { SkBlendMode::kDstOut, "DstOut" }, |
| 26 | { SkBlendMode::kSrcATop, "SrcATop" }, |
| 27 | { SkBlendMode::kDstATop, "DstATop" }, |
| 28 | { SkBlendMode::kXor, "Xor" }, |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | const int gWidth = 64; |
| 32 | const int gHeight = 64; |
| 33 | const SkScalar W = SkIntToScalar(gWidth); |
| 34 | const SkScalar H = SkIntToScalar(gHeight); |
| 35 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 36 | static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) { |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 37 | |
| 38 | SkPaint paint; |
| 39 | paint.setAntiAlias(true); |
| 40 | |
| 41 | SkRect r = SkRect::MakeWH(W, H); |
| 42 | r.inset(W/10, H/10); |
| 43 | |
| 44 | paint.setColor(SK_ColorBLUE); |
| 45 | paint.setAlpha(a0); |
| 46 | canvas->drawOval(r, paint); |
| 47 | |
| 48 | paint.setColor(SK_ColorRED); |
| 49 | paint.setAlpha(a1); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 50 | paint.setBlendMode(mode); |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 51 | for (int angle = 0; angle < 24; ++angle) { |
| 52 | SkScalar x = SkScalarCos(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gWidth; |
| 53 | SkScalar y = SkScalarSin(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gHeight; |
| 54 | paint.setStrokeWidth(SK_Scalar1 * angle * 2 / 24); |
| 55 | canvas->drawLine(W/2, H/2, W/2 + x, H/2 + y, paint); |
| 56 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 57 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 58 | return H; |
| 59 | } |
| 60 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 61 | static sk_sp<SkShader> make_bg_shader() { |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 62 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 63 | bm.allocN32Pixels(2, 2); |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 64 | *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF; |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 65 | *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCE, 0xCF, 0xCE); |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 66 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 67 | SkMatrix m; |
| 68 | m.setScale(SkIntToScalar(6), SkIntToScalar(6)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 69 | return SkShader::MakeBitmapShader(bm, |
| 70 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &m); |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | namespace skiagm { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 74 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 75 | class HairModesGM : public GM { |
| 76 | SkPaint fBGPaint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 77 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 78 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 79 | SkString onShortName() override { |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 80 | return SkString("hairmodes"); |
| 81 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 82 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 83 | virtual SkISize onISize() override { return SkISize::Make(640, 480); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 84 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 85 | void onOnceBeforeDraw() override { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 86 | fBGPaint.setShader(make_bg_shader()); |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 87 | } |
| 88 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 89 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 90 | const SkRect bounds = SkRect::MakeWH(W, H); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 91 | constexpr SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 92 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 93 | canvas->translate(SkIntToScalar(4), SkIntToScalar(4)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 94 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 95 | for (int alpha = 0; alpha < 4; ++alpha) { |
| 96 | canvas->save(); |
| 97 | canvas->save(); |
| 98 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) { |
| 99 | if (6 == i) { |
| 100 | canvas->restore(); |
| 101 | canvas->translate(W * 5, 0); |
| 102 | canvas->save(); |
| 103 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 104 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 105 | canvas->drawRect(bounds, fBGPaint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 106 | canvas->saveLayer(&bounds, nullptr); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 107 | SkScalar dy = drawCell(canvas, gModes[i].fMode, |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 108 | gAlphaValue[alpha & 1], |
| 109 | gAlphaValue[alpha & 2]); |
| 110 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 111 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 112 | canvas->translate(0, dy * 5 / 4); |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 113 | } |
| 114 | canvas->restore(); |
| 115 | canvas->restore(); |
| 116 | canvas->translate(W * 5 / 4, 0); |
| 117 | } |
| 118 | } |
reed@google.com | fbc2117 | 2011-09-19 19:08:33 +0000 | [diff] [blame] | 119 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 120 | private: |
| 121 | typedef GM INHERITED; |
| 122 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 123 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 124 | ////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 125 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 126 | static GM* MyFactory(void*) { return new HairModesGM; } |
| 127 | static GMRegistry reg(MyFactory); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 128 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 129 | } |