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