reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +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 | */ |
| 7 | |
| 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkColorPriv.h" |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 12 | #include "SkPaint.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 13 | #include "SkPath.h" |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 14 | #include "SkRandom.h" |
| 15 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 16 | #define W 150 |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 17 | #define H 200 |
| 18 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 19 | static void show_text(SkCanvas* canvas, bool doAA) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 20 | SkRandom rand; |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 21 | SkPaint paint; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 22 | paint.setAntiAlias(doAA); |
| 23 | paint.setLCDRenderText(true); |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 24 | paint.setTextSize(SkIntToScalar(20)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 25 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 26 | for (int i = 0; i < 200; ++i) { |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 27 | paint.setColor((SK_A32_MASK << SK_A32_SHIFT) | rand.nextU()); |
| 28 | canvas->drawText("Hamburgefons", 12, |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 29 | rand.nextSScalar1() * W, rand.nextSScalar1() * H + 20, |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 30 | paint); |
| 31 | } |
| 32 | } |
| 33 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 34 | static void show_fill(SkCanvas* canvas, bool doAA) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 35 | SkRandom rand; |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 36 | SkPaint paint; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 37 | paint.setAntiAlias(doAA); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 38 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 39 | for (int i = 0; i < 50; ++i) { |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 40 | SkRect r; |
| 41 | SkPath p; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 42 | |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 43 | r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H, |
| 44 | rand.nextUScalar1() * W, rand.nextUScalar1() * H); |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 45 | paint.setColor(rand.nextU()); |
| 46 | canvas->drawRect(r, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 47 | |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 48 | r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H, |
| 49 | rand.nextUScalar1() * W, rand.nextUScalar1() * H); |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 50 | paint.setColor(rand.nextU()); |
| 51 | p.addOval(r); |
| 52 | canvas->drawPath(p, paint); |
| 53 | } |
| 54 | } |
| 55 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 56 | static SkScalar randRange(SkRandom& rand, SkScalar min, SkScalar max) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 57 | SkASSERT(min <= max); |
| 58 | return min + SkScalarMul(rand.nextUScalar1(), max - min); |
| 59 | } |
| 60 | |
| 61 | static void show_stroke(SkCanvas* canvas, bool doAA, SkScalar strokeWidth, int n) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 62 | SkRandom rand; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 63 | SkPaint paint; |
| 64 | paint.setAntiAlias(doAA); |
| 65 | paint.setStyle(SkPaint::kStroke_Style); |
| 66 | paint.setStrokeWidth(strokeWidth); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 67 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 68 | for (int i = 0; i < n; ++i) { |
| 69 | SkRect r; |
| 70 | SkPath p; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 71 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 72 | r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H, |
| 73 | rand.nextUScalar1() * W, rand.nextUScalar1() * H); |
| 74 | paint.setColor(rand.nextU()); |
| 75 | canvas->drawRect(r, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 76 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 77 | r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H, |
| 78 | rand.nextUScalar1() * W, rand.nextUScalar1() * H); |
| 79 | paint.setColor(rand.nextU()); |
| 80 | p.addOval(r); |
| 81 | canvas->drawPath(p, paint); |
| 82 | |
| 83 | const SkScalar minx = -SkIntToScalar(W)/4; |
| 84 | const SkScalar maxx = 5*SkIntToScalar(W)/4; |
| 85 | const SkScalar miny = -SkIntToScalar(H)/4; |
| 86 | const SkScalar maxy = 5*SkIntToScalar(H)/4; |
| 87 | paint.setColor(rand.nextU()); |
| 88 | canvas->drawLine(randRange(rand, minx, maxx), randRange(rand, miny, maxy), |
| 89 | randRange(rand, minx, maxx), randRange(rand, miny, maxy), |
| 90 | paint); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static void show_hair(SkCanvas* canvas, bool doAA) { |
| 95 | show_stroke(canvas, doAA, 0, 150); |
| 96 | } |
| 97 | |
| 98 | static void show_thick(SkCanvas* canvas, bool doAA) { |
| 99 | show_stroke(canvas, doAA, SkIntToScalar(5), 50); |
| 100 | } |
| 101 | |
| 102 | typedef void (*CanvasProc)(SkCanvas*, bool); |
| 103 | |
| 104 | #include "SkAAClip.h" |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 105 | |
| 106 | class ClipView : public SampleView { |
| 107 | public: |
| 108 | ClipView() { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 109 | SkAAClip clip; |
| 110 | SkIRect r = { -2, -3, 842, 18 }; |
| 111 | clip.setRect(r); |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | virtual ~ClipView() { |
| 115 | } |
| 116 | |
| 117 | protected: |
| 118 | // overrides from SkEventSink |
| 119 | virtual bool onQuery(SkEvent* evt) { |
| 120 | if (SampleCode::TitleQ(*evt)) { |
| 121 | SampleCode::TitleR(evt, "Clip"); |
| 122 | return true; |
| 123 | } |
| 124 | return this->INHERITED::onQuery(evt); |
| 125 | } |
| 126 | |
| 127 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 128 | canvas->drawColor(SK_ColorWHITE); |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 129 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 130 | |
| 131 | static const CanvasProc gProc[] = { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 132 | show_text, show_thick, show_hair, show_fill |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 133 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 134 | |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 135 | SkRect r = { 0, 0, SkIntToScalar(W), SkIntToScalar(H) }; |
| 136 | SkPath clipPath; |
| 137 | r.inset(SK_Scalar1 / 4, SK_Scalar1 / 4); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 138 | clipPath.addRoundRect(r, SkIntToScalar(20), SkIntToScalar(20)); |
| 139 | |
| 140 | // clipPath.toggleInverseFillType(); |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 141 | |
| 142 | for (int aa = 0; aa <= 1; ++aa) { |
| 143 | canvas->save(); |
| 144 | for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); ++i) { |
| 145 | canvas->save(); |
robertphillips@google.com | 67a6db3 | 2012-05-03 18:05:18 +0000 | [diff] [blame] | 146 | canvas->clipPath(clipPath, SkRegion::kIntersect_Op, SkToBool(aa)); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 147 | // canvas->drawColor(SK_ColorWHITE); |
| 148 | gProc[i](canvas, SkToBool(aa)); |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 149 | canvas->restore(); |
| 150 | canvas->translate(W * SK_Scalar1 * 8 / 7, 0); |
| 151 | } |
| 152 | canvas->restore(); |
| 153 | canvas->translate(0, H * SK_Scalar1 * 8 / 7); |
| 154 | } |
| 155 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 156 | |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 157 | private: |
| 158 | typedef SampleView INHERITED; |
| 159 | }; |
| 160 | |
| 161 | ////////////////////////////////////////////////////////////////////////////// |
| 162 | |
| 163 | static SkView* MyFactory() { return new ClipView; } |
| 164 | static SkViewRegister reg(MyFactory); |