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