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