epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkDevice.h" |
| 12 | #include "SkPaint.h" |
| 13 | #include "SkShader.h" |
| 14 | |
| 15 | static SkBitmap createBitmap(int n) { |
| 16 | SkBitmap bitmap; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 17 | bitmap.allocN32Pixels(n, n); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 18 | bitmap.eraseColor(SK_ColorGREEN); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 19 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 20 | SkCanvas canvas(bitmap); |
| 21 | SkRect r; |
| 22 | r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n)); |
| 23 | SkPaint paint; |
| 24 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 26 | paint.setColor(SK_ColorRED); |
| 27 | canvas.drawOval(r, paint); |
| 28 | paint.setColor(SK_ColorBLUE); |
| 29 | paint.setStrokeWidth(SkIntToScalar(n)/15); |
| 30 | paint.setStyle(SkPaint::kStroke_Style); |
| 31 | canvas.drawLine(0, 0, r.fRight, r.fBottom, paint); |
| 32 | canvas.drawLine(0, r.fBottom, r.fRight, 0, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 33 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 34 | return bitmap; |
| 35 | } |
| 36 | |
reed@google.com | 70b6125 | 2011-05-05 14:12:36 +0000 | [diff] [blame] | 37 | class AARectView : public SampleView { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 38 | SkBitmap fBitmap; |
| 39 | enum { |
| 40 | N = 64 |
| 41 | }; |
| 42 | public: |
| 43 | AARectView() { |
| 44 | fBitmap = createBitmap(N); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 45 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 46 | fWidth = N; |
| 47 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 48 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 49 | protected: |
| 50 | // overrides from SkEventSink |
| 51 | virtual bool onQuery(SkEvent* evt) { |
| 52 | if (SampleCode::TitleQ(*evt)) { |
| 53 | SampleCode::TitleR(evt, "AA Rects"); |
| 54 | return true; |
| 55 | } |
| 56 | return this->INHERITED::onQuery(evt); |
| 57 | } |
| 58 | |
reed@google.com | 70b6125 | 2011-05-05 14:12:36 +0000 | [diff] [blame] | 59 | virtual void onDrawContent(SkCanvas* canvas) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 60 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 61 | |
| 62 | SkPaint bluePaint; |
| 63 | bluePaint.setARGB(0xff, 0x0, 0x0, 0xff); |
| 64 | SkPaint bmpPaint; |
| 65 | SkShader* bmpShader = SkShader::CreateBitmapShader(fBitmap, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); |
| 66 | bmpPaint.setShader(bmpShader); |
| 67 | bmpShader->unref(); |
| 68 | |
| 69 | bluePaint.setStrokeWidth(3); |
| 70 | bmpPaint.setStrokeWidth(3); |
| 71 | |
| 72 | SkPaint paints[] = { bluePaint, bmpPaint }; |
| 73 | |
| 74 | SkRect rect; |
| 75 | |
| 76 | SkScalar dx = SkIntToScalar(80); |
| 77 | SkScalar dy = SkIntToScalar(100); |
| 78 | SkMatrix matrix; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 79 | for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 80 | for (int stroke = 0; stroke < 2; ++stroke) { |
| 81 | paints[p].setStyle(stroke ? SkPaint::kStroke_Style : SkPaint::kFill_Style); |
| 82 | for (int a = 0; a < 3; ++ a) { |
| 83 | paints[p].setAntiAlias(a > 0); |
| 84 | paints[p].setAlpha(a > 1 ? 0x80 : 0xff); |
| 85 | |
| 86 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 87 | rect = SkRect::MakeLTRB(0.f, |
| 88 | 0.f, |
| 89 | 40.f, |
| 90 | 40.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 91 | canvas->drawRect(rect, paints[p]); |
| 92 | canvas->translate(dx, 0); |
| 93 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 94 | rect = SkRect::MakeLTRB(0.5f, |
| 95 | 0.5f, |
| 96 | 40.5f, |
| 97 | 40.5f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 98 | canvas->drawRect(rect, paints[p]); |
| 99 | canvas->translate(dx, 0); |
| 100 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 101 | rect = SkRect::MakeLTRB(0.5f, |
| 102 | 0.5f, |
| 103 | 40.f, |
| 104 | 40.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 105 | canvas->drawRect(rect, paints[p]); |
| 106 | canvas->translate(dx, 0); |
| 107 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 108 | rect = SkRect::MakeLTRB(0.75f, |
| 109 | 0.75f, |
| 110 | 40.75f, |
| 111 | 40.75f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 112 | canvas->drawRect(rect, paints[p]); |
| 113 | canvas->translate(dx, 0); |
| 114 | |
| 115 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 116 | canvas->translate(.33f, .67f); |
| 117 | rect = SkRect::MakeLTRB(0.0f, |
| 118 | 0.0f, |
| 119 | 40.0f, |
| 120 | 40.0f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 121 | canvas->drawRect(rect, paints[p]); |
| 122 | canvas->restore(); |
| 123 | canvas->translate(dx, 0); |
| 124 | |
| 125 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 126 | matrix.setRotate(45.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 127 | canvas->concat(matrix); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 128 | canvas->translate(20.0f / sqrtf(2.f), |
| 129 | 20.0f / sqrtf(2.f)); |
| 130 | rect = SkRect::MakeLTRB(-20.0f, |
| 131 | -20.0f, |
| 132 | 20.0f, |
| 133 | 20.0f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 134 | canvas->drawRect(rect, paints[p]); |
| 135 | canvas->restore(); |
| 136 | canvas->translate(dx, 0); |
| 137 | |
| 138 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 139 | canvas->rotate(90.f); |
| 140 | rect = SkRect::MakeLTRB(0.0f, |
| 141 | 0.0f, |
| 142 | 40.0f, |
| 143 | -40.0f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 144 | canvas->drawRect(rect, paints[p]); |
| 145 | canvas->restore(); |
| 146 | canvas->translate(dx, 0); |
| 147 | |
| 148 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 149 | canvas->rotate(90.f); |
| 150 | rect = SkRect::MakeLTRB(0.5f, |
| 151 | 0.5f, |
| 152 | 40.5f, |
| 153 | -40.5f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 154 | canvas->drawRect(rect, paints[p]); |
| 155 | canvas->restore(); |
| 156 | canvas->translate(dx, 0); |
| 157 | |
| 158 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 159 | matrix.setScale(-1.f, -1.f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 160 | canvas->concat(matrix); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 161 | rect = SkRect::MakeLTRB(0.5f, |
| 162 | 0.5f, |
| 163 | -40.5f, |
| 164 | -40.5f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 165 | canvas->drawRect(rect, paints[p]); |
| 166 | canvas->restore(); |
| 167 | canvas->translate(dx, 0); |
| 168 | |
| 169 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 170 | matrix.setScale(2.1f, 4.1f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 171 | canvas->concat(matrix); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 172 | rect = SkRect::MakeLTRB(0.1f, |
| 173 | 0.1f, |
| 174 | 19.1f, |
| 175 | 9.1f); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 176 | canvas->drawRect(rect, paints[p]); |
| 177 | canvas->restore(); |
| 178 | canvas->translate(dx, 0); |
| 179 | |
| 180 | canvas->restore(); |
| 181 | canvas->translate(0, dy); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 186 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 187 | private: |
| 188 | int fWidth; |
| 189 | |
reed@google.com | 70b6125 | 2011-05-05 14:12:36 +0000 | [diff] [blame] | 190 | typedef SampleView INHERITED; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | ////////////////////////////////////////////////////////////////////////////// |
| 194 | |
| 195 | static SkView* MyFactory() { return new AARectView; } |
| 196 | static SkViewRegister reg(MyFactory); |