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 | |
| 14 | static void drawClip(SkCanvas* canvas, const SkAAClip& clip) { |
| 15 | SkMask mask; |
| 16 | SkBitmap bm; |
| 17 | |
| 18 | clip.copyToMask(&mask); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 19 | SkAutoMaskFreeImage amfi(mask.fImage); |
| 20 | |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 21 | bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), |
| 22 | mask.fBounds.height(), mask.fRowBytes); |
| 23 | bm.setPixels(mask.fImage); |
| 24 | |
| 25 | SkPaint paint; |
bsalomon@google.com | 820e80a | 2011-10-24 21:09:40 +0000 | [diff] [blame^] | 26 | canvas->drawBitmap(bm, |
| 27 | SK_Scalar1 * mask.fBounds.fLeft, |
| 28 | SK_Scalar1 * mask.fBounds.fTop, |
| 29 | &paint); |
reed@google.com | 3228789 | 2011-10-05 16:27:44 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | class AAClipView : public SampleView { |
| 33 | public: |
| 34 | AAClipView() { |
| 35 | } |
| 36 | |
| 37 | protected: |
| 38 | // overrides from SkEventSink |
| 39 | virtual bool onQuery(SkEvent* evt) { |
| 40 | if (SampleCode::TitleQ(*evt)) { |
| 41 | SampleCode::TitleR(evt, "AAClip"); |
| 42 | return true; |
| 43 | } |
| 44 | return this->INHERITED::onQuery(evt); |
| 45 | } |
| 46 | |
| 47 | virtual void onDrawContent(SkCanvas* canvas) { |
| 48 | #if 1 |
| 49 | SkAAClip aaclip; |
| 50 | SkPath path; |
| 51 | SkRect bounds; |
| 52 | |
| 53 | bounds.set(0, 0, 20, 20); |
| 54 | bounds.inset(SK_ScalarHalf, SK_ScalarHalf); |
| 55 | |
| 56 | // path.addRect(bounds); |
| 57 | // path.addOval(bounds); |
| 58 | path.addRoundRect(bounds, 4, 4); |
| 59 | aaclip.setPath(path); |
| 60 | canvas->translate(30, 30); |
| 61 | drawClip(canvas, aaclip); |
| 62 | |
| 63 | SkAAClip aaclip2; |
| 64 | path.offset(10, 10); |
| 65 | aaclip2.setPath(path); |
| 66 | canvas->translate(30, 0); |
| 67 | drawClip(canvas, aaclip2); |
| 68 | |
| 69 | SkAAClip aaclip3; |
| 70 | aaclip3.op(aaclip, aaclip2, SkRegion::kIntersect_Op); |
| 71 | canvas->translate(30, 0); |
| 72 | drawClip(canvas, aaclip3); |
| 73 | |
| 74 | #endif |
| 75 | |
| 76 | #if 0 |
| 77 | SkRect r; |
| 78 | r.set(0, 0, this->width(), this->height()); |
| 79 | r.inset(20, 20); |
| 80 | canvas->clipRect(r); |
| 81 | |
| 82 | SkPath path; |
| 83 | path.addRect(r); |
| 84 | SkPaint paint; |
| 85 | paint.setAntiAlias(true); |
| 86 | paint.setColor(SK_ColorRED); |
| 87 | canvas->drawPath(path, paint); |
| 88 | #endif |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | typedef SkView INHERITED; |
| 93 | }; |
| 94 | |
| 95 | ////////////////////////////////////////////////////////////////////////////// |
| 96 | |
| 97 | static SkView* MyFactory() { return new AAClipView; } |
| 98 | static SkViewRegister reg(MyFactory); |
| 99 | |