blob: b7cc5e8866407a6e515ea390e3e57969755828cb [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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "include/core/SkBitmap.h"
8#include "include/core/SkCanvas.h"
9#include "include/core/SkPaint.h"
10#include "include/core/SkShader.h"
11#include "samplecode/Sample.h"
bsalomon@google.com205d4602011-04-25 12:43:45 +000012
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;
Mike Reed92b33352019-08-24 19:39:13 -040020 r.setWH(SkIntToScalar(n), SkIntToScalar(n));
bsalomon@google.com205d4602011-04-25 12:43:45 +000021 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
Hal Canaryd7639af2019-07-17 09:08:11 -040035static constexpr int N = 64;
36
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040037class AARectView : public Sample {
bsalomon@google.com205d4602011-04-25 12:43:45 +000038 SkBitmap fBitmap;
Cary Clark58627cb2018-04-10 09:16:41 -040039
Cary Clark58627cb2018-04-10 09:16:41 -040040 void onOnceBeforeDraw() override {
Cary Clark1ad81982017-12-19 09:18:59 -050041 fBitmap = createBitmap(N);
Cary Clark1ad81982017-12-19 09:18:59 -050042 }
43
Hal Canary8a027312019-07-03 10:55:44 -040044 SkString name() override { return SkString("AA Rects"); }
bsalomon@google.com205d4602011-04-25 12:43:45 +000045
Cary Clark58627cb2018-04-10 09:16:41 -040046 void onDrawContent(SkCanvas* canvas) override {
bsalomon@google.com205d4602011-04-25 12:43:45 +000047 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
48
49 SkPaint bluePaint;
50 bluePaint.setARGB(0xff, 0x0, 0x0, 0xff);
51 SkPaint bmpPaint;
Mike Reed82abece2020-12-12 09:51:11 -050052 bmpPaint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
53 SkSamplingOptions()));
bsalomon@google.com205d4602011-04-25 12:43:45 +000054 bluePaint.setStrokeWidth(3);
55 bmpPaint.setStrokeWidth(3);
56
57 SkPaint paints[] = { bluePaint, bmpPaint };
58
59 SkRect rect;
60
61 SkScalar dx = SkIntToScalar(80);
62 SkScalar dy = SkIntToScalar(100);
63 SkMatrix matrix;
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000064 for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) {
bsalomon@google.com205d4602011-04-25 12:43:45 +000065 for (int stroke = 0; stroke < 2; ++stroke) {
66 paints[p].setStyle(stroke ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
67 for (int a = 0; a < 3; ++ a) {
68 paints[p].setAntiAlias(a > 0);
69 paints[p].setAlpha(a > 1 ? 0x80 : 0xff);
70
71 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000072 rect = SkRect::MakeLTRB(0.f,
73 0.f,
74 40.f,
75 40.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +000076 canvas->drawRect(rect, paints[p]);
77 canvas->translate(dx, 0);
78
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000079 rect = SkRect::MakeLTRB(0.5f,
80 0.5f,
81 40.5f,
82 40.5f);
bsalomon@google.com205d4602011-04-25 12:43:45 +000083 canvas->drawRect(rect, paints[p]);
84 canvas->translate(dx, 0);
85
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000086 rect = SkRect::MakeLTRB(0.5f,
87 0.5f,
88 40.f,
89 40.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +000090 canvas->drawRect(rect, paints[p]);
91 canvas->translate(dx, 0);
92
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000093 rect = SkRect::MakeLTRB(0.75f,
94 0.75f,
95 40.75f,
96 40.75f);
bsalomon@google.com205d4602011-04-25 12:43:45 +000097 canvas->drawRect(rect, paints[p]);
98 canvas->translate(dx, 0);
99
100 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000101 canvas->translate(.33f, .67f);
102 rect = SkRect::MakeLTRB(0.0f,
103 0.0f,
104 40.0f,
105 40.0f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000106 canvas->drawRect(rect, paints[p]);
107 canvas->restore();
108 canvas->translate(dx, 0);
109
110 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000111 matrix.setRotate(45.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000112 canvas->concat(matrix);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000113 canvas->translate(20.0f / sqrtf(2.f),
114 20.0f / sqrtf(2.f));
115 rect = SkRect::MakeLTRB(-20.0f,
116 -20.0f,
117 20.0f,
118 20.0f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000119 canvas->drawRect(rect, paints[p]);
120 canvas->restore();
121 canvas->translate(dx, 0);
122
123 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000124 canvas->rotate(90.f);
125 rect = SkRect::MakeLTRB(0.0f,
126 0.0f,
127 40.0f,
128 -40.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.5f,
136 0.5f,
137 40.5f,
138 -40.5f);
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 matrix.setScale(-1.f, -1.f);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000145 canvas->concat(matrix);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000146 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(2.1f, 4.1f);
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.1f,
158 0.1f,
159 19.1f,
160 9.1f);
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->restore();
166 canvas->translate(0, dy);
167 }
168 }
169 }
170 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000171
bsalomon@google.com205d4602011-04-25 12:43:45 +0000172private:
bsalomon@google.com205d4602011-04-25 12:43:45 +0000173
John Stiles7571f9e2020-09-02 22:42:33 -0400174 using INHERITED = Sample;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000175};
176
177//////////////////////////////////////////////////////////////////////////////
178
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400179DEF_SAMPLE( return new AARectView(); )