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 "sk_tool_utils.h"
     10 #include "SkBitmap.h"
     11 #include "SkPath.h"
     12 #include "SkPathOps.h"
     13 #include "SkRect.h"
     14 
     15 namespace skiagm {
     16 
     17 class PathOpsInverseGM : public GM {
     18 public:
     19     PathOpsInverseGM() {
     20     }
     21 
     22 protected:
     23     void onOnceBeforeDraw() override {
     24         const unsigned oneColor = sk_tool_utils::color_to_565(0xFF8080FF);
     25         const unsigned twoColor = 0x807F1f1f;
     26         SkColor blendColor = blend(oneColor, twoColor);
     27         makePaint(&fOnePaint, oneColor);
     28         makePaint(&fTwoPaint, twoColor);
     29         makePaint(&fOpPaint[kDifference_SkPathOp], oneColor);
     30         makePaint(&fOpPaint[kIntersect_SkPathOp], blendColor);
     31         makePaint(&fOpPaint[kUnion_SkPathOp], sk_tool_utils::color_to_565(0xFFc0FFc0));
     32         makePaint(&fOpPaint[kReverseDifference_SkPathOp], twoColor);
     33         makePaint(&fOpPaint[kXOR_SkPathOp], sk_tool_utils::color_to_565(0xFFa0FFe0));
     34         makePaint(&fOutlinePaint, 0xFF000000);
     35         fOutlinePaint.setStyle(SkPaint::kStroke_Style);
     36     }
     37 
     38     SkColor blend(SkColor one, SkColor two) {
     39         SkBitmap temp;
     40         temp.allocN32Pixels(1, 1);
     41         SkCanvas canvas(temp);
     42         canvas.drawColor(one);
     43         canvas.drawColor(two);
     44         void* pixels = temp.getPixels();
     45         return *(SkColor*) pixels;
     46     }
     47 
     48     void makePaint(SkPaint* paint, SkColor color) {
     49         paint->setAntiAlias(true);
     50         paint->setStyle(SkPaint::kFill_Style);
     51         paint->setColor(color);
     52     }
     53 
     54     SkString onShortName() override {
     55         return SkString("pathopsinverse");
     56     }
     57 
     58     SkISize onISize() override {
     59         return SkISize::Make(1200, 900);
     60     }
     61 
     62     void onDraw(SkCanvas* canvas) override {
     63         SkPath one, two;
     64         int yPos = 0;
     65         for (int oneFill = 0; oneFill <= 1; ++oneFill) {
     66             SkPath::FillType oneF = oneFill ? SkPath::kInverseEvenOdd_FillType
     67                     : SkPath::kEvenOdd_FillType;
     68             for (int twoFill = 0; twoFill <= 1; ++twoFill) {
     69                 SkPath::FillType twoF = twoFill ? SkPath::kInverseEvenOdd_FillType
     70                         : SkPath::kEvenOdd_FillType;
     71                 one.reset();
     72                 one.setFillType(oneF);
     73                 one.addRect(10, 10, 70, 70);
     74                 two.reset();
     75                 two.setFillType(twoF);
     76                 two.addRect(40, 40, 100, 100);
     77                 canvas->save();
     78                 canvas->translate(0, SkIntToScalar(yPos));
     79                 canvas->clipRect(SkRect::MakeWH(110, 110), true);
     80                 canvas->drawPath(one, fOnePaint);
     81                 canvas->drawPath(one, fOutlinePaint);
     82                 canvas->drawPath(two, fTwoPaint);
     83                 canvas->drawPath(two, fOutlinePaint);
     84                 canvas->restore();
     85                 int xPos = 150;
     86                 for (int op = kDifference_SkPathOp; op <= kReverseDifference_SkPathOp; ++op) {
     87                     SkPath result;
     88                     Op(one, two, (SkPathOp) op, &result);
     89                     canvas->save();
     90                     canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos));
     91                     canvas->clipRect(SkRect::MakeWH(110, 110), true);
     92                     canvas->drawPath(result, fOpPaint[op]);
     93                     canvas->drawPath(result, fOutlinePaint);
     94                     canvas->restore();
     95                     xPos += 150;
     96                 }
     97                 yPos += 150;
     98             }
     99         }
    100     }
    101 
    102 private:
    103     SkPaint fOnePaint;
    104     SkPaint fTwoPaint;
    105     SkPaint fOutlinePaint;
    106     SkPaint fOpPaint[kReverseDifference_SkPathOp - kDifference_SkPathOp + 1];
    107     typedef GM INHERITED;
    108 };
    109 
    110 //////////////////////////////////////////////////////////////////////////////
    111 
    112 DEF_GM( return new PathOpsInverseGM; )
    113 
    114 }
    115