blob: 4c7956053102b1d992baa218150979a1a15359e0 [file] [log] [blame]
reed@google.com32287892011-10-05 16:27:44 +00001/*
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 "SampleCode.h"
reedfb8c1fc2015-08-04 18:44:56 -07009#include "SkAAClip.h"
bungemand3ebb482015-08-05 13:57:49 -070010#include "SkCanvas.h"
11#include "SkPath.h"
12#include "SkView.h"
reed@google.com32287892011-10-05 16:27:44 +000013
reed@google.comc9041912011-10-27 16:58:46 +000014static 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.comae933ce2012-08-23 18:19:56 +000020
humper@google.com0e515772013-01-07 19:54:40 +000021 SkDEBUGCODE(SkIRect r2 = c2.getBounds());
reed@google.comc9041912011-10-27 16:58:46 +000022 SkASSERT(r2 == expectedR);
23}
24
25static 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
36static 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.com32287892011-10-05 16:27:44 +000042static void drawClip(SkCanvas* canvas, const SkAAClip& clip) {
43 SkMask mask;
44 SkBitmap bm;
rmistry@google.comae933ce2012-08-23 18:19:56 +000045
reed@google.com32287892011-10-05 16:27:44 +000046 clip.copyToMask(&mask);
reed@google.com045e62d2011-10-24 12:19:46 +000047 SkAutoMaskFreeImage amfi(mask.fImage);
48
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000049 bm.installMaskPixels(mask);
reed@google.com32287892011-10-05 16:27:44 +000050
51 SkPaint paint;
bsalomon@google.com820e80a2011-10-24 21:09:40 +000052 canvas->drawBitmap(bm,
53 SK_Scalar1 * mask.fBounds.fLeft,
54 SK_Scalar1 * mask.fBounds.fTop,
55 &paint);
reed@google.com32287892011-10-05 16:27:44 +000056}
57
58class AAClipView : public SampleView {
59public:
60 AAClipView() {
reed@google.comc9041912011-10-27 16:58:46 +000061 testop();
reed@google.com32287892011-10-05 16:27:44 +000062 }
63
64protected:
65 // overrides from SkEventSink
66 virtual bool onQuery(SkEvent* evt) {
67 if (SampleCode::TitleQ(*evt)) {
68 SampleCode::TitleR(evt, "AAClip");
69 return true;
70 }
71 return this->INHERITED::onQuery(evt);
72 }
73
74 virtual void onDrawContent(SkCanvas* canvas) {
75#if 1
76 SkAAClip aaclip;
77 SkPath path;
78 SkRect bounds;
rmistry@google.comae933ce2012-08-23 18:19:56 +000079
reed@google.com32287892011-10-05 16:27:44 +000080 bounds.set(0, 0, 20, 20);
81 bounds.inset(SK_ScalarHalf, SK_ScalarHalf);
82
83// path.addRect(bounds);
84// path.addOval(bounds);
85 path.addRoundRect(bounds, 4, 4);
86 aaclip.setPath(path);
87 canvas->translate(30, 30);
88 drawClip(canvas, aaclip);
89
90 SkAAClip aaclip2;
91 path.offset(10, 10);
92 aaclip2.setPath(path);
93 canvas->translate(30, 0);
94 drawClip(canvas, aaclip2);
95
96 SkAAClip aaclip3;
97 aaclip3.op(aaclip, aaclip2, SkRegion::kIntersect_Op);
98 canvas->translate(30, 0);
99 drawClip(canvas, aaclip3);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
reed@google.com32287892011-10-05 16:27:44 +0000101#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
reed@google.com32287892011-10-05 16:27:44 +0000103#if 0
104 SkRect r;
105 r.set(0, 0, this->width(), this->height());
106 r.inset(20, 20);
107 canvas->clipRect(r);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reed@google.com32287892011-10-05 16:27:44 +0000109 SkPath path;
110 path.addRect(r);
111 SkPaint paint;
112 paint.setAntiAlias(true);
113 paint.setColor(SK_ColorRED);
114 canvas->drawPath(path, paint);
115#endif
116 }
117
118private:
119 typedef SkView INHERITED;
120};
121
122//////////////////////////////////////////////////////////////////////////////
123
124static SkView* MyFactory() { return new AAClipView; }
125static SkViewRegister reg(MyFactory);