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 "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorPriv.h"
14 #include "include/core/SkMatrix.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkTileMode.h"
23 #include "include/core/SkTypes.h"
24
25 constexpr SkBlendMode gModes[] = {
26 SkBlendMode::kClear,
27 SkBlendMode::kSrc,
28 SkBlendMode::kDst,
29 SkBlendMode::kSrcOver,
30 SkBlendMode::kDstOver,
31 SkBlendMode::kSrcIn,
32 SkBlendMode::kDstIn,
33 SkBlendMode::kSrcOut,
34 SkBlendMode::kDstOut,
35 SkBlendMode::kSrcATop,
36 SkBlendMode::kDstATop,
37 SkBlendMode::kXor,
38 };
39
40 const int gWidth = 64;
41 const int gHeight = 64;
42 const SkScalar W = SkIntToScalar(gWidth);
43 const SkScalar H = SkIntToScalar(gHeight);
44
drawCell(SkCanvas * canvas,SkBlendMode mode,SkAlpha a0,SkAlpha a1)45 static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) {
46
47 SkPaint paint;
48 paint.setAntiAlias(true);
49
50 SkRect r = SkRect::MakeWH(W, H);
51 r.inset(W/10, H/10);
52
53 paint.setColor(SK_ColorBLUE);
54 paint.setAlpha(a0);
55 canvas->drawOval(r, paint);
56
57 paint.setColor(SK_ColorRED);
58 paint.setAlpha(a1);
59 paint.setBlendMode(mode);
60 for (int angle = 0; angle < 24; ++angle) {
61 SkScalar x = SkScalarCos(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gWidth;
62 SkScalar y = SkScalarSin(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gHeight;
63 paint.setStrokeWidth(SK_Scalar1 * angle * 2 / 24);
64 canvas->drawLine(W/2, H/2, W/2 + x, H/2 + y, paint);
65 }
66
67 return H;
68 }
69
make_bg_shader()70 static sk_sp<SkShader> make_bg_shader() {
71 SkBitmap bm;
72 bm.allocN32Pixels(2, 2);
73 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
74 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCE, 0xCF, 0xCE);
75
76 SkMatrix m;
77 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
78 return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
79 }
80
81 namespace skiagm {
82
83 class HairModesGM : public GM {
84 SkPaint fBGPaint;
85
86 protected:
onShortName()87 SkString onShortName() override {
88 return SkString("hairmodes");
89 }
90
onISize()91 virtual SkISize onISize() override { return SkISize::Make(640, 480); }
92
onOnceBeforeDraw()93 void onOnceBeforeDraw() override {
94 fBGPaint.setShader(make_bg_shader());
95 }
96
onDraw(SkCanvas * canvas)97 void onDraw(SkCanvas* canvas) override {
98 const SkRect bounds = SkRect::MakeWH(W, H);
99 constexpr SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
100
101 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
102
103 for (int alpha = 0; alpha < 4; ++alpha) {
104 canvas->save();
105 canvas->save();
106 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
107 if (6 == i) {
108 canvas->restore();
109 canvas->translate(W * 5, 0);
110 canvas->save();
111 }
112
113 canvas->drawRect(bounds, fBGPaint);
114 canvas->saveLayer(&bounds, nullptr);
115 SkScalar dy = drawCell(canvas, gModes[i],
116 gAlphaValue[alpha & 1],
117 gAlphaValue[alpha & 2]);
118 canvas->restore();
119
120 canvas->translate(0, dy * 5 / 4);
121 }
122 canvas->restore();
123 canvas->restore();
124 canvas->translate(W * 5 / 4, 0);
125 }
126 }
127
128 private:
129 typedef GM INHERITED;
130 };
131
132 //////////////////////////////////////////////////////////////////////////////
133
134 DEF_GM( return new HairModesGM; )
135 }
136