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" |
| 11 | #include "SkDevice.h" |
| 12 | #include "SkColorPriv.h" |
| 13 | #include "SkShader.h" |
| 14 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 15 | static const struct { |
| 16 | SkXfermode::Mode fMode; |
| 17 | const char* fLabel; |
| 18 | } gModes[] = { |
| 19 | { SkXfermode::kClear_Mode, "Clear" }, |
| 20 | { SkXfermode::kSrc_Mode, "Src" }, |
| 21 | { SkXfermode::kDst_Mode, "Dst" }, |
| 22 | { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| 23 | { SkXfermode::kDstOver_Mode, "DstOver" }, |
| 24 | { SkXfermode::kSrcIn_Mode, "SrcIn" }, |
| 25 | { SkXfermode::kDstIn_Mode, "DstIn" }, |
| 26 | { SkXfermode::kSrcOut_Mode, "SrcOut" }, |
| 27 | { SkXfermode::kDstOut_Mode, "DstOut" }, |
| 28 | { SkXfermode::kSrcATop_Mode, "SrcATop" }, |
| 29 | { SkXfermode::kDstATop_Mode, "DstATop" }, |
| 30 | { SkXfermode::kXor_Mode, "Xor" }, |
| 31 | }; |
| 32 | |
| 33 | const int gWidth = 64; |
| 34 | const int gHeight = 64; |
| 35 | const SkScalar W = SkIntToScalar(gWidth); |
| 36 | const SkScalar H = SkIntToScalar(gHeight); |
| 37 | |
| 38 | static SkScalar drawCell(SkCanvas* canvas, SkXfermode* mode, |
| 39 | SkAlpha a0, SkAlpha a1) { |
| 40 | |
| 41 | SkPaint paint; |
| 42 | paint.setAntiAlias(true); |
| 43 | |
| 44 | SkRect r = SkRect::MakeWH(W, H); |
| 45 | r.inset(W/10, H/10); |
| 46 | |
| 47 | paint.setColor(SK_ColorBLUE); |
| 48 | paint.setAlpha(a0); |
| 49 | canvas->drawOval(r, paint); |
| 50 | |
| 51 | paint.setColor(SK_ColorRED); |
| 52 | paint.setAlpha(a1); |
| 53 | paint.setXfermode(mode); |
| 54 | |
| 55 | SkScalar offset = SK_Scalar1 / 3; |
| 56 | SkRect rect = SkRect::MakeXYWH(W / 4 + offset, |
| 57 | H / 4 + offset, |
| 58 | W / 2, H / 2); |
| 59 | canvas->drawRect(rect, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 60 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 61 | return H; |
| 62 | } |
| 63 | |
| 64 | static SkShader* make_bg_shader() { |
| 65 | SkBitmap bm; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 66 | bm.allocN32Pixels(2, 2); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 67 | *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF; |
| 68 | *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC, |
| 69 | 0xCC, 0xCC); |
| 70 | |
| 71 | SkShader* s = SkShader::CreateBitmapShader(bm, |
| 72 | SkShader::kRepeat_TileMode, |
| 73 | SkShader::kRepeat_TileMode); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 74 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 75 | SkMatrix m; |
| 76 | m.setScale(SkIntToScalar(6), SkIntToScalar(6)); |
| 77 | s->setLocalMatrix(m); |
| 78 | return s; |
| 79 | } |
| 80 | |
| 81 | class AARectsModesView : public SampleView { |
| 82 | SkPaint fBGPaint; |
| 83 | public: |
| 84 | AARectsModesView () { |
| 85 | fBGPaint.setShader(make_bg_shader())->unref(); |
| 86 | } |
| 87 | |
| 88 | protected: |
| 89 | // overrides from SkEventSink |
| 90 | virtual bool onQuery(SkEvent* evt) { |
| 91 | if (SampleCode::TitleQ(*evt)) { |
| 92 | SampleCode::TitleR(evt, "AARectsModes"); |
| 93 | return true; |
| 94 | } |
| 95 | return this->INHERITED::onQuery(evt); |
| 96 | } |
| 97 | |
| 98 | virtual void onDrawContent(SkCanvas* canvas) { |
| 99 | const SkRect bounds = SkRect::MakeWH(W, H); |
| 100 | static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 }; |
| 101 | |
| 102 | canvas->translate(SkIntToScalar(4), SkIntToScalar(4)); |
| 103 | |
| 104 | for (int alpha = 0; alpha < 4; ++alpha) { |
| 105 | canvas->save(); |
| 106 | canvas->save(); |
| 107 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) { |
| 108 | if (6 == i) { |
| 109 | canvas->restore(); |
| 110 | canvas->translate(W * 5, 0); |
| 111 | canvas->save(); |
| 112 | } |
| 113 | SkXfermode* mode = SkXfermode::Create(gModes[i].fMode); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 114 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 115 | canvas->drawRect(bounds, fBGPaint); |
| 116 | canvas->saveLayer(&bounds, NULL); |
| 117 | SkScalar dy = drawCell(canvas, mode, |
| 118 | gAlphaValue[alpha & 1], |
| 119 | gAlphaValue[alpha & 2]); |
| 120 | canvas->restore(); |
| 121 | |
| 122 | canvas->translate(0, dy * 5 / 4); |
| 123 | SkSafeUnref(mode); |
| 124 | } |
| 125 | canvas->restore(); |
| 126 | canvas->restore(); |
| 127 | canvas->translate(W * 5 / 4, 0); |
| 128 | } |
| 129 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 130 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 131 | private: |
| 132 | typedef SampleView INHERITED; |
| 133 | }; |
| 134 | |
| 135 | /////////////////////////////////////////////////////////////////////////////// |
| 136 | |
| 137 | static SkView* MyFactory() { return new AARectsModesView; } |
| 138 | static SkViewRegister reg(MyFactory); |