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