reed@google.com | 3228789 | 2011-10-05 16:27:44 +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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkPath.h" |
| 10 | #include "samplecode/Sample.h" |
| 11 | #include "src/core/SkAAClip.h" |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 12 | |
reed@google.com | c904191 | 2011-10-27 16:58:46 +0000 | [diff] [blame] | 13 | static void testop(const SkIRect& r0, const SkIRect& r1, SkRegion::Op op, |
| 14 | const SkIRect& expectedR) { |
| 15 | SkAAClip c0, c1, c2; |
| 16 | c0.setRect(r0); |
| 17 | c1.setRect(r1); |
| 18 | c2.op(c0, c1, op); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 19 | |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 20 | SkDEBUGCODE(SkIRect r2 = c2.getBounds()); |
reed@google.com | c904191 | 2011-10-27 16:58:46 +0000 | [diff] [blame] | 21 | SkASSERT(r2 == expectedR); |
| 22 | } |
| 23 | |
| 24 | static const struct { |
| 25 | SkIRect r0; |
| 26 | SkIRect r1; |
| 27 | SkRegion::Op op; |
| 28 | SkIRect expectedR; |
| 29 | } gRec[] = { |
| 30 | {{ 1, 2, 9, 3 }, { -3, 2, 5, 11 }, SkRegion::kDifference_Op, { 5, 2, 9, 3 }}, |
| 31 | {{ 1, 10, 5, 13 }, { 1, 2, 5, 11 }, SkRegion::kDifference_Op, { 1, 11, 5, 13 }}, |
| 32 | {{ 1, 10, 5, 13 }, { 1, 2, 5, 11 }, SkRegion::kReverseDifference_Op, { 1, 2, 5, 10 }}, |
| 33 | }; |
| 34 | |
| 35 | static void testop() { |
| 36 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 37 | testop(gRec[i].r0, gRec[i].r1, gRec[i].op, gRec[i].expectedR); |
| 38 | } |
| 39 | } |
| 40 | |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 41 | static void drawClip(SkCanvas* canvas, const SkAAClip& clip) { |
| 42 | SkMask mask; |
| 43 | SkBitmap bm; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 44 | |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 45 | clip.copyToMask(&mask); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 46 | SkAutoMaskFreeImage amfi(mask.fImage); |
| 47 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 48 | bm.installMaskPixels(mask); |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 49 | |
| 50 | SkPaint paint; |
bsalomon@google.com | 820e80a | 2011-10-24 21:09:40 +0000 | [diff] [blame] | 51 | canvas->drawBitmap(bm, |
| 52 | SK_Scalar1 * mask.fBounds.fLeft, |
| 53 | SK_Scalar1 * mask.fBounds.fTop, |
| 54 | &paint); |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 57 | class AAClipView : public Sample { |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 58 | SkString name() override { return SkString("AAClip"); } |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 59 | |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 60 | void onOnceBeforeDraw() override { testop(); } |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 61 | |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 62 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 63 | #if 1 |
| 64 | SkAAClip aaclip; |
| 65 | SkPath path; |
| 66 | SkRect bounds; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 67 | |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 68 | bounds.setLTRB(0, 0, 20, 20); |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 69 | bounds.inset(SK_ScalarHalf, SK_ScalarHalf); |
| 70 | |
| 71 | // path.addRect(bounds); |
| 72 | // path.addOval(bounds); |
Mike Reed | 4241f5e | 2019-09-14 19:13:23 +0000 | [diff] [blame] | 73 | path.addRoundRect(bounds, 4, 4); |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 74 | aaclip.setPath(path); |
| 75 | canvas->translate(30, 30); |
| 76 | drawClip(canvas, aaclip); |
| 77 | |
| 78 | SkAAClip aaclip2; |
| 79 | path.offset(10, 10); |
| 80 | aaclip2.setPath(path); |
| 81 | canvas->translate(30, 0); |
| 82 | drawClip(canvas, aaclip2); |
| 83 | |
| 84 | SkAAClip aaclip3; |
| 85 | aaclip3.op(aaclip, aaclip2, SkRegion::kIntersect_Op); |
| 86 | canvas->translate(30, 0); |
| 87 | drawClip(canvas, aaclip3); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 88 | |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 89 | #endif |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 90 | |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 91 | #if 0 |
| 92 | SkRect r; |
| 93 | r.set(0, 0, this->width(), this->height()); |
| 94 | r.inset(20, 20); |
| 95 | canvas->clipRect(r); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 96 | |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 97 | SkPath path; |
| 98 | path.addRect(r); |
| 99 | SkPaint paint; |
| 100 | paint.setAntiAlias(true); |
| 101 | paint.setColor(SK_ColorRED); |
| 102 | canvas->drawPath(path, paint); |
| 103 | #endif |
| 104 | } |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 105 | }; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 106 | DEF_SAMPLE( return new AAClipView(); ) |