blob: eb92d834fef3701baa5d4eb6ee833e74d8d508cd [file] [log] [blame]
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001/*
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 Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
bungemand3ebb482015-08-05 13:57:49 -070010#include "SkPath.h"
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000011#include "SkRandom.h"
12#include "SkRRect.h"
13
14namespace skiagm {
15
16// Test out various combinations of nested rects, ovals and rrects.
17class NestedGM : public GM {
18public:
robertphillips2e5b4c52015-05-19 04:35:38 -070019 NestedGM(bool doAA, bool flipped) : fDoAA(doAA), fFlipped(flipped) {
caryclark65cdba62015-06-15 06:51:08 -070020 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000021 }
22
23protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000024
mtklein36352bf2015-03-25 18:17:31 -070025 SkString onShortName() override {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000026 SkString name("nested");
robertphillips2e5b4c52015-05-19 04:35:38 -070027 if (fFlipped) {
28 name.append("_flipY");
29 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000030 if (fDoAA) {
31 name.append("_aa");
32 } else {
33 name.append("_bw");
34 }
35 return name;
36 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070039 return SkISize::Make(kImageWidth, kImageHeight);
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000040 }
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
mtklein36352bf2015-03-25 18:17:31 -070068 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000069
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.orge0e7cfe2013-09-09 20:09:12 +000082 SkRandom rand;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000083
84 for (int y = 0; y < kImageHeight; y += 10) {
85 for (int x = 0; x < kImageWidth; x += 10) {
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +000086 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x),
87 SkIntToScalar(y),
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000088 10, 10);
89 SkPaint p;
90 p.setColor(rand.nextU() | 0xFF000000);
91 canvas->drawRect(r, p);
92 }
93 }
94
robertphillips2e5b4c52015-05-19 04:35:38 -070095 SkScalar xOff = 2, yOff = 2;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000096 for (int outerShape = 0; outerShape < kShapeCount; ++outerShape) {
robertphillips@google.com8d3c6402013-08-20 12:11:31 +000097 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.comb74bdf02013-08-21 07:01:29 +0000102 AddShape(&path, innerRects[innerRect], (Shapes) innerShape,
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000103 SkPath::kCCW_Direction);
104
robertphillips2e5b4c52015-05-19 04:35:38 -0700105 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.com8d3c6402013-08-20 12:11:31 +0000113 canvas->drawPath(path, shapePaint);
robertphillips2e5b4c52015-05-19 04:35:38 -0700114 canvas->restore();
115
116 xOff += 45;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000117 }
118 }
robertphillips2e5b4c52015-05-19 04:35:38 -0700119
120 xOff = 2;
121 yOff += 45;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000122 }
123
124 }
125
126private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700127 static constexpr int kImageWidth = 269;
128 static constexpr int kImageHeight = 134;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000129
130 bool fDoAA;
robertphillips2e5b4c52015-05-19 04:35:38 -0700131 bool fFlipped;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000132
133 typedef GM INHERITED;
134};
135
136///////////////////////////////////////////////////////////////////////////////
137
robertphillips2e5b4c52015-05-19 04:35:38 -0700138DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ false); )
139DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ false); )
140DEF_GM( return new NestedGM(/* doAA = */ true, /* flipped = */ true); )
141DEF_GM( return new NestedGM(/* doAA = */ false, /* flipped = */ true); )
robertphillips@google.com8d3c6402013-08-20 12:11:31 +0000142
143}