blob: 211b41131a6a779211bd672c3fefa0114d6dc056 [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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04007#include "Sample.h"
Mike Reed75ae4212018-01-23 11:24:08 -05008#include "SkBitmap.h"
bsalomon@google.com205d4602011-04-25 12:43:45 +00009#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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040035class AARectView : public Sample {
bsalomon@google.com205d4602011-04-25 12:43:45 +000036 SkBitmap fBitmap;
37 enum {
38 N = 64
39 };
Cary Clark58627cb2018-04-10 09:16:41 -040040
41protected:
42 void onOnceBeforeDraw() override {
Cary Clark1ad81982017-12-19 09:18:59 -050043 fBitmap = createBitmap(N);
Cary Clark394197d2017-12-19 20:20:13 +000044
Cary Clark1ad81982017-12-19 09:18:59 -050045 fWidth = N;
46 }
47
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040048 bool onQuery(Sample::Event* evt) override {
49 if (Sample::TitleQ(*evt)) {
50 Sample::TitleR(evt, "AA Rects");
bsalomon@google.com205d4602011-04-25 12:43:45 +000051 return true;
52 }
53 return this->INHERITED::onQuery(evt);
54 }
55
Cary Clark58627cb2018-04-10 09:16:41 -040056 void onDrawContent(SkCanvas* canvas) override {
bsalomon@google.com205d4602011-04-25 12:43:45 +000057 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
58
59 SkPaint bluePaint;
60 bluePaint.setARGB(0xff, 0x0, 0x0, 0xff);
61 SkPaint bmpPaint;
reed8a21c9f2016-03-08 18:50:00 -080062 bmpPaint.setShader(SkShader::MakeBitmapShader(fBitmap, SkShader::kRepeat_TileMode,
63 SkShader::kRepeat_TileMode));
bsalomon@google.com205d4602011-04-25 12:43:45 +000064 bluePaint.setStrokeWidth(3);
65 bmpPaint.setStrokeWidth(3);
66
67 SkPaint paints[] = { bluePaint, bmpPaint };
68
69 SkRect rect;
70
71 SkScalar dx = SkIntToScalar(80);
72 SkScalar dy = SkIntToScalar(100);
73 SkMatrix matrix;
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000074 for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) {
bsalomon@google.com205d4602011-04-25 12:43:45 +000075 for (int stroke = 0; stroke < 2; ++stroke) {
76 paints[p].setStyle(stroke ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
77 for (int a = 0; a < 3; ++ a) {
78 paints[p].setAntiAlias(a > 0);
79 paints[p].setAlpha(a > 1 ? 0x80 : 0xff);
80
81 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000082 rect = SkRect::MakeLTRB(0.f,
83 0.f,
84 40.f,
85 40.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +000086 canvas->drawRect(rect, paints[p]);
87 canvas->translate(dx, 0);
88
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000089 rect = SkRect::MakeLTRB(0.5f,
90 0.5f,
91 40.5f,
92 40.5f);
bsalomon@google.com205d4602011-04-25 12:43:45 +000093 canvas->drawRect(rect, paints[p]);
94 canvas->translate(dx, 0);
95
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000096 rect = SkRect::MakeLTRB(0.5f,
97 0.5f,
98 40.f,
99 40.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000100 canvas->drawRect(rect, paints[p]);
101 canvas->translate(dx, 0);
102
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000103 rect = SkRect::MakeLTRB(0.75f,
104 0.75f,
105 40.75f,
106 40.75f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000107 canvas->drawRect(rect, paints[p]);
108 canvas->translate(dx, 0);
109
110 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000111 canvas->translate(.33f, .67f);
112 rect = SkRect::MakeLTRB(0.0f,
113 0.0f,
114 40.0f,
115 40.0f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000116 canvas->drawRect(rect, paints[p]);
117 canvas->restore();
118 canvas->translate(dx, 0);
119
120 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000121 matrix.setRotate(45.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000122 canvas->concat(matrix);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000123 canvas->translate(20.0f / sqrtf(2.f),
124 20.0f / sqrtf(2.f));
125 rect = SkRect::MakeLTRB(-20.0f,
126 -20.0f,
127 20.0f,
128 20.0f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000129 canvas->drawRect(rect, paints[p]);
130 canvas->restore();
131 canvas->translate(dx, 0);
132
133 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000134 canvas->rotate(90.f);
135 rect = SkRect::MakeLTRB(0.0f,
136 0.0f,
137 40.0f,
138 -40.0f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000139 canvas->drawRect(rect, paints[p]);
140 canvas->restore();
141 canvas->translate(dx, 0);
142
143 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000144 canvas->rotate(90.f);
145 rect = SkRect::MakeLTRB(0.5f,
146 0.5f,
147 40.5f,
148 -40.5f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000149 canvas->drawRect(rect, paints[p]);
150 canvas->restore();
151 canvas->translate(dx, 0);
152
153 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000154 matrix.setScale(-1.f, -1.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000155 canvas->concat(matrix);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000156 rect = SkRect::MakeLTRB(0.5f,
157 0.5f,
158 -40.5f,
159 -40.5f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000160 canvas->drawRect(rect, paints[p]);
161 canvas->restore();
162 canvas->translate(dx, 0);
163
164 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000165 matrix.setScale(2.1f, 4.1f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000166 canvas->concat(matrix);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000167 rect = SkRect::MakeLTRB(0.1f,
168 0.1f,
169 19.1f,
170 9.1f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000171 canvas->drawRect(rect, paints[p]);
172 canvas->restore();
173 canvas->translate(dx, 0);
174
175 canvas->restore();
176 canvas->translate(0, dy);
177 }
178 }
179 }
180 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000181
bsalomon@google.com205d4602011-04-25 12:43:45 +0000182private:
183 int fWidth;
184
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400185 typedef Sample INHERITED;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000186};
187
188//////////////////////////////////////////////////////////////////////////////
189
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400190DEF_SAMPLE( return new AARectView(); )