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