blob: 942242b02e77d17a7ab71006adb9ffeb97e8be03 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
bsalomon@google.com205d4602011-04-25 12:43:45 +00007#include "SampleCode.h"
8#include "SkView.h"
9#include "SkCanvas.h"
bsalomon@google.com205d4602011-04-25 12:43:45 +000010#include "SkPaint.h"
11#include "SkShader.h"
12
13static SkBitmap createBitmap(int n) {
14 SkBitmap bitmap;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000015 bitmap.allocN32Pixels(n, n);
bsalomon@google.com205d4602011-04-25 12:43:45 +000016 bitmap.eraseColor(SK_ColorGREEN);
rmistry@google.comae933ce2012-08-23 18:19:56 +000017
bsalomon@google.com205d4602011-04-25 12:43:45 +000018 SkCanvas canvas(bitmap);
19 SkRect r;
20 r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n));
21 SkPaint paint;
22 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +000023
bsalomon@google.com205d4602011-04-25 12:43:45 +000024 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +000031
bsalomon@google.com205d4602011-04-25 12:43:45 +000032 return bitmap;
33}
34
reed@google.com70b61252011-05-05 14:12:36 +000035class AARectView : public SampleView {
bsalomon@google.com205d4602011-04-25 12:43:45 +000036 SkBitmap fBitmap;
37 enum {
38 N = 64
39 };
40public:
41 AARectView() {
42 fBitmap = createBitmap(N);
rmistry@google.comae933ce2012-08-23 18:19:56 +000043
bsalomon@google.com205d4602011-04-25 12:43:45 +000044 fWidth = N;
45 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
bsalomon@google.com205d4602011-04-25 12:43:45 +000047protected:
48 // overrides from SkEventSink
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
reed@google.com70b61252011-05-05 14:12:36 +000057 virtual void onDrawContent(SkCanvas* canvas) {
bsalomon@google.com205d4602011-04-25 12:43:45 +000058 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
59
60 SkPaint bluePaint;
61 bluePaint.setARGB(0xff, 0x0, 0x0, 0xff);
62 SkPaint bmpPaint;
reed8a21c9f2016-03-08 18:50:00 -080063 bmpPaint.setShader(SkShader::MakeBitmapShader(fBitmap, SkShader::kRepeat_TileMode,
64 SkShader::kRepeat_TileMode));
bsalomon@google.com205d4602011-04-25 12:43:45 +000065 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;
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000075 for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) {
bsalomon@google.com205d4602011-04-25 12:43:45 +000076 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();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000083 rect = SkRect::MakeLTRB(0.f,
84 0.f,
85 40.f,
86 40.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +000087 canvas->drawRect(rect, paints[p]);
88 canvas->translate(dx, 0);
89
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000090 rect = SkRect::MakeLTRB(0.5f,
91 0.5f,
92 40.5f,
93 40.5f);
bsalomon@google.com205d4602011-04-25 12:43:45 +000094 canvas->drawRect(rect, paints[p]);
95 canvas->translate(dx, 0);
96
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000097 rect = SkRect::MakeLTRB(0.5f,
98 0.5f,
99 40.f,
100 40.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000101 canvas->drawRect(rect, paints[p]);
102 canvas->translate(dx, 0);
103
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000104 rect = SkRect::MakeLTRB(0.75f,
105 0.75f,
106 40.75f,
107 40.75f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000108 canvas->drawRect(rect, paints[p]);
109 canvas->translate(dx, 0);
110
111 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000112 canvas->translate(.33f, .67f);
113 rect = SkRect::MakeLTRB(0.0f,
114 0.0f,
115 40.0f,
116 40.0f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000117 canvas->drawRect(rect, paints[p]);
118 canvas->restore();
119 canvas->translate(dx, 0);
120
121 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000122 matrix.setRotate(45.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000123 canvas->concat(matrix);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000124 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);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000130 canvas->drawRect(rect, paints[p]);
131 canvas->restore();
132 canvas->translate(dx, 0);
133
134 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000135 canvas->rotate(90.f);
136 rect = SkRect::MakeLTRB(0.0f,
137 0.0f,
138 40.0f,
139 -40.0f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000140 canvas->drawRect(rect, paints[p]);
141 canvas->restore();
142 canvas->translate(dx, 0);
143
144 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000145 canvas->rotate(90.f);
146 rect = SkRect::MakeLTRB(0.5f,
147 0.5f,
148 40.5f,
149 -40.5f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000150 canvas->drawRect(rect, paints[p]);
151 canvas->restore();
152 canvas->translate(dx, 0);
153
154 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000155 matrix.setScale(-1.f, -1.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000156 canvas->concat(matrix);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000157 rect = SkRect::MakeLTRB(0.5f,
158 0.5f,
159 -40.5f,
160 -40.5f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000161 canvas->drawRect(rect, paints[p]);
162 canvas->restore();
163 canvas->translate(dx, 0);
164
165 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000166 matrix.setScale(2.1f, 4.1f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000167 canvas->concat(matrix);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000168 rect = SkRect::MakeLTRB(0.1f,
169 0.1f,
170 19.1f,
171 9.1f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000172 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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000182
bsalomon@google.com205d4602011-04-25 12:43:45 +0000183private:
184 int fWidth;
185
reed@google.com70b61252011-05-05 14:12:36 +0000186 typedef SampleView INHERITED;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000187};
188
189//////////////////////////////////////////////////////////////////////////////
190
191static SkView* MyFactory() { return new AARectView; }
192static SkViewRegister reg(MyFactory);