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