aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/hairmodes.cpp
blob: a628f4aafed75f2b1b7d730d112fa24eb32a795c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gm.h"
#include "SkCanvas.h"
#include "SkColorPriv.h"
#include "SkShader.h"

constexpr struct {
    SkBlendMode fMode;
    const char* fLabel;
} gModes[] = {
    { SkBlendMode::kClear,    "Clear"     },
    { SkBlendMode::kSrc,      "Src"       },
    { SkBlendMode::kDst,      "Dst"       },
    { SkBlendMode::kSrcOver,  "SrcOver"   },
    { SkBlendMode::kDstOver,  "DstOver"   },
    { SkBlendMode::kSrcIn,    "SrcIn"     },
    { SkBlendMode::kDstIn,    "DstIn"     },
    { SkBlendMode::kSrcOut,   "SrcOut"    },
    { SkBlendMode::kDstOut,   "DstOut"    },
    { SkBlendMode::kSrcATop,  "SrcATop"   },
    { SkBlendMode::kDstATop,  "DstATop"   },
    { SkBlendMode::kXor,      "Xor"       },
};

const int gWidth = 64;
const int gHeight = 64;
const SkScalar W = SkIntToScalar(gWidth);
const SkScalar H = SkIntToScalar(gHeight);

static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) {

    SkPaint paint;
    paint.setAntiAlias(true);

    SkRect r = SkRect::MakeWH(W, H);
    r.inset(W/10, H/10);

    paint.setColor(SK_ColorBLUE);
    paint.setAlpha(a0);
    canvas->drawOval(r, paint);

    paint.setColor(SK_ColorRED);
    paint.setAlpha(a1);
    paint.setBlendMode(mode);
    for (int angle = 0; angle < 24; ++angle) {
        SkScalar x = SkScalarCos(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gWidth;
        SkScalar y = SkScalarSin(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gHeight;
        paint.setStrokeWidth(SK_Scalar1 * angle * 2 / 24);
        canvas->drawLine(W/2, H/2, W/2 + x, H/2 + y, paint);
    }

    return H;
}

static sk_sp<SkShader> make_bg_shader() {
    SkBitmap bm;
    bm.allocN32Pixels(2, 2);
    *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
    *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCE, 0xCF, 0xCE);

    SkMatrix m;
    m.setScale(SkIntToScalar(6), SkIntToScalar(6));
    return SkShader::MakeBitmapShader(bm,
                                      SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &m);
}

namespace skiagm {

    class HairModesGM : public GM {
        SkPaint fBGPaint;

    protected:
        SkString onShortName() override {
            return SkString("hairmodes");
        }

        virtual SkISize onISize() override { return SkISize::Make(640, 480); }

        void onOnceBeforeDraw() override {
            fBGPaint.setShader(make_bg_shader());
        }

        void onDraw(SkCanvas* canvas) override {
            const SkRect bounds = SkRect::MakeWH(W, H);
            constexpr SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };

            canvas->translate(SkIntToScalar(4), SkIntToScalar(4));

            for (int alpha = 0; alpha < 4; ++alpha) {
                canvas->save();
                canvas->save();
                for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
                    if (6 == i) {
                        canvas->restore();
                        canvas->translate(W * 5, 0);
                        canvas->save();
                    }

                    canvas->drawRect(bounds, fBGPaint);
                    canvas->saveLayer(&bounds, nullptr);
                    SkScalar dy = drawCell(canvas, gModes[i].fMode,
                                           gAlphaValue[alpha & 1],
                                           gAlphaValue[alpha & 2]);
                    canvas->restore();

                    canvas->translate(0, dy * 5 / 4);
                }
                canvas->restore();
                canvas->restore();
                canvas->translate(W * 5 / 4, 0);
            }
        }

    private:
        typedef GM INHERITED;
    };

    //////////////////////////////////////////////////////////////////////////////

    static GM* MyFactory(void*) { return new HairModesGM; }
    static GMRegistry reg(MyFactory);

}