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