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