blob: 46e4fe267ac07801683b8ae40935f8e03a7f58d3 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkPath.h"
10#include "samplecode/Sample.h"
11#include "src/core/SkAAClip.h"
reed@google.com32287892011-10-05 16:27:44 +000012
reed@google.comc9041912011-10-27 16:58:46 +000013static 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +000019
humper@google.com0e515772013-01-07 19:54:40 +000020 SkDEBUGCODE(SkIRect r2 = c2.getBounds());
reed@google.comc9041912011-10-27 16:58:46 +000021 SkASSERT(r2 == expectedR);
22}
23
24static 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
35static 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
reed@google.com32287892011-10-05 16:27:44 +000041static void drawClip(SkCanvas* canvas, const SkAAClip& clip) {
42 SkMask mask;
43 SkBitmap bm;
rmistry@google.comae933ce2012-08-23 18:19:56 +000044
reed@google.com32287892011-10-05 16:27:44 +000045 clip.copyToMask(&mask);
reed@google.com045e62d2011-10-24 12:19:46 +000046 SkAutoMaskFreeImage amfi(mask.fImage);
47
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000048 bm.installMaskPixels(mask);
reed@google.com32287892011-10-05 16:27:44 +000049
50 SkPaint paint;
bsalomon@google.com820e80a2011-10-24 21:09:40 +000051 canvas->drawBitmap(bm,
52 SK_Scalar1 * mask.fBounds.fLeft,
53 SK_Scalar1 * mask.fBounds.fTop,
54 &paint);
reed@google.com32287892011-10-05 16:27:44 +000055}
56
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040057class AAClipView : public Sample {
Hal Canaryd7639af2019-07-17 09:08:11 -040058 SkString name() override { return SkString("AAClip"); }
reed@google.com32287892011-10-05 16:27:44 +000059
Hal Canaryd7639af2019-07-17 09:08:11 -040060 void onOnceBeforeDraw() override { testop(); }
reed@google.com32287892011-10-05 16:27:44 +000061
Hal Canaryd7639af2019-07-17 09:08:11 -040062 void onDrawContent(SkCanvas* canvas) override {
reed@google.com32287892011-10-05 16:27:44 +000063#if 1
64 SkAAClip aaclip;
65 SkPath path;
66 SkRect bounds;
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
Mike Reed92b33352019-08-24 19:39:13 -040068 bounds.setLTRB(0, 0, 20, 20);
reed@google.com32287892011-10-05 16:27:44 +000069 bounds.inset(SK_ScalarHalf, SK_ScalarHalf);
70
71// path.addRect(bounds);
72// path.addOval(bounds);
Mike Reed4241f5e2019-09-14 19:13:23 +000073 path.addRoundRect(bounds, 4, 4);
reed@google.com32287892011-10-05 16:27:44 +000074 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
reed@google.com32287892011-10-05 16:27:44 +000089#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +000090
reed@google.com32287892011-10-05 16:27:44 +000091#if 0
92 SkRect r;
93 r.set(0, 0, this->width(), this->height());
94 r.inset(20, 20);
95 canvas->clipRect(r);
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
reed@google.com32287892011-10-05 16:27:44 +000097 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 }
reed@google.com32287892011-10-05 16:27:44 +0000105};
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400106DEF_SAMPLE( return new AAClipView(); )