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 namespace skiagm {
     13 
     14 static SkPath generate_square(SkScalar cx, SkScalar cy, SkScalar w) {
     15     SkRect rect = SkRect::MakeXYWH(cx - w / 2, cy - w / 2, w, w);
     16     SkPath path;
     17     path.addRect(rect);
     18     return path;
     19 }
     20 
     21 static SkPath generate_rect_line(SkScalar cx, SkScalar cy, SkScalar l) {
     22     SkRect rect = SkRect::MakeXYWH(cx - l / 2, cy, l, 0);
     23     SkPath path;
     24     path.addRect(rect);
     25     return path;
     26 }
     27 
     28 static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d) {
     29     SkPath path;
     30     path.addCircle(cx, cy, d/2, SkPath::kCW_Direction);
     31     return path;
     32 }
     33 
     34 static SkPath generate_line(SkScalar cx, SkScalar cy, SkScalar l) {
     35     SkPath path;
     36     path.moveTo(cx - l / 2, cy);
     37     path.lineTo(cx + l / 2, cy);
     38     return path;
     39 }
     40 
     41 SkPaint::Style styles[] = {
     42         SkPaint::kStroke_Style,
     43         SkPaint::kStrokeAndFill_Style,
     44         SkPaint::kFill_Style
     45 };
     46 SkScalar pathSizes[] = {
     47         40,
     48         10,
     49         0
     50 };
     51 SkScalar strokeWidths[] = {
     52         10,
     53         0
     54 };
     55 SkPath ((*paths[])(SkScalar, SkScalar, SkScalar)) = {
     56         generate_square,
     57         generate_rect_line,
     58         generate_circle,
     59         generate_line
     60 };
     61 
     62 const SkScalar slideWidth = 90, slideHeight = 90;
     63 const SkScalar slideBoundary = 5;
     64 
     65 
     66 class InversePathsGM : public GM {
     67 public:
     68     InversePathsGM() {
     69 
     70     }
     71 
     72 protected:
     73     virtual uint32_t onGetFlags() const SK_OVERRIDE {
     74         return kSkipTiled_Flag;
     75     }
     76 
     77     virtual SkString onShortName() {
     78         return SkString("inverse_paths");
     79     }
     80 
     81     virtual SkISize onISize() {
     82         return SkISize::Make(800, 900);
     83     }
     84 
     85     virtual void onDraw(SkCanvas* canvas) {
     86         SkScalar cx = slideWidth / 2 + slideBoundary;
     87         SkScalar cy = slideHeight / 2 + slideBoundary;
     88         SkScalar dx = slideWidth + 2 * slideBoundary;
     89         SkScalar dy = slideHeight + 2 * slideBoundary;
     90 
     91         SkRect clipRect = SkRect::MakeLTRB(slideBoundary, slideBoundary,
     92                                            slideBoundary + slideWidth,
     93                                            slideBoundary + slideHeight);
     94         SkPaint clipPaint;
     95         clipPaint.setStyle(SkPaint::kStroke_Style);
     96         clipPaint.setStrokeWidth(SkIntToScalar(2));
     97 
     98         SkPaint outlinePaint;
     99         outlinePaint.setColor(0x40000000);
    100         outlinePaint.setStyle(SkPaint::kStroke_Style);
    101         outlinePaint.setStrokeWidth(SkIntToScalar(0));
    102 
    103         for (size_t styleIndex = 0; styleIndex < SK_ARRAY_COUNT(styles);
    104                 styleIndex++) {
    105             for (size_t sizeIndex = 0; sizeIndex < SK_ARRAY_COUNT(pathSizes);
    106                     sizeIndex++) {
    107                 SkScalar size = pathSizes[sizeIndex];
    108 
    109                 canvas->save();
    110 
    111                 for (size_t widthIndex = 0;
    112                         widthIndex < SK_ARRAY_COUNT(strokeWidths);
    113                         widthIndex++) {
    114                     SkPaint paint;
    115                     paint.setColor(0xff007000);
    116                     paint.setStrokeWidth(strokeWidths[widthIndex]);
    117                     paint.setStyle(styles[styleIndex]);
    118 
    119                     for (size_t pathIndex = 0;
    120                             pathIndex < SK_ARRAY_COUNT(paths);
    121                             pathIndex++) {
    122                         canvas->drawRect(clipRect, clipPaint);
    123 
    124                         canvas->save();
    125                         canvas->clipRect(clipRect);
    126 
    127                         SkPath path = paths[pathIndex](cx, cy, size);
    128                         path.setFillType(SkPath::kInverseWinding_FillType);
    129                         canvas->drawPath(path, paint);
    130 
    131                         path.setFillType(SkPath::kWinding_FillType);
    132                         canvas->drawPath(path, outlinePaint);
    133 
    134                         canvas->restore();
    135                         canvas->translate(dx, 0);
    136                     }
    137                 }
    138 
    139                 canvas->restore();
    140                 canvas->translate(0, dy);
    141             }
    142         }
    143     }
    144 
    145 private:
    146     typedef GM INHERITED;
    147 };
    148 
    149 DEF_GM( return new InversePathsGM; )
    150 }
    151