reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "SampleCode.h" |
reed | fb8c1fc | 2015-08-04 18:44:56 -0700 | [diff] [blame] | 10 | #include "SkAAClip.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkView.h" |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 14 | |
reed@google.com | c904191 | 2011-10-27 16:58:46 +0000 | [diff] [blame] | 15 | static void testop(const SkIRect& r0, const SkIRect& r1, SkRegion::Op op, |
| 16 | const SkIRect& expectedR) { |
| 17 | SkAAClip c0, c1, c2; |
| 18 | c0.setRect(r0); |
| 19 | c1.setRect(r1); |
| 20 | c2.op(c0, c1, op); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 21 | |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 22 | SkDEBUGCODE(SkIRect r2 = c2.getBounds()); |
reed@google.com | c904191 | 2011-10-27 16:58:46 +0000 | [diff] [blame] | 23 | SkASSERT(r2 == expectedR); |
| 24 | } |
| 25 | |
| 26 | static const struct { |
| 27 | SkIRect r0; |
| 28 | SkIRect r1; |
| 29 | SkRegion::Op op; |
| 30 | SkIRect expectedR; |
| 31 | } gRec[] = { |
| 32 | {{ 1, 2, 9, 3 }, { -3, 2, 5, 11 }, SkRegion::kDifference_Op, { 5, 2, 9, 3 }}, |
| 33 | {{ 1, 10, 5, 13 }, { 1, 2, 5, 11 }, SkRegion::kDifference_Op, { 1, 11, 5, 13 }}, |
| 34 | {{ 1, 10, 5, 13 }, { 1, 2, 5, 11 }, SkRegion::kReverseDifference_Op, { 1, 2, 5, 10 }}, |
| 35 | }; |
| 36 | |
| 37 | static void testop() { |
| 38 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 39 | testop(gRec[i].r0, gRec[i].r1, gRec[i].op, gRec[i].expectedR); |
| 40 | } |
| 41 | } |
| 42 | |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 43 | static void drawClip(SkCanvas* canvas, const SkAAClip& clip) { |
| 44 | SkMask mask; |
| 45 | SkBitmap bm; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 46 | |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 47 | clip.copyToMask(&mask); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 48 | SkAutoMaskFreeImage amfi(mask.fImage); |
| 49 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 50 | bm.installMaskPixels(mask); |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 51 | |
| 52 | SkPaint paint; |
bsalomon@google.com | 820e80a | 2011-10-24 21:09:40 +0000 | [diff] [blame] | 53 | canvas->drawBitmap(bm, |
| 54 | SK_Scalar1 * mask.fBounds.fLeft, |
| 55 | SK_Scalar1 * mask.fBounds.fTop, |
| 56 | &paint); |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | class AAClipView : public SampleView { |
| 60 | public: |
| 61 | AAClipView() { |
reed@google.com | c904191 | 2011-10-27 16:58:46 +0000 | [diff] [blame] | 62 | testop(); |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | protected: |
| 66 | // overrides from SkEventSink |
| 67 | virtual bool onQuery(SkEvent* evt) { |
| 68 | if (SampleCode::TitleQ(*evt)) { |
| 69 | SampleCode::TitleR(evt, "AAClip"); |
| 70 | return true; |
| 71 | } |
| 72 | return this->INHERITED::onQuery(evt); |
| 73 | } |
| 74 | |
| 75 | virtual void onDrawContent(SkCanvas* canvas) { |
| 76 | #if 1 |
| 77 | SkAAClip aaclip; |
| 78 | SkPath path; |
| 79 | SkRect bounds; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 80 | |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 81 | bounds.set(0, 0, 20, 20); |
| 82 | bounds.inset(SK_ScalarHalf, SK_ScalarHalf); |
| 83 | |
| 84 | // path.addRect(bounds); |
| 85 | // path.addOval(bounds); |
| 86 | path.addRoundRect(bounds, 4, 4); |
| 87 | aaclip.setPath(path); |
| 88 | canvas->translate(30, 30); |
| 89 | drawClip(canvas, aaclip); |
| 90 | |
| 91 | SkAAClip aaclip2; |
| 92 | path.offset(10, 10); |
| 93 | aaclip2.setPath(path); |
| 94 | canvas->translate(30, 0); |
| 95 | drawClip(canvas, aaclip2); |
| 96 | |
| 97 | SkAAClip aaclip3; |
| 98 | aaclip3.op(aaclip, aaclip2, SkRegion::kIntersect_Op); |
| 99 | canvas->translate(30, 0); |
| 100 | drawClip(canvas, aaclip3); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 101 | |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 102 | #endif |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 103 | |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 104 | #if 0 |
| 105 | SkRect r; |
| 106 | r.set(0, 0, this->width(), this->height()); |
| 107 | r.inset(20, 20); |
| 108 | canvas->clipRect(r); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 109 | |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 110 | SkPath path; |
| 111 | path.addRect(r); |
| 112 | SkPaint paint; |
| 113 | paint.setAntiAlias(true); |
| 114 | paint.setColor(SK_ColorRED); |
| 115 | canvas->drawPath(path, paint); |
| 116 | #endif |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | typedef SkView INHERITED; |
| 121 | }; |
| 122 | |
| 123 | ////////////////////////////////////////////////////////////////////////////// |
| 124 | |
| 125 | static SkView* MyFactory() { return new AAClipView; } |
| 126 | static SkViewRegister reg(MyFactory); |