reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +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 "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
| 12 | #include "SkShader.h" |
| 13 | |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 14 | static const struct { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 15 | SkBlendMode fMode; |
| 16 | const char* fLabel; |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 17 | } gModes[] = { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 18 | { SkBlendMode::kClear, "Clear" }, |
| 19 | { SkBlendMode::kSrc, "Src" }, |
| 20 | { SkBlendMode::kDst, "Dst" }, |
| 21 | { SkBlendMode::kSrcOver, "SrcOver" }, |
| 22 | { SkBlendMode::kDstOver, "DstOver" }, |
| 23 | { SkBlendMode::kSrcIn, "SrcIn" }, |
| 24 | { SkBlendMode::kDstIn, "DstIn" }, |
| 25 | { SkBlendMode::kSrcOut, "SrcOut" }, |
| 26 | { SkBlendMode::kDstOut, "DstOut" }, |
| 27 | { SkBlendMode::kSrcATop, "SrcATop" }, |
| 28 | { SkBlendMode::kDstATop, "DstATop" }, |
| 29 | { SkBlendMode::kXor, "Xor" }, |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | const int gWidth = 64; |
| 33 | const int gHeight = 64; |
| 34 | const SkScalar W = SkIntToScalar(gWidth); |
| 35 | const SkScalar H = SkIntToScalar(gHeight); |
| 36 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 37 | static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) { |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 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 | 1ec27a0 | 2011-09-08 13:36:30 +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 | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 58 | return H; |
| 59 | } |
| 60 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 61 | static sk_sp<SkShader> make_bg_shader() { |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 62 | SkBitmap bm; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 63 | bm.allocN32Pixels(2, 2); |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 64 | *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF; |
| 65 | *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC, 0xCC, 0xCC); |
| 66 | |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 67 | SkMatrix m; |
| 68 | m.setScale(SkIntToScalar(6), SkIntToScalar(6)); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 69 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 70 | return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode, |
| 71 | SkShader::kRepeat_TileMode, &m); |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | class HairModesView : public SampleView { |
| 75 | SkPaint fBGPaint; |
| 76 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 77 | HairModesView() { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 78 | fBGPaint.setShader(make_bg_shader()); |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | protected: |
| 82 | // overrides from SkEventSink |
| 83 | virtual bool onQuery(SkEvent* evt) { |
| 84 | if (SampleCode::TitleQ(*evt)) { |
| 85 | SampleCode::TitleR(evt, "HairlineModes"); |
| 86 | return true; |
| 87 | } |
| 88 | return this->INHERITED::onQuery(evt); |
| 89 | } |
| 90 | |
| 91 | virtual void onDrawContent(SkCanvas* canvas) { |
| 92 | const SkRect bounds = SkRect::MakeWH(W, H); |
| 93 | static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 }; |
| 94 | |
| 95 | canvas->translate(SkIntToScalar(4), SkIntToScalar(4)); |
| 96 | |
| 97 | for (int alpha = 0; alpha < 4; ++alpha) { |
| 98 | canvas->save(); |
| 99 | canvas->save(); |
| 100 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) { |
| 101 | if (6 == i) { |
| 102 | canvas->restore(); |
| 103 | canvas->translate(W * 5, 0); |
| 104 | canvas->save(); |
| 105 | } |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 106 | canvas->drawRect(bounds, fBGPaint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 107 | canvas->saveLayer(&bounds, nullptr); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 108 | SkScalar dy = drawCell(canvas, gModes[i].fMode, |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 109 | gAlphaValue[alpha & 1], |
| 110 | gAlphaValue[alpha & 2]); |
| 111 | canvas->restore(); |
| 112 | |
| 113 | canvas->translate(0, dy * 5 / 4); |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 114 | } |
| 115 | canvas->restore(); |
| 116 | canvas->restore(); |
| 117 | canvas->translate(W * 5 / 4, 0); |
| 118 | } |
| 119 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 120 | |
reed@google.com | 1ec27a0 | 2011-09-08 13:36:30 +0000 | [diff] [blame] | 121 | private: |
| 122 | typedef SampleView INHERITED; |
| 123 | }; |
| 124 | |
| 125 | /////////////////////////////////////////////////////////////////////////////// |
| 126 | |
| 127 | static SkView* MyFactory() { return new HairModesView; } |
| 128 | static SkViewRegister reg(MyFactory); |