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