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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkPaint.h" |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame^] | 12 | #include "include/core/SkPathBuilder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkRRect.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/utils/SkRandom.h" |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 20 | |
| 21 | namespace skiagm { |
| 22 | |
| 23 | // Test out various combinations of nested rects, ovals and rrects. |
| 24 | class NestedGM : public GM { |
| 25 | public: |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 26 | NestedGM(bool doAA, bool flipped) : fDoAA(doAA), fFlipped(flipped) { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 27 | this->setBGColor(0xFFDDDDDD); |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 31 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 32 | SkString onShortName() override { |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 33 | SkString name("nested"); |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 34 | if (fFlipped) { |
| 35 | name.append("_flipY"); |
| 36 | } |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 37 | if (fDoAA) { |
| 38 | name.append("_aa"); |
| 39 | } else { |
| 40 | name.append("_bw"); |
| 41 | } |
| 42 | return name; |
| 43 | } |
| 44 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 45 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 46 | return SkISize::Make(kImageWidth, kImageHeight); |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | enum Shapes { |
| 50 | kRect_Shape = 0, |
| 51 | kRRect_Shape, |
| 52 | kOval_Shape, |
| 53 | kShapeCount |
| 54 | }; |
| 55 | |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame^] | 56 | static void AddShape(SkPathBuilder* b, const SkRect& rect, Shapes shape, SkPathDirection dir) { |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 57 | switch (shape) { |
| 58 | case kRect_Shape: |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame^] | 59 | b->addRect(rect, dir); |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 60 | break; |
| 61 | case kRRect_Shape: { |
| 62 | SkRRect rr; |
| 63 | rr.setRectXY(rect, 5, 5); |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame^] | 64 | b->addRRect(rr, dir); |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 65 | break; |
| 66 | } |
| 67 | case kOval_Shape: |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame^] | 68 | b->addOval(rect, dir); |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 69 | break; |
| 70 | default: |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 75 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 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 |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 89 | SkRandom rand; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 90 | |
| 91 | for (int y = 0; y < kImageHeight; y += 10) { |
| 92 | for (int x = 0; x < kImageWidth; x += 10) { |
skia.committer@gmail.com | b74bdf0 | 2013-08-21 07:01:29 +0000 | [diff] [blame] | 93 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), |
| 94 | SkIntToScalar(y), |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 95 | 10, 10); |
| 96 | SkPaint p; |
| 97 | p.setColor(rand.nextU() | 0xFF000000); |
| 98 | canvas->drawRect(r, p); |
| 99 | } |
| 100 | } |
| 101 | |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 102 | SkScalar xOff = 2, yOff = 2; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 103 | for (int outerShape = 0; outerShape < kShapeCount; ++outerShape) { |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 104 | for (int innerShape = 0; innerShape < kShapeCount; ++innerShape) { |
| 105 | for (size_t innerRect = 0; innerRect < SK_ARRAY_COUNT(innerRects); ++innerRect) { |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame^] | 106 | SkPathBuilder builder; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 107 | |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame^] | 108 | AddShape(&builder, outerRect, (Shapes) outerShape, SkPathDirection::kCW); |
| 109 | AddShape(&builder, innerRects[innerRect], (Shapes) innerShape, |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 110 | SkPathDirection::kCCW); |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 111 | |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 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 | |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame^] | 120 | canvas->drawPath(builder.detach(), shapePaint); |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 121 | canvas->restore(); |
| 122 | |
| 123 | xOff += 45; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 126 | |
| 127 | xOff = 2; |
| 128 | yOff += 45; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | } |
| 132 | |
| 133 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 134 | static constexpr int kImageWidth = 269; |
| 135 | static constexpr int kImageHeight = 134; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 136 | |
| 137 | bool fDoAA; |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 138 | bool fFlipped; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 139 | |
| 140 | typedef GM INHERITED; |
| 141 | }; |
| 142 | |
| 143 | /////////////////////////////////////////////////////////////////////////////// |
| 144 | |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 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); ) |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 149 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 150 | } // namespace skiagm |