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 | */ |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 7 | #include "Sample.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 8 | #include "SkBitmap.h" |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 10 | #include "SkPaint.h" |
| 11 | #include "SkShader.h" |
| 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; |
| 20 | r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n)); |
| 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 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 35 | class AARectView : public Sample { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 36 | SkBitmap fBitmap; |
| 37 | enum { |
| 38 | N = 64 |
| 39 | }; |
Cary Clark | 58627cb | 2018-04-10 09:16:41 -0400 | [diff] [blame] | 40 | |
| 41 | protected: |
| 42 | void onOnceBeforeDraw() override { |
Cary Clark | 1ad8198 | 2017-12-19 09:18:59 -0500 | [diff] [blame] | 43 | fBitmap = createBitmap(N); |
Cary Clark | 394197d | 2017-12-19 20:20:13 +0000 | [diff] [blame] | 44 | |
Cary Clark | 1ad8198 | 2017-12-19 09:18:59 -0500 | [diff] [blame] | 45 | fWidth = N; |
| 46 | } |
| 47 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 48 | bool onQuery(Sample::Event* evt) override { |
| 49 | if (Sample::TitleQ(*evt)) { |
| 50 | Sample::TitleR(evt, "AA Rects"); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | return this->INHERITED::onQuery(evt); |
| 54 | } |
| 55 | |
Cary Clark | 58627cb | 2018-04-10 09:16:41 -0400 | [diff] [blame] | 56 | void onDrawContent(SkCanvas* canvas) override { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 57 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 58 | |
| 59 | SkPaint bluePaint; |
| 60 | bluePaint.setARGB(0xff, 0x0, 0x0, 0xff); |
| 61 | SkPaint bmpPaint; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 62 | bmpPaint.setShader(SkShader::MakeBitmapShader(fBitmap, SkShader::kRepeat_TileMode, |
| 63 | SkShader::kRepeat_TileMode)); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 64 | 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.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 74 | for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 75 | 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.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 82 | rect = SkRect::MakeLTRB(0.f, |
| 83 | 0.f, |
| 84 | 40.f, |
| 85 | 40.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 86 | canvas->drawRect(rect, paints[p]); |
| 87 | canvas->translate(dx, 0); |
| 88 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 89 | rect = SkRect::MakeLTRB(0.5f, |
| 90 | 0.5f, |
| 91 | 40.5f, |
| 92 | 40.5f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 93 | canvas->drawRect(rect, paints[p]); |
| 94 | canvas->translate(dx, 0); |
| 95 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 96 | rect = SkRect::MakeLTRB(0.5f, |
| 97 | 0.5f, |
| 98 | 40.f, |
| 99 | 40.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 100 | canvas->drawRect(rect, paints[p]); |
| 101 | canvas->translate(dx, 0); |
| 102 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 103 | rect = SkRect::MakeLTRB(0.75f, |
| 104 | 0.75f, |
| 105 | 40.75f, |
| 106 | 40.75f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 107 | canvas->drawRect(rect, paints[p]); |
| 108 | canvas->translate(dx, 0); |
| 109 | |
| 110 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 111 | canvas->translate(.33f, .67f); |
| 112 | rect = SkRect::MakeLTRB(0.0f, |
| 113 | 0.0f, |
| 114 | 40.0f, |
| 115 | 40.0f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 116 | canvas->drawRect(rect, paints[p]); |
| 117 | canvas->restore(); |
| 118 | canvas->translate(dx, 0); |
| 119 | |
| 120 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 121 | matrix.setRotate(45.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 122 | canvas->concat(matrix); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 123 | 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.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.0f, |
| 136 | 0.0f, |
| 137 | 40.0f, |
| 138 | -40.0f); |
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 | canvas->rotate(90.f); |
| 145 | rect = SkRect::MakeLTRB(0.5f, |
| 146 | 0.5f, |
| 147 | 40.5f, |
| 148 | -40.5f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 149 | canvas->drawRect(rect, paints[p]); |
| 150 | canvas->restore(); |
| 151 | canvas->translate(dx, 0); |
| 152 | |
| 153 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 154 | matrix.setScale(-1.f, -1.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 155 | canvas->concat(matrix); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 156 | rect = SkRect::MakeLTRB(0.5f, |
| 157 | 0.5f, |
| 158 | -40.5f, |
| 159 | -40.5f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 160 | canvas->drawRect(rect, paints[p]); |
| 161 | canvas->restore(); |
| 162 | canvas->translate(dx, 0); |
| 163 | |
| 164 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 165 | matrix.setScale(2.1f, 4.1f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 166 | canvas->concat(matrix); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 167 | rect = SkRect::MakeLTRB(0.1f, |
| 168 | 0.1f, |
| 169 | 19.1f, |
| 170 | 9.1f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 171 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 181 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 182 | private: |
| 183 | int fWidth; |
| 184 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 185 | typedef Sample INHERITED; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | ////////////////////////////////////////////////////////////////////////////// |
| 189 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 190 | DEF_SAMPLE( return new AARectView(); ) |