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 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 8 | #include "Sample.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 9 | #include "SkBitmap.h" |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 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 | |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 14 | static const SkBlendMode gModes[] = { |
| 15 | SkBlendMode::kClear, |
| 16 | SkBlendMode::kSrc, |
| 17 | SkBlendMode::kDst, |
| 18 | SkBlendMode::kSrcOver, |
| 19 | SkBlendMode::kDstOver, |
| 20 | SkBlendMode::kSrcIn, |
| 21 | SkBlendMode::kDstIn, |
| 22 | SkBlendMode::kSrcOut, |
| 23 | SkBlendMode::kDstOut, |
| 24 | SkBlendMode::kSrcATop, |
| 25 | SkBlendMode::kDstATop, |
| 26 | SkBlendMode::kXor, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | const int gWidth = 64; |
| 30 | const int gHeight = 64; |
| 31 | const SkScalar W = SkIntToScalar(gWidth); |
| 32 | const SkScalar H = SkIntToScalar(gHeight); |
| 33 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 34 | static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 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); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 48 | |
| 49 | SkScalar offset = SK_Scalar1 / 3; |
| 50 | SkRect rect = SkRect::MakeXYWH(W / 4 + offset, |
| 51 | H / 4 + offset, |
| 52 | W / 2, H / 2); |
| 53 | canvas->drawRect(rect, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 54 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 55 | return H; |
| 56 | } |
| 57 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 58 | static sk_sp<SkShader> make_bg_shader() { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 59 | SkBitmap bm; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 60 | bm.allocN32Pixels(2, 2); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 61 | *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF; |
| 62 | *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC, |
| 63 | 0xCC, 0xCC); |
| 64 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 65 | SkMatrix m; |
| 66 | m.setScale(SkIntToScalar(6), SkIntToScalar(6)); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 67 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 68 | return SkShader::MakeBitmapShader(bm, SkTileMode::kRepeat, SkTileMode::kRepeat, &m); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 71 | class AARectsModesView : public Sample { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 72 | SkPaint fBGPaint; |
| 73 | public: |
| 74 | AARectsModesView () { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 75 | fBGPaint.setShader(make_bg_shader()); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 79 | virtual bool onQuery(Sample::Event* evt) { |
| 80 | if (Sample::TitleQ(*evt)) { |
| 81 | Sample::TitleR(evt, "AARectsModes"); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | return this->INHERITED::onQuery(evt); |
| 85 | } |
| 86 | |
| 87 | virtual void onDrawContent(SkCanvas* canvas) { |
| 88 | const SkRect bounds = SkRect::MakeWH(W, H); |
| 89 | static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 }; |
| 90 | |
| 91 | canvas->translate(SkIntToScalar(4), SkIntToScalar(4)); |
| 92 | |
| 93 | for (int alpha = 0; alpha < 4; ++alpha) { |
| 94 | canvas->save(); |
| 95 | canvas->save(); |
| 96 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) { |
| 97 | if (6 == i) { |
| 98 | canvas->restore(); |
| 99 | canvas->translate(W * 5, 0); |
| 100 | canvas->save(); |
| 101 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 102 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 103 | canvas->drawRect(bounds, fBGPaint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 104 | canvas->saveLayer(&bounds, nullptr); |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 105 | SkScalar dy = drawCell(canvas, gModes[i], gAlphaValue[alpha & 1], |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 106 | gAlphaValue[alpha & 2]); |
| 107 | canvas->restore(); |
| 108 | |
| 109 | canvas->translate(0, dy * 5 / 4); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 110 | } |
| 111 | canvas->restore(); |
| 112 | canvas->restore(); |
| 113 | canvas->translate(W * 5 / 4, 0); |
| 114 | } |
| 115 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 116 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 117 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 118 | typedef Sample INHERITED; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | /////////////////////////////////////////////////////////////////////////////// |
| 122 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 123 | DEF_SAMPLE( return new AARectsModesView(); ) |