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" |
Kevin Lubick | 0d4d114 | 2023-02-13 09:13:10 -0500 | [diff] [blame] | 19 | #include "src/base/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: |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 31 | SkString getName() const override { |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 32 | SkString name("nested"); |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 33 | if (fFlipped) { |
| 34 | name.append("_flipY"); |
| 35 | } |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 36 | if (fDoAA) { |
| 37 | name.append("_aa"); |
| 38 | } else { |
| 39 | name.append("_bw"); |
| 40 | } |
| 41 | return name; |
| 42 | } |
| 43 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 44 | SkISize getISize() override { return SkISize::Make(kImageWidth, kImageHeight); } |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 45 | |
| 46 | enum Shapes { |
| 47 | kRect_Shape = 0, |
| 48 | kRRect_Shape, |
| 49 | kOval_Shape, |
| 50 | kShapeCount |
| 51 | }; |
| 52 | |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 53 | 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] | 54 | switch (shape) { |
| 55 | case kRect_Shape: |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 56 | b->addRect(rect, dir); |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 57 | break; |
| 58 | case kRRect_Shape: { |
| 59 | SkRRect rr; |
| 60 | rr.setRectXY(rect, 5, 5); |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 61 | b->addRRect(rr, dir); |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 62 | break; |
| 63 | } |
| 64 | case kOval_Shape: |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 65 | b->addOval(rect, dir); |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 66 | break; |
| 67 | default: |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 73 | |
| 74 | SkPaint shapePaint; |
| 75 | shapePaint.setColor(SK_ColorBLACK); |
| 76 | shapePaint.setAntiAlias(fDoAA); |
| 77 | |
| 78 | SkRect outerRect = SkRect::MakeWH(40, 40); |
| 79 | |
| 80 | SkRect innerRects[] = { |
| 81 | { 10, 10, 30, 30 }, // small |
| 82 | { .5f, 18, 4.5f, 22 } // smaller and offset to left |
| 83 | }; |
| 84 | |
| 85 | // draw a background pattern to make transparency errors more apparent |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 86 | SkRandom rand; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 87 | |
| 88 | for (int y = 0; y < kImageHeight; y += 10) { |
| 89 | for (int x = 0; x < kImageWidth; x += 10) { |
skia.committer@gmail.com | b74bdf0 | 2013-08-21 07:01:29 +0000 | [diff] [blame] | 90 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), |
| 91 | SkIntToScalar(y), |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 92 | 10, 10); |
| 93 | SkPaint p; |
| 94 | p.setColor(rand.nextU() | 0xFF000000); |
| 95 | canvas->drawRect(r, p); |
| 96 | } |
| 97 | } |
| 98 | |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 99 | SkScalar xOff = 2, yOff = 2; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 100 | for (int outerShape = 0; outerShape < kShapeCount; ++outerShape) { |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 101 | for (int innerShape = 0; innerShape < kShapeCount; ++innerShape) { |
Herb Derby | c37b386 | 2022-06-21 09:49:17 -0400 | [diff] [blame] | 102 | for (size_t innerRect = 0; innerRect < std::size(innerRects); ++innerRect) { |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 103 | SkPathBuilder builder; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 104 | |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 105 | AddShape(&builder, outerRect, (Shapes) outerShape, SkPathDirection::kCW); |
| 106 | AddShape(&builder, innerRects[innerRect], (Shapes) innerShape, |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 107 | SkPathDirection::kCCW); |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 108 | |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 109 | canvas->save(); |
| 110 | if (fFlipped) { |
| 111 | canvas->scale(1.0f, -1.0f); |
| 112 | canvas->translate(xOff, -yOff - 40.0f); |
| 113 | } else { |
| 114 | canvas->translate(xOff, yOff); |
| 115 | } |
| 116 | |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 117 | canvas->drawPath(builder.detach(), shapePaint); |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 118 | canvas->restore(); |
| 119 | |
| 120 | xOff += 45; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 123 | |
| 124 | xOff = 2; |
| 125 | yOff += 45; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | private: |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 131 | inline static constexpr int kImageWidth = 269; |
| 132 | inline static constexpr int kImageHeight = 134; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 133 | |
| 134 | bool fDoAA; |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 135 | bool fFlipped; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 136 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 137 | using INHERITED = GM; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | /////////////////////////////////////////////////////////////////////////////// |
| 141 | |
robertphillips | 2e5b4c5 | 2015-05-19 04:35:38 -0700 | [diff] [blame] | 142 | DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ false); ) |
| 143 | DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ false); ) |
| 144 | DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ true); ) |
| 145 | DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ true); ) |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 146 | |
Michael Ludwig | 144926d | 2021-09-10 09:34:47 -0400 | [diff] [blame] | 147 | DEF_SIMPLE_GM(nested_hairline_square, canvas, 64, 64) { |
| 148 | // See crbug.com/1234194 - This should draw 1 row of 3 stroked squares, with a second 0.5px |
| 149 | // shifted row of squares below it. |
| 150 | auto drawEllipses = [&]() { |
| 151 | canvas->save(); |
| 152 | // Originally the SVG string "M5,14H0V9h5V14Z M1,13h3v-3H1V13Z" but that just specifies a |
| 153 | // 5px wide square outside a 3px wide square. |
| 154 | SkPath square; |
| 155 | square.addRect(SkRect::MakeLTRB(0.f, 9.f, 5.f, 14.f)); |
| 156 | square.addRect(SkRect::MakeLTRB(1.f, 10.f, 4.f, 13.f), SkPathDirection::kCCW); |
| 157 | |
| 158 | // From the bug, SVG viewbox was (0, 0, 24, 24), so the above coordinates are relative to |
| 159 | // that, but the svg was then the child of a div that was 16x16, so it's scaled down. This |
| 160 | // converts the 1px wide nested rects into subpixel nested rects. |
| 161 | canvas->scale(16.f / 24.f, 16.f / 24.f); |
| 162 | |
| 163 | SkPaint paint; |
| 164 | paint.setColor(SkColorSetARGB(255, 70, 70, 70)); |
| 165 | paint.setAntiAlias(true); |
| 166 | |
| 167 | // The original SVG drew 3 separate paths, but these were just translations of the original |
| 168 | // path baked into a path string. |
| 169 | canvas->drawPath(square, paint); |
| 170 | canvas->translate(10.f, 0.f); |
| 171 | canvas->drawPath(square, paint); |
| 172 | canvas->translate(10.f, 0.f); |
| 173 | canvas->drawPath(square, paint); |
| 174 | |
| 175 | canvas->restore(); |
| 176 | }; |
| 177 | |
| 178 | drawEllipses(); |
| 179 | canvas->translate(0.5f, 16.f); |
| 180 | drawEllipses(); |
| 181 | } |
| 182 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 183 | } // namespace skiagm |