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