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 #include "Sample.h"
8 #include "SkBitmap.h"
9 #include "SkCanvas.h"
10 #include "SkPaint.h"
11 #include "SkShader.h"
12 
createBitmap(int n)13 static SkBitmap createBitmap(int n) {
14     SkBitmap bitmap;
15     bitmap.allocN32Pixels(n, n);
16     bitmap.eraseColor(SK_ColorGREEN);
17 
18     SkCanvas canvas(bitmap);
19     SkRect r;
20     r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n));
21     SkPaint paint;
22     paint.setAntiAlias(true);
23 
24     paint.setColor(SK_ColorRED);
25     canvas.drawOval(r, paint);
26     paint.setColor(SK_ColorBLUE);
27     paint.setStrokeWidth(SkIntToScalar(n)/15);
28     paint.setStyle(SkPaint::kStroke_Style);
29     canvas.drawLine(0, 0, r.fRight, r.fBottom, paint);
30     canvas.drawLine(0, r.fBottom, r.fRight, 0, paint);
31 
32     return bitmap;
33 }
34 
35 class AARectView : public Sample {
36     SkBitmap fBitmap;
37     enum {
38         N = 64
39     };
40 
41 protected:
onOnceBeforeDraw()42     void onOnceBeforeDraw() override {
43         fBitmap = createBitmap(N);
44 
45         fWidth = N;
46     }
47 
onQuery(Sample::Event * evt)48     bool onQuery(Sample::Event* evt) override {
49         if (Sample::TitleQ(*evt)) {
50             Sample::TitleR(evt, "AA Rects");
51             return true;
52         }
53         return this->INHERITED::onQuery(evt);
54     }
55 
onDrawContent(SkCanvas * canvas)56     void onDrawContent(SkCanvas* canvas) override {
57         canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
58 
59         SkPaint bluePaint;
60         bluePaint.setARGB(0xff, 0x0, 0x0, 0xff);
61         SkPaint bmpPaint;
62         bmpPaint.setShader(SkShader::MakeBitmapShader(fBitmap, SkShader::kRepeat_TileMode,
63                                                       SkShader::kRepeat_TileMode));
64         bluePaint.setStrokeWidth(3);
65         bmpPaint.setStrokeWidth(3);
66 
67         SkPaint paints[] = { bluePaint, bmpPaint };
68 
69         SkRect rect;
70 
71         SkScalar dx = SkIntToScalar(80);
72         SkScalar dy = SkIntToScalar(100);
73         SkMatrix matrix;
74         for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) {
75             for (int stroke = 0; stroke < 2; ++stroke) {
76                 paints[p].setStyle(stroke ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
77                 for (int a = 0; a < 3; ++ a) {
78                     paints[p].setAntiAlias(a > 0);
79                     paints[p].setAlpha(a > 1 ? 0x80 : 0xff);
80 
81                     canvas->save();
82                         rect = SkRect::MakeLTRB(0.f,
83                                                 0.f,
84                                                 40.f,
85                                                 40.f);
86                         canvas->drawRect(rect, paints[p]);
87                         canvas->translate(dx, 0);
88 
89                         rect = SkRect::MakeLTRB(0.5f,
90                                                 0.5f,
91                                                 40.5f,
92                                                 40.5f);
93                         canvas->drawRect(rect, paints[p]);
94                         canvas->translate(dx, 0);
95 
96                         rect = SkRect::MakeLTRB(0.5f,
97                                                 0.5f,
98                                                 40.f,
99                                                 40.f);
100                         canvas->drawRect(rect, paints[p]);
101                         canvas->translate(dx, 0);
102 
103                         rect = SkRect::MakeLTRB(0.75f,
104                                                 0.75f,
105                                                 40.75f,
106                                                 40.75f);
107                         canvas->drawRect(rect, paints[p]);
108                         canvas->translate(dx, 0);
109 
110                         canvas->save();
111                             canvas->translate(.33f, .67f);
112                             rect = SkRect::MakeLTRB(0.0f,
113                                                     0.0f,
114                                                     40.0f,
115                                                     40.0f);
116                             canvas->drawRect(rect, paints[p]);
117                         canvas->restore();
118                         canvas->translate(dx, 0);
119 
120                         canvas->save();
121                             matrix.setRotate(45.f);
122                             canvas->concat(matrix);
123                             canvas->translate(20.0f / sqrtf(2.f),
124                                                 20.0f / sqrtf(2.f));
125                             rect = SkRect::MakeLTRB(-20.0f,
126                                                     -20.0f,
127                                                     20.0f,
128                                                     20.0f);
129                             canvas->drawRect(rect, paints[p]);
130                         canvas->restore();
131                         canvas->translate(dx, 0);
132 
133                         canvas->save();
134                             canvas->rotate(90.f);
135                             rect = SkRect::MakeLTRB(0.0f,
136                                                     0.0f,
137                                                     40.0f,
138                                                     -40.0f);
139                             canvas->drawRect(rect, paints[p]);
140                         canvas->restore();
141                         canvas->translate(dx, 0);
142 
143                         canvas->save();
144                             canvas->rotate(90.f);
145                             rect = SkRect::MakeLTRB(0.5f,
146                                                     0.5f,
147                                                     40.5f,
148                                                     -40.5f);
149                             canvas->drawRect(rect, paints[p]);
150                         canvas->restore();
151                         canvas->translate(dx, 0);
152 
153                         canvas->save();
154                             matrix.setScale(-1.f, -1.f);
155                             canvas->concat(matrix);
156                             rect = SkRect::MakeLTRB(0.5f,
157                                                     0.5f,
158                                                     -40.5f,
159                                                     -40.5f);
160                             canvas->drawRect(rect, paints[p]);
161                         canvas->restore();
162                         canvas->translate(dx, 0);
163 
164                         canvas->save();
165                             matrix.setScale(2.1f, 4.1f);
166                             canvas->concat(matrix);
167                             rect = SkRect::MakeLTRB(0.1f,
168                                                     0.1f,
169                                                     19.1f,
170                                                     9.1f);
171                             canvas->drawRect(rect, paints[p]);
172                         canvas->restore();
173                         canvas->translate(dx, 0);
174 
175                     canvas->restore();
176                     canvas->translate(0, dy);
177                 }
178             }
179         }
180     }
181 
182 private:
183     int fWidth;
184 
185     typedef Sample INHERITED;
186 };
187 
188 //////////////////////////////////////////////////////////////////////////////
189 
190 DEF_SAMPLE( return new AARectView(); )
191