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 "SkRandom.h"
     10 #include "SkRRect.h"
     11 
     12 namespace skiagm {
     13 
     14 // Test out various combinations of nested rects, ovals and rrects.
     15 class NestedGM : public GM {
     16 public:
     17     NestedGM(bool doAA) : fDoAA(doAA) {
     18         this->setBGColor(0xFFDDDDDD);
     19     }
     20 
     21 protected:
     22     virtual uint32_t onGetFlags() const SK_OVERRIDE {
     23         return kSkipTiled_Flag;
     24     }
     25 
     26     virtual SkString onShortName() SK_OVERRIDE {
     27         SkString name("nested");
     28         if (fDoAA) {
     29             name.append("_aa");
     30         } else {
     31             name.append("_bw");
     32         }
     33         return name;
     34     }
     35 
     36     virtual SkISize onISize() SK_OVERRIDE {
     37         return SkISize::Make(kImageWidth, kImageHeight);
     38     }
     39 
     40     enum Shapes {
     41         kRect_Shape = 0,
     42         kRRect_Shape,
     43         kOval_Shape,
     44         kShapeCount
     45     };
     46 
     47     static void AddShape(SkPath* path, const SkRect& rect, Shapes shape, SkPath::Direction dir) {
     48         switch (shape) {
     49             case kRect_Shape:
     50                 path->addRect(rect, dir);
     51                 break;
     52             case kRRect_Shape: {
     53                 SkRRect rr;
     54                 rr.setRectXY(rect, 5, 5);
     55                 path->addRRect(rr, dir);
     56                 break;
     57                 }
     58             case kOval_Shape:
     59                 path->addOval(rect, dir);
     60                 break;
     61             default:
     62                 break;
     63         }
     64     }
     65 
     66     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
     67 
     68         SkPaint shapePaint;
     69         shapePaint.setColor(SK_ColorBLACK);
     70         shapePaint.setAntiAlias(fDoAA);
     71 
     72         SkRect outerRect = SkRect::MakeWH(40, 40);
     73 
     74         SkRect innerRects[] = {
     75             { 10, 10, 30, 30 },     // small
     76             { .5f, 18, 4.5f, 22 }   // smaller and offset to left
     77         };
     78 
     79         // draw a background pattern to make transparency errors more apparent
     80         SkRandom rand;
     81 
     82         for (int y = 0; y < kImageHeight; y += 10) {
     83             for (int x = 0; x < kImageWidth; x += 10) {
     84                 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x),
     85                                             SkIntToScalar(y),
     86                                             10, 10);
     87                 SkPaint p;
     88                 p.setColor(rand.nextU() | 0xFF000000);
     89                 canvas->drawRect(r, p);
     90             }
     91         }
     92 
     93         canvas->translate(2, 2);
     94         for (int outerShape = 0; outerShape < kShapeCount; ++outerShape) {
     95             canvas->save();
     96             for (int innerShape = 0; innerShape < kShapeCount; ++innerShape) {
     97                 for (size_t innerRect = 0; innerRect < SK_ARRAY_COUNT(innerRects); ++innerRect) {
     98                     SkPath path;
     99 
    100                     AddShape(&path, outerRect, (Shapes) outerShape, SkPath::kCW_Direction);
    101                     AddShape(&path, innerRects[innerRect], (Shapes) innerShape,
    102                              SkPath::kCCW_Direction);
    103 
    104                     canvas->drawPath(path, shapePaint);
    105                     canvas->translate(45, 0);
    106                 }
    107             }
    108             canvas->restore();
    109             canvas->translate(0, 45);
    110         }
    111 
    112     }
    113 
    114 private:
    115     static const int kImageWidth = 269;
    116     static const int kImageHeight = 134;
    117 
    118     bool fDoAA;
    119 
    120     typedef GM INHERITED;
    121 };
    122 
    123 ///////////////////////////////////////////////////////////////////////////////
    124 
    125 DEF_GM( return new NestedGM(true); )
    126 DEF_GM( return new NestedGM(false); )
    127 
    128 
    129 }
    130