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