SampleHairModes.cpp revision 2cbcd12281ee807214df094964c584c78932e10b
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 "SkColorPriv.h"
12#include "SkShader.h"
13
14static 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
32const int gWidth = 64;
33const int gHeight = 64;
34const SkScalar W = SkIntToScalar(gWidth);
35const SkScalar H = SkIntToScalar(gHeight);
36
37static SkScalar drawCell(SkCanvas* canvas, sk_sp<SkXfermode> mode, SkAlpha a0, SkAlpha a1) {
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    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    }
57
58    return H;
59}
60
61static sk_sp<SkShader> make_bg_shader() {
62    SkBitmap bm;
63    bm.allocN32Pixels(2, 2);
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
67    SkMatrix m;
68    m.setScale(SkIntToScalar(6), SkIntToScalar(6));
69
70    return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode,
71                                      SkShader::kRepeat_TileMode, &m);
72}
73
74class HairModesView : public SampleView {
75    SkPaint fBGPaint;
76public:
77    HairModesView() {
78        fBGPaint.setShader(make_bg_shader());
79    }
80
81protected:
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                }
106                canvas->drawRect(bounds, fBGPaint);
107                canvas->saveLayer(&bounds, nullptr);
108                SkScalar dy = drawCell(canvas, SkXfermode::Make(gModes[i].fMode),
109                                       gAlphaValue[alpha & 1],
110                                       gAlphaValue[alpha & 2]);
111                canvas->restore();
112
113                canvas->translate(0, dy * 5 / 4);
114            }
115            canvas->restore();
116            canvas->restore();
117            canvas->translate(W * 5 / 4, 0);
118        }
119    }
120
121private:
122    typedef SampleView INHERITED;
123};
124
125///////////////////////////////////////////////////////////////////////////////
126
127static SkView* MyFactory() { return new HairModesView; }
128static SkViewRegister reg(MyFactory);
129