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