• 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 #include "src/core/SkClipOpPriv.h"
19 
20 class CircularClipsGM : public skiagm::GM {
21     SkScalar fX1, fX2, fY, fR;
22     SkPath   fCircle1, fCircle2;
23 
24 protected:
onOnceBeforeDraw()25     void onOnceBeforeDraw() override {
26         fX1 = 80;
27         fX2 = 120;
28         fY = 50;
29         fR = 40;
30 
31         fCircle1.addCircle(fX1, fY, fR, SkPath::kCW_Direction);
32         fCircle2.addCircle(fX2, fY, fR, SkPath::kCW_Direction);
33     }
34 
35 
runAsBench() const36     bool runAsBench() const override { return true; }
37 
onShortName()38     SkString onShortName() override {
39         return SkString("circular-clips");
40     }
41 
onISize()42     SkISize onISize() override {
43         return SkISize::Make(800, 600);
44     }
45 
onDraw(SkCanvas * canvas)46     void onDraw(SkCanvas* canvas) override {
47         const SkClipOp ops[] = {
48             kDifference_SkClipOp,
49             kIntersect_SkClipOp,
50             kUnion_SkClipOp,
51             kXOR_SkClipOp,
52             kReverseDifference_SkClipOp,
53             kReplace_SkClipOp,
54         };
55 
56         SkRect rect = SkRect::MakeLTRB(fX1 - fR, fY - fR, fX2 + fR, fY + fR);
57 
58         SkPaint fillPaint;
59 
60         // Giant background circular clips (AA, non-inverted, replace/isect)
61         fillPaint.setColor(0x80808080);
62         canvas->save();
63         canvas->scale(10, 10);
64         canvas->translate(-((fX1 + fX2)/2 - fR), -(fY - 2*fR/3));
65         canvas->clipPath(fCircle1, true);
66         canvas->clipPath(fCircle2, true);
67 
68         canvas->drawRect(rect, fillPaint);
69 
70         canvas->restore();
71 
72         fillPaint.setColor(0xFF000000);
73 
74         for (size_t i = 0; i < 4; i++) {
75             fCircle1.toggleInverseFillType();
76             if (i % 2 == 0) {
77                 fCircle2.toggleInverseFillType();
78             }
79 
80             canvas->save();
81             for (size_t op = 0; op < SK_ARRAY_COUNT(ops); op++) {
82                 canvas->save();
83 
84                 canvas->clipPath(fCircle1);
85                 canvas->clipPath(fCircle2, ops[op]);
86 
87                 canvas->drawRect(rect, fillPaint);
88 
89                 canvas->restore();
90                 canvas->translate(0, 2 * fY);
91             }
92             canvas->restore();
93             canvas->translate(fX1 + fX2, 0);
94         }
95     }
96 
97 private:
98     typedef skiagm::GM INHERITED;
99 };
100 
101 //////////////////////////////////////////////////////////////////////////////
102 
103 DEF_GM( return new CircularClipsGM; )
104