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