robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 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: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 22 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 23 | SkString onShortName() SK_OVERRIDE { |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 24 | SkString name("nested"); |
| 25 | if (fDoAA) { |
| 26 | name.append("_aa"); |
| 27 | } else { |
| 28 | name.append("_bw"); |
| 29 | } |
| 30 | return name; |
| 31 | } |
| 32 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 33 | SkISize onISize() SK_OVERRIDE { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 34 | return SkISize::Make(kImageWidth, kImageHeight); |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 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 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 63 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 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 |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 77 | SkRandom rand; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 78 | |
| 79 | for (int y = 0; y < kImageHeight; y += 10) { |
| 80 | for (int x = 0; x < kImageWidth; x += 10) { |
skia.committer@gmail.com | b74bdf0 | 2013-08-21 07:01:29 +0000 | [diff] [blame] | 81 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), |
| 82 | SkIntToScalar(y), |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 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); |
skia.committer@gmail.com | b74bdf0 | 2013-08-21 07:01:29 +0000 | [diff] [blame] | 98 | AddShape(&path, innerRects[innerRect], (Shapes) innerShape, |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 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 | } |