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