blob: e66f8dfc4336143a3028a28fc9d616cfd0795c65 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
bsalomon@google.com205d4602011-04-25 12:43:45 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkDevice.h"
12#include "SkPaint.h"
13#include "SkShader.h"
14
15static SkBitmap createBitmap(int n) {
16 SkBitmap bitmap;
17 bitmap.setConfig(SkBitmap::kARGB_8888_Config, n, n);
18 bitmap.allocPixels();
19 bitmap.eraseColor(SK_ColorGREEN);
rmistry@google.comae933ce2012-08-23 18:19:56 +000020
bsalomon@google.com205d4602011-04-25 12:43:45 +000021 SkCanvas canvas(bitmap);
22 SkRect r;
23 r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n));
24 SkPaint paint;
25 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +000026
bsalomon@google.com205d4602011-04-25 12:43:45 +000027 paint.setColor(SK_ColorRED);
28 canvas.drawOval(r, paint);
29 paint.setColor(SK_ColorBLUE);
30 paint.setStrokeWidth(SkIntToScalar(n)/15);
31 paint.setStyle(SkPaint::kStroke_Style);
32 canvas.drawLine(0, 0, r.fRight, r.fBottom, paint);
33 canvas.drawLine(0, r.fBottom, r.fRight, 0, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000034
bsalomon@google.com205d4602011-04-25 12:43:45 +000035 return bitmap;
36}
37
reed@google.com70b61252011-05-05 14:12:36 +000038class AARectView : public SampleView {
bsalomon@google.com205d4602011-04-25 12:43:45 +000039 SkBitmap fBitmap;
40 enum {
41 N = 64
42 };
43public:
44 AARectView() {
45 fBitmap = createBitmap(N);
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
bsalomon@google.com205d4602011-04-25 12:43:45 +000047 fWidth = N;
48 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000049
bsalomon@google.com205d4602011-04-25 12:43:45 +000050protected:
51 // overrides from SkEventSink
52 virtual bool onQuery(SkEvent* evt) {
53 if (SampleCode::TitleQ(*evt)) {
54 SampleCode::TitleR(evt, "AA Rects");
55 return true;
56 }
57 return this->INHERITED::onQuery(evt);
58 }
59
reed@google.com70b61252011-05-05 14:12:36 +000060 virtual void onDrawContent(SkCanvas* canvas) {
bsalomon@google.com205d4602011-04-25 12:43:45 +000061 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
62
63 SkPaint bluePaint;
64 bluePaint.setARGB(0xff, 0x0, 0x0, 0xff);
65 SkPaint bmpPaint;
66 SkShader* bmpShader = SkShader::CreateBitmapShader(fBitmap, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
67 bmpPaint.setShader(bmpShader);
68 bmpShader->unref();
69
70 bluePaint.setStrokeWidth(3);
71 bmpPaint.setStrokeWidth(3);
72
73 SkPaint paints[] = { bluePaint, bmpPaint };
74
75 SkRect rect;
76
77 SkScalar dx = SkIntToScalar(80);
78 SkScalar dy = SkIntToScalar(100);
79 SkMatrix matrix;
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000080 for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) {
bsalomon@google.com205d4602011-04-25 12:43:45 +000081 for (int stroke = 0; stroke < 2; ++stroke) {
82 paints[p].setStyle(stroke ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
83 for (int a = 0; a < 3; ++ a) {
84 paints[p].setAntiAlias(a > 0);
85 paints[p].setAlpha(a > 1 ? 0x80 : 0xff);
86
87 canvas->save();
88 rect = SkRect::MakeLTRB(SkFloatToScalar(0.f),
89 SkFloatToScalar(0.f),
90 SkFloatToScalar(40.f),
91 SkFloatToScalar(40.f));
92 canvas->drawRect(rect, paints[p]);
93 canvas->translate(dx, 0);
94
95 rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
96 SkFloatToScalar(0.5f),
97 SkFloatToScalar(40.5f),
98 SkFloatToScalar(40.5f));
99 canvas->drawRect(rect, paints[p]);
100 canvas->translate(dx, 0);
101
102 rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
103 SkFloatToScalar(0.5f),
104 SkFloatToScalar(40.f),
105 SkFloatToScalar(40.f));
106 canvas->drawRect(rect, paints[p]);
107 canvas->translate(dx, 0);
108
109 rect = SkRect::MakeLTRB(SkFloatToScalar(0.75f),
110 SkFloatToScalar(0.75f),
111 SkFloatToScalar(40.75f),
112 SkFloatToScalar(40.75f));
113 canvas->drawRect(rect, paints[p]);
114 canvas->translate(dx, 0);
115
116 canvas->save();
117 canvas->translate(SkFloatToScalar(.33f), SkFloatToScalar(.67f));
118 rect = SkRect::MakeLTRB(SkFloatToScalar(0.0f),
119 SkFloatToScalar(0.0f),
120 SkFloatToScalar(40.0f),
121 SkFloatToScalar(40.0f));
122 canvas->drawRect(rect, paints[p]);
123 canvas->restore();
124 canvas->translate(dx, 0);
125
126 canvas->save();
127 matrix.setRotate(SkFloatToScalar(45.f));
128 canvas->concat(matrix);
129 canvas->translate(SkFloatToScalar(20.0f / sqrtf(2.f)),
130 SkFloatToScalar(20.0f / sqrtf(2.f)));
131 rect = SkRect::MakeLTRB(SkFloatToScalar(-20.0f),
132 SkFloatToScalar(-20.0f),
133 SkFloatToScalar(20.0f),
134 SkFloatToScalar(20.0f));
135 canvas->drawRect(rect, paints[p]);
136 canvas->restore();
137 canvas->translate(dx, 0);
138
139 canvas->save();
140 canvas->rotate(SkFloatToScalar(90.f));
141 rect = SkRect::MakeLTRB(SkFloatToScalar(0.0f),
142 SkFloatToScalar(0.0f),
143 SkFloatToScalar(40.0f),
144 SkFloatToScalar(-40.0f));
145 canvas->drawRect(rect, paints[p]);
146 canvas->restore();
147 canvas->translate(dx, 0);
148
149 canvas->save();
150 canvas->rotate(SkFloatToScalar(90.f));
151 rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
152 SkFloatToScalar(0.5f),
153 SkFloatToScalar(40.5f),
154 SkFloatToScalar(-40.5f));
155 canvas->drawRect(rect, paints[p]);
156 canvas->restore();
157 canvas->translate(dx, 0);
158
159 canvas->save();
160 matrix.setScale(SkFloatToScalar(-1.f), SkFloatToScalar(-1.f));
161 canvas->concat(matrix);
162 rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
163 SkFloatToScalar(0.5f),
164 SkFloatToScalar(-40.5f),
165 SkFloatToScalar(-40.5f));
166 canvas->drawRect(rect, paints[p]);
167 canvas->restore();
168 canvas->translate(dx, 0);
169
170 canvas->save();
171 matrix.setScale(SkFloatToScalar(2.1f), SkFloatToScalar(4.1f));
172 canvas->concat(matrix);
173 rect = SkRect::MakeLTRB(SkFloatToScalar(0.1f),
174 SkFloatToScalar(0.1f),
175 SkFloatToScalar(19.1f),
176 SkFloatToScalar(9.1f));
177 canvas->drawRect(rect, paints[p]);
178 canvas->restore();
179 canvas->translate(dx, 0);
180
181 canvas->restore();
182 canvas->translate(0, dy);
183 }
184 }
185 }
186 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000187
bsalomon@google.com205d4602011-04-25 12:43:45 +0000188private:
189 int fWidth;
190
reed@google.com70b61252011-05-05 14:12:36 +0000191 typedef SampleView INHERITED;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000192};
193
194//////////////////////////////////////////////////////////////////////////////
195
196static SkView* MyFactory() { return new AARectView; }
197static SkViewRegister reg(MyFactory);
198