blob: 78baf3d5545065aa2143ab08d50b42086a776c4e [file] [log] [blame]
reed@google.com32287892011-10-05 16:27:44 +00001
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"
reedfb8c1fc2015-08-04 18:44:56 -070010#include "SkAAClip.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkCanvas.h"
12#include "SkPath.h"
13#include "SkView.h"
reed@google.com32287892011-10-05 16:27:44 +000014
reed@google.comc9041912011-10-27 16:58:46 +000015static 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +000021
humper@google.com0e515772013-01-07 19:54:40 +000022 SkDEBUGCODE(SkIRect r2 = c2.getBounds());
reed@google.comc9041912011-10-27 16:58:46 +000023 SkASSERT(r2 == expectedR);
24}
25
26static 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
37static 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
reed@google.com32287892011-10-05 16:27:44 +000043static void drawClip(SkCanvas* canvas, const SkAAClip& clip) {
44 SkMask mask;
45 SkBitmap bm;
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
reed@google.com32287892011-10-05 16:27:44 +000047 clip.copyToMask(&mask);
reed@google.com045e62d2011-10-24 12:19:46 +000048 SkAutoMaskFreeImage amfi(mask.fImage);
49
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000050 bm.installMaskPixels(mask);
reed@google.com32287892011-10-05 16:27:44 +000051
52 SkPaint paint;
bsalomon@google.com820e80a2011-10-24 21:09:40 +000053 canvas->drawBitmap(bm,
54 SK_Scalar1 * mask.fBounds.fLeft,
55 SK_Scalar1 * mask.fBounds.fTop,
56 &paint);
reed@google.com32287892011-10-05 16:27:44 +000057}
58
59class AAClipView : public SampleView {
60public:
61 AAClipView() {
reed@google.comc9041912011-10-27 16:58:46 +000062 testop();
reed@google.com32287892011-10-05 16:27:44 +000063 }
64
65protected:
66 // overrides from SkEventSink
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
75 virtual void onDrawContent(SkCanvas* canvas) {
76#if 1
77 SkAAClip aaclip;
78 SkPath path;
79 SkRect bounds;
rmistry@google.comae933ce2012-08-23 18:19:56 +000080
reed@google.com32287892011-10-05 16:27:44 +000081 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000101
reed@google.com32287892011-10-05 16:27:44 +0000102#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000103
reed@google.com32287892011-10-05 16:27:44 +0000104#if 0
105 SkRect r;
106 r.set(0, 0, this->width(), this->height());
107 r.inset(20, 20);
108 canvas->clipRect(r);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000109
reed@google.com32287892011-10-05 16:27:44 +0000110 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
119private:
120 typedef SkView INHERITED;
121};
122
123//////////////////////////////////////////////////////////////////////////////
124
125static SkView* MyFactory() { return new AAClipView; }
126static SkViewRegister reg(MyFactory);