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