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