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 | */ |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
| 8 | #include "SkView.h" |
| 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 | |
reed@google.com | 70b6125 | 2011-05-05 14:12:36 +0000 | [diff] [blame] | 35 | class AARectView : public SampleView { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 36 | SkBitmap fBitmap; |
| 37 | enum { |
| 38 | N = 64 |
| 39 | }; |
| 40 | public: |
| 41 | AARectView() { |
| 42 | fBitmap = createBitmap(N); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 43 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 44 | fWidth = N; |
| 45 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 46 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 47 | protected: |
| 48 | // overrides from SkEventSink |
| 49 | virtual bool onQuery(SkEvent* evt) { |
| 50 | if (SampleCode::TitleQ(*evt)) { |
| 51 | SampleCode::TitleR(evt, "AA Rects"); |
| 52 | return true; |
| 53 | } |
| 54 | return this->INHERITED::onQuery(evt); |
| 55 | } |
| 56 | |
reed@google.com | 70b6125 | 2011-05-05 14:12:36 +0000 | [diff] [blame] | 57 | virtual void onDrawContent(SkCanvas* canvas) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 58 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 59 | |
| 60 | SkPaint bluePaint; |
| 61 | bluePaint.setARGB(0xff, 0x0, 0x0, 0xff); |
| 62 | SkPaint bmpPaint; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 63 | bmpPaint.setShader(SkShader::MakeBitmapShader(fBitmap, SkShader::kRepeat_TileMode, |
| 64 | SkShader::kRepeat_TileMode)); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 65 | bluePaint.setStrokeWidth(3); |
| 66 | bmpPaint.setStrokeWidth(3); |
| 67 | |
| 68 | SkPaint paints[] = { bluePaint, bmpPaint }; |
| 69 | |
| 70 | SkRect rect; |
| 71 | |
| 72 | SkScalar dx = SkIntToScalar(80); |
| 73 | SkScalar dy = SkIntToScalar(100); |
| 74 | SkMatrix matrix; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 75 | for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 76 | for (int stroke = 0; stroke < 2; ++stroke) { |
| 77 | paints[p].setStyle(stroke ? SkPaint::kStroke_Style : SkPaint::kFill_Style); |
| 78 | for (int a = 0; a < 3; ++ a) { |
| 79 | paints[p].setAntiAlias(a > 0); |
| 80 | paints[p].setAlpha(a > 1 ? 0x80 : 0xff); |
| 81 | |
| 82 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 83 | rect = SkRect::MakeLTRB(0.f, |
| 84 | 0.f, |
| 85 | 40.f, |
| 86 | 40.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 87 | canvas->drawRect(rect, paints[p]); |
| 88 | canvas->translate(dx, 0); |
| 89 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 90 | rect = SkRect::MakeLTRB(0.5f, |
| 91 | 0.5f, |
| 92 | 40.5f, |
| 93 | 40.5f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 94 | canvas->drawRect(rect, paints[p]); |
| 95 | canvas->translate(dx, 0); |
| 96 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 97 | rect = SkRect::MakeLTRB(0.5f, |
| 98 | 0.5f, |
| 99 | 40.f, |
| 100 | 40.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 101 | canvas->drawRect(rect, paints[p]); |
| 102 | canvas->translate(dx, 0); |
| 103 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 104 | rect = SkRect::MakeLTRB(0.75f, |
| 105 | 0.75f, |
| 106 | 40.75f, |
| 107 | 40.75f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 108 | canvas->drawRect(rect, paints[p]); |
| 109 | canvas->translate(dx, 0); |
| 110 | |
| 111 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 112 | canvas->translate(.33f, .67f); |
| 113 | rect = SkRect::MakeLTRB(0.0f, |
| 114 | 0.0f, |
| 115 | 40.0f, |
| 116 | 40.0f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 117 | canvas->drawRect(rect, paints[p]); |
| 118 | canvas->restore(); |
| 119 | canvas->translate(dx, 0); |
| 120 | |
| 121 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 122 | matrix.setRotate(45.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 123 | canvas->concat(matrix); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 124 | canvas->translate(20.0f / sqrtf(2.f), |
| 125 | 20.0f / sqrtf(2.f)); |
| 126 | rect = SkRect::MakeLTRB(-20.0f, |
| 127 | -20.0f, |
| 128 | 20.0f, |
| 129 | 20.0f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 130 | canvas->drawRect(rect, paints[p]); |
| 131 | canvas->restore(); |
| 132 | canvas->translate(dx, 0); |
| 133 | |
| 134 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 135 | canvas->rotate(90.f); |
| 136 | rect = SkRect::MakeLTRB(0.0f, |
| 137 | 0.0f, |
| 138 | 40.0f, |
| 139 | -40.0f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 140 | canvas->drawRect(rect, paints[p]); |
| 141 | canvas->restore(); |
| 142 | canvas->translate(dx, 0); |
| 143 | |
| 144 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 145 | canvas->rotate(90.f); |
| 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(-1.f, -1.f); |
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.5f, |
| 158 | 0.5f, |
| 159 | -40.5f, |
| 160 | -40.5f); |
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->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 166 | matrix.setScale(2.1f, 4.1f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 167 | canvas->concat(matrix); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 168 | rect = SkRect::MakeLTRB(0.1f, |
| 169 | 0.1f, |
| 170 | 19.1f, |
| 171 | 9.1f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 172 | canvas->drawRect(rect, paints[p]); |
| 173 | canvas->restore(); |
| 174 | canvas->translate(dx, 0); |
| 175 | |
| 176 | canvas->restore(); |
| 177 | canvas->translate(0, dy); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 182 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 183 | private: |
| 184 | int fWidth; |
| 185 | |
reed@google.com | 70b6125 | 2011-05-05 14:12:36 +0000 | [diff] [blame] | 186 | typedef SampleView INHERITED; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | ////////////////////////////////////////////////////////////////////////////// |
| 190 | |
| 191 | static SkView* MyFactory() { return new AARectView; } |
| 192 | static SkViewRegister reg(MyFactory); |