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 8 #include "Sample.h" 9 #include "SkAAClip.h" 10 #include "SkCanvas.h" 11 #include "SkPath.h" 12 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); 19 20 SkDEBUGCODE(SkIRect r2 = c2.getBounds()); 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 41 static void drawClip(SkCanvas* canvas, const SkAAClip& clip) { 42 SkMask mask; 43 SkBitmap bm; 44 45 clip.copyToMask(&mask); 46 SkAutoMaskFreeImage amfi(mask.fImage); 47 48 bm.installMaskPixels(mask); 49 50 SkPaint paint; 51 canvas->drawBitmap(bm, 52 SK_Scalar1 * mask.fBounds.fLeft, 53 SK_Scalar1 * mask.fBounds.fTop, 54 &paint); 55 } 56 57 class AAClipView : public Sample { 58 public: 59 AAClipView() { 60 testop(); 61 } 62 63 protected: 64 virtual bool onQuery(Sample::Event* evt) { 65 if (Sample::TitleQ(*evt)) { 66 Sample::TitleR(evt, "AAClip"); 67 return true; 68 } 69 return this->INHERITED::onQuery(evt); 70 } 71 72 virtual void onDrawContent(SkCanvas* canvas) { 73 #if 1 74 SkAAClip aaclip; 75 SkPath path; 76 SkRect bounds; 77 78 bounds.set(0, 0, 20, 20); 79 bounds.inset(SK_ScalarHalf, SK_ScalarHalf); 80 81 // path.addRect(bounds); 82 // path.addOval(bounds); 83 path.addRoundRect(bounds, 4, 4); 84 aaclip.setPath(path); 85 canvas->translate(30, 30); 86 drawClip(canvas, aaclip); 87 88 SkAAClip aaclip2; 89 path.offset(10, 10); 90 aaclip2.setPath(path); 91 canvas->translate(30, 0); 92 drawClip(canvas, aaclip2); 93 94 SkAAClip aaclip3; 95 aaclip3.op(aaclip, aaclip2, SkRegion::kIntersect_Op); 96 canvas->translate(30, 0); 97 drawClip(canvas, aaclip3); 98 99 #endif 100 101 #if 0 102 SkRect r; 103 r.set(0, 0, this->width(), this->height()); 104 r.inset(20, 20); 105 canvas->clipRect(r); 106 107 SkPath path; 108 path.addRect(r); 109 SkPaint paint; 110 paint.setAntiAlias(true); 111 paint.setColor(SK_ColorRED); 112 canvas->drawPath(path, paint); 113 #endif 114 } 115 116 private: 117 typedef Sample INHERITED; 118 }; 119 120 ////////////////////////////////////////////////////////////////////////////// 121 122 DEF_SAMPLE( return new AAClipView(); ) 123