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