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 "include/core/SkCanvas.h"
9 #include "include/core/SkPath.h"
10 #include "samplecode/Sample.h"
11 #include "src/core/SkAAClip.h"
12
testop(const SkIRect & r0,const SkIRect & r1,SkRegion::Op op,const SkIRect & expectedR)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
testop()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
drawClip(SkCanvas * canvas,const SkAAClip & clip)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 {
name()58 SkString name() override { return SkString("AAClip"); }
59
onOnceBeforeDraw()60 void onOnceBeforeDraw() override { testop(); }
61
onDrawContent(SkCanvas * canvas)62 void onDrawContent(SkCanvas* canvas) override {
63 #if 1
64 SkAAClip aaclip;
65 SkPath path;
66 SkRect bounds;
67
68 bounds.setLTRB(0, 0, 20, 20);
69 bounds.inset(SK_ScalarHalf, SK_ScalarHalf);
70
71 // path.addRect(bounds);
72 // path.addOval(bounds);
73 path.addRoundRect(bounds, 4, 4);
74 aaclip.setPath(path);
75 canvas->translate(30, 30);
76 drawClip(canvas, aaclip);
77
78 SkAAClip aaclip2;
79 path.offset(10, 10);
80 aaclip2.setPath(path);
81 canvas->translate(30, 0);
82 drawClip(canvas, aaclip2);
83
84 SkAAClip aaclip3;
85 aaclip3.op(aaclip, aaclip2, SkRegion::kIntersect_Op);
86 canvas->translate(30, 0);
87 drawClip(canvas, aaclip3);
88
89 #endif
90
91 #if 0
92 SkRect r;
93 r.set(0, 0, this->width(), this->height());
94 r.inset(20, 20);
95 canvas->clipRect(r);
96
97 SkPath path;
98 path.addRect(r);
99 SkPaint paint;
100 paint.setAntiAlias(true);
101 paint.setColor(SK_ColorRED);
102 canvas->drawPath(path, paint);
103 #endif
104 }
105 };
106 DEF_SAMPLE( return new AAClipView(); )
107