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" |
Ben Wagner | 8a1036c | 2016-11-09 15:00:49 -0500 | [diff] [blame] | 9 | #include "SkAAClip.h" |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 10 | #include "SkView.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkColorPriv.h" |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 13 | #include "SkPaint.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 14 | #include "SkPath.h" |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 15 | #include "SkRandom.h" |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 16 | #include "SkClipOpPriv.h" |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 17 | |
Ben Wagner | 8a1036c | 2016-11-09 15:00:49 -0500 | [diff] [blame] | 18 | constexpr int W = 150; |
| 19 | constexpr int H = 200; |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 20 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 21 | static void show_text(SkCanvas* canvas, bool doAA) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 22 | SkRandom rand; |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 23 | SkPaint paint; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 24 | paint.setAntiAlias(doAA); |
| 25 | paint.setLCDRenderText(true); |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 26 | paint.setTextSize(SkIntToScalar(20)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 27 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 28 | for (int i = 0; i < 200; ++i) { |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 29 | paint.setColor((SK_A32_MASK << SK_A32_SHIFT) | rand.nextU()); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 30 | canvas->drawString("Hamburgefons", |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 31 | rand.nextSScalar1() * W, rand.nextSScalar1() * H + 20, |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 32 | paint); |
| 33 | } |
| 34 | } |
| 35 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 36 | static void show_fill(SkCanvas* canvas, bool doAA) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 37 | SkRandom rand; |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 38 | SkPaint paint; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 39 | paint.setAntiAlias(doAA); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 40 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 41 | for (int i = 0; i < 50; ++i) { |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 42 | SkRect r; |
| 43 | SkPath p; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 44 | |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 45 | r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H, |
| 46 | rand.nextUScalar1() * W, rand.nextUScalar1() * H); |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 47 | paint.setColor(rand.nextU()); |
| 48 | canvas->drawRect(r, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 49 | |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 50 | r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H, |
| 51 | rand.nextUScalar1() * W, rand.nextUScalar1() * H); |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 52 | paint.setColor(rand.nextU()); |
| 53 | p.addOval(r); |
| 54 | canvas->drawPath(p, paint); |
| 55 | } |
| 56 | } |
| 57 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 58 | static SkScalar randRange(SkRandom& rand, SkScalar min, SkScalar max) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 59 | SkASSERT(min <= max); |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 60 | return min + rand.nextUScalar1() * (max - min); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | 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] | 64 | SkRandom rand; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 65 | SkPaint paint; |
| 66 | paint.setAntiAlias(doAA); |
| 67 | paint.setStyle(SkPaint::kStroke_Style); |
| 68 | paint.setStrokeWidth(strokeWidth); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 69 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 70 | for (int i = 0; i < n; ++i) { |
| 71 | SkRect r; |
| 72 | SkPath p; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 73 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 74 | r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H, |
| 75 | rand.nextUScalar1() * W, rand.nextUScalar1() * H); |
| 76 | paint.setColor(rand.nextU()); |
| 77 | canvas->drawRect(r, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 78 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 79 | r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H, |
| 80 | rand.nextUScalar1() * W, rand.nextUScalar1() * H); |
| 81 | paint.setColor(rand.nextU()); |
| 82 | p.addOval(r); |
| 83 | canvas->drawPath(p, paint); |
| 84 | |
| 85 | const SkScalar minx = -SkIntToScalar(W)/4; |
| 86 | const SkScalar maxx = 5*SkIntToScalar(W)/4; |
| 87 | const SkScalar miny = -SkIntToScalar(H)/4; |
| 88 | const SkScalar maxy = 5*SkIntToScalar(H)/4; |
| 89 | paint.setColor(rand.nextU()); |
| 90 | canvas->drawLine(randRange(rand, minx, maxx), randRange(rand, miny, maxy), |
| 91 | randRange(rand, minx, maxx), randRange(rand, miny, maxy), |
| 92 | paint); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | static void show_hair(SkCanvas* canvas, bool doAA) { |
| 97 | show_stroke(canvas, doAA, 0, 150); |
| 98 | } |
| 99 | |
| 100 | static void show_thick(SkCanvas* canvas, bool doAA) { |
| 101 | show_stroke(canvas, doAA, SkIntToScalar(5), 50); |
| 102 | } |
| 103 | |
| 104 | typedef void (*CanvasProc)(SkCanvas*, bool); |
| 105 | |
reed@google.com | 3bcf8d3 | 2011-10-10 15:42:36 +0000 | [diff] [blame] | 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(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 146 | canvas->clipPath(clipPath, kIntersect_SkClipOp, 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); |