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