• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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 "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkClipOp.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkString.h"
17 #include "include/core/SkTypes.h"
18 
19 class CircularClipsGM : public skiagm::GM {
20     SkScalar fX1, fX2, fY, fR;
21     SkPath   fCircle1, fCircle2;
22 
23 protected:
onOnceBeforeDraw()24     void onOnceBeforeDraw() override {
25         fX1 = 80;
26         fX2 = 120;
27         fY = 50;
28         fR = 40;
29 
30         fCircle1 = SkPath::Circle(fX1, fY, fR, SkPathDirection::kCW);
31         fCircle2 = SkPath::Circle(fX2, fY, fR, SkPathDirection::kCW);
32     }
33 
34 
runAsBench() const35     bool runAsBench() const override { return true; }
36 
onShortName()37     SkString onShortName() override {
38         return SkString("circular-clips");
39     }
40 
onISize()41     SkISize onISize() override {
42         return SkISize::Make(800, 200);
43     }
44 
onDraw(SkCanvas * canvas)45     void onDraw(SkCanvas* canvas) override {
46         const SkClipOp ops[] = {
47             SkClipOp::kDifference,
48             SkClipOp::kIntersect
49         };
50 
51         SkRect rect = SkRect::MakeLTRB(fX1 - fR, fY - fR, fX2 + fR, fY + fR);
52 
53         SkPaint fillPaint;
54 
55         // Giant background circular clips (AA, non-inverted, replace/isect)
56         fillPaint.setColor(0x80808080);
57         canvas->save();
58         canvas->scale(10, 10);
59         canvas->translate(-((fX1 + fX2)/2 - fR), -(fY - 2*fR/3));
60         canvas->clipPath(fCircle1, true);
61         canvas->clipPath(fCircle2, true);
62 
63         canvas->drawRect(rect, fillPaint);
64 
65         canvas->restore();
66 
67         fillPaint.setColor(0xFF000000);
68 
69         for (size_t i = 0; i < 4; i++) {
70             fCircle1.toggleInverseFillType();
71             if (i % 2 == 0) {
72                 fCircle2.toggleInverseFillType();
73             }
74 
75             canvas->save();
76             for (size_t op = 0; op < SK_ARRAY_COUNT(ops); op++) {
77                 canvas->save();
78 
79                 canvas->clipPath(fCircle1);
80                 canvas->clipPath(fCircle2, ops[op]);
81 
82                 canvas->drawRect(rect, fillPaint);
83 
84                 canvas->restore();
85                 canvas->translate(0, 2 * fY);
86             }
87             canvas->restore();
88             canvas->translate(fX1 + fX2, 0);
89         }
90     }
91 
92 private:
93     using INHERITED = skiagm::GM;
94 };
95 
96 //////////////////////////////////////////////////////////////////////////////
97 
98 DEF_GM( return new CircularClipsGM; )
99