epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 12 | |
| 13 | static SkBitmap createBitmap(int n) { |
| 14 | SkBitmap bitmap; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 15 | bitmap.allocN32Pixels(n, n); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 16 | bitmap.eraseColor(SK_ColorGREEN); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 18 | SkCanvas canvas(bitmap); |
| 19 | SkRect r; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 20 | r.setWH(SkIntToScalar(n), SkIntToScalar(n)); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 21 | SkPaint paint; |
| 22 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 23 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 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); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 31 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 32 | return bitmap; |
| 33 | } |
| 34 | |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 35 | static constexpr int N = 64; |
| 36 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 37 | class AARectView : public Sample { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 38 | SkBitmap fBitmap; |
Cary Clark | 58627cb | 2018-04-10 09:16:41 -0400 | [diff] [blame] | 39 | |
Cary Clark | 58627cb | 2018-04-10 09:16:41 -0400 | [diff] [blame] | 40 | void onOnceBeforeDraw() override { |
Cary Clark | 1ad8198 | 2017-12-19 09:18:59 -0500 | [diff] [blame] | 41 | fBitmap = createBitmap(N); |
Cary Clark | 1ad8198 | 2017-12-19 09:18:59 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 44 | SkString name() override { return SkString("AA Rects"); } |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 45 | |
Cary Clark | 58627cb | 2018-04-10 09:16:41 -0400 | [diff] [blame] | 46 | void onDrawContent(SkCanvas* canvas) override { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 47 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 48 | |
| 49 | SkPaint bluePaint; |
| 50 | bluePaint.setARGB(0xff, 0x0, 0x0, 0xff); |
| 51 | SkPaint bmpPaint; |
Mike Reed | 82abece | 2020-12-12 09:51:11 -0500 | [diff] [blame] | 52 | bmpPaint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 53 | SkSamplingOptions())); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 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; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 64 | for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 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(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 72 | rect = SkRect::MakeLTRB(0.f, |
| 73 | 0.f, |
| 74 | 40.f, |
| 75 | 40.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 76 | canvas->drawRect(rect, paints[p]); |
| 77 | canvas->translate(dx, 0); |
| 78 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 79 | rect = SkRect::MakeLTRB(0.5f, |
| 80 | 0.5f, |
| 81 | 40.5f, |
| 82 | 40.5f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 83 | canvas->drawRect(rect, paints[p]); |
| 84 | canvas->translate(dx, 0); |
| 85 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 86 | rect = SkRect::MakeLTRB(0.5f, |
| 87 | 0.5f, |
| 88 | 40.f, |
| 89 | 40.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 90 | canvas->drawRect(rect, paints[p]); |
| 91 | canvas->translate(dx, 0); |
| 92 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 93 | rect = SkRect::MakeLTRB(0.75f, |
| 94 | 0.75f, |
| 95 | 40.75f, |
| 96 | 40.75f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 97 | canvas->drawRect(rect, paints[p]); |
| 98 | canvas->translate(dx, 0); |
| 99 | |
| 100 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 101 | canvas->translate(.33f, .67f); |
| 102 | rect = SkRect::MakeLTRB(0.0f, |
| 103 | 0.0f, |
| 104 | 40.0f, |
| 105 | 40.0f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 106 | canvas->drawRect(rect, paints[p]); |
| 107 | canvas->restore(); |
| 108 | canvas->translate(dx, 0); |
| 109 | |
| 110 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 111 | matrix.setRotate(45.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 112 | canvas->concat(matrix); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 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); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 119 | canvas->drawRect(rect, paints[p]); |
| 120 | canvas->restore(); |
| 121 | canvas->translate(dx, 0); |
| 122 | |
| 123 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 124 | canvas->rotate(90.f); |
| 125 | rect = SkRect::MakeLTRB(0.0f, |
| 126 | 0.0f, |
| 127 | 40.0f, |
| 128 | -40.0f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 129 | canvas->drawRect(rect, paints[p]); |
| 130 | canvas->restore(); |
| 131 | canvas->translate(dx, 0); |
| 132 | |
| 133 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 134 | canvas->rotate(90.f); |
| 135 | rect = SkRect::MakeLTRB(0.5f, |
| 136 | 0.5f, |
| 137 | 40.5f, |
| 138 | -40.5f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 139 | canvas->drawRect(rect, paints[p]); |
| 140 | canvas->restore(); |
| 141 | canvas->translate(dx, 0); |
| 142 | |
| 143 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 144 | matrix.setScale(-1.f, -1.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 145 | canvas->concat(matrix); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 146 | rect = SkRect::MakeLTRB(0.5f, |
| 147 | 0.5f, |
| 148 | -40.5f, |
| 149 | -40.5f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 150 | canvas->drawRect(rect, paints[p]); |
| 151 | canvas->restore(); |
| 152 | canvas->translate(dx, 0); |
| 153 | |
| 154 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 155 | matrix.setScale(2.1f, 4.1f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 156 | canvas->concat(matrix); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 157 | rect = SkRect::MakeLTRB(0.1f, |
| 158 | 0.1f, |
| 159 | 19.1f, |
| 160 | 9.1f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 171 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 172 | private: |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 173 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 174 | using INHERITED = Sample; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | ////////////////////////////////////////////////////////////////////////////// |
| 178 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 179 | DEF_SAMPLE( return new AARectView(); ) |