Home | History | Annotate | Download | only in gm
      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 public:
     17     CircularClipsGM() {
     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 protected:
     28     virtual uint32_t onGetFlags() const SK_OVERRIDE {
     29         return kSkipTiled_Flag | kAsBench_Flag;
     30     }
     31 
     32     virtual SkString onShortName() {
     33         return SkString("circular-clips");
     34     }
     35 
     36     virtual SkISize onISize() {
     37         return SkISize::Make(800, 600);
     38     }
     39 
     40     virtual void onDraw(SkCanvas* canvas) {
     41         SkRegion::Op ops[] = {
     42             SkRegion::kDifference_Op,
     43             SkRegion::kIntersect_Op,
     44             SkRegion::kUnion_Op,
     45             SkRegion::kXOR_Op,
     46             SkRegion::kReverseDifference_Op,
     47             SkRegion::kReplace_Op,
     48         };
     49 
     50         SkRect rect = SkRect::MakeLTRB(fX1 - fR, fY - fR, fX2 + fR, fY + fR);
     51 
     52         SkPaint fillPaint;
     53 
     54         for (size_t i = 0; i < 4; i++) {
     55             fCircle1.toggleInverseFillType();
     56             if (i % 2 == 0) {
     57                 fCircle2.toggleInverseFillType();
     58             }
     59 
     60             canvas->save();
     61             for (size_t op = 0; op < SK_ARRAY_COUNT(ops); op++) {
     62                 canvas->save();
     63 
     64                 canvas->clipPath(fCircle1, SkRegion::kReplace_Op);
     65                 canvas->clipPath(fCircle2, ops[op]);
     66 
     67                 canvas->drawRect(rect, fillPaint);
     68 
     69                 canvas->restore();
     70                 canvas->translate(0, 2 * fY);
     71             }
     72             canvas->restore();
     73             canvas->translate(fX1 + fX2, 0);
     74         }
     75     }
     76 
     77 private:
     78     typedef skiagm::GM INHERITED;
     79 };
     80 
     81 //////////////////////////////////////////////////////////////////////////////
     82 
     83 DEF_GM( return new CircularClipsGM; )
     84