bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +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" |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
| 12 | #include "SkShader.h" |
| 13 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 14 | static const struct { |
| 15 | SkXfermode::Mode fMode; |
| 16 | const char* fLabel; |
| 17 | } gModes[] = { |
| 18 | { SkXfermode::kClear_Mode, "Clear" }, |
| 19 | { SkXfermode::kSrc_Mode, "Src" }, |
| 20 | { SkXfermode::kDst_Mode, "Dst" }, |
| 21 | { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| 22 | { SkXfermode::kDstOver_Mode, "DstOver" }, |
| 23 | { SkXfermode::kSrcIn_Mode, "SrcIn" }, |
| 24 | { SkXfermode::kDstIn_Mode, "DstIn" }, |
| 25 | { SkXfermode::kSrcOut_Mode, "SrcOut" }, |
| 26 | { SkXfermode::kDstOut_Mode, "DstOut" }, |
| 27 | { SkXfermode::kSrcATop_Mode, "SrcATop" }, |
| 28 | { SkXfermode::kDstATop_Mode, "DstATop" }, |
| 29 | { SkXfermode::kXor_Mode, "Xor" }, |
| 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 | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 37 | static SkScalar drawCell(SkCanvas* canvas, const sk_sp<SkXfermode>& mode, SkAlpha a0, SkAlpha a1) { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +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); |
| 50 | paint.setXfermode(mode); |
| 51 | |
| 52 | SkScalar offset = SK_Scalar1 / 3; |
| 53 | SkRect rect = SkRect::MakeXYWH(W / 4 + offset, |
| 54 | H / 4 + offset, |
| 55 | W / 2, H / 2); |
| 56 | canvas->drawRect(rect, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 57 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +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() { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +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); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +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, |
| 66 | 0xCC, 0xCC); |
| 67 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 68 | SkMatrix m; |
| 69 | m.setScale(SkIntToScalar(6), SkIntToScalar(6)); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 70 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 71 | return SkShader::MakeBitmapShader(bm, |
| 72 | SkShader::kRepeat_TileMode, |
| 73 | SkShader::kRepeat_TileMode, |
| 74 | &m); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | class AARectsModesView : public SampleView { |
| 78 | SkPaint fBGPaint; |
| 79 | public: |
| 80 | AARectsModesView () { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 81 | fBGPaint.setShader(make_bg_shader()); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | protected: |
| 85 | // overrides from SkEventSink |
| 86 | virtual bool onQuery(SkEvent* evt) { |
| 87 | if (SampleCode::TitleQ(*evt)) { |
| 88 | SampleCode::TitleR(evt, "AARectsModes"); |
| 89 | return true; |
| 90 | } |
| 91 | return this->INHERITED::onQuery(evt); |
| 92 | } |
| 93 | |
| 94 | virtual void onDrawContent(SkCanvas* canvas) { |
| 95 | const SkRect bounds = SkRect::MakeWH(W, H); |
| 96 | static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 }; |
| 97 | |
| 98 | canvas->translate(SkIntToScalar(4), SkIntToScalar(4)); |
| 99 | |
| 100 | for (int alpha = 0; alpha < 4; ++alpha) { |
| 101 | canvas->save(); |
| 102 | canvas->save(); |
| 103 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) { |
| 104 | if (6 == i) { |
| 105 | canvas->restore(); |
| 106 | canvas->translate(W * 5, 0); |
| 107 | canvas->save(); |
| 108 | } |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 109 | sk_sp<SkXfermode> mode = SkXfermode::Make(gModes[i].fMode); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 110 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 111 | canvas->drawRect(bounds, fBGPaint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 112 | canvas->saveLayer(&bounds, nullptr); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 113 | SkScalar dy = drawCell(canvas, mode, gAlphaValue[alpha & 1], |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 114 | gAlphaValue[alpha & 2]); |
| 115 | canvas->restore(); |
| 116 | |
| 117 | canvas->translate(0, dy * 5 / 4); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 118 | } |
| 119 | canvas->restore(); |
| 120 | canvas->restore(); |
| 121 | canvas->translate(W * 5 / 4, 0); |
| 122 | } |
| 123 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 124 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 125 | private: |
| 126 | typedef SampleView INHERITED; |
| 127 | }; |
| 128 | |
| 129 | /////////////////////////////////////////////////////////////////////////////// |
| 130 | |
| 131 | static SkView* MyFactory() { return new AARectsModesView; } |
| 132 | static SkViewRegister reg(MyFactory); |