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 "SkAAClip.h"
11 #include "SkCanvas.h"
12 #include "SkPath.h"
13 #include "SkView.h"
14
testop(const SkIRect & r0,const SkIRect & r1,SkRegion::Op op,const SkIRect & expectedR)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);
21
22 SkDEBUGCODE(SkIRect r2 = c2.getBounds());
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
testop()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
drawClip(SkCanvas * canvas,const SkAAClip & clip)43 static void drawClip(SkCanvas* canvas, const SkAAClip& clip) {
44 SkMask mask;
45 SkBitmap bm;
46
47 clip.copyToMask(&mask);
48 SkAutoMaskFreeImage amfi(mask.fImage);
49
50 bm.installMaskPixels(mask);
51
52 SkPaint paint;
53 canvas->drawBitmap(bm,
54 SK_Scalar1 * mask.fBounds.fLeft,
55 SK_Scalar1 * mask.fBounds.fTop,
56 &paint);
57 }
58
59 class AAClipView : public SampleView {
60 public:
AAClipView()61 AAClipView() {
62 testop();
63 }
64
65 protected:
66 // overrides from SkEventSink
onQuery(SkEvent * evt)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
onDrawContent(SkCanvas * canvas)75 virtual void onDrawContent(SkCanvas* canvas) {
76 #if 1
77 SkAAClip aaclip;
78 SkPath path;
79 SkRect bounds;
80
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);
101
102 #endif
103
104 #if 0
105 SkRect r;
106 r.set(0, 0, this->width(), this->height());
107 r.inset(20, 20);
108 canvas->clipRect(r);
109
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
MyFactory()125 static SkView* MyFactory() { return new AAClipView; }
126 static SkViewRegister reg(MyFactory);
127