blob: f552df6f1586c4ac580ea6d2fe97926c185b3f80 [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;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000017 bitmap.allocN32Pixels(n, n);
bsalomon@google.com205d4602011-04-25 12:43:45 +000018 bitmap.eraseColor(SK_ColorGREEN);
rmistry@google.comae933ce2012-08-23 18:19:56 +000019
bsalomon@google.com205d4602011-04-25 12:43:45 +000020 SkCanvas canvas(bitmap);
21 SkRect r;
22 r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n));
23 SkPaint paint;
24 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +000025
bsalomon@google.com205d4602011-04-25 12:43:45 +000026 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +000033
bsalomon@google.com205d4602011-04-25 12:43:45 +000034 return bitmap;
35}
36
reed@google.com70b61252011-05-05 14:12:36 +000037class AARectView : public SampleView {
bsalomon@google.com205d4602011-04-25 12:43:45 +000038 SkBitmap fBitmap;
39 enum {
40 N = 64
41 };
42public:
43 AARectView() {
44 fBitmap = createBitmap(N);
rmistry@google.comae933ce2012-08-23 18:19:56 +000045
bsalomon@google.com205d4602011-04-25 12:43:45 +000046 fWidth = N;
47 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000048
bsalomon@google.com205d4602011-04-25 12:43:45 +000049protected:
50 // overrides from SkEventSink
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
reed@google.com70b61252011-05-05 14:12:36 +000059 virtual void onDrawContent(SkCanvas* canvas) {
bsalomon@google.com205d4602011-04-25 12:43:45 +000060 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;
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000079 for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) {
bsalomon@google.com205d4602011-04-25 12:43:45 +000080 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();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000087 rect = SkRect::MakeLTRB(0.f,
88 0.f,
89 40.f,
90 40.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +000091 canvas->drawRect(rect, paints[p]);
92 canvas->translate(dx, 0);
93
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000094 rect = SkRect::MakeLTRB(0.5f,
95 0.5f,
96 40.5f,
97 40.5f);
bsalomon@google.com205d4602011-04-25 12:43:45 +000098 canvas->drawRect(rect, paints[p]);
99 canvas->translate(dx, 0);
100
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000101 rect = SkRect::MakeLTRB(0.5f,
102 0.5f,
103 40.f,
104 40.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000105 canvas->drawRect(rect, paints[p]);
106 canvas->translate(dx, 0);
107
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000108 rect = SkRect::MakeLTRB(0.75f,
109 0.75f,
110 40.75f,
111 40.75f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000112 canvas->drawRect(rect, paints[p]);
113 canvas->translate(dx, 0);
114
115 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000116 canvas->translate(.33f, .67f);
117 rect = SkRect::MakeLTRB(0.0f,
118 0.0f,
119 40.0f,
120 40.0f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000121 canvas->drawRect(rect, paints[p]);
122 canvas->restore();
123 canvas->translate(dx, 0);
124
125 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000126 matrix.setRotate(45.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000127 canvas->concat(matrix);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000128 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);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000134 canvas->drawRect(rect, paints[p]);
135 canvas->restore();
136 canvas->translate(dx, 0);
137
138 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000139 canvas->rotate(90.f);
140 rect = SkRect::MakeLTRB(0.0f,
141 0.0f,
142 40.0f,
143 -40.0f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000144 canvas->drawRect(rect, paints[p]);
145 canvas->restore();
146 canvas->translate(dx, 0);
147
148 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000149 canvas->rotate(90.f);
150 rect = SkRect::MakeLTRB(0.5f,
151 0.5f,
152 40.5f,
153 -40.5f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000154 canvas->drawRect(rect, paints[p]);
155 canvas->restore();
156 canvas->translate(dx, 0);
157
158 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000159 matrix.setScale(-1.f, -1.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000160 canvas->concat(matrix);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000161 rect = SkRect::MakeLTRB(0.5f,
162 0.5f,
163 -40.5f,
164 -40.5f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000165 canvas->drawRect(rect, paints[p]);
166 canvas->restore();
167 canvas->translate(dx, 0);
168
169 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000170 matrix.setScale(2.1f, 4.1f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000171 canvas->concat(matrix);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000172 rect = SkRect::MakeLTRB(0.1f,
173 0.1f,
174 19.1f,
175 9.1f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000176 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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000186
bsalomon@google.com205d4602011-04-25 12:43:45 +0000187private:
188 int fWidth;
189
reed@google.com70b61252011-05-05 14:12:36 +0000190 typedef SampleView INHERITED;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000191};
192
193//////////////////////////////////////////////////////////////////////////////
194
195static SkView* MyFactory() { return new AARectView; }
196static SkViewRegister reg(MyFactory);