1 /*
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 */
7 #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"
12
createBitmap(int n)13 static SkBitmap createBitmap(int n) {
14 SkBitmap bitmap;
15 bitmap.allocN32Pixels(n, n);
16 bitmap.eraseColor(SK_ColorGREEN);
17
18 SkCanvas canvas(bitmap);
19 SkRect r;
20 r.setWH(SkIntToScalar(n), SkIntToScalar(n));
21 SkPaint paint;
22 paint.setAntiAlias(true);
23
24 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);
31
32 return bitmap;
33 }
34
35 static constexpr int N = 64;
36
37 class AARectView : public Sample {
38 SkBitmap fBitmap;
39
onOnceBeforeDraw()40 void onOnceBeforeDraw() override {
41 fBitmap = createBitmap(N);
42 }
43
name()44 SkString name() override { return SkString("AA Rects"); }
45
onDrawContent(SkCanvas * canvas)46 void onDrawContent(SkCanvas* canvas) override {
47 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
48
49 SkPaint bluePaint;
50 bluePaint.setARGB(0xff, 0x0, 0x0, 0xff);
51 SkPaint bmpPaint;
52 bmpPaint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
53 SkSamplingOptions()));
54 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;
64 for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) {
65 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();
72 rect = SkRect::MakeLTRB(0.f,
73 0.f,
74 40.f,
75 40.f);
76 canvas->drawRect(rect, paints[p]);
77 canvas->translate(dx, 0);
78
79 rect = SkRect::MakeLTRB(0.5f,
80 0.5f,
81 40.5f,
82 40.5f);
83 canvas->drawRect(rect, paints[p]);
84 canvas->translate(dx, 0);
85
86 rect = SkRect::MakeLTRB(0.5f,
87 0.5f,
88 40.f,
89 40.f);
90 canvas->drawRect(rect, paints[p]);
91 canvas->translate(dx, 0);
92
93 rect = SkRect::MakeLTRB(0.75f,
94 0.75f,
95 40.75f,
96 40.75f);
97 canvas->drawRect(rect, paints[p]);
98 canvas->translate(dx, 0);
99
100 canvas->save();
101 canvas->translate(.33f, .67f);
102 rect = SkRect::MakeLTRB(0.0f,
103 0.0f,
104 40.0f,
105 40.0f);
106 canvas->drawRect(rect, paints[p]);
107 canvas->restore();
108 canvas->translate(dx, 0);
109
110 canvas->save();
111 matrix.setRotate(45.f);
112 canvas->concat(matrix);
113 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);
119 canvas->drawRect(rect, paints[p]);
120 canvas->restore();
121 canvas->translate(dx, 0);
122
123 canvas->save();
124 canvas->rotate(90.f);
125 rect = SkRect::MakeLTRB(0.0f,
126 0.0f,
127 40.0f,
128 -40.0f);
129 canvas->drawRect(rect, paints[p]);
130 canvas->restore();
131 canvas->translate(dx, 0);
132
133 canvas->save();
134 canvas->rotate(90.f);
135 rect = SkRect::MakeLTRB(0.5f,
136 0.5f,
137 40.5f,
138 -40.5f);
139 canvas->drawRect(rect, paints[p]);
140 canvas->restore();
141 canvas->translate(dx, 0);
142
143 canvas->save();
144 matrix.setScale(-1.f, -1.f);
145 canvas->concat(matrix);
146 rect = SkRect::MakeLTRB(0.5f,
147 0.5f,
148 -40.5f,
149 -40.5f);
150 canvas->drawRect(rect, paints[p]);
151 canvas->restore();
152 canvas->translate(dx, 0);
153
154 canvas->save();
155 matrix.setScale(2.1f, 4.1f);
156 canvas->concat(matrix);
157 rect = SkRect::MakeLTRB(0.1f,
158 0.1f,
159 19.1f,
160 9.1f);
161 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 }
171
172 private:
173
174 using INHERITED = Sample;
175 };
176
177 //////////////////////////////////////////////////////////////////////////////
178
179 DEF_SAMPLE( return new AARectView(); )
180