epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +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 | */ |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkColorFilter.h" |
| 10 | #include "include/core/SkColorPriv.h" |
| 11 | #include "include/core/SkGraphics.h" |
| 12 | #include "include/core/SkPath.h" |
| 13 | #include "include/core/SkRegion.h" |
| 14 | #include "include/core/SkShader.h" |
| 15 | #include "include/core/SkTime.h" |
| 16 | #include "include/core/SkTypeface.h" |
| 17 | #include "include/effects/SkGradientShader.h" |
| 18 | #include "include/private/SkTo.h" |
| 19 | #include "samplecode/Sample.h" |
| 20 | #include "src/utils/SkUTF.h" |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 21 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 22 | #include <utility> |
| 23 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 24 | class PathClipView : public Sample { |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 25 | public: |
| 26 | SkRect fOval; |
| 27 | SkPoint fCenter; |
| 28 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 29 | PathClipView() : fOval(SkRect::MakeWH(200, 50)), fCenter(SkPoint::Make(250, 250)) {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 30 | |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 31 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 32 | SkString name() override { return SkString("PathClip"); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 33 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 34 | void onDrawContent(SkCanvas* canvas) override { |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 35 | const SkRect oval = fOval.makeOffset(fCenter.fX - fOval.centerX(), |
| 36 | fCenter.fY - fOval.centerY()); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 37 | |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 38 | SkPaint p; |
| 39 | p.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 40 | |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 41 | p.setStyle(SkPaint::kStroke_Style); |
| 42 | canvas->drawOval(oval, p); |
| 43 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 44 | const SkRect r = SkRect::MakeLTRB(200, 200, 300, 300); |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 45 | canvas->clipRect(r); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 46 | |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 47 | p.setStyle(SkPaint::kFill_Style); |
| 48 | p.setColor(SK_ColorRED); |
| 49 | canvas->drawRect(r, p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 50 | |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 51 | p.setColor(0x800000FF); |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 52 | canvas->drawOval(oval, p); |
| 53 | } |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 54 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame^] | 55 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 56 | return new Click(); |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 57 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 58 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 59 | bool onClick(Click* click) override { |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 60 | fCenter.set(click->fCurr.fX, click->fCurr.fY); |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 61 | return false; |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 62 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 63 | |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 64 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 65 | typedef Sample INHERITED; |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 66 | }; |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 67 | DEF_SAMPLE( return new PathClipView; ) |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 68 | |
| 69 | ////////////////////////////////////////////////////////////////////////////// |
| 70 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 71 | static int clip_line(const SkRect& bounds, SkPoint p0, SkPoint p1, SkPoint edges[]) { |
| 72 | SkPoint* edgesStart = edges; |
| 73 | |
| 74 | if (p0.fY == p1.fY) { |
| 75 | return 0; |
| 76 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 77 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 78 | if (p0.fY > p1.fY) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 79 | using std::swap; |
| 80 | swap(p0, p1); |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 81 | } |
| 82 | // now we're monotonic in Y: p0 <= p1 |
| 83 | if (p1.fY <= bounds.top() || p0.fY >= bounds.bottom()) { |
| 84 | return 0; |
| 85 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 86 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 87 | double dxdy = (double)(p1.fX - p0.fX) / (p1.fY - p0.fY); |
| 88 | if (p0.fY < bounds.top()) { |
| 89 | p0.fX = SkDoubleToScalar(p0.fX + dxdy * (bounds.top() - p0.fY)); |
| 90 | p0.fY = bounds.top(); |
| 91 | } |
| 92 | if (p1.fY > bounds.bottom()) { |
| 93 | p1.fX = SkDoubleToScalar(p1.fX + dxdy * (bounds.bottom() - p1.fY)); |
| 94 | p1.fY = bounds.bottom(); |
| 95 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 96 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 97 | // Now p0...p1 is strictly inside bounds vertically, so we just need to clip horizontally |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 98 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 99 | if (p0.fX > p1.fX) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 100 | using std::swap; |
| 101 | swap(p0, p1); |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 102 | } |
| 103 | // now we're left-to-right: p0 .. p1 |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 104 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 105 | if (p1.fX <= bounds.left()) { // entirely to the left |
| 106 | p0.fX = p1.fX = bounds.left(); |
| 107 | *edges++ = p0; |
| 108 | *edges++ = p1; |
| 109 | return 2; |
| 110 | } |
| 111 | if (p0.fX >= bounds.right()) { // entirely to the right |
| 112 | p0.fX = p1.fX = bounds.right(); |
| 113 | *edges++ = p0; |
| 114 | *edges++ = p1; |
| 115 | return 2; |
| 116 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 117 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 118 | if (p0.fX < bounds.left()) { |
| 119 | float y = SkDoubleToScalar(p0.fY + (bounds.left() - p0.fX) / dxdy); |
| 120 | *edges++ = SkPoint::Make(bounds.left(), p0.fY); |
| 121 | *edges++ = SkPoint::Make(bounds.left(), y); |
| 122 | p0.set(bounds.left(), y); |
| 123 | } |
| 124 | if (p1.fX > bounds.right()) { |
| 125 | float y = SkDoubleToScalar(p0.fY + (bounds.right() - p0.fX) / dxdy); |
| 126 | *edges++ = p0; |
| 127 | *edges++ = SkPoint::Make(bounds.right(), y); |
| 128 | *edges++ = SkPoint::Make(bounds.right(), p1.fY); |
| 129 | } else { |
| 130 | *edges++ = p0; |
| 131 | *edges++ = p1; |
| 132 | } |
| 133 | return SkToInt(edges - edgesStart); |
| 134 | } |
| 135 | |
| 136 | static void draw_clipped_line(SkCanvas* canvas, const SkRect& bounds, |
| 137 | SkPoint p0, SkPoint p1, const SkPaint& paint) { |
| 138 | SkPoint verts[6]; |
| 139 | int count = clip_line(bounds, p0, p1, verts); |
| 140 | |
| 141 | SkPath path; |
| 142 | path.addPoly(verts, count, false); |
| 143 | canvas->drawPath(path, paint); |
| 144 | } |
| 145 | |
| 146 | // Demonstrate edge-clipping that is used in the scan converter |
| 147 | // |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 148 | class EdgeClipView : public Sample { |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 149 | enum { |
| 150 | N = 3 |
| 151 | }; |
| 152 | public: |
| 153 | SkPoint fPoly[N]; |
| 154 | SkRect fClip; |
| 155 | SkColor fEdgeColor[N]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 156 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 157 | EdgeClipView() : fClip(SkRect::MakeLTRB(150, 150, 550, 450)) { |
| 158 | fPoly[0].set(300, 40); |
| 159 | fPoly[1].set(550, 250); |
| 160 | fPoly[2].set(40, 450); |
| 161 | |
| 162 | fEdgeColor[0] = 0xFFFF0000; |
| 163 | fEdgeColor[1] = 0xFF00FF00; |
| 164 | fEdgeColor[2] = 0xFF0000FF; |
| 165 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 166 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 167 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 168 | SkString name() override { return SkString("EdgeClip"); } |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 169 | |
| 170 | static SkScalar snap(SkScalar x) { |
| 171 | return SkScalarRoundToScalar(x * 0.5f) * 2; |
| 172 | } |
| 173 | static SkPoint snap(const SkPoint& pt) { |
| 174 | return SkPoint::Make(snap(pt.x()), snap(pt.y())); |
| 175 | } |
| 176 | static void snap(SkPoint dst[], const SkPoint src[], int count) { |
| 177 | for (int i = 0; i < count; ++i) { |
| 178 | dst[i] = snap(src[i]); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void onDrawContent(SkCanvas* canvas) override { |
| 183 | SkPath path; |
| 184 | path.addPoly(fPoly, N, true); |
| 185 | |
| 186 | // Draw the full triangle, stroked and filled |
| 187 | SkPaint p; |
| 188 | p.setAntiAlias(true); |
| 189 | p.setColor(0xFFE0E0E0); |
| 190 | canvas->drawPath(path, p); |
| 191 | p.setStyle(SkPaint::kStroke_Style); |
| 192 | p.setStrokeWidth(2); |
| 193 | for (int i = 0; i < N; ++i) { |
| 194 | const int j = (i + 1) % N; |
| 195 | p.setColor(fEdgeColor[i]); |
| 196 | p.setAlpha(0x88); |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 197 | canvas->drawLine(fPoly[i], fPoly[j], p); |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 198 | } |
| 199 | p.setStyle(SkPaint::kFill_Style); |
| 200 | |
| 201 | // Draw the clip itself |
| 202 | p.setColor(0xFF8888CC); |
| 203 | canvas->drawRect(fClip, p); |
| 204 | |
| 205 | // Draw the filled triangle through the clip |
| 206 | p.setColor(0xFF88CC88); |
| 207 | canvas->save(); |
| 208 | canvas->clipRect(fClip); |
| 209 | canvas->drawPath(path, p); |
| 210 | canvas->restore(); |
| 211 | |
| 212 | p.setStyle(SkPaint::kStroke_Style); |
| 213 | p.setStrokeWidth(6); |
| 214 | |
| 215 | // Draw each of the "Edges" that survived the clipping |
| 216 | // We use a layer, so we can PLUS the different edge-colors, showing where two edges |
| 217 | // canceled each other out. |
| 218 | canvas->saveLayer(nullptr, nullptr); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 219 | p.setBlendMode(SkBlendMode::kPlus); |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 220 | for (int i = 0; i < N; ++i) { |
| 221 | const int j = (i + 1) % N; |
| 222 | p.setColor(fEdgeColor[i]); |
| 223 | draw_clipped_line(canvas, fClip, fPoly[i], fPoly[j], p); |
| 224 | } |
| 225 | canvas->restore(); |
| 226 | } |
| 227 | |
| 228 | class MyClick : public Click { |
| 229 | public: |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 230 | MyClick() {} |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 231 | virtual void handleMove() = 0; |
| 232 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 233 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 234 | class VertClick : public MyClick { |
| 235 | SkPoint* fPt; |
| 236 | public: |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 237 | VertClick(SkPoint* pt) : fPt(pt) {} |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 238 | void handleMove() override { *fPt = snap(fCurr); } |
| 239 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 240 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 241 | class DragRectClick : public MyClick { |
| 242 | SkRect* fRect; |
| 243 | public: |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 244 | DragRectClick(SkRect* rect) : fRect(rect) {} |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 245 | void handleMove() override { fRect->offset(fCurr.x() - fPrev.x(), fCurr.y() - fPrev.y()); } |
| 246 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 247 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 248 | class DragPolyClick : public MyClick { |
| 249 | SkPoint fSrc[100]; |
| 250 | SkPoint* fPoly; |
| 251 | int fCount; |
| 252 | public: |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 253 | DragPolyClick(SkPoint poly[], int count) : fPoly(poly), fCount(count) |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 254 | { |
| 255 | SkASSERT((size_t)count <= SK_ARRAY_COUNT(fSrc)); |
| 256 | memcpy(fSrc, poly, count * sizeof(SkPoint)); |
| 257 | } |
| 258 | void handleMove() override { |
| 259 | const SkScalar dx = fCurr.x() - fOrig.x(); |
| 260 | const SkScalar dy = fCurr.y() - fOrig.y(); |
| 261 | for (int i = 0; i < fCount; ++i) { |
| 262 | fPoly[i].set(snap(fSrc[i].x() + dx), snap(fSrc[i].y() + dy)); |
| 263 | } |
| 264 | } |
| 265 | }; |
| 266 | |
| 267 | class DoNothingClick : public MyClick { |
| 268 | public: |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 269 | DoNothingClick() {} |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 270 | void handleMove() override {} |
| 271 | }; |
| 272 | |
| 273 | static bool hit_test(const SkPoint& pt, SkScalar x, SkScalar y) { |
| 274 | const SkScalar rad = 8; |
| 275 | const SkScalar dx = pt.x() - x; |
| 276 | const SkScalar dy = pt.y() - y; |
| 277 | return dx*dx + dy*dy <= rad*rad; |
| 278 | } |
| 279 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame^] | 280 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override { |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 281 | for (int i = 0; i < N; ++i) { |
| 282 | if (hit_test(fPoly[i], x, y)) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 283 | return new VertClick(&fPoly[i]); |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 286 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 287 | SkPath path; |
| 288 | path.addPoly(fPoly, N, true); |
| 289 | if (path.contains(x, y)) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 290 | return new DragPolyClick(fPoly, N); |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | if (fClip.intersects(SkRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1))) { |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 294 | return new DragRectClick(&fClip); |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 295 | } |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 296 | return new DoNothingClick(); |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 297 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 298 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 299 | bool onClick(Click* click) override { |
| 300 | ((MyClick*)click)->handleMove(); |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 301 | return false; |
| 302 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 303 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 304 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 305 | typedef Sample INHERITED; |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 306 | }; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 307 | |
reed | 67c6513 | 2015-09-28 06:16:07 -0700 | [diff] [blame] | 308 | DEF_SAMPLE( return new EdgeClipView; ) |