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.h"
9 #include "SkCanvas.h"
10 #include "SkColorPriv.h"
11 #include "SkPath.h"
12 #include "SkShader.h"
13
test4(SkCanvas * canvas)14 static void test4(SkCanvas* canvas) {
15 SkPaint paint;
16 paint.setAntiAlias(true);
17 SkPoint pts[] = {
18 {10, 160}, {610, 160},
19 {610, 160}, {10, 160},
20
21 {610, 160}, {610, 160},
22 {610, 199}, {610, 199},
23
24 {10, 198}, {610, 198},
25 {610, 199}, {10, 199},
26
27 {10, 160}, {10, 160},
28 {10, 199}, {10, 199}
29 };
30 char verbs[] = {
31 0, 1, 1, 1, 4,
32 0, 1, 1, 1, 4,
33 0, 1, 1, 1, 4,
34 0, 1, 1, 1, 4
35 };
36 SkPath path;
37 SkPoint* ptPtr = pts;
38 for (size_t i = 0; i < sizeof(verbs); ++i) {
39 switch ((SkPath::Verb) verbs[i]) {
40 case SkPath::kMove_Verb:
41 path.moveTo(ptPtr->fX, ptPtr->fY);
42 ++ptPtr;
43 break;
44 case SkPath::kLine_Verb:
45 path.lineTo(ptPtr->fX, ptPtr->fY);
46 ++ptPtr;
47 break;
48 case SkPath::kClose_Verb:
49 path.close();
50 break;
51 default:
52 SkASSERT(false);
53 break;
54 }
55 }
56 SkRect clip = {0, 130, 772, 531};
57 canvas->clipRect(clip);
58 canvas->drawPath(path, paint);
59 }
60
61 constexpr SkBlendMode gModes[] = {
62 SkBlendMode::kClear,
63 SkBlendMode::kSrc,
64 SkBlendMode::kDst,
65 SkBlendMode::kSrcOver,
66 SkBlendMode::kDstOver,
67 SkBlendMode::kSrcIn,
68 SkBlendMode::kDstIn,
69 SkBlendMode::kSrcOut,
70 SkBlendMode::kDstOut,
71 SkBlendMode::kSrcATop,
72 SkBlendMode::kDstATop,
73 SkBlendMode::kXor,
74 };
75
76 const int gWidth = 64;
77 const int gHeight = 64;
78 const SkScalar W = SkIntToScalar(gWidth);
79 const SkScalar H = SkIntToScalar(gHeight);
80
drawCell(SkCanvas * canvas,SkBlendMode mode,SkAlpha a0,SkAlpha a1)81 static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) {
82
83 SkPaint paint;
84 paint.setAntiAlias(true);
85
86 SkRect r = SkRect::MakeWH(W, H);
87 r.inset(W/10, H/10);
88
89 paint.setColor(SK_ColorBLUE);
90 paint.setAlpha(a0);
91 canvas->drawOval(r, paint);
92
93 paint.setColor(SK_ColorRED);
94 paint.setAlpha(a1);
95 paint.setBlendMode(mode);
96
97 SkScalar offset = SK_Scalar1 / 3;
98 SkRect rect = SkRect::MakeXYWH(W / 4 + offset,
99 H / 4 + offset,
100 W / 2, H / 2);
101 canvas->drawRect(rect, paint);
102
103 return H;
104 }
105
make_bg_shader()106 static sk_sp<SkShader> make_bg_shader() {
107 SkBitmap bm;
108 bm.allocN32Pixels(2, 2);
109 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
110 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCE,
111 0xCF, 0xCE);
112
113 const SkMatrix m = SkMatrix::MakeScale(SkIntToScalar(6), SkIntToScalar(6));
114 return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode,
115 &m);
116 }
117
118 DEF_SIMPLE_GM(aarectmodes, canvas, 640, 480) {
119 SkPaint bgPaint;
120 bgPaint.setShader(make_bg_shader());
121 if (false) { // avoid bit rot, suppress warning
122 test4(canvas);
123 }
124 const SkRect bounds = SkRect::MakeWH(W, H);
125 constexpr SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
126
127 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
128
129 for (int alpha = 0; alpha < 4; ++alpha) {
130 canvas->save();
131 canvas->save();
132 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
133 if (6 == i) {
134 canvas->restore();
135 canvas->translate(W * 5, 0);
136 canvas->save();
137 }
138 canvas->drawRect(bounds, bgPaint);
139 canvas->saveLayer(&bounds, nullptr);
140 SkScalar dy = drawCell(canvas, gModes[i],
141 gAlphaValue[alpha & 1],
142 gAlphaValue[alpha & 2]);
143 canvas->restore();
144
145 canvas->translate(0, dy * 5 / 4);
146 }
147 canvas->restore();
148 canvas->restore();
149 canvas->translate(W * 5 / 4, 0);
150 }
151 }
152